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