Skip to content

Latest commit

 

History

History
930 lines (745 loc) · 38.7 KB

latex-example.org

File metadata and controls

930 lines (745 loc) · 38.7 KB

Org-mode Latex Export {{{NEWLINE}}} Example

1 Org and LaTeX exporter configuration

1.1 Basic configuration

1.2 Misc configuration

1.3 Tables related configuration

1.4 Index configuration

1.5 Graphics

1.6 Other

2 Front matter and version information

Make sure that you read this document in its raw source form. If you are looking at a rendered representation, e.g. in github, you will not be able to see the org source code.

  • Emacs version
    Emacs version: GNU Emacs 25.3.1 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9)
     of 2017-12-30
    org version: 9.1.9
        
  • LaTeX version
    pdfTeX 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian)
    kpathsea version 6.2.1
    Copyright 2015 Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
    There is NO warranty.  Redistribution of this software is
    covered by the terms of both the pdfTeX copyright and
    the Lesser GNU General Public License.
    For more information about these matters, see the file
    named COPYING and the pdfTeX source.
    Primary author of pdfTeX: Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
    Compiled with libpng 1.6.17; using libpng 1.6.17
    Compiled with zlib 1.2.8; using zlib 1.2.8
    Compiled with poppler version 0.41.0
        

3 Debugging

  • Org removes some of the intermediate files if the variable org-latex-remove-logfiles is set to true. So, for debugging, it makes sense to set it to nil. I have done this locally in this document’s local variables.
  • Use pdflatex with the option synctex=1. This option creates *.synctex.gz files which can be used by a viewer to jump to the respective text in the Tex file upon mouse clicking within the PDF. This is very useful to check the resulting LaTeX code when doing tests. Here is my own setting of the org-latex-pdf-process emacs configuration variable.
     (setq org-latex-pdf-process
    	   (let
    	       ((cmd (concat "pdflatex -shell-escape -interaction nonstopmode"
    			     " --synctex=1"
    			     " -output-directory %o %f")))
    	     (list cmd
    		   "cd %o; if test -r %b.idx; then makeindex %b.idx; fi"
    		   "cd %o; bibtex %b"
    		   cmd
    		   cmd)))
        

4 Major document elements

4.1 Equations

the following example uses escaped angular parentheses quoting \[...\]. This is short for putting the equation into a displaymath environment

\begin{displaymath}
...
\end{displaymath}

The rendered equation: \[coresextrapol = coresintern2013 ⋅ offl\% ⋅ \frac{gf ⋅ (volumeuser + volumeintern)}{volumeintern}\]

Note: You should not use the double dollar quoting $$...$$ (q.v. this stackexchange answer) and the deadly sins description.

Here follows an example for an equation embedded in the text. This is reached by either using the escaped round parentheses syntax \(...\) or the single dollar sign syntax $...$. $coresextrapol = coresintern2013 ⋅ offl\% ⋅ \frac{gf ⋅ (volumeuser + volumeintern)}{volumeintern}$ The text continues after the formula.

Here follows a numbered equation that also can be referenced like in the following parentheses (eq \ref{eq_1}). Note that we have to rely here on standard latex syntax, since org mode does not offer equations as a native element that we can mark up with #+NAME tags, etc. \begin{equation} \label{eq_1} coresextrapol = coresintern2013 ⋅ offl\% ⋅ \frac{gf ⋅ (volumeuser + volumeintern)}{volumeintern} \end{equation}

from an article by Stefaan Lippens on on using textnormal for including normal text correctly in a math environment.

\begin{eqnarray*} ∫_1^9 x dx & & \textrm{this is textrm}
∑_1^9 y & & \textsf{this is textsf}\ ∏_1^9 z & & \textnormal{this is textnormal} \end{eqnarray*}

Only textnormal will guarantee that the text appears in the default font of the document.

4.2 Figures

I can reference the figure like this: Fig. fig_simplegraph1.

Note

  • there must be no empty line between the picture’s link and the meta definitions for name, caption, etc.
  • The figure must have a caption if it is to be included in a list of figures (see \listoffigures statement at the end of the document), because the caption will trigger exporting to a LaTeX figure environment.
  • Without a caption, an image can still be exported as a floating image by using the :float option.
  • The OPTION tex:t must be set for references to work.
