1 ;;; org-e-latex.el --- LaTeX Back-End For Org Export Engine
3 ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; This library implements a LaTeX back-end for Org generic exporter.
27 ;; M-: (org-export-to-buffer 'e-latex "*Test e-LaTeX*") RET
29 ;; in an org-mode buffer then switch to the buffer to see the LaTeX
30 ;; export. See contrib/lisp/org-export.el for more details on how
31 ;; this exporter works.
33 ;; It introduces three new buffer keywords: "LATEX_CLASS",
34 ;; "LATEX_CLASS_OPTIONS" and "LATEX_HEADER".
38 (eval-when-compile (require 'cl
))
40 (defvar org-export-latex-default-packages-alist
)
41 (defvar org-export-latex-packages-alist
)
43 (declare-function org-element-property
"org-element" (property element
))
44 (declare-function org-element-normalize-string
"org-element" (s))
46 (declare-function org-export-data
"org-export" (data info
))
47 (declare-function org-export-directory
"org-export" (type plist
))
48 (declare-function org-export-expand-macro
"org-export" (macro info
))
49 (declare-function org-export-first-sibling-p
"org-export" (headline))
50 (declare-function org-export-footnote-first-reference-p
"org-export"
51 (footnote-reference info
))
52 (declare-function org-export-format-code
"org-export"
53 (code fun
&optional num-lines ref-alist
))
54 (declare-function org-export-format-code-default
"org-export" (element info
))
55 (declare-function org-export-get-coderef-format
"org-export" (path desc
))
56 (declare-function org-export-get-footnote-definition
"org-export"
57 (footnote-reference info
))
58 (declare-function org-export-get-footnote-number
"org-export" (footnote info
))
59 (declare-function org-export-get-previous-element
"org-export" (blob))
60 (declare-function org-export-get-relative-level
"org-export" (headline info
))
61 (declare-function org-export-unravel-code
"org-export" (element))
62 (declare-function org-export-inline-image-p
"org-export"
63 (link &optional extensions
))
64 (declare-function org-export-last-sibling-p
"org-export" (headline))
65 (declare-function org-export-low-level-p
"org-export" (headline info
))
66 (declare-function org-export-output-file-name
67 "org-export" (extension &optional subtreep pub-dir
))
68 (declare-function org-export-resolve-coderef
"org-export" (ref info
))
69 (declare-function org-export-resolve-fuzzy-link
"org-export" (link info
))
70 (declare-function org-export-resolve-radio-link
"org-export" (link info
))
71 (declare-function org-export-solidify-link-text
"org-export" (s))
73 org-export-to-buffer
"org-export"
74 (backend buffer
&optional subtreep visible-only body-only ext-plist
))
76 org-export-to-file
"org-export"
77 (backend file
&optional subtreep visible-only body-only ext-plist
))
83 (defvar org-e-latex-translate-alist
84 '((babel-call . org-e-latex-babel-call
)
85 (bold . org-e-latex-bold
)
86 (center-block . org-e-latex-center-block
)
87 (clock . org-e-latex-clock
)
88 (code . org-e-latex-code
)
89 (comment . org-e-latex-comment
)
90 (comment-block . org-e-latex-comment-block
)
91 (drawer . org-e-latex-drawer
)
92 (dynamic-block . org-e-latex-dynamic-block
)
93 (entity . org-e-latex-entity
)
94 (example-block . org-e-latex-example-block
)
95 (export-block . org-e-latex-export-block
)
96 (export-snippet . org-e-latex-export-snippet
)
97 (fixed-width . org-e-latex-fixed-width
)
98 (footnote-definition . org-e-latex-footnote-definition
)
99 (footnote-reference . org-e-latex-footnote-reference
)
100 (headline . org-e-latex-headline
)
101 (horizontal-rule . org-e-latex-horizontal-rule
)
102 (inline-babel-call . org-e-latex-inline-babel-call
)
103 (inline-src-block . org-e-latex-inline-src-block
)
104 (inlinetask . org-e-latex-inlinetask
)
105 (italic . org-e-latex-italic
)
106 (item . org-e-latex-item
)
107 (keyword . org-e-latex-keyword
)
108 (latex-environment . org-e-latex-latex-environment
)
109 (latex-fragment . org-e-latex-latex-fragment
)
110 (line-break . org-e-latex-line-break
)
111 (link . org-e-latex-link
)
112 (macro . org-e-latex-macro
)
113 (paragraph . org-e-latex-paragraph
)
114 (plain-list . org-e-latex-plain-list
)
115 (plain-text . org-e-latex-plain-text
)
116 (planning . org-e-latex-planning
)
117 (property-drawer . org-e-latex-property-drawer
)
118 (quote-block . org-e-latex-quote-block
)
119 (quote-section . org-e-latex-quote-section
)
120 (radio-target . org-e-latex-radio-target
)
121 (section . org-e-latex-section
)
122 (special-block . org-e-latex-special-block
)
123 (src-block . org-e-latex-src-block
)
124 (statistics-cookie . org-e-latex-statistics-cookie
)
125 (strike-through . org-e-latex-strike-through
)
126 (subscript . org-e-latex-subscript
)
127 (superscript . org-e-latex-superscript
)
128 (table . org-e-latex-table
)
129 (table-cell . org-e-latex-table-cell
)
130 (table-row . org-e-latex-table-row
)
131 (target . org-e-latex-target
)
132 (template . org-e-latex-template
)
133 (timestamp . org-e-latex-timestamp
)
134 (underline . org-e-latex-underline
)
135 (verbatim . org-e-latex-verbatim
)
136 (verse-block . org-e-latex-verse-block
))
137 "Alist between element or object types and translators.")
139 (defconst org-e-latex-options-alist
140 '((:date
"DATE" nil org-e-latex-date-format t
)
141 (:latex-class
"LATEX_CLASS" nil org-e-latex-default-class t
)
142 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
143 (:latex-header-extra
"LATEX_HEADER" nil nil newline
))
144 "Alist between LaTeX export properties and ways to set them.
145 See `org-export-options-alist' for more information on the
146 structure of the values.")
150 ;;; Internal Variables
152 (defconst org-e-latex-babel-language-alist
153 '(("af" .
"afrikaans")
155 ("bt-br" .
"brazilian")
161 ("de-at" .
"naustrian")
162 ("de-de" .
"ngerman")
165 ("en-au" .
"australian")
166 ("en-ca" .
"canadian")
167 ("en-gb" .
"british")
169 ("en-nz" .
"newzealand")
170 ("en-us" .
"american")
176 ("fr-ca" .
"canadien")
180 ("id" .
"indonesian")
186 ("no-no" .
"nynorsk")
188 ("pt" .
"portuguese")
192 ("sb" .
"uppersorbian")
200 ("uk" .
"ukrainian"))
201 "Alist between language code and corresponding Babel option.")
205 ;;; User Configurable Variables
207 (defgroup org-export-e-latex nil
208 "Options for exporting Org mode files to LaTeX."
209 :tag
"Org Export LaTeX"
215 (defcustom org-e-latex-default-class
"article"
216 "The default LaTeX class."
217 :group
'org-export-e-latex
218 :type
'(string :tag
"LaTeX class"))
220 (defcustom org-e-latex-classes
222 "\\documentclass[11pt]{article}"
223 ("\\section{%s}" .
"\\section*{%s}")
224 ("\\subsection{%s}" .
"\\subsection*{%s}")
225 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
226 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
227 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
229 "\\documentclass[11pt]{report}"
230 ("\\part{%s}" .
"\\part*{%s}")
231 ("\\chapter{%s}" .
"\\chapter*{%s}")
232 ("\\section{%s}" .
"\\section*{%s}")
233 ("\\subsection{%s}" .
"\\subsection*{%s}")
234 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
236 "\\documentclass[11pt]{book}"
237 ("\\part{%s}" .
"\\part*{%s}")
238 ("\\chapter{%s}" .
"\\chapter*{%s}")
239 ("\\section{%s}" .
"\\section*{%s}")
240 ("\\subsection{%s}" .
"\\subsection*{%s}")
241 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
242 "Alist of LaTeX classes and associated header and structure.
243 If #+LaTeX_CLASS is set in the buffer, use its value and the
244 associated information. Here is the structure of each cell:
248 \(numbered-section . unnumbered-section\)
254 The HEADER-STRING is the header that will be inserted into the
255 LaTeX file. It should contain the \\documentclass macro, and
256 anything else that is needed for this setup. To this header, the
257 following commands will be added:
259 - Calls to \\usepackage for all packages mentioned in the
260 variables `org-export-latex-default-packages-alist' and
261 `org-export-latex-packages-alist'. Thus, your header
262 definitions should avoid to also request these packages.
264 - Lines specified via \"#+LaTeX_HEADER:\"
266 If you need more control about the sequence in which the header
267 is built up, or if you want to exclude one of these building
268 blocks for a particular class, you can use the following
269 macro-like placeholders.
271 [DEFAULT-PACKAGES] \\usepackage statements for default packages
272 [NO-DEFAULT-PACKAGES] do not include any of the default packages
273 [PACKAGES] \\usepackage statements for packages
274 [NO-PACKAGES] do not include the packages
275 [EXTRA] the stuff from #+LaTeX_HEADER
276 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
277 [BEAMER-HEADER-EXTRA] the beamer extra headers
281 \\documentclass{article}
282 [NO-DEFAULT-PACKAGES]
284 \\providecommand{\\alert}[1]{\\textbf{#1}}
287 will omit the default packages, and will include the
288 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
289 then place \\usepackage commands based on the content of
290 `org-export-latex-packages-alist'.
292 If your header, `org-export-latex-default-packages-alist' or
293 `org-export-latex-packages-alist' inserts
294 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
295 replaced with a coding system derived from
296 `buffer-file-coding-system'. See also the variable
297 `org-e-latex-inputenc-alist' for a way to influence this
300 The sectioning structure
301 ------------------------
303 The sectioning structure of the class is given by the elements
304 following the header string. For each sectioning level, a number
305 of strings is specified. A %s formatter is mandatory in each
306 section string and will be replaced by the title of the section.
308 Instead of a cons cell \(numbered . unnumbered\), you can also
309 provide a list of 2 or 4 elements,
311 \(numbered-open numbered-close\)
315 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
317 providing opening and closing strings for a LaTeX environment
318 that should represent the document section. The opening clause
319 should have a %s to represent the section title.
321 Instead of a list of sectioning commands, you can also specify
322 a function name. That function will be called with two
323 parameters, the \(reduced) level of the headline, and a predicate
324 non-nil when the headline should be numbered. It must return
325 a format string in which the section title will be added."
326 :group
'org-export-e-latex
328 (list (string :tag
"LaTeX class")
329 (string :tag
"LaTeX header")
330 (repeat :tag
"Levels" :inline t
333 (string :tag
" numbered")
334 (string :tag
"unnumbered"))
335 (list :tag
"Environment"
336 (string :tag
"Opening (numbered)")
337 (string :tag
"Closing (numbered)")
338 (string :tag
"Opening (unnumbered)")
339 (string :tag
"Closing (unnumbered)"))
340 (function :tag
"Hook computing sectioning"))))))
342 (defcustom org-e-latex-inputenc-alist nil
343 "Alist of inputenc coding system names, and what should really be used.
344 For example, adding an entry
346 (\"utf8\" . \"utf8x\")
348 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
349 are written as utf8 files."
350 :group
'org-export-e-latex
353 (string :tag
"Derived from buffer")
354 (string :tag
"Use this instead"))))
356 (defcustom org-e-latex-date-format
358 "Format string for \\date{...}."
359 :group
'org-export-e-latex
362 (defcustom org-e-latex-title-command
"\\maketitle"
363 "The command used to insert the title just after \\begin{document}.
364 If this string contains the formatting specification \"%s\" then
365 it will be used as a formatting string, passing the title as an
367 :group
'org-export-e-latex
373 (defcustom org-e-latex-format-headline-function nil
374 "Function to format headline text.
376 This function will be called with 5 arguments:
377 TODO the todo keyword (string or nil).
378 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
379 PRIORITY the priority of the headline (integer or nil)
380 TEXT the main headline text (string).
381 TAGS the tags as a list of strings (list of strings or nil).
383 The function result will be used in the section format string.
385 As an example, one could set the variable to the following, in
386 order to reproduce the default set-up:
388 \(defun org-e-latex-format-headline (todo todo-type priority text tags)
389 \"Default format function for an headline.\"
391 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
393 \(format \"\\\\framebox{\\\\#%c} \" priority))
396 \(format \"\\\\hfill{}\\\\textsc{%s}\"
397 \(mapconcat 'identity tags \":\"))))"
398 :group
'org-export-e-latex
404 (defcustom org-e-latex-footnote-separator
"\\textsuperscript{,}\\,"
405 "Text used to separate footnotes."
406 :group
'org-export-e-latex
412 (defcustom org-e-latex-active-timestamp-format
"\\textit{%s}"
413 "A printf format string to be applied to active timestamps."
414 :group
'org-export-e-latex
417 (defcustom org-e-latex-inactive-timestamp-format
"\\textit{%s}"
418 "A printf format string to be applied to inactive timestamps."
419 :group
'org-export-e-latex
422 (defcustom org-e-latex-diary-timestamp-format
"\\textit{%s}"
423 "A printf format string to be applied to diary timestamps."
424 :group
'org-export-e-latex
430 (defcustom org-e-latex-image-default-option
"width=.9\\linewidth"
431 "Default option for images."
432 :group
'org-export-e-latex
435 (defcustom org-e-latex-default-figure-position
"htb"
436 "Default position for latex figures."
437 :group
'org-export-e-latex
440 (defcustom org-e-latex-inline-image-rules
441 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
442 "Rules characterizing image files that can be inlined into LaTeX.
444 A rule consists in an association whose key is the type of link
445 to consider, and value is a regexp that will be matched against
448 Note that, by default, the image extension *actually* allowed
449 depend on the way the LaTeX file is processed. When used with
450 pdflatex, pdf, jpg and png images are OK. When processing
451 through dvi to Postscript, only ps and eps are allowed. The
452 default we use here encompasses both."
453 :group
'org-export-e-latex
454 :type
'(alist :key-type
(string :tag
"Type")
455 :value-type
(regexp :tag
"Path")))
457 (defcustom org-e-latex-link-with-unknown-path-format
"\\texttt{%s}"
458 "Format string for links with unknown path type."
459 :group
'org-export-latex
465 (defcustom org-e-latex-default-table-environment
"tabular"
466 "Default environment used to build tables."
467 :group
'org-export-e-latex
470 (defcustom org-e-latex-tables-centered t
471 "When non-nil, tables are exported in a center environment."
472 :group
'org-export-e-latex
475 (defcustom org-e-latex-tables-verbatim nil
476 "When non-nil, tables are exported verbatim."
477 :group
'org-export-e-latex
480 (defcustom org-e-latex-tables-booktabs nil
481 "When non-nil, display tables in a formal \"booktabs\" style.
482 This option assumes that the \"booktabs\" package is properly
483 loaded in the header of the document. This value can be ignored
484 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
486 :group
'org-export-e-latex
489 (defcustom org-e-latex-table-caption-above t
490 "When non-nil, place caption string at the beginning of the table.
491 Otherwise, place it near the end."
492 :group
'org-export-e-latex
495 (defcustom org-e-latex-table-scientific-notation
"%s\\,(%s)"
496 "Format string to display numbers in scientific notation.
497 The format should have \"%s\" twice, for mantissa and exponent
498 \(i.e. \"%s\\\\times10^{%s}\").
500 When nil, no transformation is made."
501 :group
'org-export-e-latex
503 (string :tag
"Format string")
504 (const :tag
"No formatting")))
509 (defcustom org-e-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
511 (italic .
"\\emph{%s}")
512 (strike-through .
"\\st{%s}")
513 (underline .
"\\underline{%s}")
514 (verbatim . protectedtexttt
))
515 "Alist of LaTeX expressions to convert text markup.
517 The key must be a symbol among `bold', `code', `italic',
518 `strike-through', `underline' and `verbatim'. The value is
519 a formatting string to wrap fontified text with.
521 Value can also be set to the following symbols: `verb' and
522 `protectedtexttt'. For the former, Org will use \"\\verb\" to
523 create a format string and select a delimiter character that
524 isn't in the string. For the latter, Org will use \"\\texttt\"
525 to typeset and try to protect special characters.
527 If no association can be found for a given markup, text will be
529 :group
'org-export-e-latex
531 :options
'(bold code italic strike-through underline verbatim
))
536 (defcustom org-e-latex-format-drawer-function nil
537 "Function called to format a drawer in LaTeX code.
539 The function must accept two parameters:
540 NAME the drawer name, like \"LOGBOOK\"
541 CONTENTS the contents of the drawer.
543 The function should return the string to be exported.
545 For example, the variable could be set to the following function
546 in order to mimic default behaviour:
548 \(defun org-e-latex-format-drawer-default \(name contents\)
549 \"Format a drawer element for LaTeX export.\"
551 :group
'org-export-e-latex
557 (defcustom org-e-latex-format-inlinetask-function nil
558 "Function called to format an inlinetask in LaTeX code.
560 The function must accept six parameters:
561 TODO the todo keyword, as a string
562 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
563 PRIORITY the inlinetask priority, as a string
564 NAME the inlinetask name, as a string.
565 TAGS the inlinetask tags, as a list of strings.
566 CONTENTS the contents of the inlinetask, as a string.
568 The function should return the string to be exported.
570 For example, the variable could be set to the following function
571 in order to mimic default behaviour:
573 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
574 \"Format an inline task element for LaTeX export.\"
578 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
579 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
582 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
583 \(mapconcat 'identity tags \":\")))))
584 \(format (concat \"\\\\begin{center}\\n\"
586 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
588 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
590 \"\\\\end{minipage}}\"
592 full-title contents))"
593 :group
'org-export-e-latex
599 (defcustom org-e-latex-listings nil
600 "Non-nil means export source code using the listings package.
601 This package will fontify source code, possibly even with color.
602 If you want to use this, you also need to make LaTeX use the
603 listings package, and if you want to have color, the color
604 package. Just add these to `org-export-latex-packages-alist',
605 for example using customize, or with something like:
607 \(require 'org-e-latex)
608 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
609 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
613 \(setq org-e-latex-listings 'minted)
615 causes source code to be exported using the minted package as
616 opposed to listings. If you want to use minted, you need to add
617 the minted package to `org-export-latex-packages-alist', for
618 example using customize, or with
620 \(require 'org-e-latex)
621 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
623 In addition, it is necessary to install pygments
624 \(http://pygments.org), and to configure the variable
625 `org-e-latex-pdf-process' so that the -shell-escape option is
627 :group
'org-export-e-latex
629 (const :tag
"Use listings" t
)
630 (const :tag
"Use minted" 'minted
)
631 (const :tag
"Export verbatim" nil
)))
633 (defcustom org-e-latex-listings-langs
634 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
637 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
638 (html "HTML") (xml "XML")
639 (tex "TeX") (latex "TeX")
640 (shell-script "bash")
642 (ocaml "Caml") (caml "Caml")
643 (sql "SQL") (sqlite "sql"))
644 "Alist mapping languages to their listing language counterpart.
645 The key is a symbol, the major mode symbol without the \"-mode\".
646 The value is the string that should be inserted as the language
647 parameter for the listings package. If the mode name and the
648 listings name are the same, the language does not need an entry
649 in this list - but it does not hurt if it is present."
650 :group
'org-export-e-latex
653 (symbol :tag
"Major mode ")
654 (string :tag
"Listings language"))))
656 (defcustom org-e-latex-listings-options nil
657 "Association list of options for the latex listings package.
659 These options are supplied as a comma-separated list to the
660 \\lstset command. Each element of the association list should be
661 a list containing two strings: the name of the option, and the
664 (setq org-e-latex-listings-options
665 '((\"basicstyle\" \"\\small\")
666 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
668 will typeset the code in a small size font with underlined, bold
671 Note that the same options will be applied to blocks of all
673 :group
'org-export-e-latex
676 (string :tag
"Listings option name ")
677 (string :tag
"Listings option value"))))
679 (defcustom org-e-latex-minted-langs
680 '((emacs-lisp "common-lisp")
683 (shell-script "bash")
685 "Alist mapping languages to their minted language counterpart.
686 The key is a symbol, the major mode symbol without the \"-mode\".
687 The value is the string that should be inserted as the language
688 parameter for the minted package. If the mode name and the
689 listings name are the same, the language does not need an entry
690 in this list - but it does not hurt if it is present.
692 Note that minted uses all lower case for language identifiers,
693 and that the full list of language identifiers can be obtained
696 pygmentize -L lexers"
697 :group
'org-export-e-latex
700 (symbol :tag
"Major mode ")
701 (string :tag
"Minted language"))))
703 (defcustom org-e-latex-minted-options nil
704 "Association list of options for the latex minted package.
706 These options are supplied within square brackets in
707 \\begin{minted} environments. Each element of the alist should
708 be a list containing two strings: the name of the option, and the
711 \(setq org-e-latex-minted-options
712 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
714 will result in src blocks being exported with
716 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
718 as the start of the minted environment. Note that the same
719 options will be applied to blocks of all languages."
720 :group
'org-export-e-latex
723 (string :tag
"Minted option name ")
724 (string :tag
"Minted option value"))))
726 (defvar org-e-latex-custom-lang-environments nil
727 "Alist mapping languages to language-specific LaTeX environments.
729 It is used during export of src blocks by the listings and minted
730 latex packages. For example,
732 \(setq org-e-latex-custom-lang-environments
733 '\(\(python \"pythoncode\"\)\)\)
735 would have the effect that if org encounters begin_src python
736 during latex export it will output
745 (defcustom org-e-latex-quotes
747 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"«~")
748 ("\\(\\S-\\)\"" .
"~»")
749 ("\\(\\s-\\|(\\|^\\)'" .
"'"))
751 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"``")
752 ("\\(\\S-\\)\"" .
"''")
753 ("\\(\\s-\\|(\\|^\\)'" .
"`")))
754 "Alist for quotes to use when converting english double-quotes.
756 The CAR of each item in this alist is the language code.
757 The CDR of each item in this alist is a list of three CONS:
758 - the first CONS defines the opening quote;
759 - the second CONS defines the closing quote;
760 - the last CONS defines single quotes.
762 For each item in a CONS, the first string is a regexp
763 for allowed characters before/after the quote, the second
764 string defines the replacement string for this quote."
765 :group
'org-export-e-latex
767 (cons :tag
"Opening quote"
768 (string :tag
"Regexp for char before")
769 (string :tag
"Replacement quote "))
770 (cons :tag
"Closing quote"
771 (string :tag
"Regexp for char after ")
772 (string :tag
"Replacement quote "))
773 (cons :tag
"Single quote"
774 (string :tag
"Regexp for char before")
775 (string :tag
"Replacement quote "))))
780 (defcustom org-e-latex-pdf-process
781 '("pdflatex -interaction nonstopmode -output-directory %o %f"
782 "pdflatex -interaction nonstopmode -output-directory %o %f"
783 "pdflatex -interaction nonstopmode -output-directory %o %f")
784 "Commands to process a LaTeX file to a PDF file.
785 This is a list of strings, each of them will be given to the
786 shell as a command. %f in the command will be replaced by the
787 full file name, %b by the file base name \(i.e. without
788 extension) and %o by the base directory of the file.
790 The reason why this is a list is that it usually takes several
791 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
792 does not have a clever mechanism to detect which of these
793 commands have to be run to get to a stable result, and it also
794 does not do any error checking.
796 By default, Org uses 3 runs of `pdflatex' to do the processing.
797 If you have texi2dvi on your system and if that does not cause
798 the infamous egrep/locale bug:
800 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
802 then `texi2dvi' is the superior choice. Org does offer it as one
803 of the customize options.
805 Alternatively, this may be a Lisp function that does the
806 processing, so you could use this to apply the machinery of
807 AUCTeX or the Emacs LaTeX mode. This function should accept the
808 file name as its single argument."
809 :group
'org-export-pdf
811 (repeat :tag
"Shell command sequence"
812 (string :tag
"Shell command"))
813 (const :tag
"2 runs of pdflatex"
814 ("pdflatex -interaction nonstopmode -output-directory %o %f"
815 "pdflatex -interaction nonstopmode -output-directory %o %f"))
816 (const :tag
"3 runs of pdflatex"
817 ("pdflatex -interaction nonstopmode -output-directory %o %f"
818 "pdflatex -interaction nonstopmode -output-directory %o %f"
819 "pdflatex -interaction nonstopmode -output-directory %o %f"))
820 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
821 ("pdflatex -interaction nonstopmode -output-directory %o %f"
823 "pdflatex -interaction nonstopmode -output-directory %o %f"
824 "pdflatex -interaction nonstopmode -output-directory %o %f"))
825 (const :tag
"texi2dvi"
826 ("texi2dvi -p -b -c -V %f"))
828 ("rubber -d --into %o %f"))
831 (defcustom org-e-latex-logfiles-extensions
832 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
833 "The list of file extensions to consider as LaTeX logfiles."
834 :group
'org-export-e-latex
835 :type
'(repeat (string :tag
"Extension")))
837 (defcustom org-e-latex-remove-logfiles t
838 "Non-nil means remove the logfiles produced by PDF production.
839 These are the .aux, .log, .out, and .toc files."
840 :group
'org-export-e-latex
845 ;;; Internal Functions
847 (defun org-e-latex--caption/label-string
(caption label info
)
848 "Return caption and label LaTeX string for floats.
850 CAPTION is a cons cell of secondary strings, the car being the
851 standard caption and the cdr its short form. LABEL is a string
852 representing the label. INFO is a plist holding contextual
855 If there's no caption nor label, return the empty string.
857 For non-floats, see `org-e-latex--wrap-label'."
858 (let ((label-str (if label
(format "\\label{%s}" label
) "")))
860 ((and (not caption
) (not label
)) "")
861 ((not caption
) (format "\\label{%s}\n" label
))
862 ;; Option caption format with short name.
864 (format "\\caption[%s]{%s%s}\n"
865 (org-export-data (cdr caption
) info
)
867 (org-export-data (car caption
) info
)))
868 ;; Standard caption format.
869 (t (format "\\caption{%s%s}\n"
871 (org-export-data (car caption
) info
))))))
873 (defun org-e-latex--guess-babel-language (header info
)
874 "Set Babel's language according to LANGUAGE keyword.
876 HEADER is the LaTeX header string. INFO is the plist used as
877 a communication channel.
879 Insertion of guessed language only happens when Babel package has
880 explicitly been loaded. Then it is added to the rest of
883 Return the new header."
884 (let ((language-code (plist-get info
:language
)))
885 ;; If no language is set or Babel package is not loaded, return
887 (if (or (not (stringp language-code
))
888 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
890 (let ((options (save-match-data
891 (org-split-string (match-string 1 header
) ",")))
892 (language (cdr (assoc language-code
893 org-e-latex-babel-language-alist
))))
894 ;; If LANGUAGE is already loaded, return header. Otherwise,
895 ;; append LANGUAGE to other options.
896 (if (member language options
) header
897 (replace-match (mapconcat 'identity
898 (append options
(list language
))
900 nil nil header
1))))))
902 (defun org-e-latex--guess-inputenc (header)
903 "Set the coding system in inputenc to what the buffer is.
904 HEADER is the LaTeX header string. Return the new header."
905 (let* ((cs (or (ignore-errors
906 (latexenc-coding-system-to-inputenc
907 buffer-file-coding-system
))
910 ;; First translate if that is requested.
911 (setq cs
(or (cdr (assoc cs org-e-latex-inputenc-alist
)) cs
))
912 ;; Then find the \usepackage statement and replace the option.
913 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
914 cs header t nil
1))))
916 (defun org-e-latex--find-verb-separator (s)
917 "Return a character not used in string S.
918 This is used to choose a separator for constructs like \\verb."
919 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
920 (loop for c across ll
921 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
922 return
(char-to-string c
))))
924 (defun org-e-latex--make-option-string (options)
925 "Return a comma separated string of keywords and values.
926 OPTIONS is an alist where the key is the options keyword as
927 a string, and the value a list containing the keyword value, or
929 (mapconcat (lambda (pair)
931 (when (> (length (second pair
)) 0)
932 (concat "=" (second pair
)))))
936 (defun org-e-latex--quotation-marks (text info
)
937 "Export quotation marks depending on language conventions.
938 TEXT is a string containing quotation marks to be replaced. INFO
939 is a plist used as a communication channel."
942 (while (setq start
(string-match (car l
) text start
))
943 (let ((new-quote (concat (match-string 1 text
) (cdr l
))))
944 (setq text
(replace-match new-quote t t text
))))))
945 (cdr (or (assoc (plist-get info
:language
) org-e-latex-quotes
)
946 ;; Falls back on English.
947 (assoc "en" org-e-latex-quotes
))))
950 (defun org-e-latex--wrap-label (element output
)
951 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
952 This function shouldn't be used for floats. See
953 `org-e-latex--caption/label-string'."
954 (let ((label (org-element-property :name element
)))
955 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
957 (concat (format "\\label{%s}\n" label
) output
))))
959 (defun org-e-latex--text-markup (text markup
)
960 "Format TEXT depending on MARKUP text markup.
961 See `org-e-latex-text-markup-alist' for details."
962 (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist
))))
964 ;; No format string: Return raw text.
966 ;; Handle the `verb' special case: Find and appropriate separator
967 ;; and use "\\verb" command.
969 (let ((separator (org-e-latex--find-verb-separator text
)))
970 (concat "\\verb" separator text separator
)))
971 ;; Handle the `protectedtexttt' special case: Protect some
972 ;; special chars and use "\texttt{%s}" format string.
973 ((eq 'protectedtexttt fmt
)
975 (trans '(("\\" .
"\\textbackslash{}")
976 ("~" .
"\\textasciitilde{}")
977 ("^" .
"\\textasciicircum{}")))
980 (while (string-match "[\\{}$%&_#~^]" text
)
981 (setq char
(match-string 0 text
))
982 (if (> (match-beginning 0) 0)
983 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
984 (setq text
(substring text
(1+ (match-beginning 0))))
985 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
986 rtn
(concat rtn char
)))
987 (setq text
(concat rtn text
)
989 (while (string-match "--" text
)
990 (setq text
(replace-match "-{}-" t t text
)))
992 ;; Else use format string.
993 (t (format fmt text
)))))
995 (defun org-e-latex--delayed-footnotes-definitions (element info
)
996 "Return footnotes definitions in ELEMENT as a string.
998 INFO is a plist used as a communication channel.
1000 Footnotes definitions are returned within \"\\footnotetxt{}\"
1003 This functions is used within constructs that don't support
1004 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1005 \"\\footnotemark\" is used within the construct and this function
1010 "\\footnotetext[%s]{%s}"
1011 (org-export-get-footnote-number ref info
)
1014 (org-export-get-footnote-definition ref info
) info
))))
1015 ;; Find every footnote reference in ELEMENT.
1017 search-refs
; For byte-compiler.
1021 ;; Return a list of all footnote references never seen
1024 data
'footnote-reference
1026 (when (org-export-footnote-first-reference-p ref info
)
1028 (when (eq (org-element-property :type ref
) 'standard
)
1029 (funcall search-refs
1030 (org-export-get-footnote-definition ref info
)))))
1032 (reverse all-refs
)))))
1033 (funcall search-refs element
))
1040 (defun org-e-latex-template (contents info
)
1041 "Return complete document string after LaTeX conversion.
1042 CONTENTS is the transcoded contents string. INFO is a plist
1043 holding export options."
1044 (let ((title (org-export-data (plist-get info
:title
) info
)))
1047 (and (plist-get info
:time-stamp-file
)
1048 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1049 ;; 2. Document class and packages.
1050 (let ((class (plist-get info
:latex-class
))
1051 (class-options (plist-get info
:latex-class-options
)))
1052 (org-element-normalize-string
1053 (let* ((header (nth 1 (assoc class org-e-latex-classes
)))
1054 (document-class-string
1055 (and (stringp header
)
1057 (replace-regexp-in-string
1058 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
1059 class-options header t nil
1)
1061 (when document-class-string
1062 (org-e-latex--guess-babel-language
1063 (org-e-latex--guess-inputenc
1064 (org-splice-latex-header
1065 document-class-string
1066 org-export-latex-default-packages-alist
; defined in org.el
1067 org-export-latex-packages-alist nil
; defined in org.el
1068 (plist-get info
:latex-header-extra
)))
1070 ;; 3. Define alert if not yet defined.
1071 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
1072 ;; 4. Possibly limit depth for headline numbering.
1073 (let ((sec-num (plist-get info
:section-numbers
)))
1074 (when (integerp sec-num
)
1075 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1077 (let ((author (and (plist-get info
:with-author
)
1078 (let ((auth (plist-get info
:author
)))
1079 (and auth
(org-export-data auth info
)))))
1080 (email (and (plist-get info
:with-email
)
1081 (org-export-data (plist-get info
:email
) info
))))
1082 (cond ((and author email
(not (string= "" email
)))
1083 (format "\\author{%s\\thanks{%s}}\n" author email
))
1084 (author (format "\\author{%s}\n" author
))
1085 (t "\\author{}\n")))
1087 (let ((date (plist-get info
:date
)))
1088 (and date
(format "\\date{%s}\n" date
)))
1090 (format "\\title{%s}\n" title
)
1091 ;; 8. Hyperref options.
1092 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1093 (or (plist-get info
:keywords
) "")
1094 (or (plist-get info
:description
) "")
1095 (if (not (plist-get info
:with-creator
)) ""
1096 (plist-get info
:creator
)))
1097 ;; 9. Document start.
1098 "\\begin{document}\n\n"
1099 ;; 10. Title command.
1100 (org-element-normalize-string
1101 (cond ((string= "" title
) nil
)
1102 ((not (stringp org-e-latex-title-command
)) nil
)
1103 ((string-match "\\(?:[^%]\\|^\\)%s"
1104 org-e-latex-title-command
)
1105 (format org-e-latex-title-command title
))
1106 (t org-e-latex-title-command
)))
1107 ;; 11. Table of contents.
1108 (let ((depth (plist-get info
:with-toc
)))
1110 (concat (when (wholenump depth
)
1111 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1112 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
1113 ;; 12. Document's body.
1116 (let ((creator-info (plist-get info
:with-creator
)))
1118 ((not creator-info
) "")
1119 ((eq creator-info
'comment
)
1120 (format "%% %s\n" (plist-get info
:creator
)))
1121 (t (concat (plist-get info
:creator
) "\n"))))
1122 ;; 14. Document end.
1123 "\\end{document}")))
1127 ;;; Transcode Functions
1131 ;; Babel Calls are ignored.
1136 (defun org-e-latex-bold (bold contents info
)
1137 "Transcode BOLD from Org to LaTeX.
1138 CONTENTS is the text with bold markup. INFO is a plist holding
1139 contextual information."
1140 (org-e-latex--text-markup contents
'bold
))
1145 (defun org-e-latex-center-block (center-block contents info
)
1146 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1147 CONTENTS holds the contents of the center block. INFO is a plist
1148 holding contextual information."
1149 (org-e-latex--wrap-label
1151 (format "\\begin{center}\n%s\\end{center}" contents
)))
1156 (defun org-e-latex-clock (clock contents info
)
1157 "Transcode a CLOCK element from Org to LaTeX.
1158 CONTENTS is nil. INFO is a plist holding contextual
1162 (format "\\textbf{%s} " org-clock-string
)
1163 (format org-e-latex-inactive-timestamp-format
1164 (concat (org-translate-time (org-element-property :value clock
))
1165 (let ((time (org-element-property :time clock
)))
1166 (and time
(format " (%s)" time
)))))
1172 (defun org-e-latex-code (code contents info
)
1173 "Transcode a CODE object from Org to LaTeX.
1174 CONTENTS is nil. INFO is a plist used as a communication
1176 (org-e-latex--text-markup (org-element-property :value code
) 'code
))
1181 ;; Comments are ignored.
1186 ;; Comment Blocks are ignored.
1191 (defun org-e-latex-drawer (drawer contents info
)
1192 "Transcode a DRAWER element from Org to LaTeX.
1193 CONTENTS holds the contents of the block. INFO is a plist
1194 holding contextual information."
1195 (let* ((name (org-element-property :drawer-name drawer
))
1196 (output (if (functionp org-e-latex-format-drawer-function
)
1197 (funcall org-e-latex-format-drawer-function
1199 ;; If there's no user defined function: simply
1200 ;; display contents of the drawer.
1202 (org-e-latex--wrap-label drawer output
)))
1207 (defun org-e-latex-dynamic-block (dynamic-block contents info
)
1208 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1209 CONTENTS holds the contents of the block. INFO is a plist
1210 holding contextual information. See `org-export-data'."
1211 (org-e-latex--wrap-label dynamic-block contents
))
1216 (defun org-e-latex-entity (entity contents info
)
1217 "Transcode an ENTITY object from Org to LaTeX.
1218 CONTENTS are the definition itself. INFO is a plist holding
1219 contextual information."
1220 (let ((ent (org-element-property :latex entity
)))
1221 (if (org-element-property :latex-math-p entity
) (format "$%s$" ent
) ent
)))
1226 (defun org-e-latex-example-block (example-block contents info
)
1227 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1228 CONTENTS is nil. INFO is a plist holding contextual
1230 (org-e-latex--wrap-label
1232 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1233 (org-export-format-code-default example-block info
))))
1238 (defun org-e-latex-export-block (export-block contents info
)
1239 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1240 CONTENTS is nil. INFO is a plist holding contextual information."
1241 (when (string= (org-element-property :type export-block
) "LATEX")
1242 (org-remove-indentation (org-element-property :value export-block
))))
1247 (defun org-e-latex-export-snippet (export-snippet contents info
)
1248 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1249 CONTENTS is nil. INFO is a plist holding contextual information."
1250 (when (eq (org-export-snippet-backend export-snippet
) 'e-latex
)
1251 (org-element-property :value export-snippet
)))
1256 (defun org-e-latex-fixed-width (fixed-width contents info
)
1257 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1258 CONTENTS is nil. INFO is a plist holding contextual information."
1259 (org-e-latex--wrap-label
1261 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1262 (org-remove-indentation
1263 (org-element-property :value fixed-width
)))))
1266 ;;;; Footnote Definition
1268 ;; Footnote Definitions are ignored.
1271 ;;;; Footnote Reference
1273 (defun org-e-latex-footnote-reference (footnote-reference contents info
)
1274 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1275 CONTENTS is nil. INFO is a plist holding contextual information."
1277 ;; Insert separator between two footnotes in a row.
1278 (let ((prev (org-export-get-previous-element footnote-reference
)))
1279 (when (eq (org-element-type prev
) 'footnote-reference
)
1280 org-e-latex-footnote-separator
))
1282 ;; Use \footnotemark if the footnote has already been defined.
1283 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1284 (format "\\footnotemark[%s]{}"
1285 (org-export-get-footnote-number footnote-reference info
)))
1286 ;; Use also \footnotemark if reference is within another footnote
1287 ;; reference or footnote definition.
1288 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1289 thereis
(memq (org-element-type parent
)
1290 '(footnote-reference footnote-definition
)))
1291 (let ((num (org-export-get-footnote-number footnote-reference info
)))
1292 (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num
)))
1293 ;; Use also \footnotemark if reference is within an item's tag.
1294 ;; Note: this won't work if reference has already been defined
1295 ;; since we cannot specify footnote number through square
1296 ;; brackets, forbidden in an optional argument.
1297 ((eq (org-element-type (org-export-get-parent-element footnote-reference
))
1299 (format "\\footnotemark\\setcounter{footnote}{%s}"
1300 (org-export-get-footnote-number footnote-reference info
)))
1301 ;; Otherwise, define it with \footnote command.
1303 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1304 (unless (eq (org-element-type def
) 'org-data
)
1305 (setq def
(cons 'org-data
(cons nil def
))))
1307 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1308 ;; Retrieve all footnote references within the footnote and
1309 ;; add their definition after it, since LaTeX doesn't support
1311 (org-e-latex--delayed-footnotes-definitions def info
)))))))
1316 (defun org-e-latex-headline (headline contents info
)
1317 "Transcode an HEADLINE element from Org to LaTeX.
1318 CONTENTS holds the contents of the headline. INFO is a plist
1319 holding contextual information."
1320 (let* ((class (plist-get info
:latex-class
))
1321 (level (org-export-get-relative-level headline info
))
1322 (numberedp (org-export-numbered-headline-p headline info
))
1323 (class-sectionning (assoc class org-e-latex-classes
))
1324 ;; Section formatting will set two placeholders: one for the
1325 ;; title and the other for the contents.
1327 (let ((sec (if (and (symbolp (nth 2 class-sectionning
))
1328 (fboundp (nth 2 class-sectionning
)))
1329 (funcall (nth 2 class-sectionning
) level numberedp
)
1330 (nth (1+ level
) class-sectionning
))))
1332 ;; No section available for that LEVEL.
1334 ;; Section format directly returned by a function.
1336 ;; (numbered-section . unnumbered-section)
1337 ((not (consp (cdr sec
)))
1338 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1339 ;; (numbered-open numbered-close)
1341 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1342 ;; (num-in num-out no-num-in no-num-out)
1344 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1345 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1346 (text (org-export-data (org-element-property :title headline
) info
))
1348 (and (plist-get info
:with-todo-keywords
)
1349 (let ((todo (org-element-property :todo-keyword headline
)))
1350 (and todo
(org-export-data todo info
)))))
1351 (todo-type (and todo
(org-element-property :todo-type headline
)))
1352 (tags (and (plist-get info
:with-tags
)
1353 (org-export-get-tags headline info
)))
1354 (priority (and (plist-get info
:with-priority
)
1355 (org-element-property :priority headline
)))
1356 ;; Create the headline text along with a no-tag version. The
1357 ;; latter is required to remove tags from table of contents.
1358 (full-text (if (functionp org-e-latex-format-headline-function
)
1359 ;; User-defined formatting function.
1360 (funcall org-e-latex-format-headline-function
1361 todo todo-type priority text tags
)
1362 ;; Default formatting.
1365 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1366 (when priority
(format "\\framebox{\\#%c} " priority
))
1369 (format "\\hfill{}\\textsc{:%s:}"
1370 (mapconcat 'identity tags
":"))))))
1372 (if (functionp org-e-latex-format-headline-function
)
1373 ;; User-defined formatting function.
1374 (funcall org-e-latex-format-headline-function
1375 todo todo-type priority text nil
)
1376 ;; Default formatting.
1378 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1379 (when priority
(format "\\framebox{\\#%c} " priority
))
1381 ;; Associate some \label to the headline for internal links.
1383 (format "\\label{sec-%s}\n"
1384 (mapconcat 'number-to-string
1385 (org-export-get-headline-number headline info
)
1388 (make-string (org-element-property :pre-blank headline
) 10)))
1390 ;; Case 1: This is a footnote section: ignore it.
1391 ((org-element-property :footnote-section-p headline
) nil
)
1392 ;; Case 2. This is a deep sub-tree: export it as a list item.
1393 ;; Also export as items headlines for which no section
1394 ;; format has been found.
1395 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1396 ;; Build the real contents of the sub-tree.
1397 (let ((low-level-body
1399 ;; If the headline is the first sibling, start a list.
1400 (when (org-export-first-sibling-p headline
)
1401 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1403 "\\item " full-text
"\n" headline-label pre-blanks contents
)))
1404 ;; If headline is not the last sibling simply return
1405 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1407 (if (not (org-export-last-sibling-p headline
)) low-level-body
1408 (replace-regexp-in-string
1410 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1412 ;; Case 3. Standard headline. Export it as a section.
1415 ((not (and tags
(eq (plist-get info
:with-tags
) 'not-in-toc
)))
1416 ;; Regular section. Use specified format string.
1417 (format section-fmt full-text
1418 (concat headline-label pre-blanks contents
)))
1419 ((string-match "\\`\\\\\\(.*?\\){" section-fmt
)
1420 ;; If tags should be removed from table of contents, insert
1421 ;; title without tags as an alternative heading in sectioning
1423 (format (replace-match (concat (match-string 1 section-fmt
) "[%s]")
1424 nil nil section-fmt
1)
1425 ;; Replace square brackets with parenthesis since
1426 ;; square brackets are not supported in optional
1428 (replace-regexp-in-string
1430 (replace-regexp-in-string
1434 (concat headline-label pre-blanks contents
)))
1436 ;; Impossible to add an alternative heading. Fallback to
1437 ;; regular sectioning format string.
1438 (format section-fmt full-text
1439 (concat headline-label pre-blanks contents
))))))))
1442 ;;;; Horizontal Rule
1444 (defun org-e-latex-horizontal-rule (horizontal-rule contents info
)
1445 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1446 CONTENTS is nil. INFO is a plist holding contextual information."
1447 (let ((attr (mapconcat #'identity
1448 (org-element-property :attr_latex horizontal-rule
)
1450 (org-e-latex--wrap-label horizontal-rule
(concat "\\hrule " attr
))))
1453 ;;;; Inline Babel Call
1455 ;; Inline Babel Calls are ignored.
1458 ;;;; Inline Src Block
1460 (defun org-e-latex-inline-src-block (inline-src-block contents info
)
1461 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1462 CONTENTS holds the contents of the item. INFO is a plist holding
1463 contextual information."
1464 (let* ((code (org-element-property :value inline-src-block
))
1465 (separator (org-e-latex--find-verb-separator code
)))
1467 ;; Do not use a special package: transcode it verbatim.
1468 ((not org-e-latex-listings
)
1469 (concat "\\verb" separator code separator
))
1470 ;; Use minted package.
1471 ((eq org-e-latex-listings
'minted
)
1472 (let* ((org-lang (org-element-property :language inline-src-block
))
1473 (mint-lang (or (cadr (assq (intern org-lang
)
1474 org-e-latex-minted-langs
))
1476 (options (org-e-latex--make-option-string
1477 org-e-latex-minted-options
)))
1478 (concat (format "\\mint%s{%s}"
1479 (if (string= options
"") "" (format "[%s]" options
))
1481 separator code separator
)))
1482 ;; Use listings package.
1484 ;; Maybe translate language's name.
1485 (let* ((org-lang (org-element-property :language inline-src-block
))
1486 (lst-lang (or (cadr (assq (intern org-lang
)
1487 org-e-latex-listings-langs
))
1489 (options (org-e-latex--make-option-string
1490 (append org-e-latex-listings-options
1491 `(("language" ,lst-lang
))))))
1492 (concat (format "\\lstinline[%s]" options
)
1493 separator code separator
))))))
1498 (defun org-e-latex-inlinetask (inlinetask contents info
)
1499 "Transcode an INLINETASK element from Org to LaTeX.
1500 CONTENTS holds the contents of the block. INFO is a plist
1501 holding contextual information."
1502 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1503 (todo (and (plist-get info
:with-todo-keywords
)
1504 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1505 (and todo
(org-export-data todo info
)))))
1506 (todo-type (org-element-property :todo-type inlinetask
))
1507 (tags (and (plist-get info
:with-tags
)
1508 (org-export-get-tags inlinetask info
)))
1509 (priority (and (plist-get info
:with-priority
)
1510 (org-element-property :priority inlinetask
))))
1511 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1512 ;; with appropriate arguments.
1513 (if (functionp org-e-latex-format-inlinetask-function
)
1514 (funcall org-e-latex-format-inlinetask-function
1515 todo todo-type priority title tags contents
)
1516 ;; Otherwise, use a default template.
1517 (org-e-latex--wrap-label
1521 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1522 (when priority
(format "\\framebox{\\#%c} " priority
))
1524 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1525 (mapconcat 'identity tags
":"))))))
1526 (format (concat "\\begin{center}\n"
1528 "\\begin{minipage}[c]{.6\\textwidth}\n"
1530 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1535 full-title contents
))))))
1540 (defun org-e-latex-italic (italic contents info
)
1541 "Transcode ITALIC from Org to LaTeX.
1542 CONTENTS is the text with italic markup. INFO is a plist holding
1543 contextual information."
1544 (org-e-latex--text-markup contents
'italic
))
1549 (defun org-e-latex-item (item contents info
)
1550 "Transcode an ITEM element from Org to LaTeX.
1551 CONTENTS holds the contents of the item. INFO is a plist holding
1552 contextual information."
1554 (let ((count (org-element-property :counter item
))
1556 (loop for parent in
(org-export-get-genealogy item
)
1557 count
(eq (org-element-type parent
) 'plain-list
)
1558 until
(eq (org-element-type parent
) 'headline
))))
1561 (format "\\setcounter{enum%s}{%s}\n"
1562 (nth (1- level
) '("i" "ii" "iii" "iv"))
1564 (checkbox (case (org-element-property :checkbox item
)
1565 (on "$\\boxtimes$ ")
1567 (trans "$\\boxminus$ ")))
1568 (tag (let ((tag (org-element-property :tag item
)))
1569 ;; Check-boxes must belong to the tag.
1570 (and tag
(format "[%s] "
1572 (org-export-data tag info
)))))))
1573 (concat counter
"\\item" (or tag
(concat " " checkbox
))
1575 ;; If there are footnotes references in tag, be sure to
1576 ;; add their definition at the end of the item. This
1577 ;; workaround is necessary since "\footnote{}" command is
1578 ;; not supported in tags.
1580 (org-e-latex--delayed-footnotes-definitions
1581 (org-element-property :tag item
) info
)))))
1586 (defun org-e-latex-keyword (keyword contents info
)
1587 "Transcode a KEYWORD element from Org to LaTeX.
1588 CONTENTS is nil. INFO is a plist holding contextual information."
1589 (let ((key (org-element-property :key keyword
))
1590 (value (org-element-property :value keyword
)))
1592 ((string= key
"LATEX") value
)
1593 ((string= key
"INDEX") (format "\\index{%s}" value
))
1594 ;; Invisible targets.
1595 ((string= key
"TARGET") nil
)
1596 ((string= key
"TOC")
1597 (let ((value (downcase value
)))
1599 ((string-match "\\<headlines\\>" value
)
1600 (let ((depth (or (and (string-match "[0-9]+" value
)
1601 (string-to-number (match-string 0 value
)))
1602 (plist-get info
:with-toc
))))
1604 (when (wholenump depth
)
1605 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1606 "\\tableofcontents")))
1607 ((string= "tables" value
) "\\listoftables")
1608 ((string= "figures" value
) "\\listoffigures")
1609 ((string= "listings" value
)
1611 ((eq org-e-latex-listings
'minted
) "\\listoflistings")
1612 (org-e-latex-listings "\\lstlistoflistings")
1613 ;; At the moment, src blocks with a caption are wrapped
1614 ;; into a figure environment.
1615 (t "\\listoffigures")))))))))
1618 ;;;; Latex Environment
1620 (defun org-e-latex-latex-environment (latex-environment contents info
)
1621 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1622 CONTENTS is nil. INFO is a plist holding contextual information."
1623 (let ((label (org-element-property :name latex-environment
))
1624 (value (org-remove-indentation
1625 (org-element-property :value latex-environment
))))
1626 (if (not (org-string-nw-p label
)) value
1627 ;; Environment is labelled: label must be within the environment
1628 ;; (otherwise, a reference pointing to that element will count
1629 ;; the section instead).
1632 (goto-char (point-min))
1634 (insert (format "\\label{%s}\n" label
))
1640 (defun org-e-latex-latex-fragment (latex-fragment contents info
)
1641 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1642 CONTENTS is nil. INFO is a plist holding contextual information."
1643 (org-element-property :value latex-fragment
))
1648 (defun org-e-latex-line-break (line-break contents info
)
1649 "Transcode a LINE-BREAK object from Org to LaTeX.
1650 CONTENTS is nil. INFO is a plist holding contextual information."
1656 (defun org-e-latex-link--inline-image (link info
)
1657 "Return LaTeX code for an inline image.
1658 LINK is the link pointing to the inline image. INFO is a plist
1659 used as a communication channel."
1660 (let* ((parent (org-export-get-parent-element link
))
1661 (path (let ((raw-path (org-element-property :path link
)))
1662 (if (not (file-name-absolute-p raw-path
)) raw-path
1663 (expand-file-name raw-path
))))
1664 (caption (org-e-latex--caption/label-string
1665 (org-element-property :caption parent
)
1666 (org-element-property :name parent
)
1668 ;; Retrieve latex attributes from the element around.
1669 (attr (let ((raw-attr
1670 (mapconcat #'identity
1671 (org-element-property :attr_latex parent
)
1673 (unless (string= raw-attr
"") raw-attr
)))
1676 ((and attr
(string-match "\\<wrap\\>" attr
)) 'wrap
)
1677 ((and attr
(string-match "\\<multicolumn\\>" attr
)) 'multicolumn
)
1678 ((or (and attr
(string-match "\\<float\\>" attr
))
1679 (not (string= caption
"")))
1683 ((and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
1684 (org-match-string-no-properties 1 attr
))
1685 ((eq disposition
'wrap
) "{l}{0.5\\textwidth}")
1686 ((eq disposition
'float
)
1687 (concat "[" org-e-latex-default-figure-position
"]"))
1689 ;; Now clear ATTR from any special keyword and set a default
1690 ;; value if nothing is left.
1694 (replace-regexp-in-string
1695 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr
))))
1696 (setq attr
(cond ((not (string= attr
"")) attr
)
1697 ((eq disposition
'float
) "width=0.7\\textwidth")
1698 ((eq disposition
'wrap
) "width=0.48\\textwidth")
1699 (t (or org-e-latex-image-default-option
""))))
1700 ;; Return proper string, depending on DISPOSITION.
1702 (wrap (format "\\begin{wrapfigure}%s
1704 \\includegraphics[%s]{%s}
1705 %s\\end{wrapfigure}" placement attr path caption
))
1706 (multicolumn (format "\\begin{figure*}%s
1708 \\includegraphics[%s]{%s}
1709 %s\\end{figure*}" placement attr path caption
))
1710 (float (format "\\begin{figure}%s
1712 \\includegraphics[%s]{%s}
1713 %s\\end{figure}" placement attr path caption
))
1714 (t (format "\\includegraphics[%s]{%s}" attr path
)))))
1716 (defun org-e-latex-link (link desc info
)
1717 "Transcode a LINK object from Org to LaTeX.
1719 DESC is the description part of the link, or the empty string.
1720 INFO is a plist holding contextual information. See
1722 (let* ((type (org-element-property :type link
))
1723 (raw-path (org-element-property :path link
))
1724 ;; Ensure DESC really exists, or set it to nil.
1725 (desc (and (not (string= desc
"")) desc
))
1726 (imagep (org-export-inline-image-p
1727 link org-e-latex-inline-image-rules
))
1729 ((member type
'("http" "https" "ftp" "mailto"))
1730 (concat type
":" raw-path
))
1731 ((string= type
"file")
1732 (when (string-match "\\(.+\\)::.+" raw-path
)
1733 (setq raw-path
(match-string 1 raw-path
)))
1734 (if (file-name-absolute-p raw-path
)
1735 (concat "file://" (expand-file-name raw-path
))
1736 (concat "file://" raw-path
)))
1741 (imagep (org-e-latex-link--inline-image link info
))
1742 ;; Radio link: Transcode target's contents and use them as link's
1744 ((string= type
"radio")
1745 (let ((destination (org-export-resolve-radio-link link info
)))
1747 (format "\\hyperref[%s]{%s}"
1748 (org-export-solidify-link-text path
)
1749 (org-export-data (org-element-contents destination
) info
)))))
1750 ;; Links pointing to an headline: Find destination and build
1751 ;; appropriate referencing command.
1752 ((member type
'("custom-id" "fuzzy" "id"))
1753 (let ((destination (if (string= type
"fuzzy")
1754 (org-export-resolve-fuzzy-link link info
)
1755 (org-export-resolve-id-link link info
))))
1756 (case (org-element-type destination
)
1757 ;; Id link points to an external file.
1759 (if desc
(format "\\href{file://%s}{%s}" destination desc
)
1760 (format "\\url{file://%s}" destination
)))
1761 ;; Fuzzy link points nowhere.
1763 (format org-e-latex-link-with-unknown-path-format
1766 (org-element-property :raw-link link
) info
))))
1767 ;; Fuzzy link points to an invisible target.
1769 ;; LINK points to an headline. If headlines are numbered
1770 ;; and the link has no description, display headline's
1771 ;; number. Otherwise, display description or headline's
1778 (org-export-get-headline-number destination info
)
1780 (if (and (plist-get info
:section-numbers
) (not desc
))
1781 (format "\\ref{%s}" label
)
1782 (format "\\hyperref[%s]{%s}" label
1785 (org-element-property :title destination
) info
))))))
1786 ;; Fuzzy link points to a target. Do as above.
1788 (let ((path (org-export-solidify-link-text path
)))
1789 (if (not desc
) (format "\\ref{%s}" path
)
1790 (format "\\hyperref[%s]{%s}" path desc
)))))))
1791 ;; Coderef: replace link with the reference name or the
1792 ;; equivalent line number.
1793 ((string= type
"coderef")
1794 (format (org-export-get-coderef-format path desc
)
1795 (org-export-resolve-coderef path info
)))
1796 ;; Link type is handled by a special function.
1797 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1798 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1799 ;; External link with a description part.
1800 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1801 ;; External link without a description part.
1802 (path (format "\\url{%s}" path
))
1803 ;; No path, only description. Try to do something useful.
1804 (t (format org-e-latex-link-with-unknown-path-format desc
)))))
1809 (defun org-e-latex-macro (macro contents info
)
1810 "Transcode a MACRO element from Org to LaTeX.
1811 CONTENTS is nil. INFO is a plist holding contextual information."
1812 ;; Use available tools.
1813 (org-export-expand-macro macro info
))
1818 (defun org-e-latex-paragraph (paragraph contents info
)
1819 "Transcode a PARAGRAPH element from Org to LaTeX.
1820 CONTENTS is the contents of the paragraph, as a string. INFO is
1821 the plist used as a communication channel."
1827 (defun org-e-latex-plain-list (plain-list contents info
)
1828 "Transcode a PLAIN-LIST element from Org to LaTeX.
1829 CONTENTS is the contents of the list. INFO is a plist holding
1830 contextual information."
1831 (let* ((type (org-element-property :type plain-list
))
1832 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1833 "inparadesc" "asparadesc"))
1834 (paralist-regexp (concat
1836 (mapconcat 'identity paralist-types
"\\|")
1838 (attr (mapconcat #'identity
1839 (org-element-property :attr_latex plain-list
)
1844 (format "\\<%s\\>" paralist-regexp
) attr
))
1845 (match-string 1 attr
))
1846 ((eq type
'ordered
) "enumerate")
1847 ((eq type
'unordered
) "itemize")
1848 ((eq type
'descriptive
) "description"))))
1849 (org-e-latex--wrap-label
1851 (format "\\begin{%s}%s\n%s\\end{%s}"
1853 ;; Once special environment, if any, has been removed, the
1854 ;; rest of the attributes will be optional arguments.
1855 ;; They will be put inside square brackets if necessary.
1856 (let ((opt (replace-regexp-in-string
1857 (format " *%s *" paralist-regexp
) "" attr
)))
1858 (cond ((string= opt
"") "")
1859 ((string-match "\\`\\[[^][]+\\]\\'" opt
) opt
)
1860 (t (format "[%s]" opt
))))
1867 (defun org-e-latex-plain-text (text info
)
1868 "Transcode a TEXT string from Org to LaTeX.
1869 TEXT is the string to transcode. INFO is a plist holding
1870 contextual information."
1871 ;; Protect %, #, &, $, ~, ^, _, { and }.
1872 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text
)
1874 (replace-match (format "\\%s" (match-string 2 text
)) nil t text
2)))
1876 (setq text
(replace-regexp-in-string
1877 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1878 "$\\backslash$" text nil t
1))
1879 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1880 (let ((case-fold-search nil
)
1882 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start
)
1883 (setq text
(replace-match
1884 (format "\\%s{}" (match-string 1 text
)) nil t text
)
1885 start
(match-end 0))))
1886 ;; Handle quotation marks
1887 (setq text
(org-e-latex--quotation-marks text info
))
1888 ;; Convert special strings.
1889 (when (plist-get info
:with-special-strings
)
1890 (while (string-match (regexp-quote "...") text
)
1891 (setq text
(replace-match "\\ldots{}" nil t text
))))
1892 ;; Handle break preservation if required.
1893 (when (plist-get info
:preserve-breaks
)
1894 (setq text
(replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1902 (defun org-e-latex-planning (planning contents info
)
1903 "Transcode a PLANNING element from Org to LaTeX.
1904 CONTENTS is nil. INFO is a plist holding contextual
1912 (let ((closed (org-element-property :closed planning
)))
1915 (format "\\textbf{%s} " org-closed-string
)
1916 (format org-e-latex-inactive-timestamp-format
1917 (org-translate-time closed
)))))
1918 (let ((deadline (org-element-property :deadline planning
)))
1921 (format "\\textbf{%s} " org-deadline-string
)
1922 (format org-e-latex-active-timestamp-format
1923 (org-translate-time deadline
)))))
1924 (let ((scheduled (org-element-property :scheduled planning
)))
1927 (format "\\textbf{%s} " org-scheduled-string
)
1928 (format org-e-latex-active-timestamp-format
1929 (org-translate-time scheduled
)))))))
1934 ;;;; Property Drawer
1936 (defun org-e-latex-property-drawer (property-drawer contents info
)
1937 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1938 CONTENTS is nil. INFO is a plist holding contextual
1940 ;; The property drawer isn't exported but we want separating blank
1941 ;; lines nonetheless.
1947 (defun org-e-latex-quote-block (quote-block contents info
)
1948 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1949 CONTENTS holds the contents of the block. INFO is a plist
1950 holding contextual information."
1951 (org-e-latex--wrap-label
1953 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
1958 (defun org-e-latex-quote-section (quote-section contents info
)
1959 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1960 CONTENTS is nil. INFO is a plist holding contextual information."
1961 (let ((value (org-remove-indentation
1962 (org-element-property :value quote-section
))))
1963 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
1968 (defun org-e-latex-radio-target (radio-target text info
)
1969 "Transcode a RADIO-TARGET object from Org to LaTeX.
1970 TEXT is the text of the target. INFO is a plist holding
1971 contextual information."
1972 (format "\\label{%s}%s"
1973 (org-export-solidify-link-text
1974 (org-element-property :value radio-target
))
1980 (defun org-e-latex-section (section contents info
)
1981 "Transcode a SECTION element from Org to LaTeX.
1982 CONTENTS holds the contents of the section. INFO is a plist
1983 holding contextual information."
1989 (defun org-e-latex-special-block (special-block contents info
)
1990 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1991 CONTENTS holds the contents of the block. INFO is a plist
1992 holding contextual information."
1993 (let ((type (downcase (org-element-property :type special-block
))))
1994 (org-e-latex--wrap-label
1996 (format "\\begin{%s}\n%s\\end{%s}" type contents type
))))
2001 (defun org-e-latex-src-block (src-block contents info
)
2002 "Transcode a SRC-BLOCK element from Org to LaTeX.
2003 CONTENTS holds the contents of the item. INFO is a plist holding
2004 contextual information."
2005 (let* ((lang (org-element-property :language src-block
))
2006 (caption (org-element-property :caption src-block
))
2007 (label (org-element-property :name src-block
))
2008 (custom-env (and lang
2009 (cadr (assq (intern lang
)
2010 org-e-latex-custom-lang-environments
))))
2011 (num-start (case (org-element-property :number-lines src-block
)
2012 (continued (org-export-get-loc src-block info
))
2014 (retain-labels (org-element-property :retain-labels src-block
)))
2016 ;; Case 1. No source fontification.
2017 ((not org-e-latex-listings
)
2018 (let ((caption-str (org-e-latex--caption/label-string caption label info
))
2019 (float-env (when caption
"\\begin{figure}[H]\n%s\n\\end{figure}")))
2023 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2024 (org-export-format-code-default src-block info
))))))
2025 ;; Case 2. Custom environment.
2026 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2028 (org-export-format-code-default src-block info
)
2030 ;; Case 3. Use minted package.
2031 ((eq org-e-latex-listings
'minted
)
2032 (let ((float-env (when (or label caption
)
2033 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2034 (org-e-latex--caption/label-string
2035 caption label info
))))
2038 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2040 (org-e-latex--make-option-string
2041 (if (not num-start
) org-e-latex-minted-options
2042 (append `(("linenos")
2043 ("firstnumber" ,(number-to-string (1+ num-start
))))
2044 org-e-latex-minted-options
)))
2046 (or (cadr (assq (intern lang
) org-e-latex-minted-langs
)) lang
)
2048 (let* ((code-info (org-export-unravel-code src-block
))
2052 (org-split-string (car code-info
) "\n")))))
2053 (org-export-format-code
2055 (lambda (loc num ref
)
2059 ;; Ensure references are flushed to the right,
2060 ;; separated with 6 spaces from the widest line
2062 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2063 (format "(%s)" ref
)))))
2064 nil
(and retain-labels
(cdr code-info
)))))))
2066 (if float-env
(format float-env body
) body
)))
2067 ;; Case 4. Use listings package.
2070 (or (cadr (assq (intern lang
) org-e-latex-listings-langs
)) lang
))
2073 (let ((main (org-export-data (car caption
) info
)))
2074 (if (not (cdr caption
)) (format "{%s}" main
)
2076 (org-export-data (cdr caption
) info
)
2080 (format "\\lstset{%s}\n"
2081 (org-e-latex--make-option-string
2082 (append org-e-latex-listings-options
2083 `(("language" ,lst-lang
))
2084 (when label
`(("label" ,label
)))
2085 (when caption-str
`(("caption" ,caption-str
)))
2086 (cond ((not num-start
) '(("numbers" "none")))
2087 ((zerop num-start
) '(("numbers" "left")))
2088 (t `(("numbers" "left")
2090 ,(number-to-string (1+ num-start
)))))))))
2093 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2094 (let* ((code-info (org-export-unravel-code src-block
))
2098 (org-split-string (car code-info
) "\n")))))
2099 (org-export-format-code
2101 (lambda (loc num ref
)
2105 ;; Ensure references are flushed to the right,
2106 ;; separated with 6 spaces from the widest line of
2108 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2109 (format "(%s)" ref
)))))
2110 nil
(and retain-labels
(cdr code-info
)))))))))))
2113 ;;;; Statistics Cookie
2115 (defun org-e-latex-statistics-cookie (statistics-cookie contents info
)
2116 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2117 CONTENTS is nil. INFO is a plist holding contextual information."
2118 (org-element-property :value statistics-cookie
))
2123 (defun org-e-latex-strike-through (strike-through contents info
)
2124 "Transcode STRIKE-THROUGH from Org to LaTeX.
2125 CONTENTS is the text with strike-through markup. INFO is a plist
2126 holding contextual information."
2127 (org-e-latex--text-markup contents
'strike-through
))
2132 (defun org-e-latex-subscript (subscript contents info
)
2133 "Transcode a SUBSCRIPT object from Org to LaTeX.
2134 CONTENTS is the contents of the object. INFO is a plist holding
2135 contextual information."
2136 (format (if (= (length contents
) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents
))
2141 (defun org-e-latex-superscript (superscript contents info
)
2142 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2143 CONTENTS is the contents of the object. INFO is a plist holding
2144 contextual information."
2145 (format (if (= (length contents
) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents
))
2150 ;; `org-e-latex-table' is the entry point for table transcoding. It
2151 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
2152 ;; delegates the job to either `org-e-latex-table--table.el-table' or
2153 ;; `org-e-latex-table--org-table' functions, depending of the type of
2156 ;; `org-e-latex-table--align-string' is a subroutine used to build
2157 ;; alignment string for Org tables.
2159 (defun org-e-latex-table (table contents info
)
2160 "Transcode a TABLE element from Org to LaTeX.
2161 CONTENTS is the contents of the table. INFO is a plist holding
2162 contextual information."
2164 ;; Case 1: verbatim table.
2165 ((or org-e-latex-tables-verbatim
2166 (let ((attr (mapconcat 'identity
2167 (org-element-property :attr_latex table
)
2169 (and attr
(string-match "\\<verbatim\\>" attr
))))
2170 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2171 ;; Re-create table, without affiliated keywords.
2173 (org-element-interpret-data
2174 `(table nil
,@(org-element-contents table
))))))
2175 ;; Case 2: table.el table. Convert it using appropriate tools.
2176 ((eq (org-element-property :type table
) 'table.el
)
2177 (org-e-latex-table--table.el-table table contents info
))
2178 ;; Case 3: Standard table.
2179 (t (org-e-latex-table--org-table table contents info
))))
2181 (defun org-e-latex-table--align-string (table info
)
2182 "Return an appropriate LaTeX alignment string.
2183 TABLE is the considered table. INFO is a plist used as
2184 a communication channel."
2185 (let ((attr (mapconcat 'identity
2186 (org-element-property :attr_latex table
)
2188 (if (string-match "\\<align=\\(\\S-+\\)" attr
) (match-string 1 attr
)
2190 ;; Extract column groups and alignment from first (non-rule)
2196 (and (eq (org-element-property :type row
) 'standard
) row
))
2200 (let ((borders (org-export-table-cell-borders cell info
)))
2201 ;; Check left border for the first cell only.
2202 (when (and (memq 'left borders
) (not alignment
))
2203 (push "|" alignment
))
2204 (push (case (org-export-table-cell-alignment cell info
)
2209 (when (memq 'right borders
) (push "|" alignment
))))
2211 (apply 'concat
(reverse alignment
))))))
2213 (defun org-e-latex-table--org-table (table contents info
)
2214 "Return appropriate LaTeX code for an Org table.
2216 TABLE is the table type element to transcode. CONTENTS is its
2217 contents, as a string. INFO is a plist used as a communication
2220 This function assumes TABLE has `org' as its `:type' attribute."
2221 (let* ((label (org-element-property :name table
))
2222 (caption (org-e-latex--caption/label-string
2223 (org-element-property :caption table
) label info
))
2224 (attr (mapconcat 'identity
2225 (org-element-property :attr_latex table
)
2227 ;; Determine alignment string.
2228 (alignment (org-e-latex-table--align-string table info
))
2229 ;; Determine environment for the table: longtable, tabular...
2231 ((not attr
) org-e-latex-default-table-environment
)
2232 ((string-match "\\<longtable\\>" attr
) "longtable")
2233 ((string-match "\\<tabular.?\\>" attr
)
2234 (org-match-string-no-properties 0 attr
))
2235 (t org-e-latex-default-table-environment
)))
2236 ;; If table is a float, determine environment: table, table*
2237 ;; or sidewaystable.
2239 ((string= "longtable" table-env
) nil
)
2240 ((and attr
(string-match "\\<sidewaystable\\>" attr
))
2243 (or (string-match (regexp-quote "table*") attr
)
2244 (string-match "\\<multicolumn\\>" attr
)))
2246 ((or (not (string= caption
"")) label
) "table")))
2247 ;; Extract others display options.
2248 (width (and attr
(string-match "\\<width=\\(\\S-+\\)" attr
)
2249 (org-match-string-no-properties 1 attr
)))
2251 (if (and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
2252 (org-match-string-no-properties 1 attr
)
2253 (format "[%s]" org-e-latex-default-figure-position
))))
2254 ;; Prepare the final format string for the table.
2257 ((string= "longtable" table-env
)
2259 "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
2261 (if (or (not org-e-latex-table-caption-above
) (string= "" caption
)) ""
2262 (concat (org-trim caption
) "\\\\\n"))
2264 (if (or org-e-latex-table-caption-above
(string= "" caption
)) ""
2265 (concat (org-trim caption
) "\\\\\n"))))
2267 (t (concat (when float-env
2269 (format "\\begin{%s}%s\n" float-env placement
)
2270 (if org-e-latex-table-caption-above caption
"")))
2271 (when org-e-latex-tables-centered
"\\begin{center}\n")
2272 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2274 (if width
(format "{%s}" width
) "")
2278 (when org-e-latex-tables-centered
"\n\\end{center}")
2280 (concat (if org-e-latex-table-caption-above
"" caption
)
2281 (format "\n\\end{%s}" float-env
))))))))
2283 (defun org-e-latex-table--table.el-table
(table contents info
)
2284 "Return appropriate LaTeX code for a table.el table.
2286 TABLE is the table type element to transcode. CONTENTS is its
2287 contents, as a string. INFO is a plist used as a communication
2290 This function assumes TABLE has `table.el' as its `:type'
2293 ;; Ensure "*org-export-table*" buffer is empty.
2294 (with-current-buffer (get-buffer-create "*org-export-table*")
2296 (let ((output (with-temp-buffer
2297 (insert (org-element-property :value table
))
2299 (re-search-forward "^[ \t]*|[^|]" nil t
)
2300 (table-generate-source 'latex
"*org-export-table*")
2301 (with-current-buffer "*org-export-table*"
2302 (org-trim (buffer-string))))))
2303 (kill-buffer (get-buffer "*org-export-table*"))
2304 ;; Remove left out comments.
2305 (while (string-match "^%.*\n" output
)
2306 (setq output
(replace-match "" t t output
)))
2307 ;; When the "rmlines" attribute is provided, remove all hlines but
2308 ;; the the one separating heading from the table body.
2309 (let ((attr (mapconcat 'identity
2310 (org-element-property :attr_latex table
)
2312 (when (and attr
(string-match "\\<rmlines\\>" attr
))
2313 (let ((n 0) (pos 0))
2314 (while (and (< (length output
) pos
)
2315 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2318 (setq output
(replace-match "" nil nil output
)))))))
2319 (if (not org-e-latex-tables-centered
) output
2320 (format "\\begin{center}\n%s\n\\end{center}" output
))))
2325 (defun org-e-latex-table-cell (table-cell contents info
)
2326 "Transcode a TABLE-CELL element from Org to LaTeX.
2327 CONTENTS is the cell contents. INFO is a plist used as
2328 a communication channel."
2329 (concat (if (and contents
2330 org-e-latex-table-scientific-notation
2331 (string-match orgtbl-exp-regexp contents
))
2332 ;; Use appropriate format string for scientific
2334 (format org-e-latex-table-scientific-notation
2335 (match-string 1 contents
)
2336 (match-string 2 contents
))
2338 (when (org-export-get-next-element table-cell
) " & ")))
2343 (defun org-e-latex-table-row (table-row contents info
)
2344 "Transcode a TABLE-ROW element from Org to LaTeX.
2345 CONTENTS is the contents of the row. INFO is a plist used as
2346 a communication channel."
2347 ;; Rules are ignored since table separators are deduced from
2348 ;; borders of the current row.
2349 (when (eq (org-element-property :type table-row
) 'standard
)
2350 (let* ((attr (mapconcat 'identity
2351 (org-element-property
2352 :attr_latex
(org-export-get-parent table-row
))
2354 (longtablep (and attr
(string-match "\\<longtable\\>" attr
)))
2356 (or (and attr
(string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr
))
2357 org-e-latex-tables-booktabs
))
2358 ;; TABLE-ROW's borders are extracted from its first cell.
2360 (org-export-table-cell-borders
2361 (car (org-element-contents table-row
)) info
)))
2363 ;; When BOOKTABS are activated enforce top-rule even when no
2364 ;; hline was specifically marked.
2365 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2366 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2369 ;; Special case for long tables. Define header and footers.
2370 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2373 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2376 (if booktabsp
"\\midrule" "\\hline")
2377 (if booktabsp
"\\midrule" "\\hline")
2378 ;; Number of columns.
2379 (cdr (org-export-table-dimensions
2380 (org-export-get-parent-table table-row
) info
))))
2381 ;; When BOOKTABS are activated enforce bottom rule even when
2382 ;; no hline was specifically marked.
2383 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2384 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2385 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2390 (defun org-e-latex-target (target contents info
)
2391 "Transcode a TARGET object from Org to LaTeX.
2392 CONTENTS is nil. INFO is a plist holding contextual
2394 (format "\\label{%s}"
2395 (org-export-solidify-link-text (org-element-property :value target
))))
2400 (defun org-e-latex-timestamp (timestamp contents info
)
2401 "Transcode a TIMESTAMP object from Org to LaTeX.
2402 CONTENTS is nil. INFO is a plist holding contextual
2404 (let ((value (org-translate-time (org-element-property :value timestamp
)))
2405 (type (org-element-property :type timestamp
)))
2406 (cond ((memq type
'(active active-range
))
2407 (format org-e-latex-active-timestamp-format value
))
2408 ((memq type
'(inactive inactive-range
))
2409 (format org-e-latex-inactive-timestamp-format value
))
2410 (t (format org-e-latex-diary-timestamp-format value
)))))
2415 (defun org-e-latex-underline (underline contents info
)
2416 "Transcode UNDERLINE from Org to LaTeX.
2417 CONTENTS is the text with underline markup. INFO is a plist
2418 holding contextual information."
2419 (org-e-latex--text-markup contents
'underline
))
2424 (defun org-e-latex-verbatim (verbatim contents info
)
2425 "Transcode a VERBATIM object from Org to LaTeX.
2426 CONTENTS is nil. INFO is a plist used as a communication
2428 (org-e-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2433 (defun org-e-latex-verse-block (verse-block contents info
)
2434 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2435 CONTENTS is verse block contents. INFO is a plist holding
2436 contextual information."
2437 (org-e-latex--wrap-label
2439 ;; In a verse environment, add a line break to each newline
2440 ;; character and change each white space at beginning of a line
2441 ;; into a space of 1 em. Also change each blank line with
2442 ;; a vertical space of 1 em.
2444 (setq contents
(replace-regexp-in-string
2445 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2446 (replace-regexp-in-string
2447 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2448 (while (string-match "^[ \t]+" contents
)
2449 (let ((new-str (format "\\hspace*{%dem}"
2450 (length (match-string 0 contents
)))))
2451 (setq contents
(replace-match new-str nil t contents
))))
2452 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2456 ;;; Interactive functions
2458 (defun org-e-latex-export-to-latex
2459 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2460 "Export current buffer to a LaTeX file.
2462 If narrowing is active in the current buffer, only export its
2465 If a region is active, export that region.
2467 When optional argument SUBTREEP is non-nil, export the sub-tree
2468 at point, extracting information from the headline properties
2471 When optional argument VISIBLE-ONLY is non-nil, don't export
2472 contents of hidden elements.
2474 When optional argument BODY-ONLY is non-nil, only write code
2475 between \"\\begin{document}\" and \"\\end{document}\".
2477 EXT-PLIST, when provided, is a property list with external
2478 parameters overriding Org default settings, but still inferior to
2479 file-local settings.
2481 When optional argument PUB-DIR is set, use it as the publishing
2484 Return output file's name."
2486 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir
)))
2488 'e-latex outfile subtreep visible-only body-only ext-plist
)))
2490 (defun org-e-latex-export-to-pdf
2491 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2492 "Export current buffer to LaTeX then process through to PDF.
2494 If narrowing is active in the current buffer, only export its
2497 If a region is active, export that region.
2499 When optional argument SUBTREEP is non-nil, export the sub-tree
2500 at point, extracting information from the headline properties
2503 When optional argument VISIBLE-ONLY is non-nil, don't export
2504 contents of hidden elements.
2506 When optional argument BODY-ONLY is non-nil, only write code
2507 between \"\\begin{document}\" and \"\\end{document}\".
2509 EXT-PLIST, when provided, is a property list with external
2510 parameters overriding Org default settings, but still inferior to
2511 file-local settings.
2513 When optional argument PUB-DIR is set, use it as the publishing
2516 Return PDF file's name."
2518 (org-e-latex-compile
2519 (org-e-latex-export-to-latex
2520 subtreep visible-only body-only ext-plist pub-dir
)))
2522 (defun org-e-latex-compile (texfile)
2523 "Compile a TeX file.
2525 TEXFILE is the name of the file being compiled. Processing is
2526 done through the command specified in `org-e-latex-pdf-process'.
2528 Return PDF file name or an error if it couldn't be produced."
2529 (let* ((wconfig (current-window-configuration))
2530 (texfile (file-truename texfile
))
2531 (base (file-name-sans-extension texfile
))
2533 (message (format "Processing LaTeX file %s ..." texfile
))
2537 ;; A function is provided: Apply it.
2538 ((functionp org-e-latex-pdf-process
)
2539 (funcall org-e-latex-pdf-process
(shell-quote-argument texfile
)))
2540 ;; A list is provided: Replace %b, %f and %o with appropriate
2541 ;; values in each command before applying it. Output is
2542 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2543 ((consp org-e-latex-pdf-process
)
2544 (let* ((out-dir (or (file-name-directory texfile
) "./"))
2545 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2549 (replace-regexp-in-string
2550 "%b" (shell-quote-argument base
)
2551 (replace-regexp-in-string
2552 "%f" (shell-quote-argument texfile
)
2553 (replace-regexp-in-string
2554 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
2556 org-e-latex-pdf-process
)
2557 ;; Collect standard errors from output buffer.
2558 (setq errors
(org-e-latex-collect-errors outbuf
))))
2559 (t (error "No valid command to process to PDF")))
2560 (let ((pdffile (concat base
".pdf")))
2561 ;; Check for process failure. Provide collected errors if
2563 (if (not (file-exists-p pdffile
))
2564 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2565 (when errors
(concat ": " errors
))))
2566 ;; Else remove log files, when specified, and signal end of
2567 ;; process to user, along with any error encountered.
2568 (when org-e-latex-remove-logfiles
2569 (dolist (ext org-e-latex-logfiles-extensions
)
2570 (let ((file (concat base
"." ext
)))
2571 (when (file-exists-p file
) (delete-file file
)))))
2572 (message (concat "Process completed"
2573 (if (not errors
) "."
2574 (concat " with errors: " errors
)))))
2575 ;; Return output file name.
2577 (set-window-configuration wconfig
))))
2579 (defun org-e-latex-collect-errors (buffer)
2580 "Collect some kind of errors from \"pdflatex\" command output.
2582 BUFFER is the buffer containing output.
2584 Return collected error types as a string, or nil if there was
2586 (with-current-buffer buffer
2588 (goto-char (point-max))
2589 ;; Find final "pdflatex" run.
2590 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t
)
2591 (let ((case-fold-search t
)
2593 (when (save-excursion
2594 (re-search-forward "Reference.*?undefined" nil t
))
2595 (setq errors
(concat errors
" [undefined reference]")))
2596 (when (save-excursion
2597 (re-search-forward "Citation.*?undefined" nil t
))
2598 (setq errors
(concat errors
" [undefined citation]")))
2599 (when (save-excursion
2600 (re-search-forward "Undefined control sequence" nil t
))
2601 (setq errors
(concat errors
" [undefined control sequence]")))
2602 (when (save-excursion
2603 (re-search-forward "^! LaTeX.*?Error" nil t
))
2604 (setq errors
(concat errors
" [LaTeX error]")))
2605 (when (save-excursion
2606 (re-search-forward "^! Package.*?Error" nil t
))
2607 (setq errors
(concat errors
" [package error]")))
2608 (and (org-string-nw-p errors
) (org-trim errors
)))))))
2611 (provide 'org-e-latex
)
2612 ;;; org-e-latex.el ends here