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