SpecifierPermission
hPlace the float here, i.e., approximately at the same point it occurs in the
source text (however, not exactly at the spot)
tPosition at the top of the page.
bPosition at the bottom of the page.
pPut on a special page for floats only.
\!Override internal parameters LaTeX uses for determining “good” float positions.
HPlaces the float at precisely the location in the LaTeX code. Requires the
float package, e.g., \textbackslash{}usepackage{float}. This is somewhat equivalent to h!.

../beamer/fig/simplegraph1.png

../beamer/fig/simplegraph1.png

../beamer/fig/simplegraph1.png

A pdf can be included the same way, e.g. by specifying

#+ATTR_LATEX: :options page=10 :width 10cm
[[file:myfig.pdf]]

4.2.1 inclusion of SVG graphics

q.v. my my plantuml example documentation.

4.3 Tables

4.3.1 nicer table formatting using booktab style

Some interesting tips for \myindex{booktab style tables} by M. Püschel.

Booktabs can be turned on by default for all tables by setting this variable for the document or globally:

org-latex-tables-booktabs: t

Whether table captions appear above or below the table can be configured using this variable setting:

org-latex-table-caption-above: nil
Column 1Column 2Column 3Column 4
1101001000
2111011001
3121021002
4131031003
5141041004
15605105010
Column 1Column 2Column 3Column 4
1101001000
2111011001
3121021002
4131031003
5141041004
15605105010

4.3.2 Math in tables

Use math or inline math together with array environment.

Here we use the simple math mode

Column 1Column 2
sin(x)tan(x)

This uses the inline-math mode

Column 1Column 2
sin(x)tan(x)

4.3.3 Table font size

The font size is determined by the :font switch in the #+ATTR_LATEX line.

Column 1Column 2
Some textSome other text
1020

Sidenote:

  • When a caption is used, the latex export uses a table environment.
  • The previous captionless table generates a tabular environment.
Column 1Column 2
Some textSome other text
1020
Column 1Column 2
Some textSome other text
1020
Column 1Column 2
Some textSome other text
1020
Column 1Column 2
Some textSome other text
1020

4.3.4 Sidewaystable

