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/>.
23 ;; This library implements a LaTeX back-end for Org generic exporter.
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".
38 (eval-when-compile (require 'cl
))
39 (require 'org-element
)
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"
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
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}"))
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}"))
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:
100 \(numbered-section . unnumbered-section\)
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
131 \\documentclass{article}
132 [NO-DEFAULT-PACKAGES]
134 \\providecommand{\\alert}[1]{\\textbf{#1}}
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
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
175 (list (string :tag
"LaTeX class")
176 (string :tag
"LaTeX header")
177 (repeat :tag
"Levels" :inline t
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
200 (string :tag
"Derived from buffer")
201 (string :tag
"Use this instead"))))
203 (defcustom org-e-latex-date-format
205 "Format string for \\date{...}."
206 :group
'org-export-e-latex
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
214 :group
'org-export-e-latex
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\)\)
240 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
241 :group
'org-export-e-latex
247 (defcustom org-e-latex-emphasis-alist
248 '(("*" .
"\\textbf{%s}")
250 ("_" .
"\\underline{%s}")
252 ("=" . protectedtexttt
)
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
270 (defcustom org-e-latex-footnote-separator
"\\textsuperscript{,}\\,"
271 "Text used to separate footnotes."
272 :group
'org-export-e-latex
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
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
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
296 (defcustom org-e-latex-image-default-option
"width=.9\\linewidth"
297 "Default option for images."
298 :group
'org-export-e-latex
301 (defcustom org-e-latex-default-figure-position
"htb"
302 "Default position for latex figures."
303 :group
'org-export-e-latex
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
315 :group
'org-export-e-latex
316 :type
'(repeat (string :tag
"Extension")))
321 (defcustom org-e-latex-default-table-environment
"tabular"
322 "Default environment used to build tables."
323 :group
'org-export-e-latex
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
331 (defcustom org-e-latex-tables-verbatim nil
332 "When non-nil, tables are exported verbatim."
333 :group
'org-export-e-latex
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
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.\"
360 :group
'org-export-e-latex
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.\"
386 \(when todo \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
387 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
389 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
390 \(format \(concat \"\\\\begin{center}\\n\"
392 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
394 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
396 \"\\\\end{minipage}}\"
397 \"\\\\end{center}\"\)
398 full-title contents\)\)"
399 :group
'org-export-e-latex
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\"))
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
433 :group
'org-export-e-latex
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")
443 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
444 (html "HTML") (xml "XML")
445 (tex "TeX") (latex "TeX")
446 (shell-script "bash")
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
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
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
477 Note that the same options will be applied to blocks of all
479 :group
'org-export-e-latex
482 (string :tag
"Listings option name ")
483 (string :tag
"Listings option value"))))
485 (defcustom org-e-latex-minted-langs
486 '((emacs-lisp "common-lisp")
489 (shell-script "bash")
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
501 pygmentize -L lexers"
502 :group
'org-export-e-latex
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
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
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
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
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 "))))
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
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
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
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"
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"))
624 ("rubber -d --into %o %f"))
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
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
) "")))
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
)
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
))
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
700 (mapconcat (lambda (pair)
702 (when (> (length (second pair
)) 0)
703 (concat "=" (second pair
)))))
707 (defun org-e-latex--quotation-marks (text info
)
708 "Export quotation marks depending on language conventions."
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
))))
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
""))
726 (concat (format "\\label{%s}\n" label
) output
))))
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
)))
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
)
750 (replace-regexp-in-string
751 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
752 class-options header t nil
1)
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"
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
))
775 (let ((date (plist-get info
:date
)))
776 (and date
(format "\\date{%s}\n" date
)))
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
)))
785 ((not creator-info
) "")
786 ((eq creator-info
'comment
) "")
787 (t (plist-get info
:creator
)))))
788 ;; 7. Document start.
789 "\\begin{document}\n\n"
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
)))
801 (concat (when (wholenump depth
)
802 (format "\\setcounter{tocdepth}{%d}\n" depth
))
803 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
804 ;; 10. Document's body.
807 (let ((creator-info (plist-get info
:with-creator
)))
810 ((eq creator-info
'comment
)
811 (format "%% %s\n" (plist-get info
:creator
)))
812 (t (concat (plist-get info
:creator
) "\n"))))
818 ;;; Transcode Functions
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
828 (format "\\begin{center}\n%s\\end{center}" contents
)))
833 ;; Comments are ignored.
838 ;; Comment Blocks are ignored.
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
851 ;; If there's no user defined function: simply
852 ;; display contents of the drawer.
854 (org-e-latex--wrap-label drawer output
)))
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
864 (org-e-latex--wrap-label dynamic-block contents
))
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
))
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
)
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
))))
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
))
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
))))
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
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."
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.
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}"
956 (org-export-secondary-string
957 (org-export-get-footnote-definition footnote-reference info
)
959 ;; Non-inline footnotes definitions are full Org data.
961 (format "\\footnote{%s}"
964 (org-export-get-footnote-definition footnote-reference info
)
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.
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
))))
987 ;; No section available for that LEVEL.
989 ;; Section format directly returned by a function.
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)
996 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
997 ;; (num-in num-out no-num-in no-num-out)
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
)))
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.
1022 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1023 (when priority
(format "\\framebox{\\#%c} " priority
))
1025 (when tags
(format "\\hfill{}\\textsc{%s}" tags
)))))
1026 ;; Associate some \label to the headline for internal links.
1027 (headline-labels (mapconcat
1029 (let ((val (org-element-get-property p headline
)))
1030 (when val
(format "\\label{%s}\n"
1032 (format "headline-%s" val
)
1034 '(:custom-id
:id
:begin
) ""))
1035 (pre-blanks (make-string (org-element-get-property :pre-blank headline
)
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
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
)))
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
1059 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
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
)
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
)))
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
))
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
))
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
))
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
))))))
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
)))
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
1149 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1150 (when priority
(format "\\framebox{\\#%c} " priority
))
1152 (when tags
(format "\\hfill{}\\textsc{%s}" tags
)))))
1153 (format (concat "\\begin{center}\n"
1155 "\\begin{minipage}[c]{.6\\textwidth}\n"
1157 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1162 full-title contents
))))))
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
)))
1178 (format "\\setcounter{enum%s}{%s}\n"
1179 (nth level
'("i" "ii" "iii" "iv"))
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
)))
1187 (format "[%s]" (org-export-secondary-string
1188 tag
'e-latex info
))))))
1189 (concat counter
"\\item" tag
" " checkbox contents
)))
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
)))
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
)))
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
))))
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
1229 (org-remove-indentation (org-element-get-property :value latex-environment
))))
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
))
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."
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
)
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
)))
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
"")))
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
"]"))
1278 ;; Now clear ATTR from any special keyword and set a default
1279 ;; value if nothing is left.
1282 (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
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.
1292 ('wrap
(format "\\begin{wrapfigure}%s
1294 \\includegraphics[%s]{%s}
1295 %s\\end{wrapfigure}" placement attr path caption
))
1296 ('mulicolumn
(format "\\begin{figure*}%s
1298 \\includegraphics[%s]{%s}
1299 %s\\end{figure*}" placement attr path caption
))
1300 ('float
(format "\\begin{figure}%s
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
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
))
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
)))
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
)))
1350 ((stringp destination
)
1351 (format "\\hyperref[%s]{%s}"
1352 (org-export-solidify-link-text destination
)
1354 (org-export-secondary-string
1355 (org-element-get-property :raw-link link
) 'e-latex info
))))
1357 ((integerp destination
)
1358 (format "\\hyperref[headline-%d]{%s}"
1361 (org-export-secondary-string
1362 (org-element-get-property :raw-link link
) 'e-latex info
))))
1364 (t (format "\\texttt{%s}"
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
)))))
1387 ;; Babel Calls are ignored.
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
))
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."
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
1419 (mapconcat 'identity paralist-types
"\\|")
1421 (attr (mapconcat #'identity
1422 (org-element-get-property :attr_latex plain-list
)
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
1434 (format "\\begin{%s}%s\n%s\\end{%s}"
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
))))
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
)
1457 (replace-match (format "\\%s" (match-string 2 text
)) nil t text
2)))
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
)
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"
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
1489 ;; The property drawer isn't exported but we want separating blank
1490 ;; lines nonetheless.
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
1502 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
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
))))
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."
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
))
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
1545 (format "\\begin{%s}\n%s\\end{%s}" type contents type
))))
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
)))))
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")
1570 (format "\\begin{verbatim}\n%s\\end{verbatim}" code
)))))
1571 ;; Custom environment.
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
))
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
)
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
))
1591 (caption-str (and caption
1592 (org-export-secondary-string
1593 (org-element-get-property :caption src-block
)
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
)))
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
))
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
))
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
))
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
)
1646 ;; Determine alignment string.
1647 (alignment (org-e-latex-table--align-string attr info
))
1648 ;; Determine environment for the table: longtable, tabular...
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*.
1657 ((string= "longtable" table-env
) nil
)
1659 (or (string-match (regexp-quote "table*") attr
)
1660 (string-match "\\<multicolumn\\>" attr
)))
1662 ((or (not (string= caption
"")) label
) "table")))
1663 ;; Extract others display options.
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
)
1671 org-e-latex-default-figure-position
1673 ;; Prepare the final format string for the table.
1676 ((string= "longtable" table-env
)
1677 (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
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"))))
1688 (t (concat (when float-env
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}"
1695 (if width
(format "{%s}" width
) "")
1698 (when org-e-latex-tables-centered
"\n\\end{center}")
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'."
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
))
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
) "|")))
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
)
1739 (raw-table (org-element-get-property :raw-table table
)))
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
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
)
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
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
)))
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
)
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
1788 (format (org-e-latex-table--format-string table table-info
)
1792 (if (string-match org-table-hline-regexp row
)
1796 (org-export-secondary-string
1797 (org-element-parse-secondary-string
1799 (cdr (assq 'table org-element-string-restrictions
)))
1801 (org-split-string row
"[ \t]*|[ \t]*"))))
1802 (org-split-string clean-table
"\n"))
1803 `(:tstart nil
:tend nil
1804 ;; Longtable environment requires specific
1807 (string-match "\\<longtable\\>" attr
)
1811 \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
1814 columns-number
))))))))))
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
))
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
))))))
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
)))
1859 ;; Handle the `verb' special case.
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
)
1866 (trans '(("\\" .
"\\textbackslash{}")
1867 ("~" .
"\\textasciitilde{}")
1868 ("^" .
"\\textasciicircum{}")))
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
)
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
)))))
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
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.
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
)
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
))))
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
))
1928 (message (format "Processing LaTeX file " texfile
"..." texfile
))
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*")))
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
)))
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
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.
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
1981 (with-current-buffer buffer
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
)
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