org-e-latex: Apply signature modification of functions
[org-mode/org-mode-NeilSmithlineMods.git] / EXPERIMENTAL / org-e-latex.el
blobcfb9e9f2357c063112f6594a66b3249272c786bf
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 example-block info)))
897 (org-e-latex--wrap-label
898 example-block (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
901 ;;;; Export Snippet
903 (defun org-e-latex-export-snippet (export-snippet contents info)
904 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
905 CONTENTS is nil. INFO is a plist holding contextual information."
906 (org-element-get-property :value export-snippet))
909 ;;;; Export Block
911 (defun org-e-latex-export-block (export-block contents info)
912 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
913 CONTENTS is nil. INFO is a plist holding contextual information."
914 (when (string= (org-element-get-property :type export-block) "latex")
915 (org-remove-indentation (org-element-get-property :value export-block))))
918 ;;;; Fixed Width
920 (defun org-e-latex-fixed-width (fixed-width contents info)
921 "Transcode a FIXED-WIDTH element from Org to LaTeX.
922 CONTENTS is nil. INFO is a plist holding contextual information."
923 (let* ((value (org-element-normalize-string
924 (replace-regexp-in-string
925 "^[ \t]*: ?" ""
926 (org-element-get-property :value fixed-width)))))
927 (org-e-latex--wrap-label
928 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
931 ;;;; Footnote Definition
933 ;; Footnote Definitions are ignored.
936 ;;;; Footnote Reference
938 (defun org-e-latex-footnote-reference (footnote-reference contents info)
939 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
940 CONTENTS is nil. INFO is a plist holding contextual information."
941 (concat
942 ;; Insert separator between two footnotes in a row.
943 (let ((prev (org-export-get-previous-element footnote-reference info)))
944 (when (and (listp prev) (eq (car prev) '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-export-get-relative-level headline info))
978 (class-sectionning (assoc class org-e-latex-classes))
979 ;; Section formatting will set two placeholders: one for the
980 ;; title and the other for the contents.
981 (section-fmt
982 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
983 (fboundp (nth 2 class-sectionning)))
984 (funcall (nth 2 class-sectionning) level numberedp)
985 (nth (1+ level) class-sectionning))))
986 (cond
987 ;; No section available for that LEVEL.
988 ((not sec) nil)
989 ;; Section format directly returned by a function.
990 ((stringp sec) sec)
991 ;; (numbered-section . unnumbered-section)
992 ((not (consp (cdr sec)))
993 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
994 ;; (numbered-open numbered-close)
995 ((= (length sec) 2)
996 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
997 ;; (num-in num-out no-num-in no-num-out)
998 ((= (length sec) 4)
999 (if numberedp
1000 (concat (car sec) "\n%s" (nth 1 sec))
1001 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1002 (text (org-export-secondary-string
1003 (org-element-get-property :title headline) 'e-latex info))
1004 (todo (and (plist-get info :with-todo-keywords)
1005 (let ((todo (org-element-get-property
1006 :todo-keyword headline)))
1007 (and todo
1008 (org-export-secondary-string todo 'e-latex info)))))
1009 (todo-type (and todo (org-element-get-property :todo-type headline)))
1010 (tags (and (plist-get info :with-tags)
1011 (org-element-get-property :tags headline)))
1012 (priority (and (plist-get info :with-priority)
1013 (org-element-get-property :priority headline)))
1014 ;; Create the headline text.
1015 (full-text (if (functionp org-e-latex-format-headline-function)
1016 ;; User-defined formatting function.
1017 (funcall org-e-latex-format-headline-function
1018 todo todo-type priority text tags)
1019 ;; Default formatting.
1020 (concat
1021 (when todo
1022 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1023 (when priority (format "\\framebox{\\#%c} " priority))
1024 text
1025 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1026 ;; Associate some \label to the headline for internal links.
1027 (headline-labels (mapconcat
1028 (lambda (p)
1029 (let ((val (org-element-get-property p headline)))
1030 (when val (format "\\label{%s}\n"
1031 (if (eq p :begin)
1032 (format "headline-%s" val)
1033 val)))))
1034 '(:custom-id :id :begin) ""))
1035 (pre-blanks (make-string (org-element-get-property :pre-blank headline)
1036 10)))
1037 (cond
1038 ;; Case 1: This is a footnote section: ignore it.
1039 ((org-element-get-property :footnote-section-p headline) nil)
1040 ;; Case 2. This is a deep sub-tree: export it as a list item.
1041 ;; Also export as items headlines for which no section
1042 ;; format has been found.
1043 ((or (not section-fmt)
1044 (and (wholenump (plist-get info :headline-levels))
1045 (> level (plist-get info :headline-levels))))
1046 ;; Build the real contents of the sub-tree.
1047 (let ((low-level-body
1048 (concat
1049 ;; If the headline is the first sibling, start a list.
1050 (when (org-export-first-sibling-p headline info)
1051 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1052 ;; Itemize headline
1053 "\\item " full-text "\n" headline-labels pre-blanks contents)))
1054 ;; If headline in the last sibling, close the list, before any
1055 ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
1056 (if (org-export-last-sibling-p headline info)
1057 (replace-regexp-in-string
1058 "[ \t\n]*\\'"
1059 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1060 low-level-body)
1061 low-level-body)))
1062 ;; Case 3. Standard headline. Export it as a section.
1063 (t (format section-fmt full-text
1064 (concat headline-labels pre-blanks contents))))))
1067 ;;;; Horizontal Rule
1069 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1070 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1071 CONTENTS is nil. INFO is a plist holding contextual information."
1072 (let ((attr (mapconcat #'identity
1073 (org-element-get-property :attr_latex horizontal-rule)
1074 " ")))
1075 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1078 ;;;; Inline Babel Call
1080 ;; Inline Babel Calls are ignored.
1083 ;;;; Inline Src Block
1085 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1086 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1087 CONTENTS holds the contents of the item. INFO is a plist holding
1088 contextual information."
1089 (let* ((code (org-element-get-property :value inline-src-block))
1090 (separator (org-e-latex--find-verb-separator code)))
1091 (cond
1092 ;; Do not use a special package: transcode it verbatim.
1093 ((not org-e-latex-listings)
1094 (concat "\\verb" separator code separator))
1095 ;; Use minted package.
1096 ((eq org-e-latex-listings 'minted)
1097 (let* ((org-lang (org-element-get-property :language inline-src-block))
1098 (mint-lang (or (cadr (assq (intern org-lang)
1099 org-e-latex-minted-langs))
1100 org-lang))
1101 (options (org-e-latex--make-option-string
1102 org-e-latex-minted-options)))
1103 (concat (format "\\mint%s{%s}"
1104 (if (string= options "") "" (format "[%s]" options))
1105 mint-lang)
1106 separator code separator)))
1107 ;; Use listings package.
1109 ;; Maybe translate language's name.
1110 (let* ((org-lang (org-element-get-property :language inline-src-block))
1111 (lst-lang (or (cadr (assq (intern org-lang)
1112 org-e-latex-listings-langs))
1113 org-lang))
1114 (options (org-e-latex--make-option-string
1115 (append org-e-latex-listings-options
1116 `(("language" ,lst-lang))))))
1117 (concat (format "\\lstinline[%s]" options)
1118 separator code separator))))))
1121 ;;;; Inlinetask
1123 (defun org-e-latex-inlinetask (inlinetask contents info)
1124 "Transcode an INLINETASK element from Org to LaTeX.
1125 CONTENTS holds the contents of the block. INFO is a plist
1126 holding contextual information."
1127 (let ((title (org-export-secondary-string
1128 (org-element-get-property :title inlinetask) 'e-latex info))
1129 (todo (and (plist-get info :with-todo-keywords)
1130 (let ((todo (org-element-get-property
1131 :todo-keyword inlinetask)))
1132 (and todo
1133 (org-export-secondary-string todo 'e-latex info)))))
1134 (todo-type (org-element-get-property :todo-type inlinetask))
1135 (tags (and (plist-get info :with-tags)
1136 (org-element-get-property :tags inlinetask)))
1137 (priority (and (plist-get info :with-priority)
1138 (org-element-get-property :priority inlinetask))))
1139 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1140 ;; with appropriate arguments.
1141 (if (functionp org-e-latex-format-inlinetask-function)
1142 (funcall org-e-latex-format-inlinetask-function
1143 todo todo-type priority title tags contents)
1144 ;; Otherwise, use a default template.
1145 (org-e-latex--wrap-label
1146 inlinetask
1147 (let ((full-title
1148 (concat
1149 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1150 (when priority (format "\\framebox{\\#%c} " priority))
1151 title
1152 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1153 (format (concat "\\begin{center}\n"
1154 "\\fbox{\n"
1155 "\\begin{minipage}[c]{.6\\textwidth}\n"
1156 "%s\n\n"
1157 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1158 "%s"
1159 "\\end{minipage}\n"
1160 "}\n"
1161 "\\end{center}")
1162 full-title contents))))))
1165 ;;;; Item
1167 (defun org-e-latex-item (item contents info)
1168 "Transcode an ITEM element from Org to LaTeX.
1169 CONTENTS holds the contents of the item. INFO is a plist holding
1170 contextual information."
1171 ;; Grab `:level' from plain-list properties, which is always the
1172 ;; first element above current item.
1173 (let* ((level (org-element-get-property
1174 :level (car (plist-get info :genealogy))))
1175 (counter (let ((count (org-element-get-property :counter item)))
1176 (and count
1177 (< level 4)
1178 (format "\\setcounter{enum%s}{%s}\n"
1179 (nth level '("i" "ii" "iii" "iv"))
1180 (1- count)))))
1181 (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
1182 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1183 ((eq checkbox 'off) "$\\Box$ ")
1184 ((eq checkbox 'trans) "$\\boxminus$ "))))
1185 (tag (let ((tag (org-element-get-property :tag item)))
1186 (and tag
1187 (format "[%s]" (org-export-secondary-string
1188 tag 'e-latex info))))))
1189 (concat counter "\\item" tag " " checkbox contents)))
1192 ;;;; Keyword
1194 (defun org-e-latex-keyword (keyword contents info)
1195 "Transcode a KEYWORD element from Org to LaTeX.
1196 CONTENTS is nil. INFO is a plist holding contextual information."
1197 (let ((key (downcase (org-element-get-property :key keyword)))
1198 (value (org-element-get-property :value keyword)))
1199 (cond
1200 ((string= key "latex") value)
1201 ((string= key "index") (format "\\index{%s}" value))
1202 ((string= key "target")
1203 (format "\\label{%s}" (org-export-solidify-link-text value)))
1204 ((string= key "toc")
1205 (let ((value (downcase value)))
1206 (cond
1207 ((string-match "\\<headlines\\>" value)
1208 (let ((depth (or (and (string-match "[0-9]+" value)
1209 (string-to-number (match-string 0 value)))
1210 (plist-get info :with-toc))))
1211 (concat
1212 (when (wholenump depth)
1213 (format "\\setcounter{tocdepth}{%s}\n" depth))
1214 "\\tableofcontents")))
1215 ((string= "tables" value) "\\listoftables")
1216 ((string= "figures" value) "\\listoffigures")
1217 ((string= "listings" value) "\\listoflistings"))))
1218 ((string= key "include")
1219 (org-export-included-file keyword 'e-latex info)))))
1222 ;;;; Latex Environment
1224 (defun org-e-latex-latex-environment (latex-environment contents info)
1225 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1226 CONTENTS is nil. INFO is a plist holding contextual information."
1227 (org-e-latex--wrap-label
1228 latex-environment
1229 (org-remove-indentation (org-element-get-property :value latex-environment))))
1232 ;;;; Latex Fragment
1234 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1235 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1236 CONTENTS is nil. INFO is a plist holding contextual information."
1237 (org-element-get-property :value latex-fragment))
1240 ;;;; Line Break
1242 (defun org-e-latex-line-break (line-break contents info)
1243 "Transcode a LINE-BREAK object from Org to LaTeX.
1244 CONTENTS is nil. INFO is a plist holding contextual information."
1245 "\\\\")
1248 ;;;; Link
1250 (defun org-e-latex-link--inline-image (path info)
1251 "Return LaTeX code for an image at PATH.
1252 INFO is a plist containing export options."
1253 (let* ((parent-props (nth 1 (car (plist-get info :genealogy))))
1254 (caption (org-e-latex--caption/label-string
1255 (plist-get parent-props :caption)
1256 (plist-get parent-props :name)
1257 info))
1258 ;; Retrieve latex attributes from the element around.
1259 (attr (let ((raw-attr
1260 (mapconcat #'identity
1261 (plist-get parent-props :attr_latex) " ")))
1262 (unless (string= raw-attr "") raw-attr)))
1263 (disposition
1264 (cond
1265 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1266 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1267 ((or (and attr (string-match "\\<float\\>" attr))
1268 (not (string= caption "")))
1269 'float)))
1270 (placement
1271 (cond
1272 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1273 (org-match-string-no-properties 1 attr))
1274 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1275 ((eq disposition 'float)
1276 (concat "[" org-e-latex-default-figure-position "]"))
1277 (t ""))))
1278 ;; Now clear ATTR from any special keyword and set a default
1279 ;; value if nothing is left.
1280 (if (not attr)
1281 (setq attr "")
1282 (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
1283 attr)
1284 (replace-match "" nil nil attr))
1285 (setq attr (org-trim attr)))
1286 (setq attr (cond ((not (string= attr "")) attr)
1287 ((eq disposition 'float) "width=0.7\\textwidth")
1288 ((eq disposition 'wrap) "width=0.48\\textwidth")
1289 (t (or org-e-latex-image-default-option ""))))
1290 ;; Return proper string, depending on DISPOSITION.
1291 (case disposition
1292 ('wrap (format "\\begin{wrapfigure}%s
1293 \\centering
1294 \\includegraphics[%s]{%s}
1295 %s\\end{wrapfigure}" placement attr path caption))
1296 ('mulicolumn (format "\\begin{figure*}%s
1297 \\centering
1298 \\includegraphics[%s]{%s}
1299 %s\\end{figure*}" placement attr path caption))
1300 ('float (format "\\begin{figure}%s
1301 \\centering
1302 \\includegraphics[%s]{%s}
1303 %s\\end{figure}" placement attr path caption))
1304 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1306 (defun org-e-latex-link (link desc info)
1307 "Transcode a LINK object from Org to LaTeX.
1309 DESC is the description part of the link, or the empty string.
1310 INFO is a plist holding contextual information. See
1311 `org-export-data'."
1312 (let* ((type (org-element-get-property :type link))
1313 (raw-path (org-element-get-property :path link))
1314 ;; Ensure DESC really exists, or set it to nil.
1315 (desc (and (not (string= desc "")) desc))
1316 (imagep (org-export-inline-image-p
1317 link org-e-latex-inline-image-extensions))
1318 (path (cond
1319 ((member type '("http" "https" "ftp" "mailto"))
1320 (concat type ":" raw-path))
1321 ((and (not imagep) (string= type "file"))
1322 (when (string-match "\\(.+\\)::.+" raw-path)
1323 (setq raw-path (match-string 1 raw-path)))
1324 (if (file-name-absolute-p raw-path)
1325 (concat "file://" (expand-file-name raw-path))
1326 ;; TODO: Not implemented yet. Concat also:
1327 ;; (org-export-directory :LaTeX info)
1328 (concat "file://" raw-path)))
1329 (t raw-path)))
1330 protocol)
1331 (cond
1332 ;; Image file.
1333 (imagep (org-e-latex-link--inline-image path info))
1334 ;; Id: for now, assume it's an internal link. TODO: do something
1335 ;; to check if it isn't in the current file.
1336 ((string= type "id")
1337 (format "\\hyperref[%s]{%s}" path (or desc path)))
1338 ;; Custom-id, target or radioed target: replace link with the
1339 ;; normalized custom-id/target name.
1340 ((member type '("custom-id" "target" "radio"))
1341 (format "\\hyperref[%s]{%s}"
1342 (org-export-solidify-link-text path)
1343 (or desc (org-export-secondary-string path 'e-latex info))))
1344 ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
1345 ;; the destination of the link.
1346 ((string= type "fuzzy")
1347 (let ((destination (org-export-resolve-fuzzy-link link info)))
1348 (cond
1349 ;; Target match.
1350 ((stringp destination)
1351 (format "\\hyperref[%s]{%s}"
1352 (org-export-solidify-link-text destination)
1353 (or desc
1354 (org-export-secondary-string
1355 (org-element-get-property :raw-link link) 'e-latex info))))
1356 ;; Headline match.
1357 ((integerp destination)
1358 (format "\\hyperref[headline-%d]{%s}"
1359 destination
1360 (or desc
1361 (org-export-secondary-string
1362 (org-element-get-property :raw-link link) 'e-latex info))))
1363 ;; No match.
1364 (t (format "\\texttt{%s}"
1365 (or desc
1366 (org-export-secondary-string
1367 (org-element-get-property :raw-link link)
1368 'e-latex info)))))))
1369 ;; Coderef: replace link with the reference name or the
1370 ;; equivalent line number.
1371 ((string= type "coderef")
1372 (format (org-export-get-coderef-format path (or desc ""))
1373 (org-export-resolve-coderef path info)))
1374 ;; Link type is handled by a special function.
1375 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1376 (funcall protocol (org-link-unescape path) desc 'latex))
1377 ;; External link with a description part.
1378 ((and path desc) (format "\\href{%s}{%s}" path desc))
1379 ;; External link without a description part.
1380 (path (format "\\url{%s}" path))
1381 ;; No path, only description. Try to do something useful.
1382 (t (format "\\texttt{%s}" desc)))))
1385 ;;;; Babel Call
1387 ;; Babel Calls are ignored.
1390 ;;;; Macro
1392 (defun org-e-latex-macro (macro contents info)
1393 "Transcode a MACRO element from Org to LaTeX.
1394 CONTENTS is nil. INFO is a plist holding contextual information."
1395 ;; Use available tools.
1396 (org-export-expand-macro macro info))
1399 ;;;; Paragraph
1401 (defun org-e-latex-paragraph (paragraph contents info)
1402 "Transcode a PARAGRAPH element from Org to LaTeX.
1403 CONTENTS is the contents of the paragraph, as a string. INFO is
1404 the plist used as a communication channel."
1405 contents)
1408 ;;;; Plain List
1410 (defun org-e-latex-plain-list (plain-list contents info)
1411 "Transcode a PLAIN-LIST element from Org to LaTeX.
1412 CONTENTS is the contents of the list. INFO is a plist holding
1413 contextual information."
1414 (let* ((type (org-element-get-property :type plain-list))
1415 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1416 "inparadesc" "asparadesc"))
1417 (paralist-regexp (concat
1418 "\\("
1419 (mapconcat 'identity paralist-types "\\|")
1420 "\\)"))
1421 (attr (mapconcat #'identity
1422 (org-element-get-property :attr_latex plain-list)
1423 " "))
1424 (latex-type (cond
1425 ((and attr
1426 (string-match
1427 (format "\\<%s\\>" paralist-regexp) attr))
1428 (match-string 1 attr))
1429 ((eq type 'ordered) "enumerate")
1430 ((eq type 'unordered) "itemize")
1431 ((eq type 'descriptive) "description"))))
1432 (org-e-latex--wrap-label
1433 plain-list
1434 (format "\\begin{%s}%s\n%s\\end{%s}"
1435 latex-type
1436 ;; Once special environment, if any, has been removed, the
1437 ;; rest of the attributes will be optional arguments.
1438 ;; They will be put inside square brackets if necessary.
1439 (let ((opt (replace-regexp-in-string
1440 (format " *%s *" paralist-regexp) "" attr)))
1441 (cond ((string= opt "") "")
1442 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1443 (t (format "[%s]" opt))))
1444 contents
1445 latex-type))))
1448 ;;;; Plain Text
1450 (defun org-e-latex-plain-text (text info)
1451 "Transcode a TEXT string from Org to LaTeX.
1452 TEXT is the string to transcode. INFO is a plist holding
1453 contextual information."
1454 ;; Protect %, #, &, $, ~, ^, _, { and }.
1455 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1456 (setq text
1457 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1458 ;; Protect \
1459 (setq text (replace-regexp-in-string
1460 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1461 "$\\backslash$" text nil t 1))
1462 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1463 (let ((case-fold-search nil)
1464 (start 0))
1465 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1466 (setq text (replace-match
1467 (format "\\%s{}" (match-string 1 text)) nil t text)
1468 start (match-end 0))))
1469 ;; Handle quotation marks
1470 (setq text (org-e-latex--quotation-marks text info))
1471 ;; Convert special strings.
1472 (when (plist-get info :with-special-strings)
1473 (while (string-match (regexp-quote "...") text)
1474 (setq text (replace-match "\\ldots{}" nil t text))))
1475 ;; Handle break preservation if required.
1476 (when (plist-get info :preserve-breaks)
1477 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1478 text)))
1479 ;; Return value.
1480 text)
1483 ;;;; Property Drawer
1485 (defun org-e-latex-property-drawer (property-drawer contents info)
1486 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1487 CONTENTS is nil. INFO is a plist holding contextual
1488 information."
1489 ;; The property drawer isn't exported but we want separating blank
1490 ;; lines nonetheless.
1494 ;;;; Quote Block
1496 (defun org-e-latex-quote-block (quote-block contents info)
1497 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1498 CONTENTS holds the contents of the block. INFO is a plist
1499 holding contextual information."
1500 (org-e-latex--wrap-label
1501 quote-block
1502 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1505 ;;;; Quote Section
1507 (defun org-e-latex-quote-section (quote-section contents info)
1508 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1509 CONTENTS is nil. INFO is a plist holding contextual information."
1510 (let ((value (org-remove-indentation
1511 (org-element-get-property :value quote-section))))
1512 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1515 ;;;; Section
1517 (defun org-e-latex-section (section contents info)
1518 "Transcode a SECTION element from Org to LaTeX.
1519 CONTENTS holds the contents of the section. INFO is a plist
1520 holding contextual information."
1521 contents)
1524 ;;;; Radio Target
1526 (defun org-e-latex-radio-target (radio-target text info)
1527 "Transcode a RADIO-TARGET object from Org to LaTeX.
1528 TEXT is the text of the target. INFO is a plist holding
1529 contextual information."
1530 (format "\\label{%s}%s"
1531 (org-export-solidify-link-text
1532 (org-element-get-property :raw-value radio-target))
1533 text))
1536 ;;;; Special Block
1538 (defun org-e-latex-special-block (special-block contents info)
1539 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1540 CONTENTS holds the contents of the block. INFO is a plist
1541 holding contextual information."
1542 (let ((type (downcase (org-element-get-property :type special-block))))
1543 (org-e-latex--wrap-label
1544 special-block
1545 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1548 ;;;; Src Block
1550 (defun org-e-latex-src-block (src-block contents info)
1551 "Transcode a SRC-BLOCK element from Org to LaTeX.
1552 CONTENTS holds the contents of the item. INFO is a plist holding
1553 contextual information."
1554 (let* ((lang (org-element-get-property :language src-block))
1555 (code (org-export-handle-code src-block info))
1556 (caption (org-element-get-property :caption src-block))
1557 (label (org-element-get-property :name src-block))
1558 (custom-env (and lang
1559 (cadr (assq (intern lang)
1560 org-e-latex-custom-lang-environments)))))
1561 (cond
1562 ;; No source fontification.
1563 ((not org-e-latex-listings)
1564 (let ((caption-str (org-e-latex--caption/label-string
1565 caption label info))
1566 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1567 (format (or float-env "%s")
1568 (concat
1569 caption-str
1570 (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
1571 ;; Custom environment.
1572 (custom-env
1573 (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
1574 ;; Use minted package.
1575 ((eq org-e-latex-listings 'minted)
1576 (let* ((mint-lang (or (cadr (assq (intern lang) org-e-latex-minted-langs))
1577 lang))
1578 (float-env (when (or label caption)
1579 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1580 (org-e-latex--caption/label-string
1581 caption label info))))
1582 (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1583 (org-e-latex--make-option-string
1584 org-e-latex-minted-options)
1585 mint-lang code)))
1586 (if float-env (format float-env body) body)))
1587 ;; Use listings package.
1589 (let ((lst-lang (or (cadr (assq (intern lang) org-e-latex-listings-langs))
1590 lang))
1591 (caption-str (and caption
1592 (org-export-secondary-string
1593 (org-element-get-property :caption src-block)
1594 'e-latex info))))
1595 (concat (format "\\lstset{%s}\n"
1596 (org-e-latex--make-option-string
1597 (append org-e-latex-listings-options
1598 `(("language" ,lst-lang))
1599 (when label `(("label" ,label)))
1600 (when caption-str
1601 `(("caption" ,caption-str))))))
1602 (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
1605 ;;;; Statistics Cookie
1607 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1608 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1609 CONTENTS is nil. INFO is a plist holding contextual information."
1610 (org-element-get-property :value statistics-cookie))
1613 ;;;; Subscript
1615 (defun org-e-latex-subscript (subscript contents info)
1616 "Transcode a SUBSCRIPT object from Org to LaTeX.
1617 CONTENTS is the contents of the object. INFO is a plist holding
1618 contextual information."
1619 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1622 ;;;; Superscript
1624 (defun org-e-latex-superscript (superscript contents info)
1625 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1626 CONTENTS is the contents of the object. INFO is a plist holding
1627 contextual information."
1628 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1631 ;;;; Table
1633 (defun org-e-latex-table--format-string (table info)
1634 "Return an appropriate format string for TABLE.
1636 INFO is the plist containing format info about the table, as
1637 returned by `org-export-table-format-info'.
1639 The format string one placeholder for the body of the table."
1640 (let* ((label (org-element-get-property :name table))
1641 (caption (org-e-latex--caption/label-string
1642 (org-element-get-property :caption table) label info))
1643 (attr (mapconcat #'identity
1644 (org-element-get-property :attr_latex table)
1645 " "))
1646 ;; Determine alignment string.
1647 (alignment (org-e-latex-table--align-string attr info))
1648 ;; Determine environment for the table: longtable, tabular...
1649 (table-env (cond
1650 ((not attr) org-e-latex-default-table-environment)
1651 ((string-match "\\<longtable\\>" attr) "longtable")
1652 ((string-match "\\(tabular.\\)" attr)
1653 (org-match-string-no-properties 1 attr))
1654 (t org-e-latex-default-table-environment)))
1655 ;; If table is a float, determine environment: table or table*.
1656 (float-env (cond
1657 ((string= "longtable" table-env) nil)
1658 ((and attr
1659 (or (string-match (regexp-quote "table*") attr)
1660 (string-match "\\<multicolumn\\>" attr)))
1661 "table*")
1662 ((or (not (string= caption "")) label) "table")))
1663 ;; Extract others display options.
1664 (width (and attr
1665 (string-match "\\<width=\\(\\S-+\\)" attr)
1666 (org-match-string-no-properties 1 attr)))
1667 (placement (if (and attr
1668 (string-match "\\<placement=\\(\\S-+\\)" attr))
1669 (org-match-string-no-properties 1 attr)
1670 (concat "["
1671 org-e-latex-default-figure-position
1672 "]"))))
1673 ;; Prepare the final format string for the table.
1674 (cond
1675 ;; Longtable.
1676 ((string= "longtable" table-env)
1677 (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1678 alignment
1679 (if (or (not org-e-latex-table-caption-above)
1680 (string= "" caption))
1682 (concat (org-trim caption) "\\\\"))
1683 (if (or org-e-latex-table-caption-above
1684 (string= "" caption))
1686 (concat (org-trim caption) "\\\\\n"))))
1687 ;; Others.
1688 (t (concat (when float-env
1689 (concat
1690 (format "\\begin{%s}%s\n" float-env placement)
1691 (if org-e-latex-table-caption-above caption "")))
1692 (when org-e-latex-tables-centered "\\begin{center}\n")
1693 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1694 table-env
1695 (if width (format "{%s}" width) "")
1696 alignment
1697 table-env)
1698 (when org-e-latex-tables-centered "\n\\end{center}")
1699 (when float-env
1700 (concat (if org-e-latex-table-caption-above "" caption)
1701 (format "\n\\end{%s}" float-env))))))))
1703 (defun org-e-latex-table--align-string (attr info)
1704 "Return an appropriate LaTeX alignment string.
1706 INFO is the plist containing format info about the table, as
1707 returned by `org-export-table-format-info'."
1708 (or (and attr
1709 (string-match "\\<align=\\(\\S-+\\)" attr)
1710 (match-string 1 attr))
1711 (let* ((align (copy-sequence (plist-get info :alignment)))
1712 (colgroups (copy-sequence (plist-get info :column-groups)))
1713 (cols (length align))
1714 (separators (make-vector (1+ cols) "")))
1715 ;; Ignore the first column if it's special.
1716 (when (plist-get info :special-column-p)
1717 (aset align 0 "") (aset colgroups 0 nil))
1718 (let ((col 0))
1719 (mapc (lambda (el)
1720 (let ((gr (aref colgroups col)))
1721 (when (memq gr '(start start-end))
1722 (aset separators col "|"))
1723 (when (memq gr '(end start-end))
1724 (aset separators (1+ col) "|")))
1725 (incf col))
1726 align))
1727 ;; Build the LaTeX specific alignment string.
1728 (loop for al across align
1729 for sep across separators
1730 concat (concat sep al) into output
1731 finally return (concat output (aref separators cols))))))
1733 (defun org-e-latex-table (table contents info)
1734 "Transcode a TABLE element from Org to LaTeX.
1735 CONTENTS is nil. INFO is a plist holding contextual information."
1736 (let ((attr (mapconcat #'identity
1737 (org-element-get-property :attr_latex table)
1738 " "))
1739 (raw-table (org-element-get-property :raw-table table)))
1740 (cond
1741 ;; Case 1: verbatim table.
1742 ((or org-e-latex-tables-verbatim
1743 (and attr (string-match "\\<verbatim\\>" attr)))
1744 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1745 (org-export-clean-table
1746 raw-table
1747 (plist-get (org-export-table-format-info raw-table)
1748 :special-column-p))))
1749 ;; Case 2: table.el table. Convert it using appropriate tools.
1750 ((eq (org-element-get-property :type table) 'table.el)
1751 (require 'table)
1752 ;; Ensure "*org-export-table*" buffer is empty.
1753 (and (get-buffer "*org-export-table*")
1754 (kill-buffer (get-buffer "*org-export-table*")))
1755 (let ((output (with-temp-buffer
1756 (insert raw-table)
1757 (goto-char 1)
1758 (re-search-forward "^[ \t]*|[^|]" nil t)
1759 (table-generate-source 'latex "*org-export-table*")
1760 (with-current-buffer "*org-export-table*"
1761 (org-trim (buffer-string))))))
1762 (kill-buffer (get-buffer "*org-export-table*"))
1763 ;; Remove left out comments.
1764 (while (string-match "^%.*\n" output)
1765 (setq output (replace-match "" t t output)))
1766 ;; When the "rmlines" attribute is provided, remove all hlines
1767 ;; but the the one separating heading from the table body.
1768 (when (and attr (string-match "\\<rmlines\\>" attr))
1769 (let ((n 0) (pos 0))
1770 (while (and (< (length output) pos)
1771 (setq pos (string-match "^\\\\hline\n?" output pos)))
1772 (incf n)
1773 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
1774 (if org-e-latex-tables-centered
1775 (format "\\begin{center}\n%s\n\\end{center}" output)
1776 output)))
1777 ;; Case 3: Standard table.
1779 (let* ((table-info (org-export-table-format-info raw-table))
1780 (clean-table (org-export-clean-table
1781 raw-table (plist-get table-info :special-column-p)))
1782 (columns-number (length (plist-get table-info :alignment))))
1783 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
1784 ;; particular, send each cell to
1785 ;; `org-element-parse-secondary-string' to expand any Org
1786 ;; object within. Eventually, flesh the format string out with
1787 ;; the table.
1788 (format (org-e-latex-table--format-string table table-info)
1789 (orgtbl-to-latex
1790 (mapcar
1791 (lambda (row)
1792 (if (string-match org-table-hline-regexp row)
1793 'hline
1794 (mapcar
1795 (lambda (cell)
1796 (org-export-secondary-string
1797 (org-element-parse-secondary-string
1798 cell
1799 (cdr (assq 'table org-element-string-restrictions)))
1800 'e-latex info))
1801 (org-split-string row "[ \t]*|[ \t]*"))))
1802 (org-split-string clean-table "\n"))
1803 `(:tstart nil :tend nil
1804 ;; Longtable environment requires specific
1805 ;; header line end.
1806 :hlend ,(and attr
1807 (string-match "\\<longtable\\>" attr)
1808 (format "\\\\
1809 \\hline
1810 \\endhead
1811 \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
1812 \\endfoot
1813 \\endlastfoot"
1814 columns-number))))))))))
1817 ;;;; Target
1819 (defun org-e-latex-target (target text info)
1820 "Transcode a TARGET object from Org to LaTeX.
1821 TEXT is the text of the target. INFO is a plist holding
1822 contextual information."
1823 (format "\\label{%s}%s"
1824 (org-export-solidify-link-text
1825 (org-element-get-property :raw-value target))
1826 text))
1829 ;;;; Time-stamp
1831 (defun org-e-latex-time-stamp (time-stamp contents info)
1832 "Transcode a TIME-STAMP object from Org to LaTeX.
1833 CONTENTS is nil. INFO is a plist holding contextual information."
1834 (let ((value (org-element-get-property :value time-stamp))
1835 (type (org-element-get-property :type time-stamp))
1836 (appt-type (org-element-get-property :appt-type time-stamp)))
1837 (concat (cond ((eq appt-type 'scheduled)
1838 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
1839 ((eq appt-type 'deadline)
1840 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
1841 ((eq appt-type 'closed)
1842 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
1843 (cond ((memq type '(active active-range))
1844 (format org-e-latex-active-timestamp-format value))
1845 ((memq type '(inactive inactive-range))
1846 (format org-e-latex-inactive-timestamp-format value))
1848 (format org-e-latex-diary-timestamp-format value))))))
1851 ;;;; Verbatim
1853 (defun org-e-latex-verbatim (element contents info)
1854 "Return verbatim text in LaTeX."
1855 (let ((fmt (cdr (assoc (org-element-get-property :marker element)
1856 org-e-latex-emphasis-alist)))
1857 (value (org-element-get-property :value element)))
1858 (cond
1859 ;; Handle the `verb' special case.
1860 ((eq 'verb fmt)
1861 (let ((separator (org-e-latex--find-verb-separator value)))
1862 (concat "\\verb" separator value separator)))
1863 ;; Handle the `protectedtexttt' special case.
1864 ((eq 'protectedtexttt fmt)
1865 (let ((start 0)
1866 (trans '(("\\" . "\\textbackslash{}")
1867 ("~" . "\\textasciitilde{}")
1868 ("^" . "\\textasciicircum{}")))
1869 (rtn "")
1870 char)
1871 (while (string-match "[\\{}$%&_#~^]" value)
1872 (setq char (match-string 0 value))
1873 (if (> (match-beginning 0) 0)
1874 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
1875 (setq value (substring value (1+ (match-beginning 0))))
1876 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1877 rtn (concat rtn char)))
1878 (setq value (concat rtn value)
1879 fmt "\\texttt{%s}")
1880 (while (string-match "--" value)
1881 (setq value (replace-match "-{}-" t t value)))
1882 (format fmt value)))
1883 ;; Else use format string.
1884 (t (format fmt value)))))
1887 ;;;; Verse Block
1889 (defun org-e-latex-verse-block (verse-block contents info)
1890 "Transcode a VERSE-BLOCK element from Org to LaTeX.
1891 CONTENTS is nil. INFO is a plist holding contextual information."
1892 (org-e-latex--wrap-label
1893 verse-block
1894 ;; In a verse environment, add a line break to each newline
1895 ;; character and change each white space at beginning of a line
1896 ;; into a space of 1 em. Also change each blank line with
1897 ;; a vertical space of 1 em.
1898 (progn
1899 (setq contents (replace-regexp-in-string
1900 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1901 (replace-regexp-in-string
1902 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1903 (org-remove-indentation
1904 (org-export-secondary-string
1905 (org-element-get-property :value verse-block)
1906 'e-latex info)))))
1907 (while (string-match "^[ \t]+" contents)
1908 (let ((new-str (format "\\hspace*{%dem}"
1909 (length (match-string 0 contents)))))
1910 (setq contents (replace-match new-str nil t contents))))
1911 (format "\\begin{verse}\n%s\\end{verse}" contents))))
1915 ;;; Compilation
1917 (defun org-e-latex-compile (texfile)
1918 "Compile a TeX file.
1920 TEXFILE is the name of the file being compiled. Processing is
1921 done through the command specified in `org-e-latex-pdf-process'.
1923 Return PDF file name or an error if it couldn't be produced."
1924 (let* ((wconfig (current-window-configuration))
1925 (texfile (file-truename texfile))
1926 (base (file-name-sans-extension texfile))
1927 errors)
1928 (message (format "Processing LaTeX file " texfile "..." texfile))
1929 (unwind-protect
1930 (progn
1931 (cond
1932 ;; A function is provided: Apply it.
1933 ((functionp org-latex-to-pdf-process)
1934 (funcall org-latex-to-pdf-process (shell-quote-argument texfile)))
1935 ;; A list is provided: Replace %b, %f and %o with appropriate
1936 ;; values in each command before applying it. Output is
1937 ;; redirected to "*Org PDF LaTeX Output*" buffer.
1938 ((consp org-e-latex-pdf-process)
1939 (let* ((out-dir (or (file-name-directory texfile) "./"))
1940 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
1941 (mapc
1942 (lambda (command)
1943 (shell-command
1944 (replace-regexp-in-string
1945 "%b" (shell-quote-argument base)
1946 (replace-regexp-in-string
1947 "%f" (shell-quote-argument texfile)
1948 (replace-regexp-in-string
1949 "%o" (shell-quote-argument out-dir) command)))
1950 outbuf))
1951 org-e-latex-pdf-process)
1952 ;; Collect standard errors from output buffer.
1953 (setq errors (org-e-latex-collect-errors outbuf))))
1954 (t (error "No valid command to process to PDF")))
1955 (let ((pdffile (concat base ".pdf")))
1956 ;; Check for process failure. Provide collected errors if
1957 ;; possible.
1958 (if (not (file-exists-p pdffile))
1959 (error (concat (format "PDF file %s wasn't produced" pdffile)
1960 (when errors (concat ": " errors))))
1961 ;; Else remove log files, when specified, and signal end of
1962 ;; process to user, along with any error encountered.
1963 (when org-e-latex-remove-logfiles
1964 (dolist (ext org-e-latex-logfiles-extensions)
1965 (let ((file (concat base "." ext)))
1966 (when (file-exists-p file) (delete-file file)))))
1967 (message (concat "Process completed"
1968 (if (not errors) "."
1969 (concat " with errors: " errors)))))
1970 ;; Return output file name.
1971 pdffile))
1972 (set-window-configuration wconfig))))
1974 (defun org-e-latex-collect-errors (buffer)
1975 "Collect some kind of errors from \"pdflatex\" command output.
1977 BUFFER is the buffer containing output.
1979 Return collected error types as a string, or nil if there was
1980 none."
1981 (with-current-buffer buffer
1982 (save-excursion
1983 (goto-char (point-max))
1984 ;; Find final "pdflatex" run.
1985 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
1986 (let ((case-fold-search t)
1987 (errors ""))
1988 (when (save-excursion
1989 (re-search-forward "Reference.*?undefined" nil t))
1990 (setq errors (concat errors " [undefined reference]")))
1991 (when (save-excursion
1992 (re-search-forward "Citation.*?undefined" nil t))
1993 (setq errors (concat errors " [undefined citation]")))
1994 (when (save-excursion
1995 (re-search-forward "Undefined control sequence" nil t))
1996 (setq errors (concat errors " [undefined control sequence]")))
1997 (when (save-excursion
1998 (re-search-forward "^! LaTeX.*?Error" nil t))
1999 (setq errors (concat errors " [LaTeX error]")))
2000 (when (save-excursion
2001 (re-search-forward "^! Package.*?Error" nil t))
2002 (setq errors (concat errors " [package error]")))
2003 (and (org-string-nw-p errors) (org-trim errors)))))))
2006 (provide 'org-e-latex)
2007 ;;; org-e-latex.el ends here