org-e-latex: Ignore empty captions in lstlisting environments
[org-mode.git] / contrib / lisp / org-e-latex.el
blob458579c16b43058a309dfa655c542b6141820aa7
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)
825 ;;; Internal Functions
827 (defun org-e-latex--caption/label-string (element info)
828 "Return caption and label LaTeX string for ELEMENT.
830 INFO is a plist holding contextual information. If there's no
831 caption nor label, return the empty string.
833 For non-floats, see `org-e-latex--wrap-label'."
834 (let* ((label (org-element-property :name element))
835 (label-str (if label (format "\\label{%s}" label) ""))
836 (main (org-export-get-caption element))
837 (short (org-export-get-caption element t)))
838 (cond
839 ((and (not main) (not label)) "")
840 ((not main) (format "\\label{%s}\n" label))
841 ;; Option caption format with short name.
842 (short (format "\\caption[%s]{%s%s}\n"
843 (org-export-data short info)
844 label-str
845 (org-export-data main info)))
846 ;; Standard caption format.
847 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
849 (defun org-e-latex--guess-babel-language (header info)
850 "Set Babel's language according to LANGUAGE keyword.
852 HEADER is the LaTeX header string. INFO is the plist used as
853 a communication channel.
855 Insertion of guessed language only happens when Babel package has
856 explicitly been loaded. Then it is added to the rest of
857 package's options.
859 Return the new header."
860 (let ((language-code (plist-get info :language)))
861 ;; If no language is set or Babel package is not loaded, return
862 ;; HEADER as-is.
863 (if (or (not (stringp language-code))
864 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
865 header
866 (let ((options (save-match-data
867 (org-split-string (match-string 1 header) ",")))
868 (language (cdr (assoc language-code
869 org-e-latex-babel-language-alist))))
870 ;; If LANGUAGE is already loaded, return header. Otherwise,
871 ;; append LANGUAGE to other options.
872 (if (member language options) header
873 (replace-match (mapconcat 'identity
874 (append options (list language))
875 ",")
876 nil nil header 1))))))
878 (defun org-e-latex--guess-inputenc (header)
879 "Set the coding system in inputenc to what the buffer is.
880 HEADER is the LaTeX header string. Return the new header."
881 (let* ((cs (or (ignore-errors
882 (latexenc-coding-system-to-inputenc
883 buffer-file-coding-system))
884 "utf8")))
885 (if (not cs) header
886 ;; First translate if that is requested.
887 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
888 ;; Then find the \usepackage statement and replace the option.
889 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
890 cs header t nil 1))))
892 (defun org-e-latex--find-verb-separator (s)
893 "Return a character not used in string S.
894 This is used to choose a separator for constructs like \\verb."
895 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
896 (loop for c across ll
897 when (not (string-match (regexp-quote (char-to-string c)) s))
898 return (char-to-string c))))
900 (defun org-e-latex--make-option-string (options)
901 "Return a comma separated string of keywords and values.
902 OPTIONS is an alist where the key is the options keyword as
903 a string, and the value a list containing the keyword value, or
904 nil."
905 (mapconcat (lambda (pair)
906 (concat (first pair)
907 (when (> (length (second pair)) 0)
908 (concat "=" (second pair)))))
909 options
910 ","))
912 (defun org-e-latex--quotation-marks (text info)
913 "Export quotation marks depending on language conventions.
914 TEXT is a string containing quotation marks to be replaced. INFO
915 is a plist used as a communication channel."
916 (mapc (lambda(l)
917 (let ((start 0))
918 (while (setq start (string-match (car l) text start))
919 (let ((new-quote (concat (match-string 1 text) (cdr l))))
920 (setq text (replace-match new-quote t t text))))))
921 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
922 ;; Falls back on English.
923 (assoc "en" org-e-latex-quotes))))
924 text)
926 (defun org-e-latex--wrap-label (element output)
927 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
928 This function shouldn't be used for floats. See
929 `org-e-latex--caption/label-string'."
930 (let ((label (org-element-property :name element)))
931 (if (or (not output) (not label) (string= output "") (string= label ""))
932 output
933 (concat (format "\\label{%s}\n" label) output))))
935 (defun org-e-latex--text-markup (text markup)
936 "Format TEXT depending on MARKUP text markup.
937 See `org-e-latex-text-markup-alist' for details."
938 (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist))))
939 (cond
940 ;; No format string: Return raw text.
941 ((not fmt) text)
942 ;; Handle the `verb' special case: Find and appropriate separator
943 ;; and use "\\verb" command.
944 ((eq 'verb fmt)
945 (let ((separator (org-e-latex--find-verb-separator text)))
946 (concat "\\verb" separator text separator)))
947 ;; Handle the `protectedtexttt' special case: Protect some
948 ;; special chars and use "\texttt{%s}" format string.
949 ((eq 'protectedtexttt fmt)
950 (let ((start 0)
951 (trans '(("\\" . "\\textbackslash{}")
952 ("~" . "\\textasciitilde{}")
953 ("^" . "\\textasciicircum{}")))
954 (rtn "")
955 char)
956 (while (string-match "[\\{}$%&_#~^]" text)
957 (setq char (match-string 0 text))
958 (if (> (match-beginning 0) 0)
959 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
960 (setq text (substring text (1+ (match-beginning 0))))
961 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
962 rtn (concat rtn char)))
963 (setq text (concat rtn text)
964 fmt "\\texttt{%s}")
965 (while (string-match "--" text)
966 (setq text (replace-match "-{}-" t t text)))
967 (format fmt text)))
968 ;; Else use format string.
969 (t (format fmt text)))))
971 (defun org-e-latex--delayed-footnotes-definitions (element info)
972 "Return footnotes definitions in ELEMENT as a string.
974 INFO is a plist used as a communication channel.
976 Footnotes definitions are returned within \"\\footnotetxt{}\"
977 commands.
979 This function is used within constructs that don't support
980 \"\\footnote{}\" command (i.e. an item's tag). In that case,
981 \"\\footnotemark\" is used within the construct and the function
982 just outside of it."
983 (mapconcat
984 (lambda (ref)
985 (format
986 "\\footnotetext[%s]{%s}"
987 (org-export-get-footnote-number ref info)
988 (org-trim
989 (org-export-data
990 (org-export-get-footnote-definition ref info) info))))
991 ;; Find every footnote reference in ELEMENT.
992 (let* (all-refs
993 search-refs ; For byte-compiler.
994 (search-refs
995 (function
996 (lambda (data)
997 ;; Return a list of all footnote references never seen
998 ;; before in DATA.
999 (org-element-map
1000 data 'footnote-reference
1001 (lambda (ref)
1002 (when (org-export-footnote-first-reference-p ref info)
1003 (push ref all-refs)
1004 (when (eq (org-element-property :type ref) 'standard)
1005 (funcall search-refs
1006 (org-export-get-footnote-definition ref info)))))
1007 info)
1008 (reverse all-refs)))))
1009 (funcall search-refs element))
1010 ""))
1014 ;;; Template
1016 (defun org-e-latex-template (contents info)
1017 "Return complete document string after LaTeX conversion.
1018 CONTENTS is the transcoded contents string. INFO is a plist
1019 holding export options."
1020 (let ((title (org-export-data (plist-get info :title) info)))
1021 (concat
1022 ;; Time-stamp.
1023 (and (plist-get info :time-stamp-file)
1024 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1025 ;; Document class and packages.
1026 (let ((class (plist-get info :latex-class))
1027 (class-options (plist-get info :latex-class-options)))
1028 (org-element-normalize-string
1029 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
1030 (document-class-string
1031 (and (stringp header)
1032 (if (not class-options) header
1033 (replace-regexp-in-string
1034 "^[ \t]*\\\\documentclass\\(\\(\\[.*\\]\\)?\\)"
1035 class-options header t nil 1)))))
1036 (when document-class-string
1037 (org-e-latex--guess-babel-language
1038 (org-e-latex--guess-inputenc
1039 (org-splice-latex-header
1040 document-class-string
1041 org-export-latex-default-packages-alist ; defined in org.el
1042 org-export-latex-packages-alist nil ; defined in org.el
1043 (plist-get info :latex-header-extra)))
1044 info)))))
1045 ;; Possibly limit depth for headline numbering.
1046 (let ((sec-num (plist-get info :section-numbers)))
1047 (when (integerp sec-num)
1048 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1049 ;; Author.
1050 (let ((author (and (plist-get info :with-author)
1051 (let ((auth (plist-get info :author)))
1052 (and auth (org-export-data auth info)))))
1053 (email (and (plist-get info :with-email)
1054 (org-export-data (plist-get info :email) info))))
1055 (cond ((and author email (not (string= "" email)))
1056 (format "\\author{%s\\thanks{%s}}\n" author email))
1057 (author (format "\\author{%s}\n" author))
1058 (t "\\author{}\n")))
1059 ;; Date.
1060 (let ((date (org-export-data (plist-get info :date) info)))
1061 (and date (format "\\date{%s}\n" date)))
1062 ;; Title
1063 (format "\\title{%s}\n" title)
1064 ;; Hyperref options.
1065 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1066 (or (plist-get info :keywords) "")
1067 (or (plist-get info :description) "")
1068 (if (not (plist-get info :with-creator)) ""
1069 (plist-get info :creator)))
1070 ;; Document start.
1071 "\\begin{document}\n\n"
1072 ;; Title command.
1073 (org-element-normalize-string
1074 (cond ((string= "" title) nil)
1075 ((not (stringp org-e-latex-title-command)) nil)
1076 ((string-match "\\(?:[^%]\\|^\\)%s"
1077 org-e-latex-title-command)
1078 (format org-e-latex-title-command title))
1079 (t org-e-latex-title-command)))
1080 ;; Table of contents.
1081 (let ((depth (plist-get info :with-toc)))
1082 (when depth
1083 (concat (when (wholenump depth)
1084 (format "\\setcounter{tocdepth}{%d}\n" depth))
1085 org-e-latex-toc-command)))
1086 ;; Document's body.
1087 contents
1088 ;; Creator.
1089 (let ((creator-info (plist-get info :with-creator)))
1090 (cond
1091 ((not creator-info) "")
1092 ((eq creator-info 'comment)
1093 (format "%% %s\n" (plist-get info :creator)))
1094 (t (concat (plist-get info :creator) "\n"))))
1095 ;; Document end.
1096 "\\end{document}")))
1100 ;;; Transcode Functions
1102 ;;;; Babel Call
1104 ;; Babel Calls are ignored.
1107 ;;;; Bold
1109 (defun org-e-latex-bold (bold contents info)
1110 "Transcode BOLD from Org to LaTeX.
1111 CONTENTS is the text with bold markup. INFO is a plist holding
1112 contextual information."
1113 (org-e-latex--text-markup contents 'bold))
1116 ;;;; Center Block
1118 (defun org-e-latex-center-block (center-block contents info)
1119 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1120 CONTENTS holds the contents of the center block. INFO is a plist
1121 holding contextual information."
1122 (org-e-latex--wrap-label
1123 center-block
1124 (format "\\begin{center}\n%s\\end{center}" contents)))
1127 ;;;; Clock
1129 (defun org-e-latex-clock (clock contents info)
1130 "Transcode a CLOCK element from Org to LaTeX.
1131 CONTENTS is nil. INFO is a plist holding contextual
1132 information."
1133 (concat
1134 "\\noindent"
1135 (format "\\textbf{%s} " org-clock-string)
1136 (format org-e-latex-inactive-timestamp-format
1137 (concat (org-translate-time (org-element-property :value clock))
1138 (let ((time (org-element-property :time clock)))
1139 (and time (format " (%s)" time)))))
1140 "\\\\"))
1143 ;;;; Code
1145 (defun org-e-latex-code (code contents info)
1146 "Transcode a CODE object from Org to LaTeX.
1147 CONTENTS is nil. INFO is a plist used as a communication
1148 channel."
1149 (org-e-latex--text-markup (org-element-property :value code) 'code))
1152 ;;;; Comment
1154 ;; Comments are ignored.
1157 ;;;; Comment Block
1159 ;; Comment Blocks are ignored.
1162 ;;;; Drawer
1164 (defun org-e-latex-drawer (drawer contents info)
1165 "Transcode a DRAWER element from Org to LaTeX.
1166 CONTENTS holds the contents of the block. INFO is a plist
1167 holding contextual information."
1168 (let* ((name (org-element-property :drawer-name drawer))
1169 (output (if (functionp org-e-latex-format-drawer-function)
1170 (funcall org-e-latex-format-drawer-function
1171 name contents)
1172 ;; If there's no user defined function: simply
1173 ;; display contents of the drawer.
1174 contents)))
1175 (org-e-latex--wrap-label drawer output)))
1178 ;;;; Dynamic Block
1180 (defun org-e-latex-dynamic-block (dynamic-block contents info)
1181 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1182 CONTENTS holds the contents of the block. INFO is a plist
1183 holding contextual information. See `org-export-data'."
1184 (org-e-latex--wrap-label dynamic-block contents))
1187 ;;;; Entity
1189 (defun org-e-latex-entity (entity contents info)
1190 "Transcode an ENTITY object from Org to LaTeX.
1191 CONTENTS are the definition itself. INFO is a plist holding
1192 contextual information."
1193 (let ((ent (org-element-property :latex entity)))
1194 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1197 ;;;; Example Block
1199 (defun org-e-latex-example-block (example-block contents info)
1200 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1201 CONTENTS is nil. INFO is a plist holding contextual
1202 information."
1203 (org-e-latex--wrap-label
1204 example-block
1205 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1206 (org-export-format-code-default example-block info))))
1209 ;;;; Export Block
1211 (defun org-e-latex-export-block (export-block contents info)
1212 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1213 CONTENTS is nil. INFO is a plist holding contextual information."
1214 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1215 (org-remove-indentation (org-element-property :value export-block))))
1218 ;;;; Export Snippet
1220 (defun org-e-latex-export-snippet (export-snippet contents info)
1221 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1222 CONTENTS is nil. INFO is a plist holding contextual information."
1223 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
1224 (org-element-property :value export-snippet)))
1227 ;;;; Fixed Width
1229 (defun org-e-latex-fixed-width (fixed-width contents info)
1230 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1231 CONTENTS is nil. INFO is a plist holding contextual information."
1232 (org-e-latex--wrap-label
1233 fixed-width
1234 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1235 (org-remove-indentation
1236 (org-element-property :value fixed-width)))))
1239 ;;;; Footnote Definition
1241 ;; Footnote Definitions are ignored.
1244 ;;;; Footnote Reference
1246 ;; Footnote reference export is handled by
1247 ;; `org-e-latex-footnote-reference'.
1249 ;; Internally, `org-e-latex--get-footnote-counter' is used to restore
1250 ;; the value of the LaTeX "footnote" counter after a jump due to
1251 ;; a reference to an already defined footnote. It is only needed in
1252 ;; item tags since the optional argument to \footnotemark is not
1253 ;; allowed there.
1255 (defun org-e-latex--get-footnote-counter (footnote-reference info)
1256 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1257 INFO is a plist used as a communication channel."
1258 ;; Find original counter value by counting number of footnote
1259 ;; references appearing for the first time before the current
1260 ;; footnote reference.
1261 (let* ((label (org-element-property :label footnote-reference))
1262 seen-refs
1263 search-ref ; For byte-compiler.
1264 (search-ref
1265 (function
1266 (lambda (data)
1267 ;; Search footnote references through DATA, filling
1268 ;; SEEN-REFS along the way.
1269 (org-element-map
1270 data 'footnote-reference
1271 (lambda (fn)
1272 (let ((fn-lbl (org-element-property :label fn)))
1273 (cond
1274 ;; Anonymous footnote match: return number.
1275 ((eq fn footnote-reference) (length seen-refs))
1276 ;; Anonymous footnote: it's always a new one.
1277 ;; Also, be sure to return nil from the `cond' so
1278 ;; `first-match' doesn't get us out of the loop.
1279 ((not fn-lbl) (push 'inline seen-refs) nil)
1280 ;; Label not seen so far: add it so SEEN-REFS.
1282 ;; Also search for subsequent references in
1283 ;; footnote definition so numbering follows reading
1284 ;; logic. Note that we don't have to care about
1285 ;; inline definitions, since `org-element-map'
1286 ;; already traverse them at the right time.
1287 ((not (member fn-lbl seen-refs))
1288 (push fn-lbl seen-refs)
1289 (funcall search-ref
1290 (org-export-get-footnote-definition fn info))))))
1291 ;; Don't enter footnote definitions since it will happen
1292 ;; when their first reference is found.
1293 info 'first-match 'footnote-definition)))))
1294 (funcall search-ref (plist-get info :parse-tree))))
1296 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1297 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1298 CONTENTS is nil. INFO is a plist holding contextual information."
1299 (concat
1300 ;; Insert separator between two footnotes in a row.
1301 (let ((prev (org-export-get-previous-element footnote-reference info)))
1302 (when (eq (org-element-type prev) 'footnote-reference)
1303 org-e-latex-footnote-separator))
1304 (cond
1305 ;; Use \footnotemark if reference is within an item's tag.
1306 ((eq (org-element-type (org-export-get-parent-element footnote-reference))
1307 'item)
1308 (if (org-export-footnote-first-reference-p footnote-reference info)
1309 "\\footnotemark"
1310 ;; Since we can't specify footnote number as an optional
1311 ;; argument within an item tag, some extra work has to be done
1312 ;; when the footnote has already been referenced. In that
1313 ;; case, set footnote counter to the desired number, use the
1314 ;; footnotemark, then set counter back to its original value.
1315 (format
1316 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1317 (1- (org-export-get-footnote-number footnote-reference info))
1318 (org-e-latex--get-footnote-counter footnote-reference info))))
1319 ;; Use \footnotemark if the footnote has already been defined.
1320 ((not (org-export-footnote-first-reference-p footnote-reference info))
1321 (format "\\footnotemark[%s]{}"
1322 (org-export-get-footnote-number footnote-reference info)))
1323 ;; Use \footnotemark if reference is within another footnote
1324 ;; reference or footnote definition.
1325 ((loop for parent in (org-export-get-genealogy footnote-reference)
1326 thereis (memq (org-element-type parent)
1327 '(footnote-reference footnote-definition)))
1328 "\\footnotemark")
1329 ;; Otherwise, define it with \footnote command.
1331 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1332 (unless (eq (org-element-type def) 'org-data)
1333 (setq def (cons 'org-data (cons nil def))))
1334 (concat
1335 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1336 ;; Retrieve all footnote references within the footnote and
1337 ;; add their definition after it, since LaTeX doesn't support
1338 ;; them inside.
1339 (org-e-latex--delayed-footnotes-definitions def info)))))))
1342 ;;;; Headline
1344 (defun org-e-latex-headline (headline contents info)
1345 "Transcode an HEADLINE element from Org to LaTeX.
1346 CONTENTS holds the contents of the headline. INFO is a plist
1347 holding contextual information."
1348 (let* ((class (plist-get info :latex-class))
1349 (level (org-export-get-relative-level headline info))
1350 (numberedp (org-export-numbered-headline-p headline info))
1351 (class-sectionning (assoc class org-e-latex-classes))
1352 ;; Section formatting will set two placeholders: one for the
1353 ;; title and the other for the contents.
1354 (section-fmt
1355 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1356 (fboundp (nth 2 class-sectionning)))
1357 (funcall (nth 2 class-sectionning) level numberedp)
1358 (nth (1+ level) class-sectionning))))
1359 (cond
1360 ;; No section available for that LEVEL.
1361 ((not sec) nil)
1362 ;; Section format directly returned by a function.
1363 ((stringp sec) sec)
1364 ;; (numbered-section . unnumbered-section)
1365 ((not (consp (cdr sec)))
1366 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1367 ;; (numbered-open numbered-close)
1368 ((= (length sec) 2)
1369 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1370 ;; (num-in num-out no-num-in no-num-out)
1371 ((= (length sec) 4)
1372 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1373 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1374 (text (org-export-data (org-element-property :title headline) info))
1375 (todo
1376 (and (plist-get info :with-todo-keywords)
1377 (let ((todo (org-element-property :todo-keyword headline)))
1378 (and todo (org-export-data todo info)))))
1379 (todo-type (and todo (org-element-property :todo-type headline)))
1380 (tags (and (plist-get info :with-tags)
1381 (org-export-get-tags headline info)))
1382 (priority (and (plist-get info :with-priority)
1383 (org-element-property :priority headline)))
1384 ;; Create the headline text along with a no-tag version. The
1385 ;; latter is required to remove tags from table of contents.
1386 (full-text (if (functionp org-e-latex-format-headline-function)
1387 ;; User-defined formatting function.
1388 (funcall org-e-latex-format-headline-function
1389 todo todo-type priority text tags)
1390 ;; Default formatting.
1391 (concat
1392 (when todo
1393 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1394 (when priority (format "\\framebox{\\#%c} " priority))
1395 text
1396 (when tags
1397 (format "\\hfill{}\\textsc{:%s:}"
1398 (mapconcat 'identity tags ":"))))))
1399 (full-text-no-tag
1400 (if (functionp org-e-latex-format-headline-function)
1401 ;; User-defined formatting function.
1402 (funcall org-e-latex-format-headline-function
1403 todo todo-type priority text nil)
1404 ;; Default formatting.
1405 (concat
1406 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1407 (when priority (format "\\framebox{\\#%c} " priority))
1408 text)))
1409 ;; Associate some \label to the headline for internal links.
1410 (headline-label
1411 (format "\\label{sec-%s}\n"
1412 (mapconcat 'number-to-string
1413 (org-export-get-headline-number headline info)
1414 "-")))
1415 (pre-blanks
1416 (make-string (org-element-property :pre-blank headline) 10)))
1417 (cond
1418 ;; Case 1: This is a footnote section: ignore it.
1419 ((org-element-property :footnote-section-p headline) nil)
1420 ;; Case 2. This is a deep sub-tree: export it as a list item.
1421 ;; Also export as items headlines for which no section
1422 ;; format has been found.
1423 ((or (not section-fmt) (org-export-low-level-p headline info))
1424 ;; Build the real contents of the sub-tree.
1425 (let ((low-level-body
1426 (concat
1427 ;; If the headline is the first sibling, start a list.
1428 (when (org-export-first-sibling-p headline info)
1429 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1430 ;; Itemize headline
1431 "\\item " full-text "\n" headline-label pre-blanks contents)))
1432 ;; If headline is not the last sibling simply return
1433 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1434 ;; blank line.
1435 (if (not (org-export-last-sibling-p headline info)) low-level-body
1436 (replace-regexp-in-string
1437 "[ \t\n]*\\'"
1438 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1439 low-level-body))))
1440 ;; Case 3. Standard headline. Export it as a section.
1442 (cond
1443 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1444 ;; Regular section. Use specified format string.
1445 (format section-fmt full-text
1446 (concat headline-label pre-blanks contents)))
1447 ((string-match "\\`\\\\\\(.*?\\){" section-fmt)
1448 ;; If tags should be removed from table of contents, insert
1449 ;; title without tags as an alternative heading in sectioning
1450 ;; command.
1451 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1452 nil nil section-fmt 1)
1453 ;; Replace square brackets with parenthesis since
1454 ;; square brackets are not supported in optional
1455 ;; arguments.
1456 (replace-regexp-in-string
1457 "\\[" "("
1458 (replace-regexp-in-string
1459 "\\]" ")"
1460 full-text-no-tag))
1461 full-text
1462 (concat headline-label pre-blanks contents)))
1464 ;; Impossible to add an alternative heading. Fallback to
1465 ;; regular sectioning format string.
1466 (format section-fmt full-text
1467 (concat headline-label pre-blanks contents))))))))
1470 ;;;; Horizontal Rule
1472 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1473 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1474 CONTENTS is nil. INFO is a plist holding contextual information."
1475 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1476 (prev (org-export-get-previous-element horizontal-rule info)))
1477 (concat
1478 ;; Make sure the rule doesn't start at the end of the current
1479 ;; line by separating it with a blank line from previous element.
1480 (when (and prev
1481 (let ((prev-blank (org-element-property :post-blank prev)))
1482 (or (not prev-blank) (zerop prev-blank))))
1483 "\n")
1484 (org-e-latex--wrap-label
1485 horizontal-rule
1486 (format "\\rule{%s}{%s}"
1487 (or (plist-get attr :width) "\\linewidth")
1488 (or (plist-get attr :thickness) "0.5pt"))))))
1491 ;;;; Inline Babel Call
1493 ;; Inline Babel Calls are ignored.
1496 ;;;; Inline Src Block
1498 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1499 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1500 CONTENTS holds the contents of the item. INFO is a plist holding
1501 contextual information."
1502 (let* ((code (org-element-property :value inline-src-block))
1503 (separator (org-e-latex--find-verb-separator code)))
1504 (cond
1505 ;; Do not use a special package: transcode it verbatim.
1506 ((not org-e-latex-listings)
1507 (concat "\\verb" separator code separator))
1508 ;; Use minted package.
1509 ((eq org-e-latex-listings 'minted)
1510 (let* ((org-lang (org-element-property :language inline-src-block))
1511 (mint-lang (or (cadr (assq (intern org-lang)
1512 org-e-latex-minted-langs))
1513 org-lang))
1514 (options (org-e-latex--make-option-string
1515 org-e-latex-minted-options)))
1516 (concat (format "\\mint%s{%s}"
1517 (if (string= options "") "" (format "[%s]" options))
1518 mint-lang)
1519 separator code separator)))
1520 ;; Use listings package.
1522 ;; Maybe translate language's name.
1523 (let* ((org-lang (org-element-property :language inline-src-block))
1524 (lst-lang (or (cadr (assq (intern org-lang)
1525 org-e-latex-listings-langs))
1526 org-lang))
1527 (options (org-e-latex--make-option-string
1528 (append org-e-latex-listings-options
1529 `(("language" ,lst-lang))))))
1530 (concat (format "\\lstinline[%s]" options)
1531 separator code separator))))))
1534 ;;;; Inlinetask
1536 (defun org-e-latex-inlinetask (inlinetask contents info)
1537 "Transcode an INLINETASK element from Org to LaTeX.
1538 CONTENTS holds the contents of the block. INFO is a plist
1539 holding contextual information."
1540 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1541 (todo (and (plist-get info :with-todo-keywords)
1542 (let ((todo (org-element-property :todo-keyword inlinetask)))
1543 (and todo (org-export-data todo info)))))
1544 (todo-type (org-element-property :todo-type inlinetask))
1545 (tags (and (plist-get info :with-tags)
1546 (org-export-get-tags inlinetask info)))
1547 (priority (and (plist-get info :with-priority)
1548 (org-element-property :priority inlinetask))))
1549 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1550 ;; with appropriate arguments.
1551 (if (functionp org-e-latex-format-inlinetask-function)
1552 (funcall org-e-latex-format-inlinetask-function
1553 todo todo-type priority title tags contents)
1554 ;; Otherwise, use a default template.
1555 (org-e-latex--wrap-label
1556 inlinetask
1557 (let ((full-title
1558 (concat
1559 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1560 (when priority (format "\\framebox{\\#%c} " priority))
1561 title
1562 (when tags (format "\\hfill{}\\textsc{:%s:}"
1563 (mapconcat 'identity tags ":"))))))
1564 (format (concat "\\begin{center}\n"
1565 "\\fbox{\n"
1566 "\\begin{minipage}[c]{.6\\textwidth}\n"
1567 "%s\n\n"
1568 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1569 "%s"
1570 "\\end{minipage}\n"
1571 "}\n"
1572 "\\end{center}")
1573 full-title contents))))))
1576 ;;;; Italic
1578 (defun org-e-latex-italic (italic contents info)
1579 "Transcode ITALIC from Org to LaTeX.
1580 CONTENTS is the text with italic markup. INFO is a plist holding
1581 contextual information."
1582 (org-e-latex--text-markup contents 'italic))
1585 ;;;; Item
1587 (defun org-e-latex-item (item contents info)
1588 "Transcode an ITEM element from Org to LaTeX.
1589 CONTENTS holds the contents of the item. INFO is a plist holding
1590 contextual information."
1591 (let* ((counter
1592 (let ((count (org-element-property :counter item))
1593 (level
1594 (loop for parent in (org-export-get-genealogy item)
1595 count (eq (org-element-type parent) 'plain-list)
1596 until (eq (org-element-type parent) 'headline))))
1597 (and count
1598 (< level 5)
1599 (format "\\setcounter{enum%s}{%s}\n"
1600 (nth (1- level) '("i" "ii" "iii" "iv"))
1601 (1- count)))))
1602 (checkbox (case (org-element-property :checkbox item)
1603 (on "$\\boxtimes$ ")
1604 (off "$\\Box$ ")
1605 (trans "$\\boxminus$ ")))
1606 (tag (let ((tag (org-element-property :tag item)))
1607 ;; Check-boxes must belong to the tag.
1608 (and tag (format "[%s] "
1609 (concat checkbox
1610 (org-export-data tag info)))))))
1611 (concat counter "\\item" (or tag (concat " " checkbox))
1612 (and contents (org-trim contents))
1613 ;; If there are footnotes references in tag, be sure to
1614 ;; add their definition at the end of the item. This
1615 ;; workaround is necessary since "\footnote{}" command is
1616 ;; not supported in tags.
1617 (and tag
1618 (org-e-latex--delayed-footnotes-definitions
1619 (org-element-property :tag item) info)))))
1622 ;;;; Keyword
1624 (defun org-e-latex-keyword (keyword contents info)
1625 "Transcode a KEYWORD element from Org to LaTeX.
1626 CONTENTS is nil. INFO is a plist holding contextual information."
1627 (let ((key (org-element-property :key keyword))
1628 (value (org-element-property :value keyword)))
1629 (cond
1630 ((string= key "LATEX") value)
1631 ((string= key "INDEX") (format "\\index{%s}" value))
1632 ;; Invisible targets.
1633 ((string= key "TARGET") nil)
1634 ((string= key "TOC")
1635 (let ((value (downcase value)))
1636 (cond
1637 ((string-match "\\<headlines\\>" value)
1638 (let ((depth (or (and (string-match "[0-9]+" value)
1639 (string-to-number (match-string 0 value)))
1640 (plist-get info :with-toc))))
1641 (concat
1642 (when (wholenump depth)
1643 (format "\\setcounter{tocdepth}{%s}\n" depth))
1644 "\\tableofcontents")))
1645 ((string= "tables" value) "\\listoftables")
1646 ((string= "figures" value) "\\listoffigures")
1647 ((string= "listings" value)
1648 (cond
1649 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1650 (org-e-latex-listings "\\lstlistoflistings")
1651 ;; At the moment, src blocks with a caption are wrapped
1652 ;; into a figure environment.
1653 (t "\\listoffigures")))))))))
1656 ;;;; Latex Environment
1658 (defun org-e-latex-latex-environment (latex-environment contents info)
1659 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1660 CONTENTS is nil. INFO is a plist holding contextual information."
1661 (let ((label (org-element-property :name latex-environment))
1662 (value (org-remove-indentation
1663 (org-element-property :value latex-environment))))
1664 (if (not (org-string-nw-p label)) value
1665 ;; Environment is labelled: label must be within the environment
1666 ;; (otherwise, a reference pointing to that element will count
1667 ;; the section instead).
1668 (with-temp-buffer
1669 (insert value)
1670 (goto-char (point-min))
1671 (forward-line)
1672 (insert (format "\\label{%s}\n" label))
1673 (buffer-string)))))
1676 ;;;; Latex Fragment
1678 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1679 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1680 CONTENTS is nil. INFO is a plist holding contextual information."
1681 (org-element-property :value latex-fragment))
1684 ;;;; Line Break
1686 (defun org-e-latex-line-break (line-break contents info)
1687 "Transcode a LINE-BREAK object from Org to LaTeX.
1688 CONTENTS is nil. INFO is a plist holding contextual information."
1689 "\\\\")
1692 ;;;; Link
1694 (defun org-e-latex-link--inline-image (link info)
1695 "Return LaTeX code for an inline image.
1696 LINK is the link pointing to the inline image. INFO is a plist
1697 used as a communication channel."
1698 (let* ((parent (org-export-get-parent-element link))
1699 (path (let ((raw-path (org-element-property :path link)))
1700 (if (not (file-name-absolute-p raw-path)) raw-path
1701 (expand-file-name raw-path))))
1702 (caption (org-e-latex--caption/label-string parent info))
1703 ;; Retrieve latex attributes from the element around.
1704 (attr (let ((raw-attr
1705 (mapconcat #'identity
1706 (org-element-property :attr_latex parent)
1707 " ")))
1708 (unless (string= raw-attr "") raw-attr)))
1709 (disposition
1710 (cond
1711 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1712 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1713 ((or (and attr (string-match "\\<float\\>" attr))
1714 (not (string= caption "")))
1715 'float)))
1716 (placement
1717 (cond
1718 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1719 (org-match-string-no-properties 1 attr))
1720 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1721 ((eq disposition 'float)
1722 (concat "[" org-e-latex-default-figure-position "]"))
1723 (t ""))))
1724 ;; Now clear ATTR from any special keyword and set a default
1725 ;; value if nothing is left.
1726 (setq attr
1727 (if (not attr) ""
1728 (org-trim
1729 (replace-regexp-in-string
1730 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1731 (setq attr (cond ((not (string= attr "")) attr)
1732 ((eq disposition 'float) "width=0.7\\textwidth")
1733 ((eq disposition 'wrap) "width=0.48\\textwidth")
1734 (t (or org-e-latex-image-default-option ""))))
1735 ;; Return proper string, depending on DISPOSITION.
1736 (case disposition
1737 (wrap (format "\\begin{wrapfigure}%s
1738 \\centering
1739 \\includegraphics[%s]{%s}
1740 %s\\end{wrapfigure}" placement attr path caption))
1741 (multicolumn (format "\\begin{figure*}%s
1742 \\centering
1743 \\includegraphics[%s]{%s}
1744 %s\\end{figure*}" placement attr path caption))
1745 (float (format "\\begin{figure}%s
1746 \\centering
1747 \\includegraphics[%s]{%s}
1748 %s\\end{figure}" placement attr path caption))
1749 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1751 (defun org-e-latex-link (link desc info)
1752 "Transcode a LINK object from Org to LaTeX.
1754 DESC is the description part of the link, or the empty string.
1755 INFO is a plist holding contextual information. See
1756 `org-export-data'."
1757 (let* ((type (org-element-property :type link))
1758 (raw-path (org-element-property :path link))
1759 ;; Ensure DESC really exists, or set it to nil.
1760 (desc (and (not (string= desc "")) desc))
1761 (imagep (org-export-inline-image-p
1762 link org-e-latex-inline-image-rules))
1763 (path (cond
1764 ((member type '("http" "https" "ftp" "mailto"))
1765 (concat type ":" raw-path))
1766 ((string= type "file")
1767 (if (file-name-absolute-p raw-path)
1768 (concat "file://" (expand-file-name raw-path))
1769 (concat "file://" raw-path)))
1770 (t raw-path)))
1771 protocol)
1772 (cond
1773 ;; Image file.
1774 (imagep (org-e-latex-link--inline-image link info))
1775 ;; Radio link: Transcode target's contents and use them as link's
1776 ;; description.
1777 ((string= type "radio")
1778 (let ((destination (org-export-resolve-radio-link link info)))
1779 (when destination
1780 (format "\\hyperref[%s]{%s}"
1781 (org-export-solidify-link-text path)
1782 (org-export-data (org-element-contents destination) info)))))
1783 ;; Links pointing to an headline: Find destination and build
1784 ;; appropriate referencing command.
1785 ((member type '("custom-id" "fuzzy" "id"))
1786 (let ((destination (if (string= type "fuzzy")
1787 (org-export-resolve-fuzzy-link link info)
1788 (org-export-resolve-id-link link info))))
1789 (case (org-element-type destination)
1790 ;; Id link points to an external file.
1791 (plain-text
1792 (if desc (format "\\href{file://%s}{%s}" destination desc)
1793 (format "\\url{file://%s}" destination)))
1794 ;; Fuzzy link points nowhere.
1795 ('nil
1796 (format org-e-latex-link-with-unknown-path-format
1797 (or desc
1798 (org-export-data
1799 (org-element-property :raw-link link) info))))
1800 ;; Fuzzy link points to an invisible target.
1801 (keyword nil)
1802 ;; LINK points to an headline. If headlines are numbered
1803 ;; and the link has no description, display headline's
1804 ;; number. Otherwise, display description or headline's
1805 ;; title.
1806 (headline
1807 (let ((label
1808 (format "sec-%s"
1809 (mapconcat
1810 'number-to-string
1811 (org-export-get-headline-number destination info)
1812 "-"))))
1813 (if (and (plist-get info :section-numbers) (not desc))
1814 (format "\\ref{%s}" label)
1815 (format "\\hyperref[%s]{%s}" label
1816 (or desc
1817 (org-export-data
1818 (org-element-property :title destination) info))))))
1819 ;; Fuzzy link points to a target. Do as above.
1820 (otherwise
1821 (let ((path (org-export-solidify-link-text path)))
1822 (if (not desc) (format "\\ref{%s}" path)
1823 (format "\\hyperref[%s]{%s}" path desc)))))))
1824 ;; Coderef: replace link with the reference name or the
1825 ;; equivalent line number.
1826 ((string= type "coderef")
1827 (format (org-export-get-coderef-format path desc)
1828 (org-export-resolve-coderef path info)))
1829 ;; Link type is handled by a special function.
1830 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1831 (funcall protocol (org-link-unescape path) desc 'latex))
1832 ;; External link with a description part.
1833 ((and path desc) (format "\\href{%s}{%s}" path desc))
1834 ;; External link without a description part.
1835 (path (format "\\url{%s}" path))
1836 ;; No path, only description. Try to do something useful.
1837 (t (format org-e-latex-link-with-unknown-path-format desc)))))
1840 ;;;; Paragraph
1842 (defun org-e-latex-paragraph (paragraph contents info)
1843 "Transcode a PARAGRAPH element from Org to LaTeX.
1844 CONTENTS is the contents of the paragraph, as a string. INFO is
1845 the plist used as a communication channel."
1846 contents)
1849 ;;;; Plain List
1851 (defun org-e-latex-plain-list (plain-list contents info)
1852 "Transcode a PLAIN-LIST element from Org to LaTeX.
1853 CONTENTS is the contents of the list. INFO is a plist holding
1854 contextual information."
1855 (let* ((type (org-element-property :type plain-list))
1856 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1857 "inparadesc" "asparadesc"))
1858 (paralist-regexp (concat
1859 "\\("
1860 (mapconcat 'identity paralist-types "\\|")
1861 "\\)"))
1862 (attr (mapconcat #'identity
1863 (org-element-property :attr_latex plain-list)
1864 " "))
1865 (latex-type (cond
1866 ((and attr
1867 (string-match
1868 (format "\\<%s\\>" paralist-regexp) attr))
1869 (match-string 1 attr))
1870 ((eq type 'ordered) "enumerate")
1871 ((eq type 'unordered) "itemize")
1872 ((eq type 'descriptive) "description"))))
1873 (org-e-latex--wrap-label
1874 plain-list
1875 (format "\\begin{%s}%s\n%s\\end{%s}"
1876 latex-type
1877 ;; Once special environment, if any, has been removed, the
1878 ;; rest of the attributes will be optional arguments.
1879 ;; They will be put inside square brackets if necessary.
1880 (let ((opt (replace-regexp-in-string
1881 (format " *%s *" paralist-regexp) "" attr)))
1882 (cond ((string= opt "") "")
1883 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1884 (t (format "[%s]" opt))))
1885 contents
1886 latex-type))))
1889 ;;;; Plain Text
1891 (defun org-e-latex-plain-text (text info)
1892 "Transcode a TEXT string from Org to LaTeX.
1893 TEXT is the string to transcode. INFO is a plist holding
1894 contextual information."
1895 (let ((specialp (plist-get info :with-special-strings)))
1896 ;; Protect %, #, &, $, ~, ^, _, { and }.
1897 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1898 (setq text
1899 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1900 ;; Protect \. If special strings are used, be careful not to
1901 ;; protect "\" in "\-" constructs.
1902 (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
1903 (setq text
1904 (replace-regexp-in-string
1905 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
1906 "$\\backslash$" text nil t 1)))
1907 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1908 (let ((case-fold-search nil)
1909 (start 0))
1910 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1911 (setq text (replace-match
1912 (format "\\%s{}" (match-string 1 text)) nil t text)
1913 start (match-end 0))))
1914 ;; Handle quotation marks.
1915 (setq text (org-e-latex--quotation-marks text info))
1916 ;; Convert special strings.
1917 (when specialp
1918 (setq text (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" text nil t)))
1919 ;; Handle break preservation if required.
1920 (when (plist-get info :preserve-breaks)
1921 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1922 text)))
1923 ;; Return value.
1924 text))
1927 ;;;; Planning
1929 (defun org-e-latex-planning (planning contents info)
1930 "Transcode a PLANNING element from Org to LaTeX.
1931 CONTENTS is nil. INFO is a plist holding contextual
1932 information."
1933 (concat
1934 "\\noindent"
1935 (mapconcat
1936 'identity
1937 (delq nil
1938 (list
1939 (let ((closed (org-element-property :closed planning)))
1940 (when closed
1941 (concat
1942 (format "\\textbf{%s} " org-closed-string)
1943 (format org-e-latex-inactive-timestamp-format
1944 (org-translate-time closed)))))
1945 (let ((deadline (org-element-property :deadline planning)))
1946 (when deadline
1947 (concat
1948 (format "\\textbf{%s} " org-deadline-string)
1949 (format org-e-latex-active-timestamp-format
1950 (org-translate-time deadline)))))
1951 (let ((scheduled (org-element-property :scheduled planning)))
1952 (when scheduled
1953 (concat
1954 (format "\\textbf{%s} " org-scheduled-string)
1955 (format org-e-latex-active-timestamp-format
1956 (org-translate-time scheduled)))))))
1957 " ")
1958 "\\\\"))
1961 ;;;; Property Drawer
1963 (defun org-e-latex-property-drawer (property-drawer contents info)
1964 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1965 CONTENTS is nil. INFO is a plist holding contextual
1966 information."
1967 ;; The property drawer isn't exported but we want separating blank
1968 ;; lines nonetheless.
1972 ;;;; Quote Block
1974 (defun org-e-latex-quote-block (quote-block contents info)
1975 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1976 CONTENTS holds the contents of the block. INFO is a plist
1977 holding contextual information."
1978 (org-e-latex--wrap-label
1979 quote-block
1980 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1983 ;;;; Quote Section
1985 (defun org-e-latex-quote-section (quote-section contents info)
1986 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1987 CONTENTS is nil. INFO is a plist holding contextual information."
1988 (let ((value (org-remove-indentation
1989 (org-element-property :value quote-section))))
1990 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1993 ;;;; Radio Target
1995 (defun org-e-latex-radio-target (radio-target text info)
1996 "Transcode a RADIO-TARGET object from Org to LaTeX.
1997 TEXT is the text of the target. INFO is a plist holding
1998 contextual information."
1999 (format "\\label{%s}%s"
2000 (org-export-solidify-link-text
2001 (org-element-property :value radio-target))
2002 text))
2005 ;;;; Section
2007 (defun org-e-latex-section (section contents info)
2008 "Transcode a SECTION element from Org to LaTeX.
2009 CONTENTS holds the contents of the section. INFO is a plist
2010 holding contextual information."
2011 contents)
2014 ;;;; Special Block
2016 (defun org-e-latex-special-block (special-block contents info)
2017 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2018 CONTENTS holds the contents of the block. INFO is a plist
2019 holding contextual information."
2020 (let ((type (downcase (org-element-property :type special-block))))
2021 (org-e-latex--wrap-label
2022 special-block
2023 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
2026 ;;;; Src Block
2028 (defun org-e-latex-src-block (src-block contents info)
2029 "Transcode a SRC-BLOCK element from Org to LaTeX.
2030 CONTENTS holds the contents of the item. INFO is a plist holding
2031 contextual information."
2032 (let* ((lang (org-element-property :language src-block))
2033 (caption (org-element-property :caption src-block))
2034 (label (org-element-property :name src-block))
2035 (custom-env (and lang
2036 (cadr (assq (intern lang)
2037 org-e-latex-custom-lang-environments))))
2038 (num-start (case (org-element-property :number-lines src-block)
2039 (continued (org-export-get-loc src-block info))
2040 (new 0)))
2041 (retain-labels (org-element-property :retain-labels src-block)))
2042 (cond
2043 ;; Case 1. No source fontification.
2044 ((not org-e-latex-listings)
2045 (let ((caption-str (org-e-latex--caption/label-string src-block info))
2046 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
2047 (format
2048 (or float-env "%s")
2049 (concat caption-str
2050 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2051 (org-export-format-code-default src-block info))))))
2052 ;; Case 2. Custom environment.
2053 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2054 custom-env
2055 (org-export-format-code-default src-block info)
2056 custom-env))
2057 ;; Case 3. Use minted package.
2058 ((eq org-e-latex-listings 'minted)
2059 (let ((float-env
2060 (when (or label caption)
2061 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2062 (org-e-latex--caption/label-string src-block info))))
2063 (body
2064 (format
2065 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2066 ;; Options.
2067 (org-e-latex--make-option-string
2068 (if (not num-start) org-e-latex-minted-options
2069 (append `(("linenos")
2070 ("firstnumber" ,(number-to-string (1+ num-start))))
2071 org-e-latex-minted-options)))
2072 ;; Language.
2073 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
2074 ;; Source code.
2075 (let* ((code-info (org-export-unravel-code src-block))
2076 (max-width
2077 (apply 'max
2078 (mapcar 'length
2079 (org-split-string (car code-info) "\n")))))
2080 (org-export-format-code
2081 (car code-info)
2082 (lambda (loc num ref)
2083 (concat
2085 (when ref
2086 ;; Ensure references are flushed to the right,
2087 ;; separated with 6 spaces from the widest line
2088 ;; of code.
2089 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2090 (format "(%s)" ref)))))
2091 nil (and retain-labels (cdr code-info)))))))
2092 ;; Return value.
2093 (if float-env (format float-env body) body)))
2094 ;; Case 4. Use listings package.
2096 (let ((lst-lang
2097 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
2098 (caption-str
2099 (when caption
2100 (let ((main (org-export-get-caption src-block))
2101 (secondary (org-export-get-caption src-block t)))
2102 (if (not secondary) (format "{%s}" (org-export-data main info))
2103 (format "{[%s]%s}"
2104 (org-export-data secondary info)
2105 (org-export-data main info)))))))
2106 (concat
2107 ;; Options.
2108 (format "\\lstset{%s}\n"
2109 (org-e-latex--make-option-string
2110 (append org-e-latex-listings-options
2111 `(("language" ,lst-lang))
2112 (when label `(("label" ,label)))
2113 (when caption-str `(("caption" ,caption-str)))
2114 (cond ((not num-start) '(("numbers" "none")))
2115 ((zerop num-start) '(("numbers" "left")))
2116 (t `(("numbers" "left")
2117 ("firstnumber"
2118 ,(number-to-string (1+ num-start)))))))))
2119 ;; Source code.
2120 (format
2121 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2122 (let* ((code-info (org-export-unravel-code src-block))
2123 (max-width
2124 (apply 'max
2125 (mapcar 'length
2126 (org-split-string (car code-info) "\n")))))
2127 (org-export-format-code
2128 (car code-info)
2129 (lambda (loc num ref)
2130 (concat
2132 (when ref
2133 ;; Ensure references are flushed to the right,
2134 ;; separated with 6 spaces from the widest line of
2135 ;; code
2136 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2137 (format "(%s)" ref)))))
2138 nil (and retain-labels (cdr code-info)))))))))))
2141 ;;;; Statistics Cookie
2143 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
2144 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2145 CONTENTS is nil. INFO is a plist holding contextual information."
2146 (replace-regexp-in-string
2147 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2150 ;;;; Strike-Through
2152 (defun org-e-latex-strike-through (strike-through contents info)
2153 "Transcode STRIKE-THROUGH from Org to LaTeX.
2154 CONTENTS is the text with strike-through markup. INFO is a plist
2155 holding contextual information."
2156 (org-e-latex--text-markup contents 'strike-through))
2159 ;;;; Subscript
2161 (defun org-e-latex-subscript (subscript contents info)
2162 "Transcode a SUBSCRIPT object from Org to LaTeX.
2163 CONTENTS is the contents of the object. INFO is a plist holding
2164 contextual information."
2165 (if (= (length contents) 1) (format "$_%s$" contents)
2166 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2167 ;; command for each of them.
2168 (let ((prev-blanks 0))
2169 (mapconcat
2170 (lambda (obj)
2171 (case (org-element-type obj)
2172 ((entity latex-fragment)
2173 (setq prev-blanks (org-element-property :post-blank obj))
2174 (let ((data (org-trim (org-export-data obj info))))
2175 (string-match
2176 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2177 data)
2178 (format "$_{%s}$" (match-string 1 data))))
2179 (plain-text
2180 (format "$_\\mathrm{%s}$"
2181 (concat (make-string prev-blanks ? )
2182 ;; mathrm command doesn't handle spaces,
2183 ;; so we have to enforce them.
2184 (replace-regexp-in-string
2185 " " "\\\\ " (org-export-data obj info)))))
2186 (otherwise
2187 (setq prev-blanks (org-element-property :post-blank obj))
2188 (format "$_{%s}$" (org-export-data obj info)))))
2189 (org-element-contents subscript) ""))))
2192 ;;;; Superscript
2194 (defun org-e-latex-superscript (superscript contents info)
2195 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2196 CONTENTS is the contents of the object. INFO is a plist holding
2197 contextual information."
2198 (if (= (length contents) 1) (format "$^%s$" contents)
2199 ;; Handle multiple objects in SUPERSCRIPT by creating
2200 ;; a superscript command for each of them.
2201 (let ((prev-blanks 0))
2202 (mapconcat
2203 (lambda (obj)
2204 (case (org-element-type obj)
2205 ((entity latex-fragment)
2206 (setq prev-blanks (org-element-property :post-blank obj))
2207 (let ((data (org-trim (org-export-data obj info))))
2208 (string-match
2209 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2210 data)
2211 (format "$^{%s}$" (match-string 1 data))))
2212 (plain-text
2213 (format "$^\\mathrm{%s}$"
2214 (concat (make-string prev-blanks ? )
2215 ;; mathrm command doesn't handle spaces,
2216 ;; so we have to enforce them.
2217 (replace-regexp-in-string
2218 " " "\\\\ " (org-export-data obj info)))))
2219 (otherwise
2220 (setq prev-blanks (org-element-property :post-blank obj))
2221 (format "$^{%s}$" (org-export-data obj info)))))
2222 (org-element-contents superscript) ""))))
2225 ;;;; Table
2227 ;; `org-e-latex-table' is the entry point for table transcoding. It
2228 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
2229 ;; delegates the job to either `org-e-latex-table--table.el-table' or
2230 ;; `org-e-latex-table--org-table' functions, depending of the type of
2231 ;; the table.
2233 ;; `org-e-latex-table--align-string' is a subroutine used to build
2234 ;; alignment string for Org tables.
2236 (defun org-e-latex-table (table contents info)
2237 "Transcode a TABLE element from Org to LaTeX.
2238 CONTENTS is the contents of the table. INFO is a plist holding
2239 contextual information."
2240 (cond
2241 ;; Case 1: verbatim table.
2242 ((or org-e-latex-tables-verbatim
2243 (let ((attr (mapconcat 'identity
2244 (org-element-property :attr_latex table)
2245 " ")))
2246 (and attr (string-match "\\<verbatim\\>" attr))))
2247 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2248 ;; Re-create table, without affiliated keywords.
2249 (org-trim
2250 (org-element-interpret-data
2251 `(table nil ,@(org-element-contents table))))))
2252 ;; Case 2: table.el table. Convert it using appropriate tools.
2253 ((eq (org-element-property :type table) 'table.el)
2254 (org-e-latex-table--table.el-table table contents info))
2255 ;; Case 3: Standard table.
2256 (t (org-e-latex-table--org-table table contents info))))
2258 (defun org-e-latex-table--align-string (table info)
2259 "Return an appropriate LaTeX alignment string.
2260 TABLE is the considered table. INFO is a plist used as
2261 a communication channel."
2262 (let ((attr (mapconcat 'identity
2263 (org-element-property :attr_latex table)
2264 " ")))
2265 (if (string-match "\\<align=\\(\\S-+\\)" attr) (match-string 1 attr)
2266 (let (alignment)
2267 ;; Extract column groups and alignment from first (non-rule)
2268 ;; row.
2269 (org-element-map
2270 (org-element-map
2271 table 'table-row
2272 (lambda (row)
2273 (and (eq (org-element-property :type row) 'standard) row))
2274 info 'first-match)
2275 'table-cell
2276 (lambda (cell)
2277 (let ((borders (org-export-table-cell-borders cell info)))
2278 ;; Check left border for the first cell only.
2279 (when (and (memq 'left borders) (not alignment))
2280 (push "|" alignment))
2281 (push (case (org-export-table-cell-alignment cell info)
2282 (left "l")
2283 (right "r")
2284 (center "c"))
2285 alignment)
2286 (when (memq 'right borders) (push "|" alignment))))
2287 info)
2288 (apply 'concat (reverse alignment))))))
2290 (defun org-e-latex-table--org-table (table contents info)
2291 "Return appropriate LaTeX code for an Org table.
2293 TABLE is the table type element to transcode. CONTENTS is its
2294 contents, as a string. INFO is a plist used as a communication
2295 channel.
2297 This function assumes TABLE has `org' as its `:type' attribute."
2298 (let* ((label (org-element-property :name table))
2299 (caption (org-e-latex--caption/label-string table info))
2300 (attr (mapconcat 'identity
2301 (org-element-property :attr_latex table)
2302 " "))
2303 ;; Determine alignment string.
2304 (alignment (org-e-latex-table--align-string table info))
2305 ;; Determine environment for the table: longtable, tabular...
2306 (table-env (cond
2307 ((not attr) org-e-latex-default-table-environment)
2308 ((string-match "\\<longtable\\>" attr) "longtable")
2309 ((string-match "\\<tabular.?\\>" attr)
2310 (org-match-string-no-properties 0 attr))
2311 (t org-e-latex-default-table-environment)))
2312 ;; If table is a float, determine environment: table, table*
2313 ;; or sidewaystable.
2314 (float-env (cond
2315 ((string= "longtable" table-env) nil)
2316 ((and attr (string-match "\\<sidewaystable\\>" attr))
2317 "sidewaystable")
2318 ((and attr
2319 (or (string-match (regexp-quote "table*") attr)
2320 (string-match "\\<multicolumn\\>" attr)))
2321 "table*")
2322 ((or (not (string= caption "")) label) "table")))
2323 ;; Extract others display options.
2324 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
2325 (org-match-string-no-properties 1 attr)))
2326 (placement
2327 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
2328 (org-match-string-no-properties 1 attr)
2329 (format "[%s]" org-e-latex-default-figure-position))))
2330 ;; Prepare the final format string for the table.
2331 (cond
2332 ;; Longtable.
2333 ((string= "longtable" table-env)
2334 (format
2335 "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
2336 alignment
2337 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
2338 (concat (org-trim caption) "\\\\\n"))
2339 contents
2340 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
2341 (concat (org-trim caption) "\\\\\n"))))
2342 ;; Others.
2343 (t (concat (when float-env
2344 (concat
2345 (format "\\begin{%s}%s\n" float-env placement)
2346 (if org-e-latex-table-caption-above caption "")))
2347 (when org-e-latex-tables-centered "\\begin{center}\n")
2348 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2349 table-env
2350 (if width (format "{%s}" width) "")
2351 alignment
2352 contents
2353 table-env)
2354 (when org-e-latex-tables-centered "\n\\end{center}")
2355 (when float-env
2356 (concat (if org-e-latex-table-caption-above "" caption)
2357 (format "\n\\end{%s}" float-env))))))))
2359 (defun org-e-latex-table--table.el-table (table contents info)
2360 "Return appropriate LaTeX code for a table.el table.
2362 TABLE is the table type element to transcode. CONTENTS is its
2363 contents, as a string. INFO is a plist used as a communication
2364 channel.
2366 This function assumes TABLE has `table.el' as its `:type'
2367 attribute."
2368 (require 'table)
2369 ;; Ensure "*org-export-table*" buffer is empty.
2370 (with-current-buffer (get-buffer-create "*org-export-table*")
2371 (erase-buffer))
2372 (let ((output (with-temp-buffer
2373 (insert (org-element-property :value table))
2374 (goto-char 1)
2375 (re-search-forward "^[ \t]*|[^|]" nil t)
2376 (table-generate-source 'latex "*org-export-table*")
2377 (with-current-buffer "*org-export-table*"
2378 (org-trim (buffer-string))))))
2379 (kill-buffer (get-buffer "*org-export-table*"))
2380 ;; Remove left out comments.
2381 (while (string-match "^%.*\n" output)
2382 (setq output (replace-match "" t t output)))
2383 ;; When the "rmlines" attribute is provided, remove all hlines but
2384 ;; the the one separating heading from the table body.
2385 (let ((attr (mapconcat 'identity
2386 (org-element-property :attr_latex table)
2387 " ")))
2388 (when (and attr (string-match "\\<rmlines\\>" attr))
2389 (let ((n 0) (pos 0))
2390 (while (and (< (length output) pos)
2391 (setq pos (string-match "^\\\\hline\n?" output pos)))
2392 (incf n)
2393 (unless (= n 2)
2394 (setq output (replace-match "" nil nil output)))))))
2395 (if (not org-e-latex-tables-centered) output
2396 (format "\\begin{center}\n%s\n\\end{center}" output))))
2399 ;;;; Table Cell
2401 (defun org-e-latex-table-cell (table-cell contents info)
2402 "Transcode a TABLE-CELL element from Org to LaTeX.
2403 CONTENTS is the cell contents. INFO is a plist used as
2404 a communication channel."
2405 (concat (if (and contents
2406 org-e-latex-table-scientific-notation
2407 (string-match orgtbl-exp-regexp contents))
2408 ;; Use appropriate format string for scientific
2409 ;; notation.
2410 (format org-e-latex-table-scientific-notation
2411 (match-string 1 contents)
2412 (match-string 2 contents))
2413 contents)
2414 (when (org-export-get-next-element table-cell info) " & ")))
2417 ;;;; Table Row
2419 (defun org-e-latex-table-row (table-row contents info)
2420 "Transcode a TABLE-ROW element from Org to LaTeX.
2421 CONTENTS is the contents of the row. INFO is a plist used as
2422 a communication channel."
2423 ;; Rules are ignored since table separators are deduced from
2424 ;; borders of the current row.
2425 (when (eq (org-element-property :type table-row) 'standard)
2426 (let* ((attr (mapconcat 'identity
2427 (org-element-property
2428 :attr_latex (org-export-get-parent table-row))
2429 " "))
2430 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
2431 (booktabsp
2432 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
2433 org-e-latex-tables-booktabs))
2434 ;; TABLE-ROW's borders are extracted from its first cell.
2435 (borders
2436 (org-export-table-cell-borders
2437 (car (org-element-contents table-row)) info)))
2438 (concat
2439 ;; When BOOKTABS are activated enforce top-rule even when no
2440 ;; hline was specifically marked.
2441 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2442 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2443 contents "\\\\\n"
2444 (cond
2445 ;; Special case for long tables. Define header and footers.
2446 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2447 (format "%s
2448 \\endhead
2449 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2450 \\endfoot
2451 \\endlastfoot"
2452 (if booktabsp "\\midrule" "\\hline")
2453 (if booktabsp "\\midrule" "\\hline")
2454 ;; Number of columns.
2455 (cdr (org-export-table-dimensions
2456 (org-export-get-parent-table table-row) info))))
2457 ;; When BOOKTABS are activated enforce bottom rule even when
2458 ;; no hline was specifically marked.
2459 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2460 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2461 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2464 ;;;; Target
2466 (defun org-e-latex-target (target contents info)
2467 "Transcode a TARGET object from Org to LaTeX.
2468 CONTENTS is nil. INFO is a plist holding contextual
2469 information."
2470 (format "\\label{%s}"
2471 (org-export-solidify-link-text (org-element-property :value target))))
2474 ;;;; Timestamp
2476 (defun org-e-latex-timestamp (timestamp contents info)
2477 "Transcode a TIMESTAMP object from Org to LaTeX.
2478 CONTENTS is nil. INFO is a plist holding contextual
2479 information."
2480 (let ((value (org-translate-time (org-element-property :value timestamp)))
2481 (range-end (org-element-property :range-end timestamp)))
2482 (case (org-element-property :type timestamp)
2483 (active (format org-e-latex-active-timestamp-format value))
2484 (active-range
2485 (concat (format org-e-latex-active-timestamp-format value)
2486 "--"
2487 (format org-e-latex-active-timestamp-format
2488 (org-translate-time range-end))))
2489 (inactive (format org-e-latex-inactive-timestamp-format value))
2490 (inactive-range
2491 (concat (format org-e-latex-inactive-timestamp-format value)
2492 "--"
2493 (format org-e-latex-inactive-timestamp-format
2494 (org-translate-time range-end))))
2495 (otherwise (format org-e-latex-diary-timestamp-format value)))))
2498 ;;;; Underline
2500 (defun org-e-latex-underline (underline contents info)
2501 "Transcode UNDERLINE from Org to LaTeX.
2502 CONTENTS is the text with underline markup. INFO is a plist
2503 holding contextual information."
2504 (org-e-latex--text-markup contents 'underline))
2507 ;;;; Verbatim
2509 (defun org-e-latex-verbatim (verbatim contents info)
2510 "Transcode a VERBATIM object from Org to LaTeX.
2511 CONTENTS is nil. INFO is a plist used as a communication
2512 channel."
2513 (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2516 ;;;; Verse Block
2518 (defun org-e-latex-verse-block (verse-block contents info)
2519 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2520 CONTENTS is verse block contents. INFO is a plist holding
2521 contextual information."
2522 (org-e-latex--wrap-label
2523 verse-block
2524 ;; In a verse environment, add a line break to each newline
2525 ;; character and change each white space at beginning of a line
2526 ;; into a space of 1 em. Also change each blank line with
2527 ;; a vertical space of 1 em.
2528 (progn
2529 (setq contents (replace-regexp-in-string
2530 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2531 (replace-regexp-in-string
2532 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2533 (while (string-match "^[ \t]+" contents)
2534 (let ((new-str (format "\\hspace*{%dem}"
2535 (length (match-string 0 contents)))))
2536 (setq contents (replace-match new-str nil t contents))))
2537 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2541 ;;; Interactive functions
2543 ;;;###autoload
2544 (defun org-e-latex-export-as-latex
2545 (&optional subtreep visible-only body-only ext-plist)
2546 "Export current buffer as a LaTeX buffer.
2548 If narrowing is active in the current buffer, only export its
2549 narrowed part.
2551 If a region is active, export that region.
2553 When optional argument SUBTREEP is non-nil, export the sub-tree
2554 at point, extracting information from the headline properties
2555 first.
2557 When optional argument VISIBLE-ONLY is non-nil, don't export
2558 contents of hidden elements.
2560 When optional argument BODY-ONLY is non-nil, only write code
2561 between \"\\begin{document}\" and \"\\end{document}\".
2563 EXT-PLIST, when provided, is a property list with external
2564 parameters overriding Org default settings, but still inferior to
2565 file-local settings.
2567 Export is done in a buffer named \"*Org E-LATEX Export*\", which
2568 will be displayed when `org-export-show-temporary-export-buffer'
2569 is non-nil."
2570 (interactive)
2571 (let ((outbuf (org-export-to-buffer
2572 'e-latex "*Org E-LATEX Export*"
2573 subtreep visible-only body-only ext-plist)))
2574 (with-current-buffer outbuf (LaTeX-mode))
2575 (when org-export-show-temporary-export-buffer
2576 (switch-to-buffer-other-window outbuf))))
2578 ;;;###autoload
2579 (defun org-e-latex-export-to-latex
2580 (&optional subtreep visible-only body-only ext-plist pub-dir)
2581 "Export current buffer to a LaTeX file.
2583 If narrowing is active in the current buffer, only export its
2584 narrowed part.
2586 If a region is active, export that region.
2588 When optional argument SUBTREEP is non-nil, export the sub-tree
2589 at point, extracting information from the headline properties
2590 first.
2592 When optional argument VISIBLE-ONLY is non-nil, don't export
2593 contents of hidden elements.
2595 When optional argument BODY-ONLY is non-nil, only write code
2596 between \"\\begin{document}\" and \"\\end{document}\".
2598 EXT-PLIST, when provided, is a property list with external
2599 parameters overriding Org default settings, but still inferior to
2600 file-local settings.
2602 When optional argument PUB-DIR is set, use it as the publishing
2603 directory.
2605 Return output file's name."
2606 (interactive)
2607 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2608 (org-export-to-file
2609 'e-latex outfile subtreep visible-only body-only ext-plist)))
2611 ;;;###autoload
2612 (defun org-e-latex-export-to-pdf
2613 (&optional subtreep visible-only body-only ext-plist pub-dir)
2614 "Export current buffer to LaTeX then process through to PDF.
2616 If narrowing is active in the current buffer, only export its
2617 narrowed part.
2619 If a region is active, export that region.
2621 When optional argument SUBTREEP is non-nil, export the sub-tree
2622 at point, extracting information from the headline properties
2623 first.
2625 When optional argument VISIBLE-ONLY is non-nil, don't export
2626 contents of hidden elements.
2628 When optional argument BODY-ONLY is non-nil, only write code
2629 between \"\\begin{document}\" and \"\\end{document}\".
2631 EXT-PLIST, when provided, is a property list with external
2632 parameters overriding Org default settings, but still inferior to
2633 file-local settings.
2635 When optional argument PUB-DIR is set, use it as the publishing
2636 directory.
2638 Return PDF file's name."
2639 (interactive)
2640 (org-e-latex-compile
2641 (org-e-latex-export-to-latex
2642 subtreep visible-only body-only ext-plist pub-dir)))
2644 (defun org-e-latex-compile (texfile)
2645 "Compile a TeX file.
2647 TEXFILE is the name of the file being compiled. Processing is
2648 done through the command specified in `org-e-latex-pdf-process'.
2650 Return PDF file name or an error if it couldn't be produced."
2651 (let* ((wconfig (current-window-configuration))
2652 (texfile (file-truename texfile))
2653 (base (file-name-sans-extension texfile))
2654 errors)
2655 (message (format "Processing LaTeX file %s ..." texfile))
2656 (unwind-protect
2657 (progn
2658 (cond
2659 ;; A function is provided: Apply it.
2660 ((functionp org-e-latex-pdf-process)
2661 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2662 ;; A list is provided: Replace %b, %f and %o with appropriate
2663 ;; values in each command before applying it. Output is
2664 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2665 ((consp org-e-latex-pdf-process)
2666 (let* ((out-dir (or (file-name-directory texfile) "./"))
2667 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2668 (mapc
2669 (lambda (command)
2670 (shell-command
2671 (replace-regexp-in-string
2672 "%b" (shell-quote-argument base)
2673 (replace-regexp-in-string
2674 "%f" (shell-quote-argument texfile)
2675 (replace-regexp-in-string
2676 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2677 outbuf))
2678 org-e-latex-pdf-process)
2679 ;; Collect standard errors from output buffer.
2680 (setq errors (org-e-latex--collect-errors outbuf))))
2681 (t (error "No valid command to process to PDF")))
2682 (let ((pdffile (concat base ".pdf")))
2683 ;; Check for process failure. Provide collected errors if
2684 ;; possible.
2685 (if (not (file-exists-p pdffile))
2686 (error (concat (format "PDF file %s wasn't produced" pdffile)
2687 (when errors (concat ": " errors))))
2688 ;; Else remove log files, when specified, and signal end of
2689 ;; process to user, along with any error encountered.
2690 (when org-e-latex-remove-logfiles
2691 (dolist (ext org-e-latex-logfiles-extensions)
2692 (let ((file (concat base "." ext)))
2693 (when (file-exists-p file) (delete-file file)))))
2694 (message (concat "Process completed"
2695 (if (not errors) "."
2696 (concat " with errors: " errors)))))
2697 ;; Return output file name.
2698 pdffile))
2699 (set-window-configuration wconfig))))
2701 (defun org-e-latex--collect-errors (buffer)
2702 "Collect some kind of errors from \"pdflatex\" command output.
2704 BUFFER is the buffer containing output.
2706 Return collected error types as a string, or nil if there was
2707 none."
2708 (with-current-buffer buffer
2709 (save-excursion
2710 (goto-char (point-max))
2711 ;; Find final "pdflatex" run.
2712 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2713 (let ((case-fold-search t)
2714 (errors ""))
2715 (when (save-excursion
2716 (re-search-forward "Reference.*?undefined" nil t))
2717 (setq errors (concat errors " [undefined reference]")))
2718 (when (save-excursion
2719 (re-search-forward "Citation.*?undefined" nil t))
2720 (setq errors (concat errors " [undefined citation]")))
2721 (when (save-excursion
2722 (re-search-forward "Undefined control sequence" nil t))
2723 (setq errors (concat errors " [undefined control sequence]")))
2724 (when (save-excursion
2725 (re-search-forward "^! LaTeX.*?Error" nil t))
2726 (setq errors (concat errors " [LaTeX error]")))
2727 (when (save-excursion
2728 (re-search-forward "^! Package.*?Error" nil t))
2729 (setq errors (concat errors " [package error]")))
2730 (and (org-string-nw-p errors) (org-trim errors)))))))
2733 (provide 'org-e-latex)
2734 ;;; org-e-latex.el ends here