Fix typos and spelling mistakes.
[org-mode/org-kjn.git] / EXPERIMENTAL / org-e-latex.el
blob6f7538f8efdbb134ba1c2493cd312c34bb30e472
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-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-default \(todo todo-type priority text tags\)
236 \"Default format function for an headline.\"
237 \(concat \(when todo \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
238 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
239 text
240 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
241 :group 'org-export-e-latex
242 :type 'function)
245 ;;;; Emphasis
247 (defcustom org-e-latex-emphasis-alist
248 '(("*" . "\\textbf{%s}")
249 ("/" . "\\emph{%s}")
250 ("_" . "\\underline{%s}")
251 ("+" . "\\st{%s}")
252 ("=" . protectedtexttt)
253 ("~" . verb))
254 "Alist of LaTeX expressions to convert emphasis fontifiers.
256 The key is the character used as a marker for fontification. The
257 value is a formatting string to wrap fontified text with.
259 Value can also be set to the following symbols: `verb' and
260 `protectedtexttt'. For the former, Org will use \"\\verb\" to
261 create a format string and select a delimiter character that
262 isn't in the string. For the latter, Org will use \"\\texttt\"
263 to typeset and try to protect special characters."
264 :group 'org-export-e-latex
265 :type 'alist)
268 ;;;; Footnotes
270 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
271 "Text used to separate footnotes."
272 :group 'org-export-e-latex
273 :type 'string)
276 ;;;; Time-stamps
278 (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
279 "A printf format string to be applied to active time-stamps."
280 :group 'org-export-e-latex
281 :type 'string)
283 (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
284 "A printf format string to be applied to inactive time-stamps."
285 :group 'org-export-e-latex
286 :type 'string)
288 (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
289 "A printf format string to be applied to diary time-stamps."
290 :group 'org-export-e-latex
291 :type 'string)
294 ;;;; Links
296 (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
297 "Default option for images."
298 :group 'org-export-e-latex
299 :type 'string)
301 (defcustom org-e-latex-default-figure-position "htb"
302 "Default position for latex figures."
303 :group 'org-export-e-latex
304 :type 'string)
306 (defcustom org-e-latex-inline-image-extensions
307 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
308 "Extensions of image files that can be inlined into LaTeX.
310 Note that the image extension *actually* allowed depend on the
311 way the LaTeX file is processed. When used with pdflatex, pdf,
312 jpg and png images are OK. When processing through dvi to
313 Postscript, only ps and eps are allowed. The default we use here
314 encompasses both."
315 :group 'org-export-e-latex
316 :type '(repeat (string :tag "Extension")))
319 ;;;; Tables
321 (defcustom org-e-latex-default-table-environment "tabular"
322 "Default environment used to build tables."
323 :group 'org-export-e-latex
324 :type 'string)
326 (defcustom org-e-latex-tables-centered t
327 "When non-nil, tables are exported in a center environment."
328 :group 'org-export-e-latex
329 :type 'boolean)
331 (defcustom org-e-latex-tables-verbatim nil
332 "When non-nil, tables are exported verbatim."
333 :group 'org-export-e-latex
334 :type 'boolean)
336 (defcustom org-e-latex-table-caption-above t
337 "When non-nil, place caption string at the beginning of the table.
338 Otherwise, place it near the end."
339 :group 'org-export-e-latex
340 :type 'boolean)
343 ;;;; Drawers
345 (defcustom org-e-latex-format-drawer-function nil
346 "Function called to format a drawer in LaTeX code.
348 The function must accept two parameters:
349 NAME the drawer name, like \"LOGBOOK\"
350 CONTENTS the contents of the drawer.
352 The function should return the string to be exported.
354 For example, the variable could be set to the following function
355 in order to mimic default behaviour:
357 \(defun org-e-latex-format-drawer-default \(name contents\)
358 \"Format a drawer element for LaTeX export.\"
359 contents\)"
360 :group 'org-export-e-latex
361 :type 'function)
364 ;;;; Inlinetasks
366 (defcustom org-e-latex-format-inlinetask-function nil
367 "Function called to format an inlinetask in LaTeX code.
369 The function must accept six parameters:
370 TODO the todo keyword, as a string
371 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
372 PRIORITY the inlinetask priority, as a string
373 NAME the inlinetask name, as a string.
374 TAGS the inlinetask tags, as a string.
375 CONTENTS the contents of the inlinetask, as a string.
377 The function should return the string to be exported.
379 For example, the variable could be set to the following function
380 in order to mimic default behaviour:
382 \(defun org-e-latex-format-inlinetask-default \(todo type priority name tags contents\)
383 \"Format an inline task element for LaTeX export.\"
384 \(let \(\(full-title
385 \(concat
386 \(when todo \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
387 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
388 title
389 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
390 \(format \(concat \"\\\\begin{center}\\n\"
391 \"\\\\fbox{\\n\"
392 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
393 \"%s\\n\\n\"
394 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
395 \"%s\"
396 \"\\\\end{minipage}}\"
397 \"\\\\end{center}\"\)
398 full-title contents\)\)"
399 :group 'org-export-e-latex
400 :type 'function)
403 ;; Src blocks
405 (defcustom org-e-latex-listings nil
406 "Non-nil means export source code using the listings package.
407 This package will fontify source code, possibly even with color.
408 If you want to use this, you also need to make LaTeX use the
409 listings package, and if you want to have color, the color
410 package. Just add these to `org-e-latex-packages-alist',
411 for example using customize, or with something like
413 (require 'org-e-latex)
414 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
415 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))
417 Alternatively,
419 (setq org-e-latex-listings 'minted)
421 causes source code to be exported using the minted package as
422 opposed to listings. If you want to use minted, you need to add
423 the minted package to `org-e-latex-packages-alist', for
424 example using customize, or with
426 (require 'org-e-latex)
427 (add-to-list 'org-e-latex-packages-alist '(\"\" \"minted\"))
429 In addition, it is necessary to install
430 pygments (http://pygments.org), and to configure the variable
431 `org-e-latex-to-pdf-process' so that the -shell-escape option is
432 passed to pdflatex."
433 :group 'org-export-e-latex
434 :type '(choice
435 (const :tag "Use listings" t)
436 (const :tag "Use minted" 'minted)
437 (const :tag "Export verbatim" nil)))
439 (defcustom org-e-latex-listings-langs
440 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
441 (c "C") (cc "C++")
442 (fortran "fortran")
443 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
444 (html "HTML") (xml "XML")
445 (tex "TeX") (latex "TeX")
446 (shell-script "bash")
447 (gnuplot "Gnuplot")
448 (ocaml "Caml") (caml "Caml")
449 (sql "SQL") (sqlite "sql"))
450 "Alist mapping languages to their listing language counterpart.
451 The key is a symbol, the major mode symbol without the \"-mode\".
452 The value is the string that should be inserted as the language parameter
453 for the listings package. If the mode name and the listings name are
454 the same, the language does not need an entry in this list - but it does not
455 hurt if it is present."
456 :group 'org-export-e-latex
457 :type '(repeat
458 (list
459 (symbol :tag "Major mode ")
460 (string :tag "Listings language"))))
462 (defcustom org-e-latex-listings-options nil
463 "Association list of options for the latex listings package.
465 These options are supplied as a comma-separated list to the
466 \\lstset command. Each element of the association list should be
467 a list containing two strings: the name of the option, and the
468 value. For example,
470 (setq org-export-latex-listings-options
471 '((\"basicstyle\" \"\\small\")
472 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
474 will typeset the code in a small size font with underlined, bold
475 black keywords.
477 Note that the same options will be applied to blocks of all
478 languages."
479 :group 'org-export-e-latex
480 :type '(repeat
481 (list
482 (string :tag "Listings option name ")
483 (string :tag "Listings option value"))))
485 (defcustom org-e-latex-minted-langs
486 '((emacs-lisp "common-lisp")
487 (cc "c++")
488 (cperl "perl")
489 (shell-script "bash")
490 (caml "ocaml"))
491 "Alist mapping languages to their minted language counterpart.
492 The key is a symbol, the major mode symbol without the \"-mode\".
493 The value is the string that should be inserted as the language parameter
494 for the minted package. If the mode name and the listings name are
495 the same, the language does not need an entry in this list - but it does not
496 hurt if it is present.
498 Note that minted uses all lower case for language identifiers,
499 and that the full list of language identifiers can be obtained
500 with:
501 pygmentize -L lexers"
502 :group 'org-export-e-latex
503 :type '(repeat
504 (list
505 (symbol :tag "Major mode ")
506 (string :tag "Minted language"))))
508 (defcustom org-e-latex-minted-options nil
509 "Association list of options for the latex minted package.
511 These options are supplied within square brackets in
512 \\begin{minted} environments. Each element of the alist should be
513 a list containing two strings: the name of the option, and the
514 value. For example,
516 (setq org-export-latex-minted-options
517 '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
519 will result in src blocks being exported with
521 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
523 as the start of the minted environment. Note that the same
524 options will be applied to blocks of all languages."
525 :group 'org-export-e-latex
526 :type '(repeat
527 (list
528 (string :tag "Minted option name ")
529 (string :tag "Minted option value"))))
531 (defvar org-e-latex-custom-lang-environments nil
532 "Association list mapping languages to language-specific latex
533 environments used during export of src blocks by the listings and
534 minted latex packages. For example,
536 (setq org-export-latex-custom-lang-environments
537 '((python \"pythoncode\")))
539 would have the effect that if org encounters begin_src python
540 during latex export it will output
542 \\begin{pythoncode}
543 <src block body>
544 \\end{pythoncode}")
547 ;;;; Plain text
549 (defcustom org-e-latex-quotes
550 '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
551 ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
552 "Alist for quotes to use when converting english double-quotes.
554 The CAR of each item in this alist is the language code.
555 The CDR of each item in this alist is a list of three CONS:
556 - the first CONS defines the opening quote;
557 - the second CONS defines the closing quote;
558 - the last CONS defines single quotes.
560 For each item in a CONS, the first string is a regexp
561 for allowed characters before/after the quote, the second
562 string defines the replacement string for this quote."
563 :group 'org-export-e-latex
564 :type '(list
565 (cons :tag "Opening quote"
566 (string :tag "Regexp for char before")
567 (string :tag "Replacement quote "))
568 (cons :tag "Closing quote"
569 (string :tag "Regexp for char after ")
570 (string :tag "Replacement quote "))
571 (cons :tag "Single quote"
572 (string :tag "Regexp for char before")
573 (string :tag "Replacement quote "))))
576 ;;;; Compilation
578 (defcustom org-e-latex-pdf-process
579 '("pdflatex -interaction nonstopmode -output-directory %o %f"
580 "pdflatex -interaction nonstopmode -output-directory %o %f"
581 "pdflatex -interaction nonstopmode -output-directory %o %f")
582 "Commands to process a LaTeX file to a PDF file.
583 This is a list of strings, each of them will be given to the shell
584 as a command. %f in the command will be replaced by the full file name, %b
585 by the file base name (i.e. without extension) and %o by the base directory
586 of the file.
588 The reason why this is a list is that it usually takes several runs of
589 `pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever
590 mechanism to detect which of these commands have to be run to get to a stable
591 result, and it also does not do any error checking.
593 By default, Org uses 3 runs of `pdflatex' to do the processing. If you
594 have texi2dvi on your system and if that does not cause the infamous
595 egrep/locale bug:
597 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
599 then `texi2dvi' is the superior choice. Org does offer it as one
600 of the customize options.
602 Alternatively, this may be a Lisp function that does the processing, so you
603 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
604 This function should accept the file name as its single argument."
605 :group 'org-export-pdf
606 :type '(choice
607 (repeat :tag "Shell command sequence"
608 (string :tag "Shell command"))
609 (const :tag "2 runs of pdflatex"
610 ("pdflatex -interaction nonstopmode -output-directory %o %f"
611 "pdflatex -interaction nonstopmode -output-directory %o %f"))
612 (const :tag "3 runs of pdflatex"
613 ("pdflatex -interaction nonstopmode -output-directory %o %f"
614 "pdflatex -interaction nonstopmode -output-directory %o %f"
615 "pdflatex -interaction nonstopmode -output-directory %o %f"))
616 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
617 ("pdflatex -interaction nonstopmode -output-directory %o %f"
618 "bibtex %b"
619 "pdflatex -interaction nonstopmode -output-directory %o %f"
620 "pdflatex -interaction nonstopmode -output-directory %o %f"))
621 (const :tag "texi2dvi"
622 ("texi2dvi -p -b -c -V %f"))
623 (const :tag "rubber"
624 ("rubber -d --into %o %f"))
625 (function)))
627 (defcustom org-e-latex-logfiles-extensions
628 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
629 "The list of file extensions to consider as LaTeX logfiles."
630 :group 'org-export-e-latex
631 :type '(repeat (string :tag "Extension")))
633 (defcustom org-e-latex-remove-logfiles t
634 "Non-nil means remove the logfiles produced by PDF production.
635 These are the .aux, .log, .out, and .toc files."
636 :group 'org-export-e-latex
637 :type 'boolean)
641 ;;; Internal Functions
643 (defun org-e-latex--caption/label-string (caption label info)
644 "Return caption and label LaTeX string for floats.
646 CAPTION is a secondary string \(a list of strings and Org
647 objects\) and LABEL a string representing the label. INFO is
648 a plist holding contextual information.
650 If there's no caption nor label, return the empty string.
652 For non-floats, see `org-e-latex--wrap-label'."
653 (let ((caption-str (and caption
654 (org-export-secondary-string
655 caption 'e-latex info)))
656 (label-str (if label (format "\\label{%s}" label) "")))
657 (cond
658 ((and (not caption-str) (not label)) "")
659 ((not caption-str) (format "\\label{%s}\n" label))
660 ;; Option caption format with short name.
661 ((string-match "\\[\\([^][]*\\)\\]{\\([^{}]*\\)}" caption-str)
662 (format "\\caption[%s]{%s%s}\n"
663 (org-match-string-no-properties 1 caption-str)
664 label-str
665 (org-match-string-no-properties 2 caption-str)))
666 ;; Standard caption format.
667 (t (format "\\caption{%s%s}\n" label-str caption-str)))))
669 (defun org-e-latex--guess-inputenc (header)
670 "Set the coding system in inputenc to what the buffer is.
672 HEADER is the LaTeX header string.
674 Return the new header."
675 (let* ((cs (or (ignore-errors
676 (latexenc-coding-system-to-inputenc
677 buffer-file-coding-system))
678 "utf8")))
679 (if (not cs)
680 header
681 ;; First translate if that is requested.
682 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
683 ;; Then find the \usepackage statement and replace the option.
684 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
685 cs header t nil 1))))
687 (defun org-e-latex--find-verb-separator (s)
688 "Return a character not used in string S.
689 This is used to choose a separator for constructs like \\verb."
690 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
691 (loop for c across ll
692 when (not (string-match (regexp-quote (char-to-string c)) s))
693 return (char-to-string c))))
695 (defun org-e-latex--make-option-string (options)
696 "Return a comma separated string of keywords and values.
697 OPTIONS is an alist where the key is the options keyword as
698 a string, and the value a list containing the keyword value, or
699 nil."
700 (mapconcat (lambda (pair)
701 (concat (first pair)
702 (when (> (length (second pair)) 0)
703 (concat "=" (second pair)))))
704 options
705 ","))
707 (defun org-e-latex--quotation-marks (text info)
708 "Export quotation marks depending on language conventions."
709 (mapc (lambda(l)
710 (let ((start 0))
711 (while (setq start (string-match (car l) text start))
712 (let ((new-quote (concat (match-string 1 text) (cdr l))))
713 (setq text (replace-match new-quote t t text))))))
714 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
715 ;; Falls back on English.
716 (assoc "en" org-e-latex-quotes))))
717 text)
719 (defun org-e-latex--wrap-label (element output)
720 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
721 This function shouldn't be used for floats. See
722 `org-e-latex--caption/label-string'."
723 (let ((label (org-element-get-property :name element)))
724 (if (or (not output) (not label) (string= output "") (string= label ""))
725 output
726 (concat (format "\\label{%s}\n" label) output))))
730 ;;; Template
732 (defun org-e-latex-template (contents info)
733 "Return complete document string after LaTeX conversion.
734 CONTENTS is the transcoded contents string. INFO is a plist
735 holding export options."
736 (let ((title (org-export-secondary-string
737 (plist-get info :title) 'e-latex info)))
738 (concat
739 ;; 1. Time-stamp.
740 (and (plist-get info :time-stamp-file)
741 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
742 ;; 2. Document class and packages.
743 (let ((class (plist-get info :latex-class))
744 (class-options (plist-get info :latex-class-options)))
745 (org-element-normalize-string
746 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
747 (document-class-string
748 (and (stringp header)
749 (if class-options
750 (replace-regexp-in-string
751 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
752 class-options header t nil 1)
753 header))))
754 (org-e-latex--guess-inputenc
755 (org-splice-latex-header
756 document-class-string
757 org-export-latex-default-packages-alist ; defined in org.el
758 org-export-latex-packages-alist nil ; defined in org.el
759 (plist-get info :latex-header-extra))))))
760 ;; 3. Define alert if not yet defined.
761 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
762 ;; 4. Author.
763 (let ((author (and (plist-get info :with-author)
764 (let ((auth (plist-get info :author)))
765 (and auth (org-export-secondary-string
766 auth 'e-latex info)))))
767 (email (and (plist-get info :with-email)
768 (org-export-secondary-string
769 (plist-get info :email) 'e-latex info))))
770 (cond ((and author email (not (string= "" email)))
771 (format "\\author{%s\\thanks{%s}}\n" author email))
772 (author (format "\\author{%s}\n" author))
773 (t "\\author{}\n")))
774 ;; 5. Date.
775 (let ((date (plist-get info :date)))
776 (and date (format "\\date{%s}\n" date)))
777 ;; 6. Title
778 (format "\\title{%s}\n" title)
779 ;; 7. Hyperref options.
780 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
781 (or (plist-get info :keywords) "")
782 (or (plist-get info :description) "")
783 (let ((creator-info (plist-get info :with-creator)))
784 (cond
785 ((not creator-info) "")
786 ((eq creator-info 'comment) "")
787 (t (plist-get info :creator)))))
788 ;; 7. Document start.
789 "\\begin{document}\n\n"
790 ;; 8. Title command.
791 (org-element-normalize-string
792 (cond ((string= "" title) nil)
793 ((not (stringp org-e-latex-title-command)) nil)
794 ((string-match "\\(?:[^%]\\|^\\)%s"
795 org-e-latex-title-command)
796 (format org-e-latex-title-command title))
797 (t org-e-latex-title-command)))
798 ;; 9. Table of contents.
799 (let ((depth (plist-get info :with-toc)))
800 (when depth
801 (concat (when (wholenump depth)
802 (format "\\setcounter{tocdepth}{%d}\n" depth))
803 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
804 ;; 10. Document's body.
805 contents
806 ;; 11. Creator.
807 (let ((creator-info (plist-get info :with-creator)))
808 (cond
809 ((not creator-info))
810 ((eq creator-info 'comment)
811 (format "%% %s\n" (plist-get info :creator)))
812 (t (concat (plist-get info :creator) "\n"))))
813 ;; 12. Document end.
814 "\\end{document}")))
818 ;;; Transcode Functions
820 ;;;; Block
822 (defun org-e-latex-center-block (center-block contents info)
823 "Transcode a CENTER-BLOCK element from Org to LaTeX.
824 CONTENTS holds the contents of the block. INFO is a plist
825 holding contextual information."
826 (org-e-latex--wrap-label
827 center-block
828 (format "\\begin{center}\n%s\\end{center}" contents)))
831 ;;;; Comment
833 ;; Comments are ignored.
836 ;;;; Comment Block
838 ;; Comment Blocks are ignored.
841 ;;;; Drawer
843 (defun org-e-latex-drawer (drawer contents info)
844 "Transcode a DRAWER element from Org to LaTeX.
845 CONTENTS holds the contents of the block. INFO is a plist
846 holding contextual information."
847 (let* ((name (org-element-get-property :drawer-name drawer))
848 (output (if (functionp org-e-latex-format-drawer-function)
849 (funcall org-e-latex-format-drawer-function
850 name contents)
851 ;; If there's no user defined function: simply
852 ;; display contents of the drawer.
853 contents)))
854 (org-e-latex--wrap-label drawer output)))
857 ;;;; Dynamic Block
859 (defun org-e-latex-dynamic-block (dynamic-block contents info)
860 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
861 CONTENTS holds the contents of the block. INFO is a plist
862 holding contextual information. See
863 `org-export-data'."
864 (org-e-latex--wrap-label dynamic-block contents))
867 ;;;; Emphasis
869 (defun org-e-latex-emphasis (emphasis contents info)
870 "Transcode EMPHASIS from Org to LaTeX.
871 CONTENTS is the contents of the emphasized text. INFO is a plist
872 holding contextual information.."
873 (format (cdr (assoc (org-element-get-property :marker emphasis)
874 org-e-latex-emphasis-alist))
875 contents))
878 ;;;; Entity
880 (defun org-e-latex-entity (entity contents info)
881 "Transcode an ENTITY object from Org to LaTeX.
882 CONTENTS are the definition itself. INFO is a plist holding
883 contextual information."
884 (let ((ent (org-element-get-property :latex entity)))
885 (if (org-element-get-property :latex-math-p entity)
886 (format "$%s$" ent)
887 ent)))
890 ;;;; Example Block
892 (defun org-e-latex-example-block (example-block contents info)
893 "Transcode a EXAMPLE-BLOCK element from Org to LaTeX.
894 CONTENTS is nil. INFO is a plist holding contextual information."
895 (let* ((options (or (org-element-get-property :options example-block) ""))
896 (value (org-export-handle-code
897 (org-element-get-property :value example-block) options info)))
898 (org-e-latex--wrap-label
899 example-block (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
902 ;;;; Export Snippet
904 (defun org-e-latex-export-snippet (export-snippet contents info)
905 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
906 CONTENTS is nil. INFO is a plist holding contextual information."
907 (org-element-get-property :value export-snippet))
910 ;;;; Export Block
912 (defun org-e-latex-export-block (export-block contents info)
913 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
914 CONTENTS is nil. INFO is a plist holding contextual information."
915 (when (string= (org-element-get-property :type export-block) "latex")
916 (org-remove-indentation (org-element-get-property :value export-block))))
919 ;;;; Fixed Width
921 (defun org-e-latex-fixed-width (fixed-width contents info)
922 "Transcode a FIXED-WIDTH element from Org to LaTeX.
923 CONTENTS is nil. INFO is a plist holding contextual information."
924 (let* ((value (org-element-normalize-string
925 (replace-regexp-in-string
926 "^[ \t]*: ?" ""
927 (org-element-get-property :value fixed-width)))))
928 (org-e-latex--wrap-label
929 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
932 ;;;; Footnote Definition
934 ;; Footnote Definitions are ignored.
937 ;;;; Footnote Reference
939 (defun org-e-latex-footnote-reference (footnote-reference contents info)
940 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
941 CONTENTS is nil. INFO is a plist holding contextual information."
942 (concat
943 ;; Insert separator between two footnotes in a row.
944 (when (eq (plist-get info :previous-object) 'footnote-reference)
945 org-e-latex-footnote-separator)
946 ;; Use \footnotemark if the footnote has already been defined.
947 ;; Otherwise, define it with \footnote command.
948 (cond
949 ((not (org-export-footnote-first-reference-p footnote-reference info))
950 (format "\\footnotemark[%s]"
951 (org-export-get-footnote-number footnote-reference info)))
952 ;; Inline definitions are secondary strings.
953 ((eq (org-element-get-property :type footnote-reference) 'inline)
954 (format "\\footnote{%s}"
955 (org-trim
956 (org-export-secondary-string
957 (org-export-get-footnote-definition footnote-reference info)
958 'e-latex info))))
959 ;; Non-inline footnotes definitions are full Org data.
961 (format "\\footnote{%s}"
962 (org-trim
963 (org-export-data
964 (org-export-get-footnote-definition footnote-reference info)
965 'e-latex info)))))))
968 ;;;; Headline
970 (defun org-e-latex-headline (headline contents info)
971 "Transcode an HEADLINE element from Org to LaTeX.
972 CONTENTS holds the contents of the headline. INFO is a plist
973 holding contextual information."
974 (let* ((class (plist-get info :latex-class))
975 (numberedp (plist-get info :section-numbers))
976 ;; Get level relative to current parsed data.
977 (level (+ (org-element-get-property :level headline)
978 (plist-get info :headline-offset)))
979 (class-sectionning (assoc class org-e-latex-classes))
980 ;; Section formatting will set two placeholders: one for the
981 ;; title and the other for the contents.
982 (section-fmt
983 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
984 (fboundp (nth 2 class-sectionning)))
985 (funcall (nth 2 class-sectionning) level numberedp)
986 (nth (1+ level) class-sectionning))))
987 (cond
988 ;; No section available for that LEVEL.
989 ((not sec) nil)
990 ;; Section format directly returned by a function.
991 ((stringp sec) sec)
992 ;; (numbered-section . unnumbered-section)
993 ((not (consp (cdr sec)))
994 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
995 ;; (numbered-open numbered-close)
996 ((= (length sec) 2)
997 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
998 ;; (num-in num-out no-num-in no-num-out)
999 ((= (length sec) 4)
1000 (if numberedp
1001 (concat (car sec) "\n%s" (nth 1 sec))
1002 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1003 (text (org-export-secondary-string
1004 (org-element-get-property :title headline) 'e-latex info))
1005 (todo (and (plist-get info :with-todo-keywords)
1006 (let ((todo (org-element-get-property
1007 :todo-keyword headline)))
1008 (and todo
1009 (org-export-secondary-string todo 'e-latex info)))))
1010 (todo-type (and todo (org-element-get-property :todo-type headline)))
1011 (tags (and (plist-get info :with-tags)
1012 (org-element-get-property :tags headline)))
1013 (priority (and (plist-get info :with-priority)
1014 (org-element-get-property :priority headline)))
1015 ;; Create the headline text.
1016 (full-text (if (functionp org-e-latex-format-headline-function)
1017 ;; User-defined formatting function.
1018 (funcall org-e-latex-format-headline-function
1019 todo todo-type priority text tags)
1020 ;; Default formatting.
1021 (concat
1022 (when todo
1023 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1024 (when priority (format "\\framebox{\\#%c} " priority))
1025 text
1026 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1027 ;; Associate some \label to the headline for internal links.
1028 (headline-labels (mapconcat
1029 (lambda (p)
1030 (let ((val (org-element-get-property p headline)))
1031 (when val (format "\\label{%s}\n"
1032 (if (eq p :begin)
1033 (format "headline-%s" val)
1034 val)))))
1035 '(:custom-id :id :begin) ""))
1036 (pre-blanks (make-string (org-element-get-property :pre-blank headline)
1037 10)))
1038 (cond
1039 ;; Case 1: This is a footnote section: ignore it.
1040 ((org-element-get-property :footnote-section-p headline) nil)
1041 ;; Case 2. This is a deep sub-tree: export it as a list item.
1042 ;; Also export as items headlines for which no section
1043 ;; format has been found.
1044 ((or (not section-fmt)
1045 (and (wholenump (plist-get info :headline-levels))
1046 (> level (plist-get info :headline-levels))))
1047 ;; Build the real contents of the sub-tree.
1048 (let ((low-level-body
1049 (concat
1050 ;; If the headline is the first sibling, start a list.
1051 (when (org-export-first-sibling-p headline info)
1052 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1053 ;; Itemize headline
1054 "\\item " full-text "\n" headline-labels pre-blanks contents)))
1055 ;; If headline in the last sibling, close the list, before any
1056 ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
1057 (if (org-export-last-sibling-p headline info)
1058 (replace-regexp-in-string
1059 "[ \t\n]*\\'"
1060 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1061 low-level-body)
1062 low-level-body)))
1063 ;; Case 3. Standard headline. Export it as a section.
1064 (t (format section-fmt full-text
1065 (concat headline-labels pre-blanks contents))))))
1068 ;;;; Horizontal Rule
1070 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1071 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1072 CONTENTS is nil. INFO is a plist holding contextual information."
1073 (let ((attr (mapconcat #'identity
1074 (org-element-get-property :attr_latex horizontal-rule)
1075 " ")))
1076 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1079 ;;;; Inline Babel Call
1081 ;; Inline Babel Calls are ignored.
1084 ;;;; Inline Src Block
1086 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1087 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1088 CONTENTS holds the contents of the item. INFO is a plist holding
1089 contextual information."
1090 (let* ((code (org-element-get-property :value inline-src-block))
1091 (separator (org-e-latex--find-verb-separator code)))
1092 (cond
1093 ;; Do not use a special package: transcode it verbatim.
1094 ((not org-e-latex-listings)
1095 (concat "\\verb" separator code separator))
1096 ;; Use minted package.
1097 ((eq org-e-latex-listings 'minted)
1098 (let* ((org-lang (org-element-get-property :language inline-src-block))
1099 (mint-lang (or (cadr (assq (intern org-lang)
1100 org-e-latex-minted-langs))
1101 org-lang))
1102 (options (org-e-latex--make-option-string
1103 org-e-latex-minted-options)))
1104 (concat (format "\\mint%s{%s}"
1105 (if (string= options "") "" (format "[%s]" options))
1106 mint-lang)
1107 separator code separator)))
1108 ;; Use listings package.
1110 ;; Maybe translate language's name.
1111 (let* ((org-lang (org-element-get-property :language inline-src-block))
1112 (lst-lang (or (cadr (assq (intern org-lang)
1113 org-e-latex-listings-langs))
1114 org-lang))
1115 (options (org-e-latex--make-option-string
1116 (append org-e-latex-listings-options
1117 `(("language" ,lst-lang))))))
1118 (concat (format "\\lstinline[%s]" options)
1119 separator code separator))))))
1122 ;;;; Inlinetask
1124 (defun org-e-latex-inlinetask (inlinetask contents info)
1125 "Transcode an INLINETASK element from Org to LaTeX.
1126 CONTENTS holds the contents of the block. INFO is a plist
1127 holding contextual information."
1128 (let ((title (org-export-secondary-string
1129 (org-element-get-property :title inlinetask) 'e-latex info))
1130 (todo (and (plist-get info :with-todo-keywords)
1131 (let ((todo (org-element-get-property
1132 :todo-keyword inlinetask)))
1133 (and todo
1134 (org-export-secondary-string todo 'e-latex info)))))
1135 (todo-type (org-element-get-property :todo-type inlinetask))
1136 (tags (and (plist-get info :with-tags)
1137 (org-element-get-property :tags inlinetask)))
1138 (priority (and (plist-get info :with-priority)
1139 (org-element-get-property :priority inlinetask))))
1140 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1141 ;; with appropriate arguments.
1142 (if (functionp org-e-latex-format-inlinetask-function)
1143 (funcall org-e-latex-format-inlinetask-function
1144 todo todo-type priority title tags contents)
1145 ;; Otherwise, use a default template.
1146 (org-e-latex--wrap-label
1147 inlinetask
1148 (let ((full-title
1149 (concat
1150 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1151 (when priority (format "\\framebox{\\#%c} " priority))
1152 title
1153 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1154 (format (concat "\\begin{center}\n"
1155 "\\fbox{\n"
1156 "\\begin{minipage}[c]{.6\\textwidth}\n"
1157 "%s\n\n"
1158 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1159 "%s"
1160 "\\end{minipage}\n"
1161 "}\n"
1162 "\\end{center}")
1163 full-title contents))))))
1166 ;;;; Item
1168 (defun org-e-latex-item (item contents info)
1169 "Transcode an ITEM element from Org to LaTeX.
1170 CONTENTS holds the contents of the item. INFO is a plist holding
1171 contextual information."
1172 ;; Grab `:level' from plain-list properties, which is always the
1173 ;; first element above current item.
1174 (let* ((level (org-element-get-property
1175 :level (car (plist-get info :genealogy))))
1176 (counter (let ((count (org-element-get-property :counter item)))
1177 (and count
1178 (< level 4)
1179 (format "\\setcounter{enum%s}{%s}\n"
1180 (nth level '("i" "ii" "iii" "iv"))
1181 (1- count)))))
1182 (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
1183 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1184 ((eq checkbox 'off) "$\\Box$ ")
1185 ((eq checkbox 'trans) "$\\boxminus$ "))))
1186 (tag (let ((tag (org-element-get-property :tag item)))
1187 (and tag
1188 (format "[%s]" (org-export-secondary-string
1189 tag 'e-latex info))))))
1190 (concat counter "\\item" tag " " checkbox contents)))
1193 ;;;; Keyword
1195 (defun org-e-latex-keyword (keyword contents info)
1196 "Transcode a KEYWORD element from Org to LaTeX.
1197 CONTENTS is nil. INFO is a plist holding contextual information."
1198 (let ((key (downcase (org-element-get-property :key keyword)))
1199 (value (org-element-get-property :value keyword)))
1200 (cond
1201 ((string= key "latex") value)
1202 ((string= key "index") (format "\\index{%s}" value))
1203 ((string= key "target")
1204 (format "\\label{%s}" (org-export-solidify-link-text value)))
1205 ((string= key "toc")
1206 (let ((value (downcase value)))
1207 (cond
1208 ((string-match "\\<headlines\\>" value)
1209 (let ((depth (or (and (string-match "[0-9]+" value)
1210 (string-to-number (match-string 0 value)))
1211 (plist-get info :with-toc))))
1212 (concat
1213 (when (wholenump depth)
1214 (format "\\setcounter{tocdepth}{%s}\n" depth))
1215 "\\tableofcontents")))
1216 ((string= "tables" value) "\\listoftables")
1217 ((string= "figures" value) "\\listoffigures")
1218 ((string= "listings" value) "\\listoflistings"))))
1219 ((string= key "include")
1220 (org-export-included-file keyword 'e-latex info)))))
1223 ;;;; Latex Environment
1225 (defun org-e-latex-latex-environment (latex-environment contents info)
1226 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1227 CONTENTS is nil. INFO is a plist holding contextual information."
1228 (org-e-latex--wrap-label
1229 latex-environment
1230 (org-remove-indentation (org-element-get-property :value latex-environment))))
1233 ;;;; Latex Fragment
1235 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1236 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1237 CONTENTS is nil. INFO is a plist holding contextual information."
1238 (org-element-get-property :value latex-fragment))
1241 ;;;; Line Break
1243 (defun org-e-latex-line-break (line-break contents info)
1244 "Transcode a LINE-BREAK object from Org to LaTeX.
1245 CONTENTS is nil. INFO is a plist holding contextual information."
1246 "\\\\")
1249 ;;;; Link
1251 (defun org-e-latex-link--inline-image (path info)
1252 "Return LaTeX code for an image at PATH.
1253 INFO is a plist containing export options."
1254 (let* ((parent-props (nth 1 (car (plist-get info :genealogy))))
1255 (caption (org-e-latex--caption/label-string
1256 (plist-get parent-props :caption)
1257 (plist-get parent-props :name)
1258 info))
1259 ;; Retrieve latex attributes from the element around.
1260 (attr (let ((raw-attr
1261 (mapconcat #'identity
1262 (plist-get parent-props :attr_latex) " ")))
1263 (unless (string= raw-attr "") raw-attr)))
1264 (disposition
1265 (cond
1266 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1267 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1268 ((or (and attr (string-match "\\<float\\>" attr))
1269 (not (string= caption "")))
1270 'float)))
1271 (placement
1272 (cond
1273 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1274 (org-match-string-no-properties 1 attr))
1275 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1276 ((eq disposition 'float)
1277 (concat "[" org-e-latex-default-figure-position "]"))
1278 (t ""))))
1279 ;; Now clear ATTR from any special keyword and set a default
1280 ;; value if nothing is left.
1281 (if (not attr)
1282 (setq attr "")
1283 (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
1284 attr)
1285 (replace-match "" nil nil attr))
1286 (setq attr (org-trim attr)))
1287 (setq attr (cond ((not (string= attr "")) attr)
1288 ((eq disposition 'float) "width=0.7\\textwidth")
1289 ((eq disposition 'wrap) "width=0.48\\textwidth")
1290 (t (or org-e-latex-image-default-option ""))))
1291 ;; Return proper string, depending on DISPOSITION.
1292 (case disposition
1293 ('wrap (format "\\begin{wrapfigure}%s
1294 \\centering
1295 \\includegraphics[%s]{%s}
1296 %s\\end{wrapfigure}" placement attr path caption))
1297 ('mulicolumn (format "\\begin{figure*}%s
1298 \\centering
1299 \\includegraphics[%s]{%s}
1300 %s\\end{figure*}" placement attr path caption))
1301 ('float (format "\\begin{figure}%s
1302 \\centering
1303 \\includegraphics[%s]{%s}
1304 %s\\end{figure}" placement attr path caption))
1305 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1307 (defun org-e-latex-link (link desc info)
1308 "Transcode a LINK object from Org to LaTeX.
1310 DESC is the description part of the link, or the empty string.
1311 INFO is a plist holding contextual information. See
1312 `org-export-data'."
1313 (let* ((type (org-element-get-property :type link))
1314 (raw-path (org-element-get-property :path link))
1315 ;; Ensure DESC really exists, or set it to nil.
1316 (desc (and (not (string= desc "")) desc))
1317 (imagep (org-export-inline-image-p
1318 link org-e-latex-inline-image-extensions))
1319 (path (cond
1320 ((member type '("http" "https" "ftp" "mailto"))
1321 (concat type ":" raw-path))
1322 ((and (not imagep) (string= type "file"))
1323 (when (string-match "\\(.+\\)::.+" raw-path)
1324 (setq raw-path (match-string 1 raw-path)))
1325 (if (file-name-absolute-p raw-path)
1326 (concat "file://" (expand-file-name raw-path))
1327 ;; TODO: Not implemented yet. Concat also:
1328 ;; (org-export-directory :LaTeX info)
1329 (concat "file://" raw-path)))
1330 (t raw-path)))
1331 protocol)
1332 (cond
1333 ;; Image file.
1334 (imagep (org-e-latex-link--inline-image path info))
1335 ;; Id: for now, assume it's an internal link. TODO: do something
1336 ;; to check if it isn't in the current file.
1337 ((string= type "id")
1338 (format "\\hyperref[%s]{%s}" path (or desc path)))
1339 ;; Custom-id, target or radioed target: replace link with the
1340 ;; normalized custom-id/target name.
1341 ((member type '("custom-id" "target" "radio"))
1342 (format "\\hyperref[%s]{%s}"
1343 (org-export-solidify-link-text path)
1344 (or desc (org-export-secondary-string path 'e-latex info))))
1345 ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
1346 ;; the destination of the link.
1347 ((string= type "fuzzy")
1348 (let ((destination (org-export-resolve-fuzzy-link link info)))
1349 (cond
1350 ;; Target match.
1351 ((stringp destination)
1352 (format "\\hyperref[%s]{%s}"
1353 (org-export-solidify-link-text destination)
1354 (or desc
1355 (org-export-secondary-string
1356 (org-element-get-property :raw-link link) 'e-latex info))))
1357 ;; Headline match.
1358 ((integerp destination)
1359 (format "\\hyperref[headline-%d]{%s}"
1360 destination
1361 (or desc
1362 (org-export-secondary-string
1363 (org-element-get-property :raw-link link) 'e-latex info))))
1364 ;; No match.
1365 (t (format "\\texttt{%s}"
1366 (or desc
1367 (org-export-secondary-string
1368 (org-element-get-property :raw-link link)
1369 'e-latex info)))))))
1370 ;; Coderef: replace link with the reference name or the
1371 ;; equivalent line number.
1372 ((string= type "coderef")
1373 (format (org-export-get-coderef-format path (or desc ""))
1374 (cdr (assoc path (plist-get info :code-refs)))))
1375 ;; Link type is handled by a special function.
1376 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1377 (funcall protocol (org-link-unescape path) desc 'latex))
1378 ;; External link with a description part.
1379 ((and path desc) (format "\\href{%s}{%s}" path desc))
1380 ;; External link without a description part.
1381 (path (format "\\url{%s}" path))
1382 ;; No path, only description. Try to do something useful.
1383 (t (format "\\texttt{%s}" desc)))))
1386 ;;;; Babel Call
1388 ;; Babel Calls are ignored.
1391 ;;;; Macro
1393 (defun org-e-latex-macro (macro contents info)
1394 "Transcode a MACRO element from Org to LaTeX.
1395 CONTENTS is nil. INFO is a plist holding contextual information."
1396 ;; Use available tools.
1397 (org-export-expand-macro macro info))
1400 ;;;; Paragraph
1402 (defun org-e-latex-paragraph (paragraph contents info)
1403 "Transcode a PARAGRAPH element from Org to LaTeX.
1404 CONTENTS is the contents of the paragraph, as a string. INFO is
1405 the plist used as a communication channel."
1406 contents)
1409 ;;;; Plain List
1411 (defun org-e-latex-plain-list (plain-list contents info)
1412 "Transcode a PLAIN-LIST element from Org to LaTeX.
1413 CONTENTS is the contents of the list. INFO is a plist holding
1414 contextual information."
1415 (let* ((type (org-element-get-property :type plain-list))
1416 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1417 "inparadesc" "asparadesc"))
1418 (paralist-regexp (concat
1419 "\\("
1420 (mapconcat 'identity paralist-types "\\|")
1421 "\\)"))
1422 (attr (mapconcat #'identity
1423 (org-element-get-property :attr_latex plain-list)
1424 " "))
1425 (latex-type (cond
1426 ((and attr
1427 (string-match
1428 (format "\\<%s\\>" paralist-regexp) attr))
1429 (match-string 1 attr))
1430 ((eq type 'ordered) "enumerate")
1431 ((eq type 'unordered) "itemize")
1432 ((eq type 'descriptive) "description"))))
1433 (org-e-latex--wrap-label
1434 plain-list
1435 (format "\\begin{%s}%s\n%s\\end{%s}"
1436 latex-type
1437 ;; Once special environment, if any, has been removed, the
1438 ;; rest of the attributes will be optional arguments.
1439 ;; They will be put inside square brackets if necessary.
1440 (let ((opt (replace-regexp-in-string
1441 (format " *%s *" paralist-regexp) "" attr)))
1442 (cond ((string= opt "") "")
1443 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1444 (t (format "[%s]" opt))))
1445 contents
1446 latex-type))))
1449 ;;;; Plain Text
1451 (defun org-e-latex-plain-text (text info)
1452 "Transcode a TEXT string from Org to LaTeX.
1453 TEXT is the string to transcode. INFO is a plist holding
1454 contextual information."
1455 ;; Protect %, #, &, $, ~, ^, _, { and }.
1456 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1457 (setq text
1458 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1459 ;; Protect \
1460 (setq text (replace-regexp-in-string
1461 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1462 "$\\backslash$" text nil t 1))
1463 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1464 (let ((case-fold-search nil)
1465 (start 0))
1466 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1467 (setq text (replace-match
1468 (format "\\%s{}" (match-string 1 text)) nil t text)
1469 start (match-end 0))))
1470 ;; Handle quotation marks
1471 (setq text (org-e-latex--quotation-marks text info))
1472 ;; Convert special strings.
1473 (when (plist-get info :with-special-strings)
1474 (while (string-match (regexp-quote "...") text)
1475 (setq text (replace-match "\\ldots{}" nil t text))))
1476 ;; Handle break preservation if required.
1477 (when (plist-get info :preserve-breaks)
1478 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1479 text)))
1480 ;; Return value.
1481 text)
1484 ;;;; Property Drawer
1486 (defun org-e-latex-property-drawer (property-drawer contents info)
1487 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1488 CONTENTS is nil. INFO is a plist holding contextual
1489 information."
1490 ;; The property drawer isn't exported but we want separating blank
1491 ;; lines nonetheless.
1495 ;;;; Quote Block
1497 (defun org-e-latex-quote-block (quote-block contents info)
1498 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1499 CONTENTS holds the contents of the block. INFO is a plist
1500 holding contextual information."
1501 (org-e-latex--wrap-label
1502 quote-block
1503 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1506 ;;;; Quote Section
1508 (defun org-e-latex-quote-section (quote-section contents info)
1509 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1510 CONTENTS is nil. INFO is a plist holding contextual information."
1511 (let ((value (org-remove-indentation
1512 (org-element-get-property :value quote-section))))
1513 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1516 ;;;; Section
1518 (defun org-e-latex-section (section contents info)
1519 "Transcode a SECTION element from Org to LaTeX.
1520 CONTENTS holds the contents of the section. INFO is a plist
1521 holding contextual information."
1522 contents)
1525 ;;;; Radio Target
1527 (defun org-e-latex-radio-target (radio-target text info)
1528 "Transcode a RADIO-TARGET object from Org to LaTeX.
1529 TEXT is the text of the target. INFO is a plist holding
1530 contextual information."
1531 (format "\\label{%s}%s"
1532 (org-export-solidify-link-text
1533 (org-element-get-property :raw-value radio-target))
1534 text))
1537 ;;;; Special Block
1539 (defun org-e-latex-special-block (special-block contents info)
1540 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1541 CONTENTS holds the contents of the block. INFO is a plist
1542 holding contextual information."
1543 (let ((type (downcase (org-element-get-property :type special-block))))
1544 (org-e-latex--wrap-label
1545 special-block
1546 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1549 ;;;; Src Block
1551 (defun org-e-latex-src-block (src-block contents info)
1552 "Transcode a SRC-BLOCK element from Org to LaTeX.
1553 CONTENTS holds the contents of the item. INFO is a plist holding
1554 contextual information."
1555 (let* ((lang (org-element-get-property :language src-block))
1556 (code (org-export-handle-code
1557 (org-element-get-property :value src-block)
1558 (org-element-get-property :switches src-block)
1559 info lang))
1560 (caption (org-element-get-property :caption src-block))
1561 (label (org-element-get-property :name src-block))
1562 (custom-env (and lang
1563 (cadr (assq (intern lang)
1564 org-e-latex-custom-lang-environments)))))
1565 (cond
1566 ;; No source fontification.
1567 ((not org-e-latex-listings)
1568 (let ((caption-str (org-e-latex--caption/label-string
1569 caption label info))
1570 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1571 (format (or float-env "%s")
1572 (concat
1573 caption-str
1574 (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
1575 ;; Custom environment.
1576 (custom-env
1577 (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
1578 ;; Use minted package.
1579 ((eq org-e-latex-listings 'minted)
1580 (let* ((mint-lang (or (cadr (assq (intern lang) org-e-latex-minted-langs))
1581 lang))
1582 (float-env (when (or label caption)
1583 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1584 (org-e-latex--caption/label-string
1585 caption label info))))
1586 (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1587 (org-e-latex--make-option-string
1588 org-e-latex-minted-options)
1589 mint-lang code)))
1590 (if float-env (format float-env body) body)))
1591 ;; Use listings package.
1593 (let ((lst-lang (or (cadr (assq (intern lang) org-e-latex-listings-langs))
1594 lang))
1595 (caption-str (and caption
1596 (org-export-secondary-string
1597 (org-element-get-property :caption src-block)
1598 'e-latex info))))
1599 (concat (format "\\lstset{%s}\n"
1600 (org-e-latex--make-option-string
1601 (append org-e-latex-listings-options
1602 `(("language" ,lst-lang))
1603 (when label `(("label" ,label)))
1604 (when caption-str
1605 `(("caption" ,caption-str))))))
1606 (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
1609 ;;;; Statistics Cookie
1611 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1612 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1613 CONTENTS is nil. INFO is a plist holding contextual information."
1614 (org-element-get-property :value statistics-cookie))
1617 ;;;; Subscript
1619 (defun org-e-latex-subscript (subscript contents info)
1620 "Transcode a SUBSCRIPT object from Org to LaTeX.
1621 CONTENTS is the contents of the object. INFO is a plist holding
1622 contextual information."
1623 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1626 ;;;; Superscript
1628 (defun org-e-latex-superscript (superscript contents info)
1629 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1630 CONTENTS is the contents of the object. INFO is a plist holding
1631 contextual information."
1632 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1635 ;;;; Table
1637 (defun org-e-latex-table--format-string (table info)
1638 "Return an appropriate format string for TABLE.
1640 INFO is the plist containing format info about the table, as
1641 returned by `org-export-table-format-info'.
1643 The format string one placeholder for the body of the table."
1644 (let* ((label (org-element-get-property :name table))
1645 (caption (org-e-latex--caption/label-string
1646 (org-element-get-property :caption table) label info))
1647 (attr (mapconcat #'identity
1648 (org-element-get-property :attr_latex table)
1649 " "))
1650 ;; Determine alignment string.
1651 (alignment (org-e-latex-table--align-string attr info))
1652 ;; Determine environment for the table: longtable, tabular...
1653 (table-env (cond
1654 ((not attr) org-e-latex-default-table-environment)
1655 ((string-match "\\<longtable\\>" attr) "longtable")
1656 ((string-match "\\(tabular.\\)" attr)
1657 (org-match-string-no-properties 1 attr))
1658 (t org-e-latex-default-table-environment)))
1659 ;; If table is a float, determine environment: table or table*.
1660 (float-env (cond
1661 ((string= "longtable" table-env) nil)
1662 ((and attr
1663 (or (string-match (regexp-quote "table*") attr)
1664 (string-match "\\<multicolumn\\>" attr)))
1665 "table*")
1666 ((or (not (string= caption "")) label) "table")))
1667 ;; Extract others display options.
1668 (width (and attr
1669 (string-match "\\<width=\\(\\S-+\\)" attr)
1670 (org-match-string-no-properties 1 attr)))
1671 (placement (if (and attr
1672 (string-match "\\<placement=\\(\\S-+\\)" attr))
1673 (org-match-string-no-properties 1 attr)
1674 (concat "["
1675 org-e-latex-default-figure-position
1676 "]"))))
1677 ;; Prepare the final format string for the table.
1678 (cond
1679 ;; Longtable.
1680 ((string= "longtable" table-env)
1681 (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1682 alignment
1683 (if (or (not org-e-latex-table-caption-above)
1684 (string= "" caption))
1686 (concat (org-trim caption) "\\\\"))
1687 (if (or org-e-latex-table-caption-above
1688 (string= "" caption))
1690 (concat (org-trim caption) "\\\\\n"))))
1691 ;; Others.
1692 (t (concat (when float-env
1693 (concat
1694 (format "\\begin{%s}%s\n" float-env placement)
1695 (if org-e-latex-table-caption-above caption "")))
1696 (when org-e-latex-tables-centered "\\begin{center}\n")
1697 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1698 table-env
1699 (if width (format "{%s}" width) "")
1700 alignment
1701 table-env)
1702 (when org-e-latex-tables-centered "\n\\end{center}")
1703 (when float-env
1704 (concat (if org-e-latex-table-caption-above "" caption)
1705 (format "\n\\end{%s}" float-env))))))))
1707 (defun org-e-latex-table--align-string (attr info)
1708 "Return an appropriate LaTeX alignment string.
1710 INFO is the plist containing format info about the table, as
1711 returned by `org-export-table-format-info'."
1712 (or (and attr
1713 (string-match "\\<align=\\(\\S-+\\)" attr)
1714 (match-string 1 attr))
1715 (let* ((align (copy-sequence (plist-get info :alignment)))
1716 (colgroups (copy-sequence (plist-get info :column-groups)))
1717 (cols (length align))
1718 (separators (make-vector (1+ cols) "")))
1719 ;; Ignore the first column if it's special.
1720 (when (plist-get info :special-column-p)
1721 (aset align 0 "") (aset colgroups 0 nil))
1722 (let ((col 0))
1723 (mapc (lambda (el)
1724 (let ((gr (aref colgroups col)))
1725 (when (memq gr '(start start-end))
1726 (aset separators col "|"))
1727 (when (memq gr '(end start-end))
1728 (aset separators (1+ col) "|")))
1729 (incf col))
1730 align))
1731 ;; Build the LaTeX specific alignment string.
1732 (loop for al across align
1733 for sep across separators
1734 concat (concat sep al) into output
1735 finally return (concat output (aref separators cols))))))
1737 (defun org-e-latex-table (table contents info)
1738 "Transcode a TABLE element from Org to LaTeX.
1739 CONTENTS is nil. INFO is a plist holding contextual information."
1740 (let ((attr (mapconcat #'identity
1741 (org-element-get-property :attr_latex table)
1742 " "))
1743 (raw-table (org-element-get-property :raw-table table)))
1744 (cond
1745 ;; Case 1: verbatim table.
1746 ((or org-e-latex-tables-verbatim
1747 (and attr (string-match "\\<verbatim\\>" attr)))
1748 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1749 (org-export-clean-table
1750 raw-table
1751 (plist-get (org-export-table-format-info raw-table)
1752 :special-column-p))))
1753 ;; Case 2: table.el table. Convert it using appropriate tools.
1754 ((eq (org-element-get-property :type table) 'table.el)
1755 (require 'table)
1756 ;; Ensure "*org-export-table*" buffer is empty.
1757 (and (get-buffer "*org-export-table*")
1758 (kill-buffer (get-buffer "*org-export-table*")))
1759 (let ((output (with-temp-buffer
1760 (insert raw-table)
1761 (goto-char 1)
1762 (re-search-forward "^[ \t]*|[^|]" nil t)
1763 (table-generate-source 'latex "*org-export-table*")
1764 (with-current-buffer "*org-export-table*"
1765 (org-trim (buffer-string))))))
1766 (kill-buffer (get-buffer "*org-export-table*"))
1767 ;; Remove left out comments.
1768 (while (string-match "^%.*\n" output)
1769 (setq output (replace-match "" t t output)))
1770 ;; When the "rmlines" attribute is provided, remove all hlines
1771 ;; but the the one separating heading from the table body.
1772 (when (and attr (string-match "\\<rmlines\\>" attr))
1773 (let ((n 0) (pos 0))
1774 (while (and (< (length output) pos)
1775 (setq pos (string-match "^\\\\hline\n?" output pos)))
1776 (incf n)
1777 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
1778 (if org-e-latex-tables-centered
1779 (format "\\begin{center}\n%s\n\\end{center}" output)
1780 output)))
1781 ;; Case 3: Standard table.
1783 (let* ((table-info (org-export-table-format-info raw-table))
1784 (clean-table (org-export-clean-table
1785 raw-table (plist-get table-info :special-column-p)))
1786 (columns-number (length (plist-get table-info :alignment))))
1787 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
1788 ;; particular, send each cell to
1789 ;; `org-element-parse-secondary-string' to expand any Org
1790 ;; object within. Eventually, flesh the format string out with
1791 ;; the table.
1792 (format (org-e-latex-table--format-string table table-info)
1793 (orgtbl-to-latex
1794 (mapcar
1795 (lambda (row)
1796 (if (string-match org-table-hline-regexp row)
1797 'hline
1798 (mapcar
1799 (lambda (cell)
1800 (org-export-secondary-string
1801 (org-element-parse-secondary-string
1802 cell
1803 (cdr (assq 'table org-element-string-restrictions)))
1804 'e-latex info))
1805 (org-split-string row "[ \t]*|[ \t]*"))))
1806 (org-split-string clean-table "\n"))
1807 `(:tstart nil :tend nil
1808 ;; Longtable environment requires specific
1809 ;; header line end.
1810 :hlend ,(and attr
1811 (string-match "\\<longtable\\>" attr)
1812 (format "\\\\
1813 \\hline
1814 \\endhead
1815 \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
1816 \\endfoot
1817 \\endlastfoot"
1818 columns-number))))))))))
1821 ;;;; Target
1823 (defun org-e-latex-target (target text info)
1824 "Transcode a TARGET object from Org to LaTeX.
1825 TEXT is the text of the target. INFO is a plist holding
1826 contextual information."
1827 (format "\\label{%s}%s"
1828 (org-export-solidify-link-text
1829 (org-element-get-property :raw-value target))
1830 text))
1833 ;;;; Time-stamp
1835 (defun org-e-latex-time-stamp (time-stamp contents info)
1836 "Transcode a TIME-STAMP object from Org to LaTeX.
1837 CONTENTS is nil. INFO is a plist holding contextual information."
1838 (let ((value (org-element-get-property :value time-stamp))
1839 (type (org-element-get-property :type time-stamp))
1840 (appt-type (org-element-get-property :appt-type time-stamp)))
1841 (concat (cond ((eq appt-type 'scheduled)
1842 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
1843 ((eq appt-type 'deadline)
1844 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
1845 ((eq appt-type 'closed)
1846 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
1847 (cond ((memq type '(active active-range))
1848 (format org-e-latex-active-timestamp-format value))
1849 ((memq type '(inactive inactive-range))
1850 (format org-e-latex-inactive-timestamp-format value))
1852 (format org-e-latex-diary-timestamp-format value))))))
1855 ;;;; Verbatim
1857 (defun org-e-latex-verbatim (element contents info)
1858 "Return verbatim text in LaTeX."
1859 (let ((fmt (cdr (assoc (org-element-get-property :marker element)
1860 org-e-latex-emphasis-alist)))
1861 (value (org-element-get-property :value element)))
1862 (cond
1863 ;; Handle the `verb' special case.
1864 ((eq 'verb fmt)
1865 (let ((separator (org-e-latex--find-verb-separator value)))
1866 (concat "\\verb" separator value separator)))
1867 ;; Handle the `protectedtexttt' special case.
1868 ((eq 'protectedtexttt fmt)
1869 (let ((start 0)
1870 (trans '(("\\" . "\\textbackslash{}")
1871 ("~" . "\\textasciitilde{}")
1872 ("^" . "\\textasciicircum{}")))
1873 (rtn "")
1874 char)
1875 (while (string-match "[\\{}$%&_#~^]" value)
1876 (setq char (match-string 0 value))
1877 (if (> (match-beginning 0) 0)
1878 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
1879 (setq value (substring value (1+ (match-beginning 0))))
1880 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1881 rtn (concat rtn char)))
1882 (setq value (concat rtn value)
1883 fmt "\\texttt{%s}")
1884 (while (string-match "--" value)
1885 (setq value (replace-match "-{}-" t t value)))
1886 (format fmt value)))
1887 ;; Else use format string.
1888 (t (format fmt value)))))
1891 ;;;; Verse Block
1893 (defun org-e-latex-verse-block (verse-block contents info)
1894 "Transcode a VERSE-BLOCK element from Org to LaTeX.
1895 CONTENTS is nil. INFO is a plist holding contextual information."
1896 (org-e-latex--wrap-label
1897 verse-block
1898 ;; In a verse environment, add a line break to each newline
1899 ;; character and change each white space at beginning of a line
1900 ;; into a space of 1 em. Also change each blank line with
1901 ;; a vertical space of 1 em.
1902 (progn
1903 (setq contents (replace-regexp-in-string
1904 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1905 (replace-regexp-in-string
1906 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1907 (org-remove-indentation
1908 (org-export-secondary-string
1909 (org-element-get-property :value verse-block)
1910 'e-latex info)))))
1911 (while (string-match "^[ \t]+" contents)
1912 (let ((new-str (format "\\hspace*{%dem}"
1913 (length (match-string 0 contents)))))
1914 (setq contents (replace-match new-str nil t contents))))
1915 (format "\\begin{verse}\n%s\\end{verse}" contents))))
1919 ;;; Compilation
1921 (defun org-e-latex-compile (texfile)
1922 "Compile a TeX file.
1924 TEXFILE is the name of the file being compiled. Processing is
1925 done through the command specified in `org-e-latex-pdf-process'.
1927 Return PDF file name or an error if it couldn't be produced."
1928 (let* ((wconfig (current-window-configuration))
1929 (texfile (file-truename texfile))
1930 (base (file-name-sans-extension texfile))
1931 errors)
1932 (message (format "Processing LaTeX file " texfile "..." texfile))
1933 (unwind-protect
1934 (progn
1935 (cond
1936 ;; A function is provided: Apply it.
1937 ((functionp org-latex-to-pdf-process)
1938 (funcall org-latex-to-pdf-process (shell-quote-argument texfile)))
1939 ;; A list is provided: Replace %b, %f and %o with appropriate
1940 ;; values in each command before applying it. Output is
1941 ;; redirected to "*Org PDF LaTeX Output*" buffer.
1942 ((consp org-e-latex-pdf-process)
1943 (let* ((out-dir (or (file-name-directory texfile) "./"))
1944 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
1945 (mapc
1946 (lambda (command)
1947 (shell-command
1948 (replace-regexp-in-string
1949 "%b" (shell-quote-argument base)
1950 (replace-regexp-in-string
1951 "%f" (shell-quote-argument texfile)
1952 (replace-regexp-in-string
1953 "%o" (shell-quote-argument out-dir) command)))
1954 outbuf))
1955 org-e-latex-pdf-process)
1956 ;; Collect standard errors from output buffer.
1957 (setq errors (org-e-latex-collect-errors outbuf))))
1958 (t (error "No valid command to process to PDF")))
1959 (let ((pdffile (concat base ".pdf")))
1960 ;; Check for process failure. Provide collected errors if
1961 ;; possible.
1962 (if (not (file-exists-p pdffile))
1963 (error (concat (format "PDF file %s wasn't produced" pdffile)
1964 (when errors (concat ": " errors))))
1965 ;; Else remove log files, when specified, and signal end of
1966 ;; process to user, along with any error encountered.
1967 (when org-e-latex-remove-logfiles
1968 (dolist (ext org-e-latex-logfiles-extensions)
1969 (let ((file (concat base "." ext)))
1970 (when (file-exists-p file) (delete-file file)))))
1971 (message (concat "Process completed"
1972 (if (not errors) "."
1973 (concat " with errors: " errors)))))
1974 ;; Return output file name.
1975 pdffile))
1976 (set-window-configuration wconfig))))
1978 (defun org-e-latex-collect-errors (buffer)
1979 "Collect some kind of errors from \"pdflatex\" command output.
1981 BUFFER is the buffer containing output.
1983 Return collected error types as a string, or nil if there was
1984 none."
1985 (with-current-buffer buffer
1986 (save-excursion
1987 (goto-char (point-max))
1988 ;; Find final "pdflatex" run.
1989 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
1990 (let ((case-fold-search t)
1991 (errors ""))
1992 (when (save-excursion
1993 (re-search-forward "Reference.*?undefined" nil t))
1994 (setq errors (concat errors " [undefined reference]")))
1995 (when (save-excursion
1996 (re-search-forward "Citation.*?undefined" nil t))
1997 (setq errors (concat errors " [undefined citation]")))
1998 (when (save-excursion
1999 (re-search-forward "Undefined control sequence" nil t))
2000 (setq errors (concat errors " [undefined control sequence]")))
2001 (when (save-excursion
2002 (re-search-forward "^! LaTeX.*?Error" nil t))
2003 (setq errors (concat errors " [LaTeX error]")))
2004 (when (save-excursion
2005 (re-search-forward "^! Package.*?Error" nil t))
2006 (setq errors (concat errors " [package error]")))
2007 (and (org-string-nw-p errors) (org-trim errors)))))))
2010 (provide 'org-e-latex)
2011 ;;; org-e-latex.el ends here