13.10.8 Source blocks in LaTeX export

LaTeX export backend provides multiple ways to render src blocks in LaTeX, according to the value of org-latex-src-block-backend. The default value ‘verbatim’ renders the src code verbatim, without any extra styling. Alternative values allow more colorful styling, but require additional LaTeX (‘listings’, ‘minted’), system (‘minted’), or Emacs (‘engraved’) packages. See the org-latex-src-block-backend docstring for more details.

The LaTeX export backend can make source code blocks into floating objects through the attributes ‘:float’ and ‘:options’. For ‘:float’:

t

Makes a source block float; by default floats any source block with a caption.

multicolumn

Spans the source block across multiple columns of a page.

nil

Avoids a ‘:float’ even if using a caption; useful for source code blocks that may not fit on a page.

#+ATTR_LATEX: :float nil
#+BEGIN_SRC emacs-lisp
  Lisp code that may not fit in a single page.
#+END_SRC

The LaTeX export backend passes string values in ‘:options’ to LaTeX packages for customization of that specific source block. In the example below, the ‘:options’ are set for Engraved or Minted. Minted is a source code highlighting LaTeX package with many configurable options139. Both Minted and Engraved are built on fvextra, and so support many of the same options.

#+ATTR_LATEX: :options mathescape
#+BEGIN_SRC emacs-lisp
  (defun Fib (n) ; $n_i = n_{i-2} + n_{i-1}$
    (if (< n 2) n (+ (Fib (- n 1)) (Fib (- n 2)))))
#+END_SRC

To apply similar configuration options for all source blocks in a file, use the org-latex-listings-options, org-latex-engraved-options, and org-latex-minted-options variables.


Footnotes

(139)

Minted uses an external Python package for code highlighting, which requires the flag ‘-shell-escape’ to be added to org-latex-pdf-process.