EXPERIMENTAL/org-e-latex.el: Fix defcustom groups
[org-mode.git] / EXPERIMENTAL / org-e-latex.el
blob9813ef400c6c9da9478443424034a96745475fd8
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 example-block 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
929 (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 (let* ((level (plist-get (plist-get info :parent-properties) :level))
1173 (counter (let ((count (org-element-get-property :counter item)))
1174 (and count
1175 (< level 4)
1176 (format "\\setcounter{enum%s}{%s}\n"
1177 (nth level '("i" "ii" "iii" "iv"))
1178 (1- count)))))
1179 (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
1180 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1181 ((eq checkbox 'off) "$\\Box$ ")
1182 ((eq checkbox 'trans) "$\\boxminus$ "))))
1183 (tag (let ((tag (org-element-get-property :tag item)))
1184 (and tag
1185 (format "[%s]" (org-export-secondary-string
1186 tag 'e-latex info))))))
1187 (concat counter "\\item" tag " " checkbox contents)))
1190 ;;;; Keyword
1192 (defun org-e-latex-keyword (keyword contents info)
1193 "Transcode a KEYWORD element from Org to LaTeX.
1194 CONTENTS is nil. INFO is a plist holding contextual information."
1195 (let ((key (downcase (org-element-get-property :key keyword)))
1196 (value (org-element-get-property :value keyword)))
1197 (cond
1198 ((string= key "latex") value)
1199 ((string= key "index") (format "\\index{%s}" value))
1200 ((string= key "target")
1201 (format "\\label{%s}" (org-export-solidify-link-text value)))
1202 ((string= key "toc")
1203 (let ((value (downcase value)))
1204 (cond
1205 ((string-match "\\<headlines\\>" value)
1206 (let ((depth (or (and (string-match "[0-9]+" value)
1207 (string-to-number (match-string 0 value)))
1208 (plist-get info :with-toc))))
1209 (concat
1210 (when (wholenump depth)
1211 (format "\\setcounter{tocdepth}{%s}\n" depth))
1212 "\\tableofcontents")))
1213 ((string= "tables" value) "\\listoftables")
1214 ((string= "figures" value) "\\listoffigures")
1215 ((string= "listings" value) "\\listoflistings"))))
1216 ((string= key "include")
1217 (org-export-included-file keyword 'e-latex info)))))
1220 ;;;; Latex Environment
1222 (defun org-e-latex-latex-environment (latex-environment contents info)
1223 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1224 CONTENTS is nil. INFO is a plist holding contextual information."
1225 (org-e-latex--wrap-label
1226 latex-environment
1227 (org-remove-indentation (org-element-get-property :value latex-environment))))
1230 ;;;; Latex Fragment
1232 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1233 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1234 CONTENTS is nil. INFO is a plist holding contextual information."
1235 (org-element-get-property :value latex-fragment))
1238 ;;;; Line Break
1240 (defun org-e-latex-line-break (line-break contents info)
1241 "Transcode a LINE-BREAK object from Org to LaTeX.
1242 CONTENTS is nil. INFO is a plist holding contextual information."
1243 "\\\\")
1246 ;;;; Link
1248 (defun org-e-latex-link--inline-image (path info)
1249 "Return LaTeX code for an image at PATH.
1250 INFO is a plist containing export options."
1251 (let* ((parent-props (plist-get info :parent-properties))
1252 (caption (org-e-latex--caption/label-string
1253 (plist-get parent-props :caption)
1254 (plist-get parent-props :name)
1255 info))
1256 ;; Retrieve latex attributes from the element around.
1257 (attr (let ((raw-attr
1258 (mapconcat #'identity
1259 (plist-get parent-props :attr_latex) " ")))
1260 (unless (string= raw-attr "") raw-attr)))
1261 (disposition
1262 (cond
1263 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1264 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1265 ((or (and attr (string-match "\\<float\\>" attr))
1266 (not (string= caption "")))
1267 'float)))
1268 (placement
1269 (cond
1270 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1271 (org-match-string-no-properties 1 attr))
1272 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1273 ((eq disposition 'float)
1274 (concat "[" org-e-latex-default-figure-position "]"))
1275 (t ""))))
1276 ;; Now clear ATTR from any special keyword and set a default
1277 ;; value if nothing is left.
1278 (if (not attr)
1279 (setq attr "")
1280 (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
1281 attr)
1282 (replace-match "" nil nil attr))
1283 (setq attr (org-trim attr)))
1284 (setq attr (cond ((not (string= attr "")) attr)
1285 ((eq disposition 'float) "width=0.7\\textwidth")
1286 ((eq disposition 'wrap) "width=0.48\\textwidth")
1287 (t (or org-e-latex-image-default-option ""))))
1288 ;; Return proper string, depending on DISPOSITION.
1289 (case disposition
1290 ('wrap (format "\\begin{wrapfigure}%s
1291 \\centering
1292 \\includegraphics[%s]{%s}
1293 %s\\end{wrapfigure}" placement attr path caption))
1294 ('mulicolumn (format "\\begin{figure*}%s
1295 \\centering
1296 \\includegraphics[%s]{%s}
1297 %s\\end{figure*}" placement attr path caption))
1298 ('float (format "\\begin{figure}%s
1299 \\centering
1300 \\includegraphics[%s]{%s}
1301 %s\\end{figure}" placement attr path caption))
1302 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1304 (defun org-e-latex-link (link desc info)
1305 "Transcode a LINK object from Org to LaTeX.
1307 DESC is the description part of the link, or the empty string.
1308 INFO is a plist holding contextual information. See
1309 `org-export-data'."
1310 (let* ((type (org-element-get-property :type link))
1311 (raw-path (org-element-get-property :path link))
1312 ;; Ensure DESC really exists, or set it to nil.
1313 (desc (and (not (string= desc "")) desc))
1314 (imagep (org-export-inline-image-p
1315 link org-e-latex-inline-image-extensions))
1316 (path (cond
1317 ((member type '("http" "https" "ftp" "mailto"))
1318 (concat type ":" raw-path))
1319 ((and (not imagep) (string= type "file"))
1320 (when (string-match "\\(.+\\)::.+" raw-path)
1321 (setq raw-path (match-string 1 raw-path)))
1322 (if (file-name-absolute-p raw-path)
1323 (concat "file://" (expand-file-name raw-path))
1324 ;; TODO: Not implemented yet. Concat also:
1325 ;; (org-export-directory :LaTeX info)
1326 (concat "file://" raw-path)))
1327 (t raw-path)))
1328 protocol)
1329 (cond
1330 ;; Image file.
1331 (imagep (org-e-latex-link--inline-image path info))
1332 ;; Id: for now, assume it's an internal link. TODO: do something
1333 ;; to check if it isn't in the current file.
1334 ((string= type "id")
1335 (format "\\hyperref[%s]{%s}" path (or desc path)))
1336 ;; Custom-id, target or radioed target: replace link with the
1337 ;; normalized custom-id/target name.
1338 ((member type '("custom-id" "target" "radio"))
1339 (format "\\hyperref[%s]{%s}"
1340 (org-export-solidify-link-text path)
1341 (or desc (org-export-secondary-string path 'e-latex info))))
1342 ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
1343 ;; the destination of the link.
1344 ((string= type "fuzzy")
1345 (let ((destination (org-export-resolve-fuzzy-link link info)))
1346 (cond
1347 ;; Target match.
1348 ((stringp destination)
1349 (format "\\hyperref[%s]{%s}"
1350 (org-export-solidify-link-text destination)
1351 (or desc
1352 (org-export-secondary-string
1353 (org-element-get-property :raw-link link) 'e-latex info))))
1354 ;; Headline match.
1355 ((integerp destination)
1356 (format "\\hyperref[headline-%d]{%s}"
1357 destination
1358 (or desc
1359 (org-export-secondary-string
1360 (org-element-get-property :raw-link link) 'e-latex info))))
1361 ;; No match.
1362 (t (format "\\texttt{%s}"
1363 (or desc
1364 (org-export-secondary-string
1365 (org-element-get-property :raw-link link)
1366 'e-latex info)))))))
1367 ;; Coderef: replace link with the reference name or the
1368 ;; equivalent line number.
1369 ((string= type "coderef")
1370 (format (org-export-get-coderef-format path (or desc ""))
1371 (cdr (assoc path (plist-get info :code-refs)))))
1372 ;; Link type is handled by a special function.
1373 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1374 (funcall protocol (org-link-unescape path) desc 'latex))
1375 ;; External link with a description part.
1376 ((and path desc) (format "\\href{%s}{%s}" path desc))
1377 ;; External link without a description part.
1378 (path (format "\\url{%s}" path))
1379 ;; No path, only description. Try to do something useful.
1380 (t (format "\\texttt{%s}" desc)))))
1383 ;;;; Babel Call
1385 ;; Babel Calls are ignored.
1388 ;;;; Macro
1390 (defun org-e-latex-macro (macro contents info)
1391 "Transcode a MACRO element from Org to LaTeX.
1392 CONTENTS is nil. INFO is a plist holding contextual information."
1393 ;; Use available tools.
1394 (org-export-expand-macro macro info))
1397 ;;;; Paragraph
1399 (defun org-e-latex-paragraph (paragraph contents info)
1400 "Transcode a PARAGRAPH element from Org to LaTeX.
1401 CONTENTS is the contents of the paragraph, as a string. INFO is
1402 the plist used as a communication channel."
1403 contents)
1406 ;;;; Plain List
1408 (defun org-e-latex-plain-list (plain-list contents info)
1409 "Transcode a PLAIN-LIST element from Org to LaTeX.
1410 CONTENTS is the contents of the list. INFO is a plist holding
1411 contextual information."
1412 (let* ((type (org-element-get-property :type plain-list))
1413 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1414 "inparadesc" "asparadesc"))
1415 (paralist-regexp (concat
1416 "\\("
1417 (mapconcat 'identity paralist-types "\\|")
1418 "\\)"))
1419 (attr (mapconcat #'identity
1420 (org-element-get-property :attr_latex plain-list)
1421 " "))
1422 (latex-type (cond
1423 ((and attr
1424 (string-match
1425 (format "\\<%s\\>" paralist-regexp) attr))
1426 (match-string 1 attr))
1427 ((eq type 'ordered) "enumerate")
1428 ((eq type 'unordered) "itemize")
1429 ((eq type 'descriptive) "description"))))
1430 (org-e-latex--wrap-label
1431 plain-list
1432 (format "\\begin{%s}%s\n%s\\end{%s}"
1433 latex-type
1434 ;; Once special environment, if any, has been removed, the
1435 ;; rest of the attributes will be optional arguments.
1436 ;; They will be put inside square brackets if necessary.
1437 (let ((opt (replace-regexp-in-string
1438 (format " *%s *" paralist-regexp) "" attr)))
1439 (cond ((string= opt "") "")
1440 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1441 (t (format "[%s]" opt))))
1442 contents
1443 latex-type))))
1446 ;;;; Plain Text
1448 (defun org-e-latex-plain-text (text info)
1449 "Transcode a TEXT string from Org to LaTeX.
1450 TEXT is the string to transcode. INFO is a plist holding
1451 contextual information."
1452 ;; Protect %, #, &, $, ~, ^, _, { and }.
1453 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1454 (setq text
1455 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1456 ;; Protect \
1457 (setq text (replace-regexp-in-string
1458 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1459 "$\\backslash$" text nil t 1))
1460 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1461 (let ((case-fold-search nil)
1462 (start 0))
1463 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1464 (setq text (replace-match
1465 (format "\\%s{}" (match-string 1 text)) nil t text)
1466 start (match-end 0))))
1467 ;; Handle quotation marks
1468 (setq text (org-e-latex--quotation-marks text info))
1469 ;; Convert special strings.
1470 (when (plist-get info :with-special-strings)
1471 (while (string-match (regexp-quote "...") text)
1472 (setq text (replace-match "\\ldots{}" nil t text))))
1473 ;; Handle break preservation if required.
1474 (when (plist-get info :preserve-breaks)
1475 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1476 text)))
1477 ;; Return value.
1478 text)
1481 ;;;; Property Drawer
1483 (defun org-e-latex-property-drawer (property-drawer contents info)
1484 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1485 CONTENTS is nil. INFO is a plist holding contextual
1486 information."
1487 ;; The property drawer isn't exported but we want separating blank
1488 ;; lines nonetheless.
1492 ;;;; Quote Block
1494 (defun org-e-latex-quote-block (quote-block contents info)
1495 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1496 CONTENTS holds the contents of the block. INFO is a plist
1497 holding contextual information."
1498 (org-e-latex--wrap-label
1499 quote-block
1500 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1503 ;;;; Quote Section
1505 (defun org-e-latex-quote-section (quote-section contents info)
1506 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1507 CONTENTS is nil. INFO is a plist holding contextual information."
1508 (let ((value (org-remove-indentation
1509 (org-element-get-property :value quote-section))))
1510 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1513 ;;;; Radio Target
1515 (defun org-e-latex-radio-target (radio-target text info)
1516 "Transcode a RADIO-TARGET object from Org to LaTeX.
1517 TEXT is the text of the target. INFO is a plist holding
1518 contextual information."
1519 (format "\\label{%s}%s"
1520 (org-export-solidify-link-text
1521 (org-element-get-property :raw-value radio-target))
1522 text))
1525 ;;;; Special Block
1527 (defun org-e-latex-special-block (special-block contents info)
1528 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1529 CONTENTS holds the contents of the block. INFO is a plist
1530 holding contextual information."
1531 (let ((type (downcase (org-element-get-property :type special-block))))
1532 (org-e-latex--wrap-label
1533 special-block
1534 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1537 ;;;; Src Block
1539 (defun org-e-latex-src-block (src-block contents info)
1540 "Transcode a SRC-BLOCK element from Org to LaTeX.
1541 CONTENTS holds the contents of the item. INFO is a plist holding
1542 contextual information."
1543 (let* ((lang (org-element-get-property :language src-block))
1544 (code (org-export-handle-code
1545 (org-element-get-property :value src-block)
1546 (org-element-get-property :switches src-block)
1547 info lang))
1548 (caption (org-element-get-property :caption src-block))
1549 (label (org-element-get-property :name src-block))
1550 (custom-env (and lang
1551 (cadr (assq (intern lang)
1552 org-e-latex-custom-lang-environments)))))
1553 (cond
1554 ;; No source fontification.
1555 ((not org-e-latex-listings)
1556 (let ((caption-str (org-e-latex--caption/label-string
1557 caption label info))
1558 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1559 (format (or float-env "%s")
1560 (concat
1561 caption-str
1562 (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
1563 ;; Custom environment.
1564 (custom-env
1565 (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
1566 ;; Use minted package.
1567 ((eq org-e-latex-listings 'minted)
1568 (let* ((mint-lang (or (cadr (assq (intern lang) org-e-latex-minted-langs))
1569 lang))
1570 (float-env (when (or label caption)
1571 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1572 (org-e-latex--caption/label-string
1573 caption label info))))
1574 (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1575 (org-e-latex--make-option-string
1576 org-e-latex-minted-options)
1577 mint-lang code)))
1578 (if float-env (format float-env body) body)))
1579 ;; Use listings package.
1581 (let ((lst-lang (or (cadr (assq (intern lang) org-e-latex-listings-langs))
1582 lang))
1583 (caption-str (and caption
1584 (org-export-secondary-string
1585 (org-element-get-property :caption src-block)
1586 'e-latex info))))
1587 (concat (format "\\lstset{%s}\n"
1588 (org-e-latex--make-option-string
1589 (append org-e-latex-listings-options
1590 `(("language" ,lst-lang))
1591 (when label `(("label" ,label)))
1592 (when caption-str
1593 `(("caption" ,caption-str))))))
1594 (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
1597 ;;;; Statistics Cookie
1599 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1600 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1601 CONTENTS is nil. INFO is a plist holding contextual information."
1602 (org-element-get-property :value statistics-cookie))
1605 ;;;; Subscript
1607 (defun org-e-latex-subscript (subscript contents info)
1608 "Transcode a SUBSCRIPT object from Org to LaTeX.
1609 CONTENTS is the contents of the object. INFO is a plist holding
1610 contextual information."
1611 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1614 ;;;; Superscript
1616 (defun org-e-latex-superscript (superscript contents info)
1617 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1618 CONTENTS is the contents of the object. INFO is a plist holding
1619 contextual information."
1620 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1623 ;;;; Table
1625 (defun org-e-latex-table--format-string (table info)
1626 "Return an appropriate format string for TABLE.
1628 INFO is the plist containing format info about the table, as
1629 returned by `org-export-table-format-info'.
1631 The format string one placeholder for the body of the table."
1632 (let* ((label (org-element-get-property :name table))
1633 (caption (org-e-latex--caption/label-string
1634 (org-element-get-property :caption table) label info))
1635 (attr (mapconcat #'identity
1636 (org-element-get-property :attr_latex table)
1637 " "))
1638 ;; Determine alignment string.
1639 (alignment (org-e-latex-table--align-string attr info))
1640 ;; Determine environment for the table: longtable, tabular...
1641 (table-env (cond
1642 ((not attr) org-e-latex-default-table-environment)
1643 ((string-match "\\<longtable\\>" attr) "longtable")
1644 ((string-match "\\(tabular.\\)" attr)
1645 (org-match-string-no-properties 1 attr))
1646 (t org-e-latex-default-table-environment)))
1647 ;; If table is a float, determine environment: table or table*.
1648 (float-env (cond
1649 ((string= "longtable" table-env) nil)
1650 ((and attr
1651 (or (string-match (regexp-quote "table*") attr)
1652 (string-match "\\<multicolumn\\>" attr)))
1653 "table*")
1654 ((or (not (string= caption "")) label) "table")))
1655 ;; Extract others display options.
1656 (width (and attr
1657 (string-match "\\<width=\\(\\S-+\\)" attr)
1658 (org-match-string-no-properties 1 attr)))
1659 (placement (if (and attr
1660 (string-match "\\<placement=\\(\\S-+\\)" attr))
1661 (org-match-string-no-properties 1 attr)
1662 (concat "["
1663 org-e-latex-default-figure-position
1664 "]"))))
1665 ;; Prepare the final format string for the table.
1666 (cond
1667 ;; Longtable.
1668 ((string= "longtable" table-env)
1669 (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1670 alignment
1671 (if (or (not org-e-latex-table-caption-above)
1672 (string= "" caption))
1674 (concat (org-trim caption) "\\\\"))
1675 (if (or org-e-latex-table-caption-above
1676 (string= "" caption))
1678 (concat (org-trim caption) "\\\\\n"))))
1679 ;; Others.
1680 (t (concat (when float-env
1681 (concat
1682 (format "\\begin{%s}%s\n" float-env placement)
1683 (if org-e-latex-table-caption-above caption "")))
1684 (when org-e-latex-tables-centered "\\begin{center}\n")
1685 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1686 table-env
1687 (if width (format "{%s}" width) "")
1688 alignment
1689 table-env)
1690 (when org-e-latex-tables-centered "\n\\end{center}")
1691 (when float-env
1692 (concat (if org-e-latex-table-caption-above "" caption)
1693 (format "\n\\end{%s}" float-env))))))))
1695 (defun org-e-latex-table--align-string (attr info)
1696 "Return an appropriate LaTeX alignment string.
1698 INFO is the plist containing format info about the table, as
1699 returned by `org-export-table-format-info'."
1700 (or (and attr
1701 (string-match "\\<align=\\(\\S-+\\)" attr)
1702 (match-string 1 attr))
1703 (let* ((align (copy-sequence (plist-get info :alignment)))
1704 (colgroups (copy-sequence (plist-get info :column-groups)))
1705 (cols (length align))
1706 (separators (make-vector (1+ cols) "")))
1707 ;; Ignore the first column if it's special.
1708 (when (plist-get info :special-column-p)
1709 (aset align 0 "") (aset colgroups 0 nil))
1710 (let ((col 0))
1711 (mapc (lambda (el)
1712 (let ((gr (aref colgroups col)))
1713 (when (memq gr '(start start-end))
1714 (aset separators col "|"))
1715 (when (memq gr '(end start-end))
1716 (aset separators (1+ col) "|")))
1717 (incf col))
1718 align))
1719 ;; Build the LaTeX specific alignment string.
1720 (loop for al across align
1721 for sep across separators
1722 concat (concat sep al) into output
1723 finally return (concat output (aref separators cols))))))
1725 (defun org-e-latex-table (table contents info)
1726 "Transcode a TABLE element from Org to LaTeX.
1727 CONTENTS is nil. INFO is a plist holding contextual information."
1728 (let ((attr (mapconcat #'identity
1729 (org-element-get-property :attr_latex table)
1730 " "))
1731 (raw-table (org-element-get-property :raw-table table)))
1732 (cond
1733 ;; Case 1: verbatim table.
1734 ((or org-e-latex-tables-verbatim
1735 (and attr (string-match "\\<verbatim\\>" attr)))
1736 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1737 (org-export-clean-table
1738 raw-table
1739 (plist-get (org-export-table-format-info raw-table)
1740 :special-column-p))))
1741 ;; Case 2: table.el table. Convert it using appropriate tools.
1742 ((eq (org-element-get-property :type table) 'table.el)
1743 (require 'table)
1744 ;; Ensure "*org-export-table*" buffer is empty.
1745 (and (get-buffer "*org-export-table*")
1746 (kill-buffer (get-buffer "*org-export-table*")))
1747 (let ((output (with-temp-buffer
1748 (insert raw-table)
1749 (goto-char 1)
1750 (re-search-forward "^[ \t]*|[^|]" nil t)
1751 (table-generate-source 'latex "*org-export-table*")
1752 (with-current-buffer "*org-export-table*"
1753 (org-trim (buffer-string))))))
1754 (kill-buffer (get-buffer "*org-export-table*"))
1755 ;; Remove left out comments.
1756 (while (string-match "^%.*\n" output)
1757 (setq output (replace-match "" t t output)))
1758 ;; When the "rmlines" attribute is provided, remove all hlines
1759 ;; but the the one separating heading from the table body.
1760 (when (and attr (string-match "\\<rmlines\\>" attr))
1761 (let ((n 0) (pos 0))
1762 (while (and (< (length output) pos)
1763 (setq pos (string-match "^\\\\hline\n?" output pos)))
1764 (incf n)
1765 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
1766 (if org-e-latex-tables-centered
1767 (format "\\begin{center}\n%s\n\\end{center}" output)
1768 output)))
1769 ;; Case 3: Standard table.
1771 (let* ((table-info (org-export-table-format-info raw-table))
1772 (clean-table (org-export-clean-table
1773 raw-table (plist-get table-info :special-column-p)))
1774 (columns-number (length (plist-get table-info :alignment))))
1775 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
1776 ;; particular, send each cell to
1777 ;; `org-element-parse-secondary-string' to expand any Org
1778 ;; object within. Eventually, flesh the format string out with
1779 ;; the table.
1780 (format (org-e-latex-table--format-string table table-info)
1781 (orgtbl-to-latex
1782 (mapcar
1783 (lambda (row)
1784 (if (string-match org-table-hline-regexp row)
1785 'hline
1786 (mapcar
1787 (lambda (cell)
1788 (org-export-secondary-string
1789 (org-element-parse-secondary-string
1790 cell
1791 (cdr (assq 'table org-element-string-restrictions)))
1792 'e-latex info))
1793 (org-split-string row "[ \t]*|[ \t]*"))))
1794 (org-split-string clean-table "\n"))
1795 `(:tstart nil :tend nil
1796 ;; Longtable environment requires specific
1797 ;; header line end.
1798 :hlend ,(and attr
1799 (string-match "\\<longtable\\>" attr)
1800 (format "\\\\
1801 \\hline
1802 \\endhead
1803 \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
1804 \\endfoot
1805 \\endlastfoot"
1806 columns-number))))))))))
1809 ;;;; Target
1811 (defun org-e-latex-target (target text info)
1812 "Transcode a TARGET object from Org to LaTeX.
1813 TEXT is the text of the target. INFO is a plist holding
1814 contextual information."
1815 (format "\\label{%s}%s"
1816 (org-export-solidify-link-text
1817 (org-element-get-property :raw-value target))
1818 text))
1821 ;;;; Time-stamp
1823 (defun org-e-latex-time-stamp (time-stamp contents info)
1824 "Transcode a TIME-STAMP object from Org to LaTeX.
1825 CONTENTS is nil. INFO is a plist holding contextual information."
1826 (let ((value (org-element-get-property :value time-stamp))
1827 (type (org-element-get-property :type time-stamp))
1828 (appt-type (org-element-get-property :appt-type time-stamp)))
1829 (concat (cond ((eq appt-type 'scheduled)
1830 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
1831 ((eq appt-type 'deadline)
1832 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
1833 ((eq appt-type 'closed)
1834 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
1835 (cond ((memq type '(active active-range))
1836 (format org-e-latex-active-timestamp-format value))
1837 ((memq type '(inactive inactive-range))
1838 (format org-e-latex-inactive-timestamp-format value))
1840 (format org-e-latex-diary-timestamp-format value))))))
1843 ;;;; Verbatim
1845 (defun org-e-latex-verbatim (element contents info)
1846 "Return verbatim text in LaTeX."
1847 (let ((fmt (cdr (assoc (org-element-get-property :marker element)
1848 org-e-latex-emphasis-alist)))
1849 (value (org-element-get-property :value element)))
1850 (cond
1851 ;; Handle the `verb' special case.
1852 ((eq 'verb fmt)
1853 (let ((separator (org-e-latex--find-verb-separator value)))
1854 (concat "\\verb" separator value separator)))
1855 ;; Handle the `protectedtexttt' special case.
1856 ((eq 'protectedtexttt fmt)
1857 (let ((start 0)
1858 (trans '(("\\" . "\\textbackslash{}")
1859 ("~" . "\\textasciitilde{}")
1860 ("^" . "\\textasciicircum{}")))
1861 (rtn "")
1862 char)
1863 (while (string-match "[\\{}$%&_#~^]" value)
1864 (setq char (match-string 0 value))
1865 (if (> (match-beginning 0) 0)
1866 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
1867 (setq value (substring value (1+ (match-beginning 0))))
1868 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1869 rtn (concat rtn char)))
1870 (setq value (concat rtn value)
1871 fmt "\\texttt{%s}")
1872 (while (string-match "--" value)
1873 (setq value (replace-match "-{}-" t t value)))
1874 (format fmt value)))
1875 ;; Else use format string.
1876 (t (format fmt value)))))
1879 ;;;; Verse Block
1881 (defun org-e-latex-verse-block (verse-block contents info)
1882 "Transcode a VERSE-BLOCK element from Org to LaTeX.
1883 CONTENTS is nil. INFO is a plist holding contextual information."
1884 (org-e-latex--wrap-label
1885 verse-block
1886 ;; In a verse environment, add a line break to each newline
1887 ;; character and change each white space at beginning of a line
1888 ;; into a space of 1 em. Also change each blank line with
1889 ;; a vertical space of 1 em.
1890 (progn
1891 (setq contents (replace-regexp-in-string
1892 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1893 (replace-regexp-in-string
1894 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1895 (org-remove-indentation
1896 (org-export-secondary-string
1897 (org-element-get-property :value verse-block)
1898 'e-latex info)))))
1899 (while (string-match "^[ \t]+" contents)
1900 (let ((new-str (format "\\hspace*{%dem}"
1901 (length (match-string 0 contents)))))
1902 (setq contents (replace-match new-str nil t contents))))
1903 (format "\\begin{verse}\n%s\\end{verse}" contents))))
1907 ;;; Compilation
1909 (defun org-e-latex-compile (texfile)
1910 "Compile a TeX file.
1912 TEXFILE is the name of the file being compiled. Processing is
1913 done through the command specified in `org-e-latex-pdf-process'.
1915 Return PDF file name or an error if it couldn't be produced."
1916 (let* ((wconfig (current-window-configuration))
1917 (texfile (file-truename texfile))
1918 (base (file-name-sans-extension texfile))
1919 errors)
1920 (message (format "Processing LaTeX file " texfile "..." texfile))
1921 (unwind-protect
1922 (progn
1923 (cond
1924 ;; A function is provided: Apply it.
1925 ((functionp org-latex-to-pdf-process)
1926 (funcall org-latex-to-pdf-process (shell-quote-argument texfile)))
1927 ;; A list is provided: Replace %b, %f and %o with appropriate
1928 ;; values in each command before applying it. Output is
1929 ;; redirected to "*Org PDF LaTeX Output*" buffer.
1930 ((consp org-e-latex-pdf-process)
1931 (let* ((out-dir (or (file-name-directory texfile) "./"))
1932 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
1933 (mapc
1934 (lambda (command)
1935 (shell-command
1936 (replace-regexp-in-string
1937 "%b" (shell-quote-argument base)
1938 (replace-regexp-in-string
1939 "%f" (shell-quote-argument texfile)
1940 (replace-regexp-in-string
1941 "%o" (shell-quote-argument out-dir) command)))
1942 outbuf))
1943 org-e-latex-pdf-process)
1944 ;; Collect standard errors from output buffer.
1945 (setq errors (org-e-latex-collect-errors outbuf))))
1946 (t (error "No valid command to process to PDF")))
1947 (let ((pdffile (concat base ".pdf")))
1948 ;; Check for process failure. Provide collected errors if
1949 ;; possible.
1950 (if (not (file-exists-p pdffile))
1951 (error (concat (format "PDF file %s wasn't produced" pdffile)
1952 (when errors (concat ": " errors))))
1953 ;; Else remove log files, when specified, and signal end of
1954 ;; process to user, along with any error encountered.
1955 (when org-e-latex-remove-logfiles
1956 (dolist (ext org-e-latex-logfiles-extensions)
1957 (let ((file (concat base "." ext)))
1958 (when (file-exists-p file) (delete-file file)))))
1959 (message (concat "Process completed"
1960 (if (not errors) "."
1961 (concat " with errors: " errors)))))
1962 ;; Return output file name.
1963 pdffile))
1964 (set-window-configuration wconfig))))
1966 (defun org-e-latex-collect-errors (buffer)
1967 "Collect some kind of errors from \"pdflatex\" command output.
1969 BUFFER is the buffer containing output.
1971 Return collected error types as a string, or nil if there was
1972 none."
1973 (with-current-buffer buffer
1974 (save-excursion
1975 (goto-char (point-max))
1976 ;; Find final "pdflatex" run.
1977 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
1978 (let ((case-fold-search t)
1979 (errors ""))
1980 (when (save-excursion
1981 (re-search-forward "Reference.*?undefined" nil t))
1982 (setq errors (concat errors " [undefined reference]")))
1983 (when (save-excursion
1984 (re-search-forward "Citation.*?undefined" nil t))
1985 (setq errors (concat errors " [undefined citation]")))
1986 (when (save-excursion
1987 (re-search-forward "Undefined control sequence" nil t))
1988 (setq errors (concat errors " [undefined control sequence]")))
1989 (when (save-excursion
1990 (re-search-forward "^! LaTeX.*?Error"))
1991 (setq errors (concat errors " [LaTeX error]")))
1992 (when (save-excursion
1993 (re-search-forward "^! Package.*?Error"))
1994 (setq errors (concat errors " [package error]")))
1995 (and (org-string-nw-p errors) (org-trim errors)))))))
1998 (provide 'org-e-latex)
1999 ;;; org-e-latex.el ends here