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
))
41 (defvar org-export-latex-default-packages-alist
)
42 (defvar org-export-latex-packages-alist
)
43 (defvar orgtbl-exp-regexp
)
49 (org-export-define-backend e-latex
50 ((bold . org-e-latex-bold
)
51 (center-block . org-e-latex-center-block
)
52 (clock . org-e-latex-clock
)
53 (code . org-e-latex-code
)
54 (drawer . org-e-latex-drawer
)
55 (dynamic-block . org-e-latex-dynamic-block
)
56 (entity . org-e-latex-entity
)
57 (example-block . org-e-latex-example-block
)
58 (export-block . org-e-latex-export-block
)
59 (export-snippet . org-e-latex-export-snippet
)
60 (fixed-width . org-e-latex-fixed-width
)
61 (footnote-definition . org-e-latex-footnote-definition
)
62 (footnote-reference . org-e-latex-footnote-reference
)
63 (headline . org-e-latex-headline
)
64 (horizontal-rule . org-e-latex-horizontal-rule
)
65 (inline-src-block . org-e-latex-inline-src-block
)
66 (inlinetask . org-e-latex-inlinetask
)
67 (italic . org-e-latex-italic
)
68 (item . org-e-latex-item
)
69 (keyword . org-e-latex-keyword
)
70 (latex-environment . org-e-latex-latex-environment
)
71 (latex-fragment . org-e-latex-latex-fragment
)
72 (line-break . org-e-latex-line-break
)
73 (link . org-e-latex-link
)
74 (paragraph . org-e-latex-paragraph
)
75 (plain-list . org-e-latex-plain-list
)
76 (plain-text . org-e-latex-plain-text
)
77 (planning . org-e-latex-planning
)
78 (property-drawer . org-e-latex-property-drawer
)
79 (quote-block . org-e-latex-quote-block
)
80 (quote-section . org-e-latex-quote-section
)
81 (radio-target . org-e-latex-radio-target
)
82 (section . org-e-latex-section
)
83 (special-block . org-e-latex-special-block
)
84 (src-block . org-e-latex-src-block
)
85 (statistics-cookie . org-e-latex-statistics-cookie
)
86 (strike-through . org-e-latex-strike-through
)
87 (subscript . org-e-latex-subscript
)
88 (superscript . org-e-latex-superscript
)
89 (table . org-e-latex-table
)
90 (table-cell . org-e-latex-table-cell
)
91 (table-row . org-e-latex-table-row
)
92 (target . org-e-latex-target
)
93 (template . org-e-latex-template
)
94 (timestamp . org-e-latex-timestamp
)
95 (underline . org-e-latex-underline
)
96 (verbatim . org-e-latex-verbatim
)
97 (verse-block . org-e-latex-verse-block
))
99 :options-alist
((:date
"DATE" nil org-e-latex-date-format t
)
100 (:latex-class
"LATEX_CLASS" nil org-e-latex-default-class t
)
101 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
102 (:latex-header-extra
"LATEX_HEADER" nil nil newline
)))
106 ;;; Internal Variables
108 (defconst org-e-latex-babel-language-alist
109 '(("af" .
"afrikaans")
111 ("bt-br" .
"brazilian")
117 ("de-at" .
"naustrian")
118 ("de-de" .
"ngerman")
121 ("en-au" .
"australian")
122 ("en-ca" .
"canadian")
123 ("en-gb" .
"british")
125 ("en-nz" .
"newzealand")
126 ("en-us" .
"american")
132 ("fr-ca" .
"canadien")
136 ("id" .
"indonesian")
142 ("no-no" .
"nynorsk")
144 ("pt" .
"portuguese")
148 ("sb" .
"uppersorbian")
156 ("uk" .
"ukrainian"))
157 "Alist between language code and corresponding Babel option.")
161 ;;; User Configurable Variables
163 (defgroup org-export-e-latex nil
164 "Options for exporting Org mode files to LaTeX."
165 :tag
"Org Export LaTeX"
171 (defcustom org-e-latex-default-class
"article"
172 "The default LaTeX class."
173 :group
'org-export-e-latex
174 :type
'(string :tag
"LaTeX class"))
176 (defcustom org-e-latex-classes
178 "\\documentclass[11pt]{article}"
179 ("\\section{%s}" .
"\\section*{%s}")
180 ("\\subsection{%s}" .
"\\subsection*{%s}")
181 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
182 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
183 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
185 "\\documentclass[11pt]{report}"
186 ("\\part{%s}" .
"\\part*{%s}")
187 ("\\chapter{%s}" .
"\\chapter*{%s}")
188 ("\\section{%s}" .
"\\section*{%s}")
189 ("\\subsection{%s}" .
"\\subsection*{%s}")
190 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
192 "\\documentclass[11pt]{book}"
193 ("\\part{%s}" .
"\\part*{%s}")
194 ("\\chapter{%s}" .
"\\chapter*{%s}")
195 ("\\section{%s}" .
"\\section*{%s}")
196 ("\\subsection{%s}" .
"\\subsection*{%s}")
197 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
198 "Alist of LaTeX classes and associated header and structure.
199 If #+LaTeX_CLASS is set in the buffer, use its value and the
200 associated information. Here is the structure of each cell:
204 \(numbered-section . unnumbered-section\)
210 The HEADER-STRING is the header that will be inserted into the
211 LaTeX file. It should contain the \\documentclass macro, and
212 anything else that is needed for this setup. To this header, the
213 following commands will be added:
215 - Calls to \\usepackage for all packages mentioned in the
216 variables `org-export-latex-default-packages-alist' and
217 `org-export-latex-packages-alist'. Thus, your header
218 definitions should avoid to also request these packages.
220 - Lines specified via \"#+LaTeX_HEADER:\"
222 If you need more control about the sequence in which the header
223 is built up, or if you want to exclude one of these building
224 blocks for a particular class, you can use the following
225 macro-like placeholders.
227 [DEFAULT-PACKAGES] \\usepackage statements for default packages
228 [NO-DEFAULT-PACKAGES] do not include any of the default packages
229 [PACKAGES] \\usepackage statements for packages
230 [NO-PACKAGES] do not include the packages
231 [EXTRA] the stuff from #+LaTeX_HEADER
232 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
236 \\documentclass{article}
237 [NO-DEFAULT-PACKAGES]
239 \\providecommand{\\alert}[1]{\\textbf{#1}}
242 will omit the default packages, and will include the
243 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
244 then place \\usepackage commands based on the content of
245 `org-export-latex-packages-alist'.
247 If your header, `org-export-latex-default-packages-alist' or
248 `org-export-latex-packages-alist' inserts
249 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
250 replaced with a coding system derived from
251 `buffer-file-coding-system'. See also the variable
252 `org-e-latex-inputenc-alist' for a way to influence this
255 The sectioning structure
256 ------------------------
258 The sectioning structure of the class is given by the elements
259 following the header string. For each sectioning level, a number
260 of strings is specified. A %s formatter is mandatory in each
261 section string and will be replaced by the title of the section.
263 Instead of a cons cell \(numbered . unnumbered\), you can also
264 provide a list of 2 or 4 elements,
266 \(numbered-open numbered-close\)
270 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
272 providing opening and closing strings for a LaTeX environment
273 that should represent the document section. The opening clause
274 should have a %s to represent the section title.
276 Instead of a list of sectioning commands, you can also specify
277 a function name. That function will be called with two
278 parameters, the \(reduced) level of the headline, and a predicate
279 non-nil when the headline should be numbered. It must return
280 a format string in which the section title will be added."
281 :group
'org-export-e-latex
283 (list (string :tag
"LaTeX class")
284 (string :tag
"LaTeX header")
285 (repeat :tag
"Levels" :inline t
288 (string :tag
" numbered")
289 (string :tag
"unnumbered"))
290 (list :tag
"Environment"
291 (string :tag
"Opening (numbered)")
292 (string :tag
"Closing (numbered)")
293 (string :tag
"Opening (unnumbered)")
294 (string :tag
"Closing (unnumbered)"))
295 (function :tag
"Hook computing sectioning"))))))
297 (defcustom org-e-latex-inputenc-alist nil
298 "Alist of inputenc coding system names, and what should really be used.
299 For example, adding an entry
301 (\"utf8\" . \"utf8x\")
303 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
304 are written as utf8 files."
305 :group
'org-export-e-latex
308 (string :tag
"Derived from buffer")
309 (string :tag
"Use this instead"))))
311 (defcustom org-e-latex-date-format
313 "Format string for \\date{...}."
314 :group
'org-export-e-latex
317 (defcustom org-e-latex-title-command
"\\maketitle"
318 "The command used to insert the title just after \\begin{document}.
319 If this string contains the formatting specification \"%s\" then
320 it will be used as a formatting string, passing the title as an
322 :group
'org-export-e-latex
328 (defcustom org-e-latex-format-headline-function nil
329 "Function to format headline text.
331 This function will be called with 5 arguments:
332 TODO the todo keyword (string or nil).
333 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
334 PRIORITY the priority of the headline (integer or nil)
335 TEXT the main headline text (string).
336 TAGS the tags as a list of strings (list of strings or nil).
338 The function result will be used in the section format string.
340 As an example, one could set the variable to the following, in
341 order to reproduce the default set-up:
343 \(defun org-e-latex-format-headline (todo todo-type priority text tags)
344 \"Default format function for an headline.\"
346 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
348 \(format \"\\\\framebox{\\\\#%c} \" priority))
351 \(format \"\\\\hfill{}\\\\textsc{%s}\"
352 \(mapconcat 'identity tags \":\"))))"
353 :group
'org-export-e-latex
359 (defcustom org-e-latex-footnote-separator
"\\textsuperscript{,}\\,"
360 "Text used to separate footnotes."
361 :group
'org-export-e-latex
367 (defcustom org-e-latex-active-timestamp-format
"\\textit{%s}"
368 "A printf format string to be applied to active timestamps."
369 :group
'org-export-e-latex
372 (defcustom org-e-latex-inactive-timestamp-format
"\\textit{%s}"
373 "A printf format string to be applied to inactive timestamps."
374 :group
'org-export-e-latex
377 (defcustom org-e-latex-diary-timestamp-format
"\\textit{%s}"
378 "A printf format string to be applied to diary timestamps."
379 :group
'org-export-e-latex
385 (defcustom org-e-latex-image-default-option
"width=.9\\linewidth"
386 "Default option for images."
387 :group
'org-export-e-latex
390 (defcustom org-e-latex-default-figure-position
"htb"
391 "Default position for latex figures."
392 :group
'org-export-e-latex
395 (defcustom org-e-latex-inline-image-rules
396 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
397 "Rules characterizing image files that can be inlined into LaTeX.
399 A rule consists in an association whose key is the type of link
400 to consider, and value is a regexp that will be matched against
403 Note that, by default, the image extension *actually* allowed
404 depend on the way the LaTeX file is processed. When used with
405 pdflatex, pdf, jpg and png images are OK. When processing
406 through dvi to Postscript, only ps and eps are allowed. The
407 default we use here encompasses both."
408 :group
'org-export-e-latex
409 :type
'(alist :key-type
(string :tag
"Type")
410 :value-type
(regexp :tag
"Path")))
412 (defcustom org-e-latex-link-with-unknown-path-format
"\\texttt{%s}"
413 "Format string for links with unknown path type."
414 :group
'org-export-latex
420 (defcustom org-e-latex-default-table-environment
"tabular"
421 "Default environment used to build tables."
422 :group
'org-export-e-latex
425 (defcustom org-e-latex-tables-centered t
426 "When non-nil, tables are exported in a center environment."
427 :group
'org-export-e-latex
430 (defcustom org-e-latex-tables-verbatim nil
431 "When non-nil, tables are exported verbatim."
432 :group
'org-export-e-latex
435 (defcustom org-e-latex-tables-booktabs nil
436 "When non-nil, display tables in a formal \"booktabs\" style.
437 This option assumes that the \"booktabs\" package is properly
438 loaded in the header of the document. This value can be ignored
439 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
441 :group
'org-export-e-latex
444 (defcustom org-e-latex-table-caption-above t
445 "When non-nil, place caption string at the beginning of the table.
446 Otherwise, place it near the end."
447 :group
'org-export-e-latex
450 (defcustom org-e-latex-table-scientific-notation
"%s\\,(%s)"
451 "Format string to display numbers in scientific notation.
452 The format should have \"%s\" twice, for mantissa and exponent
453 \(i.e. \"%s\\\\times10^{%s}\").
455 When nil, no transformation is made."
456 :group
'org-export-e-latex
458 (string :tag
"Format string")
459 (const :tag
"No formatting")))
464 (defcustom org-e-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
466 (italic .
"\\emph{%s}")
467 (strike-through .
"\\st{%s}")
468 (underline .
"\\underline{%s}")
469 (verbatim . protectedtexttt
))
470 "Alist of LaTeX expressions to convert text markup.
472 The key must be a symbol among `bold', `code', `italic',
473 `strike-through', `underline' and `verbatim'. The value is
474 a formatting string to wrap fontified text with.
476 Value can also be set to the following symbols: `verb' and
477 `protectedtexttt'. For the former, Org will use \"\\verb\" to
478 create a format string and select a delimiter character that
479 isn't in the string. For the latter, Org will use \"\\texttt\"
480 to typeset and try to protect special characters.
482 If no association can be found for a given markup, text will be
484 :group
'org-export-e-latex
486 :options
'(bold code italic strike-through underline verbatim
))
491 (defcustom org-e-latex-format-drawer-function nil
492 "Function called to format a drawer in LaTeX code.
494 The function must accept two parameters:
495 NAME the drawer name, like \"LOGBOOK\"
496 CONTENTS the contents of the drawer.
498 The function should return the string to be exported.
500 For example, the variable could be set to the following function
501 in order to mimic default behaviour:
503 \(defun org-e-latex-format-drawer-default \(name contents\)
504 \"Format a drawer element for LaTeX export.\"
506 :group
'org-export-e-latex
512 (defcustom org-e-latex-format-inlinetask-function nil
513 "Function called to format an inlinetask in LaTeX code.
515 The function must accept six parameters:
516 TODO the todo keyword, as a string
517 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
518 PRIORITY the inlinetask priority, as a string
519 NAME the inlinetask name, as a string.
520 TAGS the inlinetask tags, as a list of strings.
521 CONTENTS the contents of the inlinetask, as a string.
523 The function should return the string to be exported.
525 For example, the variable could be set to the following function
526 in order to mimic default behaviour:
528 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
529 \"Format an inline task element for LaTeX export.\"
533 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
534 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
537 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
538 \(mapconcat 'identity tags \":\")))))
539 \(format (concat \"\\\\begin{center}\\n\"
541 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
543 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
545 \"\\\\end{minipage}}\"
547 full-title contents))"
548 :group
'org-export-e-latex
554 (defcustom org-e-latex-listings nil
555 "Non-nil means export source code using the listings package.
556 This package will fontify source code, possibly even with color.
557 If you want to use this, you also need to make LaTeX use the
558 listings package, and if you want to have color, the color
559 package. Just add these to `org-export-latex-packages-alist',
560 for example using customize, or with something like:
562 \(require 'org-e-latex)
563 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
564 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
568 \(setq org-e-latex-listings 'minted)
570 causes source code to be exported using the minted package as
571 opposed to listings. If you want to use minted, you need to add
572 the minted package to `org-export-latex-packages-alist', for
573 example using customize, or with
575 \(require 'org-e-latex)
576 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
578 In addition, it is necessary to install pygments
579 \(http://pygments.org), and to configure the variable
580 `org-e-latex-pdf-process' so that the -shell-escape option is
582 :group
'org-export-e-latex
584 (const :tag
"Use listings" t
)
585 (const :tag
"Use minted" 'minted
)
586 (const :tag
"Export verbatim" nil
)))
588 (defcustom org-e-latex-listings-langs
589 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
592 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
593 (html "HTML") (xml "XML")
594 (tex "TeX") (latex "TeX")
595 (shell-script "bash")
597 (ocaml "Caml") (caml "Caml")
598 (sql "SQL") (sqlite "sql"))
599 "Alist mapping languages to their listing language counterpart.
600 The key is a symbol, the major mode symbol without the \"-mode\".
601 The value is the string that should be inserted as the language
602 parameter for the listings package. If the mode name and the
603 listings name are the same, the language does not need an entry
604 in this list - but it does not hurt if it is present."
605 :group
'org-export-e-latex
608 (symbol :tag
"Major mode ")
609 (string :tag
"Listings language"))))
611 (defcustom org-e-latex-listings-options nil
612 "Association list of options for the latex listings package.
614 These options are supplied as a comma-separated list to the
615 \\lstset command. Each element of the association list should be
616 a list containing two strings: the name of the option, and the
619 (setq org-e-latex-listings-options
620 '((\"basicstyle\" \"\\small\")
621 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
623 will typeset the code in a small size font with underlined, bold
626 Note that the same options will be applied to blocks of all
628 :group
'org-export-e-latex
631 (string :tag
"Listings option name ")
632 (string :tag
"Listings option value"))))
634 (defcustom org-e-latex-minted-langs
635 '((emacs-lisp "common-lisp")
638 (shell-script "bash")
640 "Alist mapping languages to their minted language counterpart.
641 The key is a symbol, the major mode symbol without the \"-mode\".
642 The value is the string that should be inserted as the language
643 parameter for the minted package. If the mode name and the
644 listings name are the same, the language does not need an entry
645 in this list - but it does not hurt if it is present.
647 Note that minted uses all lower case for language identifiers,
648 and that the full list of language identifiers can be obtained
651 pygmentize -L lexers"
652 :group
'org-export-e-latex
655 (symbol :tag
"Major mode ")
656 (string :tag
"Minted language"))))
658 (defcustom org-e-latex-minted-options nil
659 "Association list of options for the latex minted package.
661 These options are supplied within square brackets in
662 \\begin{minted} environments. Each element of the alist should
663 be a list containing two strings: the name of the option, and the
666 \(setq org-e-latex-minted-options
667 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
669 will result in src blocks being exported with
671 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
673 as the start of the minted environment. Note that the same
674 options will be applied to blocks of all languages."
675 :group
'org-export-e-latex
678 (string :tag
"Minted option name ")
679 (string :tag
"Minted option value"))))
681 (defvar org-e-latex-custom-lang-environments nil
682 "Alist mapping languages to language-specific LaTeX environments.
684 It is used during export of src blocks by the listings and minted
685 latex packages. For example,
687 \(setq org-e-latex-custom-lang-environments
688 '\(\(python \"pythoncode\"\)\)\)
690 would have the effect that if org encounters begin_src python
691 during latex export it will output
700 (defcustom org-e-latex-quotes
702 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"«~")
703 ("\\(\\S-\\)\"" .
"~»")
704 ("\\(\\s-\\|(\\|^\\)'" .
"'"))
706 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"``")
707 ("\\(\\S-\\)\"" .
"''")
708 ("\\(\\s-\\|(\\|^\\)'" .
"`")))
709 "Alist for quotes to use when converting english double-quotes.
711 The CAR of each item in this alist is the language code.
712 The CDR of each item in this alist is a list of three CONS:
713 - the first CONS defines the opening quote;
714 - the second CONS defines the closing quote;
715 - the last CONS defines single quotes.
717 For each item in a CONS, the first string is a regexp
718 for allowed characters before/after the quote, the second
719 string defines the replacement string for this quote."
720 :group
'org-export-e-latex
722 (cons :tag
"Opening quote"
723 (string :tag
"Regexp for char before")
724 (string :tag
"Replacement quote "))
725 (cons :tag
"Closing quote"
726 (string :tag
"Regexp for char after ")
727 (string :tag
"Replacement quote "))
728 (cons :tag
"Single quote"
729 (string :tag
"Regexp for char before")
730 (string :tag
"Replacement quote "))))
735 (defcustom org-e-latex-pdf-process
736 '("pdflatex -interaction nonstopmode -output-directory %o %f"
737 "pdflatex -interaction nonstopmode -output-directory %o %f"
738 "pdflatex -interaction nonstopmode -output-directory %o %f")
739 "Commands to process a LaTeX file to a PDF file.
740 This is a list of strings, each of them will be given to the
741 shell as a command. %f in the command will be replaced by the
742 full file name, %b by the file base name \(i.e. without
743 extension) and %o by the base directory of the file.
745 The reason why this is a list is that it usually takes several
746 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
747 does not have a clever mechanism to detect which of these
748 commands have to be run to get to a stable result, and it also
749 does not do any error checking.
751 By default, Org uses 3 runs of `pdflatex' to do the processing.
752 If you have texi2dvi on your system and if that does not cause
753 the infamous egrep/locale bug:
755 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
757 then `texi2dvi' is the superior choice. Org does offer it as one
758 of the customize options.
760 Alternatively, this may be a Lisp function that does the
761 processing, so you could use this to apply the machinery of
762 AUCTeX or the Emacs LaTeX mode. This function should accept the
763 file name as its single argument."
764 :group
'org-export-pdf
766 (repeat :tag
"Shell command sequence"
767 (string :tag
"Shell command"))
768 (const :tag
"2 runs of pdflatex"
769 ("pdflatex -interaction nonstopmode -output-directory %o %f"
770 "pdflatex -interaction nonstopmode -output-directory %o %f"))
771 (const :tag
"3 runs of pdflatex"
772 ("pdflatex -interaction nonstopmode -output-directory %o %f"
773 "pdflatex -interaction nonstopmode -output-directory %o %f"
774 "pdflatex -interaction nonstopmode -output-directory %o %f"))
775 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
776 ("pdflatex -interaction nonstopmode -output-directory %o %f"
778 "pdflatex -interaction nonstopmode -output-directory %o %f"
779 "pdflatex -interaction nonstopmode -output-directory %o %f"))
780 (const :tag
"2 runs of xelatex"
781 ("xelatex -interaction nonstopmode -output-directory %o %f"
782 "xelatex -interaction nonstopmode -output-directory %o %f"))
783 (const :tag
"3 runs of xelatex"
784 ("xelatex -interaction nonstopmode -output-directory %o %f"
785 "xelatex -interaction nonstopmode -output-directory %o %f"
786 "xelatex -interaction nonstopmode -output-directory %o %f"))
787 (const :tag
"xelatex,bibtex,xelatex,xelatex"
788 ("xelatex -interaction nonstopmode -output-directory %o %f"
790 "xelatex -interaction nonstopmode -output-directory %o %f"
791 "xelatex -interaction nonstopmode -output-directory %o %f"))
792 (const :tag
"texi2dvi"
793 ("texi2dvi -p -b -c -V %f"))
795 ("rubber -d --into %o %f"))
798 (defcustom org-e-latex-logfiles-extensions
799 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
800 "The list of file extensions to consider as LaTeX logfiles."
801 :group
'org-export-e-latex
802 :type
'(repeat (string :tag
"Extension")))
804 (defcustom org-e-latex-remove-logfiles t
805 "Non-nil means remove the logfiles produced by PDF production.
806 These are the .aux, .log, .out, and .toc files."
807 :group
'org-export-e-latex
812 ;;; Internal Functions
814 (defun org-e-latex--caption/label-string
(caption label info
)
815 "Return caption and label LaTeX string for floats.
817 CAPTION is a cons cell of secondary strings, the car being the
818 standard caption and the cdr its short form. LABEL is a string
819 representing the label. INFO is a plist holding contextual
822 If there's no caption nor label, return the empty string.
824 For non-floats, see `org-e-latex--wrap-label'."
825 (let ((label-str (if label
(format "\\label{%s}" label
) "")))
827 ((and (not caption
) (not label
)) "")
828 ((not caption
) (format "\\label{%s}\n" label
))
829 ;; Option caption format with short name.
831 (format "\\caption[%s]{%s%s}\n"
832 (org-export-data (cdr caption
) info
)
834 (org-export-data (car caption
) info
)))
835 ;; Standard caption format.
836 (t (format "\\caption{%s%s}\n"
838 (org-export-data (car caption
) info
))))))
840 (defun org-e-latex--guess-babel-language (header info
)
841 "Set Babel's language according to LANGUAGE keyword.
843 HEADER is the LaTeX header string. INFO is the plist used as
844 a communication channel.
846 Insertion of guessed language only happens when Babel package has
847 explicitly been loaded. Then it is added to the rest of
850 Return the new header."
851 (let ((language-code (plist-get info
:language
)))
852 ;; If no language is set or Babel package is not loaded, return
854 (if (or (not (stringp language-code
))
855 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
857 (let ((options (save-match-data
858 (org-split-string (match-string 1 header
) ",")))
859 (language (cdr (assoc language-code
860 org-e-latex-babel-language-alist
))))
861 ;; If LANGUAGE is already loaded, return header. Otherwise,
862 ;; append LANGUAGE to other options.
863 (if (member language options
) header
864 (replace-match (mapconcat 'identity
865 (append options
(list language
))
867 nil nil header
1))))))
869 (defun org-e-latex--guess-inputenc (header)
870 "Set the coding system in inputenc to what the buffer is.
871 HEADER is the LaTeX header string. Return the new header."
872 (let* ((cs (or (ignore-errors
873 (latexenc-coding-system-to-inputenc
874 buffer-file-coding-system
))
877 ;; First translate if that is requested.
878 (setq cs
(or (cdr (assoc cs org-e-latex-inputenc-alist
)) cs
))
879 ;; Then find the \usepackage statement and replace the option.
880 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
881 cs header t nil
1))))
883 (defun org-e-latex--find-verb-separator (s)
884 "Return a character not used in string S.
885 This is used to choose a separator for constructs like \\verb."
886 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
887 (loop for c across ll
888 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
889 return
(char-to-string c
))))
891 (defun org-e-latex--make-option-string (options)
892 "Return a comma separated string of keywords and values.
893 OPTIONS is an alist where the key is the options keyword as
894 a string, and the value a list containing the keyword value, or
896 (mapconcat (lambda (pair)
898 (when (> (length (second pair
)) 0)
899 (concat "=" (second pair
)))))
903 (defun org-e-latex--quotation-marks (text info
)
904 "Export quotation marks depending on language conventions.
905 TEXT is a string containing quotation marks to be replaced. INFO
906 is a plist used as a communication channel."
909 (while (setq start
(string-match (car l
) text start
))
910 (let ((new-quote (concat (match-string 1 text
) (cdr l
))))
911 (setq text
(replace-match new-quote t t text
))))))
912 (cdr (or (assoc (plist-get info
:language
) org-e-latex-quotes
)
913 ;; Falls back on English.
914 (assoc "en" org-e-latex-quotes
))))
917 (defun org-e-latex--wrap-label (element output
)
918 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
919 This function shouldn't be used for floats. See
920 `org-e-latex--caption/label-string'."
921 (let ((label (org-element-property :name element
)))
922 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
924 (concat (format "\\label{%s}\n" label
) output
))))
926 (defun org-e-latex--text-markup (text markup
)
927 "Format TEXT depending on MARKUP text markup.
928 See `org-e-latex-text-markup-alist' for details."
929 (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist
))))
931 ;; No format string: Return raw text.
933 ;; Handle the `verb' special case: Find and appropriate separator
934 ;; and use "\\verb" command.
936 (let ((separator (org-e-latex--find-verb-separator text
)))
937 (concat "\\verb" separator text separator
)))
938 ;; Handle the `protectedtexttt' special case: Protect some
939 ;; special chars and use "\texttt{%s}" format string.
940 ((eq 'protectedtexttt fmt
)
942 (trans '(("\\" .
"\\textbackslash{}")
943 ("~" .
"\\textasciitilde{}")
944 ("^" .
"\\textasciicircum{}")))
947 (while (string-match "[\\{}$%&_#~^]" text
)
948 (setq char
(match-string 0 text
))
949 (if (> (match-beginning 0) 0)
950 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
951 (setq text
(substring text
(1+ (match-beginning 0))))
952 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
953 rtn
(concat rtn char
)))
954 (setq text
(concat rtn text
)
956 (while (string-match "--" text
)
957 (setq text
(replace-match "-{}-" t t text
)))
959 ;; Else use format string.
960 (t (format fmt text
)))))
962 (defun org-e-latex--delayed-footnotes-definitions (element info
)
963 "Return footnotes definitions in ELEMENT as a string.
965 INFO is a plist used as a communication channel.
967 Footnotes definitions are returned within \"\\footnotetxt{}\"
970 This function is used within constructs that don't support
971 \"\\footnote{}\" command (i.e. an item's tag). In that case,
972 \"\\footnotemark\" is used within the construct and the function
977 "\\footnotetext[%s]{%s}"
978 (org-export-get-footnote-number ref info
)
981 (org-export-get-footnote-definition ref info
) info
))))
982 ;; Find every footnote reference in ELEMENT.
984 search-refs
; For byte-compiler.
988 ;; Return a list of all footnote references never seen
991 data
'footnote-reference
993 (when (org-export-footnote-first-reference-p ref info
)
995 (when (eq (org-element-property :type ref
) 'standard
)
997 (org-export-get-footnote-definition ref info
)))))
999 (reverse all-refs
)))))
1000 (funcall search-refs element
))
1007 (defun org-e-latex-template (contents info
)
1008 "Return complete document string after LaTeX conversion.
1009 CONTENTS is the transcoded contents string. INFO is a plist
1010 holding export options."
1011 (let ((title (org-export-data (plist-get info
:title
) info
)))
1014 (and (plist-get info
:time-stamp-file
)
1015 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1016 ;; Document class and packages.
1017 (let ((class (plist-get info
:latex-class
))
1018 (class-options (plist-get info
:latex-class-options
)))
1019 (org-element-normalize-string
1020 (let* ((header (nth 1 (assoc class org-e-latex-classes
)))
1021 (document-class-string
1022 (and (stringp header
)
1023 (if (not class-options
) header
1024 (replace-regexp-in-string
1025 "^[ \t]*\\\\documentclass\\(\\(\\[.*\\]\\)?\\)"
1026 class-options header t nil
1)))))
1027 (when document-class-string
1028 (org-e-latex--guess-babel-language
1029 (org-e-latex--guess-inputenc
1030 (org-splice-latex-header
1031 document-class-string
1032 org-export-latex-default-packages-alist
; defined in org.el
1033 org-export-latex-packages-alist nil
; defined in org.el
1034 (plist-get info
:latex-header-extra
)))
1036 ;; Possibly limit depth for headline numbering.
1037 (let ((sec-num (plist-get info
:section-numbers
)))
1038 (when (integerp sec-num
)
1039 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1041 (let ((author (and (plist-get info
:with-author
)
1042 (let ((auth (plist-get info
:author
)))
1043 (and auth
(org-export-data auth info
)))))
1044 (email (and (plist-get info
:with-email
)
1045 (org-export-data (plist-get info
:email
) info
))))
1046 (cond ((and author email
(not (string= "" email
)))
1047 (format "\\author{%s\\thanks{%s}}\n" author email
))
1048 (author (format "\\author{%s}\n" author
))
1049 (t "\\author{}\n")))
1051 (let ((date (org-export-data (plist-get info
:date
) info
)))
1052 (and date
(format "\\date{%s}\n" date
)))
1054 (format "\\title{%s}\n" title
)
1055 ;; Hyperref options.
1056 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1057 (or (plist-get info
:keywords
) "")
1058 (or (plist-get info
:description
) "")
1059 (if (not (plist-get info
:with-creator
)) ""
1060 (plist-get info
:creator
)))
1062 "\\begin{document}\n\n"
1064 (org-element-normalize-string
1065 (cond ((string= "" title
) nil
)
1066 ((not (stringp org-e-latex-title-command
)) nil
)
1067 ((string-match "\\(?:[^%]\\|^\\)%s"
1068 org-e-latex-title-command
)
1069 (format org-e-latex-title-command title
))
1070 (t org-e-latex-title-command
)))
1071 ;; Table of contents.
1072 (let ((depth (plist-get info
:with-toc
)))
1074 (concat (when (wholenump depth
)
1075 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1076 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
1080 (let ((creator-info (plist-get info
:with-creator
)))
1082 ((not creator-info
) "")
1083 ((eq creator-info
'comment
)
1084 (format "%% %s\n" (plist-get info
:creator
)))
1085 (t (concat (plist-get info
:creator
) "\n"))))
1087 "\\end{document}")))
1091 ;;; Transcode Functions
1095 ;; Babel Calls are ignored.
1100 (defun org-e-latex-bold (bold contents info
)
1101 "Transcode BOLD from Org to LaTeX.
1102 CONTENTS is the text with bold markup. INFO is a plist holding
1103 contextual information."
1104 (org-e-latex--text-markup contents
'bold
))
1109 (defun org-e-latex-center-block (center-block contents info
)
1110 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1111 CONTENTS holds the contents of the center block. INFO is a plist
1112 holding contextual information."
1113 (org-e-latex--wrap-label
1115 (format "\\begin{center}\n%s\\end{center}" contents
)))
1120 (defun org-e-latex-clock (clock contents info
)
1121 "Transcode a CLOCK element from Org to LaTeX.
1122 CONTENTS is nil. INFO is a plist holding contextual
1126 (format "\\textbf{%s} " org-clock-string
)
1127 (format org-e-latex-inactive-timestamp-format
1128 (concat (org-translate-time (org-element-property :value clock
))
1129 (let ((time (org-element-property :time clock
)))
1130 (and time
(format " (%s)" time
)))))
1136 (defun org-e-latex-code (code contents info
)
1137 "Transcode a CODE object from Org to LaTeX.
1138 CONTENTS is nil. INFO is a plist used as a communication
1140 (org-e-latex--text-markup (org-element-property :value code
) 'code
))
1145 ;; Comments are ignored.
1150 ;; Comment Blocks are ignored.
1155 (defun org-e-latex-drawer (drawer contents info
)
1156 "Transcode a DRAWER element from Org to LaTeX.
1157 CONTENTS holds the contents of the block. INFO is a plist
1158 holding contextual information."
1159 (let* ((name (org-element-property :drawer-name drawer
))
1160 (output (if (functionp org-e-latex-format-drawer-function
)
1161 (funcall org-e-latex-format-drawer-function
1163 ;; If there's no user defined function: simply
1164 ;; display contents of the drawer.
1166 (org-e-latex--wrap-label drawer output
)))
1171 (defun org-e-latex-dynamic-block (dynamic-block contents info
)
1172 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1173 CONTENTS holds the contents of the block. INFO is a plist
1174 holding contextual information. See `org-export-data'."
1175 (org-e-latex--wrap-label dynamic-block contents
))
1180 (defun org-e-latex-entity (entity contents info
)
1181 "Transcode an ENTITY object from Org to LaTeX.
1182 CONTENTS are the definition itself. INFO is a plist holding
1183 contextual information."
1184 (let ((ent (org-element-property :latex entity
)))
1185 (if (org-element-property :latex-math-p entity
) (format "$%s$" ent
) ent
)))
1190 (defun org-e-latex-example-block (example-block contents info
)
1191 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1192 CONTENTS is nil. INFO is a plist holding contextual
1194 (org-e-latex--wrap-label
1196 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1197 (org-export-format-code-default example-block info
))))
1202 (defun org-e-latex-export-block (export-block contents info
)
1203 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1204 CONTENTS is nil. INFO is a plist holding contextual information."
1205 (when (string= (org-element-property :type export-block
) "LATEX")
1206 (org-remove-indentation (org-element-property :value export-block
))))
1211 (defun org-e-latex-export-snippet (export-snippet contents info
)
1212 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1213 CONTENTS is nil. INFO is a plist holding contextual information."
1214 (when (eq (org-export-snippet-backend export-snippet
) 'e-latex
)
1215 (org-element-property :value export-snippet
)))
1220 (defun org-e-latex-fixed-width (fixed-width contents info
)
1221 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1222 CONTENTS is nil. INFO is a plist holding contextual information."
1223 (org-e-latex--wrap-label
1225 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1226 (org-remove-indentation
1227 (org-element-property :value fixed-width
)))))
1230 ;;;; Footnote Definition
1232 ;; Footnote Definitions are ignored.
1235 ;;;; Footnote Reference
1237 ;; Footnote reference export is handled by
1238 ;; `org-e-latex-footnote-reference'.
1240 ;; Internally, `org-e-latex--get-footnote-counter' is used to restore
1241 ;; the value of the LaTeX "footnote" counter after a jump due to
1242 ;; a reference to an already defined footnote. It is only needed in
1243 ;; item tags since the optional argument to \footnotemark is not
1246 (defun org-e-latex--get-footnote-counter (footnote-reference info
)
1247 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1248 INFO is a plist used as a communication channel."
1249 ;; Find original counter value by counting number of footnote
1250 ;; references appearing for the first time before the current
1251 ;; footnote reference.
1252 (let* ((label (org-element-property :label footnote-reference
))
1254 search-ref
; For byte-compiler.
1258 ;; Search footnote references through DATA, filling
1259 ;; SEEN-REFS along the way.
1261 data
'footnote-reference
1263 (let ((fn-lbl (org-element-property :label fn
)))
1265 ;; Anonymous footnote match: return number.
1266 ((eq fn footnote-reference
) (length seen-refs
))
1267 ;; Anonymous footnote: it's always a new one.
1268 ;; Also, be sure to return nil from the `cond' so
1269 ;; `first-match' doesn't get us out of the loop.
1270 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
1271 ;; Label not seen so far: add it so SEEN-REFS.
1273 ;; Also search for subsequent references in
1274 ;; footnote definition so numbering follows reading
1275 ;; logic. Note that we don't have to care about
1276 ;; inline definitions, since `org-element-map'
1277 ;; already traverse them at the right time.
1278 ((not (member fn-lbl seen-refs
))
1279 (push fn-lbl seen-refs
)
1281 (org-export-get-footnote-definition fn info
))))))
1282 ;; Don't enter footnote definitions since it will happen
1283 ;; when their first reference is found.
1284 info
'first-match
'footnote-definition
)))))
1285 (funcall search-ref
(plist-get info
:parse-tree
))))
1287 (defun org-e-latex-footnote-reference (footnote-reference contents info
)
1288 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1289 CONTENTS is nil. INFO is a plist holding contextual information."
1291 ;; Insert separator between two footnotes in a row.
1292 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1293 (when (eq (org-element-type prev
) 'footnote-reference
)
1294 org-e-latex-footnote-separator
))
1296 ;; Use \footnotemark if reference is within an item's tag.
1297 ((eq (org-element-type (org-export-get-parent-element footnote-reference
))
1299 (if (org-export-footnote-first-reference-p footnote-reference info
)
1301 ;; Since we can't specify footnote number as an optional
1302 ;; argument within an item tag, some extra work has to be done
1303 ;; when the footnote has already been referenced. In that
1304 ;; case, set footnote counter to the desired number, use the
1305 ;; footnotemark, then set counter back to its original value.
1307 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1308 (1- (org-export-get-footnote-number footnote-reference info
))
1309 (org-e-latex--get-footnote-counter footnote-reference info
))))
1310 ;; Use \footnotemark if the footnote has already been defined.
1311 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1312 (format "\\footnotemark[%s]{}"
1313 (org-export-get-footnote-number footnote-reference info
)))
1314 ;; Use \footnotemark if reference is within another footnote
1315 ;; reference or footnote definition.
1316 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1317 thereis
(memq (org-element-type parent
)
1318 '(footnote-reference footnote-definition
)))
1320 ;; Otherwise, define it with \footnote command.
1322 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1323 (unless (eq (org-element-type def
) 'org-data
)
1324 (setq def
(cons 'org-data
(cons nil def
))))
1326 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1327 ;; Retrieve all footnote references within the footnote and
1328 ;; add their definition after it, since LaTeX doesn't support
1330 (org-e-latex--delayed-footnotes-definitions def info
)))))))
1335 (defun org-e-latex-headline (headline contents info
)
1336 "Transcode an HEADLINE element from Org to LaTeX.
1337 CONTENTS holds the contents of the headline. INFO is a plist
1338 holding contextual information."
1339 (let* ((class (plist-get info
:latex-class
))
1340 (level (org-export-get-relative-level headline info
))
1341 (numberedp (org-export-numbered-headline-p headline info
))
1342 (class-sectionning (assoc class org-e-latex-classes
))
1343 ;; Section formatting will set two placeholders: one for the
1344 ;; title and the other for the contents.
1346 (let ((sec (if (and (symbolp (nth 2 class-sectionning
))
1347 (fboundp (nth 2 class-sectionning
)))
1348 (funcall (nth 2 class-sectionning
) level numberedp
)
1349 (nth (1+ level
) class-sectionning
))))
1351 ;; No section available for that LEVEL.
1353 ;; Section format directly returned by a function.
1355 ;; (numbered-section . unnumbered-section)
1356 ((not (consp (cdr sec
)))
1357 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1358 ;; (numbered-open numbered-close)
1360 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1361 ;; (num-in num-out no-num-in no-num-out)
1363 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1364 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1365 (text (org-export-data (org-element-property :title headline
) info
))
1367 (and (plist-get info
:with-todo-keywords
)
1368 (let ((todo (org-element-property :todo-keyword headline
)))
1369 (and todo
(org-export-data todo info
)))))
1370 (todo-type (and todo
(org-element-property :todo-type headline
)))
1371 (tags (and (plist-get info
:with-tags
)
1372 (org-export-get-tags headline info
)))
1373 (priority (and (plist-get info
:with-priority
)
1374 (org-element-property :priority headline
)))
1375 ;; Create the headline text along with a no-tag version. The
1376 ;; latter is required to remove tags from table of contents.
1377 (full-text (if (functionp org-e-latex-format-headline-function
)
1378 ;; User-defined formatting function.
1379 (funcall org-e-latex-format-headline-function
1380 todo todo-type priority text tags
)
1381 ;; Default formatting.
1384 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1385 (when priority
(format "\\framebox{\\#%c} " priority
))
1388 (format "\\hfill{}\\textsc{:%s:}"
1389 (mapconcat 'identity tags
":"))))))
1391 (if (functionp org-e-latex-format-headline-function
)
1392 ;; User-defined formatting function.
1393 (funcall org-e-latex-format-headline-function
1394 todo todo-type priority text nil
)
1395 ;; Default formatting.
1397 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1398 (when priority
(format "\\framebox{\\#%c} " priority
))
1400 ;; Associate some \label to the headline for internal links.
1402 (format "\\label{sec-%s}\n"
1403 (mapconcat 'number-to-string
1404 (org-export-get-headline-number headline info
)
1407 (make-string (org-element-property :pre-blank headline
) 10)))
1409 ;; Case 1: This is a footnote section: ignore it.
1410 ((org-element-property :footnote-section-p headline
) nil
)
1411 ;; Case 2. This is a deep sub-tree: export it as a list item.
1412 ;; Also export as items headlines for which no section
1413 ;; format has been found.
1414 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1415 ;; Build the real contents of the sub-tree.
1416 (let ((low-level-body
1418 ;; If the headline is the first sibling, start a list.
1419 (when (org-export-first-sibling-p headline info
)
1420 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1422 "\\item " full-text
"\n" headline-label pre-blanks contents
)))
1423 ;; If headline is not the last sibling simply return
1424 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1426 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1427 (replace-regexp-in-string
1429 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1431 ;; Case 3. Standard headline. Export it as a section.
1434 ((not (and tags
(eq (plist-get info
:with-tags
) 'not-in-toc
)))
1435 ;; Regular section. Use specified format string.
1436 (format section-fmt full-text
1437 (concat headline-label pre-blanks contents
)))
1438 ((string-match "\\`\\\\\\(.*?\\){" section-fmt
)
1439 ;; If tags should be removed from table of contents, insert
1440 ;; title without tags as an alternative heading in sectioning
1442 (format (replace-match (concat (match-string 1 section-fmt
) "[%s]")
1443 nil nil section-fmt
1)
1444 ;; Replace square brackets with parenthesis since
1445 ;; square brackets are not supported in optional
1447 (replace-regexp-in-string
1449 (replace-regexp-in-string
1453 (concat headline-label pre-blanks contents
)))
1455 ;; Impossible to add an alternative heading. Fallback to
1456 ;; regular sectioning format string.
1457 (format section-fmt full-text
1458 (concat headline-label pre-blanks contents
))))))))
1461 ;;;; Horizontal Rule
1463 (defun org-e-latex-horizontal-rule (horizontal-rule contents info
)
1464 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1465 CONTENTS is nil. INFO is a plist holding contextual information."
1466 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
1467 (prev (org-export-get-previous-element horizontal-rule info
)))
1469 ;; Make sure the rule doesn't start at the end of the current
1470 ;; line by separating it with a blank line from previous element.
1472 (let ((prev-blank (org-element-property :post-blank prev
)))
1473 (or (not prev-blank
) (zerop prev-blank
))))
1475 (org-e-latex--wrap-label
1477 (format "\\rule{%s}{%s}"
1478 (or (plist-get attr
:width
) "\\linewidth")
1479 (or (plist-get attr
:thickness
) "0.5pt"))))))
1482 ;;;; Inline Babel Call
1484 ;; Inline Babel Calls are ignored.
1487 ;;;; Inline Src Block
1489 (defun org-e-latex-inline-src-block (inline-src-block contents info
)
1490 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1491 CONTENTS holds the contents of the item. INFO is a plist holding
1492 contextual information."
1493 (let* ((code (org-element-property :value inline-src-block
))
1494 (separator (org-e-latex--find-verb-separator code
)))
1496 ;; Do not use a special package: transcode it verbatim.
1497 ((not org-e-latex-listings
)
1498 (concat "\\verb" separator code separator
))
1499 ;; Use minted package.
1500 ((eq org-e-latex-listings
'minted
)
1501 (let* ((org-lang (org-element-property :language inline-src-block
))
1502 (mint-lang (or (cadr (assq (intern org-lang
)
1503 org-e-latex-minted-langs
))
1505 (options (org-e-latex--make-option-string
1506 org-e-latex-minted-options
)))
1507 (concat (format "\\mint%s{%s}"
1508 (if (string= options
"") "" (format "[%s]" options
))
1510 separator code separator
)))
1511 ;; Use listings package.
1513 ;; Maybe translate language's name.
1514 (let* ((org-lang (org-element-property :language inline-src-block
))
1515 (lst-lang (or (cadr (assq (intern org-lang
)
1516 org-e-latex-listings-langs
))
1518 (options (org-e-latex--make-option-string
1519 (append org-e-latex-listings-options
1520 `(("language" ,lst-lang
))))))
1521 (concat (format "\\lstinline[%s]" options
)
1522 separator code separator
))))))
1527 (defun org-e-latex-inlinetask (inlinetask contents info
)
1528 "Transcode an INLINETASK element from Org to LaTeX.
1529 CONTENTS holds the contents of the block. INFO is a plist
1530 holding contextual information."
1531 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1532 (todo (and (plist-get info
:with-todo-keywords
)
1533 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1534 (and todo
(org-export-data todo info
)))))
1535 (todo-type (org-element-property :todo-type inlinetask
))
1536 (tags (and (plist-get info
:with-tags
)
1537 (org-export-get-tags inlinetask info
)))
1538 (priority (and (plist-get info
:with-priority
)
1539 (org-element-property :priority inlinetask
))))
1540 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1541 ;; with appropriate arguments.
1542 (if (functionp org-e-latex-format-inlinetask-function
)
1543 (funcall org-e-latex-format-inlinetask-function
1544 todo todo-type priority title tags contents
)
1545 ;; Otherwise, use a default template.
1546 (org-e-latex--wrap-label
1550 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1551 (when priority
(format "\\framebox{\\#%c} " priority
))
1553 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1554 (mapconcat 'identity tags
":"))))))
1555 (format (concat "\\begin{center}\n"
1557 "\\begin{minipage}[c]{.6\\textwidth}\n"
1559 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1564 full-title contents
))))))
1569 (defun org-e-latex-italic (italic contents info
)
1570 "Transcode ITALIC from Org to LaTeX.
1571 CONTENTS is the text with italic markup. INFO is a plist holding
1572 contextual information."
1573 (org-e-latex--text-markup contents
'italic
))
1578 (defun org-e-latex-item (item contents info
)
1579 "Transcode an ITEM element from Org to LaTeX.
1580 CONTENTS holds the contents of the item. INFO is a plist holding
1581 contextual information."
1583 (let ((count (org-element-property :counter item
))
1585 (loop for parent in
(org-export-get-genealogy item
)
1586 count
(eq (org-element-type parent
) 'plain-list
)
1587 until
(eq (org-element-type parent
) 'headline
))))
1590 (format "\\setcounter{enum%s}{%s}\n"
1591 (nth (1- level
) '("i" "ii" "iii" "iv"))
1593 (checkbox (case (org-element-property :checkbox item
)
1594 (on "$\\boxtimes$ ")
1596 (trans "$\\boxminus$ ")))
1597 (tag (let ((tag (org-element-property :tag item
)))
1598 ;; Check-boxes must belong to the tag.
1599 (and tag
(format "[%s] "
1601 (org-export-data tag info
)))))))
1602 (concat counter
"\\item" (or tag
(concat " " checkbox
))
1603 (and contents
(org-trim contents
))
1604 ;; If there are footnotes references in tag, be sure to
1605 ;; add their definition at the end of the item. This
1606 ;; workaround is necessary since "\footnote{}" command is
1607 ;; not supported in tags.
1609 (org-e-latex--delayed-footnotes-definitions
1610 (org-element-property :tag item
) info
)))))
1615 (defun org-e-latex-keyword (keyword contents info
)
1616 "Transcode a KEYWORD element from Org to LaTeX.
1617 CONTENTS is nil. INFO is a plist holding contextual information."
1618 (let ((key (org-element-property :key keyword
))
1619 (value (org-element-property :value keyword
)))
1621 ((string= key
"LATEX") value
)
1622 ((string= key
"INDEX") (format "\\index{%s}" value
))
1623 ;; Invisible targets.
1624 ((string= key
"TARGET") nil
)
1625 ((string= key
"TOC")
1626 (let ((value (downcase value
)))
1628 ((string-match "\\<headlines\\>" value
)
1629 (let ((depth (or (and (string-match "[0-9]+" value
)
1630 (string-to-number (match-string 0 value
)))
1631 (plist-get info
:with-toc
))))
1633 (when (wholenump depth
)
1634 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1635 "\\tableofcontents")))
1636 ((string= "tables" value
) "\\listoftables")
1637 ((string= "figures" value
) "\\listoffigures")
1638 ((string= "listings" value
)
1640 ((eq org-e-latex-listings
'minted
) "\\listoflistings")
1641 (org-e-latex-listings "\\lstlistoflistings")
1642 ;; At the moment, src blocks with a caption are wrapped
1643 ;; into a figure environment.
1644 (t "\\listoffigures")))))))))
1647 ;;;; Latex Environment
1649 (defun org-e-latex-latex-environment (latex-environment contents info
)
1650 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1651 CONTENTS is nil. INFO is a plist holding contextual information."
1652 (let ((label (org-element-property :name latex-environment
))
1653 (value (org-remove-indentation
1654 (org-element-property :value latex-environment
))))
1655 (if (not (org-string-nw-p label
)) value
1656 ;; Environment is labelled: label must be within the environment
1657 ;; (otherwise, a reference pointing to that element will count
1658 ;; the section instead).
1661 (goto-char (point-min))
1663 (insert (format "\\label{%s}\n" label
))
1669 (defun org-e-latex-latex-fragment (latex-fragment contents info
)
1670 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1671 CONTENTS is nil. INFO is a plist holding contextual information."
1672 (org-element-property :value latex-fragment
))
1677 (defun org-e-latex-line-break (line-break contents info
)
1678 "Transcode a LINE-BREAK object from Org to LaTeX.
1679 CONTENTS is nil. INFO is a plist holding contextual information."
1685 (defun org-e-latex-link--inline-image (link info
)
1686 "Return LaTeX code for an inline image.
1687 LINK is the link pointing to the inline image. INFO is a plist
1688 used as a communication channel."
1689 (let* ((parent (org-export-get-parent-element link
))
1690 (path (let ((raw-path (org-element-property :path link
)))
1691 (if (not (file-name-absolute-p raw-path
)) raw-path
1692 (expand-file-name raw-path
))))
1693 (caption (org-e-latex--caption/label-string
1694 (org-element-property :caption parent
)
1695 (org-element-property :name parent
)
1697 ;; Retrieve latex attributes from the element around.
1698 (attr (let ((raw-attr
1699 (mapconcat #'identity
1700 (org-element-property :attr_latex parent
)
1702 (unless (string= raw-attr
"") raw-attr
)))
1705 ((and attr
(string-match "\\<wrap\\>" attr
)) 'wrap
)
1706 ((and attr
(string-match "\\<multicolumn\\>" attr
)) 'multicolumn
)
1707 ((or (and attr
(string-match "\\<float\\>" attr
))
1708 (not (string= caption
"")))
1712 ((and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
1713 (org-match-string-no-properties 1 attr
))
1714 ((eq disposition
'wrap
) "{l}{0.5\\textwidth}")
1715 ((eq disposition
'float
)
1716 (concat "[" org-e-latex-default-figure-position
"]"))
1718 ;; Now clear ATTR from any special keyword and set a default
1719 ;; value if nothing is left.
1723 (replace-regexp-in-string
1724 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr
))))
1725 (setq attr
(cond ((not (string= attr
"")) attr
)
1726 ((eq disposition
'float
) "width=0.7\\textwidth")
1727 ((eq disposition
'wrap
) "width=0.48\\textwidth")
1728 (t (or org-e-latex-image-default-option
""))))
1729 ;; Return proper string, depending on DISPOSITION.
1731 (wrap (format "\\begin{wrapfigure}%s
1733 \\includegraphics[%s]{%s}
1734 %s\\end{wrapfigure}" placement attr path caption
))
1735 (multicolumn (format "\\begin{figure*}%s
1737 \\includegraphics[%s]{%s}
1738 %s\\end{figure*}" placement attr path caption
))
1739 (float (format "\\begin{figure}%s
1741 \\includegraphics[%s]{%s}
1742 %s\\end{figure}" placement attr path caption
))
1743 (t (format "\\includegraphics[%s]{%s}" attr path
)))))
1745 (defun org-e-latex-link (link desc info
)
1746 "Transcode a LINK object from Org to LaTeX.
1748 DESC is the description part of the link, or the empty string.
1749 INFO is a plist holding contextual information. See
1751 (let* ((type (org-element-property :type link
))
1752 (raw-path (org-element-property :path link
))
1753 ;; Ensure DESC really exists, or set it to nil.
1754 (desc (and (not (string= desc
"")) desc
))
1755 (imagep (org-export-inline-image-p
1756 link org-e-latex-inline-image-rules
))
1758 ((member type
'("http" "https" "ftp" "mailto"))
1759 (concat type
":" raw-path
))
1760 ((string= type
"file")
1761 (if (file-name-absolute-p raw-path
)
1762 (concat "file://" (expand-file-name raw-path
))
1763 (concat "file://" raw-path
)))
1768 (imagep (org-e-latex-link--inline-image link info
))
1769 ;; Radio link: Transcode target's contents and use them as link's
1771 ((string= type
"radio")
1772 (let ((destination (org-export-resolve-radio-link link info
)))
1774 (format "\\hyperref[%s]{%s}"
1775 (org-export-solidify-link-text path
)
1776 (org-export-data (org-element-contents destination
) info
)))))
1777 ;; Links pointing to an headline: Find destination and build
1778 ;; appropriate referencing command.
1779 ((member type
'("custom-id" "fuzzy" "id"))
1780 (let ((destination (if (string= type
"fuzzy")
1781 (org-export-resolve-fuzzy-link link info
)
1782 (org-export-resolve-id-link link info
))))
1783 (case (org-element-type destination
)
1784 ;; Id link points to an external file.
1786 (if desc
(format "\\href{file://%s}{%s}" destination desc
)
1787 (format "\\url{file://%s}" destination
)))
1788 ;; Fuzzy link points nowhere.
1790 (format org-e-latex-link-with-unknown-path-format
1793 (org-element-property :raw-link link
) info
))))
1794 ;; Fuzzy link points to an invisible target.
1796 ;; LINK points to an headline. If headlines are numbered
1797 ;; and the link has no description, display headline's
1798 ;; number. Otherwise, display description or headline's
1805 (org-export-get-headline-number destination info
)
1807 (if (and (plist-get info
:section-numbers
) (not desc
))
1808 (format "\\ref{%s}" label
)
1809 (format "\\hyperref[%s]{%s}" label
1812 (org-element-property :title destination
) info
))))))
1813 ;; Fuzzy link points to a target. Do as above.
1815 (let ((path (org-export-solidify-link-text path
)))
1816 (if (not desc
) (format "\\ref{%s}" path
)
1817 (format "\\hyperref[%s]{%s}" path desc
)))))))
1818 ;; Coderef: replace link with the reference name or the
1819 ;; equivalent line number.
1820 ((string= type
"coderef")
1821 (format (org-export-get-coderef-format path desc
)
1822 (org-export-resolve-coderef path info
)))
1823 ;; Link type is handled by a special function.
1824 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1825 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1826 ;; External link with a description part.
1827 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1828 ;; External link without a description part.
1829 (path (format "\\url{%s}" path
))
1830 ;; No path, only description. Try to do something useful.
1831 (t (format org-e-latex-link-with-unknown-path-format desc
)))))
1836 (defun org-e-latex-paragraph (paragraph contents info
)
1837 "Transcode a PARAGRAPH element from Org to LaTeX.
1838 CONTENTS is the contents of the paragraph, as a string. INFO is
1839 the plist used as a communication channel."
1845 (defun org-e-latex-plain-list (plain-list contents info
)
1846 "Transcode a PLAIN-LIST element from Org to LaTeX.
1847 CONTENTS is the contents of the list. INFO is a plist holding
1848 contextual information."
1849 (let* ((type (org-element-property :type plain-list
))
1850 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1851 "inparadesc" "asparadesc"))
1852 (paralist-regexp (concat
1854 (mapconcat 'identity paralist-types
"\\|")
1856 (attr (mapconcat #'identity
1857 (org-element-property :attr_latex plain-list
)
1862 (format "\\<%s\\>" paralist-regexp
) attr
))
1863 (match-string 1 attr
))
1864 ((eq type
'ordered
) "enumerate")
1865 ((eq type
'unordered
) "itemize")
1866 ((eq type
'descriptive
) "description"))))
1867 (org-e-latex--wrap-label
1869 (format "\\begin{%s}%s\n%s\\end{%s}"
1871 ;; Once special environment, if any, has been removed, the
1872 ;; rest of the attributes will be optional arguments.
1873 ;; They will be put inside square brackets if necessary.
1874 (let ((opt (replace-regexp-in-string
1875 (format " *%s *" paralist-regexp
) "" attr
)))
1876 (cond ((string= opt
"") "")
1877 ((string-match "\\`\\[[^][]+\\]\\'" opt
) opt
)
1878 (t (format "[%s]" opt
))))
1885 (defun org-e-latex-plain-text (text info
)
1886 "Transcode a TEXT string from Org to LaTeX.
1887 TEXT is the string to transcode. INFO is a plist holding
1888 contextual information."
1889 (let ((specialp (plist-get info
:with-special-strings
)))
1890 ;; Protect %, #, &, $, ~, ^, _, { and }.
1891 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text
)
1893 (replace-match (format "\\%s" (match-string 2 text
)) nil t text
2)))
1894 ;; Protect \. If special strings are used, be careful not to
1895 ;; protect "\" in "\-" constructs.
1896 (let ((symbols (if specialp
"-%$#&{}~^_\\" "%$#&{}~^_\\")))
1898 (replace-regexp-in-string
1899 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols
)
1900 "$\\backslash$" text nil t
1)))
1901 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1902 (let ((case-fold-search nil
)
1904 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start
)
1905 (setq text
(replace-match
1906 (format "\\%s{}" (match-string 1 text
)) nil t text
)
1907 start
(match-end 0))))
1908 ;; Handle quotation marks.
1909 (setq text
(org-e-latex--quotation-marks text info
))
1910 ;; Convert special strings.
1912 (setq text
(replace-regexp-in-string "\\.\\.\\." "\\ldots{}" text nil t
)))
1913 ;; Handle break preservation if required.
1914 (when (plist-get info
:preserve-breaks
)
1915 (setq text
(replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1923 (defun org-e-latex-planning (planning contents info
)
1924 "Transcode a PLANNING element from Org to LaTeX.
1925 CONTENTS is nil. INFO is a plist holding contextual
1933 (let ((closed (org-element-property :closed planning
)))
1936 (format "\\textbf{%s} " org-closed-string
)
1937 (format org-e-latex-inactive-timestamp-format
1938 (org-translate-time closed
)))))
1939 (let ((deadline (org-element-property :deadline planning
)))
1942 (format "\\textbf{%s} " org-deadline-string
)
1943 (format org-e-latex-active-timestamp-format
1944 (org-translate-time deadline
)))))
1945 (let ((scheduled (org-element-property :scheduled planning
)))
1948 (format "\\textbf{%s} " org-scheduled-string
)
1949 (format org-e-latex-active-timestamp-format
1950 (org-translate-time scheduled
)))))))
1955 ;;;; Property Drawer
1957 (defun org-e-latex-property-drawer (property-drawer contents info
)
1958 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1959 CONTENTS is nil. INFO is a plist holding contextual
1961 ;; The property drawer isn't exported but we want separating blank
1962 ;; lines nonetheless.
1968 (defun org-e-latex-quote-block (quote-block contents info
)
1969 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1970 CONTENTS holds the contents of the block. INFO is a plist
1971 holding contextual information."
1972 (org-e-latex--wrap-label
1974 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
1979 (defun org-e-latex-quote-section (quote-section contents info
)
1980 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1981 CONTENTS is nil. INFO is a plist holding contextual information."
1982 (let ((value (org-remove-indentation
1983 (org-element-property :value quote-section
))))
1984 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
1989 (defun org-e-latex-radio-target (radio-target text info
)
1990 "Transcode a RADIO-TARGET object from Org to LaTeX.
1991 TEXT is the text of the target. INFO is a plist holding
1992 contextual information."
1993 (format "\\label{%s}%s"
1994 (org-export-solidify-link-text
1995 (org-element-property :value radio-target
))
2001 (defun org-e-latex-section (section contents info
)
2002 "Transcode a SECTION element from Org to LaTeX.
2003 CONTENTS holds the contents of the section. INFO is a plist
2004 holding contextual information."
2010 (defun org-e-latex-special-block (special-block contents info
)
2011 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2012 CONTENTS holds the contents of the block. INFO is a plist
2013 holding contextual information."
2014 (let ((type (downcase (org-element-property :type special-block
))))
2015 (org-e-latex--wrap-label
2017 (format "\\begin{%s}\n%s\\end{%s}" type contents type
))))
2022 (defun org-e-latex-src-block (src-block contents info
)
2023 "Transcode a SRC-BLOCK element from Org to LaTeX.
2024 CONTENTS holds the contents of the item. INFO is a plist holding
2025 contextual information."
2026 (let* ((lang (org-element-property :language src-block
))
2027 (caption (org-element-property :caption src-block
))
2028 (label (org-element-property :name src-block
))
2029 (custom-env (and lang
2030 (cadr (assq (intern lang
)
2031 org-e-latex-custom-lang-environments
))))
2032 (num-start (case (org-element-property :number-lines src-block
)
2033 (continued (org-export-get-loc src-block info
))
2035 (retain-labels (org-element-property :retain-labels src-block
)))
2037 ;; Case 1. No source fontification.
2038 ((not org-e-latex-listings
)
2039 (let ((caption-str (org-e-latex--caption/label-string caption label info
))
2040 (float-env (when caption
"\\begin{figure}[H]\n%s\n\\end{figure}")))
2044 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2045 (org-export-format-code-default src-block info
))))))
2046 ;; Case 2. Custom environment.
2047 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2049 (org-export-format-code-default src-block info
)
2051 ;; Case 3. Use minted package.
2052 ((eq org-e-latex-listings
'minted
)
2053 (let ((float-env (when (or label caption
)
2054 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2055 (org-e-latex--caption/label-string
2056 caption label info
))))
2059 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2061 (org-e-latex--make-option-string
2062 (if (not num-start
) org-e-latex-minted-options
2063 (append `(("linenos")
2064 ("firstnumber" ,(number-to-string (1+ num-start
))))
2065 org-e-latex-minted-options
)))
2067 (or (cadr (assq (intern lang
) org-e-latex-minted-langs
)) lang
)
2069 (let* ((code-info (org-export-unravel-code src-block
))
2073 (org-split-string (car code-info
) "\n")))))
2074 (org-export-format-code
2076 (lambda (loc num ref
)
2080 ;; Ensure references are flushed to the right,
2081 ;; separated with 6 spaces from the widest line
2083 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2084 (format "(%s)" ref
)))))
2085 nil
(and retain-labels
(cdr code-info
)))))))
2087 (if float-env
(format float-env body
) body
)))
2088 ;; Case 4. Use listings package.
2091 (or (cadr (assq (intern lang
) org-e-latex-listings-langs
)) lang
))
2094 (let ((main (org-export-data (car caption
) info
)))
2095 (if (not (cdr caption
)) (format "{%s}" main
)
2097 (org-export-data (cdr caption
) info
)
2101 (format "\\lstset{%s}\n"
2102 (org-e-latex--make-option-string
2103 (append org-e-latex-listings-options
2104 `(("language" ,lst-lang
))
2105 (when label
`(("label" ,label
)))
2106 (when caption-str
`(("caption" ,caption-str
)))
2107 (cond ((not num-start
) '(("numbers" "none")))
2108 ((zerop num-start
) '(("numbers" "left")))
2109 (t `(("numbers" "left")
2111 ,(number-to-string (1+ num-start
)))))))))
2114 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2115 (let* ((code-info (org-export-unravel-code src-block
))
2119 (org-split-string (car code-info
) "\n")))))
2120 (org-export-format-code
2122 (lambda (loc num ref
)
2126 ;; Ensure references are flushed to the right,
2127 ;; separated with 6 spaces from the widest line of
2129 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2130 (format "(%s)" ref
)))))
2131 nil
(and retain-labels
(cdr code-info
)))))))))))
2134 ;;;; Statistics Cookie
2136 (defun org-e-latex-statistics-cookie (statistics-cookie contents info
)
2137 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2138 CONTENTS is nil. INFO is a plist holding contextual information."
2139 (replace-regexp-in-string
2140 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2145 (defun org-e-latex-strike-through (strike-through contents info
)
2146 "Transcode STRIKE-THROUGH from Org to LaTeX.
2147 CONTENTS is the text with strike-through markup. INFO is a plist
2148 holding contextual information."
2149 (org-e-latex--text-markup contents
'strike-through
))
2154 (defun org-e-latex-subscript (subscript contents info
)
2155 "Transcode a SUBSCRIPT object from Org to LaTeX.
2156 CONTENTS is the contents of the object. INFO is a plist holding
2157 contextual information."
2158 (if (= (length contents
) 1) (format "$_%s$" contents
)
2159 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2160 ;; command for each of them.
2161 (let ((prev-blanks 0))
2164 (case (org-element-type obj
)
2165 ((entity latex-fragment
)
2166 (setq prev-blanks
(org-element-property :post-blank obj
))
2167 (let ((data (org-trim (org-export-data obj info
))))
2169 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2171 (format "$_{%s}$" (match-string 1 data
))))
2173 (format "$_\\mathrm{%s}$"
2174 (concat (make-string prev-blanks ?
)
2175 ;; mathrm command doesn't handle spaces,
2176 ;; so we have to enforce them.
2177 (replace-regexp-in-string
2178 " " "\\\\ " (org-export-data obj info
)))))
2180 (setq prev-blanks
(org-element-property :post-blank obj
))
2181 (format "$_{%s}$" (org-export-data obj info
)))))
2182 (org-element-contents subscript
) ""))))
2187 (defun org-e-latex-superscript (superscript contents info
)
2188 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2189 CONTENTS is the contents of the object. INFO is a plist holding
2190 contextual information."
2191 (if (= (length contents
) 1) (format "$^%s$" contents
)
2192 ;; Handle multiple objects in SUPERSCRIPT by creating
2193 ;; a superscript command for each of them.
2194 (let ((prev-blanks 0))
2197 (case (org-element-type obj
)
2198 ((entity latex-fragment
)
2199 (setq prev-blanks
(org-element-property :post-blank obj
))
2200 (let ((data (org-trim (org-export-data obj info
))))
2202 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2204 (format "$^{%s}$" (match-string 1 data
))))
2206 (format "$^\\mathrm{%s}$"
2207 (concat (make-string prev-blanks ?
)
2208 ;; mathrm command doesn't handle spaces,
2209 ;; so we have to enforce them.
2210 (replace-regexp-in-string
2211 " " "\\\\ " (org-export-data obj info
)))))
2213 (setq prev-blanks
(org-element-property :post-blank obj
))
2214 (format "$^{%s}$" (org-export-data obj info
)))))
2215 (org-element-contents superscript
) ""))))
2220 ;; `org-e-latex-table' is the entry point for table transcoding. It
2221 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
2222 ;; delegates the job to either `org-e-latex-table--table.el-table' or
2223 ;; `org-e-latex-table--org-table' functions, depending of the type of
2226 ;; `org-e-latex-table--align-string' is a subroutine used to build
2227 ;; alignment string for Org tables.
2229 (defun org-e-latex-table (table contents info
)
2230 "Transcode a TABLE element from Org to LaTeX.
2231 CONTENTS is the contents of the table. INFO is a plist holding
2232 contextual information."
2234 ;; Case 1: verbatim table.
2235 ((or org-e-latex-tables-verbatim
2236 (let ((attr (mapconcat 'identity
2237 (org-element-property :attr_latex table
)
2239 (and attr
(string-match "\\<verbatim\\>" attr
))))
2240 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2241 ;; Re-create table, without affiliated keywords.
2243 (org-element-interpret-data
2244 `(table nil
,@(org-element-contents table
))))))
2245 ;; Case 2: table.el table. Convert it using appropriate tools.
2246 ((eq (org-element-property :type table
) 'table.el
)
2247 (org-e-latex-table--table.el-table table contents info
))
2248 ;; Case 3: Standard table.
2249 (t (org-e-latex-table--org-table table contents info
))))
2251 (defun org-e-latex-table--align-string (table info
)
2252 "Return an appropriate LaTeX alignment string.
2253 TABLE is the considered table. INFO is a plist used as
2254 a communication channel."
2255 (let ((attr (mapconcat 'identity
2256 (org-element-property :attr_latex table
)
2258 (if (string-match "\\<align=\\(\\S-+\\)" attr
) (match-string 1 attr
)
2260 ;; Extract column groups and alignment from first (non-rule)
2266 (and (eq (org-element-property :type row
) 'standard
) row
))
2270 (let ((borders (org-export-table-cell-borders cell info
)))
2271 ;; Check left border for the first cell only.
2272 (when (and (memq 'left borders
) (not alignment
))
2273 (push "|" alignment
))
2274 (push (case (org-export-table-cell-alignment cell info
)
2279 (when (memq 'right borders
) (push "|" alignment
))))
2281 (apply 'concat
(reverse alignment
))))))
2283 (defun org-e-latex-table--org-table (table contents info
)
2284 "Return appropriate LaTeX code for an Org 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 `org' as its `:type' attribute."
2291 (let* ((label (org-element-property :name table
))
2292 (caption (org-e-latex--caption/label-string
2293 (org-element-property :caption table
) label info
))
2294 (attr (mapconcat 'identity
2295 (org-element-property :attr_latex table
)
2297 ;; Determine alignment string.
2298 (alignment (org-e-latex-table--align-string table info
))
2299 ;; Determine environment for the table: longtable, tabular...
2301 ((not attr
) org-e-latex-default-table-environment
)
2302 ((string-match "\\<longtable\\>" attr
) "longtable")
2303 ((string-match "\\<tabular.?\\>" attr
)
2304 (org-match-string-no-properties 0 attr
))
2305 (t org-e-latex-default-table-environment
)))
2306 ;; If table is a float, determine environment: table, table*
2307 ;; or sidewaystable.
2309 ((string= "longtable" table-env
) nil
)
2310 ((and attr
(string-match "\\<sidewaystable\\>" attr
))
2313 (or (string-match (regexp-quote "table*") attr
)
2314 (string-match "\\<multicolumn\\>" attr
)))
2316 ((or (not (string= caption
"")) label
) "table")))
2317 ;; Extract others display options.
2318 (width (and attr
(string-match "\\<width=\\(\\S-+\\)" attr
)
2319 (org-match-string-no-properties 1 attr
)))
2321 (if (and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
2322 (org-match-string-no-properties 1 attr
)
2323 (format "[%s]" org-e-latex-default-figure-position
))))
2324 ;; Prepare the final format string for the table.
2327 ((string= "longtable" table-env
)
2329 "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
2331 (if (or (not org-e-latex-table-caption-above
) (string= "" caption
)) ""
2332 (concat (org-trim caption
) "\\\\\n"))
2334 (if (or org-e-latex-table-caption-above
(string= "" caption
)) ""
2335 (concat (org-trim caption
) "\\\\\n"))))
2337 (t (concat (when float-env
2339 (format "\\begin{%s}%s\n" float-env placement
)
2340 (if org-e-latex-table-caption-above caption
"")))
2341 (when org-e-latex-tables-centered
"\\begin{center}\n")
2342 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2344 (if width
(format "{%s}" width
) "")
2348 (when org-e-latex-tables-centered
"\n\\end{center}")
2350 (concat (if org-e-latex-table-caption-above
"" caption
)
2351 (format "\n\\end{%s}" float-env
))))))))
2353 (defun org-e-latex-table--table.el-table
(table contents info
)
2354 "Return appropriate LaTeX code for a table.el table.
2356 TABLE is the table type element to transcode. CONTENTS is its
2357 contents, as a string. INFO is a plist used as a communication
2360 This function assumes TABLE has `table.el' as its `:type'
2363 ;; Ensure "*org-export-table*" buffer is empty.
2364 (with-current-buffer (get-buffer-create "*org-export-table*")
2366 (let ((output (with-temp-buffer
2367 (insert (org-element-property :value table
))
2369 (re-search-forward "^[ \t]*|[^|]" nil t
)
2370 (table-generate-source 'latex
"*org-export-table*")
2371 (with-current-buffer "*org-export-table*"
2372 (org-trim (buffer-string))))))
2373 (kill-buffer (get-buffer "*org-export-table*"))
2374 ;; Remove left out comments.
2375 (while (string-match "^%.*\n" output
)
2376 (setq output
(replace-match "" t t output
)))
2377 ;; When the "rmlines" attribute is provided, remove all hlines but
2378 ;; the the one separating heading from the table body.
2379 (let ((attr (mapconcat 'identity
2380 (org-element-property :attr_latex table
)
2382 (when (and attr
(string-match "\\<rmlines\\>" attr
))
2383 (let ((n 0) (pos 0))
2384 (while (and (< (length output
) pos
)
2385 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2388 (setq output
(replace-match "" nil nil output
)))))))
2389 (if (not org-e-latex-tables-centered
) output
2390 (format "\\begin{center}\n%s\n\\end{center}" output
))))
2395 (defun org-e-latex-table-cell (table-cell contents info
)
2396 "Transcode a TABLE-CELL element from Org to LaTeX.
2397 CONTENTS is the cell contents. INFO is a plist used as
2398 a communication channel."
2399 (concat (if (and contents
2400 org-e-latex-table-scientific-notation
2401 (string-match orgtbl-exp-regexp contents
))
2402 ;; Use appropriate format string for scientific
2404 (format org-e-latex-table-scientific-notation
2405 (match-string 1 contents
)
2406 (match-string 2 contents
))
2408 (when (org-export-get-next-element table-cell info
) " & ")))
2413 (defun org-e-latex-table-row (table-row contents info
)
2414 "Transcode a TABLE-ROW element from Org to LaTeX.
2415 CONTENTS is the contents of the row. INFO is a plist used as
2416 a communication channel."
2417 ;; Rules are ignored since table separators are deduced from
2418 ;; borders of the current row.
2419 (when (eq (org-element-property :type table-row
) 'standard
)
2420 (let* ((attr (mapconcat 'identity
2421 (org-element-property
2422 :attr_latex
(org-export-get-parent table-row
))
2424 (longtablep (and attr
(string-match "\\<longtable\\>" attr
)))
2426 (or (and attr
(string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr
))
2427 org-e-latex-tables-booktabs
))
2428 ;; TABLE-ROW's borders are extracted from its first cell.
2430 (org-export-table-cell-borders
2431 (car (org-element-contents table-row
)) info
)))
2433 ;; When BOOKTABS are activated enforce top-rule even when no
2434 ;; hline was specifically marked.
2435 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2436 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2439 ;; Special case for long tables. Define header and footers.
2440 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2443 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2446 (if booktabsp
"\\midrule" "\\hline")
2447 (if booktabsp
"\\midrule" "\\hline")
2448 ;; Number of columns.
2449 (cdr (org-export-table-dimensions
2450 (org-export-get-parent-table table-row
) info
))))
2451 ;; When BOOKTABS are activated enforce bottom rule even when
2452 ;; no hline was specifically marked.
2453 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2454 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2455 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2460 (defun org-e-latex-target (target contents info
)
2461 "Transcode a TARGET object from Org to LaTeX.
2462 CONTENTS is nil. INFO is a plist holding contextual
2464 (format "\\label{%s}"
2465 (org-export-solidify-link-text (org-element-property :value target
))))
2470 (defun org-e-latex-timestamp (timestamp contents info
)
2471 "Transcode a TIMESTAMP object from Org to LaTeX.
2472 CONTENTS is nil. INFO is a plist holding contextual
2474 (let ((value (org-translate-time (org-element-property :value timestamp
)))
2475 (range-end (org-element-property :range-end timestamp
)))
2476 (case (org-element-property :type timestamp
)
2477 (active (format org-e-latex-active-timestamp-format value
))
2479 (concat (format org-e-latex-active-timestamp-format value
)
2481 (format org-e-latex-active-timestamp-format
2482 (org-translate-time range-end
))))
2483 (inactive (format org-e-latex-inactive-timestamp-format value
))
2485 (concat (format org-e-latex-inactive-timestamp-format value
)
2487 (format org-e-latex-inactive-timestamp-format
2488 (org-translate-time range-end
))))
2489 (otherwise (format org-e-latex-diary-timestamp-format value
)))))
2494 (defun org-e-latex-underline (underline contents info
)
2495 "Transcode UNDERLINE from Org to LaTeX.
2496 CONTENTS is the text with underline markup. INFO is a plist
2497 holding contextual information."
2498 (org-e-latex--text-markup contents
'underline
))
2503 (defun org-e-latex-verbatim (verbatim contents info
)
2504 "Transcode a VERBATIM object from Org to LaTeX.
2505 CONTENTS is nil. INFO is a plist used as a communication
2507 (org-e-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2512 (defun org-e-latex-verse-block (verse-block contents info
)
2513 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2514 CONTENTS is verse block contents. INFO is a plist holding
2515 contextual information."
2516 (org-e-latex--wrap-label
2518 ;; In a verse environment, add a line break to each newline
2519 ;; character and change each white space at beginning of a line
2520 ;; into a space of 1 em. Also change each blank line with
2521 ;; a vertical space of 1 em.
2523 (setq contents
(replace-regexp-in-string
2524 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2525 (replace-regexp-in-string
2526 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2527 (while (string-match "^[ \t]+" contents
)
2528 (let ((new-str (format "\\hspace*{%dem}"
2529 (length (match-string 0 contents
)))))
2530 (setq contents
(replace-match new-str nil t contents
))))
2531 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2535 ;;; Interactive functions
2538 (defun org-e-latex-export-as-latex
2539 (&optional subtreep visible-only body-only ext-plist
)
2540 "Export current buffer as a LaTeX buffer.
2542 If narrowing is active in the current buffer, only export its
2545 If a region is active, export that region.
2547 When optional argument SUBTREEP is non-nil, export the sub-tree
2548 at point, extracting information from the headline properties
2551 When optional argument VISIBLE-ONLY is non-nil, don't export
2552 contents of hidden elements.
2554 When optional argument BODY-ONLY is non-nil, only write code
2555 between \"\\begin{document}\" and \"\\end{document}\".
2557 EXT-PLIST, when provided, is a property list with external
2558 parameters overriding Org default settings, but still inferior to
2559 file-local settings.
2561 Export is done in a buffer named \"*Org E-LATEX Export*\", which
2562 will be displayed when `org-export-show-temporary-export-buffer'
2565 (let ((outbuf (org-export-to-buffer
2566 'e-latex
"*Org E-LATEX Export*"
2567 subtreep visible-only body-only ext-plist
)))
2568 (with-current-buffer outbuf
(LaTeX-mode))
2569 (when org-export-show-temporary-export-buffer
2570 (switch-to-buffer-other-window outbuf
))))
2573 (defun org-e-latex-export-to-latex
2574 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2575 "Export current buffer to a LaTeX file.
2577 If narrowing is active in the current buffer, only export its
2580 If a region is active, export that region.
2582 When optional argument SUBTREEP is non-nil, export the sub-tree
2583 at point, extracting information from the headline properties
2586 When optional argument VISIBLE-ONLY is non-nil, don't export
2587 contents of hidden elements.
2589 When optional argument BODY-ONLY is non-nil, only write code
2590 between \"\\begin{document}\" and \"\\end{document}\".
2592 EXT-PLIST, when provided, is a property list with external
2593 parameters overriding Org default settings, but still inferior to
2594 file-local settings.
2596 When optional argument PUB-DIR is set, use it as the publishing
2599 Return output file's name."
2601 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir
)))
2603 'e-latex outfile subtreep visible-only body-only ext-plist
)))
2606 (defun org-e-latex-export-to-pdf
2607 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2608 "Export current buffer to LaTeX then process through to PDF.
2610 If narrowing is active in the current buffer, only export its
2613 If a region is active, export that region.
2615 When optional argument SUBTREEP is non-nil, export the sub-tree
2616 at point, extracting information from the headline properties
2619 When optional argument VISIBLE-ONLY is non-nil, don't export
2620 contents of hidden elements.
2622 When optional argument BODY-ONLY is non-nil, only write code
2623 between \"\\begin{document}\" and \"\\end{document}\".
2625 EXT-PLIST, when provided, is a property list with external
2626 parameters overriding Org default settings, but still inferior to
2627 file-local settings.
2629 When optional argument PUB-DIR is set, use it as the publishing
2632 Return PDF file's name."
2634 (org-e-latex-compile
2635 (org-e-latex-export-to-latex
2636 subtreep visible-only body-only ext-plist pub-dir
)))
2638 (defun org-e-latex-compile (texfile)
2639 "Compile a TeX file.
2641 TEXFILE is the name of the file being compiled. Processing is
2642 done through the command specified in `org-e-latex-pdf-process'.
2644 Return PDF file name or an error if it couldn't be produced."
2645 (let* ((wconfig (current-window-configuration))
2646 (texfile (file-truename texfile
))
2647 (base (file-name-sans-extension texfile
))
2649 (message (format "Processing LaTeX file %s ..." texfile
))
2653 ;; A function is provided: Apply it.
2654 ((functionp org-e-latex-pdf-process
)
2655 (funcall org-e-latex-pdf-process
(shell-quote-argument texfile
)))
2656 ;; A list is provided: Replace %b, %f and %o with appropriate
2657 ;; values in each command before applying it. Output is
2658 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2659 ((consp org-e-latex-pdf-process
)
2660 (let* ((out-dir (or (file-name-directory texfile
) "./"))
2661 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2665 (replace-regexp-in-string
2666 "%b" (shell-quote-argument base
)
2667 (replace-regexp-in-string
2668 "%f" (shell-quote-argument texfile
)
2669 (replace-regexp-in-string
2670 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
2672 org-e-latex-pdf-process
)
2673 ;; Collect standard errors from output buffer.
2674 (setq errors
(org-e-latex--collect-errors outbuf
))))
2675 (t (error "No valid command to process to PDF")))
2676 (let ((pdffile (concat base
".pdf")))
2677 ;; Check for process failure. Provide collected errors if
2679 (if (not (file-exists-p pdffile
))
2680 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2681 (when errors
(concat ": " errors
))))
2682 ;; Else remove log files, when specified, and signal end of
2683 ;; process to user, along with any error encountered.
2684 (when org-e-latex-remove-logfiles
2685 (dolist (ext org-e-latex-logfiles-extensions
)
2686 (let ((file (concat base
"." ext
)))
2687 (when (file-exists-p file
) (delete-file file
)))))
2688 (message (concat "Process completed"
2689 (if (not errors
) "."
2690 (concat " with errors: " errors
)))))
2691 ;; Return output file name.
2693 (set-window-configuration wconfig
))))
2695 (defun org-e-latex--collect-errors (buffer)
2696 "Collect some kind of errors from \"pdflatex\" command output.
2698 BUFFER is the buffer containing output.
2700 Return collected error types as a string, or nil if there was
2702 (with-current-buffer buffer
2704 (goto-char (point-max))
2705 ;; Find final "pdflatex" run.
2706 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t
)
2707 (let ((case-fold-search t
)
2709 (when (save-excursion
2710 (re-search-forward "Reference.*?undefined" nil t
))
2711 (setq errors
(concat errors
" [undefined reference]")))
2712 (when (save-excursion
2713 (re-search-forward "Citation.*?undefined" nil t
))
2714 (setq errors
(concat errors
" [undefined citation]")))
2715 (when (save-excursion
2716 (re-search-forward "Undefined control sequence" nil t
))
2717 (setq errors
(concat errors
" [undefined control sequence]")))
2718 (when (save-excursion
2719 (re-search-forward "^! LaTeX.*?Error" nil t
))
2720 (setq errors
(concat errors
" [LaTeX error]")))
2721 (when (save-excursion
2722 (re-search-forward "^! Package.*?Error" nil t
))
2723 (setq errors
(concat errors
" [package error]")))
2724 (and (org-string-nw-p errors
) (org-trim errors
)))))))
2727 (provide 'org-e-latex
)
2728 ;;; org-e-latex.el ends here