1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; See Org manual for details.
29 (eval-when-compile (require 'cl
))
33 (defvar org-latex-default-packages-alist
)
34 (defvar org-latex-packages-alist
)
35 (defvar orgtbl-exp-regexp
)
41 (org-export-define-backend 'latex
42 '((bold . org-latex-bold
)
43 (center-block . org-latex-center-block
)
44 (clock . org-latex-clock
)
45 (code . org-latex-code
)
46 (comment .
(lambda (&rest args
) ""))
47 (comment-block .
(lambda (&rest args
) ""))
48 (drawer . org-latex-drawer
)
49 (dynamic-block . org-latex-dynamic-block
)
50 (entity . org-latex-entity
)
51 (example-block . org-latex-example-block
)
52 (export-block . org-latex-export-block
)
53 (export-snippet . org-latex-export-snippet
)
54 (fixed-width . org-latex-fixed-width
)
55 (footnote-definition . org-latex-footnote-definition
)
56 (footnote-reference . org-latex-footnote-reference
)
57 (headline . org-latex-headline
)
58 (horizontal-rule . org-latex-horizontal-rule
)
59 (inline-src-block . org-latex-inline-src-block
)
60 (inlinetask . org-latex-inlinetask
)
61 (italic . org-latex-italic
)
62 (item . org-latex-item
)
63 (keyword . org-latex-keyword
)
64 (latex-environment . org-latex-latex-environment
)
65 (latex-fragment . org-latex-latex-fragment
)
66 (line-break . org-latex-line-break
)
67 (link . org-latex-link
)
68 (node-property . org-latex-node-property
)
69 (paragraph . org-latex-paragraph
)
70 (plain-list . org-latex-plain-list
)
71 (plain-text . org-latex-plain-text
)
72 (planning . org-latex-planning
)
73 (property-drawer . org-latex-property-drawer
)
74 (quote-block . org-latex-quote-block
)
75 (radio-target . org-latex-radio-target
)
76 (section . org-latex-section
)
77 (special-block . org-latex-special-block
)
78 (src-block . org-latex-src-block
)
79 (statistics-cookie . org-latex-statistics-cookie
)
80 (strike-through . org-latex-strike-through
)
81 (subscript . org-latex-subscript
)
82 (superscript . org-latex-superscript
)
83 (table . org-latex-table
)
84 (table-cell . org-latex-table-cell
)
85 (table-row . org-latex-table-row
)
86 (target . org-latex-target
)
87 (template . org-latex-template
)
88 (timestamp . org-latex-timestamp
)
89 (underline . org-latex-underline
)
90 (verbatim . org-latex-verbatim
)
91 (verse-block . org-latex-verse-block
)
93 (latex-math-block . org-latex-math-block
))
94 :export-block
'("LATEX" "TEX")
96 '(?l
"Export to LaTeX"
97 ((?L
"As LaTeX buffer" org-latex-export-as-latex
)
98 (?l
"As LaTeX file" org-latex-export-to-latex
)
99 (?p
"As PDF file" org-latex-export-to-pdf
)
100 (?o
"As PDF file and open"
102 (if a
(org-latex-export-to-pdf t s v b
)
103 (org-open-file (org-latex-export-to-pdf nil s v b
)))))))
104 :options-alist
'((:date
"DATE" nil
"\\today" t
)
105 (:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
106 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
107 (:latex-header
"LATEX_HEADER" nil nil newline
)
108 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
109 (:latex-hyperref nil nil org-latex-hyperref-template t
)
110 (:latex-custom-id-labels nil nil org-latex-custom-id-as-label
))
111 :filters-alist
'((:filter-options . org-latex-math-block-options-filter
)
112 (:filter-parse-tree . org-latex-math-block-tree-filter
)))
116 ;;; Internal Variables
118 (defconst org-latex-babel-language-alist
119 '(("af" .
"afrikaans")
121 ("bt-br" .
"brazilian")
127 ("de-at" .
"naustrian")
128 ("de-de" .
"ngerman")
131 ("en-au" .
"australian")
132 ("en-ca" .
"canadian")
133 ("en-gb" .
"british")
135 ("en-nz" .
"newzealand")
136 ("en-us" .
"american")
142 ("fr-ca" .
"canadien")
146 ("id" .
"indonesian")
156 ("pt" .
"portuguese")
160 ("sb" .
"uppersorbian")
168 ("uk" .
"ukrainian"))
169 "Alist between language code and corresponding Babel option.")
171 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
172 ("qbordermatrix" .
"\\cr")
173 ("kbordermatrix" .
"\\\\"))
174 "Alist between matrix macros and their row ending.")
176 (defconst org-latex-pseudo-objects
'(latex-math-block)
177 "List of pseudo-object types introduced in the back-end.")
181 ;;; User Configurable Variables
183 (defgroup org-export-latex nil
184 "Options for exporting Org mode files to LaTeX."
185 :tag
"Org Export LaTeX"
191 (defcustom org-latex-default-class
"article"
192 "The default LaTeX class."
193 :group
'org-export-latex
194 :type
'(string :tag
"LaTeX class"))
196 (defcustom org-latex-classes
198 "\\documentclass[11pt]{article}"
199 ("\\section{%s}" .
"\\section*{%s}")
200 ("\\subsection{%s}" .
"\\subsection*{%s}")
201 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
202 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
203 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
205 "\\documentclass[11pt]{report}"
206 ("\\part{%s}" .
"\\part*{%s}")
207 ("\\chapter{%s}" .
"\\chapter*{%s}")
208 ("\\section{%s}" .
"\\section*{%s}")
209 ("\\subsection{%s}" .
"\\subsection*{%s}")
210 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
212 "\\documentclass[11pt]{book}"
213 ("\\part{%s}" .
"\\part*{%s}")
214 ("\\chapter{%s}" .
"\\chapter*{%s}")
215 ("\\section{%s}" .
"\\section*{%s}")
216 ("\\subsection{%s}" .
"\\subsection*{%s}")
217 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
218 "Alist of LaTeX classes and associated header and structure.
219 If #+LATEX_CLASS is set in the buffer, use its value and the
220 associated information. Here is the structure of each cell:
224 \(numbered-section . unnumbered-section)
230 The HEADER-STRING is the header that will be inserted into the
231 LaTeX file. It should contain the \\documentclass macro, and
232 anything else that is needed for this setup. To this header, the
233 following commands will be added:
235 - Calls to \\usepackage for all packages mentioned in the
236 variables `org-latex-default-packages-alist' and
237 `org-latex-packages-alist'. Thus, your header definitions
238 should avoid to also request these packages.
240 - Lines specified via \"#+LATEX_HEADER:\" and
241 \"#+LATEX_HEADER_EXTRA:\" keywords.
243 If you need more control about the sequence in which the header
244 is built up, or if you want to exclude one of these building
245 blocks for a particular class, you can use the following
246 macro-like placeholders.
248 [DEFAULT-PACKAGES] \\usepackage statements for default packages
249 [NO-DEFAULT-PACKAGES] do not include any of the default packages
250 [PACKAGES] \\usepackage statements for packages
251 [NO-PACKAGES] do not include the packages
252 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
253 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
257 \\documentclass{article}
258 [NO-DEFAULT-PACKAGES]
260 \\providecommand{\\alert}[1]{\\textbf{#1}}
263 will omit the default packages, and will include the
264 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
265 to \\providecommand, and then place \\usepackage commands based
266 on the content of `org-latex-packages-alist'.
268 If your header, `org-latex-default-packages-alist' or
269 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
270 AUTO will automatically be replaced with a coding system derived
271 from `buffer-file-coding-system'. See also the variable
272 `org-latex-inputenc-alist' for a way to influence this mechanism.
274 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
275 AUTO will be replaced with the language related to the language
276 code specified by `org-export-default-language', which see. Note
277 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
280 The sectioning structure
281 ------------------------
283 The sectioning structure of the class is given by the elements
284 following the header string. For each sectioning level, a number
285 of strings is specified. A %s formatter is mandatory in each
286 section string and will be replaced by the title of the section.
288 Instead of a cons cell (numbered . unnumbered), you can also
289 provide a list of 2 or 4 elements,
291 \(numbered-open numbered-close)
295 \(numbered-open numbered-close unnumbered-open unnumbered-close)
297 providing opening and closing strings for a LaTeX environment
298 that should represent the document section. The opening clause
299 should have a %s to represent the section title.
301 Instead of a list of sectioning commands, you can also specify
302 a function name. That function will be called with two
303 parameters, the (reduced) level of the headline, and a predicate
304 non-nil when the headline should be numbered. It must return
305 a format string in which the section title will be added."
306 :group
'org-export-latex
308 (list (string :tag
"LaTeX class")
309 (string :tag
"LaTeX header")
310 (repeat :tag
"Levels" :inline t
313 (string :tag
" numbered")
314 (string :tag
"unnumbered"))
315 (list :tag
"Environment"
316 (string :tag
"Opening (numbered)")
317 (string :tag
"Closing (numbered)")
318 (string :tag
"Opening (unnumbered)")
319 (string :tag
"Closing (unnumbered)"))
320 (function :tag
"Hook computing sectioning"))))))
322 (defcustom org-latex-inputenc-alist nil
323 "Alist of inputenc coding system names, and what should really be used.
324 For example, adding an entry
326 (\"utf8\" . \"utf8x\")
328 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
329 are written as utf8 files."
330 :group
'org-export-latex
333 (string :tag
"Derived from buffer")
334 (string :tag
"Use this instead"))))
336 (defcustom org-latex-title-command
"\\maketitle"
337 "The command used to insert the title just after \\begin{document}.
338 If this string contains the formatting specification \"%s\" then
339 it will be used as a formatting string, passing the title as an
341 :group
'org-export-latex
344 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
345 "LaTeX command to set the table of contents, list of figures, etc.
346 This command only applies to the table of contents generated with
347 the toc:nil option, not to those generated with #+TOC keyword."
348 :group
'org-export-latex
351 (defcustom org-latex-hyperref-template
352 "\\hypersetup{\n pdfkeywords={%k},\n pdfsubject={%d},\n pdfcreator={%c}}\n"
353 "Template for hyperref package options.
355 Value is a format string, which can contain the following placeholders:
358 %d for DESCRIPTION line
361 Set it to the empty string to ignore the command completely."
362 :group
'org-export-latex
367 (defcustom org-latex-format-headline-function
368 'org-latex-format-headline-default-function
369 "Function for formatting the headline's text.
371 This function will be called with 5 arguments:
372 TODO the todo keyword (string or nil).
373 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
374 PRIORITY the priority of the headline (integer or nil)
375 TEXT the main headline text (string).
376 TAGS the tags as a list of strings (list of strings or nil).
378 The function result will be used in the section format string.
380 Use `org-latex-format-headline-default-function' by default,
381 which format headlines like for Org version prior to 8.0."
382 :group
'org-export-latex
384 :package-version
'(Org .
"8.0")
387 (defcustom org-latex-custom-id-as-label nil
388 "Toggle use of CUSTOM_ID properties for generating section labels.
390 When this variable is non-nil, Org will use the value of a
391 headline's CUSTOM_ID property as the key for the \\label command
392 for the LaTeX section corresponding to the headline.
394 By default, Org generates its own internal section labels for all
395 headlines during LaTeX export. This process ensures that the
396 \\label keys are unique and valid, but it means the keys are not
397 available in advance of the export process.
399 Setting this variable gives you control over how Org generates
400 labels for sections during LaTeX export, so that you may know
401 their keys in advance. One reason to do this is that it allows
402 you to refer to headlines using a single label both in Org's link
403 syntax and in embedded LaTeX code.
405 For example, when this variable is non-nil, a headline like this:
411 This is section [[#sec:foo]].
413 And this is still section \\ref{sec:foo}.
416 will be exported to LaTeX as:
418 \\subsection{Some section}
420 This is section \\ref{sec:foo}.
421 And this is still section \\ref{sec:foo}.
423 Note, however, that setting this variable introduces a limitation
424 on the possible values for CUSTOM_ID. When this variable is
425 non-nil and a headline defines a CUSTOM_ID value, Org simply
426 passes this value to \\label unchanged. You are responsible for
427 ensuring that the value is a valid LaTeX \\label key, and that no
428 other \\label commands with the same key appear elsewhere in your
429 document. (Keys may contain letters, numbers, and the following
430 punctuation: '_' '.' '-' ':'.) There are no such limitations on
431 CUSTOM_ID when this variable is nil.
433 For headlines that do not define the CUSTOM_ID property, Org will
434 continue to use its default labeling scheme to generate labels
435 and resolve links into section references."
436 :group
'org-export-latex
439 :package-version
'(Org .
"8.3"))
443 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
444 "Text used to separate footnotes."
445 :group
'org-export-latex
451 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
452 "A printf format string to be applied to active timestamps."
453 :group
'org-export-latex
456 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
457 "A printf format string to be applied to inactive timestamps."
458 :group
'org-export-latex
461 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
462 "A printf format string to be applied to diary timestamps."
463 :group
'org-export-latex
469 (defcustom org-latex-image-default-option
""
470 "Default option for images."
471 :group
'org-export-latex
473 :package-version
'(Org .
"8.0")
476 (defcustom org-latex-image-default-width
".9\\linewidth"
477 "Default width for images.
478 This value will not be used if a height is provided."
479 :group
'org-export-latex
481 :package-version
'(Org .
"8.0")
484 (defcustom org-latex-image-default-height
""
485 "Default height for images.
486 This value will not be used if a width is provided, or if the
487 image is wrapped within a \"figure\" or \"wrapfigure\"
489 :group
'org-export-latex
491 :package-version
'(Org .
"8.0")
494 (defcustom org-latex-default-figure-position
"htb"
495 "Default position for latex figures."
496 :group
'org-export-latex
499 (defcustom org-latex-inline-image-rules
500 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
501 "Rules characterizing image files that can be inlined into LaTeX.
503 A rule consists in an association whose key is the type of link
504 to consider, and value is a regexp that will be matched against
507 Note that, by default, the image extension *actually* allowed
508 depend on the way the LaTeX file is processed. When used with
509 pdflatex, pdf, jpg and png images are OK. When processing
510 through dvi to Postscript, only ps and eps are allowed. The
511 default we use here encompasses both."
512 :group
'org-export-latex
514 :package-version
'(Org .
"8.0")
515 :type
'(alist :key-type
(string :tag
"Type")
516 :value-type
(regexp :tag
"Path")))
518 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
519 "Format string for links with unknown path type."
520 :group
'org-export-latex
526 (defcustom org-latex-default-table-environment
"tabular"
527 "Default environment used to build tables."
528 :group
'org-export-latex
530 :package-version
'(Org .
"8.0")
533 (defcustom org-latex-default-table-mode
'table
534 "Default mode for tables.
536 Value can be a symbol among:
538 `table' Regular LaTeX table.
540 `math' In this mode, every cell is considered as being in math
541 mode and the complete table will be wrapped within a math
542 environment. It is particularly useful to write matrices.
544 `inline-math' This mode is almost the same as `math', but the
545 math environment will be inlined.
547 `verbatim' The table is exported as it appears in the Org
548 buffer, within a verbatim environment.
550 This value can be overridden locally with, i.e. \":mode math\" in
553 When modifying this variable, it may be useful to change
554 `org-latex-default-table-environment' accordingly."
555 :group
'org-export-latex
557 :package-version
'(Org .
"8.0")
558 :type
'(choice (const :tag
"Table" table
)
559 (const :tag
"Matrix" math
)
560 (const :tag
"Inline matrix" inline-math
)
561 (const :tag
"Verbatim" verbatim
))
562 :safe
(lambda (s) (memq s
'(table math inline-math verbatim
))))
564 (defcustom org-latex-tables-centered t
565 "When non-nil, tables are exported in a center environment."
566 :group
'org-export-latex
570 (defcustom org-latex-tables-booktabs nil
571 "When non-nil, display tables in a formal \"booktabs\" style.
572 This option assumes that the \"booktabs\" package is properly
573 loaded in the header of the document. This value can be ignored
574 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
576 :group
'org-export-latex
578 :package-version
'(Org .
"8.0")
582 (defcustom org-latex-table-caption-above t
583 "When non-nil, place caption string at the beginning of the table.
584 Otherwise, place it near the end."
585 :group
'org-export-latex
589 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
590 "Format string to display numbers in scientific notation.
591 The format should have \"%s\" twice, for mantissa and exponent
592 \(i.e., \"%s\\\\times10^{%s}\").
594 When nil, no transformation is made."
595 :group
'org-export-latex
597 :package-version
'(Org .
"8.0")
599 (string :tag
"Format string")
600 (const :tag
"No formatting")))
604 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
606 (italic .
"\\emph{%s}")
607 (strike-through .
"\\sout{%s}")
608 (underline .
"\\uline{%s}")
609 (verbatim . protectedtexttt
))
610 "Alist of LaTeX expressions to convert text markup.
612 The key must be a symbol among `bold', `code', `italic',
613 `strike-through', `underline' and `verbatim'. The value is
614 a formatting string to wrap fontified text with.
616 Value can also be set to the following symbols: `verb' and
617 `protectedtexttt'. For the former, Org will use \"\\verb\" to
618 create a format string and select a delimiter character that
619 isn't in the string. For the latter, Org will use \"\\texttt\"
620 to typeset and try to protect special characters.
622 If no association can be found for a given markup, text will be
624 :group
'org-export-latex
626 :options
'(bold code italic strike-through underline verbatim
))
631 (defcustom org-latex-format-drawer-function
632 (lambda (name contents
) contents
)
633 "Function called to format a drawer in LaTeX code.
635 The function must accept two parameters:
636 NAME the drawer name, like \"LOGBOOK\"
637 CONTENTS the contents of the drawer.
639 The function should return the string to be exported.
641 The default function simply returns the value of CONTENTS."
642 :group
'org-export-latex
644 :package-version
'(Org .
"8.3")
650 (defcustom org-latex-format-inlinetask-function
'ignore
651 "Function called to format an inlinetask in LaTeX code.
653 The function must accept six parameters:
654 TODO the todo keyword, as a string
655 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
656 PRIORITY the inlinetask priority, as a string
657 NAME the inlinetask name, as a string.
658 TAGS the inlinetask tags, as a list of strings.
659 CONTENTS the contents of the inlinetask, as a string.
661 The function should return the string to be exported.
663 For example, the variable could be set to the following function
664 in order to mimic default behaviour:
666 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
667 \"Format an inline task element for LaTeX export.\"
671 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
672 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
675 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
676 \(mapconcat 'identity tags \":\")))))
677 \(format (concat \"\\\\begin{center}\\n\"
679 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
681 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
683 \"\\\\end{minipage}}\"
685 full-title contents))"
686 :group
'org-export-latex
692 (defcustom org-latex-listings nil
693 "Non-nil means export source code using the listings package.
695 This package will fontify source code, possibly even with color.
696 If you want to use this, you also need to make LaTeX use the
697 listings package, and if you want to have color, the color
698 package. Just add these to `org-latex-packages-alist', for
699 example using customize, or with something like:
702 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
703 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
707 \(setq org-latex-listings 'minted)
709 causes source code to be exported using the minted package as
710 opposed to listings. If you want to use minted, you need to add
711 the minted package to `org-latex-packages-alist', for example
712 using customize, or with
715 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
717 In addition, it is necessary to install pygments
718 \(http://pygments.org), and to configure the variable
719 `org-latex-pdf-process' so that the -shell-escape option is
722 The minted choice has possible repercussions on the preview of
723 latex fragments (see `org-preview-latex-fragment'). If you run
724 into previewing problems, please consult
726 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
727 :group
'org-export-latex
729 (const :tag
"Use listings" t
)
730 (const :tag
"Use minted" minted
)
731 (const :tag
"Export verbatim" nil
))
732 :safe
(lambda (s) (memq s
'(t nil minted
))))
734 (defcustom org-latex-listings-langs
735 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
738 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
739 (html "HTML") (xml "XML")
740 (tex "TeX") (latex "[LaTeX]TeX")
741 (shell-script "bash")
743 (ocaml "Caml") (caml "Caml")
744 (sql "SQL") (sqlite "sql"))
745 "Alist mapping languages to their listing language counterpart.
746 The key is a symbol, the major mode symbol without the \"-mode\".
747 The value is the string that should be inserted as the language
748 parameter for the listings package. If the mode name and the
749 listings name are the same, the language does not need an entry
750 in this list - but it does not hurt if it is present."
751 :group
'org-export-latex
754 (symbol :tag
"Major mode ")
755 (string :tag
"Listings language"))))
757 (defcustom org-latex-listings-options nil
758 "Association list of options for the latex listings package.
760 These options are supplied as a comma-separated list to the
761 \\lstset command. Each element of the association list should be
762 a list containing two strings: the name of the option, and the
765 (setq org-latex-listings-options
766 '((\"basicstyle\" \"\\\\small\")
767 (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
769 will typeset the code in a small size font with underlined, bold
772 Note that the same options will be applied to blocks of all
774 :group
'org-export-latex
777 (string :tag
"Listings option name ")
778 (string :tag
"Listings option value"))))
780 (defcustom org-latex-minted-langs
781 '((emacs-lisp "common-lisp")
784 (shell-script "bash")
786 "Alist mapping languages to their minted language counterpart.
787 The key is a symbol, the major mode symbol without the \"-mode\".
788 The value is the string that should be inserted as the language
789 parameter for the minted package. If the mode name and the
790 listings name are the same, the language does not need an entry
791 in this list - but it does not hurt if it is present.
793 Note that minted uses all lower case for language identifiers,
794 and that the full list of language identifiers can be obtained
797 pygmentize -L lexers"
798 :group
'org-export-latex
801 (symbol :tag
"Major mode ")
802 (string :tag
"Minted language"))))
804 (defcustom org-latex-minted-options nil
805 "Association list of options for the latex minted package.
807 These options are supplied within square brackets in
808 \\begin{minted} environments. Each element of the alist should
809 be a list containing two strings: the name of the option, and the
812 \(setq org-latex-minted-options
813 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
815 will result in src blocks being exported with
817 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
819 as the start of the minted environment. Note that the same
820 options will be applied to blocks of all languages."
821 :group
'org-export-latex
824 (string :tag
"Minted option name ")
825 (string :tag
"Minted option value"))))
827 (defvar org-latex-custom-lang-environments nil
828 "Alist mapping languages to language-specific LaTeX environments.
830 It is used during export of src blocks by the listings and minted
831 latex packages. For example,
833 \(setq org-latex-custom-lang-environments
834 '\(\(python \"pythoncode\"\)\)\)
836 would have the effect that if org encounters begin_src python
837 during latex export it will output
846 (defcustom org-latex-pdf-process
847 '("pdflatex -interaction nonstopmode -output-directory %o %f"
848 "pdflatex -interaction nonstopmode -output-directory %o %f"
849 "pdflatex -interaction nonstopmode -output-directory %o %f")
850 "Commands to process a LaTeX file to a PDF file.
851 This is a list of strings, each of them will be given to the
852 shell as a command. %f in the command will be replaced by the
853 full file name, %b by the file base name (i.e. without directory
854 and extension parts) and %o by the base directory of the file.
856 The reason why this is a list is that it usually takes several
857 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
858 does not have a clever mechanism to detect which of these
859 commands have to be run to get to a stable result, and it also
860 does not do any error checking.
862 By default, Org uses 3 runs of `pdflatex' to do the processing.
863 If you have texi2dvi on your system and if that does not cause
864 the infamous egrep/locale bug:
866 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
868 then `texi2dvi' is the superior choice as it automates the LaTeX
869 build process by calling the \"correct\" combinations of
870 auxiliary programs. Org does offer `texi2dvi' as one of the
871 customize options. Alternatively, `rubber' and `latexmk' also
872 provide similar functionality. The latter supports `biber' out
875 Alternatively, this may be a Lisp function that does the
876 processing, so you could use this to apply the machinery of
877 AUCTeX or the Emacs LaTeX mode. This function should accept the
878 file name as its single argument."
879 :group
'org-export-pdf
881 (repeat :tag
"Shell command sequence"
882 (string :tag
"Shell command"))
883 (const :tag
"2 runs of pdflatex"
884 ("pdflatex -interaction nonstopmode -output-directory %o %f"
885 "pdflatex -interaction nonstopmode -output-directory %o %f"))
886 (const :tag
"3 runs of pdflatex"
887 ("pdflatex -interaction nonstopmode -output-directory %o %f"
888 "pdflatex -interaction nonstopmode -output-directory %o %f"
889 "pdflatex -interaction nonstopmode -output-directory %o %f"))
890 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
891 ("pdflatex -interaction nonstopmode -output-directory %o %f"
893 "pdflatex -interaction nonstopmode -output-directory %o %f"
894 "pdflatex -interaction nonstopmode -output-directory %o %f"))
895 (const :tag
"2 runs of xelatex"
896 ("xelatex -interaction nonstopmode -output-directory %o %f"
897 "xelatex -interaction nonstopmode -output-directory %o %f"))
898 (const :tag
"3 runs of xelatex"
899 ("xelatex -interaction nonstopmode -output-directory %o %f"
900 "xelatex -interaction nonstopmode -output-directory %o %f"
901 "xelatex -interaction nonstopmode -output-directory %o %f"))
902 (const :tag
"xelatex,bibtex,xelatex,xelatex"
903 ("xelatex -interaction nonstopmode -output-directory %o %f"
905 "xelatex -interaction nonstopmode -output-directory %o %f"
906 "xelatex -interaction nonstopmode -output-directory %o %f"))
907 (const :tag
"texi2dvi"
908 ("texi2dvi -p -b -V %f"))
910 ("rubber -d --into %o %f"))
911 (const :tag
"latexmk"
912 ("latexmk -g -pdf %f"))
915 (defcustom org-latex-logfiles-extensions
916 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
917 "The list of file extensions to consider as LaTeX logfiles.
918 The logfiles will be remove if `org-latex-remove-logfiles' is
920 :group
'org-export-latex
921 :type
'(repeat (string :tag
"Extension")))
923 (defcustom org-latex-remove-logfiles t
924 "Non-nil means remove the logfiles produced by PDF production.
925 By default, logfiles are files with these extensions: .aux, .idx,
926 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
927 logfiles to remove, set `org-latex-logfiles-extensions'."
928 :group
'org-export-latex
931 (defcustom org-latex-known-errors
932 '(("Reference.*?undefined" .
"[undefined reference]")
933 ("Citation.*?undefined" .
"[undefined citation]")
934 ("Undefined control sequence" .
"[undefined control sequence]")
935 ("^! LaTeX.*?Error" .
"[LaTeX error]")
936 ("^! Package.*?Error" .
"[package error]")
937 ("Runaway argument" .
"Runaway argument"))
938 "Alist of regular expressions and associated messages for the user.
939 The regular expressions are used to find possible errors in the
941 :group
'org-export-latex
943 :package-version
'(Org .
"8.0")
946 (string :tag
"Regexp")
947 (string :tag
"Message"))))
951 ;;; Internal Functions
953 (defun org-latex--caption/label-string
(element info
)
954 "Return caption and label LaTeX string for ELEMENT.
956 INFO is a plist holding contextual information. If there's no
957 caption nor label, return the empty string.
959 For non-floats, see `org-latex--wrap-label'."
960 (let* ((label (org-element-property :name element
))
961 (label-str (if (not (org-string-nw-p label
)) ""
962 (format "\\label{%s}"
963 (org-export-solidify-link-text label
))))
964 (main (org-export-get-caption element
))
965 (short (org-export-get-caption element t
))
966 (caption-from-attr-latex (org-export-read-attribute :attr_latex element
:caption
)))
968 ((org-string-nw-p caption-from-attr-latex
)
969 (concat caption-from-attr-latex
"\n"))
970 ((and (not main
) (equal label-str
"")) "")
971 ((not main
) (concat label-str
"\n"))
972 ;; Option caption format with short name.
973 (short (format "\\caption[%s]{%s%s}\n"
974 (org-export-data short info
)
976 (org-export-data main info
)))
977 ;; Standard caption format.
978 (t (format "\\caption{%s%s}\n" label-str
(org-export-data main info
))))))
980 (defun org-latex-guess-inputenc (header)
981 "Set the coding system in inputenc to what the buffer is.
983 HEADER is the LaTeX header string. This function only applies
984 when specified inputenc option is \"AUTO\".
986 Return the new header, as a string."
987 (let* ((cs (or (ignore-errors
988 (latexenc-coding-system-to-inputenc
989 (or org-export-coding-system buffer-file-coding-system
)))
992 ;; First translate if that is requested.
993 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
994 ;; Then find the \usepackage statement and replace the option.
995 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
996 cs header t nil
1))))
998 (defun org-latex-guess-babel-language (header info
)
999 "Set Babel's language according to LANGUAGE keyword.
1001 HEADER is the LaTeX header string. INFO is the plist used as
1002 a communication channel.
1004 Insertion of guessed language only happens when Babel package has
1005 explicitly been loaded. Then it is added to the rest of
1008 The argument to Babel may be \"AUTO\" which is then replaced with
1009 the language of the document or `org-export-default-language'
1010 unless language in question is already loaded.
1012 Return the new header."
1013 (let ((language-code (plist-get info
:language
)))
1014 ;; If no language is set or Babel package is not loaded, return
1016 (if (or (not (stringp language-code
))
1017 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
1019 (let ((options (save-match-data
1020 (org-split-string (match-string 1 header
) ",[ \t]*")))
1021 (language (cdr (assoc language-code
1022 org-latex-babel-language-alist
))))
1023 ;; If LANGUAGE is already loaded, return header without AUTO.
1024 ;; Otherwise, replace AUTO with language or append language if
1025 ;; AUTO is not present.
1027 (mapconcat (lambda (option) (if (equal "AUTO" option
) language option
))
1028 (cond ((member language options
) (delete "AUTO" options
))
1029 ((member "AUTO" options
) options
)
1030 (t (append options
(list language
))))
1034 (defun org-latex--find-verb-separator (s)
1035 "Return a character not used in string S.
1036 This is used to choose a separator for constructs like \\verb."
1037 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1038 (loop for c across ll
1039 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1040 return
(char-to-string c
))))
1042 (defun org-latex--make-option-string (options)
1043 "Return a comma separated string of keywords and values.
1044 OPTIONS is an alist where the key is the options keyword as
1045 a string, and the value a list containing the keyword value, or
1047 (mapconcat (lambda (pair)
1048 (concat (first pair
)
1049 (when (> (length (second pair
)) 0)
1050 (concat "=" (second pair
)))))
1054 (defun org-latex--wrap-label (element output
)
1055 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1056 This function shouldn't be used for floats. See
1057 `org-latex--caption/label-string'."
1058 (let ((label (org-element-property :name element
)))
1059 (if (not (and (org-string-nw-p output
) (org-string-nw-p label
))) output
1060 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label
))
1063 (defun org-latex--text-markup (text markup
)
1064 "Format TEXT depending on MARKUP text markup.
1065 See `org-latex-text-markup-alist' for details."
1066 (let ((fmt (cdr (assq markup org-latex-text-markup-alist
))))
1068 ;; No format string: Return raw text.
1070 ;; Handle the `verb' special case: Find an appropriate separator
1071 ;; and use "\\verb" command.
1073 (let ((separator (org-latex--find-verb-separator text
)))
1074 (concat "\\verb" separator
1075 (replace-regexp-in-string "\n" " " text
)
1077 ;; Handle the `protectedtexttt' special case: Protect some
1078 ;; special chars and use "\texttt{%s}" format string.
1079 ((eq 'protectedtexttt fmt
)
1081 (trans '(("\\" .
"\\textbackslash{}")
1082 ("~" .
"\\textasciitilde{}")
1083 ("^" .
"\\textasciicircum{}")))
1086 (while (string-match "[\\{}$%&_#~^]" text
)
1087 (setq char
(match-string 0 text
))
1088 (if (> (match-beginning 0) 0)
1089 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
1090 (setq text
(substring text
(1+ (match-beginning 0))))
1091 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1092 rtn
(concat rtn char
)))
1093 (setq text
(concat rtn text
)
1095 (while (string-match "--" text
)
1096 (setq text
(replace-match "-{}-" t t text
)))
1098 ;; Else use format string.
1099 (t (format fmt text
)))))
1101 (defun org-latex--delayed-footnotes-definitions (element info
)
1102 "Return footnotes definitions in ELEMENT as a string.
1104 INFO is a plist used as a communication channel.
1106 Footnotes definitions are returned within \"\\footnotetxt{}\"
1109 This function is used within constructs that don't support
1110 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1111 \"\\footnotemark\" is used within the construct and the function
1112 just outside of it."
1116 "\\footnotetext[%s]{%s}"
1117 (org-export-get-footnote-number ref info
)
1120 (org-export-get-footnote-definition ref info
) info
))))
1121 ;; Find every footnote reference in ELEMENT.
1123 search-refs
; For byte-compiler.
1127 ;; Return a list of all footnote references never seen
1129 (org-element-map data
'footnote-reference
1131 (when (org-export-footnote-first-reference-p ref info
)
1133 (when (eq (org-element-property :type ref
) 'standard
)
1134 (funcall search-refs
1135 (org-export-get-footnote-definition ref info
)))))
1137 (reverse all-refs
)))))
1138 (funcall search-refs element
))
1141 (defun org-latex--translate (s info
)
1142 "Translate string S according to specified language.
1143 INFO is a plist used as a communication channel."
1144 (org-export-translate s
:latex info
))
1150 (defun org-latex-template (contents info
)
1151 "Return complete document string after LaTeX conversion.
1152 CONTENTS is the transcoded contents string. INFO is a plist
1153 holding export options."
1154 (let ((title (org-export-data (plist-get info
:title
) info
)))
1157 (and (plist-get info
:time-stamp-file
)
1158 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1159 ;; Document class and packages.
1160 (let* ((class (plist-get info
:latex-class
))
1161 (class-options (plist-get info
:latex-class-options
))
1162 (header (nth 1 (assoc class org-latex-classes
)))
1163 (document-class-string
1164 (and (stringp header
)
1165 (if (not class-options
) header
1166 (replace-regexp-in-string
1167 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1168 class-options header t nil
1)))))
1169 (if (not document-class-string
)
1170 (user-error "Unknown LaTeX class `%s'" class
)
1171 (org-latex-guess-babel-language
1172 (org-latex-guess-inputenc
1173 (org-element-normalize-string
1174 (org-splice-latex-header
1175 document-class-string
1176 org-latex-default-packages-alist
1177 org-latex-packages-alist nil
1178 (concat (org-element-normalize-string
1179 (plist-get info
:latex-header
))
1180 (plist-get info
:latex-header-extra
)))))
1182 ;; Possibly limit depth for headline numbering.
1183 (let ((sec-num (plist-get info
:section-numbers
)))
1184 (when (integerp sec-num
)
1185 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1187 (let ((author (and (plist-get info
:with-author
)
1188 (let ((auth (plist-get info
:author
)))
1189 (and auth
(org-export-data auth info
)))))
1190 (email (and (plist-get info
:with-email
)
1191 (org-export-data (plist-get info
:email
) info
))))
1192 (cond ((and author email
(not (string= "" email
)))
1193 (format "\\author{%s\\thanks{%s}}\n" author email
))
1194 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1196 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1197 (format "\\date{%s}\n" (org-export-data date info
)))
1199 (format "\\title{%s}\n" title
)
1200 ;; Hyperref options.
1201 (format-spec (plist-get info
:latex-hyperref
)
1203 ?k
(or (plist-get info
:keywords
) "")
1204 ?d
(or (plist-get info
:description
)"")
1205 ?c
(if (plist-get info
:with-creator
)
1206 (plist-get info
:creator
)
1209 "\\begin{document}\n\n"
1211 (org-element-normalize-string
1212 (cond ((string= "" title
) nil
)
1213 ((not (stringp org-latex-title-command
)) nil
)
1214 ((string-match "\\(?:[^%]\\|^\\)%s"
1215 org-latex-title-command
)
1216 (format org-latex-title-command title
))
1217 (t org-latex-title-command
)))
1218 ;; Table of contents.
1219 (let ((depth (plist-get info
:with-toc
)))
1221 (concat (when (wholenump depth
)
1222 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1223 org-latex-toc-command
)))
1227 (let ((creator-info (plist-get info
:with-creator
)))
1229 ((not creator-info
) "")
1230 ((eq creator-info
'comment
)
1231 (format "%% %s\n" (plist-get info
:creator
)))
1232 (t (concat (plist-get info
:creator
) "\n"))))
1234 "\\end{document}")))
1238 ;;; Transcode Functions
1242 (defun org-latex-bold (bold contents info
)
1243 "Transcode BOLD from Org to LaTeX.
1244 CONTENTS is the text with bold markup. INFO is a plist holding
1245 contextual information."
1246 (org-latex--text-markup contents
'bold
))
1251 (defun org-latex-center-block (center-block contents info
)
1252 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1253 CONTENTS holds the contents of the center block. INFO is a plist
1254 holding contextual information."
1255 (org-latex--wrap-label
1257 (format "\\begin{center}\n%s\\end{center}" contents
)))
1262 (defun org-latex-clock (clock contents info
)
1263 "Transcode a CLOCK element from Org to LaTeX.
1264 CONTENTS is nil. INFO is a plist holding contextual
1268 (format "\\textbf{%s} " org-clock-string
)
1269 (format org-latex-inactive-timestamp-format
1270 (concat (org-translate-time
1271 (org-element-property :raw-value
1272 (org-element-property :value clock
)))
1273 (let ((time (org-element-property :duration clock
)))
1274 (and time
(format " (%s)" time
)))))
1280 (defun org-latex-code (code contents info
)
1281 "Transcode a CODE object from Org to LaTeX.
1282 CONTENTS is nil. INFO is a plist used as a communication
1284 (org-latex--text-markup (org-element-property :value code
) 'code
))
1289 (defun org-latex-drawer (drawer contents info
)
1290 "Transcode a DRAWER element from Org to LaTeX.
1291 CONTENTS holds the contents of the block. INFO is a plist
1292 holding contextual information."
1293 (let* ((name (org-element-property :drawer-name drawer
))
1294 (output (funcall org-latex-format-drawer-function
1296 (org-latex--wrap-label drawer output
)))
1301 (defun org-latex-dynamic-block (dynamic-block contents info
)
1302 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1303 CONTENTS holds the contents of the block. INFO is a plist
1304 holding contextual information. See `org-export-data'."
1305 (org-latex--wrap-label dynamic-block contents
))
1310 (defun org-latex-entity (entity contents info
)
1311 "Transcode an ENTITY object from Org to LaTeX.
1312 CONTENTS are the definition itself. INFO is a plist holding
1313 contextual information."
1314 (org-element-property :latex entity
))
1319 (defun org-latex-example-block (example-block contents info
)
1320 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1321 CONTENTS is nil. INFO is a plist holding contextual
1323 (when (org-string-nw-p (org-element-property :value example-block
))
1324 (org-latex--wrap-label
1326 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1327 (org-export-format-code-default example-block info
)))))
1332 (defun org-latex-export-block (export-block contents info
)
1333 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1334 CONTENTS is nil. INFO is a plist holding contextual information."
1335 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1336 (org-remove-indentation (org-element-property :value export-block
))))
1341 (defun org-latex-export-snippet (export-snippet contents info
)
1342 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1343 CONTENTS is nil. INFO is a plist holding contextual information."
1344 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1345 (org-element-property :value export-snippet
)))
1350 (defun org-latex-fixed-width (fixed-width contents info
)
1351 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1352 CONTENTS is nil. INFO is a plist holding contextual information."
1353 (org-latex--wrap-label
1355 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1356 (org-remove-indentation
1357 (org-element-property :value fixed-width
)))))
1360 ;;;; Footnote Reference
1362 (defun org-latex-footnote-reference (footnote-reference contents info
)
1363 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1364 CONTENTS is nil. INFO is a plist holding contextual information."
1366 ;; Insert separator between two footnotes in a row.
1367 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1368 (when (eq (org-element-type prev
) 'footnote-reference
)
1369 org-latex-footnote-separator
))
1371 ;; Use \footnotemark if the footnote has already been defined.
1372 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1373 (format "\\footnotemark[%s]{}"
1374 (org-export-get-footnote-number footnote-reference info
)))
1375 ;; Use \footnotemark if reference is within another footnote
1376 ;; reference, footnote definition or table cell.
1377 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1378 thereis
(memq (org-element-type parent
)
1379 '(footnote-reference footnote-definition table-cell
)))
1381 ;; Otherwise, define it with \footnote command.
1383 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1385 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1386 ;; Retrieve all footnote references within the footnote and
1387 ;; add their definition after it, since LaTeX doesn't support
1389 (org-latex--delayed-footnotes-definitions def info
)))))))
1394 (defun org-latex-headline (headline contents info
)
1395 "Transcode a HEADLINE element from Org to LaTeX.
1396 CONTENTS holds the contents of the headline. INFO is a plist
1397 holding contextual information."
1398 (unless (org-element-property :footnote-section-p headline
)
1399 (let* ((class (plist-get info
:latex-class
))
1400 (level (org-export-get-relative-level headline info
))
1401 (numberedp (org-export-numbered-headline-p headline info
))
1402 (class-sectioning (assoc class org-latex-classes
))
1403 ;; Section formatting will set two placeholders: one for
1404 ;; the title and the other for the contents.
1406 (let ((sec (if (functionp (nth 2 class-sectioning
))
1407 (funcall (nth 2 class-sectioning
) level numberedp
)
1408 (nth (1+ level
) class-sectioning
))))
1410 ;; No section available for that LEVEL.
1412 ;; Section format directly returned by a function. Add
1413 ;; placeholder for contents.
1414 ((stringp sec
) (concat sec
"\n%s"))
1415 ;; (numbered-section . unnumbered-section)
1416 ((not (consp (cdr sec
)))
1417 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1418 ;; (numbered-open numbered-close)
1420 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1421 ;; (num-in num-out no-num-in no-num-out)
1423 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1424 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1425 ;; Create a temporary export back-end that hard-codes
1426 ;; "\underline" within "\section" and alike.
1428 (org-export-create-backend
1431 '((underline .
(lambda (o c i
) (format "\\underline{%s}" c
))))))
1433 (org-export-data-with-backend
1434 (org-element-property :title headline
) section-back-end info
))
1436 (and (plist-get info
:with-todo-keywords
)
1437 (let ((todo (org-element-property :todo-keyword headline
)))
1438 (and todo
(org-export-data todo info
)))))
1439 (todo-type (and todo
(org-element-property :todo-type headline
)))
1440 (tags (and (plist-get info
:with-tags
)
1441 (org-export-get-tags headline info
)))
1442 (priority (and (plist-get info
:with-priority
)
1443 (org-element-property :priority headline
)))
1444 ;; Create the headline text along with a no-tag version.
1445 ;; The latter is required to remove tags from toc.
1446 (full-text (funcall org-latex-format-headline-function
1447 todo todo-type priority text tags
))
1448 ;; Associate \label to the headline for internal links.
1451 (and (plist-get info
:latex-custom-id-labels
)
1452 (org-element-property :CUSTOM_ID headline
))))
1453 (if custom-label
(format "\\label{%s}\n" custom-label
)
1454 (format "\\label{sec-%s}\n"
1457 (org-export-get-headline-number headline info
)
1460 (make-string (org-element-property :pre-blank headline
) 10)))
1461 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1462 ;; This is a deep sub-tree: export it as a list item. Also
1463 ;; export as items headlines for which no section format has
1465 (let ((low-level-body
1467 ;; If headline is the first sibling, start a list.
1468 (when (org-export-first-sibling-p headline info
)
1469 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1472 (and full-text
(org-string-match-p "\\`[ \t]*\\[" full-text
)
1478 ;; If headline is not the last sibling simply return
1479 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1481 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1482 (replace-regexp-in-string
1484 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1486 ;; This is a standard headline. Export it as a section. Add
1487 ;; an alternative heading when possible, and when this is not
1488 ;; identical to the usual heading.
1490 (funcall org-latex-format-headline-function
1491 todo todo-type priority
1492 (org-export-data-with-backend
1493 (org-export-get-alt-title headline info
)
1494 section-back-end info
)
1495 (and (eq (plist-get info
:with-tags
) t
) tags
))))
1496 (if (and numberedp opt-title
1497 (not (equal opt-title full-text
))
1498 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt
))
1499 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
1500 ;; Replace square brackets with parenthesis
1501 ;; since square brackets are not supported in
1502 ;; optional arguments.
1503 (replace-regexp-in-string
1504 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
1506 (concat headline-label pre-blanks contents
))
1507 ;; Impossible to add an alternative heading. Fallback to
1508 ;; regular sectioning format string.
1509 (format section-fmt full-text
1510 (concat headline-label pre-blanks contents
))))))))
1512 (defun org-latex-format-headline-default-function
1513 (todo todo-type priority text tags
)
1514 "Default format function for a headline.
1515 See `org-latex-format-headline-function' for details."
1517 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
1518 (and priority
(format "\\framebox{\\#%c} " priority
))
1521 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags
":")))))
1524 ;;;; Horizontal Rule
1526 (defun org-latex-horizontal-rule (horizontal-rule contents info
)
1527 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1528 CONTENTS is nil. INFO is a plist holding contextual information."
1529 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
1530 (prev (org-export-get-previous-element horizontal-rule info
)))
1532 ;; Make sure the rule doesn't start at the end of the current
1533 ;; line by separating it with a blank line from previous element.
1535 (let ((prev-blank (org-element-property :post-blank prev
)))
1536 (or (not prev-blank
) (zerop prev-blank
))))
1538 (org-latex--wrap-label
1540 (format "\\rule{%s}{%s}"
1541 (or (plist-get attr
:width
) "\\linewidth")
1542 (or (plist-get attr
:thickness
) "0.5pt"))))))
1545 ;;;; Inline Src Block
1547 (defun org-latex-inline-src-block (inline-src-block contents info
)
1548 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1549 CONTENTS holds the contents of the item. INFO is a plist holding
1550 contextual information."
1551 (let* ((code (org-element-property :value inline-src-block
))
1552 (separator (org-latex--find-verb-separator code
)))
1554 ;; Do not use a special package: transcode it verbatim.
1555 ((not org-latex-listings
)
1556 (concat "\\verb" separator code separator
))
1557 ;; Use minted package.
1558 ((eq org-latex-listings
'minted
)
1559 (let* ((org-lang (org-element-property :language inline-src-block
))
1560 (mint-lang (or (cadr (assq (intern org-lang
)
1561 org-latex-minted-langs
))
1563 (options (org-latex--make-option-string
1564 org-latex-minted-options
)))
1565 (concat (format "\\mint%s{%s}"
1566 (if (string= options
"") "" (format "[%s]" options
))
1568 separator code separator
)))
1569 ;; Use listings package.
1571 ;; Maybe translate language's name.
1572 (let* ((org-lang (org-element-property :language inline-src-block
))
1573 (lst-lang (or (cadr (assq (intern org-lang
)
1574 org-latex-listings-langs
))
1576 (options (org-latex--make-option-string
1577 (append org-latex-listings-options
1578 `(("language" ,lst-lang
))))))
1579 (concat (format "\\lstinline[%s]" options
)
1580 separator code separator
))))))
1585 (defun org-latex-inlinetask (inlinetask contents info
)
1586 "Transcode an INLINETASK element from Org to LaTeX.
1587 CONTENTS holds the contents of the block. INFO is a plist
1588 holding contextual information."
1589 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1590 (todo (and (plist-get info
:with-todo-keywords
)
1591 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1592 (and todo
(org-export-data todo info
)))))
1593 (todo-type (org-element-property :todo-type inlinetask
))
1594 (tags (and (plist-get info
:with-tags
)
1595 (org-export-get-tags inlinetask info
)))
1596 (priority (and (plist-get info
:with-priority
)
1597 (org-element-property :priority inlinetask
))))
1598 ;; If `org-latex-format-inlinetask-function' is provided, call it
1599 ;; with appropriate arguments.
1600 (if (not (eq org-latex-format-inlinetask-function
'ignore
))
1601 (funcall org-latex-format-inlinetask-function
1602 todo todo-type priority title tags contents
)
1603 ;; Otherwise, use a default template.
1604 (org-latex--wrap-label
1608 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1609 (when priority
(format "\\framebox{\\#%c} " priority
))
1611 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1612 (mapconcat 'identity tags
":"))))))
1613 (format (concat "\\begin{center}\n"
1615 "\\begin{minipage}[c]{.6\\textwidth}\n"
1617 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1622 full-title contents
))))))
1627 (defun org-latex-italic (italic contents info
)
1628 "Transcode ITALIC from Org to LaTeX.
1629 CONTENTS is the text with italic markup. INFO is a plist holding
1630 contextual information."
1631 (org-latex--text-markup contents
'italic
))
1636 (defun org-latex-item (item contents info
)
1637 "Transcode an ITEM element from Org to LaTeX.
1638 CONTENTS holds the contents of the item. INFO is a plist holding
1639 contextual information."
1641 (let ((count (org-element-property :counter item
))
1643 ;; Determine level of current item to determine the
1644 ;; correct LaTeX counter to use (enumi, enumii...).
1645 (let ((parent item
) (level 0))
1646 (while (memq (org-element-type
1647 (setq parent
(org-export-get-parent parent
)))
1649 (when (and (eq (org-element-type parent
) 'plain-list
)
1650 (eq (org-element-property :type parent
)
1656 (format "\\setcounter{enum%s}{%s}\n"
1657 (nth (1- level
) '("i" "ii" "iii" "iv"))
1659 (checkbox (case (org-element-property :checkbox item
)
1660 (on "$\\boxtimes$ ")
1662 (trans "$\\boxminus$ ")))
1663 (tag (let ((tag (org-element-property :tag item
)))
1664 ;; Check-boxes must belong to the tag.
1665 (and tag
(format "[{%s}] "
1667 (org-export-data tag info
)))))))
1672 (checkbox (concat " " checkbox
))
1673 ;; Without a tag or a check-box, if CONTENTS starts with
1674 ;; an opening square bracket, add "\relax" to "\item",
1675 ;; unless the brackets comes from an initial export
1676 ;; snippet (i.e. it is inserted willingly by the user).
1678 (org-string-match-p "\\`[ \t]*\\[" contents
)
1679 (not (let ((e (car (org-element-contents item
))))
1680 (and (eq (org-element-type e
) 'paragraph
)
1681 (let ((o (car (org-element-contents e
))))
1682 (and (eq (org-element-type o
) 'export-snippet
)
1683 (eq (org-export-snippet-backend o
)
1687 (and contents
(org-trim contents
))
1688 ;; If there are footnotes references in tag, be sure to
1689 ;; add their definition at the end of the item. This
1690 ;; workaround is necessary since "\footnote{}" command is
1691 ;; not supported in tags.
1693 (org-latex--delayed-footnotes-definitions
1694 (org-element-property :tag item
) info
)))))
1699 (defun org-latex-keyword (keyword contents info
)
1700 "Transcode a KEYWORD element from Org to LaTeX.
1701 CONTENTS is nil. INFO is a plist holding contextual information."
1702 (let ((key (org-element-property :key keyword
))
1703 (value (org-element-property :value keyword
)))
1705 ((string= key
"LATEX") value
)
1706 ((string= key
"INDEX") (format "\\index{%s}" value
))
1707 ((string= key
"TOC")
1708 (let ((value (downcase value
)))
1710 ((string-match "\\<headlines\\>" value
)
1711 (let ((depth (or (and (string-match "[0-9]+" value
)
1712 (string-to-number (match-string 0 value
)))
1713 (plist-get info
:with-toc
))))
1715 (when (wholenump depth
)
1716 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1717 "\\tableofcontents")))
1718 ((string= "tables" value
) "\\listoftables")
1719 ((string= "listings" value
)
1721 ((eq org-latex-listings
'minted
) "\\listoflistings")
1722 (org-latex-listings "\\lstlistoflistings")
1723 ;; At the moment, src blocks with a caption are wrapped
1724 ;; into a figure environment.
1725 (t "\\listoffigures")))))))))
1728 ;;;; Latex Environment
1730 (defun org-latex-latex-environment (latex-environment contents info
)
1731 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1732 CONTENTS is nil. INFO is a plist holding contextual information."
1733 (when (plist-get info
:with-latex
)
1734 (let ((label (org-element-property :name latex-environment
))
1735 (value (org-remove-indentation
1736 (org-element-property :value latex-environment
))))
1737 (if (not (org-string-nw-p label
)) value
1738 ;; Environment is labeled: label must be within the environment
1739 ;; (otherwise, a reference pointing to that element will count
1740 ;; the section instead).
1743 (goto-char (point-min))
1746 (format "\\label{%s}\n" (org-export-solidify-link-text label
)))
1747 (buffer-string))))))
1752 (defun org-latex-latex-fragment (latex-fragment contents info
)
1753 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1754 CONTENTS is nil. INFO is a plist holding contextual information."
1755 (let ((value (org-element-property :value latex-fragment
)))
1756 ;; Trim math markers since the fragment is enclosed within
1757 ;; a latex-math-block object anyway.
1758 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value
)
1759 (match-string 2 value
))
1760 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value
)
1761 (match-string 1 value
))
1767 (defun org-latex-line-break (line-break contents info
)
1768 "Transcode a LINE-BREAK object from Org to LaTeX.
1769 CONTENTS is nil. INFO is a plist holding contextual information."
1775 (defun org-latex--inline-image (link info
)
1776 "Return LaTeX code for an inline image.
1777 LINK is the link pointing to the inline image. INFO is a plist
1778 used as a communication channel."
1779 (let* ((parent (org-export-get-parent-element link
))
1780 (path (let ((raw-path (org-element-property :path link
)))
1781 (if (not (file-name-absolute-p raw-path
)) raw-path
1782 (expand-file-name raw-path
))))
1783 (filetype (file-name-extension path
))
1784 (caption (org-latex--caption/label-string parent info
))
1785 ;; Retrieve latex attributes from the element around.
1786 (attr (org-export-read-attribute :attr_latex parent
))
1787 (float (let ((float (plist-get attr
:float
)))
1788 (cond ((and (not float
) (plist-member attr
:float
)) nil
)
1789 ((string= float
"wrap") 'wrap
)
1790 ((string= float
"multicolumn") 'multicolumn
)
1792 (org-element-property :caption parent
)
1793 (org-string-nw-p (plist-get attr
:caption
)))
1796 (let ((place (plist-get attr
:placement
)))
1797 (cond (place (format "%s" place
))
1798 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
1800 (format "[%s]" org-latex-default-figure-position
))
1802 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
1803 ;; It is possible to specify width and height in the
1804 ;; ATTR_LATEX line, and also via default variables.
1805 (width (cond ((plist-get attr
:width
))
1806 ((plist-get attr
:height
) "")
1807 ((eq float
'wrap
) "0.48\\textwidth")
1808 (t org-latex-image-default-width
)))
1809 (height (cond ((plist-get attr
:height
))
1810 ((or (plist-get attr
:width
)
1811 (memq float
'(figure wrap
))) "")
1812 (t org-latex-image-default-height
)))
1813 (options (let ((opt (or (plist-get attr
:options
)
1814 org-latex-image-default-option
)))
1815 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
1816 (match-string 1 opt
))))
1818 (if (member filetype
'("tikz" "pgf"))
1820 ;; - use \input to read in image file.
1821 ;; - if options are present, wrap in a tikzpicture environment.
1822 ;; - if width or height are present, use \resizebox to change
1825 (setq image-code
(format "\\input{%s}" path
))
1826 (when (org-string-nw-p options
)
1828 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1831 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
1832 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
1833 (if (org-string-nw-p width
) width
"!")
1834 (if (org-string-nw-p height
) height
"!")
1836 ;; For other images:
1837 ;; - add width and height to options.
1838 ;; - include the image with \includegraphics.
1839 (when (org-string-nw-p width
)
1840 (setq options
(concat options
",width=" width
)))
1841 (when (org-string-nw-p height
)
1842 (setq options
(concat options
",height=" height
)))
1844 (format "\\includegraphics%s{%s}"
1845 (cond ((not (org-string-nw-p options
)) "")
1846 ((= (aref options
0) ?
,)
1847 (format "[%s]"(substring options
1)))
1848 (t (format "[%s]" options
)))
1850 (when (equal filetype
"svg")
1851 (setq image-code
(replace-regexp-in-string "^\\\\includegraphics"
1855 (setq image-code
(replace-regexp-in-string "\\.svg}"
1859 ;; Return proper string, depending on FLOAT.
1861 (wrap (format "\\begin{wrapfigure}%s
1864 %s\\end{wrapfigure}" placement comment-include image-code caption
))
1865 (multicolumn (format "\\begin{figure*}%s
1868 %s\\end{figure*}" placement comment-include image-code caption
))
1869 (figure (format "\\begin{figure}%s
1872 %s\\end{figure}" placement comment-include image-code caption
))
1873 (otherwise image-code
))))
1875 (defun org-latex-link (link desc info
)
1876 "Transcode a LINK object from Org to LaTeX.
1878 DESC is the description part of the link, or the empty string.
1879 INFO is a plist holding contextual information. See
1881 (let* ((type (org-element-property :type link
))
1882 (raw-path (replace-regexp-in-string
1883 "%" "\\%" (org-element-property :path link
) nil t
))
1884 ;; Ensure DESC really exists, or set it to nil.
1885 (desc (and (not (string= desc
"")) desc
))
1886 (imagep (org-export-inline-image-p
1887 link org-latex-inline-image-rules
))
1889 ((member type
'("http" "https" "ftp" "mailto"))
1890 (concat type
":" raw-path
))
1891 ((string= type
"file")
1892 (if (not (file-name-absolute-p raw-path
)) raw-path
1893 (concat "file://" (expand-file-name raw-path
))))
1898 (imagep (org-latex--inline-image link info
))
1899 ;; Radio link: Transcode target's contents and use them as link's
1901 ((string= type
"radio")
1902 (let ((destination (org-export-resolve-radio-link link info
)))
1904 (format "\\hyperref[%s]{%s}"
1905 (org-export-solidify-link-text path
)
1906 (org-export-data (org-element-contents destination
) info
)))))
1907 ;; Links pointing to a headline: Find destination and build
1908 ;; appropriate referencing command.
1909 ((member type
'("custom-id" "fuzzy" "id"))
1910 (let ((destination (if (string= type
"fuzzy")
1911 (org-export-resolve-fuzzy-link link info
)
1912 (org-export-resolve-id-link link info
))))
1913 (case (org-element-type destination
)
1914 ;; Id link points to an external file.
1916 (if desc
(format "\\href{%s}{%s}" destination desc
)
1917 (format "\\url{%s}" destination
)))
1918 ;; Fuzzy link points nowhere.
1920 (format org-latex-link-with-unknown-path-format
1923 (org-element-property :raw-link link
) info
))))
1924 ;; LINK points to a headline. If headlines are numbered
1925 ;; and the link has no description, display headline's
1926 ;; number. Otherwise, display description or headline's
1929 (let* ((custom-label
1930 (and (plist-get info
:latex-custom-id-labels
)
1931 (org-element-property :CUSTOM_ID destination
)))
1938 (org-export-get-headline-number destination info
)
1940 (if (and (plist-get info
:section-numbers
) (not desc
))
1941 (format "\\ref{%s}" label
)
1942 (format "\\hyperref[%s]{%s}" label
1945 (org-element-property :title destination
) info
))))))
1946 ;; Fuzzy link points to a target. Do as above.
1948 (let ((path (org-export-solidify-link-text path
)))
1949 (if (not desc
) (format "\\ref{%s}" path
)
1950 (format "\\hyperref[%s]{%s}" path desc
)))))))
1951 ;; Coderef: replace link with the reference name or the
1952 ;; equivalent line number.
1953 ((string= type
"coderef")
1954 (format (org-export-get-coderef-format path desc
)
1955 (org-export-resolve-coderef path info
)))
1956 ;; Link type is handled by a special function.
1957 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1958 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1959 ;; External link with a description part.
1960 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1961 ;; External link without a description part.
1962 (path (format "\\url{%s}" path
))
1963 ;; No path, only description. Try to do something useful.
1964 (t (format org-latex-link-with-unknown-path-format desc
)))))
1969 (defun org-latex-node-property (node-property contents info
)
1970 "Transcode a NODE-PROPERTY element from Org to LaTeX.
1971 CONTENTS is nil. INFO is a plist holding contextual
1974 (org-element-property :key node-property
)
1975 (let ((value (org-element-property :value node-property
)))
1976 (if value
(concat " " value
) ""))))
1981 (defun org-latex-paragraph (paragraph contents info
)
1982 "Transcode a PARAGRAPH element from Org to LaTeX.
1983 CONTENTS is the contents of the paragraph, as a string. INFO is
1984 the plist used as a communication channel."
1990 (defun org-latex-plain-list (plain-list contents info
)
1991 "Transcode a PLAIN-LIST element from Org to LaTeX.
1992 CONTENTS is the contents of the list. INFO is a plist holding
1993 contextual information."
1994 (let* ((type (org-element-property :type plain-list
))
1995 (attr (org-export-read-attribute :attr_latex plain-list
))
1996 (latex-type (let ((env (plist-get attr
:environment
)))
1997 (cond (env (format "%s" env
))
1998 ((eq type
'ordered
) "enumerate")
1999 ((eq type
'descriptive
) "description")
2001 (org-latex--wrap-label
2003 (format "\\begin{%s}%s\n%s\\end{%s}"
2005 (or (plist-get attr
:options
) "")
2012 (defun org-latex-plain-text (text info
)
2013 "Transcode a TEXT string from Org to LaTeX.
2014 TEXT is the string to transcode. INFO is a plist holding
2015 contextual information."
2016 (let ((specialp (plist-get info
:with-special-strings
))
2018 ;; Protect %, #, &, $, _, { and }.
2019 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output
)
2022 (format "\\%s" (match-string 2 output
)) nil t output
2)))
2025 (replace-regexp-in-string
2026 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil
2))
2027 ;; Protect \. If special strings are used, be careful not to
2028 ;; protect "\" in "\-" constructs.
2029 (let ((symbols (if specialp
"-%$#&{}^_\\" "%$#&{}^_\\")))
2031 (replace-regexp-in-string
2032 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols
)
2033 "$\\backslash$" output nil t
1)))
2036 (replace-regexp-in-string
2037 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t
2))
2038 ;; Activate smart quotes. Be sure to provide original TEXT string
2039 ;; since OUTPUT may have been modified.
2040 (when (plist-get info
:with-smart-quotes
)
2041 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
2042 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
2043 (let ((case-fold-search nil
)
2045 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start
)
2046 (setq output
(replace-match
2047 (format "\\%s{}" (match-string 1 output
)) nil t output
)
2048 start
(match-end 0))))
2049 ;; Convert special strings.
2052 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t
)))
2053 ;; Handle break preservation if required.
2054 (when (plist-get info
:preserve-breaks
)
2055 (setq output
(replace-regexp-in-string
2056 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output
)))
2063 (defun org-latex-planning (planning contents info
)
2064 "Transcode a PLANNING element from Org to LaTeX.
2065 CONTENTS is nil. INFO is a plist holding contextual
2073 (let ((closed (org-element-property :closed planning
)))
2076 (format "\\textbf{%s} " org-closed-string
)
2077 (format org-latex-inactive-timestamp-format
2079 (org-element-property :raw-value closed
))))))
2080 (let ((deadline (org-element-property :deadline planning
)))
2083 (format "\\textbf{%s} " org-deadline-string
)
2084 (format org-latex-active-timestamp-format
2086 (org-element-property :raw-value deadline
))))))
2087 (let ((scheduled (org-element-property :scheduled planning
)))
2090 (format "\\textbf{%s} " org-scheduled-string
)
2091 (format org-latex-active-timestamp-format
2093 (org-element-property :raw-value scheduled
))))))))
2098 ;;;; Property Drawer
2100 (defun org-latex-property-drawer (property-drawer contents info
)
2101 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2102 CONTENTS holds the contents of the drawer. INFO is a plist
2103 holding contextual information."
2104 (and (org-string-nw-p contents
)
2105 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents
)))
2108 ;;;; Pseudo Object: LaTeX Math Block
2110 (defun org-latex--wrap-latex-math-block (data info
)
2111 "Merge contiguous math objects in a pseudo-object container.
2112 DATA is a parse tree or a secondary string. INFO is a plist
2113 containing export options. Modify DATA by side-effect and return it."
2114 (let ((valid-object-p
2116 ;; Non-nil when OBJ can be added to the latex math block.
2118 (case (org-element-type obj
)
2119 (entity (org-element-property :latex-math-p obj
))
2121 (let ((value (org-element-property :value obj
)))
2122 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value
)
2123 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value
))))
2124 ((subscript superscript
) t
))))))
2125 (org-element-map data
'(entity latex-fragment subscript superscript
)
2127 ;; Skip objects already wrapped.
2128 (when (and (not (eq (org-element-type
2129 (org-element-property :parent object
))
2131 (funcall valid-object-p object
))
2132 (let ((math-block (list 'latex-math-block nil
))
2133 (next-elements (org-export-get-next-element object info t
))
2135 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2136 (org-element-insert-before math-block object
)
2137 (org-element-extract-element object
)
2138 (org-element-adopt-elements math-block object
)
2139 (when (zerop (or (org-element-property :post-blank object
) 0))
2140 ;; MATH-BLOCK swallows consecutive math objects.
2142 (dolist (next next-elements
)
2143 (if (not (funcall valid-object-p next
)) (throw 'exit nil
)
2144 (org-element-extract-element next
)
2145 (org-element-adopt-elements math-block next
)
2146 ;; Eschew the case: \beta$x$ -> \(\betax\).
2147 (unless (memq (org-element-type next
)
2148 '(subscript superscript
))
2149 (org-element-put-property last
:post-blank
1))
2151 (when (> (or (org-element-property :post-blank next
) 0) 0)
2152 (throw 'exit nil
))))))
2153 (org-element-put-property
2154 math-block
:post-blank
(org-element-property :post-blank last
)))))
2155 info nil
'(subscript superscript latex-math-block
) t
)
2156 ;; Return updated DATA.
2159 (defun org-latex-math-block-tree-filter (tree backend info
)
2160 (org-latex--wrap-latex-math-block tree info
))
2162 (defun org-latex-math-block-options-filter (info backend
)
2163 (dolist (prop '(:author
:date
:title
) info
)
2164 (plist-put info prop
2165 (org-latex--wrap-latex-math-block (plist-get info prop
) info
))))
2167 (defun org-latex-math-block (math-block contents info
)
2168 "Transcode a MATH-BLOCK object from Org to LaTeX.
2169 CONTENTS is a string. INFO is a plist used as a communication
2171 (when (org-string-nw-p contents
)
2172 (format "\\(%s\\)" (org-trim contents
))))
2176 (defun org-latex-quote-block (quote-block contents info
)
2177 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2178 CONTENTS holds the contents of the block. INFO is a plist
2179 holding contextual information."
2180 (org-latex--wrap-label
2182 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
2187 (defun org-latex-radio-target (radio-target text info
)
2188 "Transcode a RADIO-TARGET object from Org to LaTeX.
2189 TEXT is the text of the target. INFO is a plist holding
2190 contextual information."
2191 (format "\\label{%s}%s"
2192 (org-export-solidify-link-text
2193 (org-element-property :value radio-target
))
2199 (defun org-latex-section (section contents info
)
2200 "Transcode a SECTION element from Org to LaTeX.
2201 CONTENTS holds the contents of the section. INFO is a plist
2202 holding contextual information."
2208 (defun org-latex-special-block (special-block contents info
)
2209 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2210 CONTENTS holds the contents of the block. INFO is a plist
2211 holding contextual information."
2212 (let ((type (downcase (org-element-property :type special-block
)))
2213 (opt (org-export-read-attribute :attr_latex special-block
:options
)))
2214 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2215 ;; Insert any label or caption within the block
2216 ;; (otherwise, a reference pointing to that element will
2217 ;; count the section instead).
2218 (org-latex--caption/label-string special-block info
)
2220 (format "\\end{%s}" type
))))
2225 (defun org-latex-src-block (src-block contents info
)
2226 "Transcode a SRC-BLOCK element from Org to LaTeX.
2227 CONTENTS holds the contents of the item. INFO is a plist holding
2228 contextual information."
2229 (when (org-string-nw-p (org-element-property :value src-block
))
2230 (let* ((lang (org-element-property :language src-block
))
2231 (caption (org-element-property :caption src-block
))
2232 (label (org-element-property :name src-block
))
2233 (custom-env (and lang
2234 (cadr (assq (intern lang
)
2235 org-latex-custom-lang-environments
))))
2236 (num-start (case (org-element-property :number-lines src-block
)
2237 (continued (org-export-get-loc src-block info
))
2239 (retain-labels (org-element-property :retain-labels src-block
))
2240 (attributes (org-export-read-attribute :attr_latex src-block
))
2241 (float (plist-get attributes
:float
)))
2243 ;; Case 1. No source fontification.
2244 ((not org-latex-listings
)
2245 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2247 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2248 ((string= "multicolumn" float
)
2249 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2250 org-latex-default-figure-position
2253 (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
2258 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2259 (org-export-format-code-default src-block info
))))))
2260 ;; Case 2. Custom environment.
2261 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2263 (org-export-format-code-default src-block info
)
2265 ;; Case 3. Use minted package.
2266 ((eq org-latex-listings
'minted
)
2267 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2269 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2270 ((string= "multicolumn" float
)
2271 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2274 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2279 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2281 (org-latex--make-option-string
2282 (if (or (not num-start
)
2283 (assoc "linenos" org-latex-minted-options
))
2284 org-latex-minted-options
2287 ("firstnumber" ,(number-to-string (1+ num-start
))))
2288 org-latex-minted-options
)))
2290 (or (cadr (assq (intern lang
) org-latex-minted-langs
)) lang
)
2292 (let* ((code-info (org-export-unravel-code src-block
))
2296 (org-split-string (car code-info
)
2298 (org-export-format-code
2300 (lambda (loc num ref
)
2304 ;; Ensure references are flushed to the right,
2305 ;; separated with 6 spaces from the widest line
2307 (concat (make-string (+ (- max-width
(length loc
)) 6)
2309 (format "(%s)" ref
)))))
2310 nil
(and retain-labels
(cdr code-info
)))))))
2312 (format float-env body
)))
2313 ;; Case 4. Use listings package.
2316 (or (cadr (assq (intern lang
) org-latex-listings-langs
)) lang
))
2319 (let ((main (org-export-get-caption src-block
))
2320 (secondary (org-export-get-caption src-block t
)))
2322 (format "{%s}" (org-export-data main info
))
2324 (org-export-data secondary info
)
2325 (org-export-data main info
)))))))
2330 (org-latex--make-option-string
2332 org-latex-listings-options
2334 ((and (not float
) (plist-member attributes
:float
)) nil
)
2335 ((string= "multicolumn" float
) '(("float" "*")))
2336 ((and float
(not (assoc "float" org-latex-listings-options
)))
2337 `(("float" ,org-latex-default-figure-position
))))
2338 `(("language" ,lst-lang
))
2339 (when label
`(("label" ,label
)))
2340 (when caption-str
`(("caption" ,caption-str
)))
2341 (cond ((assoc "numbers" org-latex-listings-options
) nil
)
2342 ((not num-start
) '(("numbers" "none")))
2343 ((zerop num-start
) '(("numbers" "left")))
2344 (t `(("numbers" "left")
2346 ,(number-to-string (1+ num-start
)))))))))
2349 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2350 (let* ((code-info (org-export-unravel-code src-block
))
2354 (org-split-string (car code-info
) "\n")))))
2355 (org-export-format-code
2357 (lambda (loc num ref
)
2361 ;; Ensure references are flushed to the right,
2362 ;; separated with 6 spaces from the widest line of
2364 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2365 (format "(%s)" ref
)))))
2366 nil
(and retain-labels
(cdr code-info
))))))))))))
2369 ;;;; Statistics Cookie
2371 (defun org-latex-statistics-cookie (statistics-cookie contents info
)
2372 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2373 CONTENTS is nil. INFO is a plist holding contextual information."
2374 (replace-regexp-in-string
2375 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2380 (defun org-latex-strike-through (strike-through contents info
)
2381 "Transcode STRIKE-THROUGH from Org to LaTeX.
2382 CONTENTS is the text with strike-through markup. INFO is a plist
2383 holding contextual information."
2384 (org-latex--text-markup contents
'strike-through
))
2389 (defun org-latex--script-size (object info
)
2390 "Transcode a subscript or superscript object.
2391 OBJECT is an Org object. INFO is a plist used as a communication
2393 (let ((type (org-element-type object
))
2395 (org-element-map (org-element-contents object
)
2396 (cons 'plain-text org-element-all-objects
)
2398 (case (org-element-type obj
)
2399 ((entity latex-fragment
)
2400 (let ((data (org-trim (org-export-data obj info
))))
2402 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2406 (match-string 1 data
)
2407 (let ((blank (org-element-property :post-blank obj
)))
2408 (and blank
(> blank
0) "\\ "))))))
2411 (format "%s\\text{%s}" output
(org-export-data obj info
))))
2415 (org-export-data obj info
)
2416 (let ((blank (org-element-property :post-blank obj
)))
2417 (and blank
(> blank
0) "\\ ")))))))
2418 info nil org-element-recursive-objects
)
2419 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2421 (concat (if (eq (org-element-type object
) 'subscript
) "_" "^")
2422 (and (> (length output
) 1) "{")
2424 (and (> (length output
) 1) "}"))))
2426 (defun org-latex-subscript (subscript contents info
)
2427 "Transcode a SUBSCRIPT object from Org to LaTeX.
2428 CONTENTS is the contents of the object. INFO is a plist holding
2429 contextual information."
2430 (org-latex--script-size subscript info
))
2435 (defun org-latex-superscript (superscript contents info
)
2436 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2437 CONTENTS is the contents of the object. INFO is a plist holding
2438 contextual information."
2439 (org-latex--script-size superscript info
))
2444 ;; `org-latex-table' is the entry point for table transcoding. It
2445 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2446 ;; delegates the job to either `org-latex--table.el-table',
2447 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2448 ;; depending of the type of the table and the mode requested.
2450 ;; `org-latex--align-string' is a subroutine used to build alignment
2451 ;; string for Org tables.
2453 (defun org-latex-table (table contents info
)
2454 "Transcode a TABLE element from Org to LaTeX.
2455 CONTENTS is the contents of the table. INFO is a plist holding
2456 contextual information."
2457 (if (eq (org-element-property :type table
) 'table.el
)
2458 ;; "table.el" table. Convert it using appropriate tools.
2459 (org-latex--table.el-table table info
)
2460 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
2461 org-latex-default-table-mode
)))
2463 ;; Case 1: Verbatim table.
2464 ((string= type
"verbatim")
2465 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2466 ;; Re-create table, without affiliated keywords.
2467 (org-trim (org-element-interpret-data
2468 `(table nil
,@(org-element-contents table
))
2469 org-latex-pseudo-objects
))))
2471 ((or (string= type
"math") (string= type
"inline-math"))
2472 (org-latex--math-table table info
))
2473 ;; Case 3: Standard table.
2474 (t (concat (org-latex--org-table table contents info
)
2475 ;; When there are footnote references within the
2476 ;; table, insert their definition just after it.
2477 (org-latex--delayed-footnotes-definitions table info
)))))))
2479 (defun org-latex--align-string (table info
)
2480 "Return an appropriate LaTeX alignment string.
2481 TABLE is the considered table. INFO is a plist used as
2482 a communication channel."
2483 (or (org-export-read-attribute :attr_latex table
:align
)
2485 ;; Extract column groups and alignment from first (non-rule)
2488 (org-element-map table
'table-row
2490 (and (eq (org-element-property :type row
) 'standard
) row
))
2494 (let ((borders (org-export-table-cell-borders cell info
)))
2495 ;; Check left border for the first cell only.
2496 (when (and (memq 'left borders
) (not align
))
2498 (push (case (org-export-table-cell-alignment cell info
)
2503 (when (memq 'right borders
) (push "|" align
))))
2505 (apply 'concat
(nreverse align
)))))
2507 (defun org-latex--org-table (table contents info
)
2508 "Return appropriate LaTeX code for an Org table.
2510 TABLE is the table type element to transcode. CONTENTS is its
2511 contents, as a string. INFO is a plist used as a communication
2514 This function assumes TABLE has `org' as its `:type' property and
2515 `table' as its `:mode' attribute."
2516 (let* ((caption (org-latex--caption/label-string table info
))
2517 (attr (org-export-read-attribute :attr_latex table
))
2518 ;; Determine alignment string.
2519 (alignment (org-latex--align-string table info
))
2520 ;; Determine environment for the table: longtable, tabular...
2521 (table-env (or (plist-get attr
:environment
)
2522 org-latex-default-table-environment
))
2523 ;; If table is a float, determine environment: table, table*
2524 ;; or sidewaystable.
2525 (float-env (unless (member table-env
'("longtable" "longtabu"))
2526 (let ((float (plist-get attr
:float
)))
2528 ((and (not float
) (plist-member attr
:float
)) nil
)
2529 ((string= float
"sidewaystable") "sidewaystable")
2530 ((string= float
"multicolumn") "table*")
2532 (org-element-property :caption table
)
2533 (org-string-nw-p (plist-get attr
:caption
)))
2535 ;; Extract others display options.
2536 (fontsize (let ((font (plist-get attr
:font
)))
2537 (and font
(concat font
"\n"))))
2538 (width (plist-get attr
:width
))
2539 (spreadp (plist-get attr
:spread
))
2540 (placement (or (plist-get attr
:placement
)
2541 (format "[%s]" org-latex-default-figure-position
)))
2542 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2543 org-latex-tables-centered
)))
2544 ;; Prepare the final format string for the table.
2547 ((equal "longtable" table-env
)
2548 (concat (and fontsize
(concat "{" fontsize
))
2549 (format "\\begin{longtable}{%s}\n" alignment
)
2550 (and org-latex-table-caption-above
2551 (org-string-nw-p caption
)
2552 (concat caption
"\\\\\n"))
2554 (and (not org-latex-table-caption-above
)
2555 (org-string-nw-p caption
)
2556 (concat caption
"\\\\\n"))
2557 "\\end{longtable}\n"
2558 (and fontsize
"}")))
2560 ((equal "longtabu" table-env
)
2561 (concat (and fontsize
(concat "{" fontsize
))
2562 (format "\\begin{longtabu}%s{%s}\n"
2565 (if spreadp
"spread" "to") width
) "")
2567 (and org-latex-table-caption-above
2568 (org-string-nw-p caption
)
2569 (concat caption
"\\\\\n"))
2571 (and (not org-latex-table-caption-above
)
2572 (org-string-nw-p caption
)
2573 (concat caption
"\\\\\n"))
2575 (and fontsize
"}")))
2579 (concat (format "\\begin{%s}%s\n" float-env placement
)
2580 (if org-latex-table-caption-above caption
"")
2581 (when centerp
"\\centering\n")
2583 (centerp (concat "\\begin{center}\n" fontsize
))
2584 (fontsize (concat "{" fontsize
)))
2585 (cond ((equal "tabu" table-env
)
2586 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2588 (if spreadp
" spread %s " " to %s ")
2592 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2594 (if width
(format "{%s}" width
) "")
2600 (concat (if org-latex-table-caption-above
"" caption
)
2601 (format "\n\\end{%s}" float-env
)))
2602 (centerp "\n\\end{center}")
2603 (fontsize "}")))))))
2605 (defun org-latex--table.el-table
(table info
)
2606 "Return appropriate LaTeX code for a table.el table.
2608 TABLE is the table type element to transcode. INFO is a plist
2609 used as a communication channel.
2611 This function assumes TABLE has `table.el' as its `:type'
2614 ;; Ensure "*org-export-table*" buffer is empty.
2615 (with-current-buffer (get-buffer-create "*org-export-table*")
2617 (let ((output (with-temp-buffer
2618 (insert (org-element-property :value table
))
2620 (re-search-forward "^[ \t]*|[^|]" nil t
)
2621 (table-generate-source 'latex
"*org-export-table*")
2622 (with-current-buffer "*org-export-table*"
2623 (org-trim (buffer-string))))))
2624 (kill-buffer (get-buffer "*org-export-table*"))
2625 ;; Remove left out comments.
2626 (while (string-match "^%.*\n" output
)
2627 (setq output
(replace-match "" t t output
)))
2628 (let ((attr (org-export-read-attribute :attr_latex table
)))
2629 (when (plist-get attr
:rmlines
)
2630 ;; When the "rmlines" attribute is provided, remove all hlines
2631 ;; but the the one separating heading from the table body.
2632 (let ((n 0) (pos 0))
2633 (while (and (< (length output
) pos
)
2634 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2636 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
2637 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2638 org-latex-tables-centered
)))
2639 (if (not centerp
) output
2640 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
2642 (defun org-latex--math-table (table info
)
2643 "Return appropriate LaTeX code for a matrix.
2645 TABLE is the table type element to transcode. INFO is a plist
2646 used as a communication channel.
2648 This function assumes TABLE has `org' as its `:type' property and
2649 `inline-math' or `math' as its `:mode' attribute.."
2650 (let* ((caption (org-latex--caption/label-string table info
))
2651 (attr (org-export-read-attribute :attr_latex table
))
2652 (inlinep (equal (plist-get attr
:mode
) "inline-math"))
2653 (env (or (plist-get attr
:environment
)
2654 org-latex-default-table-environment
))
2658 ;; Ignore horizontal rules.
2659 (when (eq (org-element-property :type row
) 'standard
)
2660 ;; Return each cell unmodified.
2665 (org-element-interpret-data cell org-latex-pseudo-objects
)
2667 (org-element-map row
'table-cell
'identity info
) "&")
2668 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
2670 (org-element-map table
'table-row
'identity info
) ""))
2671 ;; Variables related to math clusters (contiguous math tables
2672 ;; of the same type).
2673 (mode (org-export-read-attribute :attr_latex table
:mode
))
2674 (prev (org-export-get-previous-element table info
))
2675 (next (org-export-get-next-element table info
))
2678 ;; Non-nil when TABLE has the same mode as current table.
2679 (string= (or (org-export-read-attribute :attr_latex table
:mode
)
2680 org-latex-default-table-mode
)
2683 ;; Opening string. If TABLE is in the middle of a table cluster,
2684 ;; do not insert any.
2686 (eq (org-element-type prev
) 'table
)
2687 (memq (org-element-property :post-blank prev
) '(0 nil
))
2688 (funcall same-mode-p prev
))
2691 ((org-string-nw-p caption
) (concat "\\begin{equation}\n" caption
))
2694 (or (plist-get attr
:math-prefix
) "")
2695 ;; Environment. Also treat special cases.
2696 (cond ((equal env
"array")
2697 (let ((align (org-latex--align-string table info
)))
2698 (format "\\begin{array}{%s}\n%s\\end{array}" align contents
)))
2699 ((assoc env org-latex-table-matrix-macros
)
2700 (format "\\%s%s{\n%s}"
2702 (or (plist-get attr
:math-arguments
) "")
2704 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
2706 (or (plist-get attr
:math-suffix
) "")
2707 ;; Closing string. If TABLE is in the middle of a table cluster,
2708 ;; do not insert any. If it closes such a cluster, be sure to
2709 ;; close the cluster with a string matching the opening string.
2711 (eq (org-element-type next
) 'table
)
2712 (memq (org-element-property :post-blank table
) '(0 nil
))
2713 (funcall same-mode-p next
))
2716 ;; Find cluster beginning to know which environment to use.
2717 ((let ((cluster-beg table
) prev
)
2718 (while (and (setq prev
(org-export-get-previous-element
2720 (memq (org-element-property :post-blank prev
)
2722 (funcall same-mode-p prev
))
2723 (setq cluster-beg prev
))
2724 (and (or (org-element-property :caption cluster-beg
)
2725 (org-element-property :name cluster-beg
))
2726 "\n\\end{equation}")))
2732 (defun org-latex-table-cell (table-cell contents info
)
2733 "Transcode a TABLE-CELL element from Org to LaTeX.
2734 CONTENTS is the cell contents. INFO is a plist used as
2735 a communication channel."
2736 (concat (if (and contents
2737 org-latex-table-scientific-notation
2738 (string-match orgtbl-exp-regexp contents
))
2739 ;; Use appropriate format string for scientific
2741 (format org-latex-table-scientific-notation
2742 (match-string 1 contents
)
2743 (match-string 2 contents
))
2745 (when (org-export-get-next-element table-cell info
) " & ")))
2750 (defun org-latex-table-row (table-row contents info
)
2751 "Transcode a TABLE-ROW element from Org to LaTeX.
2752 CONTENTS is the contents of the row. INFO is a plist used as
2753 a communication channel."
2754 ;; Rules are ignored since table separators are deduced from
2755 ;; borders of the current row.
2756 (when (eq (org-element-property :type table-row
) 'standard
)
2757 (let* ((attr (org-export-read-attribute :attr_latex
2758 (org-export-get-parent table-row
)))
2759 (longtablep (member (or (plist-get attr
:environment
)
2760 org-latex-default-table-environment
)
2761 '("longtable" "longtabu")))
2762 (booktabsp (if (plist-member attr
:booktabs
)
2763 (plist-get attr
:booktabs
)
2764 org-latex-tables-booktabs
))
2765 ;; TABLE-ROW's borders are extracted from its first cell.
2766 (borders (org-export-table-cell-borders
2767 (car (org-element-contents table-row
)) info
)))
2769 ;; When BOOKTABS are activated enforce top-rule even when no
2770 ;; hline was specifically marked.
2771 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2772 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2775 ;; Special case for long tables. Define header and footers.
2776 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2779 \\multicolumn{%d}{l}{%s} \\\\
2784 %s\\multicolumn{%d}{r}{%s} \\\\
2787 (if booktabsp
"\\midrule" "\\hline")
2788 (cdr (org-export-table-dimensions
2789 (org-export-get-parent-table table-row
) info
))
2790 (org-latex--translate "Continued from previous page" info
)
2791 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2792 ((and (memq 'top borders
)
2793 (memq 'above borders
)) "\\hline\n")
2796 (if booktabsp
"\\midrule" "\\hline")
2797 (if booktabsp
"\\midrule" "\\hline")
2798 ;; Number of columns.
2799 (cdr (org-export-table-dimensions
2800 (org-export-get-parent-table table-row
) info
))
2801 (org-latex--translate "Continued on next page" info
)))
2802 ;; When BOOKTABS are activated enforce bottom rule even when
2803 ;; no hline was specifically marked.
2804 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2805 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2806 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2811 (defun org-latex-target (target contents info
)
2812 "Transcode a TARGET object from Org to LaTeX.
2813 CONTENTS is nil. INFO is a plist holding contextual
2815 (format "\\label{%s}"
2816 (org-export-solidify-link-text (org-element-property :value target
))))
2821 (defun org-latex-timestamp (timestamp contents info
)
2822 "Transcode a TIMESTAMP object from Org to LaTeX.
2823 CONTENTS is nil. INFO is a plist holding contextual
2825 (let ((value (org-latex-plain-text
2826 (org-timestamp-translate timestamp
) info
)))
2827 (case (org-element-property :type timestamp
)
2828 ((active active-range
) (format org-latex-active-timestamp-format value
))
2829 ((inactive inactive-range
)
2830 (format org-latex-inactive-timestamp-format value
))
2831 (otherwise (format org-latex-diary-timestamp-format value
)))))
2836 (defun org-latex-underline (underline contents info
)
2837 "Transcode UNDERLINE from Org to LaTeX.
2838 CONTENTS is the text with underline markup. INFO is a plist
2839 holding contextual information."
2840 (org-latex--text-markup contents
'underline
))
2845 (defun org-latex-verbatim (verbatim contents info
)
2846 "Transcode a VERBATIM object from Org to LaTeX.
2847 CONTENTS is nil. INFO is a plist used as a communication
2849 (org-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2854 (defun org-latex-verse-block (verse-block contents info
)
2855 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2856 CONTENTS is verse block contents. INFO is a plist holding
2857 contextual information."
2858 (org-latex--wrap-label
2860 ;; In a verse environment, add a line break to each newline
2861 ;; character and change each white space at beginning of a line
2862 ;; into a space of 1 em. Also change each blank line with
2863 ;; a vertical space of 1 em.
2865 (setq contents
(replace-regexp-in-string
2866 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2867 (replace-regexp-in-string
2868 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2869 (while (string-match "^[ \t]+" contents
)
2870 (let ((new-str (format "\\hspace*{%dem}"
2871 (length (match-string 0 contents
)))))
2872 (setq contents
(replace-match new-str nil t contents
))))
2873 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2877 ;;; End-user functions
2880 (defun org-latex-export-as-latex
2881 (&optional async subtreep visible-only body-only ext-plist
)
2882 "Export current buffer as a LaTeX buffer.
2884 If narrowing is active in the current buffer, only export its
2887 If a region is active, export that region.
2889 A non-nil optional argument ASYNC means the process should happen
2890 asynchronously. The resulting buffer should be accessible
2891 through the `org-export-stack' interface.
2893 When optional argument SUBTREEP is non-nil, export the sub-tree
2894 at point, extracting information from the headline properties
2897 When optional argument VISIBLE-ONLY is non-nil, don't export
2898 contents of hidden elements.
2900 When optional argument BODY-ONLY is non-nil, only write code
2901 between \"\\begin{document}\" and \"\\end{document}\".
2903 EXT-PLIST, when provided, is a property list with external
2904 parameters overriding Org default settings, but still inferior to
2905 file-local settings.
2907 Export is done in a buffer named \"*Org LATEX Export*\", which
2908 will be displayed when `org-export-show-temporary-export-buffer'
2911 (org-export-to-buffer 'latex
"*Org LATEX Export*"
2912 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
2915 (defun org-latex-convert-region-to-latex ()
2916 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2917 This can be used in any buffer. For example, you can write an
2918 itemized list in org-mode syntax in an LaTeX buffer and use this
2919 command to convert it."
2921 (org-export-replace-region-by 'latex
))
2924 (defun org-latex-export-to-latex
2925 (&optional async subtreep visible-only body-only ext-plist
)
2926 "Export current buffer to a LaTeX file.
2928 If narrowing is active in the current buffer, only export its
2931 If a region is active, export that region.
2933 A non-nil optional argument ASYNC means the process should happen
2934 asynchronously. The resulting file should be accessible through
2935 the `org-export-stack' interface.
2937 When optional argument SUBTREEP is non-nil, export the sub-tree
2938 at point, extracting information from the headline properties
2941 When optional argument VISIBLE-ONLY is non-nil, don't export
2942 contents of hidden elements.
2944 When optional argument BODY-ONLY is non-nil, only write code
2945 between \"\\begin{document}\" and \"\\end{document}\".
2947 EXT-PLIST, when provided, is a property list with external
2948 parameters overriding Org default settings, but still inferior to
2949 file-local settings."
2951 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2952 (org-export-to-file 'latex outfile
2953 async subtreep visible-only body-only ext-plist
)))
2956 (defun org-latex-export-to-pdf
2957 (&optional async subtreep visible-only body-only ext-plist
)
2958 "Export current buffer to LaTeX then process through to PDF.
2960 If narrowing is active in the current buffer, only export its
2963 If a region is active, export that region.
2965 A non-nil optional argument ASYNC means the process should happen
2966 asynchronously. The resulting file should be accessible through
2967 the `org-export-stack' interface.
2969 When optional argument SUBTREEP is non-nil, export the sub-tree
2970 at point, extracting information from the headline properties
2973 When optional argument VISIBLE-ONLY is non-nil, don't export
2974 contents of hidden elements.
2976 When optional argument BODY-ONLY is non-nil, only write code
2977 between \"\\begin{document}\" and \"\\end{document}\".
2979 EXT-PLIST, when provided, is a property list with external
2980 parameters overriding Org default settings, but still inferior to
2981 file-local settings.
2983 Return PDF file's name."
2985 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2986 (org-export-to-file 'latex outfile
2987 async subtreep visible-only body-only ext-plist
2988 (lambda (file) (org-latex-compile file
)))))
2990 (defun org-latex-compile (texfile &optional snippet
)
2991 "Compile a TeX file.
2993 TEXFILE is the name of the file being compiled. Processing is
2994 done through the command specified in `org-latex-pdf-process'.
2996 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2997 file used to preview a LaTeX snippet. In this case, do not
2998 create a log buffer and do not bother removing log files.
3000 Return PDF file name or an error if it couldn't be produced."
3001 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile
)))
3002 (full-name (file-truename texfile
))
3003 (out-dir (file-name-directory texfile
))
3004 ;; Properly set working directory for compilation.
3005 (default-directory (if (file-name-absolute-p texfile
)
3006 (file-name-directory full-name
)
3009 (unless snippet
(message (format "Processing LaTeX file %s..." texfile
)))
3010 (save-window-excursion
3012 ;; A function is provided: Apply it.
3013 ((functionp org-latex-pdf-process
)
3014 (funcall org-latex-pdf-process
(shell-quote-argument texfile
)))
3015 ;; A list is provided: Replace %b, %f and %o with appropriate
3016 ;; values in each command before applying it. Output is
3017 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3018 ((consp org-latex-pdf-process
)
3019 (let ((outbuf (and (not snippet
)
3020 (get-buffer-create "*Org PDF LaTeX Output*"))))
3024 (replace-regexp-in-string
3025 "%b" (shell-quote-argument base-name
)
3026 (replace-regexp-in-string
3027 "%f" (shell-quote-argument full-name
)
3028 (replace-regexp-in-string
3029 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
3031 org-latex-pdf-process
)
3032 ;; Collect standard errors from output buffer.
3033 (setq errors
(and (not snippet
) (org-latex--collect-errors outbuf
)))))
3034 (t (error "No valid command to process to PDF")))
3035 (let ((pdffile (concat out-dir base-name
".pdf")))
3036 ;; Check for process failure. Provide collected errors if
3038 (if (not (file-exists-p pdffile
))
3039 (error (concat (format "PDF file %s wasn't produced" pdffile
)
3040 (when errors
(concat ": " errors
))))
3041 ;; Else remove log files, when specified, and signal end of
3042 ;; process to user, along with any error encountered.
3043 (when (and (not snippet
) org-latex-remove-logfiles
)
3044 (dolist (file (directory-files
3046 (concat (regexp-quote base-name
)
3047 "\\(?:\\.[0-9]+\\)?"
3049 (regexp-opt org-latex-logfiles-extensions
))))
3050 (delete-file file
)))
3051 (message (concat "Process completed"
3052 (if (not errors
) "."
3053 (concat " with errors: " errors
)))))
3054 ;; Return output file name.
3057 (defun org-latex--collect-errors (buffer)
3058 "Collect some kind of errors from \"pdflatex\" command output.
3060 BUFFER is the buffer containing output.
3062 Return collected error types as a string, or nil if there was
3064 (with-current-buffer buffer
3066 (goto-char (point-max))
3067 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
3068 (let ((case-fold-search t
)
3070 (dolist (latex-error org-latex-known-errors
)
3071 (when (save-excursion (re-search-forward (car latex-error
) nil t
))
3072 (setq errors
(concat errors
" " (cdr latex-error
)))))
3073 (and (org-string-nw-p errors
) (org-trim errors
)))))))
3076 (defun org-latex-publish-to-latex (plist filename pub-dir
)
3077 "Publish an Org file to LaTeX.
3079 FILENAME is the filename of the Org file to be published. PLIST
3080 is the property list for the given project. PUB-DIR is the
3081 publishing directory.
3083 Return output file name."
3084 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
3087 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
3088 "Publish an Org file to PDF (via LaTeX).
3090 FILENAME is the filename of the Org file to be published. PLIST
3091 is the property list for the given project. PUB-DIR is the
3092 publishing directory.
3094 Return output file name."
3095 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3096 ;; in working directory and then moved to publishing directory.
3097 (org-publish-attachment
3099 (org-latex-compile (org-publish-org-to 'latex filename
".tex" plist
))
3106 ;; generated-autoload-file: "org-loaddefs.el"
3109 ;;; ox-latex.el ends here