ox-ascii/ox-latex/ox-html: Make use of optional title in toc
[org-mode.git] / lisp / ox-latex.el
blob116bbeabd38d4893d26e8744c0263a6983fb8da7
1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a LaTeX back-end for Org generic exporter.
25 ;; Depending on the desired output format, three commands are provided
26 ;; for export: `org-latex-export-as-latex' (temporary buffer),
27 ;; `org-latex-export-to-latex' ("tex" file) and
28 ;; `org-latex-export-to-pdf' ("pdf" file). Also, two publishing
29 ;; functions are available: `org-latex-publish-to-latex' and
30 ;; `org-latex-publish-to-pdf'.
32 ;; The library introduces three new buffer keywords: "LATEX_CLASS",
33 ;; "LATEX_CLASS_OPTIONS" and "LATEX_HEADER", and a new OPTIONS item:
34 ;; "textht".
36 ;; Table export can be controlled with a number of attributes (through
37 ;; ATTR_LATEX keyword).
39 ;; - The main one is the `:mode' attribute, which can be set to
40 ;; `table', `math', `inline-math' and `verbatim'. In particular,
41 ;; when in `math' or `inline-math' mode, every cell is exported
42 ;; as-is, horizontal rules are ignored and the table will be wrapped
43 ;; in a math environment. Also, contiguous tables sharing the same
44 ;; math mode will be wrapped within the same environment. Default
45 ;; mode is stored in `org-latex-default-table-mode'.
47 ;; - The second most important attribute is `:environment'. It is the
48 ;; environment used for the table and defaults to
49 ;; `org-latex-default-table-environment' value. It can be set to
50 ;; anything, including "tabularx", "longtable", "array",
51 ;; "bmatrix"...
53 ;; - `:float' attribute defines a float environment for the table.
54 ;; Possible values are `sidewaystable', `multicolumn' and `table'.
55 ;; If unspecified, a table with a caption will have a `table'
56 ;; environment. Moreover, `:placement' attribute can specify the
57 ;; positioning of the float
59 ;; - `:align', `:font' and `:width' attributes set, respectively, the
60 ;; alignment string of the table, its font size and its width. They
61 ;; only apply on regular tables.
63 ;; - `:booktabs', `:center' and `:rmlines' values are booleans. They
64 ;; toggle, respectively "booktabs" usage (assuming the package is
65 ;; properly loaded), table centering and removal of every horizontal
66 ;; rule but the first one (in a "table.el" table only).
68 ;; - `:math-prefix', `:math-suffix' and `:math-arguments' are string
69 ;; which will be inserted, respectively, before the table within the
70 ;; math environment, after the table within the math environment,
71 ;; and between the macro name and the contents of the table. The
72 ;; latter attribute is necessary to matrix macros that require more
73 ;; than one argument (i.e. "qbordermatrix").
75 ;; Plain lists accept two optional attributes: `:environment' and
76 ;; `:options'. The first one allows to use a non-standard environment
77 ;; (i.e. "inparaenum"). The second one allows to specify optional
78 ;; arguments for that environment (square brackets are not mandatory).
80 ;; Images accept `:float', `:placement', `:comment-include', and
81 ;; `:options' as attributes. `:float' accepts a symbol among `wrap',
82 ;; `multicolumn', and `figure', which defines the float environment
83 ;; for the table (if unspecified, an image with a caption will be set
84 ;; in a "figure" environment). `:comment-include' is a boolean that
85 ;; toggles whether to comment out the \includegraphics
86 ;; call. `:placement' is a string that will be used as argument for
87 ;; the environment chosen. `:options' is a string that will be used
88 ;; as the optional argument for "includegraphics" macro.
90 ;; Special blocks accept `:options' as attribute. Its value will be
91 ;; appended as-is to the opening string of the environment created.
93 ;; This back-end also offers enhanced support for footnotes. Thus, it
94 ;; handles nested footnotes, footnotes in tables and footnotes in item
95 ;; descriptions.
97 ;;; Code:
99 (eval-when-compile (require 'cl))
100 (require 'ox)
101 (require 'ox-publish)
103 (defvar org-latex-default-packages-alist)
104 (defvar org-latex-packages-alist)
105 (defvar orgtbl-exp-regexp)
109 ;;; Define Back-End
111 (org-export-define-backend latex
112 ((bold . org-latex-bold)
113 (center-block . org-latex-center-block)
114 (clock . org-latex-clock)
115 (code . org-latex-code)
116 (comment . (lambda (&rest args) ""))
117 (comment-block . (lambda (&rest args) ""))
118 (drawer . org-latex-drawer)
119 (dynamic-block . org-latex-dynamic-block)
120 (entity . org-latex-entity)
121 (example-block . org-latex-example-block)
122 (export-block . org-latex-export-block)
123 (export-snippet . org-latex-export-snippet)
124 (fixed-width . org-latex-fixed-width)
125 (footnote-definition . org-latex-footnote-definition)
126 (footnote-reference . org-latex-footnote-reference)
127 (headline . org-latex-headline)
128 (horizontal-rule . org-latex-horizontal-rule)
129 (inline-src-block . org-latex-inline-src-block)
130 (inlinetask . org-latex-inlinetask)
131 (italic . org-latex-italic)
132 (item . org-latex-item)
133 (keyword . org-latex-keyword)
134 (latex-environment . org-latex-latex-environment)
135 (latex-fragment . org-latex-latex-fragment)
136 (line-break . org-latex-line-break)
137 (link . org-latex-link)
138 (paragraph . org-latex-paragraph)
139 (plain-list . org-latex-plain-list)
140 (plain-text . org-latex-plain-text)
141 (planning . org-latex-planning)
142 (property-drawer . (lambda (&rest args) ""))
143 (quote-block . org-latex-quote-block)
144 (quote-section . org-latex-quote-section)
145 (radio-target . org-latex-radio-target)
146 (section . org-latex-section)
147 (special-block . org-latex-special-block)
148 (src-block . org-latex-src-block)
149 (statistics-cookie . org-latex-statistics-cookie)
150 (strike-through . org-latex-strike-through)
151 (subscript . org-latex-subscript)
152 (superscript . org-latex-superscript)
153 (table . org-latex-table)
154 (table-cell . org-latex-table-cell)
155 (table-row . org-latex-table-row)
156 (target . org-latex-target)
157 (template . org-latex-template)
158 (timestamp . org-latex-timestamp)
159 (underline . org-latex-underline)
160 (verbatim . org-latex-verbatim)
161 (verse-block . org-latex-verse-block))
162 :export-block ("LATEX" "TEX")
163 :menu-entry
164 (?l "Export to LaTeX"
165 ((?L "As LaTeX buffer" org-latex-export-as-latex)
166 (?l "As LaTeX file" org-latex-export-to-latex)
167 (?p "As PDF file" org-latex-export-to-pdf)
168 (?o "As PDF file and open"
169 (lambda (a s v b)
170 (if a (org-latex-export-to-pdf t s v b)
171 (org-open-file (org-latex-export-to-pdf nil s v b)))))))
172 :options-alist ((:date "DATE" nil "\\today" t)
173 (:date-format nil nil org-latex-date-timestamp-format)
174 (:latex-class "LATEX_CLASS" nil org-latex-default-class t)
175 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
176 (:latex-header-extra "LATEX_HEADER" nil nil newline)
177 (:latex-hyperref-p nil "texht" org-latex-with-hyperref t)))
181 ;;; Internal Variables
183 (defconst org-latex-babel-language-alist
184 '(("af" . "afrikaans")
185 ("bg" . "bulgarian")
186 ("bt-br" . "brazilian")
187 ("ca" . "catalan")
188 ("cs" . "czech")
189 ("cy" . "welsh")
190 ("da" . "danish")
191 ("de" . "germanb")
192 ("de-at" . "naustrian")
193 ("de-de" . "ngerman")
194 ("el" . "greek")
195 ("en" . "english")
196 ("en-au" . "australian")
197 ("en-ca" . "canadian")
198 ("en-gb" . "british")
199 ("en-ie" . "irish")
200 ("en-nz" . "newzealand")
201 ("en-us" . "american")
202 ("es" . "spanish")
203 ("et" . "estonian")
204 ("eu" . "basque")
205 ("fi" . "finnish")
206 ("fr" . "frenchb")
207 ("fr-ca" . "canadien")
208 ("gl" . "galician")
209 ("hr" . "croatian")
210 ("hu" . "hungarian")
211 ("id" . "indonesian")
212 ("is" . "icelandic")
213 ("it" . "italian")
214 ("la" . "latin")
215 ("ms" . "malay")
216 ("nl" . "dutch")
217 ("no-no" . "nynorsk")
218 ("pl" . "polish")
219 ("pt" . "portuguese")
220 ("ro" . "romanian")
221 ("ru" . "russian")
222 ("sa" . "sanskrit")
223 ("sb" . "uppersorbian")
224 ("sk" . "slovak")
225 ("sl" . "slovene")
226 ("sq" . "albanian")
227 ("sr" . "serbian")
228 ("sv" . "swedish")
229 ("ta" . "tamil")
230 ("tr" . "turkish")
231 ("uk" . "ukrainian"))
232 "Alist between language code and corresponding Babel option.")
234 (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
235 ("qbordermatrix" . "\\cr")
236 ("kbordermatrix" . "\\\\"))
237 "Alist between matrix macros and their row ending.")
241 ;;; User Configurable Variables
243 (defgroup org-export-latex nil
244 "Options for exporting Org mode files to LaTeX."
245 :tag "Org Export LaTeX"
246 :group 'org-export)
249 ;;;; Preamble
251 (defcustom org-latex-default-class "article"
252 "The default LaTeX class."
253 :group 'org-export-latex
254 :type '(string :tag "LaTeX class"))
256 (defcustom org-latex-classes
257 '(("article"
258 "\\documentclass[11pt]{article}"
259 ("\\section{%s}" . "\\section*{%s}")
260 ("\\subsection{%s}" . "\\subsection*{%s}")
261 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
262 ("\\paragraph{%s}" . "\\paragraph*{%s}")
263 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
264 ("report"
265 "\\documentclass[11pt]{report}"
266 ("\\part{%s}" . "\\part*{%s}")
267 ("\\chapter{%s}" . "\\chapter*{%s}")
268 ("\\section{%s}" . "\\section*{%s}")
269 ("\\subsection{%s}" . "\\subsection*{%s}")
270 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
271 ("book"
272 "\\documentclass[11pt]{book}"
273 ("\\part{%s}" . "\\part*{%s}")
274 ("\\chapter{%s}" . "\\chapter*{%s}")
275 ("\\section{%s}" . "\\section*{%s}")
276 ("\\subsection{%s}" . "\\subsection*{%s}")
277 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
278 "Alist of LaTeX classes and associated header and structure.
279 If #+LaTeX_CLASS is set in the buffer, use its value and the
280 associated information. Here is the structure of each cell:
282 \(class-name
283 header-string
284 \(numbered-section . unnumbered-section\)
285 ...\)
287 The header string
288 -----------------
290 The HEADER-STRING is the header that will be inserted into the
291 LaTeX file. It should contain the \\documentclass macro, and
292 anything else that is needed for this setup. To this header, the
293 following commands will be added:
295 - Calls to \\usepackage for all packages mentioned in the
296 variables `org-latex-default-packages-alist' and
297 `org-latex-packages-alist'. Thus, your header definitions
298 should avoid to also request these packages.
300 - Lines specified via \"#+LaTeX_HEADER:\"
302 If you need more control about the sequence in which the header
303 is built up, or if you want to exclude one of these building
304 blocks for a particular class, you can use the following
305 macro-like placeholders.
307 [DEFAULT-PACKAGES] \\usepackage statements for default packages
308 [NO-DEFAULT-PACKAGES] do not include any of the default packages
309 [PACKAGES] \\usepackage statements for packages
310 [NO-PACKAGES] do not include the packages
311 [EXTRA] the stuff from #+LaTeX_HEADER
312 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
314 So a header like
316 \\documentclass{article}
317 [NO-DEFAULT-PACKAGES]
318 [EXTRA]
319 \\providecommand{\\alert}[1]{\\textbf{#1}}
320 [PACKAGES]
322 will omit the default packages, and will include the
323 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
324 then place \\usepackage commands based on the content of
325 `org-latex-packages-alist'.
327 If your header, `org-latex-default-packages-alist' or
328 `org-latex-packages-alist' inserts
329 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
330 replaced with a coding system derived from
331 `buffer-file-coding-system'. See also the variable
332 `org-latex-inputenc-alist' for a way to influence this mechanism.
334 The sectioning structure
335 ------------------------
337 The sectioning structure of the class is given by the elements
338 following the header string. For each sectioning level, a number
339 of strings is specified. A %s formatter is mandatory in each
340 section string and will be replaced by the title of the section.
342 Instead of a cons cell \(numbered . unnumbered\), you can also
343 provide a list of 2 or 4 elements,
345 \(numbered-open numbered-close\)
349 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
351 providing opening and closing strings for a LaTeX environment
352 that should represent the document section. The opening clause
353 should have a %s to represent the section title.
355 Instead of a list of sectioning commands, you can also specify
356 a function name. That function will be called with two
357 parameters, the \(reduced) level of the headline, and a predicate
358 non-nil when the headline should be numbered. It must return
359 a format string in which the section title will be added."
360 :group 'org-export-latex
361 :type '(repeat
362 (list (string :tag "LaTeX class")
363 (string :tag "LaTeX header")
364 (repeat :tag "Levels" :inline t
365 (choice
366 (cons :tag "Heading"
367 (string :tag " numbered")
368 (string :tag "unnumbered"))
369 (list :tag "Environment"
370 (string :tag "Opening (numbered)")
371 (string :tag "Closing (numbered)")
372 (string :tag "Opening (unnumbered)")
373 (string :tag "Closing (unnumbered)"))
374 (function :tag "Hook computing sectioning"))))))
376 (defcustom org-latex-inputenc-alist nil
377 "Alist of inputenc coding system names, and what should really be used.
378 For example, adding an entry
380 (\"utf8\" . \"utf8x\")
382 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
383 are written as utf8 files."
384 :group 'org-export-latex
385 :type '(repeat
386 (cons
387 (string :tag "Derived from buffer")
388 (string :tag "Use this instead"))))
390 (defcustom org-latex-date-timestamp-format nil
391 "Time-stamp format string to use for DATE keyword.
393 The format string, when specified, only applies if date consists
394 in a single time-stamp. Otherwise its value will be ignored.
396 See `format-time-string' for details on how to build this
397 string."
398 :group 'org-export-latex
399 :type '(choice
400 (string :tag "Time-stamp format string")
401 (const :tag "No format string" nil)))
403 (defcustom org-latex-title-command "\\maketitle"
404 "The command used to insert the title just after \\begin{document}.
405 If this string contains the formatting specification \"%s\" then
406 it will be used as a formatting string, passing the title as an
407 argument."
408 :group 'org-export-latex
409 :type 'string)
411 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
412 "LaTeX command to set the table of contents, list of figures...
413 This command only applies to the table of contents generated with
414 toc:nil option, not to those generated with #+TOC keyword."
415 :group 'org-export-latex
416 :type 'string)
418 (defcustom org-latex-with-hyperref t
419 "Toggle insertion of \\hypersetup{...} in the preamble."
420 :group 'org-export-latex
421 :type 'boolean)
424 ;;;; Headline
426 (defcustom org-latex-format-headline-function
427 'org-latex-format-headline-default-function
428 "Function for formatting the headline's text.
430 This function will be called with 5 arguments:
431 TODO the todo keyword (string or nil).
432 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
433 PRIORITY the priority of the headline (integer or nil)
434 TEXT the main headline text (string).
435 TAGS the tags as a list of strings (list of strings or nil).
437 The function result will be used in the section format string.
439 Use `org-latex-format-headline-default-function' by default,
440 which format headlines like for Org version prior to 8.0."
441 :group 'org-export-latex
442 :version "24.3"
443 :type 'function)
446 ;;;; Footnotes
448 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
449 "Text used to separate footnotes."
450 :group 'org-export-latex
451 :type 'string)
454 ;;;; Timestamps
456 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
457 "A printf format string to be applied to active timestamps."
458 :group 'org-export-latex
459 :type 'string)
461 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
462 "A printf format string to be applied to inactive timestamps."
463 :group 'org-export-latex
464 :type 'string)
466 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
467 "A printf format string to be applied to diary timestamps."
468 :group 'org-export-latex
469 :type 'string)
472 ;;;; Links
474 (defcustom org-latex-image-default-option "width=.9\\linewidth"
475 "Default option for images."
476 :group 'org-export-latex
477 :type 'string)
479 (defcustom org-latex-default-figure-position "htb"
480 "Default position for latex figures."
481 :group 'org-export-latex
482 :type 'string)
484 (defcustom org-latex-inline-image-rules
485 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
486 "Rules characterizing image files that can be inlined into LaTeX.
488 A rule consists in an association whose key is the type of link
489 to consider, and value is a regexp that will be matched against
490 link's path.
492 Note that, by default, the image extension *actually* allowed
493 depend on the way the LaTeX file is processed. When used with
494 pdflatex, pdf, jpg and png images are OK. When processing
495 through dvi to Postscript, only ps and eps are allowed. The
496 default we use here encompasses both."
497 :group 'org-export-latex
498 :type '(alist :key-type (string :tag "Type")
499 :value-type (regexp :tag "Path")))
501 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
502 "Format string for links with unknown path type."
503 :group 'org-export-latex
504 :type 'string)
507 ;;;; Tables
509 (defcustom org-latex-default-table-environment "tabular"
510 "Default environment used to build tables."
511 :group 'org-export-latex
512 :type 'string)
514 (defcustom org-latex-default-table-mode 'table
515 "Default mode for tables.
517 Value can be a symbol among:
519 `table' Regular LaTeX table.
521 `math' In this mode, every cell is considered as being in math
522 mode and the complete table will be wrapped within a math
523 environment. It is particularly useful to write matrices.
525 `inline-math' This mode is almost the same as `math', but the
526 math environment will be inlined.
528 `verbatim' The table is exported as it appears in the Org
529 buffer, within a verbatim environment.
531 This value can be overridden locally with, i.e. \":mode math\" in
532 LaTeX attributes.
534 When modifying this variable, it may be useful to change
535 `org-latex-default-table-environment' accordingly."
536 :group 'org-export-latex
537 :type '(choice (const :tag "Table" table)
538 (const :tag "Matrix" math)
539 (const :tag "Inline matrix" inline-math)
540 (const :tag "Verbatim" verbatim)))
542 (defcustom org-latex-tables-centered t
543 "When non-nil, tables are exported in a center environment."
544 :group 'org-export-latex
545 :type 'boolean)
547 (defcustom org-latex-tables-booktabs nil
548 "When non-nil, display tables in a formal \"booktabs\" style.
549 This option assumes that the \"booktabs\" package is properly
550 loaded in the header of the document. This value can be ignored
551 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
552 attributes."
553 :group 'org-export-latex
554 :type 'boolean)
556 (defcustom org-latex-table-caption-above t
557 "When non-nil, place caption string at the beginning of the table.
558 Otherwise, place it near the end."
559 :group 'org-export-latex
560 :type 'boolean)
562 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
563 "Format string to display numbers in scientific notation.
564 The format should have \"%s\" twice, for mantissa and exponent
565 \(i.e. \"%s\\\\times10^{%s}\").
567 When nil, no transformation is made."
568 :group 'org-export-latex
569 :type '(choice
570 (string :tag "Format string")
571 (const :tag "No formatting")))
574 ;;;; Text markup
576 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
577 (code . verb)
578 (italic . "\\emph{%s}")
579 (strike-through . "\\st{%s}")
580 (underline . "\\underline{%s}")
581 (verbatim . protectedtexttt))
582 "Alist of LaTeX expressions to convert text markup.
584 The key must be a symbol among `bold', `code', `italic',
585 `strike-through', `underline' and `verbatim'. The value is
586 a formatting string to wrap fontified text with.
588 Value can also be set to the following symbols: `verb' and
589 `protectedtexttt'. For the former, Org will use \"\\verb\" to
590 create a format string and select a delimiter character that
591 isn't in the string. For the latter, Org will use \"\\texttt\"
592 to typeset and try to protect special characters.
594 If no association can be found for a given markup, text will be
595 returned as-is."
596 :group 'org-export-latex
597 :type 'alist
598 :options '(bold code italic strike-through underline verbatim))
601 ;;;; Drawers
603 (defcustom org-latex-format-drawer-function nil
604 "Function called to format a drawer in LaTeX code.
606 The function must accept two parameters:
607 NAME the drawer name, like \"LOGBOOK\"
608 CONTENTS the contents of the drawer.
610 The function should return the string to be exported.
612 For example, the variable could be set to the following function
613 in order to mimic default behaviour:
615 \(defun org-latex-format-drawer-default \(name contents\)
616 \"Format a drawer element for LaTeX export.\"
617 contents\)"
618 :group 'org-export-latex
619 :type 'function)
622 ;;;; Inlinetasks
624 (defcustom org-latex-format-inlinetask-function nil
625 "Function called to format an inlinetask in LaTeX code.
627 The function must accept six parameters:
628 TODO the todo keyword, as a string
629 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
630 PRIORITY the inlinetask priority, as a string
631 NAME the inlinetask name, as a string.
632 TAGS the inlinetask tags, as a list of strings.
633 CONTENTS the contents of the inlinetask, as a string.
635 The function should return the string to be exported.
637 For example, the variable could be set to the following function
638 in order to mimic default behaviour:
640 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
641 \"Format an inline task element for LaTeX export.\"
642 \(let ((full-title
643 \(concat
644 \(when todo
645 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
646 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
647 title
648 \(when tags
649 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
650 \(mapconcat 'identity tags \":\")))))
651 \(format (concat \"\\\\begin{center}\\n\"
652 \"\\\\fbox{\\n\"
653 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
654 \"%s\\n\\n\"
655 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
656 \"%s\"
657 \"\\\\end{minipage}}\"
658 \"\\\\end{center}\")
659 full-title contents))"
660 :group 'org-export-latex
661 :type 'function)
664 ;; Src blocks
666 (defcustom org-latex-listings nil
667 "Non-nil means export source code using the listings package.
668 This package will fontify source code, possibly even with color.
669 If you want to use this, you also need to make LaTeX use the
670 listings package, and if you want to have color, the color
671 package. Just add these to `org-latex-packages-alist', for
672 example using customize, or with something like:
674 \(require 'ox-latex)
675 \(add-to-list 'org-latex-packages-alist '\(\"\" \"listings\"))
676 \(add-to-list 'org-latex-packages-alist '\(\"\" \"color\"))
678 Alternatively,
680 \(setq org-latex-listings 'minted)
682 causes source code to be exported using the minted package as
683 opposed to listings. If you want to use minted, you need to add
684 the minted package to `org-latex-packages-alist', for example
685 using customize, or with
687 \(require 'ox-latex)
688 \(add-to-list 'org-latex-packages-alist '\(\"\" \"minted\"))
690 In addition, it is necessary to install pygments
691 \(http://pygments.org), and to configure the variable
692 `org-latex-pdf-process' so that the -shell-escape option is
693 passed to pdflatex."
694 :group 'org-export-latex
695 :type '(choice
696 (const :tag "Use listings" t)
697 (const :tag "Use minted" 'minted)
698 (const :tag "Export verbatim" nil)))
700 (defcustom org-latex-listings-langs
701 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
702 (c "C") (cc "C++")
703 (fortran "fortran")
704 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
705 (html "HTML") (xml "XML")
706 (tex "TeX") (latex "TeX")
707 (shell-script "bash")
708 (gnuplot "Gnuplot")
709 (ocaml "Caml") (caml "Caml")
710 (sql "SQL") (sqlite "sql"))
711 "Alist mapping languages to their listing language counterpart.
712 The key is a symbol, the major mode symbol without the \"-mode\".
713 The value is the string that should be inserted as the language
714 parameter for the listings package. If the mode name and the
715 listings name are the same, the language does not need an entry
716 in this list - but it does not hurt if it is present."
717 :group 'org-export-latex
718 :type '(repeat
719 (list
720 (symbol :tag "Major mode ")
721 (string :tag "Listings language"))))
723 (defcustom org-latex-listings-options nil
724 "Association list of options for the latex listings package.
726 These options are supplied as a comma-separated list to the
727 \\lstset command. Each element of the association list should be
728 a list containing two strings: the name of the option, and the
729 value. For example,
731 (setq org-latex-listings-options
732 '((\"basicstyle\" \"\\small\")
733 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
735 will typeset the code in a small size font with underlined, bold
736 black keywords.
738 Note that the same options will be applied to blocks of all
739 languages."
740 :group 'org-export-latex
741 :type '(repeat
742 (list
743 (string :tag "Listings option name ")
744 (string :tag "Listings option value"))))
746 (defcustom org-latex-minted-langs
747 '((emacs-lisp "common-lisp")
748 (cc "c++")
749 (cperl "perl")
750 (shell-script "bash")
751 (caml "ocaml"))
752 "Alist mapping languages to their minted language counterpart.
753 The key is a symbol, the major mode symbol without the \"-mode\".
754 The value is the string that should be inserted as the language
755 parameter for the minted package. If the mode name and the
756 listings name are the same, the language does not need an entry
757 in this list - but it does not hurt if it is present.
759 Note that minted uses all lower case for language identifiers,
760 and that the full list of language identifiers can be obtained
761 with:
763 pygmentize -L lexers"
764 :group 'org-export-latex
765 :type '(repeat
766 (list
767 (symbol :tag "Major mode ")
768 (string :tag "Minted language"))))
770 (defcustom org-latex-minted-options nil
771 "Association list of options for the latex minted package.
773 These options are supplied within square brackets in
774 \\begin{minted} environments. Each element of the alist should
775 be a list containing two strings: the name of the option, and the
776 value. For example,
778 \(setq org-latex-minted-options
779 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
781 will result in src blocks being exported with
783 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
785 as the start of the minted environment. Note that the same
786 options will be applied to blocks of all languages."
787 :group 'org-export-latex
788 :type '(repeat
789 (list
790 (string :tag "Minted option name ")
791 (string :tag "Minted option value"))))
793 (defvar org-latex-custom-lang-environments nil
794 "Alist mapping languages to language-specific LaTeX environments.
796 It is used during export of src blocks by the listings and minted
797 latex packages. For example,
799 \(setq org-latex-custom-lang-environments
800 '\(\(python \"pythoncode\"\)\)\)
802 would have the effect that if org encounters begin_src python
803 during latex export it will output
805 \\begin{pythoncode}
806 <src block body>
807 \\end{pythoncode}")
810 ;;;; Compilation
812 (defcustom org-latex-pdf-process
813 '("pdflatex -interaction nonstopmode -output-directory %o %f"
814 "pdflatex -interaction nonstopmode -output-directory %o %f"
815 "pdflatex -interaction nonstopmode -output-directory %o %f")
816 "Commands to process a LaTeX file to a PDF file.
817 This is a list of strings, each of them will be given to the
818 shell as a command. %f in the command will be replaced by the
819 full file name, %b by the file base name (i.e. without directory
820 and extension parts) and %o by the base directory of the file.
822 The reason why this is a list is that it usually takes several
823 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
824 does not have a clever mechanism to detect which of these
825 commands have to be run to get to a stable result, and it also
826 does not do any error checking.
828 By default, Org uses 3 runs of `pdflatex' to do the processing.
829 If you have texi2dvi on your system and if that does not cause
830 the infamous egrep/locale bug:
832 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
834 then `texi2dvi' is the superior choice. Org does offer it as one
835 of the customize options.
837 Alternatively, this may be a Lisp function that does the
838 processing, so you could use this to apply the machinery of
839 AUCTeX or the Emacs LaTeX mode. This function should accept the
840 file name as its single argument."
841 :group 'org-export-pdf
842 :type '(choice
843 (repeat :tag "Shell command sequence"
844 (string :tag "Shell command"))
845 (const :tag "2 runs of pdflatex"
846 ("pdflatex -interaction nonstopmode -output-directory %o %f"
847 "pdflatex -interaction nonstopmode -output-directory %o %f"))
848 (const :tag "3 runs of pdflatex"
849 ("pdflatex -interaction nonstopmode -output-directory %o %f"
850 "pdflatex -interaction nonstopmode -output-directory %o %f"
851 "pdflatex -interaction nonstopmode -output-directory %o %f"))
852 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
853 ("pdflatex -interaction nonstopmode -output-directory %o %f"
854 "bibtex %b"
855 "pdflatex -interaction nonstopmode -output-directory %o %f"
856 "pdflatex -interaction nonstopmode -output-directory %o %f"))
857 (const :tag "2 runs of xelatex"
858 ("xelatex -interaction nonstopmode -output-directory %o %f"
859 "xelatex -interaction nonstopmode -output-directory %o %f"))
860 (const :tag "3 runs of xelatex"
861 ("xelatex -interaction nonstopmode -output-directory %o %f"
862 "xelatex -interaction nonstopmode -output-directory %o %f"
863 "xelatex -interaction nonstopmode -output-directory %o %f"))
864 (const :tag "xelatex,bibtex,xelatex,xelatex"
865 ("xelatex -interaction nonstopmode -output-directory %o %f"
866 "bibtex %b"
867 "xelatex -interaction nonstopmode -output-directory %o %f"
868 "xelatex -interaction nonstopmode -output-directory %o %f"))
869 (const :tag "texi2dvi"
870 ("texi2dvi -p -b -c -V %f"))
871 (const :tag "rubber"
872 ("rubber -d --into %o %f"))
873 (function)))
875 (defcustom org-latex-logfiles-extensions
876 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
877 "The list of file extensions to consider as LaTeX logfiles."
878 :group 'org-export-latex
879 :type '(repeat (string :tag "Extension")))
881 (defcustom org-latex-remove-logfiles t
882 "Non-nil means remove the logfiles produced by PDF production.
883 These are the .aux, .log, .out, and .toc files."
884 :group 'org-export-latex
885 :type 'boolean)
887 (defcustom org-latex-known-errors
888 '(("Reference.*?undefined" . "[undefined reference]")
889 ("Citation.*?undefined" . "[undefined citation]")
890 ("Undefined control sequence" . "[undefined control sequence]")
891 ("^! LaTeX.*?Error" . "[LaTeX error]")
892 ("^! Package.*?Error" . "[package error]")
893 ("Runaway argument" . "Runaway argument"))
894 "Alist of regular expressions and associated messages for the user.
895 The regular expressions are used to find possible errors in the
896 log of a latex-run."
897 :group 'org-export-latex
898 :type '(repeat
899 (cons
900 (string :tag "Regexp")
901 (string :tag "Message"))))
905 ;;; Internal Functions
907 (defun org-latex--caption/label-string (element info)
908 "Return caption and label LaTeX string for ELEMENT.
910 INFO is a plist holding contextual information. If there's no
911 caption nor label, return the empty string.
913 For non-floats, see `org-latex--wrap-label'."
914 (let* ((label (org-element-property :name element))
915 (label-str (if (not (org-string-nw-p label)) ""
916 (format "\\label{%s}"
917 (org-export-solidify-link-text label))))
918 (main (org-export-get-caption element))
919 (short (org-export-get-caption element t)))
920 (cond
921 ((and (not main) (equal label-str "")) "")
922 ((not main) (concat label-str "\n"))
923 ;; Option caption format with short name.
924 (short (format "\\caption[%s]{%s%s}\n"
925 (org-export-data short info)
926 label-str
927 (org-export-data main info)))
928 ;; Standard caption format.
929 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
931 (defun org-latex-guess-inputenc (header)
932 "Set the coding system in inputenc to what the buffer is.
934 HEADER is the LaTeX header string. This function only applies
935 when specified inputenc option is \"AUTO\".
937 Return the new header, as a string."
938 (let* ((cs (or (ignore-errors
939 (latexenc-coding-system-to-inputenc
940 (or org-export-coding-system buffer-file-coding-system)))
941 "utf8")))
942 (if (not cs) header
943 ;; First translate if that is requested.
944 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
945 ;; Then find the \usepackage statement and replace the option.
946 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
947 cs header t nil 1))))
949 (defun org-latex-guess-babel-language (header info)
950 "Set Babel's language according to LANGUAGE keyword.
952 HEADER is the LaTeX header string. INFO is the plist used as
953 a communication channel.
955 Insertion of guessed language only happens when Babel package has
956 explicitly been loaded. Then it is added to the rest of
957 package's options.
959 Return the new header."
960 (let ((language-code (plist-get info :language)))
961 ;; If no language is set or Babel package is not loaded, return
962 ;; HEADER as-is.
963 (if (or (not (stringp language-code))
964 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
965 header
966 (let ((options (save-match-data
967 (org-split-string (match-string 1 header) ",")))
968 (language (cdr (assoc language-code
969 org-latex-babel-language-alist))))
970 ;; If LANGUAGE is already loaded, return header. Otherwise,
971 ;; append LANGUAGE to other options.
972 (if (member language options) header
973 (replace-match (mapconcat 'identity
974 (append options (list language))
975 ",")
976 nil nil header 1))))))
978 (defun org-latex--find-verb-separator (s)
979 "Return a character not used in string S.
980 This is used to choose a separator for constructs like \\verb."
981 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
982 (loop for c across ll
983 when (not (string-match (regexp-quote (char-to-string c)) s))
984 return (char-to-string c))))
986 (defun org-latex--make-option-string (options)
987 "Return a comma separated string of keywords and values.
988 OPTIONS is an alist where the key is the options keyword as
989 a string, and the value a list containing the keyword value, or
990 nil."
991 (mapconcat (lambda (pair)
992 (concat (first pair)
993 (when (> (length (second pair)) 0)
994 (concat "=" (second pair)))))
995 options
996 ","))
998 (defun org-latex--wrap-label (element output)
999 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1000 This function shouldn't be used for floats. See
1001 `org-latex--caption/label-string'."
1002 (let ((label (org-element-property :name element)))
1003 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
1004 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
1005 output))))
1007 (defun org-latex--text-markup (text markup)
1008 "Format TEXT depending on MARKUP text markup.
1009 See `org-latex-text-markup-alist' for details."
1010 (let ((fmt (cdr (assq markup org-latex-text-markup-alist))))
1011 (cond
1012 ;; No format string: Return raw text.
1013 ((not fmt) text)
1014 ;; Handle the `verb' special case: Find and appropriate separator
1015 ;; and use "\\verb" command.
1016 ((eq 'verb fmt)
1017 (let ((separator (org-latex--find-verb-separator text)))
1018 (concat "\\verb" separator text separator)))
1019 ;; Handle the `protectedtexttt' special case: Protect some
1020 ;; special chars and use "\texttt{%s}" format string.
1021 ((eq 'protectedtexttt fmt)
1022 (let ((start 0)
1023 (trans '(("\\" . "\\textbackslash{}")
1024 ("~" . "\\textasciitilde{}")
1025 ("^" . "\\textasciicircum{}")))
1026 (rtn "")
1027 char)
1028 (while (string-match "[\\{}$%&_#~^]" text)
1029 (setq char (match-string 0 text))
1030 (if (> (match-beginning 0) 0)
1031 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1032 (setq text (substring text (1+ (match-beginning 0))))
1033 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1034 rtn (concat rtn char)))
1035 (setq text (concat rtn text)
1036 fmt "\\texttt{%s}")
1037 (while (string-match "--" text)
1038 (setq text (replace-match "-{}-" t t text)))
1039 (format fmt text)))
1040 ;; Else use format string.
1041 (t (format fmt text)))))
1043 (defun org-latex--delayed-footnotes-definitions (element info)
1044 "Return footnotes definitions in ELEMENT as a string.
1046 INFO is a plist used as a communication channel.
1048 Footnotes definitions are returned within \"\\footnotetxt{}\"
1049 commands.
1051 This function is used within constructs that don't support
1052 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1053 \"\\footnotemark\" is used within the construct and the function
1054 just outside of it."
1055 (mapconcat
1056 (lambda (ref)
1057 (format
1058 "\\footnotetext[%s]{%s}"
1059 (org-export-get-footnote-number ref info)
1060 (org-trim
1061 (org-export-data
1062 (org-export-get-footnote-definition ref info) info))))
1063 ;; Find every footnote reference in ELEMENT.
1064 (let* (all-refs
1065 search-refs ; For byte-compiler.
1066 (search-refs
1067 (function
1068 (lambda (data)
1069 ;; Return a list of all footnote references never seen
1070 ;; before in DATA.
1071 (org-element-map data 'footnote-reference
1072 (lambda (ref)
1073 (when (org-export-footnote-first-reference-p ref info)
1074 (push ref all-refs)
1075 (when (eq (org-element-property :type ref) 'standard)
1076 (funcall search-refs
1077 (org-export-get-footnote-definition ref info)))))
1078 info)
1079 (reverse all-refs)))))
1080 (funcall search-refs element))
1081 ""))
1085 ;;; Template
1087 (defun org-latex-template (contents info)
1088 "Return complete document string after LaTeX conversion.
1089 CONTENTS is the transcoded contents string. INFO is a plist
1090 holding export options."
1091 (let ((title (org-export-data (plist-get info :title) info)))
1092 (concat
1093 ;; Time-stamp.
1094 (and (plist-get info :time-stamp-file)
1095 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1096 ;; Document class and packages.
1097 (let ((class (plist-get info :latex-class))
1098 (class-options (plist-get info :latex-class-options)))
1099 (org-element-normalize-string
1100 (let* ((header (nth 1 (assoc class org-latex-classes)))
1101 (document-class-string
1102 (and (stringp header)
1103 (if (not class-options) header
1104 (replace-regexp-in-string
1105 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1106 class-options header t nil 1)))))
1107 (if (not document-class-string)
1108 (user-error "Unknown LaTeX class `%s'" class)
1109 (org-latex-guess-babel-language
1110 (org-latex-guess-inputenc
1111 (org-splice-latex-header
1112 document-class-string
1113 org-latex-default-packages-alist
1114 org-latex-packages-alist nil
1115 (plist-get info :latex-header-extra)))
1116 info)))))
1117 ;; Possibly limit depth for headline numbering.
1118 (let ((sec-num (plist-get info :section-numbers)))
1119 (when (integerp sec-num)
1120 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1121 ;; Author.
1122 (let ((author (and (plist-get info :with-author)
1123 (let ((auth (plist-get info :author)))
1124 (and auth (org-export-data auth info)))))
1125 (email (and (plist-get info :with-email)
1126 (org-export-data (plist-get info :email) info))))
1127 (cond ((and author email (not (string= "" email)))
1128 (format "\\author{%s\\thanks{%s}}\n" author email))
1129 ((or author email) (format "\\author{%s}\n" (or author email)))))
1130 ;; Date.
1131 (let ((date (and (plist-get info :with-date) (plist-get info :date))))
1132 (format "\\date{%s}\n"
1133 (cond ((not date) "")
1134 ;; If `:date' consists in a single timestamp and
1135 ;; `:date-format' is provided, apply it.
1136 ((and (plist-get info :date-format)
1137 (not (cdr date))
1138 (eq (org-element-type (car date)) 'timestamp))
1139 (org-timestamp-format
1140 (car date) (plist-get info :date-format)))
1141 (t (org-export-data date info)))))
1142 ;; Title
1143 (format "\\title{%s}\n" title)
1144 ;; Hyperref options.
1145 (when (plist-get info :latex-hyperref-p)
1146 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1147 (or (plist-get info :keywords) "")
1148 (or (plist-get info :description) "")
1149 (if (not (plist-get info :with-creator)) ""
1150 (plist-get info :creator))))
1151 ;; Document start.
1152 "\\begin{document}\n\n"
1153 ;; Title command.
1154 (org-element-normalize-string
1155 (cond ((string= "" title) nil)
1156 ((not (stringp org-latex-title-command)) nil)
1157 ((string-match "\\(?:[^%]\\|^\\)%s"
1158 org-latex-title-command)
1159 (format org-latex-title-command title))
1160 (t org-latex-title-command)))
1161 ;; Table of contents.
1162 (let ((depth (plist-get info :with-toc)))
1163 (when depth
1164 (concat (when (wholenump depth)
1165 (format "\\setcounter{tocdepth}{%d}\n" depth))
1166 org-latex-toc-command)))
1167 ;; Document's body.
1168 contents
1169 ;; Creator.
1170 (let ((creator-info (plist-get info :with-creator)))
1171 (cond
1172 ((not creator-info) "")
1173 ((eq creator-info 'comment)
1174 (format "%% %s\n" (plist-get info :creator)))
1175 (t (concat (plist-get info :creator) "\n"))))
1176 ;; Document end.
1177 "\\end{document}")))
1181 ;;; Transcode Functions
1183 ;;;; Bold
1185 (defun org-latex-bold (bold contents info)
1186 "Transcode BOLD from Org to LaTeX.
1187 CONTENTS is the text with bold markup. INFO is a plist holding
1188 contextual information."
1189 (org-latex--text-markup contents 'bold))
1192 ;;;; Center Block
1194 (defun org-latex-center-block (center-block contents info)
1195 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1196 CONTENTS holds the contents of the center block. INFO is a plist
1197 holding contextual information."
1198 (org-latex--wrap-label
1199 center-block
1200 (format "\\begin{center}\n%s\\end{center}" contents)))
1203 ;;;; Clock
1205 (defun org-latex-clock (clock contents info)
1206 "Transcode a CLOCK element from Org to LaTeX.
1207 CONTENTS is nil. INFO is a plist holding contextual
1208 information."
1209 (concat
1210 "\\noindent"
1211 (format "\\textbf{%s} " org-clock-string)
1212 (format org-latex-inactive-timestamp-format
1213 (concat (org-translate-time
1214 (org-element-property :raw-value
1215 (org-element-property :value clock)))
1216 (let ((time (org-element-property :duration clock)))
1217 (and time (format " (%s)" time)))))
1218 "\\\\"))
1221 ;;;; Code
1223 (defun org-latex-code (code contents info)
1224 "Transcode a CODE object from Org to LaTeX.
1225 CONTENTS is nil. INFO is a plist used as a communication
1226 channel."
1227 (org-latex--text-markup (org-element-property :value code) 'code))
1230 ;;;; Drawer
1232 (defun org-latex-drawer (drawer contents info)
1233 "Transcode a DRAWER element from Org to LaTeX.
1234 CONTENTS holds the contents of the block. INFO is a plist
1235 holding contextual information."
1236 (let* ((name (org-element-property :drawer-name drawer))
1237 (output (if (functionp org-latex-format-drawer-function)
1238 (funcall org-latex-format-drawer-function
1239 name contents)
1240 ;; If there's no user defined function: simply
1241 ;; display contents of the drawer.
1242 contents)))
1243 (org-latex--wrap-label drawer output)))
1246 ;;;; Dynamic Block
1248 (defun org-latex-dynamic-block (dynamic-block contents info)
1249 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1250 CONTENTS holds the contents of the block. INFO is a plist
1251 holding contextual information. See `org-export-data'."
1252 (org-latex--wrap-label dynamic-block contents))
1255 ;;;; Entity
1257 (defun org-latex-entity (entity contents info)
1258 "Transcode an ENTITY object from Org to LaTeX.
1259 CONTENTS are the definition itself. INFO is a plist holding
1260 contextual information."
1261 (let ((ent (org-element-property :latex entity)))
1262 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1265 ;;;; Example Block
1267 (defun org-latex-example-block (example-block contents info)
1268 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1269 CONTENTS is nil. INFO is a plist holding contextual
1270 information."
1271 (when (org-string-nw-p (org-element-property :value example-block))
1272 (org-latex--wrap-label
1273 example-block
1274 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1275 (org-export-format-code-default example-block info)))))
1278 ;;;; Export Block
1280 (defun org-latex-export-block (export-block contents info)
1281 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1282 CONTENTS is nil. INFO is a plist holding contextual information."
1283 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1284 (org-remove-indentation (org-element-property :value export-block))))
1287 ;;;; Export Snippet
1289 (defun org-latex-export-snippet (export-snippet contents info)
1290 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1291 CONTENTS is nil. INFO is a plist holding contextual information."
1292 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1293 (org-element-property :value export-snippet)))
1296 ;;;; Fixed Width
1298 (defun org-latex-fixed-width (fixed-width contents info)
1299 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1300 CONTENTS is nil. INFO is a plist holding contextual information."
1301 (org-latex--wrap-label
1302 fixed-width
1303 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1304 (org-remove-indentation
1305 (org-element-property :value fixed-width)))))
1308 ;;;; Footnote Reference
1310 ;; Footnote reference export is handled by
1311 ;; `org-latex-footnote-reference'.
1313 ;; Internally, `org-latex--get-footnote-counter' is used to restore
1314 ;; the value of the LaTeX "footnote" counter after a jump due to
1315 ;; a reference to an already defined footnote. It is only needed in
1316 ;; item tags since the optional argument to \footnotemark is not
1317 ;; allowed there.
1319 (defun org-latex--get-footnote-counter (footnote-reference info)
1320 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1321 INFO is a plist used as a communication channel."
1322 ;; Find original counter value by counting number of footnote
1323 ;; references appearing for the first time before the current
1324 ;; footnote reference.
1325 (let* ((label (org-element-property :label footnote-reference))
1326 seen-refs
1327 search-ref ; For byte-compiler.
1328 (search-ref
1329 (function
1330 (lambda (data)
1331 ;; Search footnote references through DATA, filling
1332 ;; SEEN-REFS along the way.
1333 (org-element-map data 'footnote-reference
1334 (lambda (fn)
1335 (let ((fn-lbl (org-element-property :label fn)))
1336 (cond
1337 ;; Anonymous footnote match: return number.
1338 ((eq fn footnote-reference) (length seen-refs))
1339 ;; Anonymous footnote: it's always a new one.
1340 ;; Also, be sure to return nil from the `cond' so
1341 ;; `first-match' doesn't get us out of the loop.
1342 ((not fn-lbl) (push 'inline seen-refs) nil)
1343 ;; Label not seen so far: add it so SEEN-REFS.
1345 ;; Also search for subsequent references in
1346 ;; footnote definition so numbering follows
1347 ;; reading logic. Note that we don't care about
1348 ;; inline definitions, since `org-element-map'
1349 ;; already traverses them at the right time.
1350 ((not (member fn-lbl seen-refs))
1351 (push fn-lbl seen-refs)
1352 (funcall search-ref
1353 (org-export-get-footnote-definition fn info))))))
1354 ;; Don't enter footnote definitions since it will
1355 ;; happen when their first reference is found.
1356 info 'first-match 'footnote-definition)))))
1357 (funcall search-ref (plist-get info :parse-tree))))
1359 (defun org-latex-footnote-reference (footnote-reference contents info)
1360 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1361 CONTENTS is nil. INFO is a plist holding contextual information."
1362 (concat
1363 ;; Insert separator between two footnotes in a row.
1364 (let ((prev (org-export-get-previous-element footnote-reference info)))
1365 (when (eq (org-element-type prev) 'footnote-reference)
1366 org-latex-footnote-separator))
1367 (cond
1368 ;; Use \footnotemark if reference is within an item's tag.
1369 ((eq (org-element-type (org-export-get-parent-element footnote-reference))
1370 'item)
1371 (if (org-export-footnote-first-reference-p footnote-reference info)
1372 "\\footnotemark"
1373 ;; Since we can't specify footnote number as an optional
1374 ;; argument within an item tag, some extra work has to be done
1375 ;; when the footnote has already been referenced. In that
1376 ;; case, set footnote counter to the desired number, use the
1377 ;; footnotemark, then set counter back to its original value.
1378 (format
1379 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1380 (1- (org-export-get-footnote-number footnote-reference info))
1381 (org-latex--get-footnote-counter footnote-reference info))))
1382 ;; Use \footnotemark if the footnote has already been defined.
1383 ((not (org-export-footnote-first-reference-p footnote-reference info))
1384 (format "\\footnotemark[%s]{}"
1385 (org-export-get-footnote-number footnote-reference info)))
1386 ;; Use \footnotemark if reference is within another footnote
1387 ;; reference, footnote definition or table cell.
1388 ((loop for parent in (org-export-get-genealogy footnote-reference)
1389 thereis (memq (org-element-type parent)
1390 '(footnote-reference footnote-definition table-cell)))
1391 "\\footnotemark")
1392 ;; Otherwise, define it with \footnote command.
1394 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1395 (concat
1396 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1397 ;; Retrieve all footnote references within the footnote and
1398 ;; add their definition after it, since LaTeX doesn't support
1399 ;; them inside.
1400 (org-latex--delayed-footnotes-definitions def info)))))))
1403 ;;;; Headline
1405 (defun org-latex-headline (headline contents info)
1406 "Transcode a HEADLINE element from Org to LaTeX.
1407 CONTENTS holds the contents of the headline. INFO is a plist
1408 holding contextual information."
1409 (unless (org-element-property :footnote-section-p headline)
1410 (let* ((class (plist-get info :latex-class))
1411 (level (org-export-get-relative-level headline info))
1412 (numberedp (org-export-numbered-headline-p headline info))
1413 (class-sectionning (assoc class org-latex-classes))
1414 ;; Section formatting will set two placeholders: one for
1415 ;; the title and the other for the contents.
1416 (section-fmt
1417 (let ((sec (if (functionp (nth 2 class-sectionning))
1418 (funcall (nth 2 class-sectionning) level numberedp)
1419 (nth (1+ level) class-sectionning))))
1420 (cond
1421 ;; No section available for that LEVEL.
1422 ((not sec) nil)
1423 ;; Section format directly returned by a function. Add
1424 ;; placeholder for contents.
1425 ((stringp sec) (concat sec "\n%s"))
1426 ;; (numbered-section . unnumbered-section)
1427 ((not (consp (cdr sec)))
1428 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1429 ;; (numbered-open numbered-close)
1430 ((= (length sec) 2)
1431 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1432 ;; (num-in num-out no-num-in no-num-out)
1433 ((= (length sec) 4)
1434 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1435 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1436 (text (org-export-data (org-element-property :title headline) info))
1437 (todo
1438 (and (plist-get info :with-todo-keywords)
1439 (let ((todo (org-element-property :todo-keyword headline)))
1440 (and todo (org-export-data todo info)))))
1441 (todo-type (and todo (org-element-property :todo-type headline)))
1442 (tags (and (plist-get info :with-tags)
1443 (org-export-get-tags headline info)))
1444 (priority (and (plist-get info :with-priority)
1445 (org-element-property :priority headline)))
1446 ;; Create the headline text along with a no-tag version.
1447 ;; The latter is required to remove tags from toc.
1448 (full-text (funcall org-latex-format-headline-function
1449 todo todo-type priority text tags))
1450 ;; Associate \label to the headline for internal links.
1451 (headline-label
1452 (format "\\label{sec-%s}\n"
1453 (mapconcat 'number-to-string
1454 (org-export-get-headline-number headline info)
1455 "-")))
1456 (pre-blanks
1457 (make-string (org-element-property :pre-blank headline) 10)))
1458 (if (or (not section-fmt) (org-export-low-level-p headline info))
1459 ;; This is a deep sub-tree: export it as a list item. Also
1460 ;; export as items headlines for which no section format has
1461 ;; been found.
1462 (let ((low-level-body
1463 (concat
1464 ;; If headline is the first sibling, start a list.
1465 (when (org-export-first-sibling-p headline info)
1466 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1467 ;; Itemize headline
1468 "\\item " full-text "\n" headline-label pre-blanks contents)))
1469 ;; If headline is not the last sibling simply return
1470 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1471 ;; any blank line.
1472 (if (not (org-export-last-sibling-p headline info)) low-level-body
1473 (replace-regexp-in-string
1474 "[ \t\n]*\\'"
1475 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1476 low-level-body)))
1477 ;; This is a standard headline. Export it as a section. Add
1478 ;; an alternative heading when possible.
1479 (let ((opt-title
1480 (funcall org-latex-format-headline-function
1481 todo todo-type priority
1482 (org-export-data
1483 (or (org-export-get-optional-title headline info)
1484 (org-element-property :title headline))
1485 info)
1486 (and (eq (plist-get info :with-tags) t) tags))))
1487 (if (and opt-title (string-match "\\`\\\\\\(.*?\\){" section-fmt))
1488 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1489 ;; Replace square brackets with parenthesis
1490 ;; since square brackets are not supported in
1491 ;; optional arguments.
1492 (replace-regexp-in-string
1493 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1494 full-text
1495 (concat headline-label pre-blanks contents))
1496 ;; Impossible to add an alternative heading. Fallback to
1497 ;; regular sectioning format string.
1498 (format section-fmt full-text
1499 (concat headline-label pre-blanks contents))))))))
1501 (defun org-latex-format-headline-default-function
1502 (todo todo-type priority text tags)
1503 "Default format function for a headline.
1504 See `org-latex-format-headline-function' for details."
1505 (concat
1506 (and todo (format "\\textbf{\\textsc{\\textsf{%s}}} " todo))
1507 (and priority (format "\\framebox{\\#%c} " priority))
1508 text
1509 (and tags
1510 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
1513 ;;;; Horizontal Rule
1515 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1516 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1517 CONTENTS is nil. INFO is a plist holding contextual information."
1518 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1519 (prev (org-export-get-previous-element horizontal-rule info)))
1520 (concat
1521 ;; Make sure the rule doesn't start at the end of the current
1522 ;; line by separating it with a blank line from previous element.
1523 (when (and prev
1524 (let ((prev-blank (org-element-property :post-blank prev)))
1525 (or (not prev-blank) (zerop prev-blank))))
1526 "\n")
1527 (org-latex--wrap-label
1528 horizontal-rule
1529 (format "\\rule{%s}{%s}"
1530 (or (plist-get attr :width) "\\linewidth")
1531 (or (plist-get attr :thickness) "0.5pt"))))))
1534 ;;;; Inline Src Block
1536 (defun org-latex-inline-src-block (inline-src-block contents info)
1537 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1538 CONTENTS holds the contents of the item. INFO is a plist holding
1539 contextual information."
1540 (let* ((code (org-element-property :value inline-src-block))
1541 (separator (org-latex--find-verb-separator code)))
1542 (cond
1543 ;; Do not use a special package: transcode it verbatim.
1544 ((not org-latex-listings)
1545 (concat "\\verb" separator code separator))
1546 ;; Use minted package.
1547 ((eq org-latex-listings 'minted)
1548 (let* ((org-lang (org-element-property :language inline-src-block))
1549 (mint-lang (or (cadr (assq (intern org-lang)
1550 org-latex-minted-langs))
1551 org-lang))
1552 (options (org-latex--make-option-string
1553 org-latex-minted-options)))
1554 (concat (format "\\mint%s{%s}"
1555 (if (string= options "") "" (format "[%s]" options))
1556 mint-lang)
1557 separator code separator)))
1558 ;; Use listings package.
1560 ;; Maybe translate language's name.
1561 (let* ((org-lang (org-element-property :language inline-src-block))
1562 (lst-lang (or (cadr (assq (intern org-lang)
1563 org-latex-listings-langs))
1564 org-lang))
1565 (options (org-latex--make-option-string
1566 (append org-latex-listings-options
1567 `(("language" ,lst-lang))))))
1568 (concat (format "\\lstinline[%s]" options)
1569 separator code separator))))))
1572 ;;;; Inlinetask
1574 (defun org-latex-inlinetask (inlinetask contents info)
1575 "Transcode an INLINETASK element from Org to LaTeX.
1576 CONTENTS holds the contents of the block. INFO is a plist
1577 holding contextual information."
1578 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1579 (todo (and (plist-get info :with-todo-keywords)
1580 (let ((todo (org-element-property :todo-keyword inlinetask)))
1581 (and todo (org-export-data todo info)))))
1582 (todo-type (org-element-property :todo-type inlinetask))
1583 (tags (and (plist-get info :with-tags)
1584 (org-export-get-tags inlinetask info)))
1585 (priority (and (plist-get info :with-priority)
1586 (org-element-property :priority inlinetask))))
1587 ;; If `org-latex-format-inlinetask-function' is provided, call it
1588 ;; with appropriate arguments.
1589 (if (functionp org-latex-format-inlinetask-function)
1590 (funcall org-latex-format-inlinetask-function
1591 todo todo-type priority title tags contents)
1592 ;; Otherwise, use a default template.
1593 (org-latex--wrap-label
1594 inlinetask
1595 (let ((full-title
1596 (concat
1597 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1598 (when priority (format "\\framebox{\\#%c} " priority))
1599 title
1600 (when tags (format "\\hfill{}\\textsc{:%s:}"
1601 (mapconcat 'identity tags ":"))))))
1602 (format (concat "\\begin{center}\n"
1603 "\\fbox{\n"
1604 "\\begin{minipage}[c]{.6\\textwidth}\n"
1605 "%s\n\n"
1606 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1607 "%s"
1608 "\\end{minipage}\n"
1609 "}\n"
1610 "\\end{center}")
1611 full-title contents))))))
1614 ;;;; Italic
1616 (defun org-latex-italic (italic contents info)
1617 "Transcode ITALIC from Org to LaTeX.
1618 CONTENTS is the text with italic markup. INFO is a plist holding
1619 contextual information."
1620 (org-latex--text-markup contents 'italic))
1623 ;;;; Item
1625 (defun org-latex-item (item contents info)
1626 "Transcode an ITEM element from Org to LaTeX.
1627 CONTENTS holds the contents of the item. INFO is a plist holding
1628 contextual information."
1629 (let* ((counter
1630 (let ((count (org-element-property :counter item))
1631 (level
1632 (loop for parent in (org-export-get-genealogy item)
1633 count (eq (org-element-type parent) 'plain-list)
1634 until (eq (org-element-type parent) 'headline))))
1635 (and count
1636 (< level 5)
1637 (format "\\setcounter{enum%s}{%s}\n"
1638 (nth (1- level) '("i" "ii" "iii" "iv"))
1639 (1- count)))))
1640 (checkbox (case (org-element-property :checkbox item)
1641 (on "$\\boxtimes$ ")
1642 (off "$\\Box$ ")
1643 (trans "$\\boxminus$ ")))
1644 (tag (let ((tag (org-element-property :tag item)))
1645 ;; Check-boxes must belong to the tag.
1646 (and tag (format "[%s] "
1647 (concat checkbox
1648 (org-export-data tag info)))))))
1649 (concat counter "\\item" (or tag (concat " " checkbox))
1650 (and contents (org-trim contents))
1651 ;; If there are footnotes references in tag, be sure to
1652 ;; add their definition at the end of the item. This
1653 ;; workaround is necessary since "\footnote{}" command is
1654 ;; not supported in tags.
1655 (and tag
1656 (org-latex--delayed-footnotes-definitions
1657 (org-element-property :tag item) info)))))
1660 ;;;; Keyword
1662 (defun org-latex-keyword (keyword contents info)
1663 "Transcode a KEYWORD element from Org to LaTeX.
1664 CONTENTS is nil. INFO is a plist holding contextual information."
1665 (let ((key (org-element-property :key keyword))
1666 (value (org-element-property :value keyword)))
1667 (cond
1668 ((string= key "LATEX") value)
1669 ((string= key "INDEX") (format "\\index{%s}" value))
1670 ;; Invisible targets.
1671 ((string= key "TARGET") nil)
1672 ((string= key "TOC")
1673 (let ((value (downcase value)))
1674 (cond
1675 ((string-match "\\<headlines\\>" value)
1676 (let ((depth (or (and (string-match "[0-9]+" value)
1677 (string-to-number (match-string 0 value)))
1678 (plist-get info :with-toc))))
1679 (concat
1680 (when (wholenump depth)
1681 (format "\\setcounter{tocdepth}{%s}\n" depth))
1682 "\\tableofcontents")))
1683 ((string= "tables" value) "\\listoftables")
1684 ((string= "figures" value) "\\listoffigures")
1685 ((string= "listings" value)
1686 (cond
1687 ((eq org-latex-listings 'minted) "\\listoflistings")
1688 (org-latex-listings "\\lstlistoflistings")
1689 ;; At the moment, src blocks with a caption are wrapped
1690 ;; into a figure environment.
1691 (t "\\listoffigures")))))))))
1694 ;;;; Latex Environment
1696 (defun org-latex-latex-environment (latex-environment contents info)
1697 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1698 CONTENTS is nil. INFO is a plist holding contextual information."
1699 (when (plist-get info :with-latex)
1700 (let ((label (org-element-property :name latex-environment))
1701 (value (org-remove-indentation
1702 (org-element-property :value latex-environment))))
1703 (if (not (org-string-nw-p label)) value
1704 ;; Environment is labelled: label must be within the environment
1705 ;; (otherwise, a reference pointing to that element will count
1706 ;; the section instead).
1707 (with-temp-buffer
1708 (insert value)
1709 (goto-char (point-min))
1710 (forward-line)
1711 (insert
1712 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1713 (buffer-string))))))
1716 ;;;; Latex Fragment
1718 (defun org-latex-latex-fragment (latex-fragment contents info)
1719 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1720 CONTENTS is nil. INFO is a plist holding contextual information."
1721 (when (plist-get info :with-latex)
1722 (org-element-property :value latex-fragment)))
1725 ;;;; Line Break
1727 (defun org-latex-line-break (line-break contents info)
1728 "Transcode a LINE-BREAK object from Org to LaTeX.
1729 CONTENTS is nil. INFO is a plist holding contextual information."
1730 "\\\\\n")
1733 ;;;; Link
1735 (defun org-latex--inline-image (link info)
1736 "Return LaTeX code for an inline image.
1737 LINK is the link pointing to the inline image. INFO is a plist
1738 used as a communication channel."
1739 (let* ((parent (org-export-get-parent-element link))
1740 (path (let ((raw-path (org-element-property :path link)))
1741 (if (not (file-name-absolute-p raw-path)) raw-path
1742 (expand-file-name raw-path))))
1743 (caption (org-latex--caption/label-string parent info))
1744 ;; Retrieve latex attributes from the element around.
1745 (attr (org-export-read-attribute :attr_latex parent))
1746 (float (let ((float (plist-get attr :float)))
1747 (cond ((string= float "wrap") 'wrap)
1748 ((string= float "multicolumn") 'multicolumn)
1749 ((or (string= float "figure")
1750 (org-element-property :caption parent))
1751 'figure))))
1752 (placement
1753 (let ((place (plist-get attr :placement)))
1754 (cond (place (format "%s" place))
1755 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1756 ((eq float 'figure)
1757 (format "[%s]" org-latex-default-figure-position))
1758 (t ""))))
1759 (comment-include (if (plist-get attr :comment-include) "%" ""))
1760 ;; Options for "includegraphics" macro. Make sure it is
1761 ;; a string with square brackets when non empty. Default to
1762 ;; `org-latex-image-default-option' when possible.
1763 (options (let ((opt (format "%s"
1764 (or (plist-get attr :options)
1765 org-latex-image-default-option))))
1766 (cond ((string-match "\\`\\[.*\\]" opt) opt)
1767 ((org-string-nw-p opt) (format "[%s]" opt))
1768 ((eq float 'float) "[width=0.7\\textwidth]")
1769 ((eq float 'wrap) "[width=0.48\\textwidth]")
1770 (t "")))))
1771 ;; Return proper string, depending on FLOAT.
1772 (case float
1773 (wrap (format "\\begin{wrapfigure}%s
1774 \\centering
1775 %s\\includegraphics%s{%s}
1776 %s\\end{wrapfigure}" placement comment-include options path caption))
1777 (multicolumn (format "\\begin{figure*}%s
1778 \\centering
1779 %s\\includegraphics%s{%s}
1780 %s\\end{figure*}" placement comment-include options path caption))
1781 (figure (format "\\begin{figure}%s
1782 \\centering
1783 %s\\includegraphics%s{%s}
1784 %s\\end{figure}" placement comment-include options path caption))
1785 (t (format "\\includegraphics%s{%s}" options path)))))
1787 (defun org-latex-link (link desc info)
1788 "Transcode a LINK object from Org to LaTeX.
1790 DESC is the description part of the link, or the empty string.
1791 INFO is a plist holding contextual information. See
1792 `org-export-data'."
1793 (let* ((type (org-element-property :type link))
1794 (raw-path (org-element-property :path link))
1795 ;; Ensure DESC really exists, or set it to nil.
1796 (desc (and (not (string= desc "")) desc))
1797 (imagep (org-export-inline-image-p
1798 link org-latex-inline-image-rules))
1799 (path (cond
1800 ((member type '("http" "https" "ftp" "mailto"))
1801 (concat type ":" raw-path))
1802 ((string= type "file")
1803 (if (file-name-absolute-p raw-path)
1804 (concat "file://" (expand-file-name raw-path))
1805 (concat "file://" raw-path)))
1806 (t raw-path)))
1807 protocol)
1808 (cond
1809 ;; Image file.
1810 (imagep (org-latex--inline-image link info))
1811 ;; Radio link: Transcode target's contents and use them as link's
1812 ;; description.
1813 ((string= type "radio")
1814 (let ((destination (org-export-resolve-radio-link link info)))
1815 (when destination
1816 (format "\\hyperref[%s]{%s}"
1817 (org-export-solidify-link-text path)
1818 (org-export-data (org-element-contents destination) info)))))
1819 ;; Links pointing to a headline: Find destination and build
1820 ;; appropriate referencing command.
1821 ((member type '("custom-id" "fuzzy" "id"))
1822 (let ((destination (if (string= type "fuzzy")
1823 (org-export-resolve-fuzzy-link link info)
1824 (org-export-resolve-id-link link info))))
1825 (case (org-element-type destination)
1826 ;; Id link points to an external file.
1827 (plain-text
1828 (if desc (format "\\href{file://%s}{%s}" destination desc)
1829 (format "\\url{file://%s}" destination)))
1830 ;; Fuzzy link points nowhere.
1831 ('nil
1832 (format org-latex-link-with-unknown-path-format
1833 (or desc
1834 (org-export-data
1835 (org-element-property :raw-link link) info))))
1836 ;; Fuzzy link points to an invisible target.
1837 (keyword nil)
1838 ;; LINK points to a headline. If headlines are numbered
1839 ;; and the link has no description, display headline's
1840 ;; number. Otherwise, display description or headline's
1841 ;; title.
1842 (headline
1843 (let ((label
1844 (format "sec-%s"
1845 (mapconcat
1846 'number-to-string
1847 (org-export-get-headline-number destination info)
1848 "-"))))
1849 (if (and (plist-get info :section-numbers) (not desc))
1850 (format "\\ref{%s}" label)
1851 (format "\\hyperref[%s]{%s}" label
1852 (or desc
1853 (org-export-data
1854 (org-element-property :title destination) info))))))
1855 ;; Fuzzy link points to a target. Do as above.
1856 (otherwise
1857 (let ((path (org-export-solidify-link-text path)))
1858 (if (not desc) (format "\\ref{%s}" path)
1859 (format "\\hyperref[%s]{%s}" path desc)))))))
1860 ;; Coderef: replace link with the reference name or the
1861 ;; equivalent line number.
1862 ((string= type "coderef")
1863 (format (org-export-get-coderef-format path desc)
1864 (org-export-resolve-coderef path info)))
1865 ;; Link type is handled by a special function.
1866 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1867 (funcall protocol (org-link-unescape path) desc 'latex))
1868 ;; External link with a description part.
1869 ((and path desc) (format "\\href{%s}{%s}" path desc))
1870 ;; External link without a description part.
1871 (path (format "\\url{%s}" path))
1872 ;; No path, only description. Try to do something useful.
1873 (t (format org-latex-link-with-unknown-path-format desc)))))
1876 ;;;; Paragraph
1878 (defun org-latex-paragraph (paragraph contents info)
1879 "Transcode a PARAGRAPH element from Org to LaTeX.
1880 CONTENTS is the contents of the paragraph, as a string. INFO is
1881 the plist used as a communication channel."
1882 contents)
1885 ;;;; Plain List
1887 (defun org-latex-plain-list (plain-list contents info)
1888 "Transcode a PLAIN-LIST element from Org to LaTeX.
1889 CONTENTS is the contents of the list. INFO is a plist holding
1890 contextual information."
1891 (let* ((type (org-element-property :type plain-list))
1892 (attr (org-export-read-attribute :attr_latex plain-list))
1893 (latex-type (let ((env (plist-get attr :environment)))
1894 (cond (env (format "%s" env))
1895 ((eq type 'ordered) "enumerate")
1896 ((eq type 'unordered) "itemize")
1897 ((eq type 'descriptive) "description")))))
1898 (org-latex--wrap-label
1899 plain-list
1900 (format "\\begin{%s}%s\n%s\\end{%s}"
1901 latex-type
1902 ;; Put optional arguments, if any inside square brackets
1903 ;; when necessary.
1904 (let ((options (format "%s" (or (plist-get attr :options) ""))))
1905 (cond ((equal options "") "")
1906 ((string-match "\\`\\[.*\\]\\'" options) options)
1907 (t (format "[%s]" options))))
1908 contents
1909 latex-type))))
1912 ;;;; Plain Text
1914 (defun org-latex-plain-text (text info)
1915 "Transcode a TEXT string from Org to LaTeX.
1916 TEXT is the string to transcode. INFO is a plist holding
1917 contextual information."
1918 (let ((specialp (plist-get info :with-special-strings))
1919 (output text))
1920 ;; Protect %, #, &, $, ~, ^, _, { and }.
1921 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" output)
1922 (setq output
1923 (replace-match
1924 (format "\\%s" (match-string 2 output)) nil t output 2)))
1925 ;; Protect \. If special strings are used, be careful not to
1926 ;; protect "\" in "\-" constructs.
1927 (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
1928 (setq output
1929 (replace-regexp-in-string
1930 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
1931 "$\\backslash$" output nil t 1)))
1932 ;; Activate smart quotes. Be sure to provide original TEXT string
1933 ;; since OUTPUT may have been modified.
1934 (when (plist-get info :with-smart-quotes)
1935 (setq output (org-export-activate-smart-quotes output :latex info text)))
1936 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1937 (let ((case-fold-search nil)
1938 (start 0))
1939 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
1940 (setq output (replace-match
1941 (format "\\%s{}" (match-string 1 output)) nil t output)
1942 start (match-end 0))))
1943 ;; Convert special strings.
1944 (when specialp
1945 (setq output
1946 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
1947 ;; Handle break preservation if required.
1948 (when (plist-get info :preserve-breaks)
1949 (setq output (replace-regexp-in-string
1950 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
1951 ;; Return value.
1952 output))
1955 ;;;; Planning
1957 (defun org-latex-planning (planning contents info)
1958 "Transcode a PLANNING element from Org to LaTeX.
1959 CONTENTS is nil. INFO is a plist holding contextual
1960 information."
1961 (concat
1962 "\\noindent"
1963 (mapconcat
1964 'identity
1965 (delq nil
1966 (list
1967 (let ((closed (org-element-property :closed planning)))
1968 (when closed
1969 (concat
1970 (format "\\textbf{%s} " org-closed-string)
1971 (format org-latex-inactive-timestamp-format
1972 (org-translate-time
1973 (org-element-property :raw-value closed))))))
1974 (let ((deadline (org-element-property :deadline planning)))
1975 (when deadline
1976 (concat
1977 (format "\\textbf{%s} " org-deadline-string)
1978 (format org-latex-active-timestamp-format
1979 (org-translate-time
1980 (org-element-property :raw-value deadline))))))
1981 (let ((scheduled (org-element-property :scheduled planning)))
1982 (when scheduled
1983 (concat
1984 (format "\\textbf{%s} " org-scheduled-string)
1985 (format org-latex-active-timestamp-format
1986 (org-translate-time
1987 (org-element-property :raw-value scheduled))))))))
1988 " ")
1989 "\\\\"))
1992 ;;;; Quote Block
1994 (defun org-latex-quote-block (quote-block contents info)
1995 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1996 CONTENTS holds the contents of the block. INFO is a plist
1997 holding contextual information."
1998 (org-latex--wrap-label
1999 quote-block
2000 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2003 ;;;; Quote Section
2005 (defun org-latex-quote-section (quote-section contents info)
2006 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2007 CONTENTS is nil. INFO is a plist holding contextual information."
2008 (let ((value (org-remove-indentation
2009 (org-element-property :value quote-section))))
2010 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
2013 ;;;; Radio Target
2015 (defun org-latex-radio-target (radio-target text info)
2016 "Transcode a RADIO-TARGET object from Org to LaTeX.
2017 TEXT is the text of the target. INFO is a plist holding
2018 contextual information."
2019 (format "\\label{%s}%s"
2020 (org-export-solidify-link-text
2021 (org-element-property :value radio-target))
2022 text))
2025 ;;;; Section
2027 (defun org-latex-section (section contents info)
2028 "Transcode a SECTION element from Org to LaTeX.
2029 CONTENTS holds the contents of the section. INFO is a plist
2030 holding contextual information."
2031 contents)
2034 ;;;; Special Block
2036 (defun org-latex-special-block (special-block contents info)
2037 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2038 CONTENTS holds the contents of the block. INFO is a plist
2039 holding contextual information."
2040 (let ((type (downcase (org-element-property :type special-block)))
2041 (opt (org-export-read-attribute :attr_latex special-block :options)))
2042 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2043 ;; Insert any label or caption within the block
2044 ;; (otherwise, a reference pointing to that element will
2045 ;; count the section instead).
2046 (org-latex--caption/label-string special-block info)
2047 contents
2048 (format "\\end{%s}" type))))
2051 ;;;; Src Block
2053 (defun org-latex-src-block (src-block contents info)
2054 "Transcode a SRC-BLOCK element from Org to LaTeX.
2055 CONTENTS holds the contents of the item. INFO is a plist holding
2056 contextual information."
2057 (when (org-string-nw-p (org-element-property :value src-block))
2058 (let* ((lang (org-element-property :language src-block))
2059 (caption (org-element-property :caption src-block))
2060 (label (org-element-property :name src-block))
2061 (custom-env (and lang
2062 (cadr (assq (intern lang)
2063 org-latex-custom-lang-environments))))
2064 (num-start (case (org-element-property :number-lines src-block)
2065 (continued (org-export-get-loc src-block info))
2066 (new 0)))
2067 (retain-labels (org-element-property :retain-labels src-block)))
2068 (cond
2069 ;; Case 1. No source fontification.
2070 ((not org-latex-listings)
2071 (let ((caption-str (org-latex--caption/label-string src-block info))
2072 (float-env (and caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
2073 (format
2074 (or float-env "%s")
2075 (concat caption-str
2076 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2077 (org-export-format-code-default src-block info))))))
2078 ;; Case 2. Custom environment.
2079 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2080 custom-env
2081 (org-export-format-code-default src-block info)
2082 custom-env))
2083 ;; Case 3. Use minted package.
2084 ((eq org-latex-listings 'minted)
2085 (let ((float-env
2086 (when (or label caption)
2087 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2088 (org-latex--caption/label-string src-block info))))
2089 (body
2090 (format
2091 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2092 ;; Options.
2093 (org-latex--make-option-string
2094 (if (not num-start) org-latex-minted-options
2095 (append `(("linenos")
2096 ("firstnumber" ,(number-to-string (1+ num-start))))
2097 org-latex-minted-options)))
2098 ;; Language.
2099 (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
2100 ;; Source code.
2101 (let* ((code-info (org-export-unravel-code src-block))
2102 (max-width
2103 (apply 'max
2104 (mapcar 'length
2105 (org-split-string (car code-info)
2106 "\n")))))
2107 (org-export-format-code
2108 (car code-info)
2109 (lambda (loc num ref)
2110 (concat
2112 (when ref
2113 ;; Ensure references are flushed to the right,
2114 ;; separated with 6 spaces from the widest line
2115 ;; of code.
2116 (concat (make-string (+ (- max-width (length loc)) 6)
2117 ?\s)
2118 (format "(%s)" ref)))))
2119 nil (and retain-labels (cdr code-info)))))))
2120 ;; Return value.
2121 (if float-env (format float-env body) body)))
2122 ;; Case 4. Use listings package.
2124 (let ((lst-lang
2125 (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
2126 (caption-str
2127 (when caption
2128 (let ((main (org-export-get-caption src-block))
2129 (secondary (org-export-get-caption src-block t)))
2130 (if (not secondary)
2131 (format "{%s}" (org-export-data main info))
2132 (format "{[%s]%s}"
2133 (org-export-data secondary info)
2134 (org-export-data main info)))))))
2135 (concat
2136 ;; Options.
2137 (format "\\lstset{%s}\n"
2138 (org-latex--make-option-string
2139 (append org-latex-listings-options
2140 `(("language" ,lst-lang))
2141 (when label `(("label" ,label)))
2142 (when caption-str `(("caption" ,caption-str)))
2143 (cond ((not num-start) '(("numbers" "none")))
2144 ((zerop num-start) '(("numbers" "left")))
2145 (t `(("numbers" "left")
2146 ("firstnumber"
2147 ,(number-to-string (1+ num-start)))))))))
2148 ;; Source code.
2149 (format
2150 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2151 (let* ((code-info (org-export-unravel-code src-block))
2152 (max-width
2153 (apply 'max
2154 (mapcar 'length
2155 (org-split-string (car code-info) "\n")))))
2156 (org-export-format-code
2157 (car code-info)
2158 (lambda (loc num ref)
2159 (concat
2161 (when ref
2162 ;; Ensure references are flushed to the right,
2163 ;; separated with 6 spaces from the widest line of
2164 ;; code
2165 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2166 (format "(%s)" ref)))))
2167 nil (and retain-labels (cdr code-info))))))))))))
2170 ;;;; Statistics Cookie
2172 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2173 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2174 CONTENTS is nil. INFO is a plist holding contextual information."
2175 (replace-regexp-in-string
2176 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2179 ;;;; Strike-Through
2181 (defun org-latex-strike-through (strike-through contents info)
2182 "Transcode STRIKE-THROUGH from Org to LaTeX.
2183 CONTENTS is the text with strike-through markup. INFO is a plist
2184 holding contextual information."
2185 (org-latex--text-markup contents 'strike-through))
2188 ;;;; Subscript
2190 (defun org-latex-subscript (subscript contents info)
2191 "Transcode a SUBSCRIPT object from Org to LaTeX.
2192 CONTENTS is the contents of the object. INFO is a plist holding
2193 contextual information."
2194 (if (= (length contents) 1) (format "$_%s$" contents)
2195 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2196 ;; command for each of them.
2197 (let ((prev-blanks 0))
2198 (mapconcat
2199 (lambda (obj)
2200 (case (org-element-type obj)
2201 ((entity latex-fragment)
2202 (setq prev-blanks (org-element-property :post-blank obj))
2203 (let ((data (org-trim (org-export-data obj info))))
2204 (string-match
2205 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2206 data)
2207 (format "$_{%s}$" (match-string 1 data))))
2208 (plain-text
2209 (format "$_\\mathrm{%s}$"
2210 (concat (make-string prev-blanks ? )
2211 ;; mathrm command doesn't handle spaces,
2212 ;; so we have to enforce them.
2213 (replace-regexp-in-string
2214 " " "\\\\ " (org-export-data obj info)))))
2215 (otherwise
2216 (setq prev-blanks (org-element-property :post-blank obj))
2217 (format "$_{%s}$" (org-export-data obj info)))))
2218 (org-element-contents subscript) ""))))
2221 ;;;; Superscript
2223 (defun org-latex-superscript (superscript contents info)
2224 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2225 CONTENTS is the contents of the object. INFO is a plist holding
2226 contextual information."
2227 (if (= (length contents) 1) (format "$^%s$" contents)
2228 ;; Handle multiple objects in SUPERSCRIPT by creating
2229 ;; a superscript command for each of them.
2230 (let ((prev-blanks 0))
2231 (mapconcat
2232 (lambda (obj)
2233 (case (org-element-type obj)
2234 ((entity latex-fragment)
2235 (setq prev-blanks (org-element-property :post-blank obj))
2236 (let ((data (org-trim (org-export-data obj info))))
2237 (string-match
2238 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2239 data)
2240 (format "$^{%s}$" (match-string 1 data))))
2241 (plain-text
2242 (format "$^\\mathrm{%s}$"
2243 (concat (make-string prev-blanks ? )
2244 ;; mathrm command doesn't handle spaces,
2245 ;; so we have to enforce them.
2246 (replace-regexp-in-string
2247 " " "\\\\ " (org-export-data obj info)))))
2248 (otherwise
2249 (setq prev-blanks (org-element-property :post-blank obj))
2250 (format "$^{%s}$" (org-export-data obj info)))))
2251 (org-element-contents superscript) ""))))
2254 ;;;; Table
2256 ;; `org-latex-table' is the entry point for table transcoding. It
2257 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2258 ;; delegates the job to either `org-latex--table.el-table',
2259 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2260 ;; depending of the type of the table and the mode requested.
2262 ;; `org-latex--align-string' is a subroutine used to build alignment
2263 ;; string for Org tables.
2265 (defun org-latex-table (table contents info)
2266 "Transcode a TABLE element from Org to LaTeX.
2267 CONTENTS is the contents of the table. INFO is a plist holding
2268 contextual information."
2269 (if (eq (org-element-property :type table) 'table.el)
2270 ;; "table.el" table. Convert it using appropriate tools.
2271 (org-latex--table.el-table table info)
2272 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2273 org-latex-default-table-mode)))
2274 (cond
2275 ;; Case 1: Verbatim table.
2276 ((string= type "verbatim")
2277 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2278 ;; Re-create table, without affiliated keywords.
2279 (org-trim (org-element-interpret-data
2280 `(table nil ,@(org-element-contents table))))))
2281 ;; Case 2: Matrix.
2282 ((or (string= type "math") (string= type "inline-math"))
2283 (org-latex--math-table table info))
2284 ;; Case 3: Standard table.
2285 (t (concat (org-latex--org-table table contents info)
2286 ;; When there are footnote references within the
2287 ;; table, insert their definition just after it.
2288 (org-latex--delayed-footnotes-definitions table info)))))))
2290 (defun org-latex--align-string (table info)
2291 "Return an appropriate LaTeX alignment string.
2292 TABLE is the considered table. INFO is a plist used as
2293 a communication channel."
2294 (or (org-export-read-attribute :attr_latex table :align)
2295 (let (align)
2296 ;; Extract column groups and alignment from first (non-rule)
2297 ;; row.
2298 (org-element-map
2299 (org-element-map table 'table-row
2300 (lambda (row)
2301 (and (eq (org-element-property :type row) 'standard) row))
2302 info 'first-match)
2303 'table-cell
2304 (lambda (cell)
2305 (let ((borders (org-export-table-cell-borders cell info)))
2306 ;; Check left border for the first cell only.
2307 (when (and (memq 'left borders) (not align))
2308 (push "|" align))
2309 (push (case (org-export-table-cell-alignment cell info)
2310 (left "l")
2311 (right "r")
2312 (center "c"))
2313 align)
2314 (when (memq 'right borders) (push "|" align))))
2315 info)
2316 (apply 'concat (nreverse align)))))
2318 (defun org-latex--org-table (table contents info)
2319 "Return appropriate LaTeX code for an Org table.
2321 TABLE is the table type element to transcode. CONTENTS is its
2322 contents, as a string. INFO is a plist used as a communication
2323 channel.
2325 This function assumes TABLE has `org' as its `:type' property and
2326 `table' as its `:mode' attribute."
2327 (let* ((caption (org-latex--caption/label-string table info))
2328 (attr (org-export-read-attribute :attr_latex table))
2329 ;; Determine alignment string.
2330 (alignment (org-latex--align-string table info))
2331 ;; Determine environment for the table: longtable, tabular...
2332 (table-env (let ((env (plist-get attr :environment)))
2333 (if env (format "%s" env)
2334 org-latex-default-table-environment)))
2335 ;; If table is a float, determine environment: table, table*
2336 ;; or sidewaystable.
2337 (float-env (unless (equal "longtable" table-env)
2338 (let ((float (plist-get attr :float)))
2339 (cond
2340 ((string= float "sidewaystable") "sidewaystable")
2341 ((string= float "multicolumn") "table*")
2342 ((or (string= float "table")
2343 (org-element-property :caption table))
2344 "table")))))
2345 ;; Extract others display options.
2346 (fontsize (let ((font (plist-get attr :font)))
2347 (and font (concat (org-trim (format "%s" font)) "\n"))))
2348 (width (plist-get attr :width))
2349 (placement (or (plist-get attr :placement)
2350 (format "[%s]" org-latex-default-figure-position)))
2351 (centerp (if (plist-member attr :center) (plist-get attr :center)
2352 org-latex-tables-centered)))
2353 ;; Prepare the final format string for the table.
2354 (cond
2355 ;; Longtable.
2356 ((equal "longtable" table-env)
2357 (concat (and fontsize (concat "{" fontsize))
2358 (format "\\begin{longtable}{%s}\n" alignment)
2359 (and org-latex-table-caption-above
2360 (org-string-nw-p caption)
2361 (concat caption "\\\\\n"))
2362 contents
2363 (and (not org-latex-table-caption-above)
2364 (org-string-nw-p caption)
2365 (concat caption "\\\\\n"))
2366 "\\end{longtable}\n"
2367 (and fontsize "}")))
2368 ;; Others.
2369 (t (concat (cond
2370 (float-env
2371 (concat (format "\\begin{%s}%s\n" float-env placement)
2372 (if org-latex-table-caption-above caption "")
2373 (when centerp "\\centering\n")
2374 fontsize))
2375 (centerp (concat "\\begin{center}\n" fontsize))
2376 (fontsize (concat "{" fontsize)))
2377 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2378 table-env
2379 (if width (format "{%s}" width) "")
2380 alignment
2381 contents
2382 table-env)
2383 (cond
2384 (float-env
2385 (concat (if org-latex-table-caption-above "" caption)
2386 (format "\n\\end{%s}" float-env)))
2387 (centerp "\n\\end{center}")
2388 (fontsize "}")))))))
2390 (defun org-latex--table.el-table (table info)
2391 "Return appropriate LaTeX code for a table.el table.
2393 TABLE is the table type element to transcode. INFO is a plist
2394 used as a communication channel.
2396 This function assumes TABLE has `table.el' as its `:type'
2397 property."
2398 (require 'table)
2399 ;; Ensure "*org-export-table*" buffer is empty.
2400 (with-current-buffer (get-buffer-create "*org-export-table*")
2401 (erase-buffer))
2402 (let ((output (with-temp-buffer
2403 (insert (org-element-property :value table))
2404 (goto-char 1)
2405 (re-search-forward "^[ \t]*|[^|]" nil t)
2406 (table-generate-source 'latex "*org-export-table*")
2407 (with-current-buffer "*org-export-table*"
2408 (org-trim (buffer-string))))))
2409 (kill-buffer (get-buffer "*org-export-table*"))
2410 ;; Remove left out comments.
2411 (while (string-match "^%.*\n" output)
2412 (setq output (replace-match "" t t output)))
2413 (let ((attr (org-export-read-attribute :attr_latex table)))
2414 (when (plist-get attr :rmlines)
2415 ;; When the "rmlines" attribute is provided, remove all hlines
2416 ;; but the the one separating heading from the table body.
2417 (let ((n 0) (pos 0))
2418 (while (and (< (length output) pos)
2419 (setq pos (string-match "^\\\\hline\n?" output pos)))
2420 (incf n)
2421 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2422 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2423 org-latex-tables-centered)))
2424 (if (not centerp) output
2425 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2427 (defun org-latex--math-table (table info)
2428 "Return appropriate LaTeX code for a matrix.
2430 TABLE is the table type element to transcode. INFO is a plist
2431 used as a communication channel.
2433 This function assumes TABLE has `org' as its `:type' property and
2434 `inline-math' or `math' as its `:mode' attribute.."
2435 (let* ((caption (org-latex--caption/label-string table info))
2436 (attr (org-export-read-attribute :attr_latex table))
2437 (inlinep (eq (plist-get attr :mode) 'inline-math))
2438 (env (let ((env (plist-get attr :environment)))
2439 (if env (format "%s" env)
2440 org-latex-default-table-environment)))
2441 (contents
2442 (mapconcat
2443 (lambda (row)
2444 ;; Ignore horizontal rules.
2445 (when (eq (org-element-property :type row) 'standard)
2446 ;; Return each cell unmodified.
2447 (concat
2448 (mapconcat
2449 (lambda (cell)
2450 (substring (org-element-interpret-data cell) 0 -1))
2451 (org-element-map row 'table-cell 'identity info) "&")
2452 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2453 "\n")))
2454 (org-element-map table 'table-row 'identity info) ""))
2455 ;; Variables related to math clusters (contiguous math tables
2456 ;; of the same type).
2457 (mode (org-export-read-attribute :attr_latex table :mode))
2458 (prev (org-export-get-previous-element table info))
2459 (next (org-export-get-next-element table info))
2460 (same-mode-p
2461 (lambda (table)
2462 ;; Non-nil when TABLE has the same mode as current table.
2463 (string= (or (org-export-read-attribute :attr_latex table :mode)
2464 org-latex-default-table-mode)
2465 mode))))
2466 (concat
2467 ;; Opening string. If TABLE is in the middle of a table cluster,
2468 ;; do not insert any.
2469 (cond ((and prev
2470 (eq (org-element-type prev) 'table)
2471 (memq (org-element-property :post-blank prev) '(0 nil))
2472 (funcall same-mode-p prev))
2473 nil)
2474 (inlinep "\\(")
2475 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2476 (t "\\["))
2477 ;; Prefix (make sure it is a string).
2478 (format "%s" (or (plist-get attr :math-prefix) ""))
2479 ;; Environment. Also treat special cases.
2480 (cond ((equal env "array")
2481 (let ((align (org-latex--align-string table info)))
2482 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2483 ((assoc env org-latex-table-matrix-macros)
2484 (format "\\%s%s{\n%s}" env
2485 (format "%s" (or (plist-get attr :math-arguments) ""))
2486 contents))
2487 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2488 ;; Suffix (make sure it is a string).
2489 (format "%s" (or (plist-get attr :math-suffix) ""))
2490 ;; Closing string. If TABLE is in the middle of a table cluster,
2491 ;; do not insert any. If it closes such a cluster, be sure to
2492 ;; close the cluster with a string matching the opening string.
2493 (cond ((and next
2494 (eq (org-element-type next) 'table)
2495 (memq (org-element-property :post-blank table) '(0 nil))
2496 (funcall same-mode-p next))
2497 nil)
2498 (inlinep "\\)")
2499 ;; Find cluster beginning to know which environment to use.
2500 ((let ((cluster-beg table) prev)
2501 (while (and (setq prev (org-export-get-previous-element
2502 cluster-beg info))
2503 (memq (org-element-property :post-blank prev)
2504 '(0 nil))
2505 (funcall same-mode-p prev))
2506 (setq cluster-beg prev))
2507 (and (or (org-element-property :caption cluster-beg)
2508 (org-element-property :name cluster-beg))
2509 "\n\\end{equation}")))
2510 (t "\\]")))))
2513 ;;;; Table Cell
2515 (defun org-latex-table-cell (table-cell contents info)
2516 "Transcode a TABLE-CELL element from Org to LaTeX.
2517 CONTENTS is the cell contents. INFO is a plist used as
2518 a communication channel."
2519 (concat (if (and contents
2520 org-latex-table-scientific-notation
2521 (string-match orgtbl-exp-regexp contents))
2522 ;; Use appropriate format string for scientific
2523 ;; notation.
2524 (format org-latex-table-scientific-notation
2525 (match-string 1 contents)
2526 (match-string 2 contents))
2527 contents)
2528 (when (org-export-get-next-element table-cell info) " & ")))
2531 ;;;; Table Row
2533 (defun org-latex-table-row (table-row contents info)
2534 "Transcode a TABLE-ROW element from Org to LaTeX.
2535 CONTENTS is the contents of the row. INFO is a plist used as
2536 a communication channel."
2537 ;; Rules are ignored since table separators are deduced from
2538 ;; borders of the current row.
2539 (when (eq (org-element-property :type table-row) 'standard)
2540 (let* ((attr (org-export-read-attribute :attr_latex
2541 (org-export-get-parent table-row)))
2542 (longtablep (string= (or (plist-get attr :environment)
2543 org-latex-default-table-environment)
2544 "longtable"))
2545 (booktabsp (if (plist-member attr :booktabs)
2546 (plist-get attr :booktabs)
2547 org-latex-tables-booktabs))
2548 ;; TABLE-ROW's borders are extracted from its first cell.
2549 (borders (org-export-table-cell-borders
2550 (car (org-element-contents table-row)) info)))
2551 (concat
2552 ;; When BOOKTABS are activated enforce top-rule even when no
2553 ;; hline was specifically marked.
2554 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2555 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2556 contents "\\\\\n"
2557 (cond
2558 ;; Special case for long tables. Define header and footers.
2559 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2560 (format "%s
2561 \\endhead
2562 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2563 \\endfoot
2564 \\endlastfoot"
2565 (if booktabsp "\\midrule" "\\hline")
2566 (if booktabsp "\\midrule" "\\hline")
2567 ;; Number of columns.
2568 (cdr (org-export-table-dimensions
2569 (org-export-get-parent-table table-row) info))))
2570 ;; When BOOKTABS are activated enforce bottom rule even when
2571 ;; no hline was specifically marked.
2572 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2573 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2574 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2577 ;;;; Target
2579 (defun org-latex-target (target contents info)
2580 "Transcode a TARGET object from Org to LaTeX.
2581 CONTENTS is nil. INFO is a plist holding contextual
2582 information."
2583 (format "\\label{%s}"
2584 (org-export-solidify-link-text (org-element-property :value target))))
2587 ;;;; Timestamp
2589 (defun org-latex-timestamp (timestamp contents info)
2590 "Transcode a TIMESTAMP object from Org to LaTeX.
2591 CONTENTS is nil. INFO is a plist holding contextual
2592 information."
2593 (let ((value (org-latex-plain-text
2594 (org-timestamp-translate timestamp) info)))
2595 (case (org-element-property :type timestamp)
2596 ((active active-range) (format org-latex-active-timestamp-format value))
2597 ((inactive inactive-range)
2598 (format org-latex-inactive-timestamp-format value))
2599 (otherwise (format org-latex-diary-timestamp-format value)))))
2602 ;;;; Underline
2604 (defun org-latex-underline (underline contents info)
2605 "Transcode UNDERLINE from Org to LaTeX.
2606 CONTENTS is the text with underline markup. INFO is a plist
2607 holding contextual information."
2608 (org-latex--text-markup contents 'underline))
2611 ;;;; Verbatim
2613 (defun org-latex-verbatim (verbatim contents info)
2614 "Transcode a VERBATIM object from Org to LaTeX.
2615 CONTENTS is nil. INFO is a plist used as a communication
2616 channel."
2617 (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2620 ;;;; Verse Block
2622 (defun org-latex-verse-block (verse-block contents info)
2623 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2624 CONTENTS is verse block contents. INFO is a plist holding
2625 contextual information."
2626 (org-latex--wrap-label
2627 verse-block
2628 ;; In a verse environment, add a line break to each newline
2629 ;; character and change each white space at beginning of a line
2630 ;; into a space of 1 em. Also change each blank line with
2631 ;; a vertical space of 1 em.
2632 (progn
2633 (setq contents (replace-regexp-in-string
2634 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2635 (replace-regexp-in-string
2636 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2637 (while (string-match "^[ \t]+" contents)
2638 (let ((new-str (format "\\hspace*{%dem}"
2639 (length (match-string 0 contents)))))
2640 (setq contents (replace-match new-str nil t contents))))
2641 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2645 ;;; End-user functions
2647 ;;;###autoload
2648 (defun org-latex-export-as-latex
2649 (&optional async subtreep visible-only body-only ext-plist)
2650 "Export current buffer as a LaTeX buffer.
2652 If narrowing is active in the current buffer, only export its
2653 narrowed part.
2655 If a region is active, export that region.
2657 A non-nil optional argument ASYNC means the process should happen
2658 asynchronously. The resulting buffer should be accessible
2659 through the `org-export-stack' interface.
2661 When optional argument SUBTREEP is non-nil, export the sub-tree
2662 at point, extracting information from the headline properties
2663 first.
2665 When optional argument VISIBLE-ONLY is non-nil, don't export
2666 contents of hidden elements.
2668 When optional argument BODY-ONLY is non-nil, only write code
2669 between \"\\begin{document}\" and \"\\end{document}\".
2671 EXT-PLIST, when provided, is a property list with external
2672 parameters overriding Org default settings, but still inferior to
2673 file-local settings.
2675 Export is done in a buffer named \"*Org LATEX Export*\", which
2676 will be displayed when `org-export-show-temporary-export-buffer'
2677 is non-nil."
2678 (interactive)
2679 (if async
2680 (org-export-async-start
2681 (lambda (output)
2682 (with-current-buffer (get-buffer-create "*Org LATEX Export*")
2683 (erase-buffer)
2684 (insert output)
2685 (goto-char (point-min))
2686 (LaTeX-mode)
2687 (org-export-add-to-stack (current-buffer) 'latex)))
2688 `(org-export-as 'latex ,subtreep ,visible-only ,body-only
2689 ',ext-plist))
2690 (let ((outbuf
2691 (org-export-to-buffer 'latex "*Org LATEX Export*"
2692 subtreep visible-only body-only ext-plist)))
2693 (with-current-buffer outbuf (LaTeX-mode))
2694 (when org-export-show-temporary-export-buffer
2695 (switch-to-buffer-other-window outbuf)))))
2697 ;;;###autoload
2698 (defun org-latex-export-to-latex
2699 (&optional async subtreep visible-only body-only ext-plist)
2700 "Export current buffer to a LaTeX file.
2702 If narrowing is active in the current buffer, only export its
2703 narrowed part.
2705 If a region is active, export that region.
2707 A non-nil optional argument ASYNC means the process should happen
2708 asynchronously. The resulting file should be accessible through
2709 the `org-export-stack' interface.
2711 When optional argument SUBTREEP is non-nil, export the sub-tree
2712 at point, extracting information from the headline properties
2713 first.
2715 When optional argument VISIBLE-ONLY is non-nil, don't export
2716 contents of hidden elements.
2718 When optional argument BODY-ONLY is non-nil, only write code
2719 between \"\\begin{document}\" and \"\\end{document}\".
2721 EXT-PLIST, when provided, is a property list with external
2722 parameters overriding Org default settings, but still inferior to
2723 file-local settings.
2725 Return output file's name."
2726 (interactive)
2727 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2728 (if async
2729 (org-export-async-start
2730 (lambda (f) (org-export-add-to-stack f 'latex))
2731 `(expand-file-name
2732 (org-export-to-file
2733 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
2734 (org-export-to-file
2735 'latex outfile subtreep visible-only body-only ext-plist))))
2737 ;;;###autoload
2738 (defun org-latex-export-to-pdf
2739 (&optional async subtreep visible-only body-only ext-plist)
2740 "Export current buffer to LaTeX then process through to PDF.
2742 If narrowing is active in the current buffer, only export its
2743 narrowed part.
2745 If a region is active, export that region.
2747 A non-nil optional argument ASYNC means the process should happen
2748 asynchronously. The resulting file should be accessible through
2749 the `org-export-stack' interface.
2751 When optional argument SUBTREEP is non-nil, export the sub-tree
2752 at point, extracting information from the headline properties
2753 first.
2755 When optional argument VISIBLE-ONLY is non-nil, don't export
2756 contents of hidden elements.
2758 When optional argument BODY-ONLY is non-nil, only write code
2759 between \"\\begin{document}\" and \"\\end{document}\".
2761 EXT-PLIST, when provided, is a property list with external
2762 parameters overriding Org default settings, but still inferior to
2763 file-local settings.
2765 Return PDF file's name."
2766 (interactive)
2767 (if async
2768 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2769 (org-export-async-start
2770 (lambda (f) (org-export-add-to-stack f 'latex))
2771 `(expand-file-name
2772 (org-latex-compile
2773 (org-export-to-file
2774 'latex ,outfile ,subtreep ,visible-only ,body-only
2775 ',ext-plist)))))
2776 (org-latex-compile
2777 (org-latex-export-to-latex
2778 nil subtreep visible-only body-only ext-plist))))
2780 (defun org-latex-compile (texfile)
2781 "Compile a TeX file.
2783 TEXFILE is the name of the file being compiled. Processing is
2784 done through the command specified in `org-latex-pdf-process'.
2786 Return PDF file name or an error if it couldn't be produced."
2787 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
2788 (full-name (file-truename texfile))
2789 (out-dir (file-name-directory texfile))
2790 ;; Make sure `default-directory' is set to TEXFILE directory,
2791 ;; not to whatever value the current buffer may have.
2792 (default-directory (file-name-directory full-name))
2793 errors)
2794 (message (format "Processing LaTeX file %s ..." texfile))
2795 (save-window-excursion
2796 (cond
2797 ;; A function is provided: Apply it.
2798 ((functionp org-latex-pdf-process)
2799 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
2800 ;; A list is provided: Replace %b, %f and %o with appropriate
2801 ;; values in each command before applying it. Output is
2802 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2803 ((consp org-latex-pdf-process)
2804 (let ((outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2805 (mapc
2806 (lambda (command)
2807 (shell-command
2808 (replace-regexp-in-string
2809 "%b" (shell-quote-argument base-name)
2810 (replace-regexp-in-string
2811 "%f" (shell-quote-argument full-name)
2812 (replace-regexp-in-string
2813 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2814 outbuf))
2815 org-latex-pdf-process)
2816 ;; Collect standard errors from output buffer.
2817 (setq errors (org-latex--collect-errors outbuf))))
2818 (t (error "No valid command to process to PDF")))
2819 (let ((pdffile (concat out-dir base-name ".pdf")))
2820 ;; Check for process failure. Provide collected errors if
2821 ;; possible.
2822 (if (not (file-exists-p pdffile))
2823 (error (concat (format "PDF file %s wasn't produced" pdffile)
2824 (when errors (concat ": " errors))))
2825 ;; Else remove log files, when specified, and signal end of
2826 ;; process to user, along with any error encountered.
2827 (when org-latex-remove-logfiles
2828 (dolist (ext org-latex-logfiles-extensions)
2829 (let ((file (concat out-dir base-name "." ext)))
2830 (when (file-exists-p file) (delete-file file)))))
2831 (message (concat "Process completed"
2832 (if (not errors) "."
2833 (concat " with errors: " errors)))))
2834 ;; Return output file name.
2835 pdffile))))
2837 (defun org-latex--collect-errors (buffer)
2838 "Collect some kind of errors from \"pdflatex\" command output.
2840 BUFFER is the buffer containing output.
2842 Return collected error types as a string, or nil if there was
2843 none."
2844 (with-current-buffer buffer
2845 (save-excursion
2846 (goto-char (point-max))
2847 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
2848 (let ((case-fold-search t)
2849 (errors ""))
2850 (dolist (latex-error org-latex-known-errors)
2851 (when (save-excursion (re-search-forward (car latex-error) nil t))
2852 (setq errors (concat errors " " (cdr latex-error)))))
2853 (and (org-string-nw-p errors) (org-trim errors)))))))
2855 ;;;###autoload
2856 (defun org-latex-publish-to-latex (plist filename pub-dir)
2857 "Publish an Org file to LaTeX.
2859 FILENAME is the filename of the Org file to be published. PLIST
2860 is the property list for the given project. PUB-DIR is the
2861 publishing directory.
2863 Return output file name."
2864 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
2866 ;;;###autoload
2867 (defun org-latex-publish-to-pdf (plist filename pub-dir)
2868 "Publish an Org file to PDF (via LaTeX).
2870 FILENAME is the filename of the Org file to be published. PLIST
2871 is the property list for the given project. PUB-DIR is the
2872 publishing directory.
2874 Return output file name."
2875 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
2876 ;; in working directory and then moved to publishing directory.
2877 (org-publish-attachment
2878 plist
2879 (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
2880 pub-dir))
2883 (provide 'ox-latex)
2885 ;; Local variables:
2886 ;; generated-autoload-file: "org-loaddefs.el"
2887 ;; End:
2889 ;;; ox-latex.el ends here