org-e-latex: Consistently change label names
[org-mode.git] / contrib / lisp / org-e-latex.el
blobb90a097cda71cacc1af07142a463fe710f4969b2
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/>.
21 ;;; Commentary:
23 ;; This library implements a LaTeX back-end for Org generic exporter.
25 ;; To test it, run
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".
36 ;;; Code:
38 (eval-when-compile (require 'cl))
39 (require 'org-export)
41 (defvar org-export-latex-default-packages-alist)
42 (defvar org-export-latex-packages-alist)
43 (defvar orgtbl-exp-regexp)
47 ;;; Define Back-End
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))
98 :export-block ("LATEX" "TEX")
99 :menu-entry
100 (?l "Export to LaTeX"
101 ((?L "As TEX buffer" org-e-latex-export-as-latex)
102 (?l "As TEX file" org-e-latex-export-to-latex)
103 (?p "As PDF file" org-e-latex-export-to-pdf)
104 (?o "As PDF file and open"
105 (lambda (s v b) (org-open-file (org-e-latex-export-to-pdf s v b))))))
106 :options-alist ((:date "DATE" nil org-e-latex-date-format t)
107 (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
108 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
109 (:latex-header-extra "LATEX_HEADER" nil nil newline)))
113 ;;; Internal Variables
115 (defconst org-e-latex-babel-language-alist
116 '(("af" . "afrikaans")
117 ("bg" . "bulgarian")
118 ("bt-br" . "brazilian")
119 ("ca" . "catalan")
120 ("cs" . "czech")
121 ("cy" . "welsh")
122 ("da" . "danish")
123 ("de" . "germanb")
124 ("de-at" . "naustrian")
125 ("de-de" . "ngerman")
126 ("el" . "greek")
127 ("en" . "english")
128 ("en-au" . "australian")
129 ("en-ca" . "canadian")
130 ("en-gb" . "british")
131 ("en-ie" . "irish")
132 ("en-nz" . "newzealand")
133 ("en-us" . "american")
134 ("es" . "spanish")
135 ("et" . "estonian")
136 ("eu" . "basque")
137 ("fi" . "finnish")
138 ("fr" . "frenchb")
139 ("fr-ca" . "canadien")
140 ("gl" . "galician")
141 ("hr" . "croatian")
142 ("hu" . "hungarian")
143 ("id" . "indonesian")
144 ("is" . "icelandic")
145 ("it" . "italian")
146 ("la" . "latin")
147 ("ms" . "malay")
148 ("nl" . "dutch")
149 ("no-no" . "nynorsk")
150 ("pl" . "polish")
151 ("pt" . "portuguese")
152 ("ro" . "romanian")
153 ("ru" . "russian")
154 ("sa" . "sanskrit")
155 ("sb" . "uppersorbian")
156 ("sk" . "slovak")
157 ("sl" . "slovene")
158 ("sq" . "albanian")
159 ("sr" . "serbian")
160 ("sv" . "swedish")
161 ("ta" . "tamil")
162 ("tr" . "turkish")
163 ("uk" . "ukrainian"))
164 "Alist between language code and corresponding Babel option.")
168 ;;; User Configurable Variables
170 (defgroup org-export-e-latex nil
171 "Options for exporting Org mode files to LaTeX."
172 :tag "Org Export LaTeX"
173 :group 'org-export)
176 ;;;; Preamble
178 (defcustom org-e-latex-default-class "article"
179 "The default LaTeX class."
180 :group 'org-export-e-latex
181 :type '(string :tag "LaTeX class"))
183 (defcustom org-e-latex-classes
184 '(("article"
185 "\\documentclass[11pt]{article}"
186 ("\\section{%s}" . "\\section*{%s}")
187 ("\\subsection{%s}" . "\\subsection*{%s}")
188 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
189 ("\\paragraph{%s}" . "\\paragraph*{%s}")
190 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
191 ("report"
192 "\\documentclass[11pt]{report}"
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 ("book"
199 "\\documentclass[11pt]{book}"
200 ("\\part{%s}" . "\\part*{%s}")
201 ("\\chapter{%s}" . "\\chapter*{%s}")
202 ("\\section{%s}" . "\\section*{%s}")
203 ("\\subsection{%s}" . "\\subsection*{%s}")
204 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
205 "Alist of LaTeX classes and associated header and structure.
206 If #+LaTeX_CLASS is set in the buffer, use its value and the
207 associated information. Here is the structure of each cell:
209 \(class-name
210 header-string
211 \(numbered-section . unnumbered-section\)
212 ...\)
214 The header string
215 -----------------
217 The HEADER-STRING is the header that will be inserted into the
218 LaTeX file. It should contain the \\documentclass macro, and
219 anything else that is needed for this setup. To this header, the
220 following commands will be added:
222 - Calls to \\usepackage for all packages mentioned in the
223 variables `org-export-latex-default-packages-alist' and
224 `org-export-latex-packages-alist'. Thus, your header
225 definitions should avoid to also request these packages.
227 - Lines specified via \"#+LaTeX_HEADER:\"
229 If you need more control about the sequence in which the header
230 is built up, or if you want to exclude one of these building
231 blocks for a particular class, you can use the following
232 macro-like placeholders.
234 [DEFAULT-PACKAGES] \\usepackage statements for default packages
235 [NO-DEFAULT-PACKAGES] do not include any of the default packages
236 [PACKAGES] \\usepackage statements for packages
237 [NO-PACKAGES] do not include the packages
238 [EXTRA] the stuff from #+LaTeX_HEADER
239 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
241 So a header like
243 \\documentclass{article}
244 [NO-DEFAULT-PACKAGES]
245 [EXTRA]
246 \\providecommand{\\alert}[1]{\\textbf{#1}}
247 [PACKAGES]
249 will omit the default packages, and will include the
250 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
251 then place \\usepackage commands based on the content of
252 `org-export-latex-packages-alist'.
254 If your header, `org-export-latex-default-packages-alist' or
255 `org-export-latex-packages-alist' inserts
256 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
257 replaced with a coding system derived from
258 `buffer-file-coding-system'. See also the variable
259 `org-e-latex-inputenc-alist' for a way to influence this
260 mechanism.
262 The sectioning structure
263 ------------------------
265 The sectioning structure of the class is given by the elements
266 following the header string. For each sectioning level, a number
267 of strings is specified. A %s formatter is mandatory in each
268 section string and will be replaced by the title of the section.
270 Instead of a cons cell \(numbered . unnumbered\), you can also
271 provide a list of 2 or 4 elements,
273 \(numbered-open numbered-close\)
277 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
279 providing opening and closing strings for a LaTeX environment
280 that should represent the document section. The opening clause
281 should have a %s to represent the section title.
283 Instead of a list of sectioning commands, you can also specify
284 a function name. That function will be called with two
285 parameters, the \(reduced) level of the headline, and a predicate
286 non-nil when the headline should be numbered. It must return
287 a format string in which the section title will be added."
288 :group 'org-export-e-latex
289 :type '(repeat
290 (list (string :tag "LaTeX class")
291 (string :tag "LaTeX header")
292 (repeat :tag "Levels" :inline t
293 (choice
294 (cons :tag "Heading"
295 (string :tag " numbered")
296 (string :tag "unnumbered"))
297 (list :tag "Environment"
298 (string :tag "Opening (numbered)")
299 (string :tag "Closing (numbered)")
300 (string :tag "Opening (unnumbered)")
301 (string :tag "Closing (unnumbered)"))
302 (function :tag "Hook computing sectioning"))))))
304 (defcustom org-e-latex-inputenc-alist nil
305 "Alist of inputenc coding system names, and what should really be used.
306 For example, adding an entry
308 (\"utf8\" . \"utf8x\")
310 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
311 are written as utf8 files."
312 :group 'org-export-e-latex
313 :type '(repeat
314 (cons
315 (string :tag "Derived from buffer")
316 (string :tag "Use this instead"))))
318 (defcustom org-e-latex-date-format
319 "\\today"
320 "Format string for \\date{...}."
321 :group 'org-export-e-latex
322 :type 'boolean)
324 (defcustom org-e-latex-title-command "\\maketitle"
325 "The command used to insert the title just after \\begin{document}.
326 If this string contains the formatting specification \"%s\" then
327 it will be used as a formatting string, passing the title as an
328 argument."
329 :group 'org-export-e-latex
330 :type 'string)
332 (defcustom org-e-latex-toc-command "\\tableofcontents\n\\vspace*{1cm}\n\n"
333 "LaTeX command to set the table of contents, list of figures...
334 This command only applies to the table of contents generated with
335 toc:nil option, not to those generated with #+TOC keyword."
336 :group 'org-export-e-latex
337 :type 'string)
339 ;;;; Headline
341 (defcustom org-e-latex-format-headline-function nil
342 "Function to format headline text.
344 This function will be called with 5 arguments:
345 TODO the todo keyword (string or nil).
346 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
347 PRIORITY the priority of the headline (integer or nil)
348 TEXT the main headline text (string).
349 TAGS the tags as a list of strings (list of strings or nil).
351 The function result will be used in the section format string.
353 As an example, one could set the variable to the following, in
354 order to reproduce the default set-up:
356 \(defun org-e-latex-format-headline (todo todo-type priority text tags)
357 \"Default format function for an headline.\"
358 \(concat (when todo
359 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
360 \(when priority
361 \(format \"\\\\framebox{\\\\#%c} \" priority))
362 text
363 \(when tags
364 \(format \"\\\\hfill{}\\\\textsc{%s}\"
365 \(mapconcat 'identity tags \":\"))))"
366 :group 'org-export-e-latex
367 :type 'function)
370 ;;;; Footnotes
372 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
373 "Text used to separate footnotes."
374 :group 'org-export-e-latex
375 :type 'string)
378 ;;;; Timestamps
380 (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
381 "A printf format string to be applied to active timestamps."
382 :group 'org-export-e-latex
383 :type 'string)
385 (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
386 "A printf format string to be applied to inactive timestamps."
387 :group 'org-export-e-latex
388 :type 'string)
390 (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
391 "A printf format string to be applied to diary timestamps."
392 :group 'org-export-e-latex
393 :type 'string)
396 ;;;; Links
398 (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
399 "Default option for images."
400 :group 'org-export-e-latex
401 :type 'string)
403 (defcustom org-e-latex-default-figure-position "htb"
404 "Default position for latex figures."
405 :group 'org-export-e-latex
406 :type 'string)
408 (defcustom org-e-latex-inline-image-rules
409 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
410 "Rules characterizing image files that can be inlined into LaTeX.
412 A rule consists in an association whose key is the type of link
413 to consider, and value is a regexp that will be matched against
414 link's path.
416 Note that, by default, the image extension *actually* allowed
417 depend on the way the LaTeX file is processed. When used with
418 pdflatex, pdf, jpg and png images are OK. When processing
419 through dvi to Postscript, only ps and eps are allowed. The
420 default we use here encompasses both."
421 :group 'org-export-e-latex
422 :type '(alist :key-type (string :tag "Type")
423 :value-type (regexp :tag "Path")))
425 (defcustom org-e-latex-link-with-unknown-path-format "\\texttt{%s}"
426 "Format string for links with unknown path type."
427 :group 'org-export-latex
428 :type 'string)
431 ;;;; Tables
433 (defcustom org-e-latex-default-table-environment "tabular"
434 "Default environment used to build tables."
435 :group 'org-export-e-latex
436 :type 'string)
438 (defcustom org-e-latex-tables-centered t
439 "When non-nil, tables are exported in a center environment."
440 :group 'org-export-e-latex
441 :type 'boolean)
443 (defcustom org-e-latex-tables-verbatim nil
444 "When non-nil, tables are exported verbatim."
445 :group 'org-export-e-latex
446 :type 'boolean)
448 (defcustom org-e-latex-tables-booktabs nil
449 "When non-nil, display tables in a formal \"booktabs\" style.
450 This option assumes that the \"booktabs\" package is properly
451 loaded in the header of the document. This value can be ignored
452 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
453 attributes."
454 :group 'org-export-e-latex
455 :type 'boolean)
457 (defcustom org-e-latex-table-caption-above t
458 "When non-nil, place caption string at the beginning of the table.
459 Otherwise, place it near the end."
460 :group 'org-export-e-latex
461 :type 'boolean)
463 (defcustom org-e-latex-table-scientific-notation "%s\\,(%s)"
464 "Format string to display numbers in scientific notation.
465 The format should have \"%s\" twice, for mantissa and exponent
466 \(i.e. \"%s\\\\times10^{%s}\").
468 When nil, no transformation is made."
469 :group 'org-export-e-latex
470 :type '(choice
471 (string :tag "Format string")
472 (const :tag "No formatting")))
475 ;;;; Text markup
477 (defcustom org-e-latex-text-markup-alist '((bold . "\\textbf{%s}")
478 (code . verb)
479 (italic . "\\emph{%s}")
480 (strike-through . "\\st{%s}")
481 (underline . "\\underline{%s}")
482 (verbatim . protectedtexttt))
483 "Alist of LaTeX expressions to convert text markup.
485 The key must be a symbol among `bold', `code', `italic',
486 `strike-through', `underline' and `verbatim'. The value is
487 a formatting string to wrap fontified text with.
489 Value can also be set to the following symbols: `verb' and
490 `protectedtexttt'. For the former, Org will use \"\\verb\" to
491 create a format string and select a delimiter character that
492 isn't in the string. For the latter, Org will use \"\\texttt\"
493 to typeset and try to protect special characters.
495 If no association can be found for a given markup, text will be
496 returned as-is."
497 :group 'org-export-e-latex
498 :type 'alist
499 :options '(bold code italic strike-through underline verbatim))
502 ;;;; Drawers
504 (defcustom org-e-latex-format-drawer-function nil
505 "Function called to format a drawer in LaTeX code.
507 The function must accept two parameters:
508 NAME the drawer name, like \"LOGBOOK\"
509 CONTENTS the contents of the drawer.
511 The function should return the string to be exported.
513 For example, the variable could be set to the following function
514 in order to mimic default behaviour:
516 \(defun org-e-latex-format-drawer-default \(name contents\)
517 \"Format a drawer element for LaTeX export.\"
518 contents\)"
519 :group 'org-export-e-latex
520 :type 'function)
523 ;;;; Inlinetasks
525 (defcustom org-e-latex-format-inlinetask-function nil
526 "Function called to format an inlinetask in LaTeX code.
528 The function must accept six parameters:
529 TODO the todo keyword, as a string
530 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
531 PRIORITY the inlinetask priority, as a string
532 NAME the inlinetask name, as a string.
533 TAGS the inlinetask tags, as a list of strings.
534 CONTENTS the contents of the inlinetask, as a string.
536 The function should return the string to be exported.
538 For example, the variable could be set to the following function
539 in order to mimic default behaviour:
541 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
542 \"Format an inline task element for LaTeX export.\"
543 \(let ((full-title
544 \(concat
545 \(when todo
546 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
547 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
548 title
549 \(when tags
550 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
551 \(mapconcat 'identity tags \":\")))))
552 \(format (concat \"\\\\begin{center}\\n\"
553 \"\\\\fbox{\\n\"
554 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
555 \"%s\\n\\n\"
556 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
557 \"%s\"
558 \"\\\\end{minipage}}\"
559 \"\\\\end{center}\")
560 full-title contents))"
561 :group 'org-export-e-latex
562 :type 'function)
565 ;; Src blocks
567 (defcustom org-e-latex-listings nil
568 "Non-nil means export source code using the listings package.
569 This package will fontify source code, possibly even with color.
570 If you want to use this, you also need to make LaTeX use the
571 listings package, and if you want to have color, the color
572 package. Just add these to `org-export-latex-packages-alist',
573 for example using customize, or with something like:
575 \(require 'org-e-latex)
576 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
577 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
579 Alternatively,
581 \(setq org-e-latex-listings 'minted)
583 causes source code to be exported using the minted package as
584 opposed to listings. If you want to use minted, you need to add
585 the minted package to `org-export-latex-packages-alist', for
586 example using customize, or with
588 \(require 'org-e-latex)
589 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
591 In addition, it is necessary to install pygments
592 \(http://pygments.org), and to configure the variable
593 `org-e-latex-pdf-process' so that the -shell-escape option is
594 passed to pdflatex."
595 :group 'org-export-e-latex
596 :type '(choice
597 (const :tag "Use listings" t)
598 (const :tag "Use minted" 'minted)
599 (const :tag "Export verbatim" nil)))
601 (defcustom org-e-latex-listings-langs
602 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
603 (c "C") (cc "C++")
604 (fortran "fortran")
605 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
606 (html "HTML") (xml "XML")
607 (tex "TeX") (latex "TeX")
608 (shell-script "bash")
609 (gnuplot "Gnuplot")
610 (ocaml "Caml") (caml "Caml")
611 (sql "SQL") (sqlite "sql"))
612 "Alist mapping languages to their listing language counterpart.
613 The key is a symbol, the major mode symbol without the \"-mode\".
614 The value is the string that should be inserted as the language
615 parameter for the listings package. If the mode name and the
616 listings name are the same, the language does not need an entry
617 in this list - but it does not hurt if it is present."
618 :group 'org-export-e-latex
619 :type '(repeat
620 (list
621 (symbol :tag "Major mode ")
622 (string :tag "Listings language"))))
624 (defcustom org-e-latex-listings-options nil
625 "Association list of options for the latex listings package.
627 These options are supplied as a comma-separated list to the
628 \\lstset command. Each element of the association list should be
629 a list containing two strings: the name of the option, and the
630 value. For example,
632 (setq org-e-latex-listings-options
633 '((\"basicstyle\" \"\\small\")
634 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
636 will typeset the code in a small size font with underlined, bold
637 black keywords.
639 Note that the same options will be applied to blocks of all
640 languages."
641 :group 'org-export-e-latex
642 :type '(repeat
643 (list
644 (string :tag "Listings option name ")
645 (string :tag "Listings option value"))))
647 (defcustom org-e-latex-minted-langs
648 '((emacs-lisp "common-lisp")
649 (cc "c++")
650 (cperl "perl")
651 (shell-script "bash")
652 (caml "ocaml"))
653 "Alist mapping languages to their minted language counterpart.
654 The key is a symbol, the major mode symbol without the \"-mode\".
655 The value is the string that should be inserted as the language
656 parameter for the minted package. If the mode name and the
657 listings name are the same, the language does not need an entry
658 in this list - but it does not hurt if it is present.
660 Note that minted uses all lower case for language identifiers,
661 and that the full list of language identifiers can be obtained
662 with:
664 pygmentize -L lexers"
665 :group 'org-export-e-latex
666 :type '(repeat
667 (list
668 (symbol :tag "Major mode ")
669 (string :tag "Minted language"))))
671 (defcustom org-e-latex-minted-options nil
672 "Association list of options for the latex minted package.
674 These options are supplied within square brackets in
675 \\begin{minted} environments. Each element of the alist should
676 be a list containing two strings: the name of the option, and the
677 value. For example,
679 \(setq org-e-latex-minted-options
680 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
682 will result in src blocks being exported with
684 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
686 as the start of the minted environment. Note that the same
687 options will be applied to blocks of all languages."
688 :group 'org-export-e-latex
689 :type '(repeat
690 (list
691 (string :tag "Minted option name ")
692 (string :tag "Minted option value"))))
694 (defvar org-e-latex-custom-lang-environments nil
695 "Alist mapping languages to language-specific LaTeX environments.
697 It is used during export of src blocks by the listings and minted
698 latex packages. For example,
700 \(setq org-e-latex-custom-lang-environments
701 '\(\(python \"pythoncode\"\)\)\)
703 would have the effect that if org encounters begin_src python
704 during latex export it will output
706 \\begin{pythoncode}
707 <src block body>
708 \\end{pythoncode}")
711 ;;;; Plain text
713 (defcustom org-e-latex-quotes
714 '(("fr"
715 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
716 ("\\(\\S-\\)\"" . "~»")
717 ("\\(\\s-\\|(\\|^\\)'" . "'"))
718 ("en"
719 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
720 ("\\(\\S-\\)\"" . "''")
721 ("\\(\\s-\\|(\\|^\\)'" . "`")))
722 "Alist for quotes to use when converting english double-quotes.
724 The CAR of each item in this alist is the language code.
725 The CDR of each item in this alist is a list of three CONS:
726 - the first CONS defines the opening quote;
727 - the second CONS defines the closing quote;
728 - the last CONS defines single quotes.
730 For each item in a CONS, the first string is a regexp
731 for allowed characters before/after the quote, the second
732 string defines the replacement string for this quote."
733 :group 'org-export-e-latex
734 :type '(list
735 (cons :tag "Opening quote"
736 (string :tag "Regexp for char before")
737 (string :tag "Replacement quote "))
738 (cons :tag "Closing quote"
739 (string :tag "Regexp for char after ")
740 (string :tag "Replacement quote "))
741 (cons :tag "Single quote"
742 (string :tag "Regexp for char before")
743 (string :tag "Replacement quote "))))
746 ;;;; Compilation
748 (defcustom org-e-latex-pdf-process
749 '("pdflatex -interaction nonstopmode -output-directory %o %f"
750 "pdflatex -interaction nonstopmode -output-directory %o %f"
751 "pdflatex -interaction nonstopmode -output-directory %o %f")
752 "Commands to process a LaTeX file to a PDF file.
753 This is a list of strings, each of them will be given to the
754 shell as a command. %f in the command will be replaced by the
755 full file name, %b by the file base name \(i.e. without
756 extension) and %o by the base directory of the file.
758 The reason why this is a list is that it usually takes several
759 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
760 does not have a clever mechanism to detect which of these
761 commands have to be run to get to a stable result, and it also
762 does not do any error checking.
764 By default, Org uses 3 runs of `pdflatex' to do the processing.
765 If you have texi2dvi on your system and if that does not cause
766 the infamous egrep/locale bug:
768 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
770 then `texi2dvi' is the superior choice. Org does offer it as one
771 of the customize options.
773 Alternatively, this may be a Lisp function that does the
774 processing, so you could use this to apply the machinery of
775 AUCTeX or the Emacs LaTeX mode. This function should accept the
776 file name as its single argument."
777 :group 'org-export-pdf
778 :type '(choice
779 (repeat :tag "Shell command sequence"
780 (string :tag "Shell command"))
781 (const :tag "2 runs of pdflatex"
782 ("pdflatex -interaction nonstopmode -output-directory %o %f"
783 "pdflatex -interaction nonstopmode -output-directory %o %f"))
784 (const :tag "3 runs of pdflatex"
785 ("pdflatex -interaction nonstopmode -output-directory %o %f"
786 "pdflatex -interaction nonstopmode -output-directory %o %f"
787 "pdflatex -interaction nonstopmode -output-directory %o %f"))
788 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
789 ("pdflatex -interaction nonstopmode -output-directory %o %f"
790 "bibtex %b"
791 "pdflatex -interaction nonstopmode -output-directory %o %f"
792 "pdflatex -interaction nonstopmode -output-directory %o %f"))
793 (const :tag "2 runs of xelatex"
794 ("xelatex -interaction nonstopmode -output-directory %o %f"
795 "xelatex -interaction nonstopmode -output-directory %o %f"))
796 (const :tag "3 runs of xelatex"
797 ("xelatex -interaction nonstopmode -output-directory %o %f"
798 "xelatex -interaction nonstopmode -output-directory %o %f"
799 "xelatex -interaction nonstopmode -output-directory %o %f"))
800 (const :tag "xelatex,bibtex,xelatex,xelatex"
801 ("xelatex -interaction nonstopmode -output-directory %o %f"
802 "bibtex %b"
803 "xelatex -interaction nonstopmode -output-directory %o %f"
804 "xelatex -interaction nonstopmode -output-directory %o %f"))
805 (const :tag "texi2dvi"
806 ("texi2dvi -p -b -c -V %f"))
807 (const :tag "rubber"
808 ("rubber -d --into %o %f"))
809 (function)))
811 (defcustom org-e-latex-logfiles-extensions
812 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
813 "The list of file extensions to consider as LaTeX logfiles."
814 :group 'org-export-e-latex
815 :type '(repeat (string :tag "Extension")))
817 (defcustom org-e-latex-remove-logfiles t
818 "Non-nil means remove the logfiles produced by PDF production.
819 These are the .aux, .log, .out, and .toc files."
820 :group 'org-export-e-latex
821 :type 'boolean)
823 (defcustom org-e-latex-known-errors
824 '(("Reference.*?undefined" . "[undefined reference]")
825 ("Citation.*?undefined" . "[undefined citation]")
826 ("Undefined control sequence" . "[undefined control sequence]")
827 ("^! LaTeX.*?Error" . "[LaTeX error]")
828 ("^! Package.*?Error" . "[package error]")
829 ("Runaway argument" . "Runaway argument"))
830 "Alist of regular expressions and associated messages for the user.
831 The regular expressions are used to find possible errors in the
832 log of a latex-run."
833 :group 'org-export-e-latex
834 :type '(repeat
835 (cons
836 (string :tag "Regexp")
837 (string :tag "Message"))))
841 ;;; Internal Functions
843 (defun org-e-latex--caption/label-string (element info)
844 "Return caption and label LaTeX string for ELEMENT.
846 INFO is a plist holding contextual information. If there's no
847 caption nor label, return the empty string.
849 For non-floats, see `org-e-latex--wrap-label'."
850 (let* ((label (org-element-property :name element))
851 (label-str (if (not (org-string-nw-p label)) ""
852 (format "\\label{%s}"
853 (org-export-solidify-link-text label))))
854 (main (org-export-get-caption element))
855 (short (org-export-get-caption element t)))
856 (cond
857 ((and (not main) (equal label-str "")) "")
858 ((not main) (concat label-str "\n"))
859 ;; Option caption format with short name.
860 (short (format "\\caption[%s]{%s%s}\n"
861 (org-export-data short info)
862 label-str
863 (org-export-data main info)))
864 ;; Standard caption format.
865 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
867 (defun org-e-latex--guess-babel-language (header info)
868 "Set Babel's language according to LANGUAGE keyword.
870 HEADER is the LaTeX header string. INFO is the plist used as
871 a communication channel.
873 Insertion of guessed language only happens when Babel package has
874 explicitly been loaded. Then it is added to the rest of
875 package's options.
877 Return the new header."
878 (let ((language-code (plist-get info :language)))
879 ;; If no language is set or Babel package is not loaded, return
880 ;; HEADER as-is.
881 (if (or (not (stringp language-code))
882 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
883 header
884 (let ((options (save-match-data
885 (org-split-string (match-string 1 header) ",")))
886 (language (cdr (assoc language-code
887 org-e-latex-babel-language-alist))))
888 ;; If LANGUAGE is already loaded, return header. Otherwise,
889 ;; append LANGUAGE to other options.
890 (if (member language options) header
891 (replace-match (mapconcat 'identity
892 (append options (list language))
893 ",")
894 nil nil header 1))))))
896 (defun org-e-latex--guess-inputenc (header)
897 "Set the coding system in inputenc to what the buffer is.
898 HEADER is the LaTeX header string. Return the new header."
899 (let* ((cs (or (ignore-errors
900 (latexenc-coding-system-to-inputenc
901 buffer-file-coding-system))
902 "utf8")))
903 (if (not cs) header
904 ;; First translate if that is requested.
905 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
906 ;; Then find the \usepackage statement and replace the option.
907 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
908 cs header t nil 1))))
910 (defun org-e-latex--find-verb-separator (s)
911 "Return a character not used in string S.
912 This is used to choose a separator for constructs like \\verb."
913 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
914 (loop for c across ll
915 when (not (string-match (regexp-quote (char-to-string c)) s))
916 return (char-to-string c))))
918 (defun org-e-latex--make-option-string (options)
919 "Return a comma separated string of keywords and values.
920 OPTIONS is an alist where the key is the options keyword as
921 a string, and the value a list containing the keyword value, or
922 nil."
923 (mapconcat (lambda (pair)
924 (concat (first pair)
925 (when (> (length (second pair)) 0)
926 (concat "=" (second pair)))))
927 options
928 ","))
930 (defun org-e-latex--quotation-marks (text info)
931 "Export quotation marks depending on language conventions.
932 TEXT is a string containing quotation marks to be replaced. INFO
933 is a plist used as a communication channel."
934 (mapc (lambda(l)
935 (let ((start 0))
936 (while (setq start (string-match (car l) text start))
937 (let ((new-quote (concat (match-string 1 text) (cdr l))))
938 (setq text (replace-match new-quote t t text))))))
939 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
940 ;; Falls back on English.
941 (assoc "en" org-e-latex-quotes))))
942 text)
944 (defun org-e-latex--wrap-label (element output)
945 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
946 This function shouldn't be used for floats. See
947 `org-e-latex--caption/label-string'."
948 (let ((label (org-element-property :name element)))
949 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
950 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
951 output))))
953 (defun org-e-latex--text-markup (text markup)
954 "Format TEXT depending on MARKUP text markup.
955 See `org-e-latex-text-markup-alist' for details."
956 (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist))))
957 (cond
958 ;; No format string: Return raw text.
959 ((not fmt) text)
960 ;; Handle the `verb' special case: Find and appropriate separator
961 ;; and use "\\verb" command.
962 ((eq 'verb fmt)
963 (let ((separator (org-e-latex--find-verb-separator text)))
964 (concat "\\verb" separator text separator)))
965 ;; Handle the `protectedtexttt' special case: Protect some
966 ;; special chars and use "\texttt{%s}" format string.
967 ((eq 'protectedtexttt fmt)
968 (let ((start 0)
969 (trans '(("\\" . "\\textbackslash{}")
970 ("~" . "\\textasciitilde{}")
971 ("^" . "\\textasciicircum{}")))
972 (rtn "")
973 char)
974 (while (string-match "[\\{}$%&_#~^]" text)
975 (setq char (match-string 0 text))
976 (if (> (match-beginning 0) 0)
977 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
978 (setq text (substring text (1+ (match-beginning 0))))
979 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
980 rtn (concat rtn char)))
981 (setq text (concat rtn text)
982 fmt "\\texttt{%s}")
983 (while (string-match "--" text)
984 (setq text (replace-match "-{}-" t t text)))
985 (format fmt text)))
986 ;; Else use format string.
987 (t (format fmt text)))))
989 (defun org-e-latex--delayed-footnotes-definitions (element info)
990 "Return footnotes definitions in ELEMENT as a string.
992 INFO is a plist used as a communication channel.
994 Footnotes definitions are returned within \"\\footnotetxt{}\"
995 commands.
997 This function is used within constructs that don't support
998 \"\\footnote{}\" command (i.e. an item's tag). In that case,
999 \"\\footnotemark\" is used within the construct and the function
1000 just outside of it."
1001 (mapconcat
1002 (lambda (ref)
1003 (format
1004 "\\footnotetext[%s]{%s}"
1005 (org-export-get-footnote-number ref info)
1006 (org-trim
1007 (org-export-data
1008 (org-export-get-footnote-definition ref info) info))))
1009 ;; Find every footnote reference in ELEMENT.
1010 (let* (all-refs
1011 search-refs ; For byte-compiler.
1012 (search-refs
1013 (function
1014 (lambda (data)
1015 ;; Return a list of all footnote references never seen
1016 ;; before in DATA.
1017 (org-element-map
1018 data 'footnote-reference
1019 (lambda (ref)
1020 (when (org-export-footnote-first-reference-p ref info)
1021 (push ref all-refs)
1022 (when (eq (org-element-property :type ref) 'standard)
1023 (funcall search-refs
1024 (org-export-get-footnote-definition ref info)))))
1025 info)
1026 (reverse all-refs)))))
1027 (funcall search-refs element))
1028 ""))
1032 ;;; Template
1034 (defun org-e-latex-template (contents info)
1035 "Return complete document string after LaTeX conversion.
1036 CONTENTS is the transcoded contents string. INFO is a plist
1037 holding export options."
1038 (let ((title (org-export-data (plist-get info :title) info)))
1039 (concat
1040 ;; Time-stamp.
1041 (and (plist-get info :time-stamp-file)
1042 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1043 ;; Document class and packages.
1044 (let ((class (plist-get info :latex-class))
1045 (class-options (plist-get info :latex-class-options)))
1046 (org-element-normalize-string
1047 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
1048 (document-class-string
1049 (and (stringp header)
1050 (if (not class-options) header
1051 (replace-regexp-in-string
1052 "^[ \t]*\\\\documentclass\\(\\(\\[.*\\]\\)?\\)"
1053 class-options header t nil 1)))))
1054 (when document-class-string
1055 (org-e-latex--guess-babel-language
1056 (org-e-latex--guess-inputenc
1057 (org-splice-latex-header
1058 document-class-string
1059 org-export-latex-default-packages-alist ; defined in org.el
1060 org-export-latex-packages-alist nil ; defined in org.el
1061 (plist-get info :latex-header-extra)))
1062 info)))))
1063 ;; Possibly limit depth for headline numbering.
1064 (let ((sec-num (plist-get info :section-numbers)))
1065 (when (integerp sec-num)
1066 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1067 ;; Author.
1068 (let ((author (and (plist-get info :with-author)
1069 (let ((auth (plist-get info :author)))
1070 (and auth (org-export-data auth info)))))
1071 (email (and (plist-get info :with-email)
1072 (org-export-data (plist-get info :email) info))))
1073 (cond ((and author email (not (string= "" email)))
1074 (format "\\author{%s\\thanks{%s}}\n" author email))
1075 (author (format "\\author{%s}\n" author))
1076 (t "\\author{}\n")))
1077 ;; Date.
1078 (let ((date (org-export-data (plist-get info :date) info)))
1079 (and date (format "\\date{%s}\n" date)))
1080 ;; Title
1081 (format "\\title{%s}\n" title)
1082 ;; Hyperref options.
1083 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1084 (or (plist-get info :keywords) "")
1085 (or (plist-get info :description) "")
1086 (if (not (plist-get info :with-creator)) ""
1087 (plist-get info :creator)))
1088 ;; Document start.
1089 "\\begin{document}\n\n"
1090 ;; Title command.
1091 (org-element-normalize-string
1092 (cond ((string= "" title) nil)
1093 ((not (stringp org-e-latex-title-command)) nil)
1094 ((string-match "\\(?:[^%]\\|^\\)%s"
1095 org-e-latex-title-command)
1096 (format org-e-latex-title-command title))
1097 (t org-e-latex-title-command)))
1098 ;; Table of contents.
1099 (let ((depth (plist-get info :with-toc)))
1100 (when depth
1101 (concat (when (wholenump depth)
1102 (format "\\setcounter{tocdepth}{%d}\n" depth))
1103 org-e-latex-toc-command)))
1104 ;; Document's body.
1105 contents
1106 ;; Creator.
1107 (let ((creator-info (plist-get info :with-creator)))
1108 (cond
1109 ((not creator-info) "")
1110 ((eq creator-info 'comment)
1111 (format "%% %s\n" (plist-get info :creator)))
1112 (t (concat (plist-get info :creator) "\n"))))
1113 ;; Document end.
1114 "\\end{document}")))
1118 ;;; Transcode Functions
1120 ;;;; Babel Call
1122 ;; Babel Calls are ignored.
1125 ;;;; Bold
1127 (defun org-e-latex-bold (bold contents info)
1128 "Transcode BOLD from Org to LaTeX.
1129 CONTENTS is the text with bold markup. INFO is a plist holding
1130 contextual information."
1131 (org-e-latex--text-markup contents 'bold))
1134 ;;;; Center Block
1136 (defun org-e-latex-center-block (center-block contents info)
1137 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1138 CONTENTS holds the contents of the center block. INFO is a plist
1139 holding contextual information."
1140 (org-e-latex--wrap-label
1141 center-block
1142 (format "\\begin{center}\n%s\\end{center}" contents)))
1145 ;;;; Clock
1147 (defun org-e-latex-clock (clock contents info)
1148 "Transcode a CLOCK element from Org to LaTeX.
1149 CONTENTS is nil. INFO is a plist holding contextual
1150 information."
1151 (concat
1152 "\\noindent"
1153 (format "\\textbf{%s} " org-clock-string)
1154 (format org-e-latex-inactive-timestamp-format
1155 (concat (org-translate-time (org-element-property :value clock))
1156 (let ((time (org-element-property :time clock)))
1157 (and time (format " (%s)" time)))))
1158 "\\\\"))
1161 ;;;; Code
1163 (defun org-e-latex-code (code contents info)
1164 "Transcode a CODE object from Org to LaTeX.
1165 CONTENTS is nil. INFO is a plist used as a communication
1166 channel."
1167 (org-e-latex--text-markup (org-element-property :value code) 'code))
1170 ;;;; Comment
1172 ;; Comments are ignored.
1175 ;;;; Comment Block
1177 ;; Comment Blocks are ignored.
1180 ;;;; Drawer
1182 (defun org-e-latex-drawer (drawer contents info)
1183 "Transcode a DRAWER element from Org to LaTeX.
1184 CONTENTS holds the contents of the block. INFO is a plist
1185 holding contextual information."
1186 (let* ((name (org-element-property :drawer-name drawer))
1187 (output (if (functionp org-e-latex-format-drawer-function)
1188 (funcall org-e-latex-format-drawer-function
1189 name contents)
1190 ;; If there's no user defined function: simply
1191 ;; display contents of the drawer.
1192 contents)))
1193 (org-e-latex--wrap-label drawer output)))
1196 ;;;; Dynamic Block
1198 (defun org-e-latex-dynamic-block (dynamic-block contents info)
1199 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1200 CONTENTS holds the contents of the block. INFO is a plist
1201 holding contextual information. See `org-export-data'."
1202 (org-e-latex--wrap-label dynamic-block contents))
1205 ;;;; Entity
1207 (defun org-e-latex-entity (entity contents info)
1208 "Transcode an ENTITY object from Org to LaTeX.
1209 CONTENTS are the definition itself. INFO is a plist holding
1210 contextual information."
1211 (let ((ent (org-element-property :latex entity)))
1212 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1215 ;;;; Example Block
1217 (defun org-e-latex-example-block (example-block contents info)
1218 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1219 CONTENTS is nil. INFO is a plist holding contextual
1220 information."
1221 (org-e-latex--wrap-label
1222 example-block
1223 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1224 (org-export-format-code-default example-block info))))
1227 ;;;; Export Block
1229 (defun org-e-latex-export-block (export-block contents info)
1230 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1231 CONTENTS is nil. INFO is a plist holding contextual information."
1232 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1233 (org-remove-indentation (org-element-property :value export-block))))
1236 ;;;; Export Snippet
1238 (defun org-e-latex-export-snippet (export-snippet contents info)
1239 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1240 CONTENTS is nil. INFO is a plist holding contextual information."
1241 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
1242 (org-element-property :value export-snippet)))
1245 ;;;; Fixed Width
1247 (defun org-e-latex-fixed-width (fixed-width contents info)
1248 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1249 CONTENTS is nil. INFO is a plist holding contextual information."
1250 (org-e-latex--wrap-label
1251 fixed-width
1252 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1253 (org-remove-indentation
1254 (org-element-property :value fixed-width)))))
1257 ;;;; Footnote Definition
1259 ;; Footnote Definitions are ignored.
1262 ;;;; Footnote Reference
1264 ;; Footnote reference export is handled by
1265 ;; `org-e-latex-footnote-reference'.
1267 ;; Internally, `org-e-latex--get-footnote-counter' is used to restore
1268 ;; the value of the LaTeX "footnote" counter after a jump due to
1269 ;; a reference to an already defined footnote. It is only needed in
1270 ;; item tags since the optional argument to \footnotemark is not
1271 ;; allowed there.
1273 (defun org-e-latex--get-footnote-counter (footnote-reference info)
1274 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1275 INFO is a plist used as a communication channel."
1276 ;; Find original counter value by counting number of footnote
1277 ;; references appearing for the first time before the current
1278 ;; footnote reference.
1279 (let* ((label (org-element-property :label footnote-reference))
1280 seen-refs
1281 search-ref ; For byte-compiler.
1282 (search-ref
1283 (function
1284 (lambda (data)
1285 ;; Search footnote references through DATA, filling
1286 ;; SEEN-REFS along the way.
1287 (org-element-map
1288 data 'footnote-reference
1289 (lambda (fn)
1290 (let ((fn-lbl (org-element-property :label fn)))
1291 (cond
1292 ;; Anonymous footnote match: return number.
1293 ((eq fn footnote-reference) (length seen-refs))
1294 ;; Anonymous footnote: it's always a new one.
1295 ;; Also, be sure to return nil from the `cond' so
1296 ;; `first-match' doesn't get us out of the loop.
1297 ((not fn-lbl) (push 'inline seen-refs) nil)
1298 ;; Label not seen so far: add it so SEEN-REFS.
1300 ;; Also search for subsequent references in
1301 ;; footnote definition so numbering follows reading
1302 ;; logic. Note that we don't have to care about
1303 ;; inline definitions, since `org-element-map'
1304 ;; already traverse them at the right time.
1305 ((not (member fn-lbl seen-refs))
1306 (push fn-lbl seen-refs)
1307 (funcall search-ref
1308 (org-export-get-footnote-definition fn info))))))
1309 ;; Don't enter footnote definitions since it will happen
1310 ;; when their first reference is found.
1311 info 'first-match 'footnote-definition)))))
1312 (funcall search-ref (plist-get info :parse-tree))))
1314 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1315 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1316 CONTENTS is nil. INFO is a plist holding contextual information."
1317 (concat
1318 ;; Insert separator between two footnotes in a row.
1319 (let ((prev (org-export-get-previous-element footnote-reference info)))
1320 (when (eq (org-element-type prev) 'footnote-reference)
1321 org-e-latex-footnote-separator))
1322 (cond
1323 ;; Use \footnotemark if reference is within an item's tag.
1324 ((eq (org-element-type (org-export-get-parent-element footnote-reference))
1325 'item)
1326 (if (org-export-footnote-first-reference-p footnote-reference info)
1327 "\\footnotemark"
1328 ;; Since we can't specify footnote number as an optional
1329 ;; argument within an item tag, some extra work has to be done
1330 ;; when the footnote has already been referenced. In that
1331 ;; case, set footnote counter to the desired number, use the
1332 ;; footnotemark, then set counter back to its original value.
1333 (format
1334 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1335 (1- (org-export-get-footnote-number footnote-reference info))
1336 (org-e-latex--get-footnote-counter footnote-reference info))))
1337 ;; Use \footnotemark if the footnote has already been defined.
1338 ((not (org-export-footnote-first-reference-p footnote-reference info))
1339 (format "\\footnotemark[%s]{}"
1340 (org-export-get-footnote-number footnote-reference info)))
1341 ;; Use \footnotemark if reference is within another footnote
1342 ;; reference or footnote definition.
1343 ((loop for parent in (org-export-get-genealogy footnote-reference)
1344 thereis (memq (org-element-type parent)
1345 '(footnote-reference footnote-definition)))
1346 "\\footnotemark")
1347 ;; Otherwise, define it with \footnote command.
1349 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1350 (unless (eq (org-element-type def) 'org-data)
1351 (setq def (cons 'org-data (cons nil def))))
1352 (concat
1353 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1354 ;; Retrieve all footnote references within the footnote and
1355 ;; add their definition after it, since LaTeX doesn't support
1356 ;; them inside.
1357 (org-e-latex--delayed-footnotes-definitions def info)))))))
1360 ;;;; Headline
1362 (defun org-e-latex-headline (headline contents info)
1363 "Transcode an HEADLINE element from Org to LaTeX.
1364 CONTENTS holds the contents of the headline. INFO is a plist
1365 holding contextual information."
1366 (let* ((class (plist-get info :latex-class))
1367 (level (org-export-get-relative-level headline info))
1368 (numberedp (org-export-numbered-headline-p headline info))
1369 (class-sectionning (assoc class org-e-latex-classes))
1370 ;; Section formatting will set two placeholders: one for the
1371 ;; title and the other for the contents.
1372 (section-fmt
1373 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1374 (fboundp (nth 2 class-sectionning)))
1375 (funcall (nth 2 class-sectionning) level numberedp)
1376 (nth (1+ level) class-sectionning))))
1377 (cond
1378 ;; No section available for that LEVEL.
1379 ((not sec) nil)
1380 ;; Section format directly returned by a function.
1381 ((stringp sec) sec)
1382 ;; (numbered-section . unnumbered-section)
1383 ((not (consp (cdr sec)))
1384 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1385 ;; (numbered-open numbered-close)
1386 ((= (length sec) 2)
1387 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1388 ;; (num-in num-out no-num-in no-num-out)
1389 ((= (length sec) 4)
1390 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1391 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1392 (text (org-export-data (org-element-property :title headline) info))
1393 (todo
1394 (and (plist-get info :with-todo-keywords)
1395 (let ((todo (org-element-property :todo-keyword headline)))
1396 (and todo (org-export-data todo info)))))
1397 (todo-type (and todo (org-element-property :todo-type headline)))
1398 (tags (and (plist-get info :with-tags)
1399 (org-export-get-tags headline info)))
1400 (priority (and (plist-get info :with-priority)
1401 (org-element-property :priority headline)))
1402 ;; Create the headline text along with a no-tag version. The
1403 ;; latter is required to remove tags from table of contents.
1404 (full-text (if (functionp org-e-latex-format-headline-function)
1405 ;; User-defined formatting function.
1406 (funcall org-e-latex-format-headline-function
1407 todo todo-type priority text tags)
1408 ;; Default formatting.
1409 (concat
1410 (when todo
1411 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1412 (when priority (format "\\framebox{\\#%c} " priority))
1413 text
1414 (when tags
1415 (format "\\hfill{}\\textsc{:%s:}"
1416 (mapconcat 'identity tags ":"))))))
1417 (full-text-no-tag
1418 (if (functionp org-e-latex-format-headline-function)
1419 ;; User-defined formatting function.
1420 (funcall org-e-latex-format-headline-function
1421 todo todo-type priority text nil)
1422 ;; Default formatting.
1423 (concat
1424 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1425 (when priority (format "\\framebox{\\#%c} " priority))
1426 text)))
1427 ;; Associate some \label to the headline for internal links.
1428 (headline-label
1429 (format "\\label{sec-%s}\n"
1430 (mapconcat 'number-to-string
1431 (org-export-get-headline-number headline info)
1432 "-")))
1433 (pre-blanks
1434 (make-string (org-element-property :pre-blank headline) 10)))
1435 (cond
1436 ;; Case 1: This is a footnote section: ignore it.
1437 ((org-element-property :footnote-section-p headline) nil)
1438 ;; Case 2. This is a deep sub-tree: export it as a list item.
1439 ;; Also export as items headlines for which no section
1440 ;; format has been found.
1441 ((or (not section-fmt) (org-export-low-level-p headline info))
1442 ;; Build the real contents of the sub-tree.
1443 (let ((low-level-body
1444 (concat
1445 ;; If the headline is the first sibling, start a list.
1446 (when (org-export-first-sibling-p headline info)
1447 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1448 ;; Itemize headline
1449 "\\item " full-text "\n" headline-label pre-blanks contents)))
1450 ;; If headline is not the last sibling simply return
1451 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1452 ;; blank line.
1453 (if (not (org-export-last-sibling-p headline info)) low-level-body
1454 (replace-regexp-in-string
1455 "[ \t\n]*\\'"
1456 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1457 low-level-body))))
1458 ;; Case 3. Standard headline. Export it as a section.
1460 (cond
1461 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1462 ;; Regular section. Use specified format string.
1463 (format section-fmt full-text
1464 (concat headline-label pre-blanks contents)))
1465 ((string-match "\\`\\\\\\(.*?\\){" section-fmt)
1466 ;; If tags should be removed from table of contents, insert
1467 ;; title without tags as an alternative heading in sectioning
1468 ;; command.
1469 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1470 nil nil section-fmt 1)
1471 ;; Replace square brackets with parenthesis since
1472 ;; square brackets are not supported in optional
1473 ;; arguments.
1474 (replace-regexp-in-string
1475 "\\[" "("
1476 (replace-regexp-in-string
1477 "\\]" ")"
1478 full-text-no-tag))
1479 full-text
1480 (concat headline-label pre-blanks contents)))
1482 ;; Impossible to add an alternative heading. Fallback to
1483 ;; regular sectioning format string.
1484 (format section-fmt full-text
1485 (concat headline-label pre-blanks contents))))))))
1488 ;;;; Horizontal Rule
1490 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1491 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1492 CONTENTS is nil. INFO is a plist holding contextual information."
1493 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1494 (prev (org-export-get-previous-element horizontal-rule info)))
1495 (concat
1496 ;; Make sure the rule doesn't start at the end of the current
1497 ;; line by separating it with a blank line from previous element.
1498 (when (and prev
1499 (let ((prev-blank (org-element-property :post-blank prev)))
1500 (or (not prev-blank) (zerop prev-blank))))
1501 "\n")
1502 (org-e-latex--wrap-label
1503 horizontal-rule
1504 (format "\\rule{%s}{%s}"
1505 (or (plist-get attr :width) "\\linewidth")
1506 (or (plist-get attr :thickness) "0.5pt"))))))
1509 ;;;; Inline Babel Call
1511 ;; Inline Babel Calls are ignored.
1514 ;;;; Inline Src Block
1516 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1517 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1518 CONTENTS holds the contents of the item. INFO is a plist holding
1519 contextual information."
1520 (let* ((code (org-element-property :value inline-src-block))
1521 (separator (org-e-latex--find-verb-separator code)))
1522 (cond
1523 ;; Do not use a special package: transcode it verbatim.
1524 ((not org-e-latex-listings)
1525 (concat "\\verb" separator code separator))
1526 ;; Use minted package.
1527 ((eq org-e-latex-listings 'minted)
1528 (let* ((org-lang (org-element-property :language inline-src-block))
1529 (mint-lang (or (cadr (assq (intern org-lang)
1530 org-e-latex-minted-langs))
1531 org-lang))
1532 (options (org-e-latex--make-option-string
1533 org-e-latex-minted-options)))
1534 (concat (format "\\mint%s{%s}"
1535 (if (string= options "") "" (format "[%s]" options))
1536 mint-lang)
1537 separator code separator)))
1538 ;; Use listings package.
1540 ;; Maybe translate language's name.
1541 (let* ((org-lang (org-element-property :language inline-src-block))
1542 (lst-lang (or (cadr (assq (intern org-lang)
1543 org-e-latex-listings-langs))
1544 org-lang))
1545 (options (org-e-latex--make-option-string
1546 (append org-e-latex-listings-options
1547 `(("language" ,lst-lang))))))
1548 (concat (format "\\lstinline[%s]" options)
1549 separator code separator))))))
1552 ;;;; Inlinetask
1554 (defun org-e-latex-inlinetask (inlinetask contents info)
1555 "Transcode an INLINETASK element from Org to LaTeX.
1556 CONTENTS holds the contents of the block. INFO is a plist
1557 holding contextual information."
1558 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1559 (todo (and (plist-get info :with-todo-keywords)
1560 (let ((todo (org-element-property :todo-keyword inlinetask)))
1561 (and todo (org-export-data todo info)))))
1562 (todo-type (org-element-property :todo-type inlinetask))
1563 (tags (and (plist-get info :with-tags)
1564 (org-export-get-tags inlinetask info)))
1565 (priority (and (plist-get info :with-priority)
1566 (org-element-property :priority inlinetask))))
1567 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1568 ;; with appropriate arguments.
1569 (if (functionp org-e-latex-format-inlinetask-function)
1570 (funcall org-e-latex-format-inlinetask-function
1571 todo todo-type priority title tags contents)
1572 ;; Otherwise, use a default template.
1573 (org-e-latex--wrap-label
1574 inlinetask
1575 (let ((full-title
1576 (concat
1577 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1578 (when priority (format "\\framebox{\\#%c} " priority))
1579 title
1580 (when tags (format "\\hfill{}\\textsc{:%s:}"
1581 (mapconcat 'identity tags ":"))))))
1582 (format (concat "\\begin{center}\n"
1583 "\\fbox{\n"
1584 "\\begin{minipage}[c]{.6\\textwidth}\n"
1585 "%s\n\n"
1586 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1587 "%s"
1588 "\\end{minipage}\n"
1589 "}\n"
1590 "\\end{center}")
1591 full-title contents))))))
1594 ;;;; Italic
1596 (defun org-e-latex-italic (italic contents info)
1597 "Transcode ITALIC from Org to LaTeX.
1598 CONTENTS is the text with italic markup. INFO is a plist holding
1599 contextual information."
1600 (org-e-latex--text-markup contents 'italic))
1603 ;;;; Item
1605 (defun org-e-latex-item (item contents info)
1606 "Transcode an ITEM element from Org to LaTeX.
1607 CONTENTS holds the contents of the item. INFO is a plist holding
1608 contextual information."
1609 (let* ((counter
1610 (let ((count (org-element-property :counter item))
1611 (level
1612 (loop for parent in (org-export-get-genealogy item)
1613 count (eq (org-element-type parent) 'plain-list)
1614 until (eq (org-element-type parent) 'headline))))
1615 (and count
1616 (< level 5)
1617 (format "\\setcounter{enum%s}{%s}\n"
1618 (nth (1- level) '("i" "ii" "iii" "iv"))
1619 (1- count)))))
1620 (checkbox (case (org-element-property :checkbox item)
1621 (on "$\\boxtimes$ ")
1622 (off "$\\Box$ ")
1623 (trans "$\\boxminus$ ")))
1624 (tag (let ((tag (org-element-property :tag item)))
1625 ;; Check-boxes must belong to the tag.
1626 (and tag (format "[%s] "
1627 (concat checkbox
1628 (org-export-data tag info)))))))
1629 (concat counter "\\item" (or tag (concat " " checkbox))
1630 (and contents (org-trim contents))
1631 ;; If there are footnotes references in tag, be sure to
1632 ;; add their definition at the end of the item. This
1633 ;; workaround is necessary since "\footnote{}" command is
1634 ;; not supported in tags.
1635 (and tag
1636 (org-e-latex--delayed-footnotes-definitions
1637 (org-element-property :tag item) info)))))
1640 ;;;; Keyword
1642 (defun org-e-latex-keyword (keyword contents info)
1643 "Transcode a KEYWORD element from Org to LaTeX.
1644 CONTENTS is nil. INFO is a plist holding contextual information."
1645 (let ((key (org-element-property :key keyword))
1646 (value (org-element-property :value keyword)))
1647 (cond
1648 ((string= key "LATEX") value)
1649 ((string= key "INDEX") (format "\\index{%s}" value))
1650 ;; Invisible targets.
1651 ((string= key "TARGET") nil)
1652 ((string= key "TOC")
1653 (let ((value (downcase value)))
1654 (cond
1655 ((string-match "\\<headlines\\>" value)
1656 (let ((depth (or (and (string-match "[0-9]+" value)
1657 (string-to-number (match-string 0 value)))
1658 (plist-get info :with-toc))))
1659 (concat
1660 (when (wholenump depth)
1661 (format "\\setcounter{tocdepth}{%s}\n" depth))
1662 "\\tableofcontents")))
1663 ((string= "tables" value) "\\listoftables")
1664 ((string= "figures" value) "\\listoffigures")
1665 ((string= "listings" value)
1666 (cond
1667 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1668 (org-e-latex-listings "\\lstlistoflistings")
1669 ;; At the moment, src blocks with a caption are wrapped
1670 ;; into a figure environment.
1671 (t "\\listoffigures")))))))))
1674 ;;;; Latex Environment
1676 (defun org-e-latex-latex-environment (latex-environment contents info)
1677 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1678 CONTENTS is nil. INFO is a plist holding contextual information."
1679 (let ((label (org-element-property :name latex-environment))
1680 (value (org-remove-indentation
1681 (org-element-property :value latex-environment))))
1682 (if (not (org-string-nw-p label)) value
1683 ;; Environment is labelled: label must be within the environment
1684 ;; (otherwise, a reference pointing to that element will count
1685 ;; the section instead).
1686 (with-temp-buffer
1687 (insert value)
1688 (goto-char (point-min))
1689 (forward-line)
1690 (insert (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1691 (buffer-string)))))
1694 ;;;; Latex Fragment
1696 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1697 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1698 CONTENTS is nil. INFO is a plist holding contextual information."
1699 (org-element-property :value latex-fragment))
1702 ;;;; Line Break
1704 (defun org-e-latex-line-break (line-break contents info)
1705 "Transcode a LINE-BREAK object from Org to LaTeX.
1706 CONTENTS is nil. INFO is a plist holding contextual information."
1707 "\\\\")
1710 ;;;; Link
1712 (defun org-e-latex-link--inline-image (link info)
1713 "Return LaTeX code for an inline image.
1714 LINK is the link pointing to the inline image. INFO is a plist
1715 used as a communication channel."
1716 (let* ((parent (org-export-get-parent-element link))
1717 (path (let ((raw-path (org-element-property :path link)))
1718 (if (not (file-name-absolute-p raw-path)) raw-path
1719 (expand-file-name raw-path))))
1720 (caption (org-e-latex--caption/label-string parent info))
1721 ;; Retrieve latex attributes from the element around.
1722 (attr (let ((raw-attr
1723 (mapconcat #'identity
1724 (org-element-property :attr_latex parent)
1725 " ")))
1726 (unless (string= raw-attr "") raw-attr)))
1727 (disposition
1728 (cond
1729 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1730 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1731 ((or (and attr (string-match "\\<float\\>" attr))
1732 (not (string= caption "")))
1733 'float)))
1734 (placement
1735 (cond
1736 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1737 (org-match-string-no-properties 1 attr))
1738 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1739 ((eq disposition 'float)
1740 (concat "[" org-e-latex-default-figure-position "]"))
1741 (t ""))))
1742 ;; Now clear ATTR from any special keyword and set a default
1743 ;; value if nothing is left.
1744 (setq attr
1745 (if (not attr) ""
1746 (org-trim
1747 (replace-regexp-in-string
1748 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1749 (setq attr (cond ((not (string= attr "")) attr)
1750 ((eq disposition 'float) "width=0.7\\textwidth")
1751 ((eq disposition 'wrap) "width=0.48\\textwidth")
1752 (t (or org-e-latex-image-default-option ""))))
1753 ;; Return proper string, depending on DISPOSITION.
1754 (case disposition
1755 (wrap (format "\\begin{wrapfigure}%s
1756 \\centering
1757 \\includegraphics[%s]{%s}
1758 %s\\end{wrapfigure}" placement attr path caption))
1759 (multicolumn (format "\\begin{figure*}%s
1760 \\centering
1761 \\includegraphics[%s]{%s}
1762 %s\\end{figure*}" placement attr path caption))
1763 (float (format "\\begin{figure}%s
1764 \\centering
1765 \\includegraphics[%s]{%s}
1766 %s\\end{figure}" placement attr path caption))
1767 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1769 (defun org-e-latex-link (link desc info)
1770 "Transcode a LINK object from Org to LaTeX.
1772 DESC is the description part of the link, or the empty string.
1773 INFO is a plist holding contextual information. See
1774 `org-export-data'."
1775 (let* ((type (org-element-property :type link))
1776 (raw-path (org-element-property :path link))
1777 ;; Ensure DESC really exists, or set it to nil.
1778 (desc (and (not (string= desc "")) desc))
1779 (imagep (org-export-inline-image-p
1780 link org-e-latex-inline-image-rules))
1781 (path (cond
1782 ((member type '("http" "https" "ftp" "mailto"))
1783 (concat type ":" raw-path))
1784 ((string= type "file")
1785 (if (file-name-absolute-p raw-path)
1786 (concat "file://" (expand-file-name raw-path))
1787 (concat "file://" raw-path)))
1788 (t raw-path)))
1789 protocol)
1790 (cond
1791 ;; Image file.
1792 (imagep (org-e-latex-link--inline-image link info))
1793 ;; Radio link: Transcode target's contents and use them as link's
1794 ;; description.
1795 ((string= type "radio")
1796 (let ((destination (org-export-resolve-radio-link link info)))
1797 (when destination
1798 (format "\\hyperref[%s]{%s}"
1799 (org-export-solidify-link-text path)
1800 (org-export-data (org-element-contents destination) info)))))
1801 ;; Links pointing to an headline: Find destination and build
1802 ;; appropriate referencing command.
1803 ((member type '("custom-id" "fuzzy" "id"))
1804 (let ((destination (if (string= type "fuzzy")
1805 (org-export-resolve-fuzzy-link link info)
1806 (org-export-resolve-id-link link info))))
1807 (case (org-element-type destination)
1808 ;; Id link points to an external file.
1809 (plain-text
1810 (if desc (format "\\href{file://%s}{%s}" destination desc)
1811 (format "\\url{file://%s}" destination)))
1812 ;; Fuzzy link points nowhere.
1813 ('nil
1814 (format org-e-latex-link-with-unknown-path-format
1815 (or desc
1816 (org-export-data
1817 (org-element-property :raw-link link) info))))
1818 ;; Fuzzy link points to an invisible target.
1819 (keyword nil)
1820 ;; LINK points to an headline. If headlines are numbered
1821 ;; and the link has no description, display headline's
1822 ;; number. Otherwise, display description or headline's
1823 ;; title.
1824 (headline
1825 (let ((label
1826 (format "sec-%s"
1827 (mapconcat
1828 'number-to-string
1829 (org-export-get-headline-number destination info)
1830 "-"))))
1831 (if (and (plist-get info :section-numbers) (not desc))
1832 (format "\\ref{%s}" label)
1833 (format "\\hyperref[%s]{%s}" label
1834 (or desc
1835 (org-export-data
1836 (org-element-property :title destination) info))))))
1837 ;; Fuzzy link points to a target. Do as above.
1838 (otherwise
1839 (let ((path (org-export-solidify-link-text path)))
1840 (if (not desc) (format "\\ref{%s}" path)
1841 (format "\\hyperref[%s]{%s}" path desc)))))))
1842 ;; Coderef: replace link with the reference name or the
1843 ;; equivalent line number.
1844 ((string= type "coderef")
1845 (format (org-export-get-coderef-format path desc)
1846 (org-export-resolve-coderef path info)))
1847 ;; Link type is handled by a special function.
1848 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1849 (funcall protocol (org-link-unescape path) desc 'latex))
1850 ;; External link with a description part.
1851 ((and path desc) (format "\\href{%s}{%s}" path desc))
1852 ;; External link without a description part.
1853 (path (format "\\url{%s}" path))
1854 ;; No path, only description. Try to do something useful.
1855 (t (format org-e-latex-link-with-unknown-path-format desc)))))
1858 ;;;; Paragraph
1860 (defun org-e-latex-paragraph (paragraph contents info)
1861 "Transcode a PARAGRAPH element from Org to LaTeX.
1862 CONTENTS is the contents of the paragraph, as a string. INFO is
1863 the plist used as a communication channel."
1864 contents)
1867 ;;;; Plain List
1869 (defun org-e-latex-plain-list (plain-list contents info)
1870 "Transcode a PLAIN-LIST element from Org to LaTeX.
1871 CONTENTS is the contents of the list. INFO is a plist holding
1872 contextual information."
1873 (let* ((type (org-element-property :type plain-list))
1874 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1875 "inparadesc" "asparadesc"))
1876 (paralist-regexp (concat
1877 "\\("
1878 (mapconcat 'identity paralist-types "\\|")
1879 "\\)"))
1880 (attr (mapconcat #'identity
1881 (org-element-property :attr_latex plain-list)
1882 " "))
1883 (latex-type (cond
1884 ((and attr
1885 (string-match
1886 (format "\\<%s\\>" paralist-regexp) attr))
1887 (match-string 1 attr))
1888 ((eq type 'ordered) "enumerate")
1889 ((eq type 'unordered) "itemize")
1890 ((eq type 'descriptive) "description"))))
1891 (org-e-latex--wrap-label
1892 plain-list
1893 (format "\\begin{%s}%s\n%s\\end{%s}"
1894 latex-type
1895 ;; Once special environment, if any, has been removed, the
1896 ;; rest of the attributes will be optional arguments.
1897 ;; They will be put inside square brackets if necessary.
1898 (let ((opt (replace-regexp-in-string
1899 (format " *%s *" paralist-regexp) "" attr)))
1900 (cond ((string= opt "") "")
1901 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1902 (t (format "[%s]" opt))))
1903 contents
1904 latex-type))))
1907 ;;;; Plain Text
1909 (defun org-e-latex-plain-text (text info)
1910 "Transcode a TEXT string from Org to LaTeX.
1911 TEXT is the string to transcode. INFO is a plist holding
1912 contextual information."
1913 (let ((specialp (plist-get info :with-special-strings)))
1914 ;; Protect %, #, &, $, ~, ^, _, { and }.
1915 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1916 (setq text
1917 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1918 ;; Protect \. If special strings are used, be careful not to
1919 ;; protect "\" in "\-" constructs.
1920 (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
1921 (setq text
1922 (replace-regexp-in-string
1923 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
1924 "$\\backslash$" text nil t 1)))
1925 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1926 (let ((case-fold-search nil)
1927 (start 0))
1928 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1929 (setq text (replace-match
1930 (format "\\%s{}" (match-string 1 text)) nil t text)
1931 start (match-end 0))))
1932 ;; Handle quotation marks.
1933 (setq text (org-e-latex--quotation-marks text info))
1934 ;; Convert special strings.
1935 (when specialp
1936 (setq text (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" text nil t)))
1937 ;; Handle break preservation if required.
1938 (when (plist-get info :preserve-breaks)
1939 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1940 text)))
1941 ;; Return value.
1942 text))
1945 ;;;; Planning
1947 (defun org-e-latex-planning (planning contents info)
1948 "Transcode a PLANNING element from Org to LaTeX.
1949 CONTENTS is nil. INFO is a plist holding contextual
1950 information."
1951 (concat
1952 "\\noindent"
1953 (mapconcat
1954 'identity
1955 (delq nil
1956 (list
1957 (let ((closed (org-element-property :closed planning)))
1958 (when closed
1959 (concat
1960 (format "\\textbf{%s} " org-closed-string)
1961 (format org-e-latex-inactive-timestamp-format
1962 (org-translate-time closed)))))
1963 (let ((deadline (org-element-property :deadline planning)))
1964 (when deadline
1965 (concat
1966 (format "\\textbf{%s} " org-deadline-string)
1967 (format org-e-latex-active-timestamp-format
1968 (org-translate-time deadline)))))
1969 (let ((scheduled (org-element-property :scheduled planning)))
1970 (when scheduled
1971 (concat
1972 (format "\\textbf{%s} " org-scheduled-string)
1973 (format org-e-latex-active-timestamp-format
1974 (org-translate-time scheduled)))))))
1975 " ")
1976 "\\\\"))
1979 ;;;; Property Drawer
1981 (defun org-e-latex-property-drawer (property-drawer contents info)
1982 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1983 CONTENTS is nil. INFO is a plist holding contextual
1984 information."
1985 ;; The property drawer isn't exported but we want separating blank
1986 ;; lines nonetheless.
1990 ;;;; Quote Block
1992 (defun org-e-latex-quote-block (quote-block contents info)
1993 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1994 CONTENTS holds the contents of the block. INFO is a plist
1995 holding contextual information."
1996 (org-e-latex--wrap-label
1997 quote-block
1998 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2001 ;;;; Quote Section
2003 (defun org-e-latex-quote-section (quote-section contents info)
2004 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2005 CONTENTS is nil. INFO is a plist holding contextual information."
2006 (let ((value (org-remove-indentation
2007 (org-element-property :value quote-section))))
2008 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
2011 ;;;; Radio Target
2013 (defun org-e-latex-radio-target (radio-target text info)
2014 "Transcode a RADIO-TARGET object from Org to LaTeX.
2015 TEXT is the text of the target. INFO is a plist holding
2016 contextual information."
2017 (format "\\label{%s}%s"
2018 (org-export-solidify-link-text
2019 (org-element-property :value radio-target))
2020 text))
2023 ;;;; Section
2025 (defun org-e-latex-section (section contents info)
2026 "Transcode a SECTION element from Org to LaTeX.
2027 CONTENTS holds the contents of the section. INFO is a plist
2028 holding contextual information."
2029 contents)
2032 ;;;; Special Block
2034 (defun org-e-latex-special-block (special-block contents info)
2035 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2036 CONTENTS holds the contents of the block. INFO is a plist
2037 holding contextual information."
2038 (let ((type (downcase (org-element-property :type special-block))))
2039 (org-e-latex--wrap-label
2040 special-block
2041 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
2044 ;;;; Src Block
2046 (defun org-e-latex-src-block (src-block contents info)
2047 "Transcode a SRC-BLOCK element from Org to LaTeX.
2048 CONTENTS holds the contents of the item. INFO is a plist holding
2049 contextual information."
2050 (let* ((lang (org-element-property :language src-block))
2051 (caption (org-element-property :caption src-block))
2052 (label (org-element-property :name src-block))
2053 (custom-env (and lang
2054 (cadr (assq (intern lang)
2055 org-e-latex-custom-lang-environments))))
2056 (num-start (case (org-element-property :number-lines src-block)
2057 (continued (org-export-get-loc src-block info))
2058 (new 0)))
2059 (retain-labels (org-element-property :retain-labels src-block)))
2060 (cond
2061 ;; Case 1. No source fontification.
2062 ((not org-e-latex-listings)
2063 (let ((caption-str (org-e-latex--caption/label-string src-block info))
2064 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
2065 (format
2066 (or float-env "%s")
2067 (concat caption-str
2068 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2069 (org-export-format-code-default src-block info))))))
2070 ;; Case 2. Custom environment.
2071 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2072 custom-env
2073 (org-export-format-code-default src-block info)
2074 custom-env))
2075 ;; Case 3. Use minted package.
2076 ((eq org-e-latex-listings 'minted)
2077 (let ((float-env
2078 (when (or label caption)
2079 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2080 (org-e-latex--caption/label-string src-block info))))
2081 (body
2082 (format
2083 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2084 ;; Options.
2085 (org-e-latex--make-option-string
2086 (if (not num-start) org-e-latex-minted-options
2087 (append `(("linenos")
2088 ("firstnumber" ,(number-to-string (1+ num-start))))
2089 org-e-latex-minted-options)))
2090 ;; Language.
2091 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
2092 ;; Source code.
2093 (let* ((code-info (org-export-unravel-code src-block))
2094 (max-width
2095 (apply 'max
2096 (mapcar 'length
2097 (org-split-string (car code-info) "\n")))))
2098 (org-export-format-code
2099 (car code-info)
2100 (lambda (loc num ref)
2101 (concat
2103 (when ref
2104 ;; Ensure references are flushed to the right,
2105 ;; separated with 6 spaces from the widest line
2106 ;; of code.
2107 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2108 (format "(%s)" ref)))))
2109 nil (and retain-labels (cdr code-info)))))))
2110 ;; Return value.
2111 (if float-env (format float-env body) body)))
2112 ;; Case 4. Use listings package.
2114 (let ((lst-lang
2115 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
2116 (caption-str
2117 (when caption
2118 (let ((main (org-export-get-caption src-block))
2119 (secondary (org-export-get-caption src-block t)))
2120 (if (not secondary) (format "{%s}" (org-export-data main info))
2121 (format "{[%s]%s}"
2122 (org-export-data secondary info)
2123 (org-export-data main info)))))))
2124 (concat
2125 ;; Options.
2126 (format "\\lstset{%s}\n"
2127 (org-e-latex--make-option-string
2128 (append org-e-latex-listings-options
2129 `(("language" ,lst-lang))
2130 (when label `(("label" ,label)))
2131 (when caption-str `(("caption" ,caption-str)))
2132 (cond ((not num-start) '(("numbers" "none")))
2133 ((zerop num-start) '(("numbers" "left")))
2134 (t `(("numbers" "left")
2135 ("firstnumber"
2136 ,(number-to-string (1+ num-start)))))))))
2137 ;; Source code.
2138 (format
2139 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2140 (let* ((code-info (org-export-unravel-code src-block))
2141 (max-width
2142 (apply 'max
2143 (mapcar 'length
2144 (org-split-string (car code-info) "\n")))))
2145 (org-export-format-code
2146 (car code-info)
2147 (lambda (loc num ref)
2148 (concat
2150 (when ref
2151 ;; Ensure references are flushed to the right,
2152 ;; separated with 6 spaces from the widest line of
2153 ;; code
2154 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2155 (format "(%s)" ref)))))
2156 nil (and retain-labels (cdr code-info)))))))))))
2159 ;;;; Statistics Cookie
2161 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
2162 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2163 CONTENTS is nil. INFO is a plist holding contextual information."
2164 (replace-regexp-in-string
2165 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2168 ;;;; Strike-Through
2170 (defun org-e-latex-strike-through (strike-through contents info)
2171 "Transcode STRIKE-THROUGH from Org to LaTeX.
2172 CONTENTS is the text with strike-through markup. INFO is a plist
2173 holding contextual information."
2174 (org-e-latex--text-markup contents 'strike-through))
2177 ;;;; Subscript
2179 (defun org-e-latex-subscript (subscript contents info)
2180 "Transcode a SUBSCRIPT object from Org to LaTeX.
2181 CONTENTS is the contents of the object. INFO is a plist holding
2182 contextual information."
2183 (if (= (length contents) 1) (format "$_%s$" contents)
2184 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2185 ;; command for each of them.
2186 (let ((prev-blanks 0))
2187 (mapconcat
2188 (lambda (obj)
2189 (case (org-element-type obj)
2190 ((entity latex-fragment)
2191 (setq prev-blanks (org-element-property :post-blank obj))
2192 (let ((data (org-trim (org-export-data obj info))))
2193 (string-match
2194 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2195 data)
2196 (format "$_{%s}$" (match-string 1 data))))
2197 (plain-text
2198 (format "$_\\mathrm{%s}$"
2199 (concat (make-string prev-blanks ? )
2200 ;; mathrm command doesn't handle spaces,
2201 ;; so we have to enforce them.
2202 (replace-regexp-in-string
2203 " " "\\\\ " (org-export-data obj info)))))
2204 (otherwise
2205 (setq prev-blanks (org-element-property :post-blank obj))
2206 (format "$_{%s}$" (org-export-data obj info)))))
2207 (org-element-contents subscript) ""))))
2210 ;;;; Superscript
2212 (defun org-e-latex-superscript (superscript contents info)
2213 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2214 CONTENTS is the contents of the object. INFO is a plist holding
2215 contextual information."
2216 (if (= (length contents) 1) (format "$^%s$" contents)
2217 ;; Handle multiple objects in SUPERSCRIPT by creating
2218 ;; a superscript command for each of them.
2219 (let ((prev-blanks 0))
2220 (mapconcat
2221 (lambda (obj)
2222 (case (org-element-type obj)
2223 ((entity latex-fragment)
2224 (setq prev-blanks (org-element-property :post-blank obj))
2225 (let ((data (org-trim (org-export-data obj info))))
2226 (string-match
2227 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2228 data)
2229 (format "$^{%s}$" (match-string 1 data))))
2230 (plain-text
2231 (format "$^\\mathrm{%s}$"
2232 (concat (make-string prev-blanks ? )
2233 ;; mathrm command doesn't handle spaces,
2234 ;; so we have to enforce them.
2235 (replace-regexp-in-string
2236 " " "\\\\ " (org-export-data obj info)))))
2237 (otherwise
2238 (setq prev-blanks (org-element-property :post-blank obj))
2239 (format "$^{%s}$" (org-export-data obj info)))))
2240 (org-element-contents superscript) ""))))
2243 ;;;; Table
2245 ;; `org-e-latex-table' is the entry point for table transcoding. It
2246 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
2247 ;; delegates the job to either `org-e-latex-table--table.el-table' or
2248 ;; `org-e-latex-table--org-table' functions, depending of the type of
2249 ;; the table.
2251 ;; `org-e-latex-table--align-string' is a subroutine used to build
2252 ;; alignment string for Org tables.
2254 (defun org-e-latex-table (table contents info)
2255 "Transcode a TABLE element from Org to LaTeX.
2256 CONTENTS is the contents of the table. INFO is a plist holding
2257 contextual information."
2258 (cond
2259 ;; Case 1: verbatim table.
2260 ((or org-e-latex-tables-verbatim
2261 (let ((attr (mapconcat 'identity
2262 (org-element-property :attr_latex table)
2263 " ")))
2264 (and attr (string-match "\\<verbatim\\>" attr))))
2265 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2266 ;; Re-create table, without affiliated keywords.
2267 (org-trim
2268 (org-element-interpret-data
2269 `(table nil ,@(org-element-contents table))))))
2270 ;; Case 2: table.el table. Convert it using appropriate tools.
2271 ((eq (org-element-property :type table) 'table.el)
2272 (org-e-latex-table--table.el-table table contents info))
2273 ;; Case 3: Standard table.
2274 (t (org-e-latex-table--org-table table contents info))))
2276 (defun org-e-latex-table--align-string (table info)
2277 "Return an appropriate LaTeX alignment string.
2278 TABLE is the considered table. INFO is a plist used as
2279 a communication channel."
2280 (let ((attr (mapconcat 'identity
2281 (org-element-property :attr_latex table)
2282 " ")))
2283 (if (string-match "\\<align=\\(\\S-+\\)" attr) (match-string 1 attr)
2284 (let (alignment)
2285 ;; Extract column groups and alignment from first (non-rule)
2286 ;; row.
2287 (org-element-map
2288 (org-element-map
2289 table 'table-row
2290 (lambda (row)
2291 (and (eq (org-element-property :type row) 'standard) row))
2292 info 'first-match)
2293 'table-cell
2294 (lambda (cell)
2295 (let ((borders (org-export-table-cell-borders cell info)))
2296 ;; Check left border for the first cell only.
2297 (when (and (memq 'left borders) (not alignment))
2298 (push "|" alignment))
2299 (push (case (org-export-table-cell-alignment cell info)
2300 (left "l")
2301 (right "r")
2302 (center "c"))
2303 alignment)
2304 (when (memq 'right borders) (push "|" alignment))))
2305 info)
2306 (apply 'concat (reverse alignment))))))
2308 (defun org-e-latex-table--org-table (table contents info)
2309 "Return appropriate LaTeX code for an Org table.
2311 TABLE is the table type element to transcode. CONTENTS is its
2312 contents, as a string. INFO is a plist used as a communication
2313 channel.
2315 This function assumes TABLE has `org' as its `:type' attribute."
2316 (let* ((label (org-element-property :name table))
2317 (caption (org-e-latex--caption/label-string table info))
2318 (attr (mapconcat 'identity
2319 (org-element-property :attr_latex table)
2320 " "))
2321 ;; Determine alignment string.
2322 (alignment (org-e-latex-table--align-string table info))
2323 ;; Determine environment for the table: longtable, tabular...
2324 (table-env (cond
2325 ((not attr) org-e-latex-default-table-environment)
2326 ((string-match "\\<longtable\\>" attr) "longtable")
2327 ((string-match "\\<tabular.?\\>" attr)
2328 (org-match-string-no-properties 0 attr))
2329 (t org-e-latex-default-table-environment)))
2330 ;; If table is a float, determine environment: table, table*
2331 ;; or sidewaystable.
2332 (float-env (cond
2333 ((string= "longtable" table-env) nil)
2334 ((and attr (string-match "\\<sidewaystable\\>" attr))
2335 "sidewaystable")
2336 ((and attr
2337 (or (string-match (regexp-quote "table*") attr)
2338 (string-match "\\<multicolumn\\>" attr)))
2339 "table*")
2340 ((or (not (string= caption "")) label) "table")))
2341 ;; Extract others display options.
2342 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
2343 (org-match-string-no-properties 1 attr)))
2344 (placement
2345 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
2346 (org-match-string-no-properties 1 attr)
2347 (format "[%s]" org-e-latex-default-figure-position))))
2348 ;; Prepare the final format string for the table.
2349 (cond
2350 ;; Longtable.
2351 ((string= "longtable" table-env)
2352 (format
2353 "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
2354 alignment
2355 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
2356 (concat (org-trim caption) "\\\\\n"))
2357 contents
2358 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
2359 (concat (org-trim caption) "\\\\\n"))))
2360 ;; Others.
2361 (t (concat (when float-env
2362 (concat
2363 (format "\\begin{%s}%s\n" float-env placement)
2364 (if org-e-latex-table-caption-above caption "")))
2365 (when org-e-latex-tables-centered "\\begin{center}\n")
2366 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2367 table-env
2368 (if width (format "{%s}" width) "")
2369 alignment
2370 contents
2371 table-env)
2372 (when org-e-latex-tables-centered "\n\\end{center}")
2373 (when float-env
2374 (concat (if org-e-latex-table-caption-above "" caption)
2375 (format "\n\\end{%s}" float-env))))))))
2377 (defun org-e-latex-table--table.el-table (table contents info)
2378 "Return appropriate LaTeX code for a table.el table.
2380 TABLE is the table type element to transcode. CONTENTS is its
2381 contents, as a string. INFO is a plist used as a communication
2382 channel.
2384 This function assumes TABLE has `table.el' as its `:type'
2385 attribute."
2386 (require 'table)
2387 ;; Ensure "*org-export-table*" buffer is empty.
2388 (with-current-buffer (get-buffer-create "*org-export-table*")
2389 (erase-buffer))
2390 (let ((output (with-temp-buffer
2391 (insert (org-element-property :value table))
2392 (goto-char 1)
2393 (re-search-forward "^[ \t]*|[^|]" nil t)
2394 (table-generate-source 'latex "*org-export-table*")
2395 (with-current-buffer "*org-export-table*"
2396 (org-trim (buffer-string))))))
2397 (kill-buffer (get-buffer "*org-export-table*"))
2398 ;; Remove left out comments.
2399 (while (string-match "^%.*\n" output)
2400 (setq output (replace-match "" t t output)))
2401 ;; When the "rmlines" attribute is provided, remove all hlines but
2402 ;; the the one separating heading from the table body.
2403 (let ((attr (mapconcat 'identity
2404 (org-element-property :attr_latex table)
2405 " ")))
2406 (when (and attr (string-match "\\<rmlines\\>" attr))
2407 (let ((n 0) (pos 0))
2408 (while (and (< (length output) pos)
2409 (setq pos (string-match "^\\\\hline\n?" output pos)))
2410 (incf n)
2411 (unless (= n 2)
2412 (setq output (replace-match "" nil nil output)))))))
2413 (if (not org-e-latex-tables-centered) output
2414 (format "\\begin{center}\n%s\n\\end{center}" output))))
2417 ;;;; Table Cell
2419 (defun org-e-latex-table-cell (table-cell contents info)
2420 "Transcode a TABLE-CELL element from Org to LaTeX.
2421 CONTENTS is the cell contents. INFO is a plist used as
2422 a communication channel."
2423 (concat (if (and contents
2424 org-e-latex-table-scientific-notation
2425 (string-match orgtbl-exp-regexp contents))
2426 ;; Use appropriate format string for scientific
2427 ;; notation.
2428 (format org-e-latex-table-scientific-notation
2429 (match-string 1 contents)
2430 (match-string 2 contents))
2431 contents)
2432 (when (org-export-get-next-element table-cell info) " & ")))
2435 ;;;; Table Row
2437 (defun org-e-latex-table-row (table-row contents info)
2438 "Transcode a TABLE-ROW element from Org to LaTeX.
2439 CONTENTS is the contents of the row. INFO is a plist used as
2440 a communication channel."
2441 ;; Rules are ignored since table separators are deduced from
2442 ;; borders of the current row.
2443 (when (eq (org-element-property :type table-row) 'standard)
2444 (let* ((attr (mapconcat 'identity
2445 (org-element-property
2446 :attr_latex (org-export-get-parent table-row))
2447 " "))
2448 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
2449 (booktabsp
2450 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
2451 org-e-latex-tables-booktabs))
2452 ;; TABLE-ROW's borders are extracted from its first cell.
2453 (borders
2454 (org-export-table-cell-borders
2455 (car (org-element-contents table-row)) info)))
2456 (concat
2457 ;; When BOOKTABS are activated enforce top-rule even when no
2458 ;; hline was specifically marked.
2459 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2460 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2461 contents "\\\\\n"
2462 (cond
2463 ;; Special case for long tables. Define header and footers.
2464 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2465 (format "%s
2466 \\endhead
2467 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2468 \\endfoot
2469 \\endlastfoot"
2470 (if booktabsp "\\midrule" "\\hline")
2471 (if booktabsp "\\midrule" "\\hline")
2472 ;; Number of columns.
2473 (cdr (org-export-table-dimensions
2474 (org-export-get-parent-table table-row) info))))
2475 ;; When BOOKTABS are activated enforce bottom rule even when
2476 ;; no hline was specifically marked.
2477 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2478 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2479 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2482 ;;;; Target
2484 (defun org-e-latex-target (target contents info)
2485 "Transcode a TARGET object from Org to LaTeX.
2486 CONTENTS is nil. INFO is a plist holding contextual
2487 information."
2488 (format "\\label{%s}"
2489 (org-export-solidify-link-text (org-element-property :value target))))
2492 ;;;; Timestamp
2494 (defun org-e-latex-timestamp (timestamp contents info)
2495 "Transcode a TIMESTAMP object from Org to LaTeX.
2496 CONTENTS is nil. INFO is a plist holding contextual
2497 information."
2498 (let ((value (org-translate-time (org-element-property :value timestamp)))
2499 (range-end (org-element-property :range-end timestamp)))
2500 (case (org-element-property :type timestamp)
2501 (active (format org-e-latex-active-timestamp-format value))
2502 (active-range
2503 (concat (format org-e-latex-active-timestamp-format value)
2504 "--"
2505 (format org-e-latex-active-timestamp-format
2506 (org-translate-time range-end))))
2507 (inactive (format org-e-latex-inactive-timestamp-format value))
2508 (inactive-range
2509 (concat (format org-e-latex-inactive-timestamp-format value)
2510 "--"
2511 (format org-e-latex-inactive-timestamp-format
2512 (org-translate-time range-end))))
2513 (otherwise (format org-e-latex-diary-timestamp-format value)))))
2516 ;;;; Underline
2518 (defun org-e-latex-underline (underline contents info)
2519 "Transcode UNDERLINE from Org to LaTeX.
2520 CONTENTS is the text with underline markup. INFO is a plist
2521 holding contextual information."
2522 (org-e-latex--text-markup contents 'underline))
2525 ;;;; Verbatim
2527 (defun org-e-latex-verbatim (verbatim contents info)
2528 "Transcode a VERBATIM object from Org to LaTeX.
2529 CONTENTS is nil. INFO is a plist used as a communication
2530 channel."
2531 (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2534 ;;;; Verse Block
2536 (defun org-e-latex-verse-block (verse-block contents info)
2537 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2538 CONTENTS is verse block contents. INFO is a plist holding
2539 contextual information."
2540 (org-e-latex--wrap-label
2541 verse-block
2542 ;; In a verse environment, add a line break to each newline
2543 ;; character and change each white space at beginning of a line
2544 ;; into a space of 1 em. Also change each blank line with
2545 ;; a vertical space of 1 em.
2546 (progn
2547 (setq contents (replace-regexp-in-string
2548 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2549 (replace-regexp-in-string
2550 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2551 (while (string-match "^[ \t]+" contents)
2552 (let ((new-str (format "\\hspace*{%dem}"
2553 (length (match-string 0 contents)))))
2554 (setq contents (replace-match new-str nil t contents))))
2555 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2559 ;;; Interactive functions
2561 ;;;###autoload
2562 (defun org-e-latex-export-as-latex
2563 (&optional subtreep visible-only body-only ext-plist)
2564 "Export current buffer as a LaTeX buffer.
2566 If narrowing is active in the current buffer, only export its
2567 narrowed part.
2569 If a region is active, export that region.
2571 When optional argument SUBTREEP is non-nil, export the sub-tree
2572 at point, extracting information from the headline properties
2573 first.
2575 When optional argument VISIBLE-ONLY is non-nil, don't export
2576 contents of hidden elements.
2578 When optional argument BODY-ONLY is non-nil, only write code
2579 between \"\\begin{document}\" and \"\\end{document}\".
2581 EXT-PLIST, when provided, is a property list with external
2582 parameters overriding Org default settings, but still inferior to
2583 file-local settings.
2585 Export is done in a buffer named \"*Org E-LATEX Export*\", which
2586 will be displayed when `org-export-show-temporary-export-buffer'
2587 is non-nil."
2588 (interactive)
2589 (let ((outbuf (org-export-to-buffer
2590 'e-latex "*Org E-LATEX Export*"
2591 subtreep visible-only body-only ext-plist)))
2592 (with-current-buffer outbuf (LaTeX-mode))
2593 (when org-export-show-temporary-export-buffer
2594 (switch-to-buffer-other-window outbuf))))
2596 ;;;###autoload
2597 (defun org-e-latex-export-to-latex
2598 (&optional subtreep visible-only body-only ext-plist pub-dir)
2599 "Export current buffer to a LaTeX file.
2601 If narrowing is active in the current buffer, only export its
2602 narrowed part.
2604 If a region is active, export that region.
2606 When optional argument SUBTREEP is non-nil, export the sub-tree
2607 at point, extracting information from the headline properties
2608 first.
2610 When optional argument VISIBLE-ONLY is non-nil, don't export
2611 contents of hidden elements.
2613 When optional argument BODY-ONLY is non-nil, only write code
2614 between \"\\begin{document}\" and \"\\end{document}\".
2616 EXT-PLIST, when provided, is a property list with external
2617 parameters overriding Org default settings, but still inferior to
2618 file-local settings.
2620 When optional argument PUB-DIR is set, use it as the publishing
2621 directory.
2623 Return output file's name."
2624 (interactive)
2625 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2626 (org-export-to-file
2627 'e-latex outfile subtreep visible-only body-only ext-plist)))
2629 ;;;###autoload
2630 (defun org-e-latex-export-to-pdf
2631 (&optional subtreep visible-only body-only ext-plist pub-dir)
2632 "Export current buffer to LaTeX then process through to PDF.
2634 If narrowing is active in the current buffer, only export its
2635 narrowed part.
2637 If a region is active, export that region.
2639 When optional argument SUBTREEP is non-nil, export the sub-tree
2640 at point, extracting information from the headline properties
2641 first.
2643 When optional argument VISIBLE-ONLY is non-nil, don't export
2644 contents of hidden elements.
2646 When optional argument BODY-ONLY is non-nil, only write code
2647 between \"\\begin{document}\" and \"\\end{document}\".
2649 EXT-PLIST, when provided, is a property list with external
2650 parameters overriding Org default settings, but still inferior to
2651 file-local settings.
2653 When optional argument PUB-DIR is set, use it as the publishing
2654 directory.
2656 Return PDF file's name."
2657 (interactive)
2658 (org-e-latex-compile
2659 (org-e-latex-export-to-latex
2660 subtreep visible-only body-only ext-plist pub-dir)))
2662 (defun org-e-latex-compile (texfile)
2663 "Compile a TeX file.
2665 TEXFILE is the name of the file being compiled. Processing is
2666 done through the command specified in `org-e-latex-pdf-process'.
2668 Return PDF file name or an error if it couldn't be produced."
2669 (let* ((wconfig (current-window-configuration))
2670 (texfile (file-truename texfile))
2671 (base (file-name-sans-extension texfile))
2672 errors)
2673 (message (format "Processing LaTeX file %s ..." texfile))
2674 (unwind-protect
2675 (progn
2676 (cond
2677 ;; A function is provided: Apply it.
2678 ((functionp org-e-latex-pdf-process)
2679 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2680 ;; A list is provided: Replace %b, %f and %o with appropriate
2681 ;; values in each command before applying it. Output is
2682 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2683 ((consp org-e-latex-pdf-process)
2684 (let* ((out-dir (or (file-name-directory texfile) "./"))
2685 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2686 (mapc
2687 (lambda (command)
2688 (shell-command
2689 (replace-regexp-in-string
2690 "%b" (shell-quote-argument base)
2691 (replace-regexp-in-string
2692 "%f" (shell-quote-argument texfile)
2693 (replace-regexp-in-string
2694 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2695 outbuf))
2696 org-e-latex-pdf-process)
2697 ;; Collect standard errors from output buffer.
2698 (setq errors (org-e-latex--collect-errors outbuf))))
2699 (t (error "No valid command to process to PDF")))
2700 (let ((pdffile (concat base ".pdf")))
2701 ;; Check for process failure. Provide collected errors if
2702 ;; possible.
2703 (if (not (file-exists-p pdffile))
2704 (error (concat (format "PDF file %s wasn't produced" pdffile)
2705 (when errors (concat ": " errors))))
2706 ;; Else remove log files, when specified, and signal end of
2707 ;; process to user, along with any error encountered.
2708 (when org-e-latex-remove-logfiles
2709 (dolist (ext org-e-latex-logfiles-extensions)
2710 (let ((file (concat base "." ext)))
2711 (when (file-exists-p file) (delete-file file)))))
2712 (message (concat "Process completed"
2713 (if (not errors) "."
2714 (concat " with errors: " errors)))))
2715 ;; Return output file name.
2716 pdffile))
2717 (set-window-configuration wconfig))))
2719 (defun org-e-latex--collect-errors (buffer)
2720 "Collect some kind of errors from \"pdflatex\" command output.
2722 BUFFER is the buffer containing output.
2724 Return collected error types as a string, or nil if there was
2725 none."
2726 (with-current-buffer buffer
2727 (save-excursion
2728 (goto-char (point-max))
2729 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
2730 (let ((case-fold-search t)
2731 (errors ""))
2732 (dolist (latex-error org-e-latex-known-errors)
2733 (when (save-excursion (re-search-forward (car latex-error) nil t))
2734 (setq errors (concat errors " " (cdr latex-error)))))
2735 (and (org-string-nw-p errors) (org-trim errors)))))))
2738 (provide 'org-e-latex)
2739 ;;; org-e-latex.el ends here