# Greek characters in graphs produced by org-babel and gnuplot

I can generate plots using gnuplot source blocks with org-babel like the following:

#+begin_src gnuplot :file plot.png
plot x   w l t "$\epsilon$ = 1", \
1-x w l t "ε = 0.1"
#+end_src


which I then export to HTML using:

(org-html-export-to-html)


My problem is that I would like to put greek characters in the axis labels. As demonstrated in my example, either using a LaTeX formula or an UTF-8 character would be fine for my immediate use (although using LaTeX formulas would be more general and thus better in the long run).

Is there anything I can set-up so that org-babel can export plot with nicely rendered greek characters?

• If you run the same gnuplot snippet directly in gnuplot (ie, without going through org-mode), are you able to plot the Greek characters? – Dan Nov 3 '14 at 12:41
• the utf-8 version works, depending on the terminal I use. With the default interactive wxt terminal, it works, as well as with the pngcairo terminal. With the png terminal, it doesn't work though (I guess it is what org-babel uses behind the scenes by default for png export). – Francesco Nov 3 '14 at 12:58
• as for support of full LaTeX formulas, I know of no easy way to do it (inside or outside 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 by org-babel. – Francesco Nov 3 '14 at 13:04
• I don't know much about gnuplot or its backends, but from your description, it sounds like the issue is there rather than in org-mode. If you're willing to invest the time, you might consider R, which has strong plotting facilities and can handle LaTeX formulas in plots. – Dan Nov 3 '14 at 13:10
• Regarding "...org-babel adds a set terminal png command before my block, and I would like to know how to change it.": use a :file header argument with a different file extension or add a ":term" header argument, for example ":term pdfcairo". (I tried exporting your source code block after M-x debug-on-entry for org-babel-execute:gnuplot.) – Constantine Nov 3 '14 at 17:19

#+begin_src gnuplot :file plot.png :term pngcairo
plot x   w l t "$\epsilon$ = 1", \
1-x w l t "ε = 0.1"
#+end_src


seems to work with labels set using Unicode characters, although this feature is not documented. I discovered it by reading ob-gnuplot.el and using the elisp debugger.

• Nice. By the way, @Francesco, this tutorial suggests you might be able to get LaTeX functionality with set terminal latex. Not sure if you can set the terminal in 2 ways (as with this answer's term pngcairo) or if term latex gets you everything you need. – Dan Nov 3 '14 at 19:21
• @Constantine thanks, that's it. Reading ob-gnuplot further, I also discovered one can use the *org-babel-gnuplot-terms* variable to set the default terminal for a given file extension. For example: (push '(png . "pngcairo") *org-babel-gnuplot-terms*) – Francesco Nov 3 '14 at 20:47
• @Dan yes, using the latex terminal is one way to have LaTeX-formatted axis labels. It produces a .tex file, though, which you have to compile to produce a PDF of PostScript file, which you then have to convert to a PNG if that is what you want. I still think one could automate the whole process in org-babel, but it looks like ob-gnuplot currently very much relies on there being only one gnuplot command needed to generate the figure. – Francesco Nov 3 '14 at 20:54

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)))
: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