Use the float: sideways ATTR option (The float: sidewaystable= has been deprecated since Org 8.3, q.v. info:org#Tables in LaTeX export) Using the sidewaystable together with a :placement [H] specifier requires that the rotfloat package is loaded.

Column 1Column 2Column 3Column 4Column 5Column 6
1101001000exampleresult
2111011001exampleresult
3121021002exampleresult
4131031003exampleresult
5141041004exampleresult
6151051005exampleresult
7161061006exampleresult

This text comes after the sidewaystable (we want to check whether the placement modifier was observed).

Even though in the info documentation it reads: “Note: :placement is ignored for :float sideways tables.”, the modifier [H] is observed, as can be confirmed in the resulting TeX code.

4.3.5 Table over multiple pages with long text wrapped to cell width

Use the \myindex{longtabu} environment. This requires that you have loaded the \myindex{tabu} and \myindex{longtable} packages.

100Some extremely long sentence which surely needs a linebreak if I add some additional words like these
101Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
102bla bla
103repetition ahead
100Some extremely long sentence which surely needs a linebreak if I add some additional words like these
101Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
102bla bla
103repetition ahead
100Some extremely long sentence which surely needs a linebreak if I add some additional words like these
101Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
102bla bla
103repetition ahead
100Some extremely long sentence which surely needs a linebreak if I add some additional words like these
101Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
102bla bla
103repetition ahead
100Some extremely long sentence which surely needs a linebreak if I add some additional words like these
101Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
102bla bla
103repetition ahead
100Some extremely long sentence which surely needs a linebreak if I add some additional words like these
101Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
102bla bla
103repetition ahead
100Some extremely long sentence which surely needs a linebreak if I add some additional words like these
101Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
102bla bla
103repetition ahead
100Some extremely long sentence which surely needs a linebreak if I add some additional words like these
101Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
102bla bla
100Some extremely long sentence which surely needs a linebreak if I add some additional words like these
98Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
96bla bla
94repetition ahead
92Some extremely long sentence which surely needs a linebreak if I add some additional words like these
90Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
88bla bla
86repetition ahead
84Some extremely long sentence which surely needs a linebreak if I add some additional words like these
82Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
80bla bla
78repetition ahead
76Some extremely long sentence which surely needs a linebreak if I add some additional words like these
74Some other extremely long sentence which surely needs a linebreak if I add some additional words like these
72bla bla

4.3.6 Tables with colored rows using xcolors and colortbl

A very nice reference for color in tables is proviced by Xavier on the texblog.org: https://texblog.org/2018/01/15/color-table-series-part-2-xcolor-package/

One can use the \rowcolors command to define coloring of alternating rows. In front of the table use the following

#+LATEX: \rowcolors[]{2}{blue!10}{blue!25}

The arguments translate to

  • [2] start coloring in the second row
  • {blue!10}{blue!25} definition of the two colors for odd and even rows according to the xcolors package

In order to prevent spillover of the rowcolors definition into later table, I wrap the whole table in #+LATEX: { and #+LATEX: } definitions.

Column 1Column 2Column 3Column 4Column 5Column 6
1101001000exampleresult
2111011001exampleresult
3121021002exampleresult
4131031003exampleresult
5141041004exampleresult
6151051005exampleresult
7161061006exampleresult

4.3.7 TODO Tables with colored rows using tabu

This needs the tabu and xcolor packages to be loaded. Use the LaTeX command \taburowcolors to define the colors right before the table.

#+LATEX: \taburowcolors[2]2{lightgray..white}

The options in this command translate to

  • [2] start coloring in 2nd row
  • 2 : use 2 colors (so, if set to 2 then it is just alternating)
  • {lightgray..white} defines the first and last color in the color range. This is a color series definition provided by the xcolor package.
Column 1Column 2Column 3Column 4Column 5Column 6
1101001000exampleresult
2111011001exampleresult
3121021002exampleresult
4131031003exampleresult
5141041004exampleresult
6151051005exampleresult
7161061006exampleresult

Booktabs style does not mix well with this. The caption is too near to the table ruler, so here some work would need to be invested to get an aesthetically pleasing layout.

Column 1Column 2Column 3Column 4Column 5Column 6
1101001000exampleresult
2111011001exampleresult
3121021002exampleresult
4131031003exampleresult
5141041004exampleresult
6151051005exampleresult
7161061006exampleresult

Here, for comparison, a colored table produced by an inline LaTeX fragment. Q.v. this stackexchange discussion to understand the color series.

Note: I need to do some more testing to get a better understanding of how the color ranges are defined. E.g. here in the 5th row there is suddenly a yellow color pouring in.

4.3.8 Radio tables and skipping columns and rows

Radio tables allow to send a modified representation of an org source table to a target location. Here, we send the same table to the 2 target locations below, once using a send function to LaTeX and once to an org function.

This can be useful e.g. if one wants to skip some columns that are only used for intermediate results.

First test: Exporting to a native LaTeX table

Second test: Exporting to an org mode table

Monthitemsitems per day
Jan552.4
Feb160.8
March27812.6

4.4 Source code

In order to get nice source code formatting and markup, one needs to add the minted package. I add here the relevant excerpt from my emacs initialization file (listing listing-minted-config), which also serves as a first lisp code example

TODO: I was not yet able to figure out how to force org to place the source code listing exactly here in the text. While the documentation accepts a :float t attribute (and every source block with a caption automatically becomes a float), the placement modifier seems not to get translated

#+ATTR_LaTeX: :float t :placement [H]
(eval-after-load "ox-latex"
  '(progn 
     ;; we want source code blocks to be syntax colored when exporting
     ;; via latex.  We configure latex minted which uses python
     ;; pygments
     (add-to-list 'org-latex-packages-alist '("" "minted"))
     (setq org-latex-listings 'minted)
     ;; define mappings of src-code-language to lexer that minted shall use
     ;;(add-to-list 'org-latex-listings-langs '(ipython "Python"))
     (add-to-list 'org-latex-minted-langs '(ipython "python"))))

I also add listing listing-example-c as an example for C code markup:

#include "stdlib.h"
int main(int argc,char **argv) {
  printf("Hello World");
  exit(0);
}

5 Text features

5.1 Text font size

Text Example Huge

Text Example huge

Text Example LARGE

Text Example Large

Text Example large

Text Example normalsize

Text Example small

Text Example footnotesize

Text Example scriptsize

Text Example tiny

5.2 Footnotes and margin notes

Examples for \myindex{footnotes}: This is a text with a \myindex{footnote} 1. The footnote will be displayed on the bottom of the current page. One can also place all footnotes in a separate chapter called footnotes at the end of the org file2.

Footnotes definitions can be placed within an org section using the [fn:1] syntax and observing that no leading indentation is allowed on such a line. Alternatively the footnotes can be collected in a special section called “Footnotes”. I recommend reading the respective INFO entry (e.g. there is also the possibility to define footnotes inline). When using C-c C-x f to insert footnotes a lot of the work is taken over by org itself (also allows footnote renumbering, etc.). One can jump between the footnote reference and its definition by the usual C-c C-o combination.

\myindex{Margin notes} can be inserted by directly inlining the LaTeX command

as demonstrated in the source code for this section. By default the margin notes are justified. This often looks awkward. Using this stackexchange answer, I define a macro which yields:

I like the margin notes to be left aligned instead of being justified.

5.3 References to sections, figures, tables, equations

Here, we show the usage of links to the text sections: Examples for References to figures are also found in chapter Figures, to tables in chapter Tables, and to equations in chapter Equations.

Other references

  • Figures can be referenced like this: Fig. fig_simplegraph1.
  • These are references to table tblDefault and table tblBooktabs.
  • And an example of an equation reference: eq \ref{eq_1}. This reference requires latex syntax and a latex label as target. All the other links work based on org link syntax can use the name given to the elements via a leading #+NAME: line.

5.4 TODO Fontifying SRC BLOCK results

One can use an org source block that contains a named source block and the matching named results tag, and then put latex instructions inside as shown in this example (thanks for the idea to a mail from Eric S Fraga)

6 some interesting links

7 Index creation

Must be solved by including LaTeX source commands:

  • Requires in the preamble
    • \usepackage{makeidx}
    • \makeindex
  • Mark up words by \index{word}
  • At the location where the index should apear, use \printindex
  • to render the document, a call to the makeindex binary needs to be added in the build command. I use the following definition in my init.el.
    (setq org-latex-pdf-process
          (let
              ((cmd (concat "pdflatex -shell-escape -interaction nonstopmode"
                            " -output-directory %o %f")))
            (list cmd
                  "cd %o; if test -r %b.idx; then makeindex %b.idx; fi"
                  cmd
                  cmd)))
        

8 References

Some important org references that also display that citations directly following each other will be combined cite:schulte2012multi cite:dominik2010org. And another single reference cite:feichtinger1997direct.

The #+BIBLIOGRAPHY: command inserts the reference list at the location where it is placed. It requires the name of the bib-file (without .bib extension) and the name of a style (e.g. plain).

If no citations are created, make sure that the bibliography file is really found. You may have to run “bibtex myfile.aux” yourself, and for that you need to make sure that the AUX file is not removed after org runs (q.v. *Debugging section).

For HTML exports one can also pass options to the bibtex2html binary (look at the comments section of ox-bibtex.el and also the bibtex2html man page).

optionfunctionality
-dsort by date
-asort as BibTeX (usually by author) default
-uunsorted i.e. same order as in .bib file
-rreverse the sort
-tlimit to entries cited in document

Multiple options can be combined as follows:

option:-d option:-r 

To get the citations correctly processed rendered, one needs to add a bibtex invocation to the LaTeX command chain:

(setq org-latex-pdf-process
      (let
          ((cmd (concat "pdflatex -shell-escape -interaction nonstopmode"
                        " --synctex=1"
                        " -output-directory %o %f")))
        (list cmd
              "cd %o; if test -r %b.idx; then makeindex %b.idx; fi"
              "cd %o; bibtex %b"
              cmd
              cmd)))

To just produce a bibliography of all items in the bib file, one can use the following LaTeX snippet. The \nocite{*} command includes an item that has not been cited in the document; a star matches all documents, so all get included (q.v. this link).

#+BEGIN_LATEX
  \documentstyle{amsart}
  \begin{document}
  \nocite{*}
  \bibliographystyle{amsplain}
  \bibliography{bib-filename}
  \end{document}
#+END_LATEX

9 Indexes and tables of contents

10 Footnotes

1 This is the footnote text

2 this is another footnote