1 # This file is released by its authors and contributors under the GNU
2 # Free Documentation license v1.3 or later, code examples are released
3 # under the GNU General Public License v3 or later.
7 Org mode is used (among many other uses) by scientists taking
8 notes. Sometimes these notes include mathematics and scientists almost
9 universally use LaTeX in order to write mathematics, but since LaTeX
10 is not WYSIWYG, it is often useful to provide a preview of what
11 something will look like when typeset.
13 Most scientists use AUCTeX to write their LaTeX papers and there is a
14 LaTeX preview add-on that provides a preview facility in AUCTeX.
16 Org mode provides a similar facility, whose essentials I try to
17 summarize in this note.
21 Once the facility has been set up, it can be used very simply. One
22 writes LaTeX code and invokes the command =org-preview-latex-fragment=
23 (bound to C-c C-x C-l). The command processes the latex code and
24 produces a PNG image that is overlaid on the LaTeX code that was used
25 to produce the image, thereby providing the required preview. Pressing
26 C-c C-c gets rid of the overlay.
36 \int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
40 If you press C-c C-x C-l here (or C-u C-c C-x C-l to preview
41 everything in the subtree, or C-u C-u C-c C-x C-l to preview
42 everything in the buffer), you should see something like this (the
43 placement on the page and the size of the equations are probably going
44 to be different, but the formulas should be recognizably the same):
51 \int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
54 Generally speaking, the preview mechanism looks for LaTeX environments
55 in the current region of applicability and processes one or more of
56 them. Prefix arguments and position in the buffer affect the region of
57 applicability of the preview mechanism --- the docstring of the
58 function says it better than I possibly could:
61 Preview the LaTeX fragment at point, or all locally or globally.
63 If the cursor is on a LaTeX fragment, create the image and overlay
64 it over the source code, if there is none. Remove it otherwise.
65 If there is no fragment at point, display all fragments in the
68 With prefix ARG, preview or clear image for all fragments in the
69 current subtree or in the whole buffer when used before the first
70 headline. With a double prefix ARG C-u C-u preview or clear images
71 for all fragments in the buffer.
74 Mathematics is the most common application, but other environments (e.g.
75 a listing environment for some code fragment) also work.
79 There are two methods that can be used for LaTeX preview: dvipng and
82 In the dvipng method, the latex fragment is embedded in a complete LaTeX
83 file, which is then processed by the LaTeX program (``latex'') to
84 produce a DVI file. The ``dvipng'' program is then invoked on the DVI
85 file to produce the final PNG-format image.
87 In the imagemagick method, the same LaTeX file is run through whatever
88 LaTeX processor you have configured in the =org-latex-pdf-process=
89 variable (the default is three runs of pdflatex). The output is a PDF
90 file, which is then processed by the ``convert'' program (a member of
91 the imagemagick family, hence the name of the method) to produce the
94 The two methods produce slightly different output: the foreground and
95 background colors may differ.
98 ** Obtaining and testing the =dvipng= program
99 You need the dvipng program. On Linux, it's generally in its own package,
100 not part of the larger TeX packages. For example, on Ubuntu, it can be
101 installed with the command =apt-get install dvipng=.
103 To test the installation, create a small TeX file:
106 #+BEGIN_SRC latex :tangle /tmp/example.tex
107 \documentclass{article}
116 \int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
124 If you are looking at this on the web, you can cut and paste the latex
125 program to /tmp/example.tex and process it through latex and dvipng,
126 as shown below. If you are looking at the org file of this tutorial,
127 you can tangle the above code block with C-u C-c C-v C-t into the file
128 /tmp/example.tex and process that through latex and dvipng. The
129 following code uses the ``display'' program (part of the imagemagick
130 family) to show the PNG file, but there are many such programs. Use
131 what you have or can get easily.
133 #+BEGIN_SRC sh :exports code :results silent
135 latex /tmp/example.tex
136 dvipng -o /tmp/example.png /tmp/example.dvi
137 display /tmp/example.png
140 Don't worry about how it looks: if there are no errors, everything is
141 fine. In actual use, the Org-mode LaTeX previewer will call =dvipng= with
142 appropriate arguments for its nefarious purposes.
145 There is one variable in org that needs to be set up:
148 (setq org-latex-create-formula-image-program 'dvipng)
151 In addition, you might want to customize the set of latex
152 packages that will be needed in order to produce the preview.
153 Most of these are probably already included. Here is the current
154 (as of [2013-07-05 Fri]) list. You might have more than this
155 because of customizations in your setup. You also might have
156 less than this but that's not likely:
159 \usepackage[usenames]{color}
161 \usepackage[mathscr]{eucal}
162 \usepackage[utf8]{inputenc}
163 \usepackage[T1]{fontenc}
164 % Package fixltx2e omitted
165 \usepackage{graphicx}
166 % Package longtable omitted
167 % Package float omitted
168 % Package wrapfig omitted
169 \usepackage[normalem]{ulem}
170 \usepackage{textcomp}
171 \usepackage{marvosym}
173 \usepackage{latexsym}
175 % Package amstext omitted
176 % Package hyperref omitted
180 The set of packages is specified using the variables
181 =org-latex-default-packages-alist= and =org-latex-packages-alist=. The
182 documentation strongly recommends that you leave the first one as is
183 (unless you really know what you are doing - and even then, it's easy
184 to shoot yourself in the foot). The second one is normally empty, but
185 you can use it to add whatever packages are necessary for your line of
186 work: Feynman diagrams anybody?
188 N.B. With the exception of the setting for
189 =org-latex-create-formula-image-program=, everything else applies to
190 the imagemagick case as well. In fact, it applies not only to latex
191 preview but to latex export in general. Because of this generality,
192 you might find yourself adding packages for latex export that lead
193 to trouble with latex preview. An example is shown below.
195 Normally, all of the packages above are included for latex export,
196 but as you can see, some are omitted for latex preview. The mechanism
197 for that is explained below as well.
201 The imagemagick setup mirrors the dvipng setup almost exactly. You
202 have to install the imagemagick package on your system somehow.
204 The test file above can be used to test this setup as well. The
205 commands needed are different though:
207 #+BEGIN_SRC sh :results silent
209 pdflatex /tmp/example.tex
210 convert /tmp/example.pdf /tmp/example.png
211 display /tmp/example.png
214 The necessary org setup is now
217 (setq org-latex-create-formula-image-program 'imagemagick)
220 The package stuff is identical.
222 * Comparing and contrasting the two methods
224 So choose a method, add a LaTeX fragment of your favorite
233 and press C-c C-x C-l in the fragment. Does the preview show
234 properly? If so, congratulations. Press C-c C-c to make it go away.
236 If you are so inclined, switch to the other method by changing the
237 value of =org-latex-create-formula-image-program= and try the preview
238 again. You will have to delete the PNG file produced by the first
239 method, in order to force the new method to create it again. The image
240 files are created in the directory specified by the variable
241 =org-latex-preview-ltxpng-directory= --- by default, that directory is
242 a subdirectory, named ``ltxpng'', of the directory where the org file
245 ** Background and foreground colors
246 One difference in the two methods is that the foreground and background
247 colors may come out different. In my case, I don't do anything special
248 to specify them and I'm using a (mostly) green-foreground-on-black-background
249 emacs. The dvipng preview comes out as black-on-white and the imagemagick
250 preview comes out as green-on-white in my case. Depending on your "theme"
251 and other magic, YMMV. I don't have much insight into these aspects however,
252 so feel free to modify this paragraph for the sake of better accuracy.
255 Programmers often find themselves in the position of having to include
256 code fragments (or perhaps complete programs) in documentation. LaTeX
257 makes that fairly easy. There are two packages contending for the
258 championship: listings and minted. Many people prefer the latter
259 because they think the output looks better, but there is a price to be
260 paid: minted depends on a separate python program, whereas listings is
261 a pure LaTeX package.
263 The problem is that, by default, latex (as well as its siblings tex,
264 pdflatex, etc.) refuses to process a file that uses the minted
265 package because of the necessity of running this separate program. It
266 is considered a security risk. There are various ways to convince
267 latex to abandon its scruples in this regard. The most convenient
268 (and therefore most commonly used) is to run it with the
269 =-shell-escape= option.
271 So, let's assume that you like minted over listings and have decided
272 to use it for latex export. You read the documentation for the relevant
273 variable, =org-latex-listings=, and set up things as the documentation
277 (setq org-latex-listings 'minted)
279 (add-to-list 'org-latex-packages-alist '("" "minted"))
282 You also have to make sure that =org-latex-pdf-process= calls
283 latex (or pdflatex or xelatex or ...) with the =-shell-escape=
286 You *export* your articles to LaTeX and produce output that includes
287 very nicely typeset program code fragments. Months later, you try to
288 *preview* some mathematics and the preview does not work. What
291 Adding the minted package to =org-latex-packages-alist= as above, adds
292 the =\usepackage{minted}= line to latex files produced by either latex
293 export *or* latex preview. If you've set up preview to use the
294 imagemagick method, then everything should work, because it uses
295 =org-latex-pdf-process= and that has been set up already to use the
296 =-shell-escape= option (otherwise export would not work), so using
297 minted is not a problem.
299 But if you have set up preview to use the dvipng method, you are in
300 trouble: that method calls latex directly, the call is hardwired
301 and it does *not* use the =-shell-escape= option. LaTeX refuses to
302 run the external program that minted used, no DVI file is produced
303 and dvipng cannot produce a PNG file.
305 One way out of this predicament is to change the latex call. That
306 requires modifying the org mode code and that's not really a good
307 idea. Adding an option that can be set by the user (so that one can
308 pass the =-shell-escape= option to the latex call) is under
309 consideration at this time ([2013-07-05]), but it's not there yet.
311 Probably the best solution currently is to include minted for latex export
312 but exclude it for latex preview. Remember the list of packages
313 above, where some packages were omitted? Those packages are
314 included by default for latex export, but excluded for latex preview.
315 The magic that allows that is explained in the documentation
316 for =org-latex-packages-alist=:
319 Each cell is of the format:
321 ("options" "package" snippet-flag)
323 SNIPPET-FLAG, when t, indicates that this package is also needed
324 when turning LaTeX snippets into images for inclusion into
328 Note that, counter-intuitively, if the snippet-flag is omitted
329 altogether, then it's as if it were set to t, not nil as one would
330 normally expect. Presumably that's for backward compatibility: the
331 snippet-flag was added later and many people might have settings that
332 don't include it. If its absence were to be interpreted as nil, the
333 packages would not be included for latex preview, leading to
336 To exclude minted from latex preview then, all we have to do is change
337 what we add to =org-latex-packages-alist=:
340 (add-to-list 'org-latex-packages-alist '("" "minted" nil))
343 There still remains one problem: what does one do in order to include
344 a code fragment into a preview, but wants that code fragment to be
345 processed by minted? The best suggestion currently is: if you have to
346 do that, use the imagemagick method, not the dvipng method.
348 The dvipng method predates the imagemagick method, but maybe the
349 problem with minted above, esoteric as it may be, provides motivation
350 to use the imagemagick method instead for latex preview --- it is a
351 bit less brittle than the dvipng method.