ox-latex: Allow hyperref and footnotemark in items description tags
[org-mode/org-tableheadings.git] / lisp / ox-latex.el
blob5bb746d7009b897afd5215fccebd2f8a51ab4692
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 (paragraph . org-latex-paragraph)
67 (plain-list . org-latex-plain-list)
68 (plain-text . org-latex-plain-text)
69 (planning . org-latex-planning)
70 (property-drawer . (lambda (&rest args) ""))
71 (quote-block . org-latex-quote-block)
72 (quote-section . org-latex-quote-section)
73 (radio-target . org-latex-radio-target)
74 (section . org-latex-section)
75 (special-block . org-latex-special-block)
76 (src-block . org-latex-src-block)
77 (statistics-cookie . org-latex-statistics-cookie)
78 (strike-through . org-latex-strike-through)
79 (subscript . org-latex-subscript)
80 (superscript . org-latex-superscript)
81 (table . org-latex-table)
82 (table-cell . org-latex-table-cell)
83 (table-row . org-latex-table-row)
84 (target . org-latex-target)
85 (template . org-latex-template)
86 (timestamp . org-latex-timestamp)
87 (underline . org-latex-underline)
88 (verbatim . org-latex-verbatim)
89 (verse-block . org-latex-verse-block))
90 :export-block '("LATEX" "TEX")
91 :menu-entry
92 '(?l "Export to LaTeX"
93 ((?L "As LaTeX buffer" org-latex-export-as-latex)
94 (?l "As LaTeX file" org-latex-export-to-latex)
95 (?p "As PDF file" org-latex-export-to-pdf)
96 (?o "As PDF file and open"
97 (lambda (a s v b)
98 (if a (org-latex-export-to-pdf t s v b)
99 (org-open-file (org-latex-export-to-pdf nil s v b)))))))
100 :options-alist '((:latex-class "LATEX_CLASS" nil org-latex-default-class t)
101 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
102 (:latex-header "LATEX_HEADER" nil nil newline)
103 (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
104 (:latex-hyperref-p nil "texht" org-latex-with-hyperref t)
105 ;; Redefine regular options.
106 (:date "DATE" nil "\\today" t)))
110 ;;; Internal Variables
112 (defconst org-latex-babel-language-alist
113 '(("af" . "afrikaans")
114 ("bg" . "bulgarian")
115 ("bt-br" . "brazilian")
116 ("ca" . "catalan")
117 ("cs" . "czech")
118 ("cy" . "welsh")
119 ("da" . "danish")
120 ("de" . "germanb")
121 ("de-at" . "naustrian")
122 ("de-de" . "ngerman")
123 ("el" . "greek")
124 ("en" . "english")
125 ("en-au" . "australian")
126 ("en-ca" . "canadian")
127 ("en-gb" . "british")
128 ("en-ie" . "irish")
129 ("en-nz" . "newzealand")
130 ("en-us" . "american")
131 ("es" . "spanish")
132 ("et" . "estonian")
133 ("eu" . "basque")
134 ("fi" . "finnish")
135 ("fr" . "frenchb")
136 ("fr-ca" . "canadien")
137 ("gl" . "galician")
138 ("hr" . "croatian")
139 ("hu" . "hungarian")
140 ("id" . "indonesian")
141 ("is" . "icelandic")
142 ("it" . "italian")
143 ("la" . "latin")
144 ("ms" . "malay")
145 ("nl" . "dutch")
146 ("no-no" . "nynorsk")
147 ("pl" . "polish")
148 ("pt" . "portuguese")
149 ("ro" . "romanian")
150 ("ru" . "russian")
151 ("sa" . "sanskrit")
152 ("sb" . "uppersorbian")
153 ("sk" . "slovak")
154 ("sl" . "slovene")
155 ("sq" . "albanian")
156 ("sr" . "serbian")
157 ("sv" . "swedish")
158 ("ta" . "tamil")
159 ("tr" . "turkish")
160 ("uk" . "ukrainian"))
161 "Alist between language code and corresponding Babel option.")
163 (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
164 ("qbordermatrix" . "\\cr")
165 ("kbordermatrix" . "\\\\"))
166 "Alist between matrix macros and their row ending.")
170 ;;; User Configurable Variables
172 (defgroup org-export-latex nil
173 "Options for exporting Org mode files to LaTeX."
174 :tag "Org Export LaTeX"
175 :group 'org-export)
178 ;;;; Preamble
180 (defcustom org-latex-default-class "article"
181 "The default LaTeX class."
182 :group 'org-export-latex
183 :type '(string :tag "LaTeX class"))
185 (defcustom org-latex-classes
186 '(("article"
187 "\\documentclass[11pt]{article}"
188 ("\\section{%s}" . "\\section*{%s}")
189 ("\\subsection{%s}" . "\\subsection*{%s}")
190 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
191 ("\\paragraph{%s}" . "\\paragraph*{%s}")
192 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
193 ("report"
194 "\\documentclass[11pt]{report}"
195 ("\\part{%s}" . "\\part*{%s}")
196 ("\\chapter{%s}" . "\\chapter*{%s}")
197 ("\\section{%s}" . "\\section*{%s}")
198 ("\\subsection{%s}" . "\\subsection*{%s}")
199 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
200 ("book"
201 "\\documentclass[11pt]{book}"
202 ("\\part{%s}" . "\\part*{%s}")
203 ("\\chapter{%s}" . "\\chapter*{%s}")
204 ("\\section{%s}" . "\\section*{%s}")
205 ("\\subsection{%s}" . "\\subsection*{%s}")
206 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
207 "Alist of LaTeX classes and associated header and structure.
208 If #+LATEX_CLASS is set in the buffer, use its value and the
209 associated information. Here is the structure of each cell:
211 \(class-name
212 header-string
213 \(numbered-section . unnumbered-section)
214 ...)
216 The header string
217 -----------------
219 The HEADER-STRING is the header that will be inserted into the
220 LaTeX file. It should contain the \\documentclass macro, and
221 anything else that is needed for this setup. To this header, the
222 following commands will be added:
224 - Calls to \\usepackage for all packages mentioned in the
225 variables `org-latex-default-packages-alist' and
226 `org-latex-packages-alist'. Thus, your header definitions
227 should avoid to also request these packages.
229 - Lines specified via \"#+LATEX_HEADER:\" and
230 \"#+LATEX_HEADER_EXTRA:\" keywords.
232 If you need more control about the sequence in which the header
233 is built up, or if you want to exclude one of these building
234 blocks for a particular class, you can use the following
235 macro-like placeholders.
237 [DEFAULT-PACKAGES] \\usepackage statements for default packages
238 [NO-DEFAULT-PACKAGES] do not include any of the default packages
239 [PACKAGES] \\usepackage statements for packages
240 [NO-PACKAGES] do not include the packages
241 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
242 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
244 So a header like
246 \\documentclass{article}
247 [NO-DEFAULT-PACKAGES]
248 [EXTRA]
249 \\providecommand{\\alert}[1]{\\textbf{#1}}
250 [PACKAGES]
252 will omit the default packages, and will include the
253 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
254 to \\providecommand, and then place \\usepackage commands based
255 on the content of `org-latex-packages-alist'.
257 If your header, `org-latex-default-packages-alist' or
258 `org-latex-packages-alist' inserts
259 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
260 replaced with a coding system derived from
261 `buffer-file-coding-system'. See also the variable
262 `org-latex-inputenc-alist' for a way to influence this mechanism.
264 The sectioning structure
265 ------------------------
267 The sectioning structure of the class is given by the elements
268 following the header string. For each sectioning level, a number
269 of strings is specified. A %s formatter is mandatory in each
270 section string and will be replaced by the title of the section.
272 Instead of a cons cell (numbered . unnumbered), you can also
273 provide a list of 2 or 4 elements,
275 \(numbered-open numbered-close)
279 \(numbered-open numbered-close unnumbered-open unnumbered-close)
281 providing opening and closing strings for a LaTeX environment
282 that should represent the document section. The opening clause
283 should have a %s to represent the section title.
285 Instead of a list of sectioning commands, you can also specify
286 a function name. That function will be called with two
287 parameters, the (reduced) level of the headline, and a predicate
288 non-nil when the headline should be numbered. It must return
289 a format string in which the section title will be added."
290 :group 'org-export-latex
291 :type '(repeat
292 (list (string :tag "LaTeX class")
293 (string :tag "LaTeX header")
294 (repeat :tag "Levels" :inline t
295 (choice
296 (cons :tag "Heading"
297 (string :tag " numbered")
298 (string :tag "unnumbered"))
299 (list :tag "Environment"
300 (string :tag "Opening (numbered)")
301 (string :tag "Closing (numbered)")
302 (string :tag "Opening (unnumbered)")
303 (string :tag "Closing (unnumbered)"))
304 (function :tag "Hook computing sectioning"))))))
306 (defcustom org-latex-inputenc-alist nil
307 "Alist of inputenc coding system names, and what should really be used.
308 For example, adding an entry
310 (\"utf8\" . \"utf8x\")
312 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
313 are written as utf8 files."
314 :group 'org-export-latex
315 :type '(repeat
316 (cons
317 (string :tag "Derived from buffer")
318 (string :tag "Use this instead"))))
320 (defcustom org-latex-title-command "\\maketitle"
321 "The command used to insert the title just after \\begin{document}.
322 If this string contains the formatting specification \"%s\" then
323 it will be used as a formatting string, passing the title as an
324 argument."
325 :group 'org-export-latex
326 :type 'string)
328 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
329 "LaTeX command to set the table of contents, list of figures, etc.
330 This command only applies to the table of contents generated with
331 the toc:nil option, not to those generated with #+TOC keyword."
332 :group 'org-export-latex
333 :type 'string)
335 (defcustom org-latex-with-hyperref t
336 "Toggle insertion of \\hypersetup{...} in the preamble."
337 :group 'org-export-latex
338 :type 'boolean)
341 ;;;; Headline
343 (defcustom org-latex-format-headline-function
344 'org-latex-format-headline-default-function
345 "Function for formatting the headline's text.
347 This function will be called with 5 arguments:
348 TODO the todo keyword (string or nil).
349 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
350 PRIORITY the priority of the headline (integer or nil)
351 TEXT the main headline text (string).
352 TAGS the tags as a list of strings (list of strings or nil).
354 The function result will be used in the section format string.
356 Use `org-latex-format-headline-default-function' by default,
357 which format headlines like for Org version prior to 8.0."
358 :group 'org-export-latex
359 :version "24.4"
360 :package-version '(Org . "8.0")
361 :type 'function)
364 ;;;; Footnotes
366 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
367 "Text used to separate footnotes."
368 :group 'org-export-latex
369 :type 'string)
372 ;;;; Timestamps
374 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
375 "A printf format string to be applied to active timestamps."
376 :group 'org-export-latex
377 :type 'string)
379 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
380 "A printf format string to be applied to inactive timestamps."
381 :group 'org-export-latex
382 :type 'string)
384 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
385 "A printf format string to be applied to diary timestamps."
386 :group 'org-export-latex
387 :type 'string)
390 ;;;; Links
392 (defcustom org-latex-image-default-option ""
393 "Default option for images."
394 :group 'org-export-latex
395 :version "24.4"
396 :package-version '(Org . "8.0")
397 :type 'string)
399 (defcustom org-latex-image-default-width ".9\\linewidth"
400 "Default width for images.
401 This value will not be used if a height is provided."
402 :group 'org-export-latex
403 :version "24.4"
404 :package-version '(Org . "8.0")
405 :type 'string)
407 (defcustom org-latex-image-default-height ""
408 "Default height for images.
409 This value will not be used if a width is provided, or if the
410 image is wrapped within a \"figure\" or \"wrapfigure\"
411 environment."
412 :group 'org-export-latex
413 :version "24.4"
414 :package-version '(Org . "8.0")
415 :type 'string)
417 (defcustom org-latex-default-figure-position "htb"
418 "Default position for latex figures."
419 :group 'org-export-latex
420 :type 'string)
422 (defcustom org-latex-inline-image-rules
423 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\)\\'"))
424 "Rules characterizing image files that can be inlined into LaTeX.
426 A rule consists in an association whose key is the type of link
427 to consider, and value is a regexp that will be matched against
428 link's path.
430 Note that, by default, the image extension *actually* allowed
431 depend on the way the LaTeX file is processed. When used with
432 pdflatex, pdf, jpg and png images are OK. When processing
433 through dvi to Postscript, only ps and eps are allowed. The
434 default we use here encompasses both."
435 :group 'org-export-latex
436 :version "24.4"
437 :package-version '(Org . "8.0")
438 :type '(alist :key-type (string :tag "Type")
439 :value-type (regexp :tag "Path")))
441 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
442 "Format string for links with unknown path type."
443 :group 'org-export-latex
444 :type 'string)
447 ;;;; Tables
449 (defcustom org-latex-default-table-environment "tabular"
450 "Default environment used to build tables."
451 :group 'org-export-latex
452 :version "24.4"
453 :package-version '(Org . "8.0")
454 :type 'string)
456 (defcustom org-latex-default-table-mode 'table
457 "Default mode for tables.
459 Value can be a symbol among:
461 `table' Regular LaTeX table.
463 `math' In this mode, every cell is considered as being in math
464 mode and the complete table will be wrapped within a math
465 environment. It is particularly useful to write matrices.
467 `inline-math' This mode is almost the same as `math', but the
468 math environment will be inlined.
470 `verbatim' The table is exported as it appears in the Org
471 buffer, within a verbatim environment.
473 This value can be overridden locally with, i.e. \":mode math\" in
474 LaTeX attributes.
476 When modifying this variable, it may be useful to change
477 `org-latex-default-table-environment' accordingly."
478 :group 'org-export-latex
479 :version "24.4"
480 :package-version '(Org . "8.0")
481 :type '(choice (const :tag "Table" table)
482 (const :tag "Matrix" math)
483 (const :tag "Inline matrix" inline-math)
484 (const :tag "Verbatim" verbatim)))
486 (defcustom org-latex-tables-centered t
487 "When non-nil, tables are exported in a center environment."
488 :group 'org-export-latex
489 :type 'boolean)
491 (defcustom org-latex-tables-booktabs nil
492 "When non-nil, display tables in a formal \"booktabs\" style.
493 This option assumes that the \"booktabs\" package is properly
494 loaded in the header of the document. This value can be ignored
495 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
496 attributes."
497 :group 'org-export-latex
498 :version "24.4"
499 :package-version '(Org . "8.0")
500 :type 'boolean)
502 (defcustom org-latex-table-caption-above t
503 "When non-nil, place caption string at the beginning of the table.
504 Otherwise, place it near the end."
505 :group 'org-export-latex
506 :type 'boolean)
508 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
509 "Format string to display numbers in scientific notation.
510 The format should have \"%s\" twice, for mantissa and exponent
511 \(i.e., \"%s\\\\times10^{%s}\").
513 When nil, no transformation is made."
514 :group 'org-export-latex
515 :version "24.4"
516 :package-version '(Org . "8.0")
517 :type '(choice
518 (string :tag "Format string")
519 (const :tag "No formatting")))
522 ;;;; Text markup
524 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
525 (code . verb)
526 (italic . "\\emph{%s}")
527 (strike-through . "\\sout{%s}")
528 (underline . "\\uline{%s}")
529 (verbatim . protectedtexttt))
530 "Alist of LaTeX expressions to convert text markup.
532 The key must be a symbol among `bold', `code', `italic',
533 `strike-through', `underline' and `verbatim'. The value is
534 a formatting string to wrap fontified text with.
536 Value can also be set to the following symbols: `verb' and
537 `protectedtexttt'. For the former, Org will use \"\\verb\" to
538 create a format string and select a delimiter character that
539 isn't in the string. For the latter, Org will use \"\\texttt\"
540 to typeset and try to protect special characters.
542 If no association can be found for a given markup, text will be
543 returned as-is."
544 :group 'org-export-latex
545 :type 'alist
546 :options '(bold code italic strike-through underline verbatim))
549 ;;;; Drawers
551 (defcustom org-latex-format-drawer-function nil
552 "Function called to format a drawer in LaTeX code.
554 The function must accept two parameters:
555 NAME the drawer name, like \"LOGBOOK\"
556 CONTENTS the contents of the drawer.
558 The function should return the string to be exported.
560 For example, the variable could be set to the following function
561 in order to mimic default behaviour:
563 \(defun org-latex-format-drawer-default \(name contents\)
564 \"Format a drawer element for LaTeX export.\"
565 contents\)"
566 :group 'org-export-latex
567 :type 'function)
570 ;;;; Inlinetasks
572 (defcustom org-latex-format-inlinetask-function nil
573 "Function called to format an inlinetask in LaTeX code.
575 The function must accept six parameters:
576 TODO the todo keyword, as a string
577 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
578 PRIORITY the inlinetask priority, as a string
579 NAME the inlinetask name, as a string.
580 TAGS the inlinetask tags, as a list of strings.
581 CONTENTS the contents of the inlinetask, as a string.
583 The function should return the string to be exported.
585 For example, the variable could be set to the following function
586 in order to mimic default behaviour:
588 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
589 \"Format an inline task element for LaTeX export.\"
590 \(let ((full-title
591 \(concat
592 \(when todo
593 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
594 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
595 title
596 \(when tags
597 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
598 \(mapconcat 'identity tags \":\")))))
599 \(format (concat \"\\\\begin{center}\\n\"
600 \"\\\\fbox{\\n\"
601 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
602 \"%s\\n\\n\"
603 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
604 \"%s\"
605 \"\\\\end{minipage}}\"
606 \"\\\\end{center}\")
607 full-title contents))"
608 :group 'org-export-latex
609 :type 'function)
612 ;; Src blocks
614 (defcustom org-latex-listings nil
615 "Non-nil means export source code using the listings package.
616 This package will fontify source code, possibly even with color.
617 If you want to use this, you also need to make LaTeX use the
618 listings package, and if you want to have color, the color
619 package. Just add these to `org-latex-packages-alist', for
620 example using customize, or with something like:
622 \(require 'ox-latex)
623 \(add-to-list 'org-latex-packages-alist '\(\"\" \"listings\"))
624 \(add-to-list 'org-latex-packages-alist '\(\"\" \"color\"))
626 Alternatively,
628 \(setq org-latex-listings 'minted)
630 causes source code to be exported using the minted package as
631 opposed to listings. If you want to use minted, you need to add
632 the minted package to `org-latex-packages-alist', for example
633 using customize, or with
635 \(require 'ox-latex)
636 \(add-to-list 'org-latex-packages-alist '\(\"\" \"minted\"))
638 In addition, it is necessary to install pygments
639 \(http://pygments.org), and to configure the variable
640 `org-latex-pdf-process' so that the -shell-escape option is
641 passed to pdflatex."
642 :group 'org-export-latex
643 :type '(choice
644 (const :tag "Use listings" t)
645 (const :tag "Use minted" 'minted)
646 (const :tag "Export verbatim" nil)))
648 (defcustom org-latex-listings-langs
649 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
650 (c "C") (cc "C++")
651 (fortran "fortran")
652 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
653 (html "HTML") (xml "XML")
654 (tex "TeX") (latex "TeX")
655 (shell-script "bash")
656 (gnuplot "Gnuplot")
657 (ocaml "Caml") (caml "Caml")
658 (sql "SQL") (sqlite "sql"))
659 "Alist mapping languages to their listing language counterpart.
660 The key is a symbol, the major mode symbol without the \"-mode\".
661 The value is the string that should be inserted as the language
662 parameter for the listings package. If the mode name and the
663 listings name are the same, the language does not need an entry
664 in this list - but it does not hurt if it is present."
665 :group 'org-export-latex
666 :type '(repeat
667 (list
668 (symbol :tag "Major mode ")
669 (string :tag "Listings language"))))
671 (defcustom org-latex-listings-options nil
672 "Association list of options for the latex listings package.
674 These options are supplied as a comma-separated list to the
675 \\lstset command. Each element of the association list should be
676 a list containing two strings: the name of the option, and the
677 value. For example,
679 (setq org-latex-listings-options
680 '((\"basicstyle\" \"\\small\")
681 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
683 will typeset the code in a small size font with underlined, bold
684 black keywords.
686 Note that the same options will be applied to blocks of all
687 languages."
688 :group 'org-export-latex
689 :type '(repeat
690 (list
691 (string :tag "Listings option name ")
692 (string :tag "Listings option value"))))
694 (defcustom org-latex-minted-langs
695 '((emacs-lisp "common-lisp")
696 (cc "c++")
697 (cperl "perl")
698 (shell-script "bash")
699 (caml "ocaml"))
700 "Alist mapping languages to their minted language counterpart.
701 The key is a symbol, the major mode symbol without the \"-mode\".
702 The value is the string that should be inserted as the language
703 parameter for the minted package. If the mode name and the
704 listings name are the same, the language does not need an entry
705 in this list - but it does not hurt if it is present.
707 Note that minted uses all lower case for language identifiers,
708 and that the full list of language identifiers can be obtained
709 with:
711 pygmentize -L lexers"
712 :group 'org-export-latex
713 :type '(repeat
714 (list
715 (symbol :tag "Major mode ")
716 (string :tag "Minted language"))))
718 (defcustom org-latex-minted-options nil
719 "Association list of options for the latex minted package.
721 These options are supplied within square brackets in
722 \\begin{minted} environments. Each element of the alist should
723 be a list containing two strings: the name of the option, and the
724 value. For example,
726 \(setq org-latex-minted-options
727 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
729 will result in src blocks being exported with
731 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
733 as the start of the minted environment. Note that the same
734 options will be applied to blocks of all languages."
735 :group 'org-export-latex
736 :type '(repeat
737 (list
738 (string :tag "Minted option name ")
739 (string :tag "Minted option value"))))
741 (defcustom org-latex-long-listings nil
742 "When non-nil no listing will be wrapped within a float.
744 Removing floats may break some functionalities. For example, it
745 will be impossible to use cross-references to listings when using
746 `minted' set-up when this variable is non-nil.
748 This value can be locally ignored with \":long-listing t\" and
749 \":long-listing nil\" LaTeX attributes."
750 :group 'org-export-latex
751 :version "24.4"
752 :package-version '(Org . "8.0")
753 :type 'boolean)
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. Org does offer it as one
797 of the customize options.
799 Alternatively, this may be a Lisp function that does the
800 processing, so you could use this to apply the machinery of
801 AUCTeX or the Emacs LaTeX mode. This function should accept the
802 file name as its single argument."
803 :group 'org-export-pdf
804 :type '(choice
805 (repeat :tag "Shell command sequence"
806 (string :tag "Shell command"))
807 (const :tag "2 runs of pdflatex"
808 ("pdflatex -interaction nonstopmode -output-directory %o %f"
809 "pdflatex -interaction nonstopmode -output-directory %o %f"))
810 (const :tag "3 runs of pdflatex"
811 ("pdflatex -interaction nonstopmode -output-directory %o %f"
812 "pdflatex -interaction nonstopmode -output-directory %o %f"
813 "pdflatex -interaction nonstopmode -output-directory %o %f"))
814 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
815 ("pdflatex -interaction nonstopmode -output-directory %o %f"
816 "bibtex %b"
817 "pdflatex -interaction nonstopmode -output-directory %o %f"
818 "pdflatex -interaction nonstopmode -output-directory %o %f"))
819 (const :tag "2 runs of xelatex"
820 ("xelatex -interaction nonstopmode -output-directory %o %f"
821 "xelatex -interaction nonstopmode -output-directory %o %f"))
822 (const :tag "3 runs of xelatex"
823 ("xelatex -interaction nonstopmode -output-directory %o %f"
824 "xelatex -interaction nonstopmode -output-directory %o %f"
825 "xelatex -interaction nonstopmode -output-directory %o %f"))
826 (const :tag "xelatex,bibtex,xelatex,xelatex"
827 ("xelatex -interaction nonstopmode -output-directory %o %f"
828 "bibtex %b"
829 "xelatex -interaction nonstopmode -output-directory %o %f"
830 "xelatex -interaction nonstopmode -output-directory %o %f"))
831 (const :tag "texi2dvi"
832 ("texi2dvi -p -b -V %f"))
833 (const :tag "rubber"
834 ("rubber -d --into %o %f"))
835 (function)))
837 (defcustom org-latex-logfiles-extensions
838 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
839 "The list of file extensions to consider as LaTeX logfiles.
840 The logfiles will be remove if `org-latex-remove-logfiles' is
841 non-nil."
842 :group 'org-export-latex
843 :type '(repeat (string :tag "Extension")))
845 (defcustom org-latex-remove-logfiles t
846 "Non-nil means remove the logfiles produced by PDF production.
847 By default, logfiles are files with these extensions: .aux, .idx,
848 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
849 logfiles to remove, set `org-latex-logfiles-extensions'."
850 :group 'org-export-latex
851 :type 'boolean)
853 (defcustom org-latex-known-errors
854 '(("Reference.*?undefined" . "[undefined reference]")
855 ("Citation.*?undefined" . "[undefined citation]")
856 ("Undefined control sequence" . "[undefined control sequence]")
857 ("^! LaTeX.*?Error" . "[LaTeX error]")
858 ("^! Package.*?Error" . "[package error]")
859 ("Runaway argument" . "Runaway argument"))
860 "Alist of regular expressions and associated messages for the user.
861 The regular expressions are used to find possible errors in the
862 log of a latex-run."
863 :group 'org-export-latex
864 :version "24.4"
865 :package-version '(Org . "8.0")
866 :type '(repeat
867 (cons
868 (string :tag "Regexp")
869 (string :tag "Message"))))
873 ;;; Internal Functions
875 (defun org-latex--caption/label-string (element info)
876 "Return caption and label LaTeX string for ELEMENT.
878 INFO is a plist holding contextual information. If there's no
879 caption nor label, return the empty string.
881 For non-floats, see `org-latex--wrap-label'."
882 (let* ((label (org-element-property :name element))
883 (label-str (if (not (org-string-nw-p label)) ""
884 (format "\\label{%s}"
885 (org-export-solidify-link-text label))))
886 (main (org-export-get-caption element))
887 (short (org-export-get-caption element t)))
888 (cond
889 ((and (not main) (equal label-str "")) "")
890 ((not main) (concat label-str "\n"))
891 ;; Option caption format with short name.
892 (short (format "\\caption[%s]{%s%s}\n"
893 (org-export-data short info)
894 label-str
895 (org-export-data main info)))
896 ;; Standard caption format.
897 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
899 (defun org-latex-guess-inputenc (header)
900 "Set the coding system in inputenc to what the buffer is.
902 HEADER is the LaTeX header string. This function only applies
903 when specified inputenc option is \"AUTO\".
905 Return the new header, as a string."
906 (let* ((cs (or (ignore-errors
907 (latexenc-coding-system-to-inputenc
908 (or org-export-coding-system buffer-file-coding-system)))
909 "utf8")))
910 (if (not cs) header
911 ;; First translate if that is requested.
912 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
913 ;; Then find the \usepackage statement and replace the option.
914 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
915 cs header t nil 1))))
917 (defun org-latex-guess-babel-language (header info)
918 "Set Babel's language according to LANGUAGE keyword.
920 HEADER is the LaTeX header string. INFO is the plist used as
921 a communication channel.
923 Insertion of guessed language only happens when Babel package has
924 explicitly been loaded. Then it is added to the rest of
925 package's options.
927 Return the new header."
928 (let ((language-code (plist-get info :language)))
929 ;; If no language is set or Babel package is not loaded, return
930 ;; HEADER as-is.
931 (if (or (not (stringp language-code))
932 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
933 header
934 (let ((options (save-match-data
935 (org-split-string (match-string 1 header) ",")))
936 (language (cdr (assoc language-code
937 org-latex-babel-language-alist))))
938 ;; If LANGUAGE is already loaded, return header. Otherwise,
939 ;; append LANGUAGE to other options.
940 (if (member language options) header
941 (replace-match (mapconcat 'identity
942 (append options (list language))
943 ",")
944 nil nil header 1))))))
946 (defun org-latex--find-verb-separator (s)
947 "Return a character not used in string S.
948 This is used to choose a separator for constructs like \\verb."
949 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
950 (loop for c across ll
951 when (not (string-match (regexp-quote (char-to-string c)) s))
952 return (char-to-string c))))
954 (defun org-latex--make-option-string (options)
955 "Return a comma separated string of keywords and values.
956 OPTIONS is an alist where the key is the options keyword as
957 a string, and the value a list containing the keyword value, or
958 nil."
959 (mapconcat (lambda (pair)
960 (concat (first pair)
961 (when (> (length (second pair)) 0)
962 (concat "=" (second pair)))))
963 options
964 ","))
966 (defun org-latex--wrap-label (element output)
967 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
968 This function shouldn't be used for floats. See
969 `org-latex--caption/label-string'."
970 (let ((label (org-element-property :name element)))
971 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
972 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
973 output))))
975 (defun org-latex--text-markup (text markup)
976 "Format TEXT depending on MARKUP text markup.
977 See `org-latex-text-markup-alist' for details."
978 (let ((fmt (cdr (assq markup org-latex-text-markup-alist))))
979 (cond
980 ;; No format string: Return raw text.
981 ((not fmt) text)
982 ;; Handle the `verb' special case: Find and appropriate separator
983 ;; and use "\\verb" command.
984 ((eq 'verb fmt)
985 (let ((separator (org-latex--find-verb-separator text)))
986 (concat "\\verb" separator text separator)))
987 ;; Handle the `protectedtexttt' special case: Protect some
988 ;; special chars and use "\texttt{%s}" format string.
989 ((eq 'protectedtexttt fmt)
990 (let ((start 0)
991 (trans '(("\\" . "\\textbackslash{}")
992 ("~" . "\\textasciitilde{}")
993 ("^" . "\\textasciicircum{}")))
994 (rtn "")
995 char)
996 (while (string-match "[\\{}$%&_#~^]" text)
997 (setq char (match-string 0 text))
998 (if (> (match-beginning 0) 0)
999 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1000 (setq text (substring text (1+ (match-beginning 0))))
1001 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1002 rtn (concat rtn char)))
1003 (setq text (concat rtn text)
1004 fmt "\\texttt{%s}")
1005 (while (string-match "--" text)
1006 (setq text (replace-match "-{}-" t t text)))
1007 (format fmt text)))
1008 ;; Else use format string.
1009 (t (format fmt text)))))
1011 (defun org-latex--delayed-footnotes-definitions (element info)
1012 "Return footnotes definitions in ELEMENT as a string.
1014 INFO is a plist used as a communication channel.
1016 Footnotes definitions are returned within \"\\footnotetxt{}\"
1017 commands.
1019 This function is used within constructs that don't support
1020 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1021 \"\\footnotemark\" is used within the construct and the function
1022 just outside of it."
1023 (mapconcat
1024 (lambda (ref)
1025 (format
1026 "\\footnotetext[%s]{%s}"
1027 (org-export-get-footnote-number ref info)
1028 (org-trim
1029 (org-export-data
1030 (org-export-get-footnote-definition ref info) info))))
1031 ;; Find every footnote reference in ELEMENT.
1032 (let* (all-refs
1033 search-refs ; For byte-compiler.
1034 (search-refs
1035 (function
1036 (lambda (data)
1037 ;; Return a list of all footnote references never seen
1038 ;; before in DATA.
1039 (org-element-map data 'footnote-reference
1040 (lambda (ref)
1041 (when (org-export-footnote-first-reference-p ref info)
1042 (push ref all-refs)
1043 (when (eq (org-element-property :type ref) 'standard)
1044 (funcall search-refs
1045 (org-export-get-footnote-definition ref info)))))
1046 info)
1047 (reverse all-refs)))))
1048 (funcall search-refs element))
1049 ""))
1053 ;;; Template
1055 (defun org-latex-template (contents info)
1056 "Return complete document string after LaTeX conversion.
1057 CONTENTS is the transcoded contents string. INFO is a plist
1058 holding export options."
1059 (let ((title (org-export-data (plist-get info :title) info)))
1060 (concat
1061 ;; Time-stamp.
1062 (and (plist-get info :time-stamp-file)
1063 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1064 ;; Document class and packages.
1065 (let ((class (plist-get info :latex-class))
1066 (class-options (plist-get info :latex-class-options)))
1067 (org-element-normalize-string
1068 (let* ((header (nth 1 (assoc class org-latex-classes)))
1069 (document-class-string
1070 (and (stringp header)
1071 (if (not class-options) header
1072 (replace-regexp-in-string
1073 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1074 class-options header t nil 1)))))
1075 (if (not document-class-string)
1076 (user-error "Unknown LaTeX class `%s'" class)
1077 (org-latex-guess-babel-language
1078 (org-latex-guess-inputenc
1079 (org-splice-latex-header
1080 document-class-string
1081 org-latex-default-packages-alist
1082 org-latex-packages-alist nil
1083 (concat (plist-get info :latex-header)
1084 (plist-get info :latex-header-extra))))
1085 info)))))
1086 ;; Possibly limit depth for headline numbering.
1087 (let ((sec-num (plist-get info :section-numbers)))
1088 (when (integerp sec-num)
1089 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1090 ;; Author.
1091 (let ((author (and (plist-get info :with-author)
1092 (let ((auth (plist-get info :author)))
1093 (and auth (org-export-data auth info)))))
1094 (email (and (plist-get info :with-email)
1095 (org-export-data (plist-get info :email) info))))
1096 (cond ((and author email (not (string= "" email)))
1097 (format "\\author{%s\\thanks{%s}}\n" author email))
1098 ((or author email) (format "\\author{%s}\n" (or author email)))))
1099 ;; Date.
1100 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1101 (format "\\date{%s}\n" (org-export-data date info)))
1102 ;; Title
1103 (format "\\title{%s}\n" title)
1104 ;; Hyperref options.
1105 (when (plist-get info :latex-hyperref-p)
1106 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1107 (or (plist-get info :keywords) "")
1108 (or (plist-get info :description) "")
1109 (if (not (plist-get info :with-creator)) ""
1110 (plist-get info :creator))))
1111 ;; Document start.
1112 "\\begin{document}\n\n"
1113 ;; Title command.
1114 (org-element-normalize-string
1115 (cond ((string= "" title) nil)
1116 ((not (stringp org-latex-title-command)) nil)
1117 ((string-match "\\(?:[^%]\\|^\\)%s"
1118 org-latex-title-command)
1119 (format org-latex-title-command title))
1120 (t org-latex-title-command)))
1121 ;; Table of contents.
1122 (let ((depth (plist-get info :with-toc)))
1123 (when depth
1124 (concat (when (wholenump depth)
1125 (format "\\setcounter{tocdepth}{%d}\n" depth))
1126 org-latex-toc-command)))
1127 ;; Document's body.
1128 contents
1129 ;; Creator.
1130 (let ((creator-info (plist-get info :with-creator)))
1131 (cond
1132 ((not creator-info) "")
1133 ((eq creator-info 'comment)
1134 (format "%% %s\n" (plist-get info :creator)))
1135 (t (concat (plist-get info :creator) "\n"))))
1136 ;; Document end.
1137 "\\end{document}")))
1141 ;;; Transcode Functions
1143 ;;;; Bold
1145 (defun org-latex-bold (bold contents info)
1146 "Transcode BOLD from Org to LaTeX.
1147 CONTENTS is the text with bold markup. INFO is a plist holding
1148 contextual information."
1149 (org-latex--text-markup contents 'bold))
1152 ;;;; Center Block
1154 (defun org-latex-center-block (center-block contents info)
1155 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1156 CONTENTS holds the contents of the center block. INFO is a plist
1157 holding contextual information."
1158 (org-latex--wrap-label
1159 center-block
1160 (format "\\begin{center}\n%s\\end{center}" contents)))
1163 ;;;; Clock
1165 (defun org-latex-clock (clock contents info)
1166 "Transcode a CLOCK element from Org to LaTeX.
1167 CONTENTS is nil. INFO is a plist holding contextual
1168 information."
1169 (concat
1170 "\\noindent"
1171 (format "\\textbf{%s} " org-clock-string)
1172 (format org-latex-inactive-timestamp-format
1173 (concat (org-translate-time
1174 (org-element-property :raw-value
1175 (org-element-property :value clock)))
1176 (let ((time (org-element-property :duration clock)))
1177 (and time (format " (%s)" time)))))
1178 "\\\\"))
1181 ;;;; Code
1183 (defun org-latex-code (code contents info)
1184 "Transcode a CODE object from Org to LaTeX.
1185 CONTENTS is nil. INFO is a plist used as a communication
1186 channel."
1187 (org-latex--text-markup (org-element-property :value code) 'code))
1190 ;;;; Drawer
1192 (defun org-latex-drawer (drawer contents info)
1193 "Transcode a DRAWER element from Org to LaTeX.
1194 CONTENTS holds the contents of the block. INFO is a plist
1195 holding contextual information."
1196 (let* ((name (org-element-property :drawer-name drawer))
1197 (output (if (functionp org-latex-format-drawer-function)
1198 (funcall org-latex-format-drawer-function
1199 name contents)
1200 ;; If there's no user defined function: simply
1201 ;; display contents of the drawer.
1202 contents)))
1203 (org-latex--wrap-label drawer output)))
1206 ;;;; Dynamic Block
1208 (defun org-latex-dynamic-block (dynamic-block contents info)
1209 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1210 CONTENTS holds the contents of the block. INFO is a plist
1211 holding contextual information. See `org-export-data'."
1212 (org-latex--wrap-label dynamic-block contents))
1215 ;;;; Entity
1217 (defun org-latex-entity (entity contents info)
1218 "Transcode an ENTITY object from Org to LaTeX.
1219 CONTENTS are the definition itself. INFO is a plist holding
1220 contextual information."
1221 (let ((ent (org-element-property :latex entity)))
1222 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1225 ;;;; Example Block
1227 (defun org-latex-example-block (example-block contents info)
1228 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1229 CONTENTS is nil. INFO is a plist holding contextual
1230 information."
1231 (when (org-string-nw-p (org-element-property :value example-block))
1232 (org-latex--wrap-label
1233 example-block
1234 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1235 (org-export-format-code-default example-block info)))))
1238 ;;;; Export Block
1240 (defun org-latex-export-block (export-block contents info)
1241 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1242 CONTENTS is nil. INFO is a plist holding contextual information."
1243 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1244 (org-remove-indentation (org-element-property :value export-block))))
1247 ;;;; Export Snippet
1249 (defun org-latex-export-snippet (export-snippet contents info)
1250 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1251 CONTENTS is nil. INFO is a plist holding contextual information."
1252 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1253 (org-element-property :value export-snippet)))
1256 ;;;; Fixed Width
1258 (defun org-latex-fixed-width (fixed-width contents info)
1259 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1260 CONTENTS is nil. INFO is a plist holding contextual information."
1261 (org-latex--wrap-label
1262 fixed-width
1263 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1264 (org-remove-indentation
1265 (org-element-property :value fixed-width)))))
1268 ;;;; Footnote Reference
1270 ;; Footnote reference export is handled by
1271 ;; `org-latex-footnote-reference'.
1273 ;; Internally, `org-latex--get-footnote-counter' is used to restore
1274 ;; the value of the LaTeX "footnote" counter after a jump due to
1275 ;; a reference to an already defined footnote. It is only needed in
1276 ;; item tags since the optional argument to \footnotemark is not
1277 ;; allowed there.
1279 (defun org-latex--get-footnote-counter (footnote-reference info)
1280 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1281 INFO is a plist used as a communication channel."
1282 ;; Find original counter value by counting number of footnote
1283 ;; references appearing for the first time before the current
1284 ;; footnote reference.
1285 (let* ((label (org-element-property :label footnote-reference))
1286 seen-refs
1287 search-ref ; For byte-compiler.
1288 (search-ref
1289 (function
1290 (lambda (data)
1291 ;; Search footnote references through DATA, filling
1292 ;; SEEN-REFS along the way.
1293 (org-element-map data 'footnote-reference
1294 (lambda (fn)
1295 (let ((fn-lbl (org-element-property :label fn)))
1296 (cond
1297 ;; Anonymous footnote match: return number.
1298 ((eq fn footnote-reference) (length seen-refs))
1299 ;; Anonymous footnote: it's always a new one.
1300 ;; Also, be sure to return nil from the `cond' so
1301 ;; `first-match' doesn't get us out of the loop.
1302 ((not fn-lbl) (push 'inline seen-refs) nil)
1303 ;; Label not seen so far: add it so SEEN-REFS.
1305 ;; Also search for subsequent references in
1306 ;; footnote definition so numbering follows
1307 ;; reading logic. Note that we don't care about
1308 ;; inline definitions, since `org-element-map'
1309 ;; already traverses them at the right time.
1310 ((not (member fn-lbl seen-refs))
1311 (push fn-lbl seen-refs)
1312 (funcall search-ref
1313 (org-export-get-footnote-definition fn info))))))
1314 ;; Don't enter footnote definitions since it will
1315 ;; happen when their first reference is found.
1316 info 'first-match 'footnote-definition)))))
1317 (funcall search-ref (plist-get info :parse-tree))))
1319 (defun org-latex-footnote-reference (footnote-reference contents info)
1320 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1321 CONTENTS is nil. INFO is a plist holding contextual information."
1322 (concat
1323 ;; Insert separator between two footnotes in a row.
1324 (let ((prev (org-export-get-previous-element footnote-reference info)))
1325 (when (eq (org-element-type prev) 'footnote-reference)
1326 org-latex-footnote-separator))
1327 (cond
1328 ;; Use \footnotemark if the footnote has already been defined.
1329 ((not (org-export-footnote-first-reference-p footnote-reference info))
1330 (format "\\footnotemark[%s]{}"
1331 (org-export-get-footnote-number footnote-reference info)))
1332 ;; Use \footnotemark if reference is within another footnote
1333 ;; reference, footnote definition or table cell.
1334 ((loop for parent in (org-export-get-genealogy footnote-reference)
1335 thereis (memq (org-element-type parent)
1336 '(footnote-reference footnote-definition table-cell)))
1337 "\\footnotemark")
1338 ;; Otherwise, define it with \footnote command.
1340 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1341 (concat
1342 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1343 ;; Retrieve all footnote references within the footnote and
1344 ;; add their definition after it, since LaTeX doesn't support
1345 ;; them inside.
1346 (org-latex--delayed-footnotes-definitions def info)))))))
1349 ;;;; Headline
1351 (defun org-latex-headline (headline contents info)
1352 "Transcode a HEADLINE element from Org to LaTeX.
1353 CONTENTS holds the contents of the headline. INFO is a plist
1354 holding contextual information."
1355 (unless (org-element-property :footnote-section-p headline)
1356 (let* ((class (plist-get info :latex-class))
1357 (level (org-export-get-relative-level headline info))
1358 (numberedp (org-export-numbered-headline-p headline info))
1359 (class-sectionning (assoc class org-latex-classes))
1360 ;; Section formatting will set two placeholders: one for
1361 ;; the title and the other for the contents.
1362 (section-fmt
1363 (let ((sec (if (functionp (nth 2 class-sectionning))
1364 (funcall (nth 2 class-sectionning) level numberedp)
1365 (nth (1+ level) class-sectionning))))
1366 (cond
1367 ;; No section available for that LEVEL.
1368 ((not sec) nil)
1369 ;; Section format directly returned by a function. Add
1370 ;; placeholder for contents.
1371 ((stringp sec) (concat sec "\n%s"))
1372 ;; (numbered-section . unnumbered-section)
1373 ((not (consp (cdr sec)))
1374 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1375 ;; (numbered-open numbered-close)
1376 ((= (length sec) 2)
1377 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1378 ;; (num-in num-out no-num-in no-num-out)
1379 ((= (length sec) 4)
1380 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1381 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1382 (text (org-export-data (org-element-property :title headline) info))
1383 (todo
1384 (and (plist-get info :with-todo-keywords)
1385 (let ((todo (org-element-property :todo-keyword headline)))
1386 (and todo (org-export-data todo info)))))
1387 (todo-type (and todo (org-element-property :todo-type headline)))
1388 (tags (and (plist-get info :with-tags)
1389 (org-export-get-tags headline info)))
1390 (priority (and (plist-get info :with-priority)
1391 (org-element-property :priority headline)))
1392 ;; Create the headline text along with a no-tag version.
1393 ;; The latter is required to remove tags from toc.
1394 (full-text (funcall org-latex-format-headline-function
1395 todo todo-type priority text tags))
1396 ;; Associate \label to the headline for internal links.
1397 (headline-label
1398 (format "\\label{sec-%s}\n"
1399 (mapconcat 'number-to-string
1400 (org-export-get-headline-number headline info)
1401 "-")))
1402 (pre-blanks
1403 (make-string (org-element-property :pre-blank headline) 10)))
1404 (if (or (not section-fmt) (org-export-low-level-p headline info))
1405 ;; This is a deep sub-tree: export it as a list item. Also
1406 ;; export as items headlines for which no section format has
1407 ;; been found.
1408 (let ((low-level-body
1409 (concat
1410 ;; If headline is the first sibling, start a list.
1411 (when (org-export-first-sibling-p headline info)
1412 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1413 ;; Itemize headline
1414 "\\item " full-text "\n" headline-label pre-blanks contents)))
1415 ;; If headline is not the last sibling simply return
1416 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1417 ;; any blank line.
1418 (if (not (org-export-last-sibling-p headline info)) low-level-body
1419 (replace-regexp-in-string
1420 "[ \t\n]*\\'"
1421 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1422 low-level-body)))
1423 ;; This is a standard headline. Export it as a section. Add
1424 ;; an alternative heading when possible, and when this is not
1425 ;; identical to the usual heading.
1426 (let ((opt-title
1427 (funcall org-latex-format-headline-function
1428 todo todo-type priority
1429 (org-export-data
1430 (org-export-get-alt-title headline info) info)
1431 (and (eq (plist-get info :with-tags) t) tags))))
1432 (if (and numberedp opt-title
1433 (not (equal opt-title full-text))
1434 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1435 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1436 ;; Replace square brackets with parenthesis
1437 ;; since square brackets are not supported in
1438 ;; optional arguments.
1439 (replace-regexp-in-string
1440 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1441 full-text
1442 (concat headline-label pre-blanks contents))
1443 ;; Impossible to add an alternative heading. Fallback to
1444 ;; regular sectioning format string.
1445 (format section-fmt full-text
1446 (concat headline-label pre-blanks contents))))))))
1448 (defun org-latex-format-headline-default-function
1449 (todo todo-type priority text tags)
1450 "Default format function for a headline.
1451 See `org-latex-format-headline-function' for details."
1452 (concat
1453 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1454 (and priority (format "\\framebox{\\#%c} " priority))
1455 text
1456 (and tags
1457 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
1460 ;;;; Horizontal Rule
1462 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1463 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1464 CONTENTS is nil. INFO is a plist holding contextual information."
1465 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1466 (prev (org-export-get-previous-element horizontal-rule info)))
1467 (concat
1468 ;; Make sure the rule doesn't start at the end of the current
1469 ;; line by separating it with a blank line from previous element.
1470 (when (and prev
1471 (let ((prev-blank (org-element-property :post-blank prev)))
1472 (or (not prev-blank) (zerop prev-blank))))
1473 "\n")
1474 (org-latex--wrap-label
1475 horizontal-rule
1476 (format "\\rule{%s}{%s}"
1477 (or (plist-get attr :width) "\\linewidth")
1478 (or (plist-get attr :thickness) "0.5pt"))))))
1481 ;;;; Inline Src Block
1483 (defun org-latex-inline-src-block (inline-src-block contents info)
1484 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1485 CONTENTS holds the contents of the item. INFO is a plist holding
1486 contextual information."
1487 (let* ((code (org-element-property :value inline-src-block))
1488 (separator (org-latex--find-verb-separator code)))
1489 (cond
1490 ;; Do not use a special package: transcode it verbatim.
1491 ((not org-latex-listings)
1492 (concat "\\verb" separator code separator))
1493 ;; Use minted package.
1494 ((eq org-latex-listings 'minted)
1495 (let* ((org-lang (org-element-property :language inline-src-block))
1496 (mint-lang (or (cadr (assq (intern org-lang)
1497 org-latex-minted-langs))
1498 org-lang))
1499 (options (org-latex--make-option-string
1500 org-latex-minted-options)))
1501 (concat (format "\\mint%s{%s}"
1502 (if (string= options "") "" (format "[%s]" options))
1503 mint-lang)
1504 separator code separator)))
1505 ;; Use listings package.
1507 ;; Maybe translate language's name.
1508 (let* ((org-lang (org-element-property :language inline-src-block))
1509 (lst-lang (or (cadr (assq (intern org-lang)
1510 org-latex-listings-langs))
1511 org-lang))
1512 (options (org-latex--make-option-string
1513 (append org-latex-listings-options
1514 `(("language" ,lst-lang))))))
1515 (concat (format "\\lstinline[%s]" options)
1516 separator code separator))))))
1519 ;;;; Inlinetask
1521 (defun org-latex-inlinetask (inlinetask contents info)
1522 "Transcode an INLINETASK element from Org to LaTeX.
1523 CONTENTS holds the contents of the block. INFO is a plist
1524 holding contextual information."
1525 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1526 (todo (and (plist-get info :with-todo-keywords)
1527 (let ((todo (org-element-property :todo-keyword inlinetask)))
1528 (and todo (org-export-data todo info)))))
1529 (todo-type (org-element-property :todo-type inlinetask))
1530 (tags (and (plist-get info :with-tags)
1531 (org-export-get-tags inlinetask info)))
1532 (priority (and (plist-get info :with-priority)
1533 (org-element-property :priority inlinetask))))
1534 ;; If `org-latex-format-inlinetask-function' is provided, call it
1535 ;; with appropriate arguments.
1536 (if (functionp org-latex-format-inlinetask-function)
1537 (funcall org-latex-format-inlinetask-function
1538 todo todo-type priority title tags contents)
1539 ;; Otherwise, use a default template.
1540 (org-latex--wrap-label
1541 inlinetask
1542 (let ((full-title
1543 (concat
1544 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1545 (when priority (format "\\framebox{\\#%c} " priority))
1546 title
1547 (when tags (format "\\hfill{}\\textsc{:%s:}"
1548 (mapconcat 'identity tags ":"))))))
1549 (format (concat "\\begin{center}\n"
1550 "\\fbox{\n"
1551 "\\begin{minipage}[c]{.6\\textwidth}\n"
1552 "%s\n\n"
1553 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1554 "%s"
1555 "\\end{minipage}\n"
1556 "}\n"
1557 "\\end{center}")
1558 full-title contents))))))
1561 ;;;; Italic
1563 (defun org-latex-italic (italic contents info)
1564 "Transcode ITALIC from Org to LaTeX.
1565 CONTENTS is the text with italic markup. INFO is a plist holding
1566 contextual information."
1567 (org-latex--text-markup contents 'italic))
1570 ;;;; Item
1572 (defun org-latex-item (item contents info)
1573 "Transcode an ITEM element from Org to LaTeX.
1574 CONTENTS holds the contents of the item. INFO is a plist holding
1575 contextual information."
1576 (let* ((counter
1577 (let ((count (org-element-property :counter item))
1578 (level
1579 ;; Determine level of current item to determine the
1580 ;; correct LaTeX counter to use (enumi, enumii...).
1581 (let ((parent item) (level 0))
1582 (while (memq (org-element-type
1583 (setq parent (org-export-get-parent parent)))
1584 '(plain-list item))
1585 (when (and (eq (org-element-type parent) 'plain-list)
1586 (eq (org-element-property :type parent)
1587 'ordered))
1588 (incf level)))
1589 level)))
1590 (and count
1591 (< level 5)
1592 (format "\\setcounter{enum%s}{%s}\n"
1593 (nth (1- level) '("i" "ii" "iii" "iv"))
1594 (1- count)))))
1595 (checkbox (case (org-element-property :checkbox item)
1596 (on "$\\boxtimes$ ")
1597 (off "$\\Box$ ")
1598 (trans "$\\boxminus$ ")))
1599 (tag (let ((tag (org-element-property :tag item)))
1600 ;; Check-boxes must belong to the tag.
1601 (and tag (format "[{%s}] "
1602 (concat checkbox
1603 (org-export-data tag info)))))))
1604 (concat counter "\\item" (or tag (concat " " checkbox))
1605 (and contents (org-trim contents))
1606 ;; If there are footnotes references in tag, be sure to
1607 ;; add their definition at the end of the item. This
1608 ;; workaround is necessary since "\footnote{}" command is
1609 ;; not supported in tags.
1610 (and tag
1611 (org-latex--delayed-footnotes-definitions
1612 (org-element-property :tag item) info)))))
1615 ;;;; Keyword
1617 (defun org-latex-keyword (keyword contents info)
1618 "Transcode a KEYWORD element from Org to LaTeX.
1619 CONTENTS is nil. INFO is a plist holding contextual information."
1620 (let ((key (org-element-property :key keyword))
1621 (value (org-element-property :value keyword)))
1622 (cond
1623 ((string= key "LATEX") value)
1624 ((string= key "INDEX") (format "\\index{%s}" value))
1625 ((string= key "TOC")
1626 (let ((value (downcase value)))
1627 (cond
1628 ((string-match "\\<headlines\\>" value)
1629 (let ((depth (or (and (string-match "[0-9]+" value)
1630 (string-to-number (match-string 0 value)))
1631 (plist-get info :with-toc))))
1632 (concat
1633 (when (wholenump depth)
1634 (format "\\setcounter{tocdepth}{%s}\n" depth))
1635 "\\tableofcontents")))
1636 ((string= "tables" value) "\\listoftables")
1637 ((string= "listings" value)
1638 (cond
1639 ((eq org-latex-listings 'minted) "\\listoflistings")
1640 (org-latex-listings "\\lstlistoflistings")
1641 ;; At the moment, src blocks with a caption are wrapped
1642 ;; into a figure environment.
1643 (t "\\listoffigures")))))))))
1646 ;;;; Latex Environment
1648 (defun org-latex-latex-environment (latex-environment contents info)
1649 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1650 CONTENTS is nil. INFO is a plist holding contextual information."
1651 (when (plist-get info :with-latex)
1652 (let ((label (org-element-property :name latex-environment))
1653 (value (org-remove-indentation
1654 (org-element-property :value latex-environment))))
1655 (if (not (org-string-nw-p label)) value
1656 ;; Environment is labelled: label must be within the environment
1657 ;; (otherwise, a reference pointing to that element will count
1658 ;; the section instead).
1659 (with-temp-buffer
1660 (insert value)
1661 (goto-char (point-min))
1662 (forward-line)
1663 (insert
1664 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1665 (buffer-string))))))
1668 ;;;; Latex Fragment
1670 (defun org-latex-latex-fragment (latex-fragment contents info)
1671 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1672 CONTENTS is nil. INFO is a plist holding contextual information."
1673 (when (plist-get info :with-latex)
1674 (org-element-property :value latex-fragment)))
1677 ;;;; Line Break
1679 (defun org-latex-line-break (line-break contents info)
1680 "Transcode a LINE-BREAK object from Org to LaTeX.
1681 CONTENTS is nil. INFO is a plist holding contextual information."
1682 "\\\\\n")
1685 ;;;; Link
1687 (defun org-latex--inline-image (link info)
1688 "Return LaTeX code for an inline image.
1689 LINK is the link pointing to the inline image. INFO is a plist
1690 used as a communication channel."
1691 (let* ((parent (org-export-get-parent-element link))
1692 (path (let ((raw-path (org-element-property :path link)))
1693 (if (not (file-name-absolute-p raw-path)) raw-path
1694 (expand-file-name raw-path))))
1695 (filetype (file-name-extension path))
1696 (caption (org-latex--caption/label-string parent info))
1697 ;; Retrieve latex attributes from the element around.
1698 (attr (org-export-read-attribute :attr_latex parent))
1699 (float (let ((float (plist-get attr :float)))
1700 (cond ((string= float "wrap") 'wrap)
1701 ((string= float "multicolumn") 'multicolumn)
1702 ((or (string= float "figure")
1703 (org-element-property :caption parent))
1704 'figure))))
1705 (placement
1706 (let ((place (plist-get attr :placement)))
1707 (cond (place (format "%s" place))
1708 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1709 ((eq float 'figure)
1710 (format "[%s]" org-latex-default-figure-position))
1711 (t ""))))
1712 (comment-include (if (plist-get attr :comment-include) "%" ""))
1713 ;; It is possible to specify width and height in the
1714 ;; ATTR_LATEX line, and also via default variables.
1715 (width (cond ((plist-get attr :width))
1716 ((plist-get attr :height) "")
1717 ((eq float 'wrap) "0.48\\textwidth")
1718 (t org-latex-image-default-width)))
1719 (height (cond ((plist-get attr :height))
1720 ((or (plist-get attr :width)
1721 (memq float '(figure wrap))) "")
1722 (t org-latex-image-default-height)))
1723 (options (let ((opt (or (plist-get attr :options)
1724 org-latex-image-default-option)))
1725 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1726 (match-string 1 opt))))
1727 image-code)
1728 (if (equal filetype "tikz")
1729 ;; For tikz images:
1730 ;; - use \input to read in image file.
1731 ;; - if options are present, wrap in a tikzpicture environment.
1732 ;; - if width or height are present, use \resizebox to change
1733 ;; the image size.
1734 (progn
1735 (setq image-code (format "\\input{%s}" path))
1736 (when (org-string-nw-p options)
1737 (setq image-code
1738 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1739 options
1740 image-code)))
1741 (when (or (org-string-nw-p width) (org-string-nw-p height))
1742 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1743 (if (org-string-nw-p width) width "!")
1744 (if (org-string-nw-p height) height "!")
1745 image-code))))
1746 ;; For other images:
1747 ;; - add width and height to options.
1748 ;; - include the image with \includegraphics.
1749 (when (org-string-nw-p width)
1750 (setq options (concat options ",width=" width)))
1751 (when (org-string-nw-p height)
1752 (setq options (concat options ",height=" height)))
1753 (setq image-code
1754 (format "\\includegraphics%s{%s}"
1755 (cond ((not (org-string-nw-p options)) "")
1756 ((= (aref options 0) ?,)
1757 (format "[%s]"(substring options 1)))
1758 (t (format "[%s]" options)))
1759 path)))
1760 ;; Return proper string, depending on FLOAT.
1761 (case float
1762 (wrap (format "\\begin{wrapfigure}%s
1763 \\centering
1764 %s%s
1765 %s\\end{wrapfigure}" placement comment-include image-code caption))
1766 (multicolumn (format "\\begin{figure*}%s
1767 \\centering
1768 %s%s
1769 %s\\end{figure*}" placement comment-include image-code caption))
1770 (figure (format "\\begin{figure}%s
1771 \\centering
1772 %s%s
1773 %s\\end{figure}" placement comment-include image-code caption))
1774 (otherwise image-code))))
1776 (defun org-latex-link (link desc info)
1777 "Transcode a LINK object from Org to LaTeX.
1779 DESC is the description part of the link, or the empty string.
1780 INFO is a plist holding contextual information. See
1781 `org-export-data'."
1782 (let* ((type (org-element-property :type link))
1783 (raw-path (org-element-property :path link))
1784 ;; Ensure DESC really exists, or set it to nil.
1785 (desc (and (not (string= desc "")) desc))
1786 (imagep (org-export-inline-image-p
1787 link org-latex-inline-image-rules))
1788 (path (cond
1789 ((member type '("http" "https" "ftp" "mailto"))
1790 (concat type ":" raw-path))
1791 ((string= type "file")
1792 (if (not (file-name-absolute-p raw-path)) raw-path
1793 (concat "file://" (expand-file-name raw-path))))
1794 (t raw-path)))
1795 protocol)
1796 (cond
1797 ;; Image file.
1798 (imagep (org-latex--inline-image link info))
1799 ;; Radio link: Transcode target's contents and use them as link's
1800 ;; description.
1801 ((string= type "radio")
1802 (let ((destination (org-export-resolve-radio-link link info)))
1803 (when destination
1804 (format "\\hyperref[%s]{%s}"
1805 (org-export-solidify-link-text path)
1806 (org-export-data (org-element-contents destination) info)))))
1807 ;; Links pointing to a headline: Find destination and build
1808 ;; appropriate referencing command.
1809 ((member type '("custom-id" "fuzzy" "id"))
1810 (let ((destination (if (string= type "fuzzy")
1811 (org-export-resolve-fuzzy-link link info)
1812 (org-export-resolve-id-link link info))))
1813 (case (org-element-type destination)
1814 ;; Id link points to an external file.
1815 (plain-text
1816 (if desc (format "\\href{%s}{%s}" destination desc)
1817 (format "\\url{%s}" destination)))
1818 ;; Fuzzy link points nowhere.
1819 ('nil
1820 (format org-latex-link-with-unknown-path-format
1821 (or desc
1822 (org-export-data
1823 (org-element-property :raw-link link) info))))
1824 ;; LINK points to a headline. If headlines are numbered
1825 ;; and the link has no description, display headline's
1826 ;; number. Otherwise, display description or headline's
1827 ;; title.
1828 (headline
1829 (let ((label
1830 (format "sec-%s"
1831 (mapconcat
1832 'number-to-string
1833 (org-export-get-headline-number destination info)
1834 "-"))))
1835 (if (and (plist-get info :section-numbers) (not desc))
1836 (format "\\ref{%s}" label)
1837 (format "\\hyperref[%s]{%s}" label
1838 (or desc
1839 (org-export-data
1840 (org-element-property :title destination) info))))))
1841 ;; Fuzzy link points to a target. Do as above.
1842 (otherwise
1843 (let ((path (org-export-solidify-link-text path)))
1844 (if (not desc) (format "\\ref{%s}" path)
1845 (format "\\hyperref[%s]{%s}" path desc)))))))
1846 ;; Coderef: replace link with the reference name or the
1847 ;; equivalent line number.
1848 ((string= type "coderef")
1849 (format (org-export-get-coderef-format path desc)
1850 (org-export-resolve-coderef path info)))
1851 ;; Link type is handled by a special function.
1852 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1853 (funcall protocol (org-link-unescape path) desc 'latex))
1854 ;; External link with a description part.
1855 ((and path desc) (format "\\href{%s}{%s}" path desc))
1856 ;; External link without a description part.
1857 (path (format "\\url{%s}" path))
1858 ;; No path, only description. Try to do something useful.
1859 (t (format org-latex-link-with-unknown-path-format desc)))))
1862 ;;;; Paragraph
1864 (defun org-latex-paragraph (paragraph contents info)
1865 "Transcode a PARAGRAPH element from Org to LaTeX.
1866 CONTENTS is the contents of the paragraph, as a string. INFO is
1867 the plist used as a communication channel."
1868 contents)
1871 ;;;; Plain List
1873 (defun org-latex-plain-list (plain-list contents info)
1874 "Transcode a PLAIN-LIST element from Org to LaTeX.
1875 CONTENTS is the contents of the list. INFO is a plist holding
1876 contextual information."
1877 (let* ((type (org-element-property :type plain-list))
1878 (attr (org-export-read-attribute :attr_latex plain-list))
1879 (latex-type (let ((env (plist-get attr :environment)))
1880 (cond (env (format "%s" env))
1881 ((eq type 'ordered) "enumerate")
1882 ((eq type 'unordered) "itemize")
1883 ((eq type 'descriptive) "description")))))
1884 (org-latex--wrap-label
1885 plain-list
1886 (format "\\begin{%s}%s\n%s\\end{%s}"
1887 latex-type
1888 ;; Put optional arguments, if any inside square brackets
1889 ;; when necessary.
1890 (let ((options (format "%s" (or (plist-get attr :options) ""))))
1891 (cond ((equal options "") "")
1892 ((string-match "\\`\\[.*\\]\\'" options) options)
1893 (t (format "[%s]" options))))
1894 contents
1895 latex-type))))
1898 ;;;; Plain Text
1900 (defun org-latex-plain-text (text info)
1901 "Transcode a TEXT string from Org to LaTeX.
1902 TEXT is the string to transcode. INFO is a plist holding
1903 contextual information."
1904 (let ((specialp (plist-get info :with-special-strings))
1905 (output text))
1906 ;; Protect %, #, &, $, _, { and }.
1907 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
1908 (setq output
1909 (replace-match
1910 (format "\\%s" (match-string 2 output)) nil t output 2)))
1911 ;; Protect ^.
1912 (setq output
1913 (replace-regexp-in-string
1914 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
1915 ;; Protect \. If special strings are used, be careful not to
1916 ;; protect "\" in "\-" constructs.
1917 (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
1918 (setq output
1919 (replace-regexp-in-string
1920 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
1921 "$\\backslash$" output nil t 1)))
1922 ;; Protect ~.
1923 (setq output
1924 (replace-regexp-in-string
1925 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
1926 ;; Activate smart quotes. Be sure to provide original TEXT string
1927 ;; since OUTPUT may have been modified.
1928 (when (plist-get info :with-smart-quotes)
1929 (setq output (org-export-activate-smart-quotes output :latex info text)))
1930 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1931 (let ((case-fold-search nil)
1932 (start 0))
1933 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
1934 (setq output (replace-match
1935 (format "\\%s{}" (match-string 1 output)) nil t output)
1936 start (match-end 0))))
1937 ;; Convert special strings.
1938 (when specialp
1939 (setq output
1940 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
1941 ;; Handle break preservation if required.
1942 (when (plist-get info :preserve-breaks)
1943 (setq output (replace-regexp-in-string
1944 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
1945 ;; Return value.
1946 output))
1949 ;;;; Planning
1951 (defun org-latex-planning (planning contents info)
1952 "Transcode a PLANNING element from Org to LaTeX.
1953 CONTENTS is nil. INFO is a plist holding contextual
1954 information."
1955 (concat
1956 "\\noindent"
1957 (mapconcat
1958 'identity
1959 (delq nil
1960 (list
1961 (let ((closed (org-element-property :closed planning)))
1962 (when closed
1963 (concat
1964 (format "\\textbf{%s} " org-closed-string)
1965 (format org-latex-inactive-timestamp-format
1966 (org-translate-time
1967 (org-element-property :raw-value closed))))))
1968 (let ((deadline (org-element-property :deadline planning)))
1969 (when deadline
1970 (concat
1971 (format "\\textbf{%s} " org-deadline-string)
1972 (format org-latex-active-timestamp-format
1973 (org-translate-time
1974 (org-element-property :raw-value deadline))))))
1975 (let ((scheduled (org-element-property :scheduled planning)))
1976 (when scheduled
1977 (concat
1978 (format "\\textbf{%s} " org-scheduled-string)
1979 (format org-latex-active-timestamp-format
1980 (org-translate-time
1981 (org-element-property :raw-value scheduled))))))))
1982 " ")
1983 "\\\\"))
1986 ;;;; Quote Block
1988 (defun org-latex-quote-block (quote-block contents info)
1989 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1990 CONTENTS holds the contents of the block. INFO is a plist
1991 holding contextual information."
1992 (org-latex--wrap-label
1993 quote-block
1994 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1997 ;;;; Quote Section
1999 (defun org-latex-quote-section (quote-section contents info)
2000 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2001 CONTENTS is nil. INFO is a plist holding contextual information."
2002 (let ((value (org-remove-indentation
2003 (org-element-property :value quote-section))))
2004 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
2007 ;;;; Radio Target
2009 (defun org-latex-radio-target (radio-target text info)
2010 "Transcode a RADIO-TARGET object from Org to LaTeX.
2011 TEXT is the text of the target. INFO is a plist holding
2012 contextual information."
2013 (format "\\label{%s}%s"
2014 (org-export-solidify-link-text
2015 (org-element-property :value radio-target))
2016 text))
2019 ;;;; Section
2021 (defun org-latex-section (section contents info)
2022 "Transcode a SECTION element from Org to LaTeX.
2023 CONTENTS holds the contents of the section. INFO is a plist
2024 holding contextual information."
2025 contents)
2028 ;;;; Special Block
2030 (defun org-latex-special-block (special-block contents info)
2031 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2032 CONTENTS holds the contents of the block. INFO is a plist
2033 holding contextual information."
2034 (let ((type (downcase (org-element-property :type special-block)))
2035 (opt (org-export-read-attribute :attr_latex special-block :options)))
2036 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2037 ;; Insert any label or caption within the block
2038 ;; (otherwise, a reference pointing to that element will
2039 ;; count the section instead).
2040 (org-latex--caption/label-string special-block info)
2041 contents
2042 (format "\\end{%s}" type))))
2045 ;;;; Src Block
2047 (defun org-latex-src-block (src-block contents info)
2048 "Transcode a SRC-BLOCK element from Org to LaTeX.
2049 CONTENTS holds the contents of the item. INFO is a plist holding
2050 contextual information."
2051 (when (org-string-nw-p (org-element-property :value src-block))
2052 (let* ((lang (org-element-property :language src-block))
2053 (caption (org-element-property :caption src-block))
2054 (label (org-element-property :name src-block))
2055 (custom-env (and lang
2056 (cadr (assq (intern lang)
2057 org-latex-custom-lang-environments))))
2058 (num-start (case (org-element-property :number-lines src-block)
2059 (continued (org-export-get-loc src-block info))
2060 (new 0)))
2061 (retain-labels (org-element-property :retain-labels src-block))
2062 (long-listing
2063 (let ((attr (org-export-read-attribute :attr_latex src-block)))
2064 (if (plist-member attr :long-listing)
2065 (plist-get attr :long-listing)
2066 org-latex-long-listings))))
2067 (cond
2068 ;; Case 1. No source fontification.
2069 ((not org-latex-listings)
2070 (let* ((caption-str (org-latex--caption/label-string src-block info))
2071 (float-env (and (not long-listing)
2072 (or label caption)
2073 (format "\\begin{figure}[H]\n%s%%s\n\\end{figure}"
2074 caption-str))))
2075 (format
2076 (or float-env "%s")
2077 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2078 (org-export-format-code-default src-block info))))))
2079 ;; Case 2. Custom environment.
2080 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2081 custom-env
2082 (org-export-format-code-default src-block info)
2083 custom-env))
2084 ;; Case 3. Use minted package.
2085 ((eq org-latex-listings 'minted)
2086 (let ((float-env
2087 (and (not long-listing)
2088 (or label caption)
2089 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2090 (org-latex--caption/label-string src-block info))))
2091 (body
2092 (format
2093 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2094 ;; Options.
2095 (org-latex--make-option-string
2096 (if (or (not num-start)
2097 (assoc "linenos" org-latex-minted-options))
2098 org-latex-minted-options
2099 (append `(("linenos")
2100 ("firstnumber" ,(number-to-string (1+ num-start))))
2101 org-latex-minted-options)))
2102 ;; Language.
2103 (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
2104 ;; Source code.
2105 (let* ((code-info (org-export-unravel-code src-block))
2106 (max-width
2107 (apply 'max
2108 (mapcar 'length
2109 (org-split-string (car code-info)
2110 "\n")))))
2111 (org-export-format-code
2112 (car code-info)
2113 (lambda (loc num ref)
2114 (concat
2116 (when ref
2117 ;; Ensure references are flushed to the right,
2118 ;; separated with 6 spaces from the widest line
2119 ;; of code.
2120 (concat (make-string (+ (- max-width (length loc)) 6)
2121 ?\s)
2122 (format "(%s)" ref)))))
2123 nil (and retain-labels (cdr code-info)))))))
2124 ;; Return value.
2125 (if float-env (format float-env body) body)))
2126 ;; Case 4. Use listings package.
2128 (let ((lst-lang
2129 (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
2130 (caption-str
2131 (when caption
2132 (let ((main (org-export-get-caption src-block))
2133 (secondary (org-export-get-caption src-block t)))
2134 (if (not secondary)
2135 (format "{%s}" (org-export-data main info))
2136 (format "{[%s]%s}"
2137 (org-export-data secondary info)
2138 (org-export-data main info)))))))
2139 (concat
2140 ;; Options.
2141 (format "\\lstset{%s}\n"
2142 (org-latex--make-option-string
2143 (append
2144 org-latex-listings-options
2145 `(("language" ,lst-lang))
2146 (when label `(("label" ,label)))
2147 (when caption-str `(("caption" ,caption-str)))
2148 (cond ((assoc "numbers" org-latex-listings-options) nil)
2149 ((not num-start) '(("numbers" "none")))
2150 ((zerop num-start) '(("numbers" "left")))
2151 (t `(("numbers" "left")
2152 ("firstnumber"
2153 ,(number-to-string (1+ num-start)))))))))
2154 ;; Source code.
2155 (format
2156 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2157 (let* ((code-info (org-export-unravel-code src-block))
2158 (max-width
2159 (apply 'max
2160 (mapcar 'length
2161 (org-split-string (car code-info) "\n")))))
2162 (org-export-format-code
2163 (car code-info)
2164 (lambda (loc num ref)
2165 (concat
2167 (when ref
2168 ;; Ensure references are flushed to the right,
2169 ;; separated with 6 spaces from the widest line of
2170 ;; code
2171 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2172 (format "(%s)" ref)))))
2173 nil (and retain-labels (cdr code-info))))))))))))
2176 ;;;; Statistics Cookie
2178 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2179 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2180 CONTENTS is nil. INFO is a plist holding contextual information."
2181 (replace-regexp-in-string
2182 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2185 ;;;; Strike-Through
2187 (defun org-latex-strike-through (strike-through contents info)
2188 "Transcode STRIKE-THROUGH from Org to LaTeX.
2189 CONTENTS is the text with strike-through markup. INFO is a plist
2190 holding contextual information."
2191 (org-latex--text-markup contents 'strike-through))
2194 ;;;; Subscript
2196 (defun org-latex--script-size (object info)
2197 "Transcode a subscript or superscript object.
2198 OBJECT is an Org object. INFO is a plist used as a communication
2199 channel."
2200 (let ((in-script-p
2201 ;; Non-nil if object is already in a sub/superscript.
2202 (let ((parent object))
2203 (catch 'exit
2204 (while (setq parent (org-export-get-parent parent))
2205 (let ((type (org-element-type parent)))
2206 (cond ((memq type '(subscript superscript))
2207 (throw 'exit t))
2208 ((memq type org-element-all-elements)
2209 (throw 'exit nil))))))))
2210 (type (org-element-type object))
2211 (output ""))
2212 (org-element-map (org-element-contents object)
2213 (cons 'plain-text org-element-all-objects)
2214 (lambda (obj)
2215 (case (org-element-type obj)
2216 ((entity latex-fragment)
2217 (let ((data (org-trim (org-export-data obj info))))
2218 (string-match
2219 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2220 data)
2221 (setq output
2222 (concat output
2223 (match-string 1 data)
2224 (let ((blank (org-element-property :post-blank obj)))
2225 (and blank (> blank 0) "\\ "))))))
2226 (plain-text
2227 (setq output
2228 (format "%s\\text{%s}" output (org-export-data obj info))))
2229 (otherwise
2230 (setq output
2231 (concat output
2232 (org-export-data obj info)
2233 (let ((blank (org-element-property :post-blank obj)))
2234 (and blank (> blank 0) "\\ ")))))))
2235 info nil org-element-recursive-objects)
2236 ;; Result. Do not wrap into math mode if already in a subscript
2237 ;; or superscript. Do not wrap into curly brackets if OUTPUT is
2238 ;; a single character. Also merge consecutive subscript and
2239 ;; superscript into the same math snippet.
2240 (concat (and (not in-script-p)
2241 (let ((prev (org-export-get-previous-element object info)))
2242 (or (not prev)
2243 (not (eq (org-element-type prev)
2244 (if (eq type 'subscript) 'superscript
2245 'subscript)))
2246 (let ((blank (org-element-property :post-blank prev)))
2247 (and blank (> blank 0)))))
2248 "$")
2249 (if (eq (org-element-type object) 'subscript) "_" "^")
2250 (and (> (length output) 1) "{")
2251 output
2252 (and (> (length output) 1) "}")
2253 (and (not in-script-p)
2254 (or (let ((blank (org-element-property :post-blank object)))
2255 (and blank (> blank 0)))
2256 (not (eq (org-element-type
2257 (org-export-get-next-element object info))
2258 (if (eq type 'subscript) 'superscript
2259 'subscript))))
2260 "$"))))
2262 (defun org-latex-subscript (subscript contents info)
2263 "Transcode a SUBSCRIPT object from Org to LaTeX.
2264 CONTENTS is the contents of the object. INFO is a plist holding
2265 contextual information."
2266 (org-latex--script-size subscript info))
2269 ;;;; Superscript
2271 (defun org-latex-superscript (superscript contents info)
2272 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2273 CONTENTS is the contents of the object. INFO is a plist holding
2274 contextual information."
2275 (org-latex--script-size superscript info))
2278 ;;;; Table
2280 ;; `org-latex-table' is the entry point for table transcoding. It
2281 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2282 ;; delegates the job to either `org-latex--table.el-table',
2283 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2284 ;; depending of the type of the table and the mode requested.
2286 ;; `org-latex--align-string' is a subroutine used to build alignment
2287 ;; string for Org tables.
2289 (defun org-latex-table (table contents info)
2290 "Transcode a TABLE element from Org to LaTeX.
2291 CONTENTS is the contents of the table. INFO is a plist holding
2292 contextual information."
2293 (if (eq (org-element-property :type table) 'table.el)
2294 ;; "table.el" table. Convert it using appropriate tools.
2295 (org-latex--table.el-table table info)
2296 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2297 org-latex-default-table-mode)))
2298 (cond
2299 ;; Case 1: Verbatim table.
2300 ((string= type "verbatim")
2301 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2302 ;; Re-create table, without affiliated keywords.
2303 (org-trim (org-element-interpret-data
2304 `(table nil ,@(org-element-contents table))))))
2305 ;; Case 2: Matrix.
2306 ((or (string= type "math") (string= type "inline-math"))
2307 (org-latex--math-table table info))
2308 ;; Case 3: Standard table.
2309 (t (concat (org-latex--org-table table contents info)
2310 ;; When there are footnote references within the
2311 ;; table, insert their definition just after it.
2312 (org-latex--delayed-footnotes-definitions table info)))))))
2314 (defun org-latex--align-string (table info)
2315 "Return an appropriate LaTeX alignment string.
2316 TABLE is the considered table. INFO is a plist used as
2317 a communication channel."
2318 (or (org-export-read-attribute :attr_latex table :align)
2319 (let (align)
2320 ;; Extract column groups and alignment from first (non-rule)
2321 ;; row.
2322 (org-element-map
2323 (org-element-map table 'table-row
2324 (lambda (row)
2325 (and (eq (org-element-property :type row) 'standard) row))
2326 info 'first-match)
2327 'table-cell
2328 (lambda (cell)
2329 (let ((borders (org-export-table-cell-borders cell info)))
2330 ;; Check left border for the first cell only.
2331 (when (and (memq 'left borders) (not align))
2332 (push "|" align))
2333 (push (case (org-export-table-cell-alignment cell info)
2334 (left "l")
2335 (right "r")
2336 (center "c"))
2337 align)
2338 (when (memq 'right borders) (push "|" align))))
2339 info)
2340 (apply 'concat (nreverse align)))))
2342 (defun org-latex--org-table (table contents info)
2343 "Return appropriate LaTeX code for an Org table.
2345 TABLE is the table type element to transcode. CONTENTS is its
2346 contents, as a string. INFO is a plist used as a communication
2347 channel.
2349 This function assumes TABLE has `org' as its `:type' property and
2350 `table' as its `:mode' attribute."
2351 (let* ((caption (org-latex--caption/label-string table info))
2352 (attr (org-export-read-attribute :attr_latex table))
2353 ;; Determine alignment string.
2354 (alignment (org-latex--align-string table info))
2355 ;; Determine environment for the table: longtable, tabular...
2356 (table-env (or (plist-get attr :environment)
2357 org-latex-default-table-environment))
2358 ;; If table is a float, determine environment: table, table*
2359 ;; or sidewaystable.
2360 (float-env (unless (member table-env '("longtable" "longtabu"))
2361 (let ((float (plist-get attr :float)))
2362 (cond
2363 ((string= float "sidewaystable") "sidewaystable")
2364 ((string= float "multicolumn") "table*")
2365 ((or (string= float "table")
2366 (org-element-property :caption table))
2367 "table")))))
2368 ;; Extract others display options.
2369 (fontsize (let ((font (plist-get attr :font)))
2370 (and font (concat font "\n"))))
2371 (width (plist-get attr :width))
2372 (spreadp (plist-get attr :spread))
2373 (placement (or (plist-get attr :placement)
2374 (format "[%s]" org-latex-default-figure-position)))
2375 (centerp (if (plist-member attr :center) (plist-get attr :center)
2376 org-latex-tables-centered)))
2377 ;; Prepare the final format string for the table.
2378 (cond
2379 ;; Longtable.
2380 ((equal "longtable" table-env)
2381 (concat (and fontsize (concat "{" fontsize))
2382 (format "\\begin{longtable}{%s}\n" alignment)
2383 (and org-latex-table-caption-above
2384 (org-string-nw-p caption)
2385 (concat caption "\\\\\n"))
2386 contents
2387 (and (not org-latex-table-caption-above)
2388 (org-string-nw-p caption)
2389 (concat caption "\\\\\n"))
2390 "\\end{longtable}\n"
2391 (and fontsize "}")))
2392 ;; Longtabu
2393 ((equal "longtabu" table-env)
2394 (concat (and fontsize (concat "{" fontsize))
2395 (format "\\begin{longtabu}%s{%s}\n"
2396 (if width
2397 (format " %s %s "
2398 (if spreadp "spread" "to") width) "")
2399 alignment)
2400 (and org-latex-table-caption-above
2401 (org-string-nw-p caption)
2402 (concat caption "\\\\\n"))
2403 contents
2404 (and (not org-latex-table-caption-above)
2405 (org-string-nw-p caption)
2406 (concat caption "\\\\\n"))
2407 "\\end{longtabu}\n"
2408 (and fontsize "}")))
2409 ;; Others.
2410 (t (concat (cond
2411 (float-env
2412 (concat (format "\\begin{%s}%s\n" float-env placement)
2413 (if org-latex-table-caption-above caption "")
2414 (when centerp "\\centering\n")
2415 fontsize))
2416 (centerp (concat "\\begin{center}\n" fontsize))
2417 (fontsize (concat "{" fontsize)))
2418 (cond ((equal "tabu" table-env)
2419 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2420 (if width (format
2421 (if spreadp " spread %s " " to %s ")
2422 width) "")
2423 alignment
2424 contents))
2425 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2426 table-env
2427 (if width (format "{%s}" width) "")
2428 alignment
2429 contents
2430 table-env)))
2431 (cond
2432 (float-env
2433 (concat (if org-latex-table-caption-above "" caption)
2434 (format "\n\\end{%s}" float-env)))
2435 (centerp "\n\\end{center}")
2436 (fontsize "}")))))))
2438 (defun org-latex--table.el-table (table info)
2439 "Return appropriate LaTeX code for a table.el table.
2441 TABLE is the table type element to transcode. INFO is a plist
2442 used as a communication channel.
2444 This function assumes TABLE has `table.el' as its `:type'
2445 property."
2446 (require 'table)
2447 ;; Ensure "*org-export-table*" buffer is empty.
2448 (with-current-buffer (get-buffer-create "*org-export-table*")
2449 (erase-buffer))
2450 (let ((output (with-temp-buffer
2451 (insert (org-element-property :value table))
2452 (goto-char 1)
2453 (re-search-forward "^[ \t]*|[^|]" nil t)
2454 (table-generate-source 'latex "*org-export-table*")
2455 (with-current-buffer "*org-export-table*"
2456 (org-trim (buffer-string))))))
2457 (kill-buffer (get-buffer "*org-export-table*"))
2458 ;; Remove left out comments.
2459 (while (string-match "^%.*\n" output)
2460 (setq output (replace-match "" t t output)))
2461 (let ((attr (org-export-read-attribute :attr_latex table)))
2462 (when (plist-get attr :rmlines)
2463 ;; When the "rmlines" attribute is provided, remove all hlines
2464 ;; but the the one separating heading from the table body.
2465 (let ((n 0) (pos 0))
2466 (while (and (< (length output) pos)
2467 (setq pos (string-match "^\\\\hline\n?" output pos)))
2468 (incf n)
2469 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2470 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2471 org-latex-tables-centered)))
2472 (if (not centerp) output
2473 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2475 (defun org-latex--math-table (table info)
2476 "Return appropriate LaTeX code for a matrix.
2478 TABLE is the table type element to transcode. INFO is a plist
2479 used as a communication channel.
2481 This function assumes TABLE has `org' as its `:type' property and
2482 `inline-math' or `math' as its `:mode' attribute.."
2483 (let* ((caption (org-latex--caption/label-string table info))
2484 (attr (org-export-read-attribute :attr_latex table))
2485 (inlinep (equal (plist-get attr :mode) "inline-math"))
2486 (env (or (plist-get attr :environment)
2487 org-latex-default-table-environment))
2488 (contents
2489 (mapconcat
2490 (lambda (row)
2491 ;; Ignore horizontal rules.
2492 (when (eq (org-element-property :type row) 'standard)
2493 ;; Return each cell unmodified.
2494 (concat
2495 (mapconcat
2496 (lambda (cell)
2497 (substring (org-element-interpret-data cell) 0 -1))
2498 (org-element-map row 'table-cell 'identity info) "&")
2499 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2500 "\n")))
2501 (org-element-map table 'table-row 'identity info) ""))
2502 ;; Variables related to math clusters (contiguous math tables
2503 ;; of the same type).
2504 (mode (org-export-read-attribute :attr_latex table :mode))
2505 (prev (org-export-get-previous-element table info))
2506 (next (org-export-get-next-element table info))
2507 (same-mode-p
2508 (lambda (table)
2509 ;; Non-nil when TABLE has the same mode as current table.
2510 (string= (or (org-export-read-attribute :attr_latex table :mode)
2511 org-latex-default-table-mode)
2512 mode))))
2513 (concat
2514 ;; Opening string. If TABLE is in the middle of a table cluster,
2515 ;; do not insert any.
2516 (cond ((and prev
2517 (eq (org-element-type prev) 'table)
2518 (memq (org-element-property :post-blank prev) '(0 nil))
2519 (funcall same-mode-p prev))
2520 nil)
2521 (inlinep "\\(")
2522 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2523 (t "\\["))
2524 ;; Prefix.
2525 (or (plist-get attr :math-prefix) "")
2526 ;; Environment. Also treat special cases.
2527 (cond ((equal env "array")
2528 (let ((align (org-latex--align-string table info)))
2529 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2530 ((assoc env org-latex-table-matrix-macros)
2531 (format "\\%s%s{\n%s}"
2533 (or (plist-get attr :math-arguments) "")
2534 contents))
2535 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2536 ;; Suffix.
2537 (or (plist-get attr :math-suffix) "")
2538 ;; Closing string. If TABLE is in the middle of a table cluster,
2539 ;; do not insert any. If it closes such a cluster, be sure to
2540 ;; close the cluster with a string matching the opening string.
2541 (cond ((and next
2542 (eq (org-element-type next) 'table)
2543 (memq (org-element-property :post-blank table) '(0 nil))
2544 (funcall same-mode-p next))
2545 nil)
2546 (inlinep "\\)")
2547 ;; Find cluster beginning to know which environment to use.
2548 ((let ((cluster-beg table) prev)
2549 (while (and (setq prev (org-export-get-previous-element
2550 cluster-beg info))
2551 (memq (org-element-property :post-blank prev)
2552 '(0 nil))
2553 (funcall same-mode-p prev))
2554 (setq cluster-beg prev))
2555 (and (or (org-element-property :caption cluster-beg)
2556 (org-element-property :name cluster-beg))
2557 "\n\\end{equation}")))
2558 (t "\\]")))))
2561 ;;;; Table Cell
2563 (defun org-latex-table-cell (table-cell contents info)
2564 "Transcode a TABLE-CELL element from Org to LaTeX.
2565 CONTENTS is the cell contents. INFO is a plist used as
2566 a communication channel."
2567 (concat (if (and contents
2568 org-latex-table-scientific-notation
2569 (string-match orgtbl-exp-regexp contents))
2570 ;; Use appropriate format string for scientific
2571 ;; notation.
2572 (format org-latex-table-scientific-notation
2573 (match-string 1 contents)
2574 (match-string 2 contents))
2575 contents)
2576 (when (org-export-get-next-element table-cell info) " & ")))
2579 ;;;; Table Row
2581 (defun org-latex-table-row (table-row contents info)
2582 "Transcode a TABLE-ROW element from Org to LaTeX.
2583 CONTENTS is the contents of the row. INFO is a plist used as
2584 a communication channel."
2585 ;; Rules are ignored since table separators are deduced from
2586 ;; borders of the current row.
2587 (when (eq (org-element-property :type table-row) 'standard)
2588 (let* ((attr (org-export-read-attribute :attr_latex
2589 (org-export-get-parent table-row)))
2590 (longtablep (member (or (plist-get attr :environment)
2591 org-latex-default-table-environment)
2592 '("longtable" "longtabu")))
2593 (booktabsp (if (plist-member attr :booktabs)
2594 (plist-get attr :booktabs)
2595 org-latex-tables-booktabs))
2596 ;; TABLE-ROW's borders are extracted from its first cell.
2597 (borders (org-export-table-cell-borders
2598 (car (org-element-contents table-row)) info)))
2599 (concat
2600 ;; When BOOKTABS are activated enforce top-rule even when no
2601 ;; hline was specifically marked.
2602 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2603 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2604 contents "\\\\\n"
2605 (cond
2606 ;; Special case for long tables. Define header and footers.
2607 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2608 (format "%s
2609 \\endhead
2610 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2611 \\endfoot
2612 \\endlastfoot"
2613 (if booktabsp "\\midrule" "\\hline")
2614 (if booktabsp "\\midrule" "\\hline")
2615 ;; Number of columns.
2616 (cdr (org-export-table-dimensions
2617 (org-export-get-parent-table table-row) info))))
2618 ;; When BOOKTABS are activated enforce bottom rule even when
2619 ;; no hline was specifically marked.
2620 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2621 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2622 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2625 ;;;; Target
2627 (defun org-latex-target (target contents info)
2628 "Transcode a TARGET object from Org to LaTeX.
2629 CONTENTS is nil. INFO is a plist holding contextual
2630 information."
2631 (format "\\label{%s}"
2632 (org-export-solidify-link-text (org-element-property :value target))))
2635 ;;;; Timestamp
2637 (defun org-latex-timestamp (timestamp contents info)
2638 "Transcode a TIMESTAMP object from Org to LaTeX.
2639 CONTENTS is nil. INFO is a plist holding contextual
2640 information."
2641 (let ((value (org-latex-plain-text
2642 (org-timestamp-translate timestamp) info)))
2643 (case (org-element-property :type timestamp)
2644 ((active active-range) (format org-latex-active-timestamp-format value))
2645 ((inactive inactive-range)
2646 (format org-latex-inactive-timestamp-format value))
2647 (otherwise (format org-latex-diary-timestamp-format value)))))
2650 ;;;; Underline
2652 (defun org-latex-underline (underline contents info)
2653 "Transcode UNDERLINE from Org to LaTeX.
2654 CONTENTS is the text with underline markup. INFO is a plist
2655 holding contextual information."
2656 (org-latex--text-markup contents 'underline))
2659 ;;;; Verbatim
2661 (defun org-latex-verbatim (verbatim contents info)
2662 "Transcode a VERBATIM object from Org to LaTeX.
2663 CONTENTS is nil. INFO is a plist used as a communication
2664 channel."
2665 (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2668 ;;;; Verse Block
2670 (defun org-latex-verse-block (verse-block contents info)
2671 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2672 CONTENTS is verse block contents. INFO is a plist holding
2673 contextual information."
2674 (org-latex--wrap-label
2675 verse-block
2676 ;; In a verse environment, add a line break to each newline
2677 ;; character and change each white space at beginning of a line
2678 ;; into a space of 1 em. Also change each blank line with
2679 ;; a vertical space of 1 em.
2680 (progn
2681 (setq contents (replace-regexp-in-string
2682 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2683 (replace-regexp-in-string
2684 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2685 (while (string-match "^[ \t]+" contents)
2686 (let ((new-str (format "\\hspace*{%dem}"
2687 (length (match-string 0 contents)))))
2688 (setq contents (replace-match new-str nil t contents))))
2689 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2693 ;;; End-user functions
2695 ;;;###autoload
2696 (defun org-latex-export-as-latex
2697 (&optional async subtreep visible-only body-only ext-plist)
2698 "Export current buffer as a LaTeX buffer.
2700 If narrowing is active in the current buffer, only export its
2701 narrowed part.
2703 If a region is active, export that region.
2705 A non-nil optional argument ASYNC means the process should happen
2706 asynchronously. The resulting buffer should be accessible
2707 through the `org-export-stack' interface.
2709 When optional argument SUBTREEP is non-nil, export the sub-tree
2710 at point, extracting information from the headline properties
2711 first.
2713 When optional argument VISIBLE-ONLY is non-nil, don't export
2714 contents of hidden elements.
2716 When optional argument BODY-ONLY is non-nil, only write code
2717 between \"\\begin{document}\" and \"\\end{document}\".
2719 EXT-PLIST, when provided, is a property list with external
2720 parameters overriding Org default settings, but still inferior to
2721 file-local settings.
2723 Export is done in a buffer named \"*Org LATEX Export*\", which
2724 will be displayed when `org-export-show-temporary-export-buffer'
2725 is non-nil."
2726 (interactive)
2727 (if async
2728 (org-export-async-start
2729 (lambda (output)
2730 (with-current-buffer (get-buffer-create "*Org LATEX Export*")
2731 (erase-buffer)
2732 (insert output)
2733 (goto-char (point-min))
2734 (LaTeX-mode)
2735 (org-export-add-to-stack (current-buffer) 'latex)))
2736 `(org-export-as 'latex ,subtreep ,visible-only ,body-only
2737 ',ext-plist))
2738 (let ((outbuf
2739 (org-export-to-buffer 'latex "*Org LATEX Export*"
2740 subtreep visible-only body-only ext-plist)))
2741 (with-current-buffer outbuf (LaTeX-mode))
2742 (when org-export-show-temporary-export-buffer
2743 (switch-to-buffer-other-window outbuf)))))
2745 ;;;###autoload
2746 (defun org-latex-convert-region-to-latex ()
2747 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2748 This can be used in any buffer. For example, you can write an
2749 itemized list in org-mode syntax in an LaTeX buffer and use this
2750 command to convert it."
2751 (interactive)
2752 (org-export-replace-region-by 'latex))
2754 ;;;###autoload
2755 (defun org-latex-export-to-latex
2756 (&optional async subtreep visible-only body-only ext-plist)
2757 "Export current buffer to a LaTeX file.
2759 If narrowing is active in the current buffer, only export its
2760 narrowed part.
2762 If a region is active, export that region.
2764 A non-nil optional argument ASYNC means the process should happen
2765 asynchronously. The resulting file should be accessible through
2766 the `org-export-stack' interface.
2768 When optional argument SUBTREEP is non-nil, export the sub-tree
2769 at point, extracting information from the headline properties
2770 first.
2772 When optional argument VISIBLE-ONLY is non-nil, don't export
2773 contents of hidden elements.
2775 When optional argument BODY-ONLY is non-nil, only write code
2776 between \"\\begin{document}\" and \"\\end{document}\".
2778 EXT-PLIST, when provided, is a property list with external
2779 parameters overriding Org default settings, but still inferior to
2780 file-local settings.
2782 Return output file's name."
2783 (interactive)
2784 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2785 (if async
2786 (org-export-async-start
2787 (lambda (f) (org-export-add-to-stack f 'latex))
2788 `(expand-file-name
2789 (org-export-to-file
2790 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
2791 (org-export-to-file
2792 'latex outfile subtreep visible-only body-only ext-plist))))
2794 ;;;###autoload
2795 (defun org-latex-export-to-pdf
2796 (&optional async subtreep visible-only body-only ext-plist)
2797 "Export current buffer to LaTeX then process through to PDF.
2799 If narrowing is active in the current buffer, only export its
2800 narrowed part.
2802 If a region is active, export that region.
2804 A non-nil optional argument ASYNC means the process should happen
2805 asynchronously. The resulting file should be accessible through
2806 the `org-export-stack' interface.
2808 When optional argument SUBTREEP is non-nil, export the sub-tree
2809 at point, extracting information from the headline properties
2810 first.
2812 When optional argument VISIBLE-ONLY is non-nil, don't export
2813 contents of hidden elements.
2815 When optional argument BODY-ONLY is non-nil, only write code
2816 between \"\\begin{document}\" and \"\\end{document}\".
2818 EXT-PLIST, when provided, is a property list with external
2819 parameters overriding Org default settings, but still inferior to
2820 file-local settings.
2822 Return PDF file's name."
2823 (interactive)
2824 (if async
2825 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2826 (org-export-async-start
2827 (lambda (f) (org-export-add-to-stack f 'latex))
2828 `(expand-file-name
2829 (org-latex-compile
2830 (org-export-to-file
2831 'latex ,outfile ,subtreep ,visible-only ,body-only
2832 ',ext-plist)))))
2833 (org-latex-compile
2834 (org-latex-export-to-latex
2835 nil subtreep visible-only body-only ext-plist))))
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