org.el: Fix bug in done headline fontification.
[org-mode/org-mode-NeilSmithlineMods.git] / EXPERIMENTAL / org-e-latex.el
blob0b649a1ad527145f4f4846e587e9fd07d6c740e9
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-element)
40 (require 'org-export)
44 ;;; Internal Variables
46 (defconst org-e-latex-option-alist
47 '((:date "DATE" nil org-e-latex-date-format t)
48 (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
49 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
50 (:latex-header-extra "LATEX_HEADER" nil nil newline))
51 "Alist between LaTeX export properties and ways to set them.
52 See `org-export-option-alist' for more information on the
53 structure of the value.")
57 ;;; User Configurable Variables
59 (defgroup org-export-e-latex nil
60 "Options for exporting Org mode files to LaTeX."
61 :tag "Org Export LaTeX"
62 :group 'org-export)
65 ;;;; Preamble
67 (defcustom org-e-latex-default-class "article"
68 "The default LaTeX class."
69 :group 'org-export-e-latex
70 :type '(string :tag "LaTeX class"))
72 (defcustom org-e-latex-classes
73 '(("article"
74 "\\documentclass[11pt]{article}"
75 ("\\section{%s}" . "\\section*{%s}")
76 ("\\subsection{%s}" . "\\subsection*{%s}")
77 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
78 ("\\paragraph{%s}" . "\\paragraph*{%s}")
79 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
80 ("report"
81 "\\documentclass[11pt]{report}"
82 ("\\part{%s}" . "\\part*{%s}")
83 ("\\chapter{%s}" . "\\chapter*{%s}")
84 ("\\section{%s}" . "\\section*{%s}")
85 ("\\subsection{%s}" . "\\subsection*{%s}")
86 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
87 ("book"
88 "\\documentclass[11pt]{book}"
89 ("\\part{%s}" . "\\part*{%s}")
90 ("\\chapter{%s}" . "\\chapter*{%s}")
91 ("\\section{%s}" . "\\section*{%s}")
92 ("\\subsection{%s}" . "\\subsection*{%s}")
93 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
94 "Alist of LaTeX classes and associated header and structure.
95 If #+LaTeX_CLASS is set in the buffer, use its value and the
96 associated information. Here is the structure of each cell:
98 \(class-name
99 header-string
100 \(numbered-section . unnumbered-section\)
101 ...\)
103 The header string
104 -----------------
106 The HEADER-STRING is the header that will be inserted into the LaTeX file.
107 It should contain the \\documentclass macro, and anything else that is needed
108 for this setup. To this header, the following commands will be added:
110 - Calls to \\usepackage for all packages mentioned in the variables
111 `org-e-latex-default-packages-alist' and
112 `org-e-latex-packages-alist'. Thus, your header definitions should
113 avoid to also request these packages.
115 - Lines specified via \"#+LaTeX_HEADER:\"
117 If you need more control about the sequence in which the header is built
118 up, or if you want to exclude one of these building blocks for a particular
119 class, you can use the following macro-like placeholders.
121 [DEFAULT-PACKAGES] \\usepackage statements for default packages
122 [NO-DEFAULT-PACKAGES] do not include any of the default packages
123 [PACKAGES] \\usepackage statements for packages
124 [NO-PACKAGES] do not include the packages
125 [EXTRA] the stuff from #+LaTeX_HEADER
126 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
127 [BEAMER-HEADER-EXTRA] the beamer extra headers
129 So a header like
131 \\documentclass{article}
132 [NO-DEFAULT-PACKAGES]
133 [EXTRA]
134 \\providecommand{\\alert}[1]{\\textbf{#1}}
135 [PACKAGES]
137 will omit the default packages, and will include the #+LaTeX_HEADER lines,
138 then have a call to \\providecommand, and then place \\usepackage commands
139 based on the content of `org-e-latex-packages-alist'.
141 If your header or `org-e-latex-default-packages-alist' inserts
142 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
143 a coding system derived from `buffer-file-coding-system'. See also the
144 variable `org-e-latex-inputenc-alist' for a way to influence this
145 mechanism.
147 The sectioning structure
148 ------------------------
150 The sectioning structure of the class is given by the elements following
151 the header string. For each sectioning level, a number of strings is
152 specified. A %s formatter is mandatory in each section string and will
153 be replaced by the title of the section.
155 Instead of a cons cell \(numbered . unnumbered\), you can also
156 provide a list of 2 or 4 elements,
158 \(numbered-open numbered-close\)
162 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
164 providing opening and closing strings for a LaTeX environment that should
165 represent the document section. The opening clause should have a %s
166 to represent the section title.
168 Instead of a list of sectioning commands, you can also specify a
169 function name. That function will be called with two parameters,
170 the (reduced) level of the headline, and a predicate non-nil when
171 the headline should be numbered. It must return a format string in
172 which the section title will be added."
173 :group 'org-export-e-latex
174 :type '(repeat
175 (list (string :tag "LaTeX class")
176 (string :tag "LaTeX header")
177 (repeat :tag "Levels" :inline t
178 (choice
179 (cons :tag "Heading"
180 (string :tag " numbered")
181 (string :tag "unnumbered"))
182 (list :tag "Environment"
183 (string :tag "Opening (numbered)")
184 (string :tag "Closing (numbered)")
185 (string :tag "Opening (unnumbered)")
186 (string :tag "Closing (unnumbered)"))
187 (function :tag "Hook computing sectioning"))))))
189 (defcustom org-e-latex-inputenc-alist nil
190 "Alist of inputenc coding system names, and what should really be used.
191 For example, adding an entry
193 (\"utf8\" . \"utf8x\")
195 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
196 are written as utf8 files."
197 :group 'org-export-e-latex
198 :type '(repeat
199 (cons
200 (string :tag "Derived from buffer")
201 (string :tag "Use this instead"))))
203 (defcustom org-e-latex-date-format
204 "\\today"
205 "Format string for \\date{...}."
206 :group 'org-export-e-latex
207 :type 'boolean)
209 (defcustom org-e-latex-title-command "\\maketitle"
210 "The command used to insert the title just after \\begin{document}.
211 If this string contains the formatting specification \"%s\" then
212 it will be used as a formatting string, passing the title as an
213 argument."
214 :group 'org-export-e-latex
215 :type 'string)
218 ;;;; Headline
220 (defcustom org-e-latex-format-headline-function nil
221 "Function to format headline text.
223 This function will be called with 5 arguments:
224 TODO the todo keyword \(string or nil\).
225 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
226 PRIORITY the priority of the headline \(integer or nil\)
227 TEXT the main headline text \(string\).
228 TAGS the tags string, separated with colons \(string or nil\).
230 The function result will be used in the section format string.
232 As an example, one could set the variable to the following, in
233 order to reproduce the default set-up:
235 \(defun org-e-latex-format-headline \(todo todo-type priority text tags\)
236 \"Default format function for an headline.\"
237 \(concat \(when todo
238 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
239 \(when priority
240 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
241 text
242 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
243 :group 'org-export-e-latex
244 :type 'function)
247 ;;;; Emphasis
249 (defcustom org-e-latex-emphasis-alist
250 '(("*" . "\\textbf{%s}")
251 ("/" . "\\emph{%s}")
252 ("_" . "\\underline{%s}")
253 ("+" . "\\st{%s}")
254 ("=" . protectedtexttt)
255 ("~" . verb))
256 "Alist of LaTeX expressions to convert emphasis fontifiers.
258 The key is the character used as a marker for fontification. The
259 value is a formatting string to wrap fontified text with.
261 Value can also be set to the following symbols: `verb' and
262 `protectedtexttt'. For the former, Org will use \"\\verb\" to
263 create a format string and select a delimiter character that
264 isn't in the string. For the latter, Org will use \"\\texttt\"
265 to typeset and try to protect special characters."
266 :group 'org-export-e-latex
267 :type 'alist)
270 ;;;; Footnotes
272 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
273 "Text used to separate footnotes."
274 :group 'org-export-e-latex
275 :type 'string)
278 ;;;; Time-stamps
280 (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
281 "A printf format string to be applied to active time-stamps."
282 :group 'org-export-e-latex
283 :type 'string)
285 (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
286 "A printf format string to be applied to inactive time-stamps."
287 :group 'org-export-e-latex
288 :type 'string)
290 (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
291 "A printf format string to be applied to diary time-stamps."
292 :group 'org-export-e-latex
293 :type 'string)
296 ;;;; Links
298 (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
299 "Default option for images."
300 :group 'org-export-e-latex
301 :type 'string)
303 (defcustom org-e-latex-default-figure-position "htb"
304 "Default position for latex figures."
305 :group 'org-export-e-latex
306 :type 'string)
308 (defcustom org-e-latex-inline-image-extensions
309 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
310 "Extensions of image files that can be inlined into LaTeX.
312 Note that the image extension *actually* allowed depend on the
313 way the LaTeX file is processed. When used with pdflatex, pdf,
314 jpg and png images are OK. When processing through dvi to
315 Postscript, only ps and eps are allowed. The default we use here
316 encompasses both."
317 :group 'org-export-e-latex
318 :type '(repeat (string :tag "Extension")))
321 ;;;; Tables
323 (defcustom org-e-latex-default-table-environment "tabular"
324 "Default environment used to build tables."
325 :group 'org-export-e-latex
326 :type 'string)
328 (defcustom org-e-latex-tables-centered t
329 "When non-nil, tables are exported in a center environment."
330 :group 'org-export-e-latex
331 :type 'boolean)
333 (defcustom org-e-latex-tables-verbatim nil
334 "When non-nil, tables are exported verbatim."
335 :group 'org-export-e-latex
336 :type 'boolean)
338 (defcustom org-e-latex-table-caption-above t
339 "When non-nil, place caption string at the beginning of the table.
340 Otherwise, place it near the end."
341 :group 'org-export-e-latex
342 :type 'boolean)
345 ;;;; Drawers
347 (defcustom org-e-latex-format-drawer-function nil
348 "Function called to format a drawer in LaTeX code.
350 The function must accept two parameters:
351 NAME the drawer name, like \"LOGBOOK\"
352 CONTENTS the contents of the drawer.
354 The function should return the string to be exported.
356 For example, the variable could be set to the following function
357 in order to mimic default behaviour:
359 \(defun org-e-latex-format-drawer-default \(name contents\)
360 \"Format a drawer element for LaTeX export.\"
361 contents\)"
362 :group 'org-export-e-latex
363 :type 'function)
366 ;;;; Inlinetasks
368 (defcustom org-e-latex-format-inlinetask-function nil
369 "Function called to format an inlinetask in LaTeX code.
371 The function must accept six parameters:
372 TODO the todo keyword, as a string
373 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
374 PRIORITY the inlinetask priority, as a string
375 NAME the inlinetask name, as a string.
376 TAGS the inlinetask tags, as a string.
377 CONTENTS the contents of the inlinetask, as a string.
379 The function should return the string to be exported.
381 For example, the variable could be set to the following function
382 in order to mimic default behaviour:
384 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
385 \"Format an inline task element for LaTeX export.\"
386 \(let \(\(full-title
387 \(concat
388 \(when todo
389 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
390 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
391 title
392 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
393 \(format \(concat \"\\\\begin{center}\\n\"
394 \"\\\\fbox{\\n\"
395 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
396 \"%s\\n\\n\"
397 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
398 \"%s\"
399 \"\\\\end{minipage}}\"
400 \"\\\\end{center}\"\)
401 full-title contents\)\)"
402 :group 'org-export-e-latex
403 :type 'function)
406 ;; Src blocks
408 (defcustom org-e-latex-listings nil
409 "Non-nil means export source code using the listings package.
410 This package will fontify source code, possibly even with color.
411 If you want to use this, you also need to make LaTeX use the
412 listings package, and if you want to have color, the color
413 package. Just add these to `org-e-latex-packages-alist',
414 for example using customize, or with something like
416 (require 'org-e-latex)
417 (add-to-list 'org-e-latex-packages-alist '(\"\" \"listings\"))
418 (add-to-list 'org-e-latex-packages-alist '(\"\" \"color\"))
420 Alternatively,
422 (setq org-e-latex-listings 'minted)
424 causes source code to be exported using the minted package as
425 opposed to listings. If you want to use minted, you need to add
426 the minted package to `org-e-latex-packages-alist', for
427 example using customize, or with
429 (require 'org-e-latex)
430 (add-to-list 'org-e-latex-packages-alist '(\"\" \"minted\"))
432 In addition, it is necessary to install
433 pygments (http://pygments.org), and to configure the variable
434 `org-e-latex-to-pdf-process' so that the -shell-escape option is
435 passed to pdflatex."
436 :group 'org-export-e-latex
437 :type '(choice
438 (const :tag "Use listings" t)
439 (const :tag "Use minted" 'minted)
440 (const :tag "Export verbatim" nil)))
442 (defcustom org-e-latex-listings-langs
443 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
444 (c "C") (cc "C++")
445 (fortran "fortran")
446 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
447 (html "HTML") (xml "XML")
448 (tex "TeX") (latex "TeX")
449 (shell-script "bash")
450 (gnuplot "Gnuplot")
451 (ocaml "Caml") (caml "Caml")
452 (sql "SQL") (sqlite "sql"))
453 "Alist mapping languages to their listing language counterpart.
454 The key is a symbol, the major mode symbol without the \"-mode\".
455 The value is the string that should be inserted as the language parameter
456 for the listings package. If the mode name and the listings name are
457 the same, the language does not need an entry in this list - but it does not
458 hurt if it is present."
459 :group 'org-export-e-latex
460 :type '(repeat
461 (list
462 (symbol :tag "Major mode ")
463 (string :tag "Listings language"))))
465 (defcustom org-e-latex-listings-options nil
466 "Association list of options for the latex listings package.
468 These options are supplied as a comma-separated list to the
469 \\lstset command. Each element of the association list should be
470 a list containing two strings: the name of the option, and the
471 value. For example,
473 (setq org-e-latex-listings-options
474 '((\"basicstyle\" \"\\small\")
475 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
477 will typeset the code in a small size font with underlined, bold
478 black keywords.
480 Note that the same options will be applied to blocks of all
481 languages."
482 :group 'org-export-e-latex
483 :type '(repeat
484 (list
485 (string :tag "Listings option name ")
486 (string :tag "Listings option value"))))
488 (defcustom org-e-latex-minted-langs
489 '((emacs-lisp "common-lisp")
490 (cc "c++")
491 (cperl "perl")
492 (shell-script "bash")
493 (caml "ocaml"))
494 "Alist mapping languages to their minted language counterpart.
495 The key is a symbol, the major mode symbol without the \"-mode\".
496 The value is the string that should be inserted as the language parameter
497 for the minted package. If the mode name and the listings name are
498 the same, the language does not need an entry in this list - but it does not
499 hurt if it is present.
501 Note that minted uses all lower case for language identifiers,
502 and that the full list of language identifiers can be obtained
503 with:
504 pygmentize -L lexers"
505 :group 'org-export-e-latex
506 :type '(repeat
507 (list
508 (symbol :tag "Major mode ")
509 (string :tag "Minted language"))))
511 (defcustom org-e-latex-minted-options nil
512 "Association list of options for the latex minted package.
514 These options are supplied within square brackets in
515 \\begin{minted} environments. Each element of the alist should
516 be a list containing two strings: the name of the option, and the
517 value. For example,
519 (setq org-e-latex-minted-options
520 '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
522 will result in src blocks being exported with
524 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
526 as the start of the minted environment. Note that the same
527 options will be applied to blocks of all languages."
528 :group 'org-export-e-latex
529 :type '(repeat
530 (list
531 (string :tag "Minted option name ")
532 (string :tag "Minted option value"))))
534 (defvar org-e-latex-custom-lang-environments nil
535 "Alist mapping languages to language-specific LaTeX environments.
537 It is used during export of src blocks by the listings and minted
538 latex packages. For example,
540 \(setq org-e-latex-custom-lang-environments
541 '\(\(python \"pythoncode\"\)\)\)
543 would have the effect that if org encounters begin_src python
544 during latex export it will output
546 \\begin{pythoncode}
547 <src block body>
548 \\end{pythoncode}")
551 ;;;; Plain text
553 (defcustom org-e-latex-quotes
554 '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
555 ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
556 "Alist for quotes to use when converting english double-quotes.
558 The CAR of each item in this alist is the language code.
559 The CDR of each item in this alist is a list of three CONS:
560 - the first CONS defines the opening quote;
561 - the second CONS defines the closing quote;
562 - the last CONS defines single quotes.
564 For each item in a CONS, the first string is a regexp
565 for allowed characters before/after the quote, the second
566 string defines the replacement string for this quote."
567 :group 'org-export-e-latex
568 :type '(list
569 (cons :tag "Opening quote"
570 (string :tag "Regexp for char before")
571 (string :tag "Replacement quote "))
572 (cons :tag "Closing quote"
573 (string :tag "Regexp for char after ")
574 (string :tag "Replacement quote "))
575 (cons :tag "Single quote"
576 (string :tag "Regexp for char before")
577 (string :tag "Replacement quote "))))
580 ;;;; Compilation
582 (defcustom org-e-latex-pdf-process
583 '("pdflatex -interaction nonstopmode -output-directory %o %f"
584 "pdflatex -interaction nonstopmode -output-directory %o %f"
585 "pdflatex -interaction nonstopmode -output-directory %o %f")
586 "Commands to process a LaTeX file to a PDF file.
587 This is a list of strings, each of them will be given to the shell
588 as a command. %f in the command will be replaced by the full file name, %b
589 by the file base name (i.e. without extension) and %o by the base directory
590 of the file.
592 The reason why this is a list is that it usually takes several runs of
593 `pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever
594 mechanism to detect which of these commands have to be run to get to a stable
595 result, and it also does not do any error checking.
597 By default, Org uses 3 runs of `pdflatex' to do the processing. If you
598 have texi2dvi on your system and if that does not cause the infamous
599 egrep/locale bug:
601 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
603 then `texi2dvi' is the superior choice. Org does offer it as one
604 of the customize options.
606 Alternatively, this may be a Lisp function that does the processing, so you
607 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
608 This function should accept the file name as its single argument."
609 :group 'org-export-pdf
610 :type '(choice
611 (repeat :tag "Shell command sequence"
612 (string :tag "Shell command"))
613 (const :tag "2 runs of pdflatex"
614 ("pdflatex -interaction nonstopmode -output-directory %o %f"
615 "pdflatex -interaction nonstopmode -output-directory %o %f"))
616 (const :tag "3 runs of pdflatex"
617 ("pdflatex -interaction nonstopmode -output-directory %o %f"
618 "pdflatex -interaction nonstopmode -output-directory %o %f"
619 "pdflatex -interaction nonstopmode -output-directory %o %f"))
620 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
621 ("pdflatex -interaction nonstopmode -output-directory %o %f"
622 "bibtex %b"
623 "pdflatex -interaction nonstopmode -output-directory %o %f"
624 "pdflatex -interaction nonstopmode -output-directory %o %f"))
625 (const :tag "texi2dvi"
626 ("texi2dvi -p -b -c -V %f"))
627 (const :tag "rubber"
628 ("rubber -d --into %o %f"))
629 (function)))
631 (defcustom org-e-latex-logfiles-extensions
632 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
633 "The list of file extensions to consider as LaTeX logfiles."
634 :group 'org-export-e-latex
635 :type '(repeat (string :tag "Extension")))
637 (defcustom org-e-latex-remove-logfiles t
638 "Non-nil means remove the logfiles produced by PDF production.
639 These are the .aux, .log, .out, and .toc files."
640 :group 'org-export-e-latex
641 :type 'boolean)
645 ;;; Internal Functions
647 (defun org-e-latex--caption/label-string (caption label info)
648 "Return caption and label LaTeX string for floats.
650 CAPTION is a secondary string \(a list of strings and Org
651 objects\) and LABEL a string representing the label. INFO is
652 a plist holding contextual information.
654 If there's no caption nor label, return the empty string.
656 For non-floats, see `org-e-latex--wrap-label'."
657 (let ((caption-str (and caption
658 (org-export-secondary-string
659 caption 'e-latex info)))
660 (label-str (if label (format "\\label{%s}" label) "")))
661 (cond
662 ((and (not caption-str) (not label)) "")
663 ((not caption-str) (format "\\label{%s}\n" label))
664 ;; Option caption format with short name.
665 ((string-match "\\[\\([^][]*\\)\\]{\\([^{}]*\\)}" caption-str)
666 (format "\\caption[%s]{%s%s}\n"
667 (org-match-string-no-properties 1 caption-str)
668 label-str
669 (org-match-string-no-properties 2 caption-str)))
670 ;; Standard caption format.
671 (t (format "\\caption{%s%s}\n" label-str caption-str)))))
673 (defun org-e-latex--guess-inputenc (header)
674 "Set the coding system in inputenc to what the buffer is.
676 HEADER is the LaTeX header string.
678 Return the new header."
679 (let* ((cs (or (ignore-errors
680 (latexenc-coding-system-to-inputenc
681 buffer-file-coding-system))
682 "utf8")))
683 (if (not cs)
684 header
685 ;; First translate if that is requested.
686 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
687 ;; Then find the \usepackage statement and replace the option.
688 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
689 cs header t nil 1))))
691 (defun org-e-latex--find-verb-separator (s)
692 "Return a character not used in string S.
693 This is used to choose a separator for constructs like \\verb."
694 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
695 (loop for c across ll
696 when (not (string-match (regexp-quote (char-to-string c)) s))
697 return (char-to-string c))))
699 (defun org-e-latex--make-option-string (options)
700 "Return a comma separated string of keywords and values.
701 OPTIONS is an alist where the key is the options keyword as
702 a string, and the value a list containing the keyword value, or
703 nil."
704 (mapconcat (lambda (pair)
705 (concat (first pair)
706 (when (> (length (second pair)) 0)
707 (concat "=" (second pair)))))
708 options
709 ","))
711 (defun org-e-latex--quotation-marks (text info)
712 "Export quotation marks depending on language conventions.
713 TEXT is a string containing quotation marks to be replaced. INFO
714 is a plist used as a communication channel."
715 (mapc (lambda(l)
716 (let ((start 0))
717 (while (setq start (string-match (car l) text start))
718 (let ((new-quote (concat (match-string 1 text) (cdr l))))
719 (setq text (replace-match new-quote t t text))))))
720 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
721 ;; Falls back on English.
722 (assoc "en" org-e-latex-quotes))))
723 text)
725 (defun org-e-latex--wrap-label (element output)
726 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
727 This function shouldn't be used for floats. See
728 `org-e-latex--caption/label-string'."
729 (let ((label (org-element-get-property :name element)))
730 (if (or (not output) (not label) (string= output "") (string= label ""))
731 output
732 (concat (format "\\label{%s}\n" label) output))))
736 ;;; Template
738 (defun org-e-latex-template (contents info)
739 "Return complete document string after LaTeX conversion.
740 CONTENTS is the transcoded contents string. INFO is a plist
741 holding export options."
742 (let ((title (org-export-secondary-string
743 (plist-get info :title) 'e-latex info)))
744 (concat
745 ;; 1. Time-stamp.
746 (and (plist-get info :time-stamp-file)
747 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
748 ;; 2. Document class and packages.
749 (let ((class (plist-get info :latex-class))
750 (class-options (plist-get info :latex-class-options)))
751 (org-element-normalize-string
752 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
753 (document-class-string
754 (and (stringp header)
755 (if class-options
756 (replace-regexp-in-string
757 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
758 class-options header t nil 1)
759 header))))
760 (org-e-latex--guess-inputenc
761 (org-splice-latex-header
762 document-class-string
763 org-export-latex-default-packages-alist ; defined in org.el
764 org-export-latex-packages-alist nil ; defined in org.el
765 (plist-get info :latex-header-extra))))))
766 ;; 3. Define alert if not yet defined.
767 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
768 ;; 4. Author.
769 (let ((author (and (plist-get info :with-author)
770 (let ((auth (plist-get info :author)))
771 (and auth (org-export-secondary-string
772 auth 'e-latex info)))))
773 (email (and (plist-get info :with-email)
774 (org-export-secondary-string
775 (plist-get info :email) 'e-latex info))))
776 (cond ((and author email (not (string= "" email)))
777 (format "\\author{%s\\thanks{%s}}\n" author email))
778 (author (format "\\author{%s}\n" author))
779 (t "\\author{}\n")))
780 ;; 5. Date.
781 (let ((date (plist-get info :date)))
782 (and date (format "\\date{%s}\n" date)))
783 ;; 6. Title
784 (format "\\title{%s}\n" title)
785 ;; 7. Hyperref options.
786 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
787 (or (plist-get info :keywords) "")
788 (or (plist-get info :description) "")
789 (let ((creator-info (plist-get info :with-creator)))
790 (cond
791 ((not creator-info) "")
792 ((eq creator-info 'comment) "")
793 (t (plist-get info :creator)))))
794 ;; 7. Document start.
795 "\\begin{document}\n\n"
796 ;; 8. Title command.
797 (org-element-normalize-string
798 (cond ((string= "" title) nil)
799 ((not (stringp org-e-latex-title-command)) nil)
800 ((string-match "\\(?:[^%]\\|^\\)%s"
801 org-e-latex-title-command)
802 (format org-e-latex-title-command title))
803 (t org-e-latex-title-command)))
804 ;; 9. Table of contents.
805 (let ((depth (plist-get info :with-toc)))
806 (when depth
807 (concat (when (wholenump depth)
808 (format "\\setcounter{tocdepth}{%d}\n" depth))
809 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
810 ;; 10. Document's body.
811 contents
812 ;; 11. Creator.
813 (let ((creator-info (plist-get info :with-creator)))
814 (cond
815 ((not creator-info))
816 ((eq creator-info 'comment)
817 (format "%% %s\n" (plist-get info :creator)))
818 (t (concat (plist-get info :creator) "\n"))))
819 ;; 12. Document end.
820 "\\end{document}")))
824 ;;; Transcode Functions
826 ;;;; Block
828 (defun org-e-latex-center-block (center-block contents info)
829 "Transcode a CENTER-BLOCK element from Org to LaTeX.
830 CONTENTS holds the contents of the block. INFO is a plist
831 holding contextual information."
832 (org-e-latex--wrap-label
833 center-block
834 (format "\\begin{center}\n%s\\end{center}" contents)))
837 ;;;; Comment
839 ;; Comments are ignored.
842 ;;;; Comment Block
844 ;; Comment Blocks are ignored.
847 ;;;; Drawer
849 (defun org-e-latex-drawer (drawer contents info)
850 "Transcode a DRAWER element from Org to LaTeX.
851 CONTENTS holds the contents of the block. INFO is a plist
852 holding contextual information."
853 (let* ((name (org-element-get-property :drawer-name drawer))
854 (output (if (functionp org-e-latex-format-drawer-function)
855 (funcall org-e-latex-format-drawer-function
856 name contents)
857 ;; If there's no user defined function: simply
858 ;; display contents of the drawer.
859 contents)))
860 (org-e-latex--wrap-label drawer output)))
863 ;;;; Dynamic Block
865 (defun org-e-latex-dynamic-block (dynamic-block contents info)
866 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
867 CONTENTS holds the contents of the block. INFO is a plist
868 holding contextual information. See
869 `org-export-data'."
870 (org-e-latex--wrap-label dynamic-block contents))
873 ;;;; Emphasis
875 (defun org-e-latex-emphasis (emphasis contents info)
876 "Transcode EMPHASIS from Org to LaTeX.
877 CONTENTS is the contents of the emphasized text. INFO is a plist
878 holding contextual information.."
879 (format (cdr (assoc (org-element-get-property :marker emphasis)
880 org-e-latex-emphasis-alist))
881 contents))
884 ;;;; Entity
886 (defun org-e-latex-entity (entity contents info)
887 "Transcode an ENTITY object from Org to LaTeX.
888 CONTENTS are the definition itself. INFO is a plist holding
889 contextual information."
890 (let ((ent (org-element-get-property :latex entity)))
891 (if (org-element-get-property :latex-math-p entity)
892 (format "$%s$" ent)
893 ent)))
896 ;;;; Example Block
898 (defun org-e-latex-example-block (example-block contents info)
899 "Transcode a EXAMPLE-BLOCK element from Org to LaTeX.
900 CONTENTS is nil. INFO is a plist holding contextual information."
901 (let* ((options (or (org-element-get-property :options example-block) ""))
902 (value (org-export-handle-code example-block info)))
903 (org-e-latex--wrap-label
904 example-block (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
907 ;;;; Export Snippet
909 (defun org-e-latex-export-snippet (export-snippet contents info)
910 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
911 CONTENTS is nil. INFO is a plist holding contextual information."
912 (org-element-get-property :value export-snippet))
915 ;;;; Export Block
917 (defun org-e-latex-export-block (export-block contents info)
918 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
919 CONTENTS is nil. INFO is a plist holding contextual information."
920 (when (string= (org-element-get-property :type export-block) "latex")
921 (org-remove-indentation (org-element-get-property :value export-block))))
924 ;;;; Fixed Width
926 (defun org-e-latex-fixed-width (fixed-width contents info)
927 "Transcode a FIXED-WIDTH element from Org to LaTeX.
928 CONTENTS is nil. INFO is a plist holding contextual information."
929 (let* ((value (org-element-normalize-string
930 (replace-regexp-in-string
931 "^[ \t]*: ?" ""
932 (org-element-get-property :value fixed-width)))))
933 (org-e-latex--wrap-label
934 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
937 ;;;; Footnote Definition
939 ;; Footnote Definitions are ignored.
942 ;;;; Footnote Reference
944 (defun org-e-latex-footnote-reference (footnote-reference contents info)
945 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
946 CONTENTS is nil. INFO is a plist holding contextual information."
947 (concat
948 ;; Insert separator between two footnotes in a row.
949 (let ((prev (org-export-get-previous-element footnote-reference info)))
950 (when (and (listp prev) (eq (car prev) 'footnote-reference))
951 org-e-latex-footnote-separator))
952 ;; Use \footnotemark if the footnote has already been defined.
953 ;; Otherwise, define it with \footnote command.
954 (cond
955 ((not (org-export-footnote-first-reference-p footnote-reference info))
956 (format "\\footnotemark[%s]"
957 (org-export-get-footnote-number footnote-reference info)))
958 ;; Inline definitions are secondary strings.
959 ((eq (org-element-get-property :type footnote-reference) 'inline)
960 (format "\\footnote{%s}"
961 (org-trim
962 (org-export-secondary-string
963 (org-export-get-footnote-definition footnote-reference info)
964 'e-latex info))))
965 ;; Non-inline footnotes definitions are full Org data.
967 (format "\\footnote{%s}"
968 (org-trim
969 (org-export-data
970 (org-export-get-footnote-definition footnote-reference info)
971 'e-latex info)))))))
974 ;;;; Headline
976 (defun org-e-latex-headline (headline contents info)
977 "Transcode an HEADLINE element from Org to LaTeX.
978 CONTENTS holds the contents of the headline. INFO is a plist
979 holding contextual information."
980 (let* ((class (plist-get info :latex-class))
981 (numberedp (plist-get info :section-numbers))
982 ;; Get level relative to current parsed data.
983 (level (org-export-get-relative-level headline info))
984 (class-sectionning (assoc class org-e-latex-classes))
985 ;; Section formatting will set two placeholders: one for the
986 ;; title and the other for the contents.
987 (section-fmt
988 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
989 (fboundp (nth 2 class-sectionning)))
990 (funcall (nth 2 class-sectionning) level numberedp)
991 (nth (1+ level) class-sectionning))))
992 (cond
993 ;; No section available for that LEVEL.
994 ((not sec) nil)
995 ;; Section format directly returned by a function.
996 ((stringp sec) sec)
997 ;; (numbered-section . unnumbered-section)
998 ((not (consp (cdr sec)))
999 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1000 ;; (numbered-open numbered-close)
1001 ((= (length sec) 2)
1002 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1003 ;; (num-in num-out no-num-in no-num-out)
1004 ((= (length sec) 4)
1005 (if numberedp
1006 (concat (car sec) "\n%s" (nth 1 sec))
1007 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1008 (text (org-export-secondary-string
1009 (org-element-get-property :title headline) 'e-latex info))
1010 (todo (and (plist-get info :with-todo-keywords)
1011 (let ((todo (org-element-get-property
1012 :todo-keyword headline)))
1013 (and todo
1014 (org-export-secondary-string todo 'e-latex info)))))
1015 (todo-type (and todo (org-element-get-property :todo-type headline)))
1016 (tags (and (plist-get info :with-tags)
1017 (org-element-get-property :tags headline)))
1018 (priority (and (plist-get info :with-priority)
1019 (org-element-get-property :priority headline)))
1020 ;; Create the headline text.
1021 (full-text (if (functionp org-e-latex-format-headline-function)
1022 ;; User-defined formatting function.
1023 (funcall org-e-latex-format-headline-function
1024 todo todo-type priority text tags)
1025 ;; Default formatting.
1026 (concat
1027 (when todo
1028 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1029 (when priority (format "\\framebox{\\#%c} " priority))
1030 text
1031 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1032 ;; Associate some \label to the headline for internal links.
1033 (headline-labels (mapconcat
1034 (lambda (p)
1035 (let ((val (org-element-get-property p headline)))
1036 (when val (format "\\label{%s}\n"
1037 (if (eq p :begin)
1038 (format "headline-%s" val)
1039 val)))))
1040 '(:custom-id :id :begin) ""))
1041 (pre-blanks (make-string (org-element-get-property :pre-blank headline)
1042 10)))
1043 (cond
1044 ;; Case 1: This is a footnote section: ignore it.
1045 ((org-element-get-property :footnote-section-p headline) nil)
1046 ;; Case 2. This is a deep sub-tree: export it as a list item.
1047 ;; Also export as items headlines for which no section
1048 ;; format has been found.
1049 ((or (not section-fmt) (org-export-low-level-p headline info))
1050 ;; Build the real contents of the sub-tree.
1051 (let ((low-level-body
1052 (concat
1053 ;; If the headline is the first sibling, start a list.
1054 (when (org-export-first-sibling-p headline info)
1055 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1056 ;; Itemize headline
1057 "\\item " full-text "\n" headline-labels pre-blanks contents)))
1058 ;; If headline in the last sibling, close the list, before any
1059 ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
1060 (if (org-export-last-sibling-p headline info)
1061 (replace-regexp-in-string
1062 "[ \t\n]*\\'"
1063 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1064 low-level-body)
1065 low-level-body)))
1066 ;; Case 3. Standard headline. Export it as a section.
1067 (t (format section-fmt full-text
1068 (concat headline-labels pre-blanks contents))))))
1071 ;;;; Horizontal Rule
1073 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1074 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1075 CONTENTS is nil. INFO is a plist holding contextual information."
1076 (let ((attr (mapconcat #'identity
1077 (org-element-get-property :attr_latex horizontal-rule)
1078 " ")))
1079 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1082 ;;;; Inline Babel Call
1084 ;; Inline Babel Calls are ignored.
1087 ;;;; Inline Src Block
1089 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1090 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1091 CONTENTS holds the contents of the item. INFO is a plist holding
1092 contextual information."
1093 (let* ((code (org-element-get-property :value inline-src-block))
1094 (separator (org-e-latex--find-verb-separator code)))
1095 (cond
1096 ;; Do not use a special package: transcode it verbatim.
1097 ((not org-e-latex-listings)
1098 (concat "\\verb" separator code separator))
1099 ;; Use minted package.
1100 ((eq org-e-latex-listings 'minted)
1101 (let* ((org-lang (org-element-get-property :language inline-src-block))
1102 (mint-lang (or (cadr (assq (intern org-lang)
1103 org-e-latex-minted-langs))
1104 org-lang))
1105 (options (org-e-latex--make-option-string
1106 org-e-latex-minted-options)))
1107 (concat (format "\\mint%s{%s}"
1108 (if (string= options "") "" (format "[%s]" options))
1109 mint-lang)
1110 separator code separator)))
1111 ;; Use listings package.
1113 ;; Maybe translate language's name.
1114 (let* ((org-lang (org-element-get-property :language inline-src-block))
1115 (lst-lang (or (cadr (assq (intern org-lang)
1116 org-e-latex-listings-langs))
1117 org-lang))
1118 (options (org-e-latex--make-option-string
1119 (append org-e-latex-listings-options
1120 `(("language" ,lst-lang))))))
1121 (concat (format "\\lstinline[%s]" options)
1122 separator code separator))))))
1125 ;;;; Inlinetask
1127 (defun org-e-latex-inlinetask (inlinetask contents info)
1128 "Transcode an INLINETASK element from Org to LaTeX.
1129 CONTENTS holds the contents of the block. INFO is a plist
1130 holding contextual information."
1131 (let ((title (org-export-secondary-string
1132 (org-element-get-property :title inlinetask) 'e-latex info))
1133 (todo (and (plist-get info :with-todo-keywords)
1134 (let ((todo (org-element-get-property
1135 :todo-keyword inlinetask)))
1136 (and todo
1137 (org-export-secondary-string todo 'e-latex info)))))
1138 (todo-type (org-element-get-property :todo-type inlinetask))
1139 (tags (and (plist-get info :with-tags)
1140 (org-element-get-property :tags inlinetask)))
1141 (priority (and (plist-get info :with-priority)
1142 (org-element-get-property :priority inlinetask))))
1143 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1144 ;; with appropriate arguments.
1145 (if (functionp org-e-latex-format-inlinetask-function)
1146 (funcall org-e-latex-format-inlinetask-function
1147 todo todo-type priority title tags contents)
1148 ;; Otherwise, use a default template.
1149 (org-e-latex--wrap-label
1150 inlinetask
1151 (let ((full-title
1152 (concat
1153 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1154 (when priority (format "\\framebox{\\#%c} " priority))
1155 title
1156 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1157 (format (concat "\\begin{center}\n"
1158 "\\fbox{\n"
1159 "\\begin{minipage}[c]{.6\\textwidth}\n"
1160 "%s\n\n"
1161 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1162 "%s"
1163 "\\end{minipage}\n"
1164 "}\n"
1165 "\\end{center}")
1166 full-title contents))))))
1169 ;;;; Item
1171 (defun org-e-latex-item (item contents info)
1172 "Transcode an ITEM element from Org to LaTeX.
1173 CONTENTS holds the contents of the item. INFO is a plist holding
1174 contextual information."
1175 ;; Grab `:level' from plain-list properties, which is always the
1176 ;; first element above current item.
1177 (let* ((level (org-element-get-property
1178 :level (car (plist-get info :genealogy))))
1179 (counter (let ((count (org-element-get-property :counter item)))
1180 (and count
1181 (< level 4)
1182 (format "\\setcounter{enum%s}{%s}\n"
1183 (nth level '("i" "ii" "iii" "iv"))
1184 (1- count)))))
1185 (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
1186 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1187 ((eq checkbox 'off) "$\\Box$ ")
1188 ((eq checkbox 'trans) "$\\boxminus$ "))))
1189 (tag (let ((tag (org-element-get-property :tag item)))
1190 (and tag
1191 (format "[%s]" (org-export-secondary-string
1192 tag 'e-latex info))))))
1193 (concat counter "\\item" tag " " checkbox contents)))
1196 ;;;; Keyword
1198 (defun org-e-latex-keyword (keyword contents info)
1199 "Transcode a KEYWORD element from Org to LaTeX.
1200 CONTENTS is nil. INFO is a plist holding contextual information."
1201 (let ((key (downcase (org-element-get-property :key keyword)))
1202 (value (org-element-get-property :value keyword)))
1203 (cond
1204 ((string= key "latex") value)
1205 ((string= key "index") (format "\\index{%s}" value))
1206 ((string= key "target")
1207 (format "\\label{%s}" (org-export-solidify-link-text value)))
1208 ((string= key "toc")
1209 (let ((value (downcase value)))
1210 (cond
1211 ((string-match "\\<headlines\\>" value)
1212 (let ((depth (or (and (string-match "[0-9]+" value)
1213 (string-to-number (match-string 0 value)))
1214 (plist-get info :with-toc))))
1215 (concat
1216 (when (wholenump depth)
1217 (format "\\setcounter{tocdepth}{%s}\n" depth))
1218 "\\tableofcontents")))
1219 ((string= "tables" value) "\\listoftables")
1220 ((string= "figures" value) "\\listoffigures")
1221 ((string= "listings" value) "\\listoflistings"))))
1222 ((string= key "include")
1223 (org-export-included-file keyword 'e-latex info)))))
1226 ;;;; Latex Environment
1228 (defun org-e-latex-latex-environment (latex-environment contents info)
1229 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1230 CONTENTS is nil. INFO is a plist holding contextual information."
1231 (org-e-latex--wrap-label
1232 latex-environment
1233 (org-remove-indentation (org-element-get-property :value latex-environment))))
1236 ;;;; Latex Fragment
1238 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1239 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1240 CONTENTS is nil. INFO is a plist holding contextual information."
1241 (org-element-get-property :value latex-fragment))
1244 ;;;; Line Break
1246 (defun org-e-latex-line-break (line-break contents info)
1247 "Transcode a LINE-BREAK object from Org to LaTeX.
1248 CONTENTS is nil. INFO is a plist holding contextual information."
1249 "\\\\")
1252 ;;;; Link
1254 (defun org-e-latex-link--inline-image (path info)
1255 "Return LaTeX code for an image at PATH.
1256 INFO is a plist containing export options."
1257 (let* ((parent-props (nth 1 (car (plist-get info :genealogy))))
1258 (caption (org-e-latex--caption/label-string
1259 (plist-get parent-props :caption)
1260 (plist-get parent-props :name)
1261 info))
1262 ;; Retrieve latex attributes from the element around.
1263 (attr (let ((raw-attr
1264 (mapconcat #'identity
1265 (plist-get parent-props :attr_latex) " ")))
1266 (unless (string= raw-attr "") raw-attr)))
1267 (disposition
1268 (cond
1269 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1270 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1271 ((or (and attr (string-match "\\<float\\>" attr))
1272 (not (string= caption "")))
1273 'float)))
1274 (placement
1275 (cond
1276 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1277 (org-match-string-no-properties 1 attr))
1278 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1279 ((eq disposition 'float)
1280 (concat "[" org-e-latex-default-figure-position "]"))
1281 (t ""))))
1282 ;; Now clear ATTR from any special keyword and set a default
1283 ;; value if nothing is left.
1284 (if (not attr)
1285 (setq attr "")
1286 (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
1287 attr)
1288 (replace-match "" nil nil attr))
1289 (setq attr (org-trim attr)))
1290 (setq attr (cond ((not (string= attr "")) attr)
1291 ((eq disposition 'float) "width=0.7\\textwidth")
1292 ((eq disposition 'wrap) "width=0.48\\textwidth")
1293 (t (or org-e-latex-image-default-option ""))))
1294 ;; Return proper string, depending on DISPOSITION.
1295 (case disposition
1296 ('wrap (format "\\begin{wrapfigure}%s
1297 \\centering
1298 \\includegraphics[%s]{%s}
1299 %s\\end{wrapfigure}" placement attr path caption))
1300 ('mulicolumn (format "\\begin{figure*}%s
1301 \\centering
1302 \\includegraphics[%s]{%s}
1303 %s\\end{figure*}" placement attr path caption))
1304 ('float (format "\\begin{figure}%s
1305 \\centering
1306 \\includegraphics[%s]{%s}
1307 %s\\end{figure}" placement attr path caption))
1308 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1310 (defun org-e-latex-link (link desc info)
1311 "Transcode a LINK object from Org to LaTeX.
1313 DESC is the description part of the link, or the empty string.
1314 INFO is a plist holding contextual information. See
1315 `org-export-data'."
1316 (let* ((type (org-element-get-property :type link))
1317 (raw-path (org-element-get-property :path link))
1318 ;; Ensure DESC really exists, or set it to nil.
1319 (desc (and (not (string= desc "")) desc))
1320 (imagep (org-export-inline-image-p
1321 link org-e-latex-inline-image-extensions))
1322 (path (cond
1323 ((member type '("http" "https" "ftp" "mailto"))
1324 (concat type ":" raw-path))
1325 ((and (not imagep) (string= type "file"))
1326 (when (string-match "\\(.+\\)::.+" raw-path)
1327 (setq raw-path (match-string 1 raw-path)))
1328 (if (file-name-absolute-p raw-path)
1329 (concat "file://" (expand-file-name raw-path))
1330 ;; TODO: Not implemented yet. Concat also:
1331 ;; (org-export-directory :LaTeX info)
1332 (concat "file://" raw-path)))
1333 (t raw-path)))
1334 protocol)
1335 (cond
1336 ;; Image file.
1337 (imagep (org-e-latex-link--inline-image path info))
1338 ;; Id: for now, assume it's an internal link. TODO: do something
1339 ;; to check if it isn't in the current file.
1340 ((string= type "id")
1341 (format "\\hyperref[%s]{%s}" path (or desc path)))
1342 ;; Custom-id, target or radioed target: replace link with the
1343 ;; normalized custom-id/target name.
1344 ((member type '("custom-id" "target" "radio"))
1345 (format "\\hyperref[%s]{%s}"
1346 (org-export-solidify-link-text path)
1347 (or desc (org-export-secondary-string path 'e-latex info))))
1348 ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
1349 ;; the destination of the link.
1350 ((string= type "fuzzy")
1351 (let ((destination (org-export-resolve-fuzzy-link link info)))
1352 (case (car destination)
1353 (target
1354 (format "\\hyperref[%s]{%s}"
1355 (org-export-solidify-link-text
1356 (org-element-get-property :raw-value destination))
1357 (or desc
1358 (org-export-secondary-string
1359 (org-element-get-property :raw-link link)
1360 'e-latex info))))
1361 (headline
1362 (format "\\hyperref[headline-%d]{%s}"
1363 (org-element-get-property :begin destination)
1364 (or desc
1365 (org-export-secondary-string
1366 (org-element-get-property :raw-link link)
1367 'e-latex info))))
1368 (otherwise
1369 (format "\\texttt{%s}"
1370 (or desc
1371 (org-export-secondary-string
1372 (org-element-get-property :raw-link link)
1373 'e-latex info)))))))
1374 ;; Coderef: replace link with the reference name or the
1375 ;; equivalent line number.
1376 ((string= type "coderef")
1377 (format (org-export-get-coderef-format path (or desc ""))
1378 (org-export-resolve-coderef path info)))
1379 ;; Link type is handled by a special function.
1380 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1381 (funcall protocol (org-link-unescape path) desc 'latex))
1382 ;; External link with a description part.
1383 ((and path desc) (format "\\href{%s}{%s}" path desc))
1384 ;; External link without a description part.
1385 (path (format "\\url{%s}" path))
1386 ;; No path, only description. Try to do something useful.
1387 (t (format "\\texttt{%s}" desc)))))
1390 ;;;; Babel Call
1392 ;; Babel Calls are ignored.
1395 ;;;; Macro
1397 (defun org-e-latex-macro (macro contents info)
1398 "Transcode a MACRO element from Org to LaTeX.
1399 CONTENTS is nil. INFO is a plist holding contextual information."
1400 ;; Use available tools.
1401 (org-export-expand-macro macro info))
1404 ;;;; Paragraph
1406 (defun org-e-latex-paragraph (paragraph contents info)
1407 "Transcode a PARAGRAPH element from Org to LaTeX.
1408 CONTENTS is the contents of the paragraph, as a string. INFO is
1409 the plist used as a communication channel."
1410 contents)
1413 ;;;; Plain List
1415 (defun org-e-latex-plain-list (plain-list contents info)
1416 "Transcode a PLAIN-LIST element from Org to LaTeX.
1417 CONTENTS is the contents of the list. INFO is a plist holding
1418 contextual information."
1419 (let* ((type (org-element-get-property :type plain-list))
1420 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1421 "inparadesc" "asparadesc"))
1422 (paralist-regexp (concat
1423 "\\("
1424 (mapconcat 'identity paralist-types "\\|")
1425 "\\)"))
1426 (attr (mapconcat #'identity
1427 (org-element-get-property :attr_latex plain-list)
1428 " "))
1429 (latex-type (cond
1430 ((and attr
1431 (string-match
1432 (format "\\<%s\\>" paralist-regexp) attr))
1433 (match-string 1 attr))
1434 ((eq type 'ordered) "enumerate")
1435 ((eq type 'unordered) "itemize")
1436 ((eq type 'descriptive) "description"))))
1437 (org-e-latex--wrap-label
1438 plain-list
1439 (format "\\begin{%s}%s\n%s\\end{%s}"
1440 latex-type
1441 ;; Once special environment, if any, has been removed, the
1442 ;; rest of the attributes will be optional arguments.
1443 ;; They will be put inside square brackets if necessary.
1444 (let ((opt (replace-regexp-in-string
1445 (format " *%s *" paralist-regexp) "" attr)))
1446 (cond ((string= opt "") "")
1447 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1448 (t (format "[%s]" opt))))
1449 contents
1450 latex-type))))
1453 ;;;; Plain Text
1455 (defun org-e-latex-plain-text (text info)
1456 "Transcode a TEXT string from Org to LaTeX.
1457 TEXT is the string to transcode. INFO is a plist holding
1458 contextual information."
1459 ;; Protect %, #, &, $, ~, ^, _, { and }.
1460 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1461 (setq text
1462 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1463 ;; Protect \
1464 (setq text (replace-regexp-in-string
1465 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1466 "$\\backslash$" text nil t 1))
1467 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1468 (let ((case-fold-search nil)
1469 (start 0))
1470 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1471 (setq text (replace-match
1472 (format "\\%s{}" (match-string 1 text)) nil t text)
1473 start (match-end 0))))
1474 ;; Handle quotation marks
1475 (setq text (org-e-latex--quotation-marks text info))
1476 ;; Convert special strings.
1477 (when (plist-get info :with-special-strings)
1478 (while (string-match (regexp-quote "...") text)
1479 (setq text (replace-match "\\ldots{}" nil t text))))
1480 ;; Handle break preservation if required.
1481 (when (plist-get info :preserve-breaks)
1482 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1483 text)))
1484 ;; Return value.
1485 text)
1488 ;;;; Property Drawer
1490 (defun org-e-latex-property-drawer (property-drawer contents info)
1491 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1492 CONTENTS is nil. INFO is a plist holding contextual
1493 information."
1494 ;; The property drawer isn't exported but we want separating blank
1495 ;; lines nonetheless.
1499 ;;;; Quote Block
1501 (defun org-e-latex-quote-block (quote-block contents info)
1502 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1503 CONTENTS holds the contents of the block. INFO is a plist
1504 holding contextual information."
1505 (org-e-latex--wrap-label
1506 quote-block
1507 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1510 ;;;; Quote Section
1512 (defun org-e-latex-quote-section (quote-section contents info)
1513 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1514 CONTENTS is nil. INFO is a plist holding contextual information."
1515 (let ((value (org-remove-indentation
1516 (org-element-get-property :value quote-section))))
1517 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1520 ;;;; Section
1522 (defun org-e-latex-section (section contents info)
1523 "Transcode a SECTION element from Org to LaTeX.
1524 CONTENTS holds the contents of the section. INFO is a plist
1525 holding contextual information."
1526 contents)
1529 ;;;; Radio Target
1531 (defun org-e-latex-radio-target (radio-target text info)
1532 "Transcode a RADIO-TARGET object from Org to LaTeX.
1533 TEXT is the text of the target. INFO is a plist holding
1534 contextual information."
1535 (format "\\label{%s}%s"
1536 (org-export-solidify-link-text
1537 (org-element-get-property :raw-value radio-target))
1538 text))
1541 ;;;; Special Block
1543 (defun org-e-latex-special-block (special-block contents info)
1544 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1545 CONTENTS holds the contents of the block. INFO is a plist
1546 holding contextual information."
1547 (let ((type (downcase (org-element-get-property :type special-block))))
1548 (org-e-latex--wrap-label
1549 special-block
1550 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1553 ;;;; Src Block
1555 (defun org-e-latex-src-block (src-block contents info)
1556 "Transcode a SRC-BLOCK element from Org to LaTeX.
1557 CONTENTS holds the contents of the item. INFO is a plist holding
1558 contextual information."
1559 (let* ((lang (org-element-get-property :language src-block))
1560 (code (org-export-handle-code src-block info))
1561 (caption (org-element-get-property :caption src-block))
1562 (label (org-element-get-property :name src-block))
1563 (custom-env (and lang
1564 (cadr (assq (intern lang)
1565 org-e-latex-custom-lang-environments)))))
1566 (cond
1567 ;; No source fontification.
1568 ((not org-e-latex-listings)
1569 (let ((caption-str (org-e-latex--caption/label-string
1570 caption label info))
1571 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1572 (format (or float-env "%s")
1573 (concat
1574 caption-str
1575 (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
1576 ;; Custom environment.
1577 (custom-env
1578 (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
1579 ;; Use minted package.
1580 ((eq org-e-latex-listings 'minted)
1581 (let* ((mint-lang (or (cadr (assq (intern lang) org-e-latex-minted-langs))
1582 lang))
1583 (float-env (when (or label caption)
1584 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1585 (org-e-latex--caption/label-string
1586 caption label info))))
1587 (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1588 (org-e-latex--make-option-string
1589 org-e-latex-minted-options)
1590 mint-lang code)))
1591 (if float-env (format float-env body) body)))
1592 ;; Use listings package.
1594 (let ((lst-lang (or (cadr (assq (intern lang) org-e-latex-listings-langs))
1595 lang))
1596 (caption-str (and caption
1597 (org-export-secondary-string
1598 (org-element-get-property :caption src-block)
1599 'e-latex info))))
1600 (concat (format "\\lstset{%s}\n"
1601 (org-e-latex--make-option-string
1602 (append org-e-latex-listings-options
1603 `(("language" ,lst-lang))
1604 (when label `(("label" ,label)))
1605 (when caption-str
1606 `(("caption" ,caption-str))))))
1607 (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
1610 ;;;; Statistics Cookie
1612 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1613 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1614 CONTENTS is nil. INFO is a plist holding contextual information."
1615 (org-element-get-property :value statistics-cookie))
1618 ;;;; Subscript
1620 (defun org-e-latex-subscript (subscript contents info)
1621 "Transcode a SUBSCRIPT object from Org to LaTeX.
1622 CONTENTS is the contents of the object. INFO is a plist holding
1623 contextual information."
1624 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1627 ;;;; Superscript
1629 (defun org-e-latex-superscript (superscript contents info)
1630 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1631 CONTENTS is the contents of the object. INFO is a plist holding
1632 contextual information."
1633 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1636 ;;;; Table
1638 (defun org-e-latex-table--format-string (table info)
1639 "Return an appropriate format string for TABLE.
1641 INFO is the plist containing format info about the table, as
1642 returned by `org-export-table-format-info'.
1644 The format string one placeholder for the body of the table."
1645 (let* ((label (org-element-get-property :name table))
1646 (caption (org-e-latex--caption/label-string
1647 (org-element-get-property :caption table) label info))
1648 (attr (mapconcat #'identity
1649 (org-element-get-property :attr_latex table)
1650 " "))
1651 ;; Determine alignment string.
1652 (alignment (org-e-latex-table--align-string attr info))
1653 ;; Determine environment for the table: longtable, tabular...
1654 (table-env (cond
1655 ((not attr) org-e-latex-default-table-environment)
1656 ((string-match "\\<longtable\\>" attr) "longtable")
1657 ((string-match "\\(tabular.\\)" attr)
1658 (org-match-string-no-properties 1 attr))
1659 (t org-e-latex-default-table-environment)))
1660 ;; If table is a float, determine environment: table or table*.
1661 (float-env (cond
1662 ((string= "longtable" table-env) nil)
1663 ((and attr
1664 (or (string-match (regexp-quote "table*") attr)
1665 (string-match "\\<multicolumn\\>" attr)))
1666 "table*")
1667 ((or (not (string= caption "")) label) "table")))
1668 ;; Extract others display options.
1669 (width (and attr
1670 (string-match "\\<width=\\(\\S-+\\)" attr)
1671 (org-match-string-no-properties 1 attr)))
1672 (placement (if (and attr
1673 (string-match "\\<placement=\\(\\S-+\\)" attr))
1674 (org-match-string-no-properties 1 attr)
1675 (concat "["
1676 org-e-latex-default-figure-position
1677 "]"))))
1678 ;; Prepare the final format string for the table.
1679 (cond
1680 ;; Longtable.
1681 ((string= "longtable" table-env)
1682 (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1683 alignment
1684 (if (or (not org-e-latex-table-caption-above)
1685 (string= "" caption))
1687 (concat (org-trim caption) "\\\\"))
1688 (if (or org-e-latex-table-caption-above
1689 (string= "" caption))
1691 (concat (org-trim caption) "\\\\\n"))))
1692 ;; Others.
1693 (t (concat (when float-env
1694 (concat
1695 (format "\\begin{%s}%s\n" float-env placement)
1696 (if org-e-latex-table-caption-above caption "")))
1697 (when org-e-latex-tables-centered "\\begin{center}\n")
1698 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1699 table-env
1700 (if width (format "{%s}" width) "")
1701 alignment
1702 table-env)
1703 (when org-e-latex-tables-centered "\n\\end{center}")
1704 (when float-env
1705 (concat (if org-e-latex-table-caption-above "" caption)
1706 (format "\n\\end{%s}" float-env))))))))
1708 (defun org-e-latex-table--align-string (attr info)
1709 "Return an appropriate LaTeX alignment string.
1710 ATTR is a string containing table's LaTeX specific attributes.
1711 INFO is the plist containing format info about the table, as
1712 returned by `org-export-table-format-info'."
1713 (or (and attr
1714 (string-match "\\<align=\\(\\S-+\\)" attr)
1715 (match-string 1 attr))
1716 (let* ((align (copy-sequence (plist-get info :alignment)))
1717 (colgroups (copy-sequence (plist-get info :column-groups)))
1718 (cols (length align))
1719 (separators (make-vector (1+ cols) "")))
1720 ;; Ignore the first column if it's special.
1721 (when (plist-get info :special-column-p)
1722 (aset align 0 "") (aset colgroups 0 nil))
1723 (let ((col 0))
1724 (mapc (lambda (el)
1725 (let ((gr (aref colgroups col)))
1726 (when (memq gr '(start start-end))
1727 (aset separators col "|"))
1728 (when (memq gr '(end start-end))
1729 (aset separators (1+ col) "|")))
1730 (incf col))
1731 align))
1732 ;; Build the LaTeX specific alignment string.
1733 (loop for al across align
1734 for sep across separators
1735 concat (concat sep al) into output
1736 finally return (concat output (aref separators cols))))))
1738 (defun org-e-latex-table (table contents info)
1739 "Transcode a TABLE element from Org to LaTeX.
1740 CONTENTS is nil. INFO is a plist holding contextual information."
1741 (let ((attr (mapconcat #'identity
1742 (org-element-get-property :attr_latex table)
1743 " "))
1744 (raw-table (org-element-get-property :raw-table table)))
1745 (cond
1746 ;; Case 1: verbatim table.
1747 ((or org-e-latex-tables-verbatim
1748 (and attr (string-match "\\<verbatim\\>" attr)))
1749 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1750 (org-export-clean-table
1751 raw-table
1752 (plist-get (org-export-table-format-info raw-table)
1753 :special-column-p))))
1754 ;; Case 2: table.el table. Convert it using appropriate tools.
1755 ((eq (org-element-get-property :type table) 'table.el)
1756 (require 'table)
1757 ;; Ensure "*org-export-table*" buffer is empty.
1758 (and (get-buffer "*org-export-table*")
1759 (kill-buffer (get-buffer "*org-export-table*")))
1760 (let ((output (with-temp-buffer
1761 (insert raw-table)
1762 (goto-char 1)
1763 (re-search-forward "^[ \t]*|[^|]" nil t)
1764 (table-generate-source 'latex "*org-export-table*")
1765 (with-current-buffer "*org-export-table*"
1766 (org-trim (buffer-string))))))
1767 (kill-buffer (get-buffer "*org-export-table*"))
1768 ;; Remove left out comments.
1769 (while (string-match "^%.*\n" output)
1770 (setq output (replace-match "" t t output)))
1771 ;; When the "rmlines" attribute is provided, remove all hlines
1772 ;; but the the one separating heading from the table body.
1773 (when (and attr (string-match "\\<rmlines\\>" attr))
1774 (let ((n 0) (pos 0))
1775 (while (and (< (length output) pos)
1776 (setq pos (string-match "^\\\\hline\n?" output pos)))
1777 (incf n)
1778 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
1779 (if org-e-latex-tables-centered
1780 (format "\\begin{center}\n%s\n\\end{center}" output)
1781 output)))
1782 ;; Case 3: Standard table.
1784 (let* ((table-info (org-export-table-format-info raw-table))
1785 (clean-table (org-export-clean-table
1786 raw-table (plist-get table-info :special-column-p)))
1787 (columns-number (length (plist-get table-info :alignment))))
1788 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
1789 ;; particular, send each cell to
1790 ;; `org-element-parse-secondary-string' to expand any Org
1791 ;; object within. Eventually, flesh the format string out with
1792 ;; the table.
1793 (format (org-e-latex-table--format-string table table-info)
1794 (orgtbl-to-latex
1795 (mapcar
1796 (lambda (row)
1797 (if (string-match org-table-hline-regexp row)
1798 'hline
1799 (mapcar
1800 (lambda (cell)
1801 (org-export-secondary-string
1802 (org-element-parse-secondary-string
1803 cell
1804 (cdr (assq 'table org-element-string-restrictions)))
1805 'e-latex info))
1806 (org-split-string row "[ \t]*|[ \t]*"))))
1807 (org-split-string clean-table "\n"))
1808 `(:tstart nil :tend nil
1809 ;; Longtable environment requires specific
1810 ;; header line end.
1811 :hlend ,(and attr
1812 (string-match "\\<longtable\\>" attr)
1813 (format "\\\\
1814 \\hline
1815 \\endhead
1816 \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
1817 \\endfoot
1818 \\endlastfoot"
1819 columns-number))))))))))
1822 ;;;; Target
1824 (defun org-e-latex-target (target text info)
1825 "Transcode a TARGET object from Org to LaTeX.
1826 TEXT is the text of the target. INFO is a plist holding
1827 contextual information."
1828 (format "\\label{%s}%s"
1829 (org-export-solidify-link-text
1830 (org-element-get-property :raw-value target))
1831 text))
1834 ;;;; Time-stamp
1836 (defun org-e-latex-time-stamp (time-stamp contents info)
1837 "Transcode a TIME-STAMP object from Org to LaTeX.
1838 CONTENTS is nil. INFO is a plist holding contextual
1839 information."
1840 (let ((value (org-element-get-property :value time-stamp))
1841 (type (org-element-get-property :type time-stamp))
1842 (appt-type (org-element-get-property :appt-type time-stamp)))
1843 (concat (cond ((eq appt-type 'scheduled)
1844 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
1845 ((eq appt-type 'deadline)
1846 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
1847 ((eq appt-type 'closed)
1848 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
1849 (cond ((memq type '(active active-range))
1850 (format org-e-latex-active-timestamp-format value))
1851 ((memq type '(inactive inactive-range))
1852 (format org-e-latex-inactive-timestamp-format value))
1854 (format org-e-latex-diary-timestamp-format value))))))
1857 ;;;; Verbatim
1859 (defun org-e-latex-verbatim (verbatim contents info)
1860 "Transcode a VERBATIM object from Org to LaTeX.
1861 CONTENTS is nil. INFO is a plist used as a communication
1862 channel."
1863 (let ((fmt (cdr (assoc (org-element-get-property :marker verbatim)
1864 org-e-latex-emphasis-alist)))
1865 (value (org-element-get-property :value verbatim)))
1866 (cond
1867 ;; Handle the `verb' special case.
1868 ((eq 'verb fmt)
1869 (let ((separator (org-e-latex--find-verb-separator value)))
1870 (concat "\\verb" separator value separator)))
1871 ;; Handle the `protectedtexttt' special case.
1872 ((eq 'protectedtexttt fmt)
1873 (let ((start 0)
1874 (trans '(("\\" . "\\textbackslash{}")
1875 ("~" . "\\textasciitilde{}")
1876 ("^" . "\\textasciicircum{}")))
1877 (rtn "")
1878 char)
1879 (while (string-match "[\\{}$%&_#~^]" value)
1880 (setq char (match-string 0 value))
1881 (if (> (match-beginning 0) 0)
1882 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
1883 (setq value (substring value (1+ (match-beginning 0))))
1884 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1885 rtn (concat rtn char)))
1886 (setq value (concat rtn value)
1887 fmt "\\texttt{%s}")
1888 (while (string-match "--" value)
1889 (setq value (replace-match "-{}-" t t value)))
1890 (format fmt value)))
1891 ;; Else use format string.
1892 (t (format fmt value)))))
1895 ;;;; Verse Block
1897 (defun org-e-latex-verse-block (verse-block contents info)
1898 "Transcode a VERSE-BLOCK element from Org to LaTeX.
1899 CONTENTS is nil. INFO is a plist holding contextual information."
1900 (org-e-latex--wrap-label
1901 verse-block
1902 ;; In a verse environment, add a line break to each newline
1903 ;; character and change each white space at beginning of a line
1904 ;; into a space of 1 em. Also change each blank line with
1905 ;; a vertical space of 1 em.
1906 (progn
1907 (setq contents (replace-regexp-in-string
1908 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1909 (replace-regexp-in-string
1910 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1911 (org-remove-indentation
1912 (org-export-secondary-string
1913 (org-element-get-property :value verse-block)
1914 'e-latex info)))))
1915 (while (string-match "^[ \t]+" contents)
1916 (let ((new-str (format "\\hspace*{%dem}"
1917 (length (match-string 0 contents)))))
1918 (setq contents (replace-match new-str nil t contents))))
1919 (format "\\begin{verse}\n%s\\end{verse}" contents))))
1923 ;;; Interactive functions
1925 (defun org-e-latex-export-to-latex
1926 (&optional subtreep visible-only body-only ext-plist pub-dir)
1927 "Export current buffer to a LaTeX file.
1929 If narrowing is active in the current buffer, only export its
1930 narrowed part.
1932 If a region is active, export that region.
1934 When optional argument SUBTREEP is non-nil, export the sub-tree
1935 at point, extracting information from the headline properties
1936 first.
1938 When optional argument VISIBLE-ONLY is non-nil, don't export
1939 contents of hidden elements.
1941 When optional argument BODY-ONLY is non-nil, only write code
1942 between \"\\begin{document}\" and \"\\end{document}\".
1944 EXT-PLIST, when provided, is a property list with external
1945 parameters overriding Org default settings, but still inferior to
1946 file-local settings.
1948 When optional argument PUB-DIR is set, use it as the publishing
1949 directory.
1951 Return output file's name."
1952 (interactive)
1953 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
1954 (org-export-to-file
1955 'e-latex outfile subtreep visible-only body-only ext-plist)))
1957 (defun org-e-latex-export-to-pdf
1958 (&optional subtreep visible-only body-only ext-plist pub-dir)
1959 "Export current buffer to LaTeX then process through to PDF.
1961 If narrowing is active in the current buffer, only export its
1962 narrowed part.
1964 If a region is active, export that region.
1966 When optional argument SUBTREEP is non-nil, export the sub-tree
1967 at point, extracting information from the headline properties
1968 first.
1970 When optional argument VISIBLE-ONLY is non-nil, don't export
1971 contents of hidden elements.
1973 When optional argument BODY-ONLY is non-nil, only write code
1974 between \"\\begin{document}\" and \"\\end{document}\".
1976 EXT-PLIST, when provided, is a property list with external
1977 parameters overriding Org default settings, but still inferior to
1978 file-local settings.
1980 When optional argument PUB-DIR is set, use it as the publishing
1981 directory.
1983 Return PDF file's name."
1984 (interactive)
1985 (org-e-latex-compile
1986 (org-e-latex-export-to-latex
1987 subtreep visible-only body-only ext-plist pub-dir)))
1989 (defun org-e-latex-compile (texfile)
1990 "Compile a TeX file.
1992 TEXFILE is the name of the file being compiled. Processing is
1993 done through the command specified in `org-e-latex-pdf-process'.
1995 Return PDF file name or an error if it couldn't be produced."
1996 (let* ((wconfig (current-window-configuration))
1997 (texfile (file-truename texfile))
1998 (base (file-name-sans-extension texfile))
1999 errors)
2000 (message (format "Processing LaTeX file " texfile "..." texfile))
2001 (unwind-protect
2002 (progn
2003 (cond
2004 ;; A function is provided: Apply it.
2005 ((functionp org-latex-to-pdf-process)
2006 (funcall org-latex-to-pdf-process (shell-quote-argument texfile)))
2007 ;; A list is provided: Replace %b, %f and %o with appropriate
2008 ;; values in each command before applying it. Output is
2009 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2010 ((consp org-e-latex-pdf-process)
2011 (let* ((out-dir (or (file-name-directory texfile) "./"))
2012 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2013 (mapc
2014 (lambda (command)
2015 (shell-command
2016 (replace-regexp-in-string
2017 "%b" (shell-quote-argument base)
2018 (replace-regexp-in-string
2019 "%f" (shell-quote-argument texfile)
2020 (replace-regexp-in-string
2021 "%o" (shell-quote-argument out-dir) command)))
2022 outbuf))
2023 org-e-latex-pdf-process)
2024 ;; Collect standard errors from output buffer.
2025 (setq errors (org-e-latex-collect-errors outbuf))))
2026 (t (error "No valid command to process to PDF")))
2027 (let ((pdffile (concat base ".pdf")))
2028 ;; Check for process failure. Provide collected errors if
2029 ;; possible.
2030 (if (not (file-exists-p pdffile))
2031 (error (concat (format "PDF file %s wasn't produced" pdffile)
2032 (when errors (concat ": " errors))))
2033 ;; Else remove log files, when specified, and signal end of
2034 ;; process to user, along with any error encountered.
2035 (when org-e-latex-remove-logfiles
2036 (dolist (ext org-e-latex-logfiles-extensions)
2037 (let ((file (concat base "." ext)))
2038 (when (file-exists-p file) (delete-file file)))))
2039 (message (concat "Process completed"
2040 (if (not errors) "."
2041 (concat " with errors: " errors)))))
2042 ;; Return output file name.
2043 pdffile))
2044 (set-window-configuration wconfig))))
2046 (defun org-e-latex-collect-errors (buffer)
2047 "Collect some kind of errors from \"pdflatex\" command output.
2049 BUFFER is the buffer containing output.
2051 Return collected error types as a string, or nil if there was
2052 none."
2053 (with-current-buffer buffer
2054 (save-excursion
2055 (goto-char (point-max))
2056 ;; Find final "pdflatex" run.
2057 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2058 (let ((case-fold-search t)
2059 (errors ""))
2060 (when (save-excursion
2061 (re-search-forward "Reference.*?undefined" nil t))
2062 (setq errors (concat errors " [undefined reference]")))
2063 (when (save-excursion
2064 (re-search-forward "Citation.*?undefined" nil t))
2065 (setq errors (concat errors " [undefined citation]")))
2066 (when (save-excursion
2067 (re-search-forward "Undefined control sequence" nil t))
2068 (setq errors (concat errors " [undefined control sequence]")))
2069 (when (save-excursion
2070 (re-search-forward "^! LaTeX.*?Error" nil t))
2071 (setq errors (concat errors " [LaTeX error]")))
2072 (when (save-excursion
2073 (re-search-forward "^! Package.*?Error" nil t))
2074 (setq errors (concat errors " [package error]")))
2075 (and (org-string-nw-p errors) (org-trim errors)))))))
2078 (provide 'org-e-latex)
2079 ;;; org-e-latex.el ends here