Playing a bit more with the code in ob-gnuplot.el
, I came up with the following advising function, which allows inserting custom preambles and postambles according to the output file extension:
(defvar *org-babel-gnuplot-preambles* nil
"Alist of file extensions and the associated gnuplot preambles.
Preambles will be added in front of the block body.")
(defvar *org-babel-gnuplot-postambles* nil
"Alist of file extensions and the associated gnuplot postambles.
Postambles will be added at the very end of the generated gnuplot
script, after the `:epilogue'")
(defun my/org-babel-expand-body:gnuplot (orig &rest args)
(let* ((body (nth 0 args))
(params (nth 1 args))
(epilogue (cdr (assoc :epilogue params)))
(out-file (cdr (assoc :file params)))
(size (or (cdr (assoc :size params)) "640x480"))
(replace-vars (lambda (str)
(setq str (replace-regexp-in-string "\\${output}" (or out-file "") str))
(setq str (replace-regexp-in-string "\\${size}" (or size "") str))
str))
(get-var-by-ext (lambda (alist)
(funcall replace-vars
(or
(when out-file
(let ((ext (file-name-extension out-file)))
(cdr (assoc (intern (downcase ext))
alist))))
""))))
(preamble (funcall get-var-by-ext *org-babel-gnuplot-preambles*))
(postamble (funcall get-var-by-ext *org-babel-gnuplot-postambles*))
(new-params (cons `(:epilogue . ,(concat epilogue "\n" postamble)) params))
(new-body (concat preamble "\n" body)))
(funcall orig new-body new-params)))
(advice-add 'org-babel-expand-body:gnuplot
:around #'my/org-babel-expand-body:gnuplot)
By means of gnuplot's !
operator (which executes arbitrary shell commands), this allows to set up complex workflows for generating a given image type.
For example, with the following configuration:
(setq *org-babel-gnuplot-preambles* '((png . "
set terminal epslatex standalone color colortext 10
set output '/tmp/plot.tex'")))
(setq *org-babel-gnuplot-postambles* '((png . "
! latex /tmp/plot.tex
! dvipdf /tmp/plot.dvi
! convert -density 600 -resize ${size} plot.pdf ${output}")))
it is possible to include full LaTeX formulas in an org-src block and still generate a PNG file for the HTML export:
#+begin_src gnuplot :file plot.png :exports both :session none
set title '$\displaystyle I = \int_0^x f_\varepsilon(x^\prime) \; dx^\prime$'
set xlabel '$x$'
set ylabel '$I$'
plot x w l t '$\varepsilon$ = 1', \
1-x w l t '$\varepsilon$ = 0.1'
#+end_src

org-mode
), are you able to plot the Greek characters?wxt
terminal, it works, as well as with thepngcairo
terminal. With thepng
terminal, it doesn't work though (I guess it is whatorg-babel
uses behind the scenes by default for png export).org-babel
). The only way I know involves several steps, producing intermediate plots and converting them. But I don't see why this could not be automated byorg-babel
.org-mode
. If you're willing to invest the time, you might considerR
, which has strong plotting facilities and can handle LaTeX formulas in plots.