Modification to the type signature of `org-export-inline-image-p'
[org-mode/org-mode-NeilSmithlineMods.git] / EXPERIMENTAL / org-e-latex.el
blob74e9a39188592c753608b6fbf9a0b3ef359b03ac
1 ;;; org-e-latex.el --- LaTeX Back-End For Org Export Engine
3 ;; Copyright (C) 2011 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-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-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-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-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-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-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-latex
265 :type 'alist)
268 ;;;; Footnotes
270 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
271 "Text used to separate footnotes."
272 :group 'org-export-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-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-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-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-latex
299 :type 'string)
301 (defcustom org-e-latex-default-figure-position "htb"
302 "Default position for latex figures."
303 :group 'org-export-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-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-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-latex
329 :type 'boolean)
331 (defcustom org-e-latex-tables-verbatim nil
332 "When non-nil, tables are exported verbatim."
333 :group 'org-export-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-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-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-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-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-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-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-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-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-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 "))))
577 ;;; Internal Functions
579 (defun org-e-latex--caption/label-string (caption label info)
580 "Return caption and label LaTeX string for floats.
582 CAPTION is a secondary string \(a list of strings and Org
583 objects\) and LABEL a string representing the label. INFO is
584 a plist holding contextual information.
586 If there's no caption nor label, return the empty string.
588 For non-floats, see `org-e-latex--wrap-label'."
589 (let ((caption-str (and caption
590 (org-export-secondary-string
591 caption 'latex info)))
592 (label-str (if label (format "\\label{%s}" label) "")))
593 (cond
594 ((and (not caption-str) (not label)) "")
595 ((not caption-str) (format "\\label{%s}\n" label))
596 ;; Option caption format with short name.
597 ((string-match "\\[\\([^][]*\\)\\]{\\([^{}]*\\)}" caption-str)
598 (format "\\caption[%s]{%s%s}\n"
599 (org-match-string-no-properties 1 caption-str)
600 label-str
601 (org-match-string-no-properties 2 caption-str)))
602 ;; Standard caption format.
603 (t (format "\\caption{%s%s}\n" label-str caption-str)))))
605 (defun org-e-latex--guess-inputenc (header)
606 "Set the coding system in inputenc to what the buffer is.
608 HEADER is the LaTeX header string.
610 Return the new header."
611 (let* ((cs (or (ignore-errors
612 (latexenc-coding-system-to-inputenc
613 buffer-file-coding-system))
614 "utf8")))
615 (if (not cs)
616 header
617 ;; First translate if that is requested.
618 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
619 ;; Then find the \usepackage statement and replace the option.
620 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
621 cs header t nil 1))))
623 (defun org-e-latex--find-verb-separator (s)
624 "Return a character not used in string S.
625 This is used to choose a separator for constructs like \\verb."
626 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
627 (loop for c across ll
628 when (not (string-match (regexp-quote (char-to-string c)) s))
629 return (char-to-string c))))
631 (defun org-e-latex--make-option-string (options)
632 "Return a comma separated string of keywords and values.
633 OPTIONS is an alist where the key is the options keyword as
634 a string, and the value a list containing the keyword value, or
635 nil."
636 (mapconcat (lambda (pair)
637 (concat (first pair)
638 (when (> (length (second pair)) 0)
639 (concat "=" (second pair)))))
640 options
641 ","))
643 (defun org-e-latex--quotation-marks (text info)
644 "Export quotation marks depending on language conventions."
645 (mapc (lambda(l)
646 (let ((start 0))
647 (while (setq start (string-match (car l) text start))
648 (let ((new-quote (concat (match-string 1 text) (cdr l))))
649 (setq text (replace-match new-quote t t text))))))
650 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
651 ;; Falls back on English.
652 (assoc "en" org-e-latex-quotes))))
653 text)
655 (defun org-e-latex--wrap-label (element output)
656 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
657 This function shouldn't be used for floats. See
658 `org-e-latex--caption/label-string'."
659 (let ((label (org-element-get-property :name element)))
660 (if (or (not output) (not label) (string= output "") (string= label ""))
661 output
662 (concat (format "\\label{%s}\n" label) output))))
666 ;;; Template
668 (defun org-e-latex-template (contents info)
669 "Return complete document string after LaTeX conversion.
670 CONTENTS is the transcoded contents string. INFO is a plist
671 holding export options."
672 (let ((title (org-export-secondary-string
673 (plist-get info :title) 'latex info)))
674 (concat
675 ;; 1. Time-stamp.
676 (and (plist-get info :time-stamp-file)
677 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
678 ;; 2. Document class and packages.
679 (let ((class (plist-get info :latex-class))
680 (class-options (plist-get info :latex-class-options)))
681 (org-element-normalize-string
682 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
683 (document-class-string
684 (and (stringp header)
685 (if class-options
686 (replace-regexp-in-string
687 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
688 class-options header t nil 1)
689 header))))
690 (org-e-latex--guess-inputenc
691 (org-splice-latex-header
692 document-class-string
693 org-export-latex-default-packages-alist ; defined in org.el
694 org-export-latex-packages-alist nil ; defined in org.el
695 (plist-get info :latex-header-extra))))))
696 ;; 3. Define alert if not yet defined.
697 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
698 ;; 4. Author.
699 (let ((author (and (plist-get info :with-author)
700 (let ((auth (plist-get info :author)))
701 (and auth (org-export-secondary-string
702 auth 'latex info)))))
703 (email (and (plist-get info :with-email)
704 (org-export-secondary-string
705 (plist-get info :email) 'latex info))))
706 (cond ((and author email (not (string= "" email)))
707 (format "\\author{%s\\thanks{%s}}\n" author email))
708 (author (format "\\author{%s}\n" author))
709 (t "\\author{}\n")))
710 ;; 5. Date.
711 (let ((date (plist-get info :date)))
712 (and date (format "\\date{%s}\n" date)))
713 ;; 6. Title
714 (format "\\title{%s}\n" title)
715 ;; 7. Hyperref options.
716 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
717 (or (plist-get info :keywords) "")
718 (or (plist-get info :description) "")
719 (let ((creator-info (plist-get info :with-creator)))
720 (cond
721 ((not creator-info) "")
722 ((eq creator-info 'comment) "")
723 (t (plist-get info :creator)))))
724 ;; 7. Document start.
725 "\\begin{document}\n\n"
726 ;; 8. Title command.
727 (org-element-normalize-string
728 (cond ((string= "" title) nil)
729 ((not (stringp org-e-latex-title-command)) nil)
730 ((string-match "\\(?:[^%]\\|^\\)%s"
731 org-e-latex-title-command)
732 (format org-e-latex-title-command title))
733 (t org-e-latex-title-command)))
734 ;; 9. Table of contents.
735 (let ((depth (plist-get info :with-toc)))
736 (when depth
737 (concat (when (wholenump depth)
738 (format "\\setcounter{tocdepth}{%d}\n" depth))
739 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
740 ;; 10. Document's body.
741 contents
742 ;; 11. Creator.
743 (let ((creator-info (plist-get info :with-creator)))
744 (cond
745 ((not creator-info))
746 ((eq creator-info 'comment)
747 (format "%% %s\n" (plist-get info :creator)))
748 (t (concat (plist-get info :creator) "\n"))))
749 ;; 12. Document end.
750 "\\end{document}")))
754 ;;; Transcode Functions
756 ;;;; Block
758 (defun org-e-latex-center-block (center-block contents info)
759 "Transcode a CENTER-BLOCK element from Org to LaTeX.
760 CONTENTS holds the contents of the block. INFO is a plist
761 holding contextual information."
762 (org-e-latex--wrap-label
763 center-block
764 (format "\\begin{center}\n%s\\end{center}" contents)))
767 ;;;; Comment
769 ;; Comments are ignored.
772 ;;;; Comment Block
774 ;; Comment Blocks are ignored.
777 ;;;; Drawer
779 (defun org-e-latex-drawer (drawer contents info)
780 "Transcode a DRAWER element from Org to LaTeX.
781 CONTENTS holds the contents of the block. INFO is a plist
782 holding contextual information."
783 (let* ((name (org-element-get-property :drawer-name drawer))
784 (output (if (functionp org-e-latex-format-drawer-function)
785 (funcall org-e-latex-format-drawer-function
786 name contents)
787 ;; If there's no user defined function: simply
788 ;; display contents of the drawer.
789 contents)))
790 (org-e-latex--wrap-label drawer output)))
793 ;;;; Dynamic Block
795 (defun org-e-latex-dynamic-block (dynamic-block contents info)
796 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
797 CONTENTS holds the contents of the block. INFO is a plist
798 holding contextual information. See
799 `org-export-data'."
800 (org-e-latex--wrap-label dynamic-block contents))
803 ;;;; Emphasis
805 (defun org-e-latex-emphasis (emphasis contents info)
806 "Transcode EMPHASIS from Org to LaTeX.
807 CONTENTS is the contents of the emphasized text. INFO is a plist
808 holding contextual information.."
809 (format (cdr (assoc (org-element-get-property :marker emphasis)
810 org-e-latex-emphasis-alist))
811 contents))
814 ;;;; Entity
816 (defun org-e-latex-entity (entity contents info)
817 "Transcode an ENTITY object from Org to LaTeX.
818 CONTENTS are the definition itself. INFO is a plist holding
819 contextual information."
820 (let ((ent (org-element-get-property :latex entity)))
821 (if (org-element-get-property :latex-math-p entity)
822 (format "$%s$" ent)
823 ent)))
826 ;;;; Example Block
828 (defun org-e-latex-example-block (example-block contents info)
829 "Transcode a EXAMPLE-BLOCK element from Org to LaTeX.
830 CONTENTS is nil. INFO is a plist holding contextual information."
831 (let* ((options (or (org-element-get-property :options example-block) ""))
832 (value (org-export-handle-code
833 (org-element-get-property :value example-block) options info)))
834 (org-e-latex--wrap-label example-block value)))
837 ;;;; Export Snippet
839 (defun org-e-latex-export-snippet (export-snippet contents info)
840 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
841 CONTENTS is nil. INFO is a plist holding contextual information."
842 (org-element-get-property :value export-snippet))
845 ;;;; Export Block
847 (defun org-e-latex-export-block (export-block contents info)
848 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
849 CONTENTS is nil. INFO is a plist holding contextual information."
850 (when (string= (org-element-get-property :type export-block) "latex")
851 (org-remove-indentation (org-element-get-property :value export-block))))
854 ;;;; Fixed Width
856 (defun org-e-latex-fixed-width (fixed-width contents info)
857 "Transcode a FIXED-WIDTH element from Org to LaTeX.
858 CONTENTS is nil. INFO is a plist holding contextual information."
859 (let* ((value (org-element-normalize-string
860 (replace-regexp-in-string
861 "^[ \t]*: ?" ""
862 (org-element-get-property :value fixed-width)))))
863 (org-e-latex--wrap-label
864 fixed-width
865 (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
868 ;;;; Footnote Definition
870 ;; Footnote Definitions are ignored.
873 ;;;; Footnote Reference
875 (defun org-e-latex-footnote-reference (footnote-reference contents info)
876 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
877 CONTENTS is nil. INFO is a plist holding contextual information."
878 (concat
879 ;; Insert separator between two footnotes in a row.
880 (when (eq (plist-get info :previous-object) 'footnote-reference)
881 org-e-latex-footnote-separator)
882 ;; Use \footnotemark if the footnote has already been defined.
883 ;; Otherwise, define it with \footnote command.
884 (let* ((all-seen (plist-get info :seen-footnote-labels))
885 (label (org-element-get-property :label footnote-reference))
886 ;; Anonymous footnotes are always new footnotes.
887 (seenp (and label (member label all-seen)))
888 (inline-def-p (org-element-get-property
889 :inline-definition footnote-reference)))
890 (cond
891 (seenp (format "\\footnotemark[%s]" (length seenp)))
892 ;; Inline definitions are secondary strings.
893 (inline-def-p
894 (format "\\footnote{%s}"
895 (org-trim
896 (org-export-secondary-string inline-def-p 'latex info))))
897 ;; Non-inline footnotes necessarily contain a label. Retrieve
898 ;; match definition in `:footnotes-labels-alist'.
900 (format "\\footnote{%s}"
901 (org-trim
902 (org-export-data
903 (cdr (assoc label (plist-get info :footnotes-labels-alist)))
904 'latex info))))))))
907 ;;;; Headline
909 (defun org-e-latex-headline (headline contents info)
910 "Transcode an HEADLINE element from Org to LaTeX.
911 CONTENTS holds the contents of the headline. INFO is a plist
912 holding contextual information."
913 (let* ((class (plist-get info :latex-class))
914 (numberedp (plist-get info :section-numbers))
915 ;; Get level relative to current parsed data.
916 (level (+ (org-element-get-property :level headline)
917 (plist-get info :headline-offset)))
918 (class-sectionning (assoc class org-e-latex-classes))
919 ;; Section formatting will set two placeholders: one for the
920 ;; title and the other for the contents.
921 (section-fmt
922 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
923 (fboundp (nth 2 class-sectionning)))
924 (funcall (nth 2 class-sectionning) level numberedp)
925 (nth (1+ level) class-sectionning))))
926 (cond
927 ;; No section available for that LEVEL.
928 ((not sec) nil)
929 ;; Section format directly returned by a function.
930 ((stringp sec) sec)
931 ;; (numbered-section . unnumbered-section)
932 ((not (consp (cdr sec)))
933 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
934 ;; (numbered-open numbered-close)
935 ((= (length sec) 2)
936 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
937 ;; (num-in num-out no-num-in no-num-out)
938 ((= (length sec) 4)
939 (if numberedp
940 (concat (car sec) "\n%s" (nth 1 sec))
941 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
942 (text (org-export-secondary-string
943 (org-element-get-property :title headline) 'latex info))
944 (todo (and (plist-get info :with-todo-keywords)
945 (let ((todo (org-element-get-property
946 :todo-keyword headline)))
947 (and todo
948 (org-export-secondary-string todo 'latex info)))))
949 (todo-type (and todo (org-element-get-property :todo-type headline)))
950 (tags (and (plist-get info :with-tags)
951 (org-element-get-property :tags headline)))
952 (priority (and (plist-get info :with-priority)
953 (org-element-get-property :priority headline)))
954 ;; Create the headline text.
955 (full-text (if (functionp org-e-latex-format-headline-function)
956 ;; User-defined formatting function.
957 (funcall org-e-latex-format-headline-function
958 todo todo-type priority text tags)
959 ;; Default formatting.
960 (concat
961 (when todo
962 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
963 (when priority (format "\\framebox{\\#%c} " priority))
964 text
965 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
966 ;; Associate some \label to the headline for internal links.
967 (headline-labels (mapconcat
968 (lambda (p)
969 (let ((val (org-element-get-property p headline)))
970 (when val (format "\\label{%s}\n"
971 (if (eq p :begin)
972 (format "headline-%s" val)
973 val)))))
974 '(:custom-id :id :begin) ""))
975 (pre-blanks (make-string (org-element-get-property :pre-blank headline)
976 10)))
977 (cond
978 ;; Case 1: This is a footnote section: ignore it.
979 ((org-element-get-property :footnote-section-p headline) nil)
980 ;; Case 2. This is a deep sub-tree: export it as a list item.
981 ;; Also export as items headlines for which no section
982 ;; format has been found.
983 ((or (not section-fmt)
984 (and (wholenump (plist-get info :headline-levels))
985 (> level (plist-get info :headline-levels))))
986 ;; Build the real contents of the sub-tree.
987 (let ((low-level-body
988 (concat
989 ;; If the headline is the first sibling, start a list.
990 (when (org-export-first-sibling-p headline info)
991 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
992 ;; Itemize headline
993 "\\item " full-text "\n" headline-labels pre-blanks contents)))
994 ;; If headline in the last sibling, close the list, before any
995 ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
996 (if (org-export-last-sibling-p headline info)
997 (replace-regexp-in-string
998 "[ \t\n]*\\'"
999 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1000 low-level-body)
1001 low-level-body)))
1002 ;; Case 3. Standard headline. Export it as a section.
1003 (t (format section-fmt full-text
1004 (concat headline-labels pre-blanks contents))))))
1007 ;;;; Horizontal Rule
1009 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1010 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1011 CONTENTS is nil. INFO is a plist holding contextual information."
1012 (let ((attr (mapconcat #'identity
1013 (org-element-get-property :attr_latex horizontal-rule)
1014 " ")))
1015 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1018 ;;;; Inline Babel Call
1020 ;; Inline Babel Calls are ignored.
1023 ;;;; Inline Src Block
1025 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1026 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1027 CONTENTS holds the contents of the item. INFO is a plist holding
1028 contextual information."
1029 (let* ((code (org-element-get-property :value inline-src-block))
1030 (separator (org-e-latex--find-verb-separator code)))
1031 (cond
1032 ;; Do not use a special package: transcode it verbatim.
1033 ((not org-e-latex-listings)
1034 (concat "\\verb" separator code separator))
1035 ;; Use minted package.
1036 ((eq org-e-latex-listings 'minted)
1037 (let* ((org-lang (org-element-get-property :language inline-src-block))
1038 (mint-lang (or (cadr (assq (intern org-lang)
1039 org-e-latex-minted-langs))
1040 org-lang))
1041 (options (org-e-latex--make-option-string
1042 org-e-latex-minted-options)))
1043 (concat (format "\\mint%s{%s}"
1044 (if (string= options "") "" (format "[%s]" options))
1045 mint-lang)
1046 separator code separator)))
1047 ;; Use listings package.
1049 ;; Maybe translate language's name.
1050 (let* ((org-lang (org-element-get-property :language inline-src-block))
1051 (lst-lang (or (cadr (assq (intern org-lang)
1052 org-e-latex-listings-langs))
1053 org-lang))
1054 (options (org-e-latex--make-option-string
1055 (append org-e-latex-listings-options
1056 `(("language" ,lst-lang))))))
1057 (concat (format "\\lstinline[%s]" options)
1058 separator code separator))))))
1061 ;;;; Inlinetask
1063 (defun org-e-latex-inlinetask (inlinetask contents info)
1064 "Transcode an INLINETASK element from Org to LaTeX.
1065 CONTENTS holds the contents of the block. INFO is a plist
1066 holding contextual information."
1067 (let ((title (org-export-secondary-string
1068 (org-element-get-property :title inlinetask) 'latex info))
1069 (todo (and (plist-get info :with-todo-keywords)
1070 (let ((todo (org-element-get-property
1071 :todo-keyword inlinetask)))
1072 (and todo
1073 (org-export-secondary-string todo 'latex info)))))
1074 (todo-type (org-element-get-property :todo-type inlinetask))
1075 (tags (and (plist-get info :with-tags)
1076 (org-element-get-property :tags inlinetask)))
1077 (priority (and (plist-get info :with-priority)
1078 (org-element-get-property :priority inlinetask))))
1079 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1080 ;; with appropriate arguments.
1081 (if (functionp org-e-latex-format-inlinetask-function)
1082 (funcall org-e-latex-format-inlinetask-function
1083 todo todo-type priority title tags contents)
1084 ;; Otherwise, use a default template.
1085 (org-e-latex--wrap-label
1086 inlinetask
1087 (let ((full-title
1088 (concat
1089 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1090 (when priority (format "\\framebox{\\#%c} " priority))
1091 title
1092 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1093 (format (concat "\\begin{center}\n"
1094 "\\fbox{\n"
1095 "\\begin{minipage}[c]{.6\\textwidth}\n"
1096 "%s\n\n"
1097 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1098 "%s"
1099 "\\end{minipage}\n"
1100 "}\n"
1101 "\\end{center}")
1102 full-title contents))))))
1105 ;;;; Item
1107 (defun org-e-latex-item (item contents info)
1108 "Transcode an ITEM element from Org to LaTeX.
1109 CONTENTS holds the contents of the item. INFO is a plist holding
1110 contextual information."
1111 (let* ((level (plist-get (plist-get info :parent-properties) :level))
1112 (counter (let ((count (org-element-get-property :counter item)))
1113 (and count
1114 (< level 4)
1115 (format "\\setcounter{enum%s}{%s}\n"
1116 (nth level '("i" "ii" "iii" "iv"))
1117 (1- count)))))
1118 (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
1119 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1120 ((eq checkbox 'off) "$\\Box$ ")
1121 ((eq checkbox 'trans) "$\\boxminus$ "))))
1122 (tag (let ((tag (org-element-get-property :tag item)))
1123 (and tag
1124 (format "[%s]" (org-export-secondary-string
1125 tag 'latex info))))))
1126 (concat counter "\\item" tag " " checkbox contents)))
1129 ;;;; Keyword
1131 (defun org-e-latex-keyword (keyword contents info)
1132 "Transcode a KEYWORD element from Org to LaTeX.
1133 CONTENTS is nil. INFO is a plist holding contextual information."
1134 (let ((key (downcase (org-element-get-property :key keyword)))
1135 (value (org-element-get-property :value keyword)))
1136 (cond
1137 ((string= key "latex") value)
1138 ((string= key "index") (format "\\index{%s}" value))
1139 ((string= key "target")
1140 (format "\\label{%s}" (org-export-solidify-link-text value)))
1141 ((string= key "toc")
1142 (let ((value (downcase value)))
1143 (cond
1144 ((string-match "\\<headlines\\>" value)
1145 (let ((depth (or (and (string-match "[0-9]+" value)
1146 (string-to-number (match-string 0 value)))
1147 (plist-get info :with-toc))))
1148 (concat
1149 (when (wholenump depth)
1150 (format "\\setcounter{tocdepth}{%s}\n" depth))
1151 "\\tableofcontents")))
1152 ((string= "tables" value) "\\listoftables")
1153 ((string= "figures" value) "\\listoffigures")
1154 ((string= "listings" value) "\\listoflistings"))))
1155 ((string= key "include")
1156 (org-export-included-file keyword 'latex info)))))
1159 ;;;; Latex Environment
1161 (defun org-e-latex-latex-environment (latex-environment contents info)
1162 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1163 CONTENTS is nil. INFO is a plist holding contextual information."
1164 (org-e-latex--wrap-label
1165 latex-environment
1166 (org-remove-indentation (org-element-get-property :value latex-environment))))
1169 ;;;; Latex Fragment
1171 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1172 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1173 CONTENTS is nil. INFO is a plist holding contextual information."
1174 (org-element-get-property :value latex-fragment))
1177 ;;;; Line Break
1179 (defun org-e-latex-line-break (line-break contents info)
1180 "Transcode a LINE-BREAK object from Org to LaTeX.
1181 CONTENTS is nil. INFO is a plist holding contextual information."
1182 "\\\\")
1185 ;;;; Link
1187 (defun org-e-latex-link--inline-image (path info)
1188 "Return LaTeX code for an image at PATH.
1189 INFO is a plist containing export options."
1190 (let* ((parent-props (plist-get info :parent-properties))
1191 (caption (org-e-latex--caption/label-string
1192 (plist-get parent-props :caption)
1193 (plist-get parent-props :name)
1194 info))
1195 ;; Retrieve latex attributes from the element around.
1196 (attr (let ((raw-attr
1197 (mapconcat #'identity
1198 (plist-get parent-props :attr_latex) " ")))
1199 (unless (string= raw-attr "") raw-attr)))
1200 (disposition
1201 (cond
1202 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1203 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1204 ((or (and attr (string-match "\\<float\\>" attr))
1205 (not (string= caption "")))
1206 'float)))
1207 (placement
1208 (cond
1209 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1210 (org-match-string-no-properties 1 attr))
1211 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1212 ((eq disposition 'float)
1213 (concat "[" org-e-latex-default-figure-position "]"))
1214 (t ""))))
1215 ;; Now clear ATTR from any special keyword and set a default
1216 ;; value if nothing is left.
1217 (if (not attr)
1218 (setq attr "")
1219 (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
1220 attr)
1221 (replace-match "" nil nil attr))
1222 (setq attr (org-trim attr)))
1223 (setq attr (cond ((not (string= attr "")) attr)
1224 ((eq disposition 'float) "width=0.7\\textwidth")
1225 ((eq disposition 'wrap) "width=0.48\\textwidth")
1226 (t (or org-e-latex-image-default-option ""))))
1227 ;; Return proper string, depending on DISPOSITION.
1228 (case disposition
1229 ('wrap (format "\\begin{wrapfigure}%s
1230 \\centering
1231 \\includegraphics[%s]{%s}
1232 %s\\end{wrapfigure}" placement attr path caption))
1233 ('mulicolumn (format "\\begin{figure*}%s
1234 \\centering
1235 \\includegraphics[%s]{%s}
1236 %s\\end{figure*}" placement attr path caption))
1237 ('float (format "\\begin{figure}%s
1238 \\centering
1239 \\includegraphics[%s]{%s}
1240 %s\\end{figure}" placement attr path caption))
1241 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1243 (defun org-e-latex-link (link desc info)
1244 "Transcode a LINK object from Org to LaTeX.
1246 DESC is the description part of the link, or the empty string.
1247 INFO is a plist holding contextual information. See
1248 `org-export-data'."
1249 (let* ((type (org-element-get-property :type link))
1250 (raw-path (org-element-get-property :path link))
1251 ;; Ensure DESC really exists, or set it to nil.
1252 (desc (and (not (string= desc "")) desc))
1253 (imagep (org-export-inline-image-p
1254 link org-e-latex-inline-image-extensions))
1255 (path (cond
1256 ((member type '("http" "https" "ftp" "mailto"))
1257 (concat type ":" raw-path))
1258 ((and (not imagep) (string= type "file"))
1259 (when (string-match "\\(.+\\)::.+" raw-path)
1260 (setq raw-path (match-string 1 raw-path)))
1261 (if (file-name-absolute-p raw-path)
1262 (concat "file://" (expand-file-name raw-path))
1263 ;; TODO: Not implemented yet. Concat also:
1264 ;; (org-export-directory :LaTeX info)
1265 (concat "file://" raw-path)))
1266 (t raw-path)))
1267 protocol)
1268 (cond
1269 ;; Image file.
1270 (imagep (org-e-latex-link--inline-image path info))
1271 ;; Id: for now, assume it's an internal link. TODO: do something
1272 ;; to check if it isn't in the current file.
1273 ((string= type "id")
1274 (format "\\hyperref[%s]{%s}" path (or desc path)))
1275 ;; Custom-id, target or radioed target: replace link with the
1276 ;; normalized custom-id/target name.
1277 ((member type '("custom-id" "target" "radio"))
1278 (format "\\hyperref[%s]{%s}"
1279 (org-export-solidify-link-text path)
1280 (or desc (org-export-secondary-string path 'latex info))))
1281 ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
1282 ;; the destination of the link.
1283 ((string= type "fuzzy")
1284 (let ((destination (org-export-resolve-fuzzy-link link info)))
1285 (cond
1286 ;; Target match.
1287 ((stringp destination)
1288 (format "\\hyperref[%s]{%s}"
1289 (org-export-solidify-link-text destination)
1290 (or desc
1291 (org-export-secondary-string
1292 (org-element-get-property :raw-link link) 'latex info))))
1293 ;; Headline match.
1294 ((integerp destination)
1295 (format "\\hyperref[headline-%d]{%s}"
1296 destination
1297 (or desc
1298 (org-export-secondary-string
1299 (org-element-get-property :raw-link link) 'latex info))))
1300 ;; No match.
1301 (t (format "\\texttt{%s}"
1302 (or desc
1303 (org-export-secondary-string
1304 (org-element-get-property :raw-link link)
1305 'latex info)))))))
1306 ;; Coderef: replace link with the reference name or the
1307 ;; equivalent line number.
1308 ((string= type "coderef")
1309 (format (org-export-get-coderef-format path (or desc ""))
1310 (cdr (assoc path (plist-get info :code-refs)))))
1311 ;; Link type is handled by a special function.
1312 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1313 (funcall protocol (org-link-unescape path) desc 'latex))
1314 ;; External link with a description part.
1315 ((and path desc) (format "\\href{%s}{%s}" path desc))
1316 ;; External link without a description part.
1317 (path (format "\\url{%s}" path))
1318 ;; No path, only description. Try to do something useful.
1319 (t (format "\\texttt{%s}" desc)))))
1322 ;;;; Babel Call
1324 ;; Babel Calls are ignored.
1327 ;;;; Macro
1329 (defun org-e-latex-macro (macro contents info)
1330 "Transcode a MACRO element from Org to LaTeX.
1331 CONTENTS is nil. INFO is a plist holding contextual information."
1332 ;; Use available tools.
1333 (org-export-expand-macro macro info))
1336 ;;;; Paragraph
1338 (defun org-e-latex-paragraph (paragraph contents info)
1339 "Transcode a PARAGRAPH element from Org to LaTeX.
1340 CONTENTS is the contents of the paragraph, as a string. INFO is
1341 the plist used as a communication channel."
1342 contents)
1345 ;;;; Plain List
1347 (defun org-e-latex-plain-list (plain-list contents info)
1348 "Transcode a PLAIN-LIST element from Org to LaTeX.
1349 CONTENTS is the contents of the list. INFO is a plist holding
1350 contextual information."
1351 (let* ((type (org-element-get-property :type plain-list))
1352 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1353 "inparadesc" "asparadesc"))
1354 (paralist-regexp (concat
1355 "\\("
1356 (mapconcat 'identity paralist-types "\\|")
1357 "\\)"))
1358 (attr (mapconcat #'identity
1359 (org-element-get-property :attr_latex plain-list)
1360 " "))
1361 (latex-type (cond
1362 ((and attr
1363 (string-match
1364 (format "\\<%s\\>" paralist-regexp) attr))
1365 (match-string 1 attr))
1366 ((eq type 'ordered) "enumerate")
1367 ((eq type 'unordered) "itemize")
1368 ((eq type 'descriptive) "description"))))
1369 (org-e-latex--wrap-label
1370 plain-list
1371 (format "\\begin{%s}%s\n%s\\end{%s}"
1372 latex-type
1373 ;; Once special environment, if any, has been removed, the
1374 ;; rest of the attributes will be optional arguments.
1375 ;; They will be put inside square brackets if necessary.
1376 (let ((opt (replace-regexp-in-string
1377 (format " *%s *" paralist-regexp) "" attr)))
1378 (cond ((string= opt "") "")
1379 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1380 (t (format "[%s]" opt))))
1381 contents
1382 latex-type))))
1385 ;;;; Plain Text
1387 (defun org-e-latex-plain-text (text info)
1388 "Transcode a TEXT string from Org to LaTeX.
1389 TEXT is the string to transcode. INFO is a plist holding
1390 contextual information."
1391 ;; Protect %, #, &, $, ~, ^, _, { and }.
1392 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1393 (setq text
1394 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1395 ;; Protect \
1396 (setq text (replace-regexp-in-string
1397 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1398 "$\\backslash$" text nil t 1))
1399 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1400 (let ((case-fold-search nil)
1401 (start 0))
1402 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1403 (setq text (replace-match
1404 (format "\\%s{}" (match-string 1 text)) nil t text)
1405 start (match-end 0))))
1406 ;; Handle quotation marks
1407 (setq text (org-e-latex--quotation-marks text info))
1408 ;; Convert special strings.
1409 (when (plist-get info :with-special-strings)
1410 (while (string-match (regexp-quote "...") text)
1411 (setq text (replace-match "\\ldots{}" nil t text))))
1412 ;; Handle break preservation if required.
1413 (when (plist-get info :preserve-breaks)
1414 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1415 text)))
1416 ;; Return value.
1417 text)
1420 ;;;; Property Drawer
1422 (defun org-e-latex-property-drawer (property-drawer contents info)
1423 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1424 CONTENTS is nil. INFO is a plist holding contextual
1425 information."
1426 ;; The property drawer isn't exported but we want separating blank
1427 ;; lines nonetheless.
1431 ;;;; Quote Block
1433 (defun org-e-latex-quote-block (quote-block contents info)
1434 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1435 CONTENTS holds the contents of the block. INFO is a plist
1436 holding contextual information."
1437 (org-e-latex--wrap-label
1438 quote-block
1439 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1442 ;;;; Quote Section
1444 (defun org-e-latex-quote-section (quote-section contents info)
1445 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1446 CONTENTS is nil. INFO is a plist holding contextual information."
1447 (let ((value (org-remove-indentation
1448 (org-element-get-property :value quote-section))))
1449 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1452 ;;;; Radio Target
1454 (defun org-e-latex-radio-target (radio-target text info)
1455 "Transcode a RADIO-TARGET object from Org to LaTeX.
1456 TEXT is the text of the target. INFO is a plist holding
1457 contextual information."
1458 (format "\\label{%s}%s"
1459 (org-export-solidify-link-text
1460 (org-element-get-property :raw-value radio-target))
1461 text))
1464 ;;;; Special Block
1466 (defun org-e-latex-special-block (special-block contents info)
1467 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1468 CONTENTS holds the contents of the block. INFO is a plist
1469 holding contextual information."
1470 (let ((type (downcase (org-element-get-property :type special-block))))
1471 (org-e-latex--wrap-label
1472 special-block
1473 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1476 ;;;; Src Block
1478 (defun org-e-latex-src-block (src-block contents info)
1479 "Transcode a SRC-BLOCK element from Org to LaTeX.
1480 CONTENTS holds the contents of the item. INFO is a plist holding
1481 contextual information."
1482 (let* ((lang (org-element-get-property :language src-block))
1483 (code (org-export-handle-code
1484 (org-element-get-property :value src-block)
1485 (org-element-get-property :switches src-block)
1486 info lang))
1487 (caption (org-element-get-property :caption src-block))
1488 (label (org-element-get-property :name src-block))
1489 (custom-env (and lang
1490 (cadr (assq (intern lang)
1491 org-e-latex-custom-lang-environments)))))
1492 (cond
1493 ;; No source fontification.
1494 ((not org-e-latex-listings)
1495 (let ((caption-str (org-e-latex--caption/label-string
1496 caption label info))
1497 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1498 (format (or float-env "%s")
1499 (concat
1500 caption-str
1501 (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
1502 ;; Custom environment.
1503 (custom-env
1504 (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
1505 ;; Use minted package.
1506 ((eq org-e-latex-listings 'minted)
1507 (let* ((mint-lang (or (cadr (assq (intern lang) org-e-latex-minted-langs))
1508 lang))
1509 (float-env (when (or label caption)
1510 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1511 (org-e-latex--caption/label-string
1512 caption label info))))
1513 (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1514 (org-e-latex--make-option-string
1515 org-e-latex-minted-options)
1516 mint-lang code)))
1517 (if float-env (format float-env body) body)))
1518 ;; Use listings package.
1520 (let ((lst-lang (or (cadr (assq (intern lang) org-e-latex-listings-langs))
1521 lang))
1522 (caption-str (and caption
1523 (org-export-secondary-string
1524 (org-element-get-property :caption src-block)
1525 'latex info))))
1526 (concat (format "\\lstset{%s}\n"
1527 (org-e-latex--make-option-string
1528 (append org-e-latex-listings-options
1529 `(("language" ,lst-lang))
1530 (when label `(("label" ,label)))
1531 (when caption-str
1532 `(("caption" ,caption-str))))))
1533 (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
1536 ;;;; Statistics Cookie
1538 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1539 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1540 CONTENTS is nil. INFO is a plist holding contextual information."
1541 (org-element-get-property :value statistics-cookie))
1544 ;;;; Subscript
1546 (defun org-e-latex-subscript (subscript contents info)
1547 "Transcode a SUBSCRIPT object from Org to LaTeX.
1548 CONTENTS is the contents of the object. INFO is a plist holding
1549 contextual information."
1550 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1553 ;;;; Superscript
1555 (defun org-e-latex-superscript (superscript contents info)
1556 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1557 CONTENTS is the contents of the object. INFO is a plist holding
1558 contextual information."
1559 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1562 ;;;; Table
1564 (defun org-e-latex-table--format-string (table info)
1565 "Return an appropriate format string for TABLE.
1567 INFO is the plist containing format info about the table, as
1568 returned by `org-export-table-format-info'.
1570 The format string one placeholder for the body of the table."
1571 (let* ((label (org-element-get-property :name table))
1572 (caption (org-e-latex--caption/label-string
1573 (org-element-get-property :caption table) label info))
1574 (attr (mapconcat #'identity
1575 (org-element-get-property :attr_latex table)
1576 " "))
1577 ;; Determine alignment string.
1578 (alignment (org-e-latex-table--align-string attr info))
1579 ;; Determine environment for the table: longtable, tabular...
1580 (table-env (cond
1581 ((not attr) org-e-latex-default-table-environment)
1582 ((string-match "\\<longtable\\>" attr) "longtable")
1583 ((string-match "\\(tabular.\\)" attr)
1584 (org-match-string-no-properties 1 attr))
1585 (t org-e-latex-default-table-environment)))
1586 ;; If table is a float, determine environment: table or table*.
1587 (float-env (cond
1588 ((string= "longtable" table-env) nil)
1589 ((and attr
1590 (or (string-match (regexp-quote "table*") attr)
1591 (string-match "\\<multicolumn\\>" attr)))
1592 "table*")
1593 ((or (not (string= caption "")) label) "table")))
1594 ;; Extract others display options.
1595 (width (and attr
1596 (string-match "\\<width=\\(\\S-+\\)" attr)
1597 (org-match-string-no-properties 1 attr)))
1598 (placement (if (and attr
1599 (string-match "\\<placement=\\(\\S-+\\)" attr))
1600 (org-match-string-no-properties 1 attr)
1601 (concat "["
1602 org-e-latex-default-figure-position
1603 "]"))))
1604 ;; Prepare the final format string for the table.
1605 (cond
1606 ;; Longtable.
1607 ((string= "longtable" table-env)
1608 (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1609 alignment
1610 (if (or (not org-e-latex-table-caption-above)
1611 (string= "" caption))
1613 (concat (org-trim caption) "\\\\"))
1614 (if (or org-e-latex-table-caption-above
1615 (string= "" caption))
1617 (concat (org-trim caption) "\\\\\n"))))
1618 ;; Others.
1619 (t (concat (when float-env
1620 (concat
1621 (format "\\begin{%s}%s\n" float-env placement)
1622 (if org-e-latex-table-caption-above caption "")))
1623 (when org-e-latex-tables-centered "\\begin{center}\n")
1624 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1625 table-env
1626 (if width (format "{%s}" width) "")
1627 alignment
1628 table-env)
1629 (when org-e-latex-tables-centered "\n\\end{center}")
1630 (when float-env
1631 (concat (if org-e-latex-table-caption-above "" caption)
1632 (format "\n\\end{%s}" float-env))))))))
1634 (defun org-e-latex-table--align-string (attr info)
1635 "Return an appropriate LaTeX alignment string.
1637 INFO is the plist containing format info about the table, as
1638 returned by `org-export-table-format-info'."
1639 (or (and attr
1640 (string-match "\\<align=\\(\\S-+\\)" attr)
1641 (match-string 1 attr))
1642 (let* ((align (copy-sequence (plist-get info :alignment)))
1643 (colgroups (copy-sequence (plist-get info :column-groups)))
1644 (cols (length align))
1645 (separators (make-vector (1+ cols) "")))
1646 ;; Ignore the first column if it's special.
1647 (when (plist-get info :special-column-p)
1648 (aset align 0 "") (aset colgroups 0 nil))
1649 (let ((col 0))
1650 (mapc (lambda (el)
1651 (let ((gr (aref colgroups col)))
1652 (when (memq gr '(start start-end))
1653 (aset separators col "|"))
1654 (when (memq gr '(end start-end))
1655 (aset separators (1+ col) "|")))
1656 (incf col))
1657 align))
1658 ;; Build the LaTeX specific alignment string.
1659 (loop for al across align
1660 for sep across separators
1661 concat (concat sep al) into output
1662 finally return (concat output (aref separators cols))))))
1664 (defun org-e-latex-table (table contents info)
1665 "Transcode a TABLE element from Org to LaTeX.
1666 CONTENTS is nil. INFO is a plist holding contextual information."
1667 (let ((attr (mapconcat #'identity
1668 (org-element-get-property :attr_latex table)
1669 " "))
1670 (raw-table (org-element-get-property :raw-table table)))
1671 (cond
1672 ;; Case 1: verbatim table.
1673 ((or org-e-latex-tables-verbatim
1674 (and attr (string-match "\\<verbatim\\>" attr)))
1675 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1676 (org-export-clean-table
1677 raw-table
1678 (plist-get (org-export-table-format-info raw-table)
1679 :special-column-p))))
1680 ;; Case 2: table.el table. Convert it using appropriate tools.
1681 ((eq (org-element-get-property :type table) 'table.el)
1682 (require 'table)
1683 ;; Ensure "*org-export-table*" buffer is empty.
1684 (and (get-buffer "*org-export-table*")
1685 (kill-buffer (get-buffer "*org-export-table*")))
1686 (let ((output (with-temp-buffer
1687 (insert raw-table)
1688 (goto-char 1)
1689 (re-search-forward "^[ \t]*|[^|]" nil t)
1690 (table-generate-source 'latex "*org-export-table*")
1691 (with-current-buffer "*org-export-table*"
1692 (org-trim (buffer-string))))))
1693 (kill-buffer (get-buffer "*org-export-table*"))
1694 ;; Remove left out comments.
1695 (while (string-match "^%.*\n" output)
1696 (setq output (replace-match "" t t output)))
1697 ;; When the "rmlines" attribute is provided, remove all hlines
1698 ;; but the the one separating heading from the table body.
1699 (when (and attr (string-match "\\<rmlines\\>" attr))
1700 (let ((n 0) (pos 0))
1701 (while (and (< (length output) pos)
1702 (setq pos (string-match "^\\\\hline\n?" output pos)))
1703 (incf n)
1704 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
1705 (if org-e-latex-tables-centered
1706 (format "\\begin{center}\n%s\n\\end{center}" output)
1707 output)))
1708 ;; Case 3: Standard table.
1710 (let* ((table-info (org-export-table-format-info raw-table))
1711 (clean-table (org-export-clean-table
1712 raw-table (plist-get table-info :special-column-p)))
1713 (columns-number (length (plist-get table-info :alignment))))
1714 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
1715 ;; particular, send each cell to
1716 ;; `org-element-parse-secondary-string' to expand any Org
1717 ;; object within. Eventually, flesh the format string out with
1718 ;; the table.
1719 (format (org-e-latex-table--format-string table table-info)
1720 (orgtbl-to-latex
1721 (mapcar
1722 (lambda (row)
1723 (if (string-match org-table-hline-regexp row)
1724 'hline
1725 (mapcar
1726 (lambda (cell)
1727 (org-export-secondary-string
1728 (org-element-parse-secondary-string
1729 cell
1730 (cdr (assq 'table org-element-string-restrictions)))
1731 'latex info))
1732 (org-split-string row "[ \t]*|[ \t]*"))))
1733 (org-split-string clean-table "\n"))
1734 `(:tstart nil :tend nil
1735 ;; Longtable environment requires specific
1736 ;; header line end.
1737 :hlend ,(and attr
1738 (string-match "\\<longtable\\>" attr)
1739 (format "\\\\
1740 \\hline
1741 \\endhead
1742 \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
1743 \\endfoot
1744 \\endlastfoot"
1745 columns-number))))))))))
1748 ;;;; Target
1750 (defun org-e-latex-target (target text info)
1751 "Transcode a TARGET object from Org to LaTeX.
1752 TEXT is the text of the target. INFO is a plist holding
1753 contextual information."
1754 (format "\\label{%s}%s"
1755 (org-export-solidify-link-text
1756 (org-element-get-property :raw-value target))
1757 text))
1760 ;;;; Time-stamp
1762 (defun org-e-latex-time-stamp (time-stamp contents info)
1763 "Transcode a TIME-STAMP object from Org to LaTeX.
1764 CONTENTS is nil. INFO is a plist holding contextual information."
1765 (let ((value (org-element-get-property :value time-stamp))
1766 (type (org-element-get-property :type time-stamp))
1767 (appt-type (org-element-get-property :appt-type time-stamp)))
1768 (concat (cond ((eq appt-type 'scheduled)
1769 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
1770 ((eq appt-type 'deadline)
1771 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
1772 ((eq appt-type 'closed)
1773 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
1774 (cond ((memq type '(active active-range))
1775 (format org-e-latex-active-timestamp-format value))
1776 ((memq type '(inactive inactive-range))
1777 (format org-e-latex-inactive-timestamp-format value))
1779 (format org-e-latex-diary-timestamp-format value))))))
1782 ;;;; Verbatim
1784 (defun org-e-latex-verbatim (element contents info)
1785 "Return verbatim text in LaTeX."
1786 (let ((fmt (cdr (assoc (org-element-get-property :marker element)
1787 org-e-latex-emphasis-alist)))
1788 (value (org-element-get-property :value element)))
1789 (cond
1790 ;; Handle the `verb' special case.
1791 ((eq 'verb fmt)
1792 (let ((separator (org-e-latex--find-verb-separator value)))
1793 (concat "\\verb" separator value separator)))
1794 ;; Handle the `protectedtexttt' special case.
1795 ((eq 'protectedtexttt fmt)
1796 (let ((start 0)
1797 (trans '(("\\" . "\\textbackslash{}")
1798 ("~" . "\\textasciitilde{}")
1799 ("^" . "\\textasciicircum{}")))
1800 (rtn "")
1801 char)
1802 (while (string-match "[\\{}$%&_#~^]" value)
1803 (setq char (match-string 0 value))
1804 (if (> (match-beginning 0) 0)
1805 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
1806 (setq value (substring value (1+ (match-beginning 0))))
1807 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1808 rtn (concat rtn char)))
1809 (setq value (concat rtn value)
1810 fmt "\\texttt{%s}")
1811 (while (string-match "--" value)
1812 (setq value (replace-match "-{}-" t t value)))
1813 (format fmt value)))
1814 ;; Else use format string.
1815 (t (format fmt value)))))
1818 ;;;; Verse Block
1820 (defun org-e-latex-verse-block (verse-block contents info)
1821 "Transcode a VERSE-BLOCK element from Org to LaTeX.
1822 CONTENTS is nil. INFO is a plist holding contextual information."
1823 (org-e-latex--wrap-label
1824 verse-block
1825 ;; In a verse environment, add a line break to each newline
1826 ;; character and change each white space at beginning of a line
1827 ;; into a space of 1 em. Also change each blank line with
1828 ;; a vertical space of 1 em.
1829 (progn
1830 (setq contents (replace-regexp-in-string
1831 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1832 (replace-regexp-in-string
1833 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1834 (org-remove-indentation
1835 (org-export-secondary-string
1836 (org-element-get-property :value verse-block)
1837 'latex info)))))
1838 (while (string-match "^[ \t]+" contents)
1839 (let ((new-str (format "\\hspace*{%dem}"
1840 (length (match-string 0 contents)))))
1841 (setq contents (replace-match new-str nil t contents))))
1842 (format "\\begin{verse}\n%s\\end{verse}" contents))))
1845 (provide 'org-e-latex)
1846 ;;; org-e-latex.el ends here