fixed broken @index commands in documentation
[org-mode.git] / lisp / ox-latex.el
blobf11d5d44f5c5daa26623890d0099360b57fb3da6
1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; See Org manual for details.
27 ;;; Code:
29 (eval-when-compile (require 'cl))
30 (require 'ox)
31 (require 'ox-publish)
33 (defvar org-latex-default-packages-alist)
34 (defvar org-latex-packages-alist)
35 (defvar orgtbl-exp-regexp)
39 ;;; Define Back-End
41 (org-export-define-backend 'latex
42 '((bold . org-latex-bold)
43 (center-block . org-latex-center-block)
44 (clock . org-latex-clock)
45 (code . org-latex-code)
46 (comment . (lambda (&rest args) ""))
47 (comment-block . (lambda (&rest args) ""))
48 (drawer . org-latex-drawer)
49 (dynamic-block . org-latex-dynamic-block)
50 (entity . org-latex-entity)
51 (example-block . org-latex-example-block)
52 (export-block . org-latex-export-block)
53 (export-snippet . org-latex-export-snippet)
54 (fixed-width . org-latex-fixed-width)
55 (footnote-definition . org-latex-footnote-definition)
56 (footnote-reference . org-latex-footnote-reference)
57 (headline . org-latex-headline)
58 (horizontal-rule . org-latex-horizontal-rule)
59 (inline-src-block . org-latex-inline-src-block)
60 (inlinetask . org-latex-inlinetask)
61 (italic . org-latex-italic)
62 (item . org-latex-item)
63 (keyword . org-latex-keyword)
64 (latex-environment . org-latex-latex-environment)
65 (latex-fragment . org-latex-latex-fragment)
66 (line-break . org-latex-line-break)
67 (link . org-latex-link)
68 (node-property . org-latex-node-property)
69 (paragraph . org-latex-paragraph)
70 (plain-list . org-latex-plain-list)
71 (plain-text . org-latex-plain-text)
72 (planning . org-latex-planning)
73 (property-drawer . org-latex-property-drawer)
74 (quote-block . org-latex-quote-block)
75 (radio-target . org-latex-radio-target)
76 (section . org-latex-section)
77 (special-block . org-latex-special-block)
78 (src-block . org-latex-src-block)
79 (statistics-cookie . org-latex-statistics-cookie)
80 (strike-through . org-latex-strike-through)
81 (subscript . org-latex-subscript)
82 (superscript . org-latex-superscript)
83 (table . org-latex-table)
84 (table-cell . org-latex-table-cell)
85 (table-row . org-latex-table-row)
86 (target . org-latex-target)
87 (template . org-latex-template)
88 (timestamp . org-latex-timestamp)
89 (underline . org-latex-underline)
90 (verbatim . org-latex-verbatim)
91 (verse-block . org-latex-verse-block)
92 ;; Pseudo objects.
93 (latex-math-block . org-latex-math-block))
94 :export-block '("LATEX" "TEX")
95 :menu-entry
96 '(?l "Export to LaTeX"
97 ((?L "As LaTeX buffer" org-latex-export-as-latex)
98 (?l "As LaTeX file" org-latex-export-to-latex)
99 (?p "As PDF file" org-latex-export-to-pdf)
100 (?o "As PDF file and open"
101 (lambda (a s v b)
102 (if a (org-latex-export-to-pdf t s v b)
103 (org-open-file (org-latex-export-to-pdf nil s v b)))))))
104 :options-alist '((:date "DATE" nil "\\today" t)
105 (:latex-class "LATEX_CLASS" nil org-latex-default-class t)
106 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
107 (:latex-header "LATEX_HEADER" nil nil newline)
108 (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
109 (:latex-hyperref nil nil org-latex-hyperref-template t)
110 (:latex-custom-id-labels nil nil org-latex-custom-id-as-label))
111 :filters-alist '((:filter-options . org-latex-math-block-options-filter)
112 (:filter-parse-tree . org-latex-math-block-tree-filter)))
116 ;;; Internal Variables
118 (defconst org-latex-babel-language-alist
119 '(("af" . "afrikaans")
120 ("bg" . "bulgarian")
121 ("bt-br" . "brazilian")
122 ("ca" . "catalan")
123 ("cs" . "czech")
124 ("cy" . "welsh")
125 ("da" . "danish")
126 ("de" . "germanb")
127 ("de-at" . "naustrian")
128 ("de-de" . "ngerman")
129 ("el" . "greek")
130 ("en" . "english")
131 ("en-au" . "australian")
132 ("en-ca" . "canadian")
133 ("en-gb" . "british")
134 ("en-ie" . "irish")
135 ("en-nz" . "newzealand")
136 ("en-us" . "american")
137 ("es" . "spanish")
138 ("et" . "estonian")
139 ("eu" . "basque")
140 ("fi" . "finnish")
141 ("fr" . "frenchb")
142 ("fr-ca" . "canadien")
143 ("gl" . "galician")
144 ("hr" . "croatian")
145 ("hu" . "hungarian")
146 ("id" . "indonesian")
147 ("is" . "icelandic")
148 ("it" . "italian")
149 ("la" . "latin")
150 ("ms" . "malay")
151 ("nl" . "dutch")
152 ("nb" . "norsk")
153 ("nn" . "nynorsk")
154 ("no" . "norsk")
155 ("pl" . "polish")
156 ("pt" . "portuguese")
157 ("ro" . "romanian")
158 ("ru" . "russian")
159 ("sa" . "sanskrit")
160 ("sb" . "uppersorbian")
161 ("sk" . "slovak")
162 ("sl" . "slovene")
163 ("sq" . "albanian")
164 ("sr" . "serbian")
165 ("sv" . "swedish")
166 ("ta" . "tamil")
167 ("tr" . "turkish")
168 ("uk" . "ukrainian"))
169 "Alist between language code and corresponding Babel option.")
171 (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
172 ("qbordermatrix" . "\\cr")
173 ("kbordermatrix" . "\\\\"))
174 "Alist between matrix macros and their row ending.")
176 (defconst org-latex-pseudo-objects '(latex-math-block)
177 "List of pseudo-object types introduced in the back-end.")
181 ;;; User Configurable Variables
183 (defgroup org-export-latex nil
184 "Options for exporting Org mode files to LaTeX."
185 :tag "Org Export LaTeX"
186 :group 'org-export)
189 ;;;; Preamble
191 (defcustom org-latex-default-class "article"
192 "The default LaTeX class."
193 :group 'org-export-latex
194 :type '(string :tag "LaTeX class"))
196 (defcustom org-latex-classes
197 '(("article"
198 "\\documentclass[11pt]{article}"
199 ("\\section{%s}" . "\\section*{%s}")
200 ("\\subsection{%s}" . "\\subsection*{%s}")
201 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
202 ("\\paragraph{%s}" . "\\paragraph*{%s}")
203 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
204 ("report"
205 "\\documentclass[11pt]{report}"
206 ("\\part{%s}" . "\\part*{%s}")
207 ("\\chapter{%s}" . "\\chapter*{%s}")
208 ("\\section{%s}" . "\\section*{%s}")
209 ("\\subsection{%s}" . "\\subsection*{%s}")
210 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
211 ("book"
212 "\\documentclass[11pt]{book}"
213 ("\\part{%s}" . "\\part*{%s}")
214 ("\\chapter{%s}" . "\\chapter*{%s}")
215 ("\\section{%s}" . "\\section*{%s}")
216 ("\\subsection{%s}" . "\\subsection*{%s}")
217 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
218 "Alist of LaTeX classes and associated header and structure.
219 If #+LATEX_CLASS is set in the buffer, use its value and the
220 associated information. Here is the structure of each cell:
222 \(class-name
223 header-string
224 \(numbered-section . unnumbered-section)
225 ...)
227 The header string
228 -----------------
230 The HEADER-STRING is the header that will be inserted into the
231 LaTeX file. It should contain the \\documentclass macro, and
232 anything else that is needed for this setup. To this header, the
233 following commands will be added:
235 - Calls to \\usepackage for all packages mentioned in the
236 variables `org-latex-default-packages-alist' and
237 `org-latex-packages-alist'. Thus, your header definitions
238 should avoid to also request these packages.
240 - Lines specified via \"#+LATEX_HEADER:\" and
241 \"#+LATEX_HEADER_EXTRA:\" keywords.
243 If you need more control about the sequence in which the header
244 is built up, or if you want to exclude one of these building
245 blocks for a particular class, you can use the following
246 macro-like placeholders.
248 [DEFAULT-PACKAGES] \\usepackage statements for default packages
249 [NO-DEFAULT-PACKAGES] do not include any of the default packages
250 [PACKAGES] \\usepackage statements for packages
251 [NO-PACKAGES] do not include the packages
252 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
253 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
255 So a header like
257 \\documentclass{article}
258 [NO-DEFAULT-PACKAGES]
259 [EXTRA]
260 \\providecommand{\\alert}[1]{\\textbf{#1}}
261 [PACKAGES]
263 will omit the default packages, and will include the
264 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
265 to \\providecommand, and then place \\usepackage commands based
266 on the content of `org-latex-packages-alist'.
268 If your header, `org-latex-default-packages-alist' or
269 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
270 AUTO will automatically be replaced with a coding system derived
271 from `buffer-file-coding-system'. See also the variable
272 `org-latex-inputenc-alist' for a way to influence this mechanism.
274 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
275 AUTO will be replaced with the language related to the language
276 code specified by `org-export-default-language', which see. Note
277 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
278 are permitted.
280 The sectioning structure
281 ------------------------
283 The sectioning structure of the class is given by the elements
284 following the header string. For each sectioning level, a number
285 of strings is specified. A %s formatter is mandatory in each
286 section string and will be replaced by the title of the section.
288 Instead of a cons cell (numbered . unnumbered), you can also
289 provide a list of 2 or 4 elements,
291 \(numbered-open numbered-close)
295 \(numbered-open numbered-close unnumbered-open unnumbered-close)
297 providing opening and closing strings for a LaTeX environment
298 that should represent the document section. The opening clause
299 should have a %s to represent the section title.
301 Instead of a list of sectioning commands, you can also specify
302 a function name. That function will be called with two
303 parameters, the (reduced) level of the headline, and a predicate
304 non-nil when the headline should be numbered. It must return
305 a format string in which the section title will be added."
306 :group 'org-export-latex
307 :type '(repeat
308 (list (string :tag "LaTeX class")
309 (string :tag "LaTeX header")
310 (repeat :tag "Levels" :inline t
311 (choice
312 (cons :tag "Heading"
313 (string :tag " numbered")
314 (string :tag "unnumbered"))
315 (list :tag "Environment"
316 (string :tag "Opening (numbered)")
317 (string :tag "Closing (numbered)")
318 (string :tag "Opening (unnumbered)")
319 (string :tag "Closing (unnumbered)"))
320 (function :tag "Hook computing sectioning"))))))
322 (defcustom org-latex-inputenc-alist nil
323 "Alist of inputenc coding system names, and what should really be used.
324 For example, adding an entry
326 (\"utf8\" . \"utf8x\")
328 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
329 are written as utf8 files."
330 :group 'org-export-latex
331 :type '(repeat
332 (cons
333 (string :tag "Derived from buffer")
334 (string :tag "Use this instead"))))
336 (defcustom org-latex-title-command "\\maketitle"
337 "The command used to insert the title just after \\begin{document}.
338 If this string contains the formatting specification \"%s\" then
339 it will be used as a formatting string, passing the title as an
340 argument."
341 :group 'org-export-latex
342 :type 'string)
344 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
345 "LaTeX command to set the table of contents, list of figures, etc.
346 This command only applies to the table of contents generated with
347 the toc:nil option, not to those generated with #+TOC keyword."
348 :group 'org-export-latex
349 :type 'string)
351 (defcustom org-latex-hyperref-template
352 "\\hypersetup{\n pdfkeywords={%k},\n pdfsubject={%d},\n pdfcreator={%c}}\n"
353 "Template for hyperref package options.
355 Value is a format string, which can contain the following placeholders:
357 %k for KEYWORDS line
358 %d for DESCRIPTION line
359 %c for CREATOR line
361 Set it to the empty string to ignore the command completely."
362 :group 'org-export-latex
363 :type 'string)
365 ;;;; Headline
367 (defcustom org-latex-format-headline-function
368 'org-latex-format-headline-default-function
369 "Function for formatting the headline's text.
371 This function will be called with 5 arguments:
372 TODO the todo keyword (string or nil).
373 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
374 PRIORITY the priority of the headline (integer or nil)
375 TEXT the main headline text (string).
376 TAGS the tags as a list of strings (list of strings or nil).
378 The function result will be used in the section format string.
380 Use `org-latex-format-headline-default-function' by default,
381 which format headlines like for Org version prior to 8.0."
382 :group 'org-export-latex
383 :version "24.4"
384 :package-version '(Org . "8.0")
385 :type 'function)
387 (defcustom org-latex-custom-id-as-label nil
388 "Toggle use of CUSTOM_ID properties for generating section labels.
390 When this variable is non-nil, Org will use the value of a
391 headline's CUSTOM_ID property as the key for the \\label command
392 for the LaTeX section corresponding to the headline.
394 By default, Org generates its own internal section labels for all
395 headlines during LaTeX export. This process ensures that the
396 \\label keys are unique and valid, but it means the keys are not
397 available in advance of the export process.
399 Setting this variable gives you control over how Org generates
400 labels for sections during LaTeX export, so that you may know
401 their keys in advance. One reason to do this is that it allows
402 you to refer to headlines using a single label both in Org's link
403 syntax and in embedded LaTeX code.
405 For example, when this variable is non-nil, a headline like this:
407 ** Some section
408 :PROPERTIES:
409 :CUSTOM_ID: sec:foo
410 :END:
411 This is section [[#sec:foo]].
412 #+BEGIN_LATEX
413 And this is still section \\ref{sec:foo}.
414 #+END_LATEX
416 will be exported to LaTeX as:
418 \\subsection{Some section}
419 \\label{sec:foo}
420 This is section \\ref{sec:foo}.
421 And this is still section \\ref{sec:foo}.
423 Note, however, that setting this variable introduces a limitation
424 on the possible values for CUSTOM_ID. When this variable is
425 non-nil and a headline defines a CUSTOM_ID value, Org simply
426 passes this value to \\label unchanged. You are responsible for
427 ensuring that the value is a valid LaTeX \\label key, and that no
428 other \\label commands with the same key appear elsewhere in your
429 document. (Keys may contain letters, numbers, and the following
430 punctuation: '_' '.' '-' ':'.) There are no such limitations on
431 CUSTOM_ID when this variable is nil.
433 For headlines that do not define the CUSTOM_ID property, Org will
434 continue to use its default labeling scheme to generate labels
435 and resolve links into section references."
436 :group 'org-export-latex
437 :type 'boolean
438 :version "24.5"
439 :package-version '(Org . "8.3"))
441 ;;;; Footnotes
443 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
444 "Text used to separate footnotes."
445 :group 'org-export-latex
446 :type 'string)
449 ;;;; Timestamps
451 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
452 "A printf format string to be applied to active timestamps."
453 :group 'org-export-latex
454 :type 'string)
456 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
457 "A printf format string to be applied to inactive timestamps."
458 :group 'org-export-latex
459 :type 'string)
461 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
462 "A printf format string to be applied to diary timestamps."
463 :group 'org-export-latex
464 :type 'string)
467 ;;;; Links
469 (defcustom org-latex-image-default-option ""
470 "Default option for images."
471 :group 'org-export-latex
472 :version "24.4"
473 :package-version '(Org . "8.0")
474 :type 'string)
476 (defcustom org-latex-image-default-width ".9\\linewidth"
477 "Default width for images.
478 This value will not be used if a height is provided."
479 :group 'org-export-latex
480 :version "24.4"
481 :package-version '(Org . "8.0")
482 :type 'string)
484 (defcustom org-latex-image-default-height ""
485 "Default height for images.
486 This value will not be used if a width is provided, or if the
487 image is wrapped within a \"figure\" or \"wrapfigure\"
488 environment."
489 :group 'org-export-latex
490 :version "24.4"
491 :package-version '(Org . "8.0")
492 :type 'string)
494 (defcustom org-latex-default-figure-position "htb"
495 "Default position for latex figures."
496 :group 'org-export-latex
497 :type 'string)
499 (defcustom org-latex-inline-image-rules
500 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
501 "Rules characterizing image files that can be inlined into LaTeX.
503 A rule consists in an association whose key is the type of link
504 to consider, and value is a regexp that will be matched against
505 link's path.
507 Note that, by default, the image extension *actually* allowed
508 depend on the way the LaTeX file is processed. When used with
509 pdflatex, pdf, jpg and png images are OK. When processing
510 through dvi to Postscript, only ps and eps are allowed. The
511 default we use here encompasses both."
512 :group 'org-export-latex
513 :version "24.4"
514 :package-version '(Org . "8.0")
515 :type '(alist :key-type (string :tag "Type")
516 :value-type (regexp :tag "Path")))
518 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
519 "Format string for links with unknown path type."
520 :group 'org-export-latex
521 :type 'string)
524 ;;;; Tables
526 (defcustom org-latex-default-table-environment "tabular"
527 "Default environment used to build tables."
528 :group 'org-export-latex
529 :version "24.4"
530 :package-version '(Org . "8.0")
531 :type 'string)
533 (defcustom org-latex-default-table-mode 'table
534 "Default mode for tables.
536 Value can be a symbol among:
538 `table' Regular LaTeX table.
540 `math' In this mode, every cell is considered as being in math
541 mode and the complete table will be wrapped within a math
542 environment. It is particularly useful to write matrices.
544 `inline-math' This mode is almost the same as `math', but the
545 math environment will be inlined.
547 `verbatim' The table is exported as it appears in the Org
548 buffer, within a verbatim environment.
550 This value can be overridden locally with, i.e. \":mode math\" in
551 LaTeX attributes.
553 When modifying this variable, it may be useful to change
554 `org-latex-default-table-environment' accordingly."
555 :group 'org-export-latex
556 :version "24.4"
557 :package-version '(Org . "8.0")
558 :type '(choice (const :tag "Table" table)
559 (const :tag "Matrix" math)
560 (const :tag "Inline matrix" inline-math)
561 (const :tag "Verbatim" verbatim))
562 :safe (lambda (s) (memq s '(table math inline-math verbatim))))
564 (defcustom org-latex-tables-centered t
565 "When non-nil, tables are exported in a center environment."
566 :group 'org-export-latex
567 :type 'boolean
568 :safe #'booleanp)
570 (defcustom org-latex-tables-booktabs nil
571 "When non-nil, display tables in a formal \"booktabs\" style.
572 This option assumes that the \"booktabs\" package is properly
573 loaded in the header of the document. This value can be ignored
574 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
575 attributes."
576 :group 'org-export-latex
577 :version "24.4"
578 :package-version '(Org . "8.0")
579 :type 'boolean
580 :safe #'booleanp)
582 (defcustom org-latex-table-caption-above t
583 "When non-nil, place caption string at the beginning of the table.
584 Otherwise, place it near the end."
585 :group 'org-export-latex
586 :type 'boolean
587 :safe #'booleanp)
589 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
590 "Format string to display numbers in scientific notation.
591 The format should have \"%s\" twice, for mantissa and exponent
592 \(i.e., \"%s\\\\times10^{%s}\").
594 When nil, no transformation is made."
595 :group 'org-export-latex
596 :version "24.4"
597 :package-version '(Org . "8.0")
598 :type '(choice
599 (string :tag "Format string")
600 (const :tag "No formatting")))
602 ;;;; Text markup
604 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
605 (code . verb)
606 (italic . "\\emph{%s}")
607 (strike-through . "\\sout{%s}")
608 (underline . "\\uline{%s}")
609 (verbatim . protectedtexttt))
610 "Alist of LaTeX expressions to convert text markup.
612 The key must be a symbol among `bold', `code', `italic',
613 `strike-through', `underline' and `verbatim'. The value is
614 a formatting string to wrap fontified text with.
616 Value can also be set to the following symbols: `verb' and
617 `protectedtexttt'. For the former, Org will use \"\\verb\" to
618 create a format string and select a delimiter character that
619 isn't in the string. For the latter, Org will use \"\\texttt\"
620 to typeset and try to protect special characters.
622 If no association can be found for a given markup, text will be
623 returned as-is."
624 :group 'org-export-latex
625 :type 'alist
626 :options '(bold code italic strike-through underline verbatim))
629 ;;;; Drawers
631 (defcustom org-latex-format-drawer-function
632 (lambda (name contents) contents)
633 "Function called to format a drawer in LaTeX code.
635 The function must accept two parameters:
636 NAME the drawer name, like \"LOGBOOK\"
637 CONTENTS the contents of the drawer.
639 The function should return the string to be exported.
641 The default function simply returns the value of CONTENTS."
642 :group 'org-export-latex
643 :version "24.4"
644 :package-version '(Org . "8.3")
645 :type 'function)
648 ;;;; Inlinetasks
650 (defcustom org-latex-format-inlinetask-function 'ignore
651 "Function called to format an inlinetask in LaTeX code.
653 The function must accept six parameters:
654 TODO the todo keyword, as a string
655 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
656 PRIORITY the inlinetask priority, as a string
657 NAME the inlinetask name, as a string.
658 TAGS the inlinetask tags, as a list of strings.
659 CONTENTS the contents of the inlinetask, as a string.
661 The function should return the string to be exported.
663 For example, the variable could be set to the following function
664 in order to mimic default behaviour:
666 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
667 \"Format an inline task element for LaTeX export.\"
668 \(let ((full-title
669 \(concat
670 \(when todo
671 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
672 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
673 title
674 \(when tags
675 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
676 \(mapconcat 'identity tags \":\")))))
677 \(format (concat \"\\\\begin{center}\\n\"
678 \"\\\\fbox{\\n\"
679 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
680 \"%s\\n\\n\"
681 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
682 \"%s\"
683 \"\\\\end{minipage}}\"
684 \"\\\\end{center}\")
685 full-title contents))"
686 :group 'org-export-latex
687 :type 'function)
690 ;; Src blocks
692 (defcustom org-latex-listings nil
693 "Non-nil means export source code using the listings package.
695 This package will fontify source code, possibly even with color.
696 If you want to use this, you also need to make LaTeX use the
697 listings package, and if you want to have color, the color
698 package. Just add these to `org-latex-packages-alist', for
699 example using customize, or with something like:
701 \(require 'ox-latex)
702 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
703 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
705 Alternatively,
707 \(setq org-latex-listings 'minted)
709 causes source code to be exported using the minted package as
710 opposed to listings. If you want to use minted, you need to add
711 the minted package to `org-latex-packages-alist', for example
712 using customize, or with
714 \(require 'ox-latex)
715 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
717 In addition, it is necessary to install pygments
718 \(http://pygments.org), and to configure the variable
719 `org-latex-pdf-process' so that the -shell-escape option is
720 passed to pdflatex.
722 The minted choice has possible repercussions on the preview of
723 latex fragments (see `org-preview-latex-fragment'). If you run
724 into previewing problems, please consult
726 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
727 :group 'org-export-latex
728 :type '(choice
729 (const :tag "Use listings" t)
730 (const :tag "Use minted" minted)
731 (const :tag "Export verbatim" nil))
732 :safe (lambda (s) (memq s '(t nil minted))))
734 (defcustom org-latex-listings-langs
735 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
736 (c "C") (cc "C++")
737 (fortran "fortran")
738 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
739 (html "HTML") (xml "XML")
740 (tex "TeX") (latex "[LaTeX]TeX")
741 (shell-script "bash")
742 (gnuplot "Gnuplot")
743 (ocaml "Caml") (caml "Caml")
744 (sql "SQL") (sqlite "sql"))
745 "Alist mapping languages to their listing language counterpart.
746 The key is a symbol, the major mode symbol without the \"-mode\".
747 The value is the string that should be inserted as the language
748 parameter for the listings package. If the mode name and the
749 listings name are the same, the language does not need an entry
750 in this list - but it does not hurt if it is present."
751 :group 'org-export-latex
752 :type '(repeat
753 (list
754 (symbol :tag "Major mode ")
755 (string :tag "Listings language"))))
757 (defcustom org-latex-listings-options nil
758 "Association list of options for the latex listings package.
760 These options are supplied as a comma-separated list to the
761 \\lstset command. Each element of the association list should be
762 a list containing two strings: the name of the option, and the
763 value. For example,
765 (setq org-latex-listings-options
766 '((\"basicstyle\" \"\\\\small\")
767 (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
769 will typeset the code in a small size font with underlined, bold
770 black keywords.
772 Note that the same options will be applied to blocks of all
773 languages."
774 :group 'org-export-latex
775 :type '(repeat
776 (list
777 (string :tag "Listings option name ")
778 (string :tag "Listings option value"))))
780 (defcustom org-latex-minted-langs
781 '((emacs-lisp "common-lisp")
782 (cc "c++")
783 (cperl "perl")
784 (shell-script "bash")
785 (caml "ocaml"))
786 "Alist mapping languages to their minted language counterpart.
787 The key is a symbol, the major mode symbol without the \"-mode\".
788 The value is the string that should be inserted as the language
789 parameter for the minted package. If the mode name and the
790 listings name are the same, the language does not need an entry
791 in this list - but it does not hurt if it is present.
793 Note that minted uses all lower case for language identifiers,
794 and that the full list of language identifiers can be obtained
795 with:
797 pygmentize -L lexers"
798 :group 'org-export-latex
799 :type '(repeat
800 (list
801 (symbol :tag "Major mode ")
802 (string :tag "Minted language"))))
804 (defcustom org-latex-minted-options nil
805 "Association list of options for the latex minted package.
807 These options are supplied within square brackets in
808 \\begin{minted} environments. Each element of the alist should
809 be a list containing two strings: the name of the option, and the
810 value. For example,
812 \(setq org-latex-minted-options
813 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
815 will result in src blocks being exported with
817 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
819 as the start of the minted environment. Note that the same
820 options will be applied to blocks of all languages."
821 :group 'org-export-latex
822 :type '(repeat
823 (list
824 (string :tag "Minted option name ")
825 (string :tag "Minted option value"))))
827 (defvar org-latex-custom-lang-environments nil
828 "Alist mapping languages to language-specific LaTeX environments.
830 It is used during export of src blocks by the listings and minted
831 latex packages. For example,
833 \(setq org-latex-custom-lang-environments
834 '\(\(python \"pythoncode\"\)\)\)
836 would have the effect that if org encounters begin_src python
837 during latex export it will output
839 \\begin{pythoncode}
840 <src block body>
841 \\end{pythoncode}")
844 ;;;; Compilation
846 (defcustom org-latex-pdf-process
847 '("pdflatex -interaction nonstopmode -output-directory %o %f"
848 "pdflatex -interaction nonstopmode -output-directory %o %f"
849 "pdflatex -interaction nonstopmode -output-directory %o %f")
850 "Commands to process a LaTeX file to a PDF file.
851 This is a list of strings, each of them will be given to the
852 shell as a command. %f in the command will be replaced by the
853 full file name, %b by the file base name (i.e. without directory
854 and extension parts) and %o by the base directory of the file.
856 The reason why this is a list is that it usually takes several
857 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
858 does not have a clever mechanism to detect which of these
859 commands have to be run to get to a stable result, and it also
860 does not do any error checking.
862 By default, Org uses 3 runs of `pdflatex' to do the processing.
863 If you have texi2dvi on your system and if that does not cause
864 the infamous egrep/locale bug:
866 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
868 then `texi2dvi' is the superior choice as it automates the LaTeX
869 build process by calling the \"correct\" combinations of
870 auxiliary programs. Org does offer `texi2dvi' as one of the
871 customize options. Alternatively, `rubber' and `latexmk' also
872 provide similar functionality. The latter supports `biber' out
873 of the box.
875 Alternatively, this may be a Lisp function that does the
876 processing, so you could use this to apply the machinery of
877 AUCTeX or the Emacs LaTeX mode. This function should accept the
878 file name as its single argument."
879 :group 'org-export-pdf
880 :type '(choice
881 (repeat :tag "Shell command sequence"
882 (string :tag "Shell command"))
883 (const :tag "2 runs of pdflatex"
884 ("pdflatex -interaction nonstopmode -output-directory %o %f"
885 "pdflatex -interaction nonstopmode -output-directory %o %f"))
886 (const :tag "3 runs of pdflatex"
887 ("pdflatex -interaction nonstopmode -output-directory %o %f"
888 "pdflatex -interaction nonstopmode -output-directory %o %f"
889 "pdflatex -interaction nonstopmode -output-directory %o %f"))
890 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
891 ("pdflatex -interaction nonstopmode -output-directory %o %f"
892 "bibtex %b"
893 "pdflatex -interaction nonstopmode -output-directory %o %f"
894 "pdflatex -interaction nonstopmode -output-directory %o %f"))
895 (const :tag "2 runs of xelatex"
896 ("xelatex -interaction nonstopmode -output-directory %o %f"
897 "xelatex -interaction nonstopmode -output-directory %o %f"))
898 (const :tag "3 runs of xelatex"
899 ("xelatex -interaction nonstopmode -output-directory %o %f"
900 "xelatex -interaction nonstopmode -output-directory %o %f"
901 "xelatex -interaction nonstopmode -output-directory %o %f"))
902 (const :tag "xelatex,bibtex,xelatex,xelatex"
903 ("xelatex -interaction nonstopmode -output-directory %o %f"
904 "bibtex %b"
905 "xelatex -interaction nonstopmode -output-directory %o %f"
906 "xelatex -interaction nonstopmode -output-directory %o %f"))
907 (const :tag "texi2dvi"
908 ("texi2dvi -p -b -V %f"))
909 (const :tag "rubber"
910 ("rubber -d --into %o %f"))
911 (const :tag "latexmk"
912 ("latexmk -g -pdf %f"))
913 (function)))
915 (defcustom org-latex-logfiles-extensions
916 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
917 "The list of file extensions to consider as LaTeX logfiles.
918 The logfiles will be remove if `org-latex-remove-logfiles' is
919 non-nil."
920 :group 'org-export-latex
921 :type '(repeat (string :tag "Extension")))
923 (defcustom org-latex-remove-logfiles t
924 "Non-nil means remove the logfiles produced by PDF production.
925 By default, logfiles are files with these extensions: .aux, .idx,
926 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
927 logfiles to remove, set `org-latex-logfiles-extensions'."
928 :group 'org-export-latex
929 :type 'boolean)
931 (defcustom org-latex-known-errors
932 '(("Reference.*?undefined" . "[undefined reference]")
933 ("Citation.*?undefined" . "[undefined citation]")
934 ("Undefined control sequence" . "[undefined control sequence]")
935 ("^! LaTeX.*?Error" . "[LaTeX error]")
936 ("^! Package.*?Error" . "[package error]")
937 ("Runaway argument" . "Runaway argument"))
938 "Alist of regular expressions and associated messages for the user.
939 The regular expressions are used to find possible errors in the
940 log of a latex-run."
941 :group 'org-export-latex
942 :version "24.4"
943 :package-version '(Org . "8.0")
944 :type '(repeat
945 (cons
946 (string :tag "Regexp")
947 (string :tag "Message"))))
951 ;;; Internal Functions
953 (defun org-latex--caption/label-string (element info)
954 "Return caption and label LaTeX string for ELEMENT.
956 INFO is a plist holding contextual information. If there's no
957 caption nor label, return the empty string.
959 For non-floats, see `org-latex--wrap-label'."
960 (let* ((label (org-element-property :name element))
961 (label-str (if (not (org-string-nw-p label)) ""
962 (format "\\label{%s}"
963 (org-export-solidify-link-text label))))
964 (main (org-export-get-caption element))
965 (short (org-export-get-caption element t))
966 (caption-from-attr-latex (org-export-read-attribute :attr_latex element :caption)))
967 (cond
968 ((org-string-nw-p caption-from-attr-latex)
969 (concat caption-from-attr-latex "\n"))
970 ((and (not main) (equal label-str "")) "")
971 ((not main) (concat label-str "\n"))
972 ;; Option caption format with short name.
973 (short (format "\\caption[%s]{%s%s}\n"
974 (org-export-data short info)
975 label-str
976 (org-export-data main info)))
977 ;; Standard caption format.
978 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
980 (defun org-latex-guess-inputenc (header)
981 "Set the coding system in inputenc to what the buffer is.
983 HEADER is the LaTeX header string. This function only applies
984 when specified inputenc option is \"AUTO\".
986 Return the new header, as a string."
987 (let* ((cs (or (ignore-errors
988 (latexenc-coding-system-to-inputenc
989 (or org-export-coding-system buffer-file-coding-system)))
990 "utf8")))
991 (if (not cs) header
992 ;; First translate if that is requested.
993 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
994 ;; Then find the \usepackage statement and replace the option.
995 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
996 cs header t nil 1))))
998 (defun org-latex-guess-babel-language (header info)
999 "Set Babel's language according to LANGUAGE keyword.
1001 HEADER is the LaTeX header string. INFO is the plist used as
1002 a communication channel.
1004 Insertion of guessed language only happens when Babel package has
1005 explicitly been loaded. Then it is added to the rest of
1006 package's options.
1008 The argument to Babel may be \"AUTO\" which is then replaced with
1009 the language of the document or `org-export-default-language'
1010 unless language in question is already loaded.
1012 Return the new header."
1013 (let ((language-code (plist-get info :language)))
1014 ;; If no language is set or Babel package is not loaded, return
1015 ;; HEADER as-is.
1016 (if (or (not (stringp language-code))
1017 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1018 header
1019 (let ((options (save-match-data
1020 (org-split-string (match-string 1 header) ",[ \t]*")))
1021 (language (cdr (assoc language-code
1022 org-latex-babel-language-alist))))
1023 ;; If LANGUAGE is already loaded, return header without AUTO.
1024 ;; Otherwise, replace AUTO with language or append language if
1025 ;; AUTO is not present.
1026 (replace-match
1027 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1028 (cond ((member language options) (delete "AUTO" options))
1029 ((member "AUTO" options) options)
1030 (t (append options (list language))))
1031 ", ")
1032 t nil header 1)))))
1034 (defun org-latex--find-verb-separator (s)
1035 "Return a character not used in string S.
1036 This is used to choose a separator for constructs like \\verb."
1037 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1038 (loop for c across ll
1039 when (not (string-match (regexp-quote (char-to-string c)) s))
1040 return (char-to-string c))))
1042 (defun org-latex--make-option-string (options)
1043 "Return a comma separated string of keywords and values.
1044 OPTIONS is an alist where the key is the options keyword as
1045 a string, and the value a list containing the keyword value, or
1046 nil."
1047 (mapconcat (lambda (pair)
1048 (concat (first pair)
1049 (when (> (length (second pair)) 0)
1050 (concat "=" (second pair)))))
1051 options
1052 ","))
1054 (defun org-latex--wrap-label (element output)
1055 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1056 This function shouldn't be used for floats. See
1057 `org-latex--caption/label-string'."
1058 (let ((label (org-element-property :name element)))
1059 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
1060 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
1061 output))))
1063 (defun org-latex--text-markup (text markup)
1064 "Format TEXT depending on MARKUP text markup.
1065 See `org-latex-text-markup-alist' for details."
1066 (let ((fmt (cdr (assq markup org-latex-text-markup-alist))))
1067 (cond
1068 ;; No format string: Return raw text.
1069 ((not fmt) text)
1070 ;; Handle the `verb' special case: Find and appropriate separator
1071 ;; and use "\\verb" command.
1072 ((eq 'verb fmt)
1073 (let ((separator (org-latex--find-verb-separator text)))
1074 (concat "\\verb" separator text separator)))
1075 ;; Handle the `protectedtexttt' special case: Protect some
1076 ;; special chars and use "\texttt{%s}" format string.
1077 ((eq 'protectedtexttt fmt)
1078 (let ((start 0)
1079 (trans '(("\\" . "\\textbackslash{}")
1080 ("~" . "\\textasciitilde{}")
1081 ("^" . "\\textasciicircum{}")))
1082 (rtn "")
1083 char)
1084 (while (string-match "[\\{}$%&_#~^]" text)
1085 (setq char (match-string 0 text))
1086 (if (> (match-beginning 0) 0)
1087 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1088 (setq text (substring text (1+ (match-beginning 0))))
1089 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1090 rtn (concat rtn char)))
1091 (setq text (concat rtn text)
1092 fmt "\\texttt{%s}")
1093 (while (string-match "--" text)
1094 (setq text (replace-match "-{}-" t t text)))
1095 (format fmt text)))
1096 ;; Else use format string.
1097 (t (format fmt text)))))
1099 (defun org-latex--delayed-footnotes-definitions (element info)
1100 "Return footnotes definitions in ELEMENT as a string.
1102 INFO is a plist used as a communication channel.
1104 Footnotes definitions are returned within \"\\footnotetxt{}\"
1105 commands.
1107 This function is used within constructs that don't support
1108 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1109 \"\\footnotemark\" is used within the construct and the function
1110 just outside of it."
1111 (mapconcat
1112 (lambda (ref)
1113 (format
1114 "\\footnotetext[%s]{%s}"
1115 (org-export-get-footnote-number ref info)
1116 (org-trim
1117 (org-export-data
1118 (org-export-get-footnote-definition ref info) info))))
1119 ;; Find every footnote reference in ELEMENT.
1120 (let* (all-refs
1121 search-refs ; For byte-compiler.
1122 (search-refs
1123 (function
1124 (lambda (data)
1125 ;; Return a list of all footnote references never seen
1126 ;; before in DATA.
1127 (org-element-map data 'footnote-reference
1128 (lambda (ref)
1129 (when (org-export-footnote-first-reference-p ref info)
1130 (push ref all-refs)
1131 (when (eq (org-element-property :type ref) 'standard)
1132 (funcall search-refs
1133 (org-export-get-footnote-definition ref info)))))
1134 info)
1135 (reverse all-refs)))))
1136 (funcall search-refs element))
1137 ""))
1139 (defun org-latex--translate (s info)
1140 "Translate string S according to specified language.
1141 INFO is a plist used as a communication channel."
1142 (org-export-translate s :latex info))
1146 ;;; Template
1148 (defun org-latex-template (contents info)
1149 "Return complete document string after LaTeX conversion.
1150 CONTENTS is the transcoded contents string. INFO is a plist
1151 holding export options."
1152 (let ((title (org-export-data (plist-get info :title) info)))
1153 (concat
1154 ;; Time-stamp.
1155 (and (plist-get info :time-stamp-file)
1156 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1157 ;; Document class and packages.
1158 (let* ((class (plist-get info :latex-class))
1159 (class-options (plist-get info :latex-class-options))
1160 (header (nth 1 (assoc class org-latex-classes)))
1161 (document-class-string
1162 (and (stringp header)
1163 (if (not class-options) header
1164 (replace-regexp-in-string
1165 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1166 class-options header t nil 1)))))
1167 (if (not document-class-string)
1168 (user-error "Unknown LaTeX class `%s'" class)
1169 (org-latex-guess-babel-language
1170 (org-latex-guess-inputenc
1171 (org-element-normalize-string
1172 (org-splice-latex-header
1173 document-class-string
1174 org-latex-default-packages-alist
1175 org-latex-packages-alist nil
1176 (concat (org-element-normalize-string
1177 (plist-get info :latex-header))
1178 (plist-get info :latex-header-extra)))))
1179 info)))
1180 ;; Possibly limit depth for headline numbering.
1181 (let ((sec-num (plist-get info :section-numbers)))
1182 (when (integerp sec-num)
1183 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1184 ;; Author.
1185 (let ((author (and (plist-get info :with-author)
1186 (let ((auth (plist-get info :author)))
1187 (and auth (org-export-data auth info)))))
1188 (email (and (plist-get info :with-email)
1189 (org-export-data (plist-get info :email) info))))
1190 (cond ((and author email (not (string= "" email)))
1191 (format "\\author{%s\\thanks{%s}}\n" author email))
1192 ((or author email) (format "\\author{%s}\n" (or author email)))))
1193 ;; Date.
1194 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1195 (format "\\date{%s}\n" (org-export-data date info)))
1196 ;; Title
1197 (format "\\title{%s}\n" title)
1198 ;; Hyperref options.
1199 (format-spec (plist-get info :latex-hyperref)
1200 (format-spec-make
1201 ?k (or (plist-get info :keywords) "")
1202 ?d (or (plist-get info :description)"")
1203 ?c (if (plist-get info :with-creator)
1204 (plist-get info :creator)
1205 "")))
1206 ;; Document start.
1207 "\\begin{document}\n\n"
1208 ;; Title command.
1209 (org-element-normalize-string
1210 (cond ((string= "" title) nil)
1211 ((not (stringp org-latex-title-command)) nil)
1212 ((string-match "\\(?:[^%]\\|^\\)%s"
1213 org-latex-title-command)
1214 (format org-latex-title-command title))
1215 (t org-latex-title-command)))
1216 ;; Table of contents.
1217 (let ((depth (plist-get info :with-toc)))
1218 (when depth
1219 (concat (when (wholenump depth)
1220 (format "\\setcounter{tocdepth}{%d}\n" depth))
1221 org-latex-toc-command)))
1222 ;; Document's body.
1223 contents
1224 ;; Creator.
1225 (let ((creator-info (plist-get info :with-creator)))
1226 (cond
1227 ((not creator-info) "")
1228 ((eq creator-info 'comment)
1229 (format "%% %s\n" (plist-get info :creator)))
1230 (t (concat (plist-get info :creator) "\n"))))
1231 ;; Document end.
1232 "\\end{document}")))
1236 ;;; Transcode Functions
1238 ;;;; Bold
1240 (defun org-latex-bold (bold contents info)
1241 "Transcode BOLD from Org to LaTeX.
1242 CONTENTS is the text with bold markup. INFO is a plist holding
1243 contextual information."
1244 (org-latex--text-markup contents 'bold))
1247 ;;;; Center Block
1249 (defun org-latex-center-block (center-block contents info)
1250 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1251 CONTENTS holds the contents of the center block. INFO is a plist
1252 holding contextual information."
1253 (org-latex--wrap-label
1254 center-block
1255 (format "\\begin{center}\n%s\\end{center}" contents)))
1258 ;;;; Clock
1260 (defun org-latex-clock (clock contents info)
1261 "Transcode a CLOCK element from Org to LaTeX.
1262 CONTENTS is nil. INFO is a plist holding contextual
1263 information."
1264 (concat
1265 "\\noindent"
1266 (format "\\textbf{%s} " org-clock-string)
1267 (format org-latex-inactive-timestamp-format
1268 (concat (org-translate-time
1269 (org-element-property :raw-value
1270 (org-element-property :value clock)))
1271 (let ((time (org-element-property :duration clock)))
1272 (and time (format " (%s)" time)))))
1273 "\\\\"))
1276 ;;;; Code
1278 (defun org-latex-code (code contents info)
1279 "Transcode a CODE object from Org to LaTeX.
1280 CONTENTS is nil. INFO is a plist used as a communication
1281 channel."
1282 (org-latex--text-markup (org-element-property :value code) 'code))
1285 ;;;; Drawer
1287 (defun org-latex-drawer (drawer contents info)
1288 "Transcode a DRAWER element from Org to LaTeX.
1289 CONTENTS holds the contents of the block. INFO is a plist
1290 holding contextual information."
1291 (let* ((name (org-element-property :drawer-name drawer))
1292 (output (funcall org-latex-format-drawer-function
1293 name contents)))
1294 (org-latex--wrap-label drawer output)))
1297 ;;;; Dynamic Block
1299 (defun org-latex-dynamic-block (dynamic-block contents info)
1300 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1301 CONTENTS holds the contents of the block. INFO is a plist
1302 holding contextual information. See `org-export-data'."
1303 (org-latex--wrap-label dynamic-block contents))
1306 ;;;; Entity
1308 (defun org-latex-entity (entity contents info)
1309 "Transcode an ENTITY object from Org to LaTeX.
1310 CONTENTS are the definition itself. INFO is a plist holding
1311 contextual information."
1312 (org-element-property :latex entity))
1315 ;;;; Example Block
1317 (defun org-latex-example-block (example-block contents info)
1318 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1319 CONTENTS is nil. INFO is a plist holding contextual
1320 information."
1321 (when (org-string-nw-p (org-element-property :value example-block))
1322 (org-latex--wrap-label
1323 example-block
1324 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1325 (org-export-format-code-default example-block info)))))
1328 ;;;; Export Block
1330 (defun org-latex-export-block (export-block contents info)
1331 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1332 CONTENTS is nil. INFO is a plist holding contextual information."
1333 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1334 (org-remove-indentation (org-element-property :value export-block))))
1337 ;;;; Export Snippet
1339 (defun org-latex-export-snippet (export-snippet contents info)
1340 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1341 CONTENTS is nil. INFO is a plist holding contextual information."
1342 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1343 (org-element-property :value export-snippet)))
1346 ;;;; Fixed Width
1348 (defun org-latex-fixed-width (fixed-width contents info)
1349 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1350 CONTENTS is nil. INFO is a plist holding contextual information."
1351 (org-latex--wrap-label
1352 fixed-width
1353 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1354 (org-remove-indentation
1355 (org-element-property :value fixed-width)))))
1358 ;;;; Footnote Reference
1360 (defun org-latex-footnote-reference (footnote-reference contents info)
1361 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1362 CONTENTS is nil. INFO is a plist holding contextual information."
1363 (concat
1364 ;; Insert separator between two footnotes in a row.
1365 (let ((prev (org-export-get-previous-element footnote-reference info)))
1366 (when (eq (org-element-type prev) 'footnote-reference)
1367 org-latex-footnote-separator))
1368 (cond
1369 ;; Use \footnotemark if the footnote has already been defined.
1370 ((not (org-export-footnote-first-reference-p footnote-reference info))
1371 (format "\\footnotemark[%s]{}"
1372 (org-export-get-footnote-number footnote-reference info)))
1373 ;; Use \footnotemark if reference is within another footnote
1374 ;; reference, footnote definition or table cell.
1375 ((loop for parent in (org-export-get-genealogy footnote-reference)
1376 thereis (memq (org-element-type parent)
1377 '(footnote-reference footnote-definition table-cell)))
1378 "\\footnotemark")
1379 ;; Otherwise, define it with \footnote command.
1381 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1382 (concat
1383 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1384 ;; Retrieve all footnote references within the footnote and
1385 ;; add their definition after it, since LaTeX doesn't support
1386 ;; them inside.
1387 (org-latex--delayed-footnotes-definitions def info)))))))
1390 ;;;; Headline
1392 (defun org-latex-headline (headline contents info)
1393 "Transcode a HEADLINE element from Org to LaTeX.
1394 CONTENTS holds the contents of the headline. INFO is a plist
1395 holding contextual information."
1396 (unless (org-element-property :footnote-section-p headline)
1397 (let* ((class (plist-get info :latex-class))
1398 (level (org-export-get-relative-level headline info))
1399 (numberedp (org-export-numbered-headline-p headline info))
1400 (class-sectioning (assoc class org-latex-classes))
1401 ;; Section formatting will set two placeholders: one for
1402 ;; the title and the other for the contents.
1403 (section-fmt
1404 (let ((sec (if (functionp (nth 2 class-sectioning))
1405 (funcall (nth 2 class-sectioning) level numberedp)
1406 (nth (1+ level) class-sectioning))))
1407 (cond
1408 ;; No section available for that LEVEL.
1409 ((not sec) nil)
1410 ;; Section format directly returned by a function. Add
1411 ;; placeholder for contents.
1412 ((stringp sec) (concat sec "\n%s"))
1413 ;; (numbered-section . unnumbered-section)
1414 ((not (consp (cdr sec)))
1415 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1416 ;; (numbered-open numbered-close)
1417 ((= (length sec) 2)
1418 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1419 ;; (num-in num-out no-num-in no-num-out)
1420 ((= (length sec) 4)
1421 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1422 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1423 (text (org-export-data (org-element-property :title headline) info))
1424 (todo
1425 (and (plist-get info :with-todo-keywords)
1426 (let ((todo (org-element-property :todo-keyword headline)))
1427 (and todo (org-export-data todo info)))))
1428 (todo-type (and todo (org-element-property :todo-type headline)))
1429 (tags (and (plist-get info :with-tags)
1430 (org-export-get-tags headline info)))
1431 (priority (and (plist-get info :with-priority)
1432 (org-element-property :priority headline)))
1433 ;; Create the headline text along with a no-tag version.
1434 ;; The latter is required to remove tags from toc.
1435 (full-text (funcall org-latex-format-headline-function
1436 todo todo-type priority text tags))
1437 ;; Associate \label to the headline for internal links.
1438 (headline-label
1439 (let ((custom-label
1440 (and (plist-get info :latex-custom-id-labels)
1441 (org-element-property :CUSTOM_ID headline))))
1442 (if custom-label (format "\\label{%s}\n" custom-label)
1443 (format "\\label{sec-%s}\n"
1444 (mapconcat
1445 #'number-to-string
1446 (org-export-get-headline-number headline info)
1447 "-")))))
1448 (pre-blanks
1449 (make-string (org-element-property :pre-blank headline) 10)))
1450 (if (or (not section-fmt) (org-export-low-level-p headline info))
1451 ;; This is a deep sub-tree: export it as a list item. Also
1452 ;; export as items headlines for which no section format has
1453 ;; been found.
1454 (let ((low-level-body
1455 (concat
1456 ;; If headline is the first sibling, start a list.
1457 (when (org-export-first-sibling-p headline info)
1458 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1459 ;; Itemize headline
1460 "\\item"
1461 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1462 "\\relax")
1463 " " full-text "\n"
1464 headline-label
1465 pre-blanks
1466 contents)))
1467 ;; If headline is not the last sibling simply return
1468 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1469 ;; any blank line.
1470 (if (not (org-export-last-sibling-p headline info)) low-level-body
1471 (replace-regexp-in-string
1472 "[ \t\n]*\\'"
1473 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1474 low-level-body)))
1475 ;; This is a standard headline. Export it as a section. Add
1476 ;; an alternative heading when possible, and when this is not
1477 ;; identical to the usual heading.
1478 (let ((opt-title
1479 (funcall org-latex-format-headline-function
1480 todo todo-type priority
1481 (org-export-data
1482 (org-export-get-alt-title headline info) info)
1483 (and (eq (plist-get info :with-tags) t) tags))))
1484 (if (and numberedp opt-title
1485 (not (equal opt-title full-text))
1486 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1487 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1488 ;; Replace square brackets with parenthesis
1489 ;; since square brackets are not supported in
1490 ;; optional arguments.
1491 (replace-regexp-in-string
1492 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1493 full-text
1494 (concat headline-label pre-blanks contents))
1495 ;; Impossible to add an alternative heading. Fallback to
1496 ;; regular sectioning format string.
1497 (format section-fmt full-text
1498 (concat headline-label pre-blanks contents))))))))
1500 (defun org-latex-format-headline-default-function
1501 (todo todo-type priority text tags)
1502 "Default format function for a headline.
1503 See `org-latex-format-headline-function' for details."
1504 (concat
1505 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1506 (and priority (format "\\framebox{\\#%c} " priority))
1507 text
1508 (and tags
1509 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
1512 ;;;; Horizontal Rule
1514 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1515 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1516 CONTENTS is nil. INFO is a plist holding contextual information."
1517 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1518 (prev (org-export-get-previous-element horizontal-rule info)))
1519 (concat
1520 ;; Make sure the rule doesn't start at the end of the current
1521 ;; line by separating it with a blank line from previous element.
1522 (when (and prev
1523 (let ((prev-blank (org-element-property :post-blank prev)))
1524 (or (not prev-blank) (zerop prev-blank))))
1525 "\n")
1526 (org-latex--wrap-label
1527 horizontal-rule
1528 (format "\\rule{%s}{%s}"
1529 (or (plist-get attr :width) "\\linewidth")
1530 (or (plist-get attr :thickness) "0.5pt"))))))
1533 ;;;; Inline Src Block
1535 (defun org-latex-inline-src-block (inline-src-block contents info)
1536 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1537 CONTENTS holds the contents of the item. INFO is a plist holding
1538 contextual information."
1539 (let* ((code (org-element-property :value inline-src-block))
1540 (separator (org-latex--find-verb-separator code)))
1541 (cond
1542 ;; Do not use a special package: transcode it verbatim.
1543 ((not org-latex-listings)
1544 (concat "\\verb" separator code separator))
1545 ;; Use minted package.
1546 ((eq org-latex-listings 'minted)
1547 (let* ((org-lang (org-element-property :language inline-src-block))
1548 (mint-lang (or (cadr (assq (intern org-lang)
1549 org-latex-minted-langs))
1550 org-lang))
1551 (options (org-latex--make-option-string
1552 org-latex-minted-options)))
1553 (concat (format "\\mint%s{%s}"
1554 (if (string= options "") "" (format "[%s]" options))
1555 mint-lang)
1556 separator code separator)))
1557 ;; Use listings package.
1559 ;; Maybe translate language's name.
1560 (let* ((org-lang (org-element-property :language inline-src-block))
1561 (lst-lang (or (cadr (assq (intern org-lang)
1562 org-latex-listings-langs))
1563 org-lang))
1564 (options (org-latex--make-option-string
1565 (append org-latex-listings-options
1566 `(("language" ,lst-lang))))))
1567 (concat (format "\\lstinline[%s]" options)
1568 separator code separator))))))
1571 ;;;; Inlinetask
1573 (defun org-latex-inlinetask (inlinetask contents info)
1574 "Transcode an INLINETASK element from Org to LaTeX.
1575 CONTENTS holds the contents of the block. INFO is a plist
1576 holding contextual information."
1577 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1578 (todo (and (plist-get info :with-todo-keywords)
1579 (let ((todo (org-element-property :todo-keyword inlinetask)))
1580 (and todo (org-export-data todo info)))))
1581 (todo-type (org-element-property :todo-type inlinetask))
1582 (tags (and (plist-get info :with-tags)
1583 (org-export-get-tags inlinetask info)))
1584 (priority (and (plist-get info :with-priority)
1585 (org-element-property :priority inlinetask))))
1586 ;; If `org-latex-format-inlinetask-function' is provided, call it
1587 ;; with appropriate arguments.
1588 (if (not (eq org-latex-format-inlinetask-function 'ignore))
1589 (funcall org-latex-format-inlinetask-function
1590 todo todo-type priority title tags contents)
1591 ;; Otherwise, use a default template.
1592 (org-latex--wrap-label
1593 inlinetask
1594 (let ((full-title
1595 (concat
1596 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1597 (when priority (format "\\framebox{\\#%c} " priority))
1598 title
1599 (when tags (format "\\hfill{}\\textsc{:%s:}"
1600 (mapconcat 'identity tags ":"))))))
1601 (format (concat "\\begin{center}\n"
1602 "\\fbox{\n"
1603 "\\begin{minipage}[c]{.6\\textwidth}\n"
1604 "%s\n\n"
1605 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1606 "%s"
1607 "\\end{minipage}\n"
1608 "}\n"
1609 "\\end{center}")
1610 full-title contents))))))
1613 ;;;; Italic
1615 (defun org-latex-italic (italic contents info)
1616 "Transcode ITALIC from Org to LaTeX.
1617 CONTENTS is the text with italic markup. INFO is a plist holding
1618 contextual information."
1619 (org-latex--text-markup contents 'italic))
1622 ;;;; Item
1624 (defun org-latex-item (item contents info)
1625 "Transcode an ITEM element from Org to LaTeX.
1626 CONTENTS holds the contents of the item. INFO is a plist holding
1627 contextual information."
1628 (let* ((counter
1629 (let ((count (org-element-property :counter item))
1630 (level
1631 ;; Determine level of current item to determine the
1632 ;; correct LaTeX counter to use (enumi, enumii...).
1633 (let ((parent item) (level 0))
1634 (while (memq (org-element-type
1635 (setq parent (org-export-get-parent parent)))
1636 '(plain-list item))
1637 (when (and (eq (org-element-type parent) 'plain-list)
1638 (eq (org-element-property :type parent)
1639 'ordered))
1640 (incf level)))
1641 level)))
1642 (and count
1643 (< level 5)
1644 (format "\\setcounter{enum%s}{%s}\n"
1645 (nth (1- level) '("i" "ii" "iii" "iv"))
1646 (1- count)))))
1647 (checkbox (case (org-element-property :checkbox item)
1648 (on "$\\boxtimes$ ")
1649 (off "$\\square$ ")
1650 (trans "$\\boxminus$ ")))
1651 (tag (let ((tag (org-element-property :tag item)))
1652 ;; Check-boxes must belong to the tag.
1653 (and tag (format "[{%s}] "
1654 (concat checkbox
1655 (org-export-data tag info)))))))
1656 (concat counter
1657 "\\item"
1658 (cond
1659 (tag)
1660 (checkbox (concat " " checkbox))
1661 ;; Without a tag or a check-box, if CONTENTS starts with
1662 ;; an opening square bracket, add "\relax" to "\item",
1663 ;; unless the brackets comes from an initial export
1664 ;; snippet (i.e. it is inserted willingly by the user).
1665 ((and contents
1666 (org-string-match-p "\\`[ \t]*\\[" contents)
1667 (not (let ((e (car (org-element-contents item))))
1668 (and (eq (org-element-type e) 'paragraph)
1669 (let ((o (car (org-element-contents e))))
1670 (and (eq (org-element-type o) 'export-snippet)
1671 (eq (org-export-snippet-backend o)
1672 'latex)))))))
1673 "\\relax ")
1674 (t " "))
1675 (and contents (org-trim contents))
1676 ;; If there are footnotes references in tag, be sure to
1677 ;; add their definition at the end of the item. This
1678 ;; workaround is necessary since "\footnote{}" command is
1679 ;; not supported in tags.
1680 (and tag
1681 (org-latex--delayed-footnotes-definitions
1682 (org-element-property :tag item) info)))))
1685 ;;;; Keyword
1687 (defun org-latex-keyword (keyword contents info)
1688 "Transcode a KEYWORD element from Org to LaTeX.
1689 CONTENTS is nil. INFO is a plist holding contextual information."
1690 (let ((key (org-element-property :key keyword))
1691 (value (org-element-property :value keyword)))
1692 (cond
1693 ((string= key "LATEX") value)
1694 ((string= key "INDEX") (format "\\index{%s}" value))
1695 ((string= key "TOC")
1696 (let ((value (downcase value)))
1697 (cond
1698 ((string-match "\\<headlines\\>" value)
1699 (let ((depth (or (and (string-match "[0-9]+" value)
1700 (string-to-number (match-string 0 value)))
1701 (plist-get info :with-toc))))
1702 (concat
1703 (when (wholenump depth)
1704 (format "\\setcounter{tocdepth}{%s}\n" depth))
1705 "\\tableofcontents")))
1706 ((string= "tables" value) "\\listoftables")
1707 ((string= "listings" value)
1708 (cond
1709 ((eq org-latex-listings 'minted) "\\listoflistings")
1710 (org-latex-listings "\\lstlistoflistings")
1711 ;; At the moment, src blocks with a caption are wrapped
1712 ;; into a figure environment.
1713 (t "\\listoffigures")))))))))
1716 ;;;; Latex Environment
1718 (defun org-latex-latex-environment (latex-environment contents info)
1719 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1720 CONTENTS is nil. INFO is a plist holding contextual information."
1721 (when (plist-get info :with-latex)
1722 (let ((label (org-element-property :name latex-environment))
1723 (value (org-remove-indentation
1724 (org-element-property :value latex-environment))))
1725 (if (not (org-string-nw-p label)) value
1726 ;; Environment is labeled: label must be within the environment
1727 ;; (otherwise, a reference pointing to that element will count
1728 ;; the section instead).
1729 (with-temp-buffer
1730 (insert value)
1731 (goto-char (point-min))
1732 (forward-line)
1733 (insert
1734 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1735 (buffer-string))))))
1738 ;;;; Latex Fragment
1740 (defun org-latex-latex-fragment (latex-fragment contents info)
1741 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1742 CONTENTS is nil. INFO is a plist holding contextual information."
1743 (let ((value (org-element-property :value latex-fragment)))
1744 ;; Trim math markers since the fragment is enclosed within
1745 ;; a latex-math-block object anyway.
1746 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1747 (match-string 2 value))
1748 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1749 (match-string 1 value))
1750 (t value))))
1753 ;;;; Line Break
1755 (defun org-latex-line-break (line-break contents info)
1756 "Transcode a LINE-BREAK object from Org to LaTeX.
1757 CONTENTS is nil. INFO is a plist holding contextual information."
1758 "\\\\\n")
1761 ;;;; Link
1763 (defun org-latex--inline-image (link info)
1764 "Return LaTeX code for an inline image.
1765 LINK is the link pointing to the inline image. INFO is a plist
1766 used as a communication channel."
1767 (let* ((parent (org-export-get-parent-element link))
1768 (path (let ((raw-path (org-element-property :path link)))
1769 (if (not (file-name-absolute-p raw-path)) raw-path
1770 (expand-file-name raw-path))))
1771 (filetype (file-name-extension path))
1772 (caption (org-latex--caption/label-string parent info))
1773 ;; Retrieve latex attributes from the element around.
1774 (attr (org-export-read-attribute :attr_latex parent))
1775 (float (let ((float (plist-get attr :float)))
1776 (cond ((and (not float) (plist-member attr :float)) nil)
1777 ((string= float "wrap") 'wrap)
1778 ((string= float "multicolumn") 'multicolumn)
1779 ((or float
1780 (org-element-property :caption parent)
1781 (org-string-nw-p (plist-get attr :caption)))
1782 'figure))))
1783 (placement
1784 (let ((place (plist-get attr :placement)))
1785 (cond (place (format "%s" place))
1786 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1787 ((eq float 'figure)
1788 (format "[%s]" org-latex-default-figure-position))
1789 (t ""))))
1790 (comment-include (if (plist-get attr :comment-include) "%" ""))
1791 ;; It is possible to specify width and height in the
1792 ;; ATTR_LATEX line, and also via default variables.
1793 (width (cond ((plist-get attr :width))
1794 ((plist-get attr :height) "")
1795 ((eq float 'wrap) "0.48\\textwidth")
1796 (t org-latex-image-default-width)))
1797 (height (cond ((plist-get attr :height))
1798 ((or (plist-get attr :width)
1799 (memq float '(figure wrap))) "")
1800 (t org-latex-image-default-height)))
1801 (options (let ((opt (or (plist-get attr :options)
1802 org-latex-image-default-option)))
1803 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1804 (match-string 1 opt))))
1805 image-code)
1806 (if (member filetype '("tikz" "pgf"))
1807 ;; For tikz images:
1808 ;; - use \input to read in image file.
1809 ;; - if options are present, wrap in a tikzpicture environment.
1810 ;; - if width or height are present, use \resizebox to change
1811 ;; the image size.
1812 (progn
1813 (setq image-code (format "\\input{%s}" path))
1814 (when (org-string-nw-p options)
1815 (setq image-code
1816 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1817 options
1818 image-code)))
1819 (when (or (org-string-nw-p width) (org-string-nw-p height))
1820 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1821 (if (org-string-nw-p width) width "!")
1822 (if (org-string-nw-p height) height "!")
1823 image-code))))
1824 ;; For other images:
1825 ;; - add width and height to options.
1826 ;; - include the image with \includegraphics.
1827 (when (org-string-nw-p width)
1828 (setq options (concat options ",width=" width)))
1829 (when (org-string-nw-p height)
1830 (setq options (concat options ",height=" height)))
1831 (setq image-code
1832 (format "\\includegraphics%s{%s}"
1833 (cond ((not (org-string-nw-p options)) "")
1834 ((= (aref options 0) ?,)
1835 (format "[%s]"(substring options 1)))
1836 (t (format "[%s]" options)))
1837 path))
1838 (when (equal filetype "svg")
1839 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
1840 "\\includesvg"
1841 image-code
1842 nil t))
1843 (setq image-code (replace-regexp-in-string "\\.svg}"
1845 image-code
1846 nil t))))
1847 ;; Return proper string, depending on FLOAT.
1848 (case float
1849 (wrap (format "\\begin{wrapfigure}%s
1850 \\centering
1851 %s%s
1852 %s\\end{wrapfigure}" placement comment-include image-code caption))
1853 (multicolumn (format "\\begin{figure*}%s
1854 \\centering
1855 %s%s
1856 %s\\end{figure*}" placement comment-include image-code caption))
1857 (figure (format "\\begin{figure}%s
1858 \\centering
1859 %s%s
1860 %s\\end{figure}" placement comment-include image-code caption))
1861 (otherwise image-code))))
1863 (defun org-latex-link (link desc info)
1864 "Transcode a LINK object from Org to LaTeX.
1866 DESC is the description part of the link, or the empty string.
1867 INFO is a plist holding contextual information. See
1868 `org-export-data'."
1869 (let* ((type (org-element-property :type link))
1870 (raw-path (org-element-property :path link))
1871 ;; Ensure DESC really exists, or set it to nil.
1872 (desc (and (not (string= desc "")) desc))
1873 (imagep (org-export-inline-image-p
1874 link org-latex-inline-image-rules))
1875 (path (cond
1876 ((member type '("http" "https" "ftp" "mailto"))
1877 (concat type ":" raw-path))
1878 ((string= type "file")
1879 (if (not (file-name-absolute-p raw-path)) raw-path
1880 (concat "file://" (expand-file-name raw-path))))
1881 (t raw-path)))
1882 protocol)
1883 (cond
1884 ;; Image file.
1885 (imagep (org-latex--inline-image link info))
1886 ;; Radio link: Transcode target's contents and use them as link's
1887 ;; description.
1888 ((string= type "radio")
1889 (let ((destination (org-export-resolve-radio-link link info)))
1890 (when destination
1891 (format "\\hyperref[%s]{%s}"
1892 (org-export-solidify-link-text path)
1893 (org-export-data (org-element-contents destination) info)))))
1894 ;; Links pointing to a headline: Find destination and build
1895 ;; appropriate referencing command.
1896 ((member type '("custom-id" "fuzzy" "id"))
1897 (let ((destination (if (string= type "fuzzy")
1898 (org-export-resolve-fuzzy-link link info)
1899 (org-export-resolve-id-link link info))))
1900 (case (org-element-type destination)
1901 ;; Id link points to an external file.
1902 (plain-text
1903 (if desc (format "\\href{%s}{%s}" destination desc)
1904 (format "\\url{%s}" destination)))
1905 ;; Fuzzy link points nowhere.
1906 ('nil
1907 (format org-latex-link-with-unknown-path-format
1908 (or desc
1909 (org-export-data
1910 (org-element-property :raw-link link) info))))
1911 ;; LINK points to a headline. If headlines are numbered
1912 ;; and the link has no description, display headline's
1913 ;; number. Otherwise, display description or headline's
1914 ;; title.
1915 (headline
1916 (let* ((custom-label
1917 (and (plist-get info :latex-custom-id-labels)
1918 (org-element-property :CUSTOM_ID destination)))
1919 (label
1921 custom-label
1922 (format "sec-%s"
1923 (mapconcat
1924 #'number-to-string
1925 (org-export-get-headline-number destination info)
1926 "-")))))
1927 (if (and (plist-get info :section-numbers) (not desc))
1928 (format "\\ref{%s}" label)
1929 (format "\\hyperref[%s]{%s}" label
1930 (or desc
1931 (org-export-data
1932 (org-element-property :title destination) info))))))
1933 ;; Fuzzy link points to a target. Do as above.
1934 (otherwise
1935 (let ((path (org-export-solidify-link-text path)))
1936 (if (not desc) (format "\\ref{%s}" path)
1937 (format "\\hyperref[%s]{%s}" path desc)))))))
1938 ;; Coderef: replace link with the reference name or the
1939 ;; equivalent line number.
1940 ((string= type "coderef")
1941 (format (org-export-get-coderef-format path desc)
1942 (org-export-resolve-coderef path info)))
1943 ;; Link type is handled by a special function.
1944 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1945 (funcall protocol (org-link-unescape path) desc 'latex))
1946 ;; External link with a description part.
1947 ((and path desc) (format "\\href{%s}{%s}" path desc))
1948 ;; External link without a description part.
1949 (path (format "\\url{%s}" path))
1950 ;; No path, only description. Try to do something useful.
1951 (t (format org-latex-link-with-unknown-path-format desc)))))
1954 ;;;; Node Property
1956 (defun org-latex-node-property (node-property contents info)
1957 "Transcode a NODE-PROPERTY element from Org to LaTeX.
1958 CONTENTS is nil. INFO is a plist holding contextual
1959 information."
1960 (format "%s:%s"
1961 (org-element-property :key node-property)
1962 (let ((value (org-element-property :value node-property)))
1963 (if value (concat " " value) ""))))
1966 ;;;; Paragraph
1968 (defun org-latex-paragraph (paragraph contents info)
1969 "Transcode a PARAGRAPH element from Org to LaTeX.
1970 CONTENTS is the contents of the paragraph, as a string. INFO is
1971 the plist used as a communication channel."
1972 contents)
1975 ;;;; Plain List
1977 (defun org-latex-plain-list (plain-list contents info)
1978 "Transcode a PLAIN-LIST element from Org to LaTeX.
1979 CONTENTS is the contents of the list. INFO is a plist holding
1980 contextual information."
1981 (let* ((type (org-element-property :type plain-list))
1982 (attr (org-export-read-attribute :attr_latex plain-list))
1983 (latex-type (let ((env (plist-get attr :environment)))
1984 (cond (env (format "%s" env))
1985 ((eq type 'ordered) "enumerate")
1986 ((eq type 'descriptive) "description")
1987 (t "itemize")))))
1988 (org-latex--wrap-label
1989 plain-list
1990 (format "\\begin{%s}%s\n%s\\end{%s}"
1991 latex-type
1992 (or (plist-get attr :options) "")
1993 contents
1994 latex-type))))
1997 ;;;; Plain Text
1999 (defun org-latex-plain-text (text info)
2000 "Transcode a TEXT string from Org to LaTeX.
2001 TEXT is the string to transcode. INFO is a plist holding
2002 contextual information."
2003 (let ((specialp (plist-get info :with-special-strings))
2004 (output text))
2005 ;; Protect %, #, &, $, _, { and }.
2006 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
2007 (setq output
2008 (replace-match
2009 (format "\\%s" (match-string 2 output)) nil t output 2)))
2010 ;; Protect ^.
2011 (setq output
2012 (replace-regexp-in-string
2013 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
2014 ;; Protect \. If special strings are used, be careful not to
2015 ;; protect "\" in "\-" constructs.
2016 (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
2017 (setq output
2018 (replace-regexp-in-string
2019 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
2020 "$\\backslash$" output nil t 1)))
2021 ;; Protect ~.
2022 (setq output
2023 (replace-regexp-in-string
2024 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
2025 ;; Activate smart quotes. Be sure to provide original TEXT string
2026 ;; since OUTPUT may have been modified.
2027 (when (plist-get info :with-smart-quotes)
2028 (setq output (org-export-activate-smart-quotes output :latex info text)))
2029 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
2030 (let ((case-fold-search nil)
2031 (start 0))
2032 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
2033 (setq output (replace-match
2034 (format "\\%s{}" (match-string 1 output)) nil t output)
2035 start (match-end 0))))
2036 ;; Convert special strings.
2037 (when specialp
2038 (setq output
2039 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
2040 ;; Handle break preservation if required.
2041 (when (plist-get info :preserve-breaks)
2042 (setq output (replace-regexp-in-string
2043 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
2044 ;; Return value.
2045 output))
2048 ;;;; Planning
2050 (defun org-latex-planning (planning contents info)
2051 "Transcode a PLANNING element from Org to LaTeX.
2052 CONTENTS is nil. INFO is a plist holding contextual
2053 information."
2054 (concat
2055 "\\noindent"
2056 (mapconcat
2057 'identity
2058 (delq nil
2059 (list
2060 (let ((closed (org-element-property :closed planning)))
2061 (when closed
2062 (concat
2063 (format "\\textbf{%s} " org-closed-string)
2064 (format org-latex-inactive-timestamp-format
2065 (org-translate-time
2066 (org-element-property :raw-value closed))))))
2067 (let ((deadline (org-element-property :deadline planning)))
2068 (when deadline
2069 (concat
2070 (format "\\textbf{%s} " org-deadline-string)
2071 (format org-latex-active-timestamp-format
2072 (org-translate-time
2073 (org-element-property :raw-value deadline))))))
2074 (let ((scheduled (org-element-property :scheduled planning)))
2075 (when scheduled
2076 (concat
2077 (format "\\textbf{%s} " org-scheduled-string)
2078 (format org-latex-active-timestamp-format
2079 (org-translate-time
2080 (org-element-property :raw-value scheduled))))))))
2081 " ")
2082 "\\\\"))
2085 ;;;; Property Drawer
2087 (defun org-latex-property-drawer (property-drawer contents info)
2088 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2089 CONTENTS holds the contents of the drawer. INFO is a plist
2090 holding contextual information."
2091 (and (org-string-nw-p contents)
2092 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2095 ;;;; Pseudo Object: LaTeX Math Block
2097 (defun org-latex--wrap-latex-math-block (data info)
2098 "Merge contiguous math objects in a pseudo-object container.
2099 DATA is a parse tree or a secondary string. INFO is a plist
2100 containing export options. Modify DATA by side-effect and return it."
2101 (let ((valid-object-p
2102 (function
2103 ;; Non-nil when OBJ can be added to the latex math block.
2104 (lambda (obj)
2105 (case (org-element-type obj)
2106 (entity (org-element-property :latex-math-p obj))
2107 (latex-fragment
2108 (let ((value (org-element-property :value obj)))
2109 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2110 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2111 ((subscript superscript) t))))))
2112 (org-element-map data '(entity latex-fragment subscript superscript)
2113 (lambda (object)
2114 ;; Skip objects already wrapped.
2115 (when (and (not (eq (org-element-type
2116 (org-element-property :parent object))
2117 'latex-math-block))
2118 (funcall valid-object-p object))
2119 (let ((math-block (list 'latex-math-block nil))
2120 (next-elements (org-export-get-next-element object info t))
2121 (last object))
2122 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2123 (org-element-insert-before math-block object)
2124 (org-element-extract-element object)
2125 (org-element-adopt-elements math-block object)
2126 (when (zerop (or (org-element-property :post-blank object) 0))
2127 ;; MATH-BLOCK swallows consecutive math objects.
2128 (catch 'exit
2129 (dolist (next next-elements)
2130 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2131 (org-element-extract-element next)
2132 (org-element-adopt-elements math-block next)
2133 ;; Eschew the case: \beta$x$ -> \(\betax\).
2134 (unless (memq (org-element-type next)
2135 '(subscript superscript))
2136 (org-element-put-property last :post-blank 1))
2137 (setq last next)
2138 (when (> (or (org-element-property :post-blank next) 0) 0)
2139 (throw 'exit nil))))))
2140 (org-element-put-property
2141 math-block :post-blank (org-element-property :post-blank last)))))
2142 info nil '(subscript superscript latex-math-block) t)
2143 ;; Return updated DATA.
2144 data))
2146 (defun org-latex-math-block-tree-filter (tree backend info)
2147 (org-latex--wrap-latex-math-block tree info))
2149 (defun org-latex-math-block-options-filter (info backend)
2150 (dolist (prop '(:author :date :title) info)
2151 (plist-put info prop
2152 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2154 (defun org-latex-math-block (math-block contents info)
2155 "Transcode a MATH-BLOCK object from Org to LaTeX.
2156 CONTENTS is a string. INFO is a plist used as a communication
2157 channel."
2158 (when (org-string-nw-p contents)
2159 (format "\\(%s\\)" (org-trim contents))))
2161 ;;;; Quote Block
2163 (defun org-latex-quote-block (quote-block contents info)
2164 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2165 CONTENTS holds the contents of the block. INFO is a plist
2166 holding contextual information."
2167 (org-latex--wrap-label
2168 quote-block
2169 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2172 ;;;; Radio Target
2174 (defun org-latex-radio-target (radio-target text info)
2175 "Transcode a RADIO-TARGET object from Org to LaTeX.
2176 TEXT is the text of the target. INFO is a plist holding
2177 contextual information."
2178 (format "\\label{%s}%s"
2179 (org-export-solidify-link-text
2180 (org-element-property :value radio-target))
2181 text))
2184 ;;;; Section
2186 (defun org-latex-section (section contents info)
2187 "Transcode a SECTION element from Org to LaTeX.
2188 CONTENTS holds the contents of the section. INFO is a plist
2189 holding contextual information."
2190 contents)
2193 ;;;; Special Block
2195 (defun org-latex-special-block (special-block contents info)
2196 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2197 CONTENTS holds the contents of the block. INFO is a plist
2198 holding contextual information."
2199 (let ((type (downcase (org-element-property :type special-block)))
2200 (opt (org-export-read-attribute :attr_latex special-block :options)))
2201 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2202 ;; Insert any label or caption within the block
2203 ;; (otherwise, a reference pointing to that element will
2204 ;; count the section instead).
2205 (org-latex--caption/label-string special-block info)
2206 contents
2207 (format "\\end{%s}" type))))
2210 ;;;; Src Block
2212 (defun org-latex-src-block (src-block contents info)
2213 "Transcode a SRC-BLOCK element from Org to LaTeX.
2214 CONTENTS holds the contents of the item. INFO is a plist holding
2215 contextual information."
2216 (when (org-string-nw-p (org-element-property :value src-block))
2217 (let* ((lang (org-element-property :language src-block))
2218 (caption (org-element-property :caption src-block))
2219 (label (org-element-property :name src-block))
2220 (custom-env (and lang
2221 (cadr (assq (intern lang)
2222 org-latex-custom-lang-environments))))
2223 (num-start (case (org-element-property :number-lines src-block)
2224 (continued (org-export-get-loc src-block info))
2225 (new 0)))
2226 (retain-labels (org-element-property :retain-labels src-block))
2227 (attributes (org-export-read-attribute :attr_latex src-block))
2228 (float (plist-get attributes :float)))
2229 (cond
2230 ;; Case 1. No source fontification.
2231 ((not org-latex-listings)
2232 (let* ((caption-str (org-latex--caption/label-string src-block info))
2233 (float-env
2234 (cond ((and (not float) (plist-member attributes :float)) "%s")
2235 ((string= "multicolumn" float)
2236 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2237 org-latex-default-figure-position
2238 caption-str))
2239 ((or caption float)
2240 (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
2241 caption-str))
2242 (t "%s"))))
2243 (format
2244 float-env
2245 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2246 (org-export-format-code-default src-block info))))))
2247 ;; Case 2. Custom environment.
2248 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2249 custom-env
2250 (org-export-format-code-default src-block info)
2251 custom-env))
2252 ;; Case 3. Use minted package.
2253 ((eq org-latex-listings 'minted)
2254 (let* ((caption-str (org-latex--caption/label-string src-block info))
2255 (float-env
2256 (cond ((and (not float) (plist-member attributes :float)) "%s")
2257 ((string= "multicolumn" float)
2258 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2259 caption-str))
2260 ((or caption float)
2261 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2262 caption-str))
2263 (t "%s")))
2264 (body
2265 (format
2266 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2267 ;; Options.
2268 (org-latex--make-option-string
2269 (if (or (not num-start)
2270 (assoc "linenos" org-latex-minted-options))
2271 org-latex-minted-options
2272 (append
2273 `(("linenos")
2274 ("firstnumber" ,(number-to-string (1+ num-start))))
2275 org-latex-minted-options)))
2276 ;; Language.
2277 (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
2278 ;; Source code.
2279 (let* ((code-info (org-export-unravel-code src-block))
2280 (max-width
2281 (apply 'max
2282 (mapcar 'length
2283 (org-split-string (car code-info)
2284 "\n")))))
2285 (org-export-format-code
2286 (car code-info)
2287 (lambda (loc num ref)
2288 (concat
2290 (when ref
2291 ;; Ensure references are flushed to the right,
2292 ;; separated with 6 spaces from the widest line
2293 ;; of code.
2294 (concat (make-string (+ (- max-width (length loc)) 6)
2295 ?\s)
2296 (format "(%s)" ref)))))
2297 nil (and retain-labels (cdr code-info)))))))
2298 ;; Return value.
2299 (format float-env body)))
2300 ;; Case 4. Use listings package.
2302 (let ((lst-lang
2303 (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
2304 (caption-str
2305 (when caption
2306 (let ((main (org-export-get-caption src-block))
2307 (secondary (org-export-get-caption src-block t)))
2308 (if (not secondary)
2309 (format "{%s}" (org-export-data main info))
2310 (format "{[%s]%s}"
2311 (org-export-data secondary info)
2312 (org-export-data main info)))))))
2313 (concat
2314 ;; Options.
2315 (format
2316 "\\lstset{%s}\n"
2317 (org-latex--make-option-string
2318 (append
2319 org-latex-listings-options
2320 (cond
2321 ((and (not float) (plist-member attributes :float)) nil)
2322 ((string= "multicolumn" float) '(("float" "*")))
2323 ((and float (not (assoc "float" org-latex-listings-options)))
2324 `(("float" ,org-latex-default-figure-position))))
2325 `(("language" ,lst-lang))
2326 (when label `(("label" ,label)))
2327 (when caption-str `(("caption" ,caption-str)))
2328 (cond ((assoc "numbers" org-latex-listings-options) nil)
2329 ((not num-start) '(("numbers" "none")))
2330 ((zerop num-start) '(("numbers" "left")))
2331 (t `(("numbers" "left")
2332 ("firstnumber"
2333 ,(number-to-string (1+ num-start)))))))))
2334 ;; Source code.
2335 (format
2336 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2337 (let* ((code-info (org-export-unravel-code src-block))
2338 (max-width
2339 (apply 'max
2340 (mapcar 'length
2341 (org-split-string (car code-info) "\n")))))
2342 (org-export-format-code
2343 (car code-info)
2344 (lambda (loc num ref)
2345 (concat
2347 (when ref
2348 ;; Ensure references are flushed to the right,
2349 ;; separated with 6 spaces from the widest line of
2350 ;; code
2351 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2352 (format "(%s)" ref)))))
2353 nil (and retain-labels (cdr code-info))))))))))))
2356 ;;;; Statistics Cookie
2358 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2359 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2360 CONTENTS is nil. INFO is a plist holding contextual information."
2361 (replace-regexp-in-string
2362 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2365 ;;;; Strike-Through
2367 (defun org-latex-strike-through (strike-through contents info)
2368 "Transcode STRIKE-THROUGH from Org to LaTeX.
2369 CONTENTS is the text with strike-through markup. INFO is a plist
2370 holding contextual information."
2371 (org-latex--text-markup contents 'strike-through))
2374 ;;;; Subscript
2376 (defun org-latex--script-size (object info)
2377 "Transcode a subscript or superscript object.
2378 OBJECT is an Org object. INFO is a plist used as a communication
2379 channel."
2380 (let ((type (org-element-type object))
2381 (output ""))
2382 (org-element-map (org-element-contents object)
2383 (cons 'plain-text org-element-all-objects)
2384 (lambda (obj)
2385 (case (org-element-type obj)
2386 ((entity latex-fragment)
2387 (let ((data (org-trim (org-export-data obj info))))
2388 (string-match
2389 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2390 data)
2391 (setq output
2392 (concat output
2393 (match-string 1 data)
2394 (let ((blank (org-element-property :post-blank obj)))
2395 (and blank (> blank 0) "\\ "))))))
2396 (plain-text
2397 (setq output
2398 (format "%s\\text{%s}" output (org-export-data obj info))))
2399 (otherwise
2400 (setq output
2401 (concat output
2402 (org-export-data obj info)
2403 (let ((blank (org-element-property :post-blank obj)))
2404 (and blank (> blank 0) "\\ ")))))))
2405 info nil org-element-recursive-objects)
2406 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2407 ;; character.
2408 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2409 (and (> (length output) 1) "{")
2410 output
2411 (and (> (length output) 1) "}"))))
2413 (defun org-latex-subscript (subscript contents info)
2414 "Transcode a SUBSCRIPT object from Org to LaTeX.
2415 CONTENTS is the contents of the object. INFO is a plist holding
2416 contextual information."
2417 (org-latex--script-size subscript info))
2420 ;;;; Superscript
2422 (defun org-latex-superscript (superscript contents info)
2423 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2424 CONTENTS is the contents of the object. INFO is a plist holding
2425 contextual information."
2426 (org-latex--script-size superscript info))
2429 ;;;; Table
2431 ;; `org-latex-table' is the entry point for table transcoding. It
2432 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2433 ;; delegates the job to either `org-latex--table.el-table',
2434 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2435 ;; depending of the type of the table and the mode requested.
2437 ;; `org-latex--align-string' is a subroutine used to build alignment
2438 ;; string for Org tables.
2440 (defun org-latex-table (table contents info)
2441 "Transcode a TABLE element from Org to LaTeX.
2442 CONTENTS is the contents of the table. INFO is a plist holding
2443 contextual information."
2444 (if (eq (org-element-property :type table) 'table.el)
2445 ;; "table.el" table. Convert it using appropriate tools.
2446 (org-latex--table.el-table table info)
2447 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2448 org-latex-default-table-mode)))
2449 (cond
2450 ;; Case 1: Verbatim table.
2451 ((string= type "verbatim")
2452 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2453 ;; Re-create table, without affiliated keywords.
2454 (org-trim (org-element-interpret-data
2455 `(table nil ,@(org-element-contents table))
2456 org-latex-pseudo-objects))))
2457 ;; Case 2: Matrix.
2458 ((or (string= type "math") (string= type "inline-math"))
2459 (org-latex--math-table table info))
2460 ;; Case 3: Standard table.
2461 (t (concat (org-latex--org-table table contents info)
2462 ;; When there are footnote references within the
2463 ;; table, insert their definition just after it.
2464 (org-latex--delayed-footnotes-definitions table info)))))))
2466 (defun org-latex--align-string (table info)
2467 "Return an appropriate LaTeX alignment string.
2468 TABLE is the considered table. INFO is a plist used as
2469 a communication channel."
2470 (or (org-export-read-attribute :attr_latex table :align)
2471 (let (align)
2472 ;; Extract column groups and alignment from first (non-rule)
2473 ;; row.
2474 (org-element-map
2475 (org-element-map table 'table-row
2476 (lambda (row)
2477 (and (eq (org-element-property :type row) 'standard) row))
2478 info 'first-match)
2479 'table-cell
2480 (lambda (cell)
2481 (let ((borders (org-export-table-cell-borders cell info)))
2482 ;; Check left border for the first cell only.
2483 (when (and (memq 'left borders) (not align))
2484 (push "|" align))
2485 (push (case (org-export-table-cell-alignment cell info)
2486 (left "l")
2487 (right "r")
2488 (center "c"))
2489 align)
2490 (when (memq 'right borders) (push "|" align))))
2491 info)
2492 (apply 'concat (nreverse align)))))
2494 (defun org-latex--org-table (table contents info)
2495 "Return appropriate LaTeX code for an Org table.
2497 TABLE is the table type element to transcode. CONTENTS is its
2498 contents, as a string. INFO is a plist used as a communication
2499 channel.
2501 This function assumes TABLE has `org' as its `:type' property and
2502 `table' as its `:mode' attribute."
2503 (let* ((caption (org-latex--caption/label-string table info))
2504 (attr (org-export-read-attribute :attr_latex table))
2505 ;; Determine alignment string.
2506 (alignment (org-latex--align-string table info))
2507 ;; Determine environment for the table: longtable, tabular...
2508 (table-env (or (plist-get attr :environment)
2509 org-latex-default-table-environment))
2510 ;; If table is a float, determine environment: table, table*
2511 ;; or sidewaystable.
2512 (float-env (unless (member table-env '("longtable" "longtabu"))
2513 (let ((float (plist-get attr :float)))
2514 (cond
2515 ((and (not float) (plist-member attr :float)) nil)
2516 ((string= float "sidewaystable") "sidewaystable")
2517 ((string= float "multicolumn") "table*")
2518 ((or float
2519 (org-element-property :caption table)
2520 (org-string-nw-p (plist-get attr :caption)))
2521 "table")))))
2522 ;; Extract others display options.
2523 (fontsize (let ((font (plist-get attr :font)))
2524 (and font (concat font "\n"))))
2525 (width (plist-get attr :width))
2526 (spreadp (plist-get attr :spread))
2527 (placement (or (plist-get attr :placement)
2528 (format "[%s]" org-latex-default-figure-position)))
2529 (centerp (if (plist-member attr :center) (plist-get attr :center)
2530 org-latex-tables-centered)))
2531 ;; Prepare the final format string for the table.
2532 (cond
2533 ;; Longtable.
2534 ((equal "longtable" table-env)
2535 (concat (and fontsize (concat "{" fontsize))
2536 (format "\\begin{longtable}{%s}\n" alignment)
2537 (and org-latex-table-caption-above
2538 (org-string-nw-p caption)
2539 (concat caption "\\\\\n"))
2540 contents
2541 (and (not org-latex-table-caption-above)
2542 (org-string-nw-p caption)
2543 (concat caption "\\\\\n"))
2544 "\\end{longtable}\n"
2545 (and fontsize "}")))
2546 ;; Longtabu
2547 ((equal "longtabu" table-env)
2548 (concat (and fontsize (concat "{" fontsize))
2549 (format "\\begin{longtabu}%s{%s}\n"
2550 (if width
2551 (format " %s %s "
2552 (if spreadp "spread" "to") width) "")
2553 alignment)
2554 (and org-latex-table-caption-above
2555 (org-string-nw-p caption)
2556 (concat caption "\\\\\n"))
2557 contents
2558 (and (not org-latex-table-caption-above)
2559 (org-string-nw-p caption)
2560 (concat caption "\\\\\n"))
2561 "\\end{longtabu}\n"
2562 (and fontsize "}")))
2563 ;; Others.
2564 (t (concat (cond
2565 (float-env
2566 (concat (format "\\begin{%s}%s\n" float-env placement)
2567 (if org-latex-table-caption-above caption "")
2568 (when centerp "\\centering\n")
2569 fontsize))
2570 (centerp (concat "\\begin{center}\n" fontsize))
2571 (fontsize (concat "{" fontsize)))
2572 (cond ((equal "tabu" table-env)
2573 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2574 (if width (format
2575 (if spreadp " spread %s " " to %s ")
2576 width) "")
2577 alignment
2578 contents))
2579 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2580 table-env
2581 (if width (format "{%s}" width) "")
2582 alignment
2583 contents
2584 table-env)))
2585 (cond
2586 (float-env
2587 (concat (if org-latex-table-caption-above "" caption)
2588 (format "\n\\end{%s}" float-env)))
2589 (centerp "\n\\end{center}")
2590 (fontsize "}")))))))
2592 (defun org-latex--table.el-table (table info)
2593 "Return appropriate LaTeX code for a table.el table.
2595 TABLE is the table type element to transcode. INFO is a plist
2596 used as a communication channel.
2598 This function assumes TABLE has `table.el' as its `:type'
2599 property."
2600 (require 'table)
2601 ;; Ensure "*org-export-table*" buffer is empty.
2602 (with-current-buffer (get-buffer-create "*org-export-table*")
2603 (erase-buffer))
2604 (let ((output (with-temp-buffer
2605 (insert (org-element-property :value table))
2606 (goto-char 1)
2607 (re-search-forward "^[ \t]*|[^|]" nil t)
2608 (table-generate-source 'latex "*org-export-table*")
2609 (with-current-buffer "*org-export-table*"
2610 (org-trim (buffer-string))))))
2611 (kill-buffer (get-buffer "*org-export-table*"))
2612 ;; Remove left out comments.
2613 (while (string-match "^%.*\n" output)
2614 (setq output (replace-match "" t t output)))
2615 (let ((attr (org-export-read-attribute :attr_latex table)))
2616 (when (plist-get attr :rmlines)
2617 ;; When the "rmlines" attribute is provided, remove all hlines
2618 ;; but the the one separating heading from the table body.
2619 (let ((n 0) (pos 0))
2620 (while (and (< (length output) pos)
2621 (setq pos (string-match "^\\\\hline\n?" output pos)))
2622 (incf n)
2623 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2624 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2625 org-latex-tables-centered)))
2626 (if (not centerp) output
2627 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2629 (defun org-latex--math-table (table info)
2630 "Return appropriate LaTeX code for a matrix.
2632 TABLE is the table type element to transcode. INFO is a plist
2633 used as a communication channel.
2635 This function assumes TABLE has `org' as its `:type' property and
2636 `inline-math' or `math' as its `:mode' attribute.."
2637 (let* ((caption (org-latex--caption/label-string table info))
2638 (attr (org-export-read-attribute :attr_latex table))
2639 (inlinep (equal (plist-get attr :mode) "inline-math"))
2640 (env (or (plist-get attr :environment)
2641 org-latex-default-table-environment))
2642 (contents
2643 (mapconcat
2644 (lambda (row)
2645 ;; Ignore horizontal rules.
2646 (when (eq (org-element-property :type row) 'standard)
2647 ;; Return each cell unmodified.
2648 (concat
2649 (mapconcat
2650 (lambda (cell)
2651 (substring
2652 (org-element-interpret-data cell org-latex-pseudo-objects)
2653 0 -1))
2654 (org-element-map row 'table-cell 'identity info) "&")
2655 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2656 "\n")))
2657 (org-element-map table 'table-row 'identity info) ""))
2658 ;; Variables related to math clusters (contiguous math tables
2659 ;; of the same type).
2660 (mode (org-export-read-attribute :attr_latex table :mode))
2661 (prev (org-export-get-previous-element table info))
2662 (next (org-export-get-next-element table info))
2663 (same-mode-p
2664 (lambda (table)
2665 ;; Non-nil when TABLE has the same mode as current table.
2666 (string= (or (org-export-read-attribute :attr_latex table :mode)
2667 org-latex-default-table-mode)
2668 mode))))
2669 (concat
2670 ;; Opening string. If TABLE is in the middle of a table cluster,
2671 ;; do not insert any.
2672 (cond ((and prev
2673 (eq (org-element-type prev) 'table)
2674 (memq (org-element-property :post-blank prev) '(0 nil))
2675 (funcall same-mode-p prev))
2676 nil)
2677 (inlinep "\\(")
2678 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2679 (t "\\["))
2680 ;; Prefix.
2681 (or (plist-get attr :math-prefix) "")
2682 ;; Environment. Also treat special cases.
2683 (cond ((equal env "array")
2684 (let ((align (org-latex--align-string table info)))
2685 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2686 ((assoc env org-latex-table-matrix-macros)
2687 (format "\\%s%s{\n%s}"
2689 (or (plist-get attr :math-arguments) "")
2690 contents))
2691 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2692 ;; Suffix.
2693 (or (plist-get attr :math-suffix) "")
2694 ;; Closing string. If TABLE is in the middle of a table cluster,
2695 ;; do not insert any. If it closes such a cluster, be sure to
2696 ;; close the cluster with a string matching the opening string.
2697 (cond ((and next
2698 (eq (org-element-type next) 'table)
2699 (memq (org-element-property :post-blank table) '(0 nil))
2700 (funcall same-mode-p next))
2701 nil)
2702 (inlinep "\\)")
2703 ;; Find cluster beginning to know which environment to use.
2704 ((let ((cluster-beg table) prev)
2705 (while (and (setq prev (org-export-get-previous-element
2706 cluster-beg info))
2707 (memq (org-element-property :post-blank prev)
2708 '(0 nil))
2709 (funcall same-mode-p prev))
2710 (setq cluster-beg prev))
2711 (and (or (org-element-property :caption cluster-beg)
2712 (org-element-property :name cluster-beg))
2713 "\n\\end{equation}")))
2714 (t "\\]")))))
2717 ;;;; Table Cell
2719 (defun org-latex-table-cell (table-cell contents info)
2720 "Transcode a TABLE-CELL element from Org to LaTeX.
2721 CONTENTS is the cell contents. INFO is a plist used as
2722 a communication channel."
2723 (concat (if (and contents
2724 org-latex-table-scientific-notation
2725 (string-match orgtbl-exp-regexp contents))
2726 ;; Use appropriate format string for scientific
2727 ;; notation.
2728 (format org-latex-table-scientific-notation
2729 (match-string 1 contents)
2730 (match-string 2 contents))
2731 contents)
2732 (when (org-export-get-next-element table-cell info) " & ")))
2735 ;;;; Table Row
2737 (defun org-latex-table-row (table-row contents info)
2738 "Transcode a TABLE-ROW element from Org to LaTeX.
2739 CONTENTS is the contents of the row. INFO is a plist used as
2740 a communication channel."
2741 ;; Rules are ignored since table separators are deduced from
2742 ;; borders of the current row.
2743 (when (eq (org-element-property :type table-row) 'standard)
2744 (let* ((attr (org-export-read-attribute :attr_latex
2745 (org-export-get-parent table-row)))
2746 (longtablep (member (or (plist-get attr :environment)
2747 org-latex-default-table-environment)
2748 '("longtable" "longtabu")))
2749 (booktabsp (if (plist-member attr :booktabs)
2750 (plist-get attr :booktabs)
2751 org-latex-tables-booktabs))
2752 ;; TABLE-ROW's borders are extracted from its first cell.
2753 (borders (org-export-table-cell-borders
2754 (car (org-element-contents table-row)) info)))
2755 (concat
2756 ;; When BOOKTABS are activated enforce top-rule even when no
2757 ;; hline was specifically marked.
2758 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2759 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2760 contents "\\\\\n"
2761 (cond
2762 ;; Special case for long tables. Define header and footers.
2763 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2764 (format "%s
2765 \\endfirsthead
2766 \\multicolumn{%d}{l}{%s} \\\\
2768 %s \\\\\n
2770 \\endhead
2771 %s\\multicolumn{%d}{r}{%s} \\\\
2772 \\endfoot
2773 \\endlastfoot"
2774 (if booktabsp "\\midrule" "\\hline")
2775 (cdr (org-export-table-dimensions
2776 (org-export-get-parent-table table-row) info))
2777 (org-latex--translate "Continued from previous page" info)
2778 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2779 ((and (memq 'top borders)
2780 (memq 'above borders)) "\\hline\n")
2781 (t ""))
2782 contents
2783 (if booktabsp "\\midrule" "\\hline")
2784 (if booktabsp "\\midrule" "\\hline")
2785 ;; Number of columns.
2786 (cdr (org-export-table-dimensions
2787 (org-export-get-parent-table table-row) info))
2788 (org-latex--translate "Continued on next page" info)))
2789 ;; When BOOKTABS are activated enforce bottom rule even when
2790 ;; no hline was specifically marked.
2791 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2792 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2793 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2796 ;;;; Target
2798 (defun org-latex-target (target contents info)
2799 "Transcode a TARGET object from Org to LaTeX.
2800 CONTENTS is nil. INFO is a plist holding contextual
2801 information."
2802 (format "\\label{%s}"
2803 (org-export-solidify-link-text (org-element-property :value target))))
2806 ;;;; Timestamp
2808 (defun org-latex-timestamp (timestamp contents info)
2809 "Transcode a TIMESTAMP object from Org to LaTeX.
2810 CONTENTS is nil. INFO is a plist holding contextual
2811 information."
2812 (let ((value (org-latex-plain-text
2813 (org-timestamp-translate timestamp) info)))
2814 (case (org-element-property :type timestamp)
2815 ((active active-range) (format org-latex-active-timestamp-format value))
2816 ((inactive inactive-range)
2817 (format org-latex-inactive-timestamp-format value))
2818 (otherwise (format org-latex-diary-timestamp-format value)))))
2821 ;;;; Underline
2823 (defun org-latex-underline (underline contents info)
2824 "Transcode UNDERLINE from Org to LaTeX.
2825 CONTENTS is the text with underline markup. INFO is a plist
2826 holding contextual information."
2827 (org-latex--text-markup contents 'underline))
2830 ;;;; Verbatim
2832 (defun org-latex-verbatim (verbatim contents info)
2833 "Transcode a VERBATIM object from Org to LaTeX.
2834 CONTENTS is nil. INFO is a plist used as a communication
2835 channel."
2836 (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2839 ;;;; Verse Block
2841 (defun org-latex-verse-block (verse-block contents info)
2842 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2843 CONTENTS is verse block contents. INFO is a plist holding
2844 contextual information."
2845 (org-latex--wrap-label
2846 verse-block
2847 ;; In a verse environment, add a line break to each newline
2848 ;; character and change each white space at beginning of a line
2849 ;; into a space of 1 em. Also change each blank line with
2850 ;; a vertical space of 1 em.
2851 (progn
2852 (setq contents (replace-regexp-in-string
2853 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2854 (replace-regexp-in-string
2855 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2856 (while (string-match "^[ \t]+" contents)
2857 (let ((new-str (format "\\hspace*{%dem}"
2858 (length (match-string 0 contents)))))
2859 (setq contents (replace-match new-str nil t contents))))
2860 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2864 ;;; End-user functions
2866 ;;;###autoload
2867 (defun org-latex-export-as-latex
2868 (&optional async subtreep visible-only body-only ext-plist)
2869 "Export current buffer as a LaTeX buffer.
2871 If narrowing is active in the current buffer, only export its
2872 narrowed part.
2874 If a region is active, export that region.
2876 A non-nil optional argument ASYNC means the process should happen
2877 asynchronously. The resulting buffer should be accessible
2878 through the `org-export-stack' interface.
2880 When optional argument SUBTREEP is non-nil, export the sub-tree
2881 at point, extracting information from the headline properties
2882 first.
2884 When optional argument VISIBLE-ONLY is non-nil, don't export
2885 contents of hidden elements.
2887 When optional argument BODY-ONLY is non-nil, only write code
2888 between \"\\begin{document}\" and \"\\end{document}\".
2890 EXT-PLIST, when provided, is a property list with external
2891 parameters overriding Org default settings, but still inferior to
2892 file-local settings.
2894 Export is done in a buffer named \"*Org LATEX Export*\", which
2895 will be displayed when `org-export-show-temporary-export-buffer'
2896 is non-nil."
2897 (interactive)
2898 (org-export-to-buffer 'latex "*Org LATEX Export*"
2899 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
2901 ;;;###autoload
2902 (defun org-latex-convert-region-to-latex ()
2903 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2904 This can be used in any buffer. For example, you can write an
2905 itemized list in org-mode syntax in an LaTeX buffer and use this
2906 command to convert it."
2907 (interactive)
2908 (org-export-replace-region-by 'latex))
2910 ;;;###autoload
2911 (defun org-latex-export-to-latex
2912 (&optional async subtreep visible-only body-only ext-plist)
2913 "Export current buffer to a LaTeX file.
2915 If narrowing is active in the current buffer, only export its
2916 narrowed part.
2918 If a region is active, export that region.
2920 A non-nil optional argument ASYNC means the process should happen
2921 asynchronously. The resulting file should be accessible through
2922 the `org-export-stack' interface.
2924 When optional argument SUBTREEP is non-nil, export the sub-tree
2925 at point, extracting information from the headline properties
2926 first.
2928 When optional argument VISIBLE-ONLY is non-nil, don't export
2929 contents of hidden elements.
2931 When optional argument BODY-ONLY is non-nil, only write code
2932 between \"\\begin{document}\" and \"\\end{document}\".
2934 EXT-PLIST, when provided, is a property list with external
2935 parameters overriding Org default settings, but still inferior to
2936 file-local settings."
2937 (interactive)
2938 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2939 (org-export-to-file 'latex outfile
2940 async subtreep visible-only body-only ext-plist)))
2942 ;;;###autoload
2943 (defun org-latex-export-to-pdf
2944 (&optional async subtreep visible-only body-only ext-plist)
2945 "Export current buffer to LaTeX then process through to PDF.
2947 If narrowing is active in the current buffer, only export its
2948 narrowed part.
2950 If a region is active, export that region.
2952 A non-nil optional argument ASYNC means the process should happen
2953 asynchronously. The resulting file should be accessible through
2954 the `org-export-stack' interface.
2956 When optional argument SUBTREEP is non-nil, export the sub-tree
2957 at point, extracting information from the headline properties
2958 first.
2960 When optional argument VISIBLE-ONLY is non-nil, don't export
2961 contents of hidden elements.
2963 When optional argument BODY-ONLY is non-nil, only write code
2964 between \"\\begin{document}\" and \"\\end{document}\".
2966 EXT-PLIST, when provided, is a property list with external
2967 parameters overriding Org default settings, but still inferior to
2968 file-local settings.
2970 Return PDF file's name."
2971 (interactive)
2972 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2973 (org-export-to-file 'latex outfile
2974 async subtreep visible-only body-only ext-plist
2975 (lambda (file) (org-latex-compile file)))))
2977 (defun org-latex-compile (texfile &optional snippet)
2978 "Compile a TeX file.
2980 TEXFILE is the name of the file being compiled. Processing is
2981 done through the command specified in `org-latex-pdf-process'.
2983 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2984 file used to preview a LaTeX snippet. In this case, do not
2985 create a log buffer and do not bother removing log files.
2987 Return PDF file name or an error if it couldn't be produced."
2988 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
2989 (full-name (file-truename texfile))
2990 (out-dir (file-name-directory texfile))
2991 ;; Properly set working directory for compilation.
2992 (default-directory (if (file-name-absolute-p texfile)
2993 (file-name-directory full-name)
2994 default-directory))
2995 errors)
2996 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
2997 (save-window-excursion
2998 (cond
2999 ;; A function is provided: Apply it.
3000 ((functionp org-latex-pdf-process)
3001 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3002 ;; A list is provided: Replace %b, %f and %o with appropriate
3003 ;; values in each command before applying it. Output is
3004 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3005 ((consp org-latex-pdf-process)
3006 (let ((outbuf (and (not snippet)
3007 (get-buffer-create "*Org PDF LaTeX Output*"))))
3008 (mapc
3009 (lambda (command)
3010 (shell-command
3011 (replace-regexp-in-string
3012 "%b" (shell-quote-argument base-name)
3013 (replace-regexp-in-string
3014 "%f" (shell-quote-argument full-name)
3015 (replace-regexp-in-string
3016 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3017 outbuf))
3018 org-latex-pdf-process)
3019 ;; Collect standard errors from output buffer.
3020 (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
3021 (t (error "No valid command to process to PDF")))
3022 (let ((pdffile (concat out-dir base-name ".pdf")))
3023 ;; Check for process failure. Provide collected errors if
3024 ;; possible.
3025 (if (not (file-exists-p pdffile))
3026 (error (concat (format "PDF file %s wasn't produced" pdffile)
3027 (when errors (concat ": " errors))))
3028 ;; Else remove log files, when specified, and signal end of
3029 ;; process to user, along with any error encountered.
3030 (when (and (not snippet) org-latex-remove-logfiles)
3031 (dolist (file (directory-files
3032 out-dir t
3033 (concat (regexp-quote base-name)
3034 "\\(?:\\.[0-9]+\\)?"
3035 "\\."
3036 (regexp-opt org-latex-logfiles-extensions))))
3037 (delete-file file)))
3038 (message (concat "Process completed"
3039 (if (not errors) "."
3040 (concat " with errors: " errors)))))
3041 ;; Return output file name.
3042 pdffile))))
3044 (defun org-latex--collect-errors (buffer)
3045 "Collect some kind of errors from \"pdflatex\" command output.
3047 BUFFER is the buffer containing output.
3049 Return collected error types as a string, or nil if there was
3050 none."
3051 (with-current-buffer buffer
3052 (save-excursion
3053 (goto-char (point-max))
3054 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3055 (let ((case-fold-search t)
3056 (errors ""))
3057 (dolist (latex-error org-latex-known-errors)
3058 (when (save-excursion (re-search-forward (car latex-error) nil t))
3059 (setq errors (concat errors " " (cdr latex-error)))))
3060 (and (org-string-nw-p errors) (org-trim errors)))))))
3062 ;;;###autoload
3063 (defun org-latex-publish-to-latex (plist filename pub-dir)
3064 "Publish an Org file to LaTeX.
3066 FILENAME is the filename of the Org file to be published. PLIST
3067 is the property list for the given project. PUB-DIR is the
3068 publishing directory.
3070 Return output file name."
3071 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3073 ;;;###autoload
3074 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3075 "Publish an Org file to PDF (via LaTeX).
3077 FILENAME is the filename of the Org file to be published. PLIST
3078 is the property list for the given project. PUB-DIR is the
3079 publishing directory.
3081 Return output file name."
3082 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3083 ;; in working directory and then moved to publishing directory.
3084 (org-publish-attachment
3085 plist
3086 (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
3087 pub-dir))
3090 (provide 'ox-latex)
3092 ;; Local variables:
3093 ;; generated-autoload-file: "org-loaddefs.el"
3094 ;; End:
3096 ;;; ox-latex.el ends here