EXPERIMENTAL/org-latex: Small refactoring.
[org-mode.git] / EXPERIMENTAL / org-latex.el
blob7831b9e2a53f0adb1a68a75d8152c79283f7f016
1 ;;; org-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 ;; It introduces three new buffer keywords: "LATEX_CLASS",
26 ;; "LATEX_CLASS_OPTIONS" and "LATEX_HEADER".
28 ;;; Code:
30 (eval-when-compile (require 'cl))
31 (require 'org-element)
32 (require 'org-export)
36 ;;; Internal Variables
38 (defconst org-latex-option-alist
39 '((:date "DATE" nil org-latex-date-format t)
40 (:latex-class "LATEX_CLASS" nil org-latex-default-class t)
41 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
42 (:latex-header-extra "LATEX_HEADER" nil nil newline))
43 "Alist between LaTeX export properties and ways to set them.
44 See `org-export-option-alist' for more information on the
45 structure of the value.")
49 ;;; User Configurable Variables
51 (defgroup org-export-latex nil
52 "Options for exporting Org mode files to LaTeX."
53 :tag "Org Export LaTeX"
54 :group 'org-export)
57 ;;;; Preamble
59 (defcustom org-latex-default-class "article"
60 "The default LaTeX class."
61 :group 'org-export-latex
62 :type '(string :tag "LaTeX class"))
64 (defcustom org-latex-classes
65 '(("article"
66 "\\documentclass[11pt]{article}"
67 ("\\section{%s}" . "\\section*{%s}")
68 ("\\subsection{%s}" . "\\subsection*{%s}")
69 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
70 ("\\paragraph{%s}" . "\\paragraph*{%s}")
71 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
72 ("report"
73 "\\documentclass[11pt]{report}"
74 ("\\part{%s}" . "\\part*{%s}")
75 ("\\chapter{%s}" . "\\chapter*{%s}")
76 ("\\section{%s}" . "\\section*{%s}")
77 ("\\subsection{%s}" . "\\subsection*{%s}")
78 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
79 ("book"
80 "\\documentclass[11pt]{book}"
81 ("\\part{%s}" . "\\part*{%s}")
82 ("\\chapter{%s}" . "\\chapter*{%s}")
83 ("\\section{%s}" . "\\section*{%s}")
84 ("\\subsection{%s}" . "\\subsection*{%s}")
85 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
86 "Alist of LaTeX classes and associated header and structure.
87 If #+LaTeX_CLASS is set in the buffer, use its value and the
88 associated information. Here is the structure of each cell:
90 \(class-name
91 header-string
92 \(numbered-section . unnumbered-section\)
93 ...\)
95 The header string
96 -----------------
98 The HEADER-STRING is the header that will be inserted into the LaTeX file.
99 It should contain the \\documentclass macro, and anything else that is needed
100 for this setup. To this header, the following commands will be added:
102 - Calls to \\usepackage for all packages mentioned in the variables
103 `org-latex-default-packages-alist' and
104 `org-latex-packages-alist'. Thus, your header definitions should
105 avoid to also request these packages.
107 - Lines specified via \"#+LaTeX_HEADER:\"
109 If you need more control about the sequence in which the header is built
110 up, or if you want to exclude one of these building blocks for a particular
111 class, you can use the following macro-like placeholders.
113 [DEFAULT-PACKAGES] \\usepackage statements for default packages
114 [NO-DEFAULT-PACKAGES] do not include any of the default packages
115 [PACKAGES] \\usepackage statements for packages
116 [NO-PACKAGES] do not include the packages
117 [EXTRA] the stuff from #+LaTeX_HEADER
118 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
119 [BEAMER-HEADER-EXTRA] the beamer extra headers
121 So a header like
123 \\documentclass{article}
124 [NO-DEFAULT-PACKAGES]
125 [EXTRA]
126 \\providecommand{\\alert}[1]{\\textbf{#1}}
127 [PACKAGES]
129 will omit the default packages, and will include the #+LaTeX_HEADER lines,
130 then have a call to \\providecommand, and then place \\usepackage commands
131 based on the content of `org-latex-packages-alist'.
133 If your header or `org-latex-default-packages-alist' inserts
134 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
135 a coding system derived from `buffer-file-coding-system'. See also the
136 variable `org-latex-inputenc-alist' for a way to influence this
137 mechanism.
139 The sectioning structure
140 ------------------------
142 The sectioning structure of the class is given by the elements following
143 the header string. For each sectioning level, a number of strings is
144 specified. A %s formatter is mandatory in each section string and will
145 be replaced by the title of the section.
147 Instead of a cons cell \(numbered . unnumbered\), you can also
148 provide a list of 2 or 4 elements,
150 \(numbered-open numbered-close\)
154 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
156 providing opening and closing strings for a LaTeX environment that should
157 represent the document section. The opening clause should have a %s
158 to represent the section title.
160 Instead of a list of sectioning commands, you can also specify a
161 function name. That function will be called with two parameters,
162 the (reduced) level of the headline, and a predicate non-nil when
163 the headline should be numbered. It must return a format string in
164 which the section title will be added."
165 :group 'org-export-latex
166 :type '(repeat
167 (list (string :tag "LaTeX class")
168 (string :tag "LaTeX header")
169 (repeat :tag "Levels" :inline t
170 (choice
171 (cons :tag "Heading"
172 (string :tag " numbered")
173 (string :tag "unnumbered"))
174 (list :tag "Environment"
175 (string :tag "Opening (numbered)")
176 (string :tag "Closing (numbered)")
177 (string :tag "Opening (unnumbered)")
178 (string :tag "Closing (unnumbered)"))
179 (function :tag "Hook computing sectioning"))))))
181 (defcustom org-latex-inputenc-alist nil
182 "Alist of inputenc coding system names, and what should really be used.
183 For example, adding an entry
185 (\"utf8\" . \"utf8x\")
187 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
188 are written as utf8 files."
189 :group 'org-export-latex
190 :type '(repeat
191 (cons
192 (string :tag "Derived from buffer")
193 (string :tag "Use this instead"))))
195 (defcustom org-latex-date-format
196 "\\today"
197 "Format string for \\date{...}."
198 :group 'org-export-latex
199 :type 'boolean)
201 (defcustom org-latex-title-command "\\maketitle"
202 "The command used to insert the title just after \\begin{document}.
203 If this string contains the formatting specification \"%s\" then
204 it will be used as a formatting string, passing the title as an
205 argument."
206 :group 'org-export-latex
207 :type 'string)
210 ;;;; Headline
212 (defcustom org-latex-format-headline-function nil
213 "Function to format headline text.
215 This function will be called with 5 arguments:
216 TODO the todo keyword \(string or nil\).
217 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
218 PRIORITY the priority of the headline \(integer or nil\)
219 TEXT the main headline text \(string\).
220 TAGS the tags string, separated with colons \(string or nil\).
222 The function result will be used in the section format string.
224 As an example, one could set the variable to the following, in
225 order to reproduce the default set-up:
227 \(defun org-latex-format-headline-default \(todo todo-type priority text tags\)
228 \"Default format function for an headline.\"
229 \(concat \(when todo \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
230 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
231 text
232 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
233 :group 'org-export-latex
234 :type 'function)
237 ;;;; Emphasis
239 (defcustom org-latex-emphasis-alist
240 '(("*" . "\\textbf{%s}")
241 ("/" . "\\emph{%s}")
242 ("_" . "\\underline{%s}")
243 ("+" . "\\st{%s}")
244 ("=" . protectedtexttt)
245 ("~" . verb))
246 "Alist of LaTeX expressions to convert emphasis fontifiers.
248 The key is the character used as a marker for fontification. The
249 value is a formatting string to wrap fontified text with.
251 Value can also be set to the following symbols: `verb' and
252 `protectedtexttt'. For the former, Org will use \"\\verb\" to
253 create a format string and select a delimiter character that
254 isn't in the string. For the latter, Org will use \"\\texttt\"
255 to typeset and try to protect special characters."
256 :group 'org-export-latex
257 :type 'alist)
260 ;;;; Footnotes
262 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
263 "Text used to separate footnotes."
264 :group 'org-export-latex
265 :type 'string)
268 ;;;; Time-stamps
270 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
271 "A printf format string to be applied to active time-stamps."
272 :group 'org-export-latex
273 :type 'string)
275 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
276 "A printf format string to be applied to inactive time-stamps."
277 :group 'org-export-latex
278 :type 'string)
280 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
281 "A printf format string to be applied to diary time-stamps."
282 :group 'org-export-latex
283 :type 'string)
286 ;;;; Links
288 (defcustom org-latex-image-default-option "width=.9\\linewidth"
289 "Default option for images."
290 :group 'org-export-latex
291 :type 'string)
293 (defcustom org-latex-default-figure-position "htb"
294 "Default position for latex figures."
295 :group 'org-export-latex
296 :type 'string)
298 (defcustom org-latex-inline-image-extensions
299 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
300 "Extensions of image files that can be inlined into LaTeX.
302 Note that the image extension *actually* allowed depend on the
303 way the LaTeX file is processed. When used with pdflatex, pdf,
304 jpg and png images are OK. When processing through dvi to
305 Postscript, only ps and eps are allowed. The default we use here
306 encompasses both."
307 :group 'org-export-latex
308 :type '(repeat (string :tag "Extension")))
311 ;;;; Tables
313 (defcustom org-latex-default-table-environment "tabular"
314 "Default environment used to build tables."
315 :group 'org-export-latex
316 :type 'string)
318 (defcustom org-latex-tables-centered t
319 "When non-nil, tables are exported in a center environment."
320 :group 'org-export-latex
321 :type 'boolean)
323 (defcustom org-latex-tables-verbatim nil
324 "When non-nil, tables are exported verbatim."
325 :group 'org-export-latex
326 :type 'boolean)
328 (defcustom org-latex-table-caption-above t
329 "When non-nil, place caption string at the beginning of the table.
330 Otherwise, place it near the end."
331 :group 'org-export-latex
332 :type 'boolean)
335 ;;;; Drawers
337 (defcustom org-latex-format-drawer-function nil
338 "Function called to format a drawer in LaTeX code.
340 The function must accept two parameters:
341 NAME the drawer name, like \"LOGBOOK\"
342 CONTENTS the contents of the drawer.
344 The function should return the string to be exported.
346 For example, the variable could be set to the following function
347 in order to mimic default behaviour:
349 \(defun org-latex-format-drawer-default \(name contents\)
350 \"Format a drawer element for LaTeX export.\"
351 contents\)"
352 :group 'org-export-latex
353 :type 'function)
356 ;;;; Inlinetasks
358 (defcustom org-latex-format-inlinetask-function nil
359 "Function called to format an inlinetask in LaTeX code.
361 The function must accept six parameters:
362 TODO the todo keyword, as a string
363 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
364 PRIORITY the inlinetask priority, as a string
365 NAME the inlinetask name, as a string.
366 TAGS the inlinetask tags, as a string.
367 CONTENTS the contents of the inlinetask, as a string.
369 The function should return the string to be exported.
371 For example, the variable could be set to the following function
372 in order to mimic default behaviour:
374 \(defun org-latex-format-inlinetask-default \(todo type priority name tags contents\)
375 \"Format an inline task element for LaTeX export.\"
376 \(let \(\(full-title
377 \(concat
378 \(when todo \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
379 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
380 title
381 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
382 \(format \(concat \"\\\\begin{center}\\n\"
383 \"\\\\fbox{\\n\"
384 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
385 \"%s\\n\\n\"
386 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
387 \"%s\"
388 \"\\\\end{minipage}}\"
389 \"\\\\end{center}\"\)
390 full-title contents\)\)"
391 :group 'org-export-latex
392 :type 'function)
395 ;; Src blocks
397 (defcustom org-latex-listings nil
398 "Non-nil means export source code using the listings package.
399 This package will fontify source code, possibly even with color.
400 If you want to use this, you also need to make LaTeX use the
401 listings package, and if you want to have color, the color
402 package. Just add these to `org-latex-packages-alist',
403 for example using customize, or with something like
405 (require 'org-latex)
406 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
407 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))
409 Alternatively,
411 (setq org-latex-listings 'minted)
413 causes source code to be exported using the minted package as
414 opposed to listings. If you want to use minted, you need to add
415 the minted package to `org-latex-packages-alist', for
416 example using customize, or with
418 (require 'org-latex)
419 (add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
421 In addition, it is necessary to install
422 pygments (http://pygments.org), and to configure the variable
423 `org-latex-to-pdf-process' so that the -shell-escape option is
424 passed to pdflatex."
425 :group 'org-export-latex
426 :type '(choice
427 (const :tag "Use listings" t)
428 (const :tag "Use minted" 'minted)
429 (const :tag "Export verbatim" nil)))
431 (defcustom org-latex-listings-langs
432 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
433 (c "C") (cc "C++")
434 (fortran "fortran")
435 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
436 (html "HTML") (xml "XML")
437 (tex "TeX") (latex "TeX")
438 (shell-script "bash")
439 (gnuplot "Gnuplot")
440 (ocaml "Caml") (caml "Caml")
441 (sql "SQL") (sqlite "sql"))
442 "Alist mapping languages to their listing language counterpart.
443 The key is a symbol, the major mode symbol without the \"-mode\".
444 The value is the string that should be inserted as the language parameter
445 for the listings package. If the mode name and the listings name are
446 the same, the language does not need an entry in this list - but it does not
447 hurt if it is present."
448 :group 'org-export-latex
449 :type '(repeat
450 (list
451 (symbol :tag "Major mode ")
452 (string :tag "Listings language"))))
454 (defcustom org-latex-listings-options nil
455 "Association list of options for the latex listings package.
457 These options are supplied as a comma-separated list to the
458 \\lstset command. Each element of the association list should be
459 a list containing two strings: the name of the option, and the
460 value. For example,
462 (setq org-export-latex-listings-options
463 '((\"basicstyle\" \"\\small\")
464 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
466 will typeset the code in a small size font with underlined, bold
467 black keywords.
469 Note that the same options will be applied to blocks of all
470 languages."
471 :group 'org-export-latex
472 :type '(repeat
473 (list
474 (string :tag "Listings option name ")
475 (string :tag "Listings option value"))))
477 (defcustom org-latex-minted-langs
478 '((emacs-lisp "common-lisp")
479 (cc "c++")
480 (cperl "perl")
481 (shell-script "bash")
482 (caml "ocaml"))
483 "Alist mapping languages to their minted language counterpart.
484 The key is a symbol, the major mode symbol without the \"-mode\".
485 The value is the string that should be inserted as the language parameter
486 for the minted package. If the mode name and the listings name are
487 the same, the language does not need an entry in this list - but it does not
488 hurt if it is present.
490 Note that minted uses all lower case for language identifiers,
491 and that the full list of language identifiers can be obtained
492 with:
493 pygmentize -L lexers"
494 :group 'org-export-latex
495 :type '(repeat
496 (list
497 (symbol :tag "Major mode ")
498 (string :tag "Minted language"))))
500 (defcustom org-latex-minted-options nil
501 "Association list of options for the latex minted package.
503 These options are supplied within square brackets in
504 \\begin{minted} environments. Each element of the alist should be
505 a list containing two strings: the name of the option, and the
506 value. For example,
508 (setq org-export-latex-minted-options
509 '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
511 will result in src blocks being exported with
513 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
515 as the start of the minted environment. Note that the same
516 options will be applied to blocks of all languages."
517 :group 'org-export-latex
518 :type '(repeat
519 (list
520 (string :tag "Minted option name ")
521 (string :tag "Minted option value"))))
523 (defvar org-latex-custom-lang-environments nil
524 "Association list mapping languages to language-specific latex
525 environments used during export of src blocks by the listings and
526 minted latex packages. For example,
528 (setq org-export-latex-custom-lang-environments
529 '((python \"pythoncode\")))
531 would have the effect that if org encounters begin_src python
532 during latex export it will output
534 \\begin{pythoncode}
535 <src block body>
536 \\end{pythoncode}")
539 ;;;; Plain text
541 (defcustom org-latex-quotes
542 '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
543 ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
544 "Alist for quotes to use when converting english double-quotes.
546 The CAR of each item in this alist is the language code.
547 The CDR of each item in this alist is a list of three CONS:
548 - the first CONS defines the opening quote;
549 - the second CONS defines the closing quote;
550 - the last CONS defines single quotes.
552 For each item in a CONS, the first string is a regexp
553 for allowed characters before/after the quote, the second
554 string defines the replacement string for this quote."
555 :group 'org-export-latex
556 :type '(list
557 (cons :tag "Opening quote"
558 (string :tag "Regexp for char before")
559 (string :tag "Replacement quote "))
560 (cons :tag "Closing quote"
561 (string :tag "Regexp for char after ")
562 (string :tag "Replacement quote "))
563 (cons :tag "Single quote"
564 (string :tag "Regexp for char before")
565 (string :tag "Replacement quote "))))
569 ;;; Internal Functions
571 (defun org-latex--caption/label-string (caption label info)
572 "Return caption and label LaTeX string for floats.
574 CAPTION is a secondary string \(a list of strings and Org
575 objects\) and LABEL a string representing the label. INFO is
576 a plist holding contextual information.
578 If there's no caption nor label, return the empty string.
580 For non-floats, see `org-latex--wrap-label'."
581 (let ((caption-str (and caption
582 (org-export-secondary-string
583 caption 'latex info)))
584 (label-str (if label (format "\\label{%s}" label) "")))
585 (cond
586 ((and (not caption-str) (not label)) "")
587 ((not caption-str) (format "\\label{%s}\n" label))
588 ;; Option caption format with short name.
589 ((string-match "\\[\\([^][]*\\)\\]{\\([^{}]*\\)}" caption-str)
590 (format "\\caption[%s]{%s%s}\n"
591 (org-match-string-no-properties 1 caption-str)
592 label-str
593 (org-match-string-no-properties 2 caption-str)))
594 ;; Standard caption format.
595 (t (format "\\caption{%s%s}\n" label-str caption-str)))))
597 (defun org-latex--guess-inputenc (header)
598 "Set the coding system in inputenc to what the buffer is.
600 HEADER is the LaTeX header string.
602 Return the new header."
603 (let* ((cs (or (ignore-errors
604 (latexenc-coding-system-to-inputenc
605 buffer-file-coding-system))
606 "utf8")))
607 (if (not cs)
608 header
609 ;; First translate if that is requested.
610 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
611 ;; Then find the \usepackage statement and replace the option.
612 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
613 cs header t nil 1))))
615 (defun org-latex--find-verb-separator (s)
616 "Return a character not used in string S.
617 This is used to choose a separator for constructs like \\verb."
618 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
619 (loop for c across ll
620 when (not (string-match (regexp-quote (char-to-string c)) s))
621 return (char-to-string c))))
623 (defun org-latex--make-option-string (options)
624 "Return a comma separated string of keywords and values.
625 OPTIONS is an alist where the key is the options keyword as
626 a string, and the value a list containing the keyword value, or
627 nil."
628 (mapconcat (lambda (pair)
629 (concat (first pair)
630 (when (> (length (second pair)) 0)
631 (concat "=" (second pair)))))
632 options
633 ","))
635 (defun org-latex--quotation-marks (text info)
636 "Export quotation marks depending on language conventions."
637 (mapc (lambda(l)
638 (let ((start 0))
639 (while (setq start (string-match (car l) text start))
640 (let ((new-quote (concat (match-string 1 text) (cdr l))))
641 (setq text (replace-match new-quote t t text))))))
642 (cdr (or (assoc (plist-get info :language) org-latex-quotes)
643 ;; Falls back on English.
644 (assoc "en" org-latex-quotes))))
645 text)
647 (defun org-latex--wrap-label (element output)
648 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
649 This function shouldn't be used for floats. See
650 `org-latex--caption/label-string'."
651 (let ((label (org-element-get-property :name element)))
652 (if (or (not output) (not label) (string= output "") (string= label ""))
653 output
654 (concat (format "\\label{%s}\n" label) output))))
658 ;;; Template
660 (defun org-latex-template (contents info)
661 "Return complete document string after LaTeX conversion.
662 CONTENTS is the transcoded contents string. INFO is a plist
663 holding export options."
664 (let ((title (org-export-secondary-string
665 (plist-get info :title) 'latex info)))
666 (concat
667 ;; 1. Time-stamp.
668 (and (plist-get info :time-stamp-file)
669 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
670 ;; 2. Document class and packages.
671 (let ((class (plist-get info :latex-class))
672 (class-options (plist-get info :latex-class-options)))
673 (org-element-normalize-string
674 (let* ((header (nth 1 (assoc class org-latex-classes)))
675 (document-class-string
676 (and (stringp header)
677 (if class-options
678 (replace-regexp-in-string
679 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
680 class-options header t nil 1)
681 header))))
682 (org-latex--guess-inputenc
683 (org-splice-latex-header
684 document-class-string
685 org-export-latex-default-packages-alist ; defined in org.el
686 org-export-latex-packages-alist nil ; defined in org.el
687 (plist-get info :latex-header-extra))))))
688 ;; 3. Define alert if not yet defined.
689 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
690 ;; 4. Author.
691 (let ((author (and (plist-get info :with-author)
692 (let ((auth (plist-get info :author)))
693 (and auth (org-export-secondary-string
694 auth 'latex info)))))
695 (email (and (plist-get info :with-email)
696 (org-export-secondary-string
697 (plist-get info :email) 'latex info))))
698 (cond ((and author email (not (string= "" email)))
699 (format "\\author{%s\\thanks{%s}}\n" author email))
700 (author (format "\\author{%s}\n" author))
701 (t "\\author{}\n")))
702 ;; 5. Date.
703 (let ((date (plist-get info :date)))
704 (and date (format "\\date{%s}\n" date)))
705 ;; 6. Title
706 (format "\\title{%s}\n" title)
707 ;; 7. Hyperref options.
708 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
709 (or (plist-get info :keywords) "")
710 (or (plist-get info :description) "")
711 (let ((creator-info (plist-get info :with-creator)))
712 (cond
713 ((not creator-info) "")
714 ((eq creator-info 'comment) "")
715 (t (plist-get info :creator)))))
716 ;; 7. Document start.
717 "\\begin{document}\n\n"
718 ;; 8. Title command.
719 (org-element-normalize-string
720 (cond ((string= "" title) nil)
721 ((not (stringp org-latex-title-command)) nil)
722 ((string-match "\\(?:[^%]\\|^\\)%s"
723 org-latex-title-command)
724 (format org-latex-title-command title))
725 (t org-latex-title-command)))
726 ;; 9. Table of contents.
727 (let ((depth (plist-get info :with-toc)))
728 (when depth
729 (concat (when (wholenump depth)
730 (format "\\setcounter{tocdepth}{%d}\n" depth))
731 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
732 ;; 10. Document's body.
733 contents
734 ;; 11. Creator.
735 (let ((creator-info (plist-get info :with-creator)))
736 (cond
737 ((not creator-info))
738 ((eq creator-info 'comment)
739 (format "%% %s\n" (plist-get info :creator)))
740 (t (concat (plist-get info :creator) "\n"))))
741 ;; 12. Document end.
742 "\\end{document}")))
746 ;;; Transcode Functions
748 ;;;; Block
750 (defun org-latex-center-block (center-block contents info)
751 "Transcode a CENTER-BLOCK element from Org to LaTeX.
752 CONTENTS holds the contents of the block. INFO is a plist
753 holding contextual information."
754 (org-latex--wrap-label
755 center-block
756 (format "\\begin{center}\n%s\\end{center}" contents)))
759 ;;;; Comment
761 ;; Comments are ignored.
764 ;;;; Comment Block
766 ;; Comment Blocks are ignored.
769 ;;;; Drawer
771 (defun org-latex-drawer (drawer contents info)
772 "Transcode a DRAWER element from Org to LaTeX.
773 CONTENTS holds the contents of the block. INFO is a plist
774 holding contextual information."
775 (let* ((name (org-element-get-property :drawer-name drawer))
776 (output (if (functionp org-latex-format-drawer-function)
777 (funcall org-latex-format-drawer-function
778 name contents)
779 ;; If there's no user defined function: simply
780 ;; display contents of the drawer.
781 contents)))
782 (org-latex--wrap-label drawer output)))
785 ;;;; Dynamic Block
787 (defun org-latex-dynamic-block (dynamic-block contents info)
788 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
789 CONTENTS holds the contents of the block. INFO is a plist
790 holding contextual information. See
791 `org-export-data'."
792 (org-latex--wrap-label dynamic-block contents))
795 ;;;; Emphasis
797 (defun org-latex-emphasis (emphasis contents info)
798 "Transcode EMPHASIS from Org to LaTeX.
799 CONTENTS is the contents of the emphasized text. INFO is a plist
800 holding contextual information.."
801 (format (cdr (assoc (org-element-get-property :marker emphasis)
802 org-latex-emphasis-alist))
803 contents))
806 ;;;; Entity
808 (defun org-latex-entity (entity contents info)
809 "Transcode an ENTITY object from Org to LaTeX.
810 CONTENTS are the definition itself. INFO is a plist holding
811 contextual information."
812 (let ((ent (org-element-get-property :latex entity)))
813 (if (org-element-get-property :latex-math-p entity)
814 (format "$%s$" ent)
815 ent)))
818 ;;;; Example Block
820 (defun org-latex-example-block (example-block contents info)
821 "Transcode a EXAMPLE-BLOCK element from Org to LaTeX.
822 CONTENTS is nil. INFO is a plist holding contextual information."
823 (let* ((options (or (org-element-get-property :options example-block) ""))
824 (value (org-export-handle-code
825 (org-element-get-property :value example-block) options info)))
826 (org-latex--wrap-label example-block value)))
829 ;;;; Export Snippet
831 (defun org-latex-export-snippet (export-snippet contents info)
832 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
833 CONTENTS is nil. INFO is a plist holding contextual information."
834 (org-element-get-property :value export-snippet))
837 ;;;; Export Block
839 (defun org-latex-export-block (export-block contents info)
840 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
841 CONTENTS is nil. INFO is a plist holding contextual information."
842 (when (string= (org-element-get-property :type export-block) "latex")
843 (org-remove-indentation (org-element-get-property :value export-block))))
846 ;;;; Fixed Width
848 (defun org-latex-fixed-width (fixed-width contents info)
849 "Transcode a FIXED-WIDTH element from Org to LaTeX.
850 CONTENTS is nil. INFO is a plist holding contextual information."
851 (let* ((value (org-element-normalize-string
852 (replace-regexp-in-string
853 "^[ \t]*: ?" ""
854 (org-element-get-property :value fixed-width)))))
855 (org-latex--wrap-label
856 fixed-width
857 (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
860 ;;;; Footnote Definition
862 ;; Footnote Definitions are ignored.
865 ;;;; Footnote Reference
867 (defun org-latex-footnote-reference (footnote-reference contents info)
868 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
869 CONTENTS is nil. INFO is a plist holding contextual information."
870 (concat
871 ;; Insert separator between two footnotes in a row.
872 (when (eq (plist-get info :previous-object) 'footnote-reference)
873 org-latex-footnote-separator)
874 ;; Use \footnotemark if the footnote has already been defined.
875 ;; Otherwise, define it with \footnote command.
876 (let* ((all-seen (plist-get info :seen-footnote-labels))
877 (label (org-element-get-property :label footnote-reference))
878 ;; Anonymous footnotes are always new footnotes.
879 (seenp (and label (member label all-seen)))
880 (inline-def-p (org-element-get-property
881 :inline-definition footnote-reference)))
882 (cond
883 (seenp (format "\\footnotemark[%s]" (length seenp)))
884 ;; Inline definitions are secondary strings.
885 (inline-def-p
886 (format "\\footnote{%s}"
887 (org-trim
888 (org-export-secondary-string inline-def-p 'latex info))))
889 ;; Non-inline footnotes necessarily contain a label. Retrieve
890 ;; match definition in `:footnotes-labels-alist'.
892 (format "\\footnote{%s}"
893 (org-trim
894 (org-export-data
895 (cdr (assoc label (plist-get info :footnotes-labels-alist)))
896 'latex info))))))))
899 ;;;; Headline
901 (defun org-latex-headline (headline contents info)
902 "Transcode an HEADLINE element from Org to LaTeX.
903 CONTENTS holds the contents of the headline. INFO is a plist
904 holding contextual information."
905 (let* ((class (plist-get info :latex-class))
906 (numberedp (plist-get info :section-numbers))
907 ;; Get level relative to current parsed data.
908 (level (+ (org-element-get-property :level headline)
909 (plist-get info :headline-offset)))
910 (class-sectionning (assoc class org-latex-classes))
911 ;; Section formatting will set two placeholders: one for the
912 ;; title and the other for the contents.
913 (section-fmt
914 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
915 (fboundp (nth 2 class-sectionning)))
916 (funcall (nth 2 class-sectionning) level numberedp)
917 (nth (1+ level) class-sectionning))))
918 (cond
919 ;; No section available for that LEVEL.
920 ((not sec) nil)
921 ;; Section format directly returned by a function.
922 ((stringp sec) sec)
923 ;; (numbered-section . unnumbered-section)
924 ((not (consp (cdr sec)))
925 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
926 ;; (numbered-open numbered-close)
927 ((= (length sec) 2)
928 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
929 ;; (num-in num-out no-num-in no-num-out)
930 ((= (length sec) 4)
931 (if numberedp
932 (concat (car sec) "\n%s" (nth 1 sec))
933 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
934 (text (org-export-secondary-string
935 (org-element-get-property :title headline) 'latex info))
936 (todo (and (plist-get info :with-todo-keywords)
937 (let ((todo (org-element-get-property
938 :todo-keyword headline)))
939 (and todo
940 (org-export-secondary-string todo 'latex info)))))
941 (todo-type (and todo (org-element-get-property :todo-type headline)))
942 (tags (and (plist-get info :with-tags)
943 (org-element-get-property :tags headline)))
944 (priority (and (plist-get info :with-priority)
945 (org-element-get-property :priority headline)))
946 ;; Create the headline text.
947 (full-text (if (functionp org-latex-format-headline-function)
948 ;; User-defined formatting function.
949 (funcall org-latex-format-headline-function
950 todo todo-type priority text tags)
951 ;; Default formatting.
952 (concat
953 (when todo
954 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
955 (when priority (format "\\framebox{\\#%c} " priority))
956 text
957 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
958 ;; Associate some \label to the headline for internal links.
959 (headline-labels (mapconcat
960 (lambda (p)
961 (let ((val (org-element-get-property p headline)))
962 (when val (format "\\label{%s}\n"
963 (if (eq p :begin)
964 (format "headline-%s" val)
965 val)))))
966 '(:custom-id :id :begin) ""))
967 (pre-blanks (make-string (org-element-get-property :pre-blank headline)
968 10)))
969 (cond
970 ;; Case 1: This is a footnote section: ignore it.
971 ((org-element-get-property :footnote-section-p headline) nil)
972 ;; Case 2. This is a deep sub-tree: export it as a list item.
973 ;; Also export as items headlines for which no section
974 ;; format has been found.
975 ((or (not section-fmt)
976 (and (wholenump (plist-get info :headline-levels))
977 (> level (plist-get info :headline-levels))))
978 ;; Build the real contents of the sub-tree.
979 (let ((low-level-body
980 (concat
981 ;; If the headline is the first sibling, start a list.
982 (when (org-export-first-sibling-p headline info)
983 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
984 ;; Itemize headline
985 "\\item " full-text "\n" headline-labels pre-blanks contents)))
986 ;; If headline in the last sibling, close the list, before any
987 ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
988 (if (org-export-last-sibling-p headline info)
989 (replace-regexp-in-string
990 "[ \t\n]*\\'"
991 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
992 low-level-body)
993 low-level-body)))
994 ;; Case 3. Standard headline. Export it as a section.
995 (t (format section-fmt full-text
996 (concat headline-labels pre-blanks contents))))))
999 ;;;; Horizontal Rule
1001 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1002 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1003 CONTENTS is nil. INFO is a plist holding contextual information."
1004 (let ((attr (mapconcat #'identity
1005 (org-element-get-property :attr_latex horizontal-rule)
1006 " ")))
1007 (org-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1010 ;;;; Inline Babel Call
1012 ;; Inline Babel Calls are ignored.
1015 ;;;; Inline Src Block
1017 (defun org-latex-inline-src-block (inline-src-block contents info)
1018 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1019 CONTENTS holds the contents of the item. INFO is a plist holding
1020 contextual information."
1021 (let* ((code (org-element-get-property :value inline-src-block))
1022 (separator (org-latex--find-verb-separator code)))
1023 (cond
1024 ;; Do not use a special package: transcode it verbatim.
1025 ((not org-latex-listings)
1026 (concat "\\verb" separator code separator))
1027 ;; Use minted package.
1028 ((eq org-latex-listings 'minted)
1029 (let* ((org-lang (org-element-get-property :language inline-src-block))
1030 (mint-lang (or (cadr (assq (intern org-lang)
1031 org-latex-minted-langs))
1032 org-lang))
1033 (options (org-latex--make-option-string
1034 org-latex-minted-options)))
1035 (concat (format "\\mint%s{%s}"
1036 (if (string= options "") "" (format "[%s]" options))
1037 mint-lang)
1038 separator code separator)))
1039 ;; Use listings package.
1041 ;; Maybe translate language's name.
1042 (let* ((org-lang (org-element-get-property :language inline-src-block))
1043 (lst-lang (or (cadr (assq (intern org-lang)
1044 org-latex-listings-langs))
1045 org-lang))
1046 (options (org-latex--make-option-string
1047 (append org-latex-listings-options
1048 `(("language" ,lst-lang))))))
1049 (concat (format "\\lstinline[%s]" options)
1050 separator code separator))))))
1053 ;;;; Inlinetask
1055 (defun org-latex-inlinetask (inlinetask contents info)
1056 "Transcode an INLINETASK element from Org to LaTeX.
1057 CONTENTS holds the contents of the block. INFO is a plist
1058 holding contextual information."
1059 (let ((title (org-export-secondary-string
1060 (org-element-get-property :title inlinetask) 'latex info))
1061 (todo (and (plist-get info :with-todo-keywords)
1062 (let ((todo (org-element-get-property
1063 :todo-keyword inlinetask)))
1064 (and todo
1065 (org-export-secondary-string todo 'latex info)))))
1066 (todo-type (org-element-get-property :todo-type inlinetask))
1067 (tags (and (plist-get info :with-tags)
1068 (org-element-get-property :tags inlinetask)))
1069 (priority (and (plist-get info :with-priority)
1070 (org-element-get-property :priority inlinetask))))
1071 ;; If `org-latex-format-inlinetask-function' is provided, call it
1072 ;; with appropriate arguments.
1073 (if (functionp org-latex-format-inlinetask-function)
1074 (funcall org-latex-format-inlinetask-function
1075 todo todo-type priority title tags contents)
1076 ;; Otherwise, use a default template.
1077 (org-latex--wrap-label
1078 inlinetask
1079 (let ((full-title
1080 (concat
1081 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1082 (when priority (format "\\framebox{\\#%c} " priority))
1083 title
1084 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1085 (format (concat "\\begin{center}\n"
1086 "\\fbox{\n"
1087 "\\begin{minipage}[c]{.6\\textwidth}\n"
1088 "%s\n\n"
1089 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1090 "%s"
1091 "\\end{minipage}\n"
1092 "}\n"
1093 "\\end{center}")
1094 full-title contents))))))
1097 ;;;; Item
1099 (defun org-latex-item (item contents info)
1100 "Transcode an ITEM element from Org to LaTeX.
1101 CONTENTS holds the contents of the item. INFO is a plist holding
1102 contextual information."
1103 (let* ((level (plist-get (plist-get info :parent-properties) :level))
1104 (counter (let ((count (org-element-get-property :counter item)))
1105 (and count
1106 (< level 4)
1107 (format "\\setcounter{enum%s}{%s}\n"
1108 (nth level '("i" "ii" "iii" "iv"))
1109 (1- count)))))
1110 (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
1111 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1112 ((eq checkbox 'off) "$\\Box$ ")
1113 ((eq checkbox 'trans) "$\\boxminus$ "))))
1114 (tag (let ((tag (org-element-get-property :tag item)))
1115 (and tag
1116 (format "[%s]" (org-export-secondary-string
1117 tag 'latex info))))))
1118 (concat counter "\\item" tag " " checkbox contents)))
1121 ;;;; Keyword
1123 (defun org-latex-keyword (keyword contents info)
1124 "Transcode a KEYWORD element from Org to LaTeX.
1125 CONTENTS is nil. INFO is a plist holding contextual information."
1126 (let ((key (downcase (org-element-get-property :key keyword)))
1127 (value (org-element-get-property :value keyword)))
1128 (cond
1129 ((string= key "latex") value)
1130 ((string= key "index") (format "\\index{%s}" value))
1131 ((string= key "target")
1132 (format "\\label{%s}" (org-export-solidify-link-text value)))
1133 ((string= key "toc")
1134 (let ((value (downcase value)))
1135 (cond
1136 ((string-match "\\<headlines\\>" value)
1137 (let ((depth (or (and (string-match "[0-9]+" value)
1138 (string-to-number (match-string 0 value)))
1139 (plist-get info :with-toc))))
1140 (concat
1141 (when (wholenump depth)
1142 (format "\\setcounter{tocdepth}{%s}\n" depth))
1143 "\\tableofcontents")))
1144 ((string= "tables" value) "\\listoftables")
1145 ((string= "figures" value) "\\listoffigures")
1146 ((string= "listings" value) "\\listoflistings"))))
1147 ((string= key "include")
1148 (org-export-included-file keyword 'latex info)))))
1151 ;;;; Latex Environment
1153 (defun org-latex-latex-environment (latex-environment contents info)
1154 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1155 CONTENTS is nil. INFO is a plist holding contextual information."
1156 (org-latex--wrap-label
1157 latex-environment
1158 (org-remove-indentation (org-element-get-property :value latex-environment))))
1161 ;;;; Latex Fragment
1163 (defun org-latex-latex-fragment (latex-fragment contents info)
1164 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1165 CONTENTS is nil. INFO is a plist holding contextual information."
1166 (org-element-get-property :value latex-fragment))
1169 ;;;; Line Break
1171 (defun org-latex-line-break (line-break contents info)
1172 "Transcode a LINE-BREAK object from Org to LaTeX.
1173 CONTENTS is nil. INFO is a plist holding contextual information."
1174 "\\\\")
1177 ;;;; Link
1179 (defun org-latex-link--inline-image (path info)
1180 "Return LaTeX code for an image at PATH.
1181 INFO is a plist containing export options."
1182 (let* ((parent-props (plist-get info :parent-properties))
1183 (caption (org-latex--caption/label-string
1184 (plist-get parent-props :caption)
1185 (plist-get parent-props :name)
1186 info))
1187 ;; Retrieve latex attributes from the element around.
1188 (attr (let ((raw-attr
1189 (mapconcat #'identity
1190 (plist-get parent-props :attr_latex) " ")))
1191 (unless (string= raw-attr "") raw-attr)))
1192 (disposition
1193 (cond
1194 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1195 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1196 ((or (and attr (string-match "\\<float\\>" attr))
1197 (not (string= caption "")))
1198 'float)))
1199 (placement
1200 (cond
1201 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1202 (org-match-string-no-properties 1 attr))
1203 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1204 ((eq disposition 'float)
1205 (concat "[" org-latex-default-figure-position "]"))
1206 (t ""))))
1207 ;; Now clear ATTR from any special keyword and set a default
1208 ;; value if nothing is left.
1209 (if (not attr)
1210 (setq attr "")
1211 (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
1212 attr)
1213 (replace-match "" nil nil attr))
1214 (setq attr (org-trim attr)))
1215 (setq attr (cond ((not (string= attr "")) attr)
1216 ((eq disposition 'float) "width=0.7\\textwidth")
1217 ((eq disposition 'wrap) "width=0.48\\textwidth")
1218 (t (or org-latex-image-default-option ""))))
1219 ;; Return proper string, depending on DISPOSITION.
1220 (case disposition
1221 ('wrap (format "\\begin{wrapfigure}%s
1222 \\centering
1223 \\includegraphics[%s]{%s}
1224 %s\\end{wrapfigure}" placement attr path caption))
1225 ('mulicolumn (format "\\begin{figure*}%s
1226 \\centering
1227 \\includegraphics[%s]{%s}
1228 %s\\end{figure*}" placement attr path caption))
1229 ('float (format "\\begin{figure}%s
1230 \\centering
1231 \\includegraphics[%s]{%s}
1232 %s\\end{figure}" placement attr path caption))
1233 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1235 (defun org-latex-link (link desc info)
1236 "Transcode a LINK object from Org to LaTeX.
1238 DESC is the description part of the link, or the empty string.
1239 INFO is a plist holding contextual information. See
1240 `org-export-data'."
1241 (let* ((type (org-element-get-property :type link))
1242 (raw-path (org-element-get-property :path link))
1243 ;; Ensure DESC really exists, or set it to nil.
1244 (desc (and (not (string= desc "")) desc))
1245 (imagep (org-export-inline-image-p
1246 link desc org-latex-inline-image-extensions))
1247 (path (cond
1248 ((member type '("http" "https" "ftp" "mailto"))
1249 (concat type ":" raw-path))
1250 ((and (not imagep) (string= type "file"))
1251 (when (string-match "\\(.+\\)::.+" raw-path)
1252 (setq raw-path (match-string 1 raw-path)))
1253 (if (file-name-absolute-p raw-path)
1254 (concat "file://" (expand-file-name raw-path))
1255 ;; TODO: Not implemented yet. Concat also:
1256 ;; (org-export-directory :LaTeX info)
1257 (concat "file://" raw-path)))
1258 (t raw-path)))
1259 protocol)
1260 (cond
1261 ;; Image file.
1262 (imagep (org-latex-link--inline-image path info))
1263 ;; Id: for now, assume it's an internal link. TODO: do something
1264 ;; to check if it isn't in the current file.
1265 ((string= type "id")
1266 (format "\\hyperref[%s]{%s}" path (or desc path)))
1267 ;; Custom-id, target or radioed target: replace link with the
1268 ;; normalized custom-id/target name.
1269 ((member type '("custom-id" "target" "radio"))
1270 (format "\\hyperref[%s]{%s}"
1271 (org-export-solidify-link-text path)
1272 (or desc (org-export-secondary-string path 'latex info))))
1273 ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
1274 ;; the destination of the link.
1275 ((string= type "fuzzy")
1276 (let ((destination (org-export-resolve-fuzzy-link link info)))
1277 (cond
1278 ;; Target match.
1279 ((stringp destination)
1280 (format "\\hyperref[%s]{%s}"
1281 (org-export-solidify-link-text destination)
1282 (or desc
1283 (org-export-secondary-string
1284 (org-element-get-property :raw-link link) 'latex info))))
1285 ;; Headline match.
1286 ((integerp destination)
1287 (format "\\hyperref[headline-%d]{%s}"
1288 destination
1289 (or desc
1290 (org-export-secondary-string
1291 (org-element-get-property :raw-link link) 'latex info))))
1292 ;; No match.
1293 (t (format "\\texttt{%s}"
1294 (or desc
1295 (org-export-secondary-string
1296 (org-element-get-property :raw-link link)
1297 'latex info)))))))
1298 ;; Coderef: replace link with the reference name or the
1299 ;; equivalent line number.
1300 ((string= type "coderef")
1301 (format (org-export-get-coderef-format path (or desc ""))
1302 (cdr (assoc path (plist-get info :code-refs)))))
1303 ;; Link type is handled by a special function.
1304 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1305 (funcall protocol (org-link-unescape path) desc 'latex))
1306 ;; External link with a description part.
1307 ((and path desc) (format "\\href{%s}{%s}" path desc))
1308 ;; External link without a description part.
1309 (path (format "\\url{%s}" path))
1310 ;; No path, only description. Try to do something useful.
1311 (t (format "\\texttt{%s}" desc)))))
1314 ;;;; Babel Call
1316 ;; Babel Calls are ignored.
1319 ;;;; Macro
1321 (defun org-latex-macro (macro contents info)
1322 "Transcode a MACRO element from Org to LaTeX.
1323 CONTENTS is nil. INFO is a plist holding contextual information."
1324 ;; Use available tools.
1325 (org-export-expand-macro macro info))
1328 ;;;; Paragraph
1330 (defun org-latex-paragraph (paragraph contents info)
1331 "Transcode a PARAGRAPH element from Org to LaTeX.
1332 CONTENTS is the contents of the paragraph, as a string. INFO is
1333 the plist used as a communication channel."
1334 contents)
1337 ;;;; Plain List
1339 (defun org-latex-plain-list (plain-list contents info)
1340 "Transcode a PLAIN-LIST element from Org to LaTeX.
1341 CONTENTS is the contents of the list. INFO is a plist holding
1342 contextual information."
1343 (let* ((type (org-element-get-property :type plain-list))
1344 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1345 "inparadesc" "asparadesc"))
1346 (paralist-regexp (concat
1347 "\\("
1348 (mapconcat 'identity paralist-types "\\|")
1349 "\\)"))
1350 (attr (mapconcat #'identity
1351 (org-element-get-property :attr_latex plain-list)
1352 " "))
1353 (latex-type (cond
1354 ((and attr
1355 (string-match
1356 (format "\\<%s\\>" paralist-regexp) attr))
1357 (match-string 1 attr))
1358 ((eq type 'ordered) "enumerate")
1359 ((eq type 'unordered) "itemize")
1360 ((eq type 'descriptive) "description"))))
1361 (org-latex--wrap-label
1362 plain-list
1363 (format "\\begin{%s}%s\n%s\\end{%s}"
1364 latex-type
1365 ;; Once special environment, if any, has been removed, the
1366 ;; rest of the attributes will be optional arguments.
1367 ;; They will be put inside square brackets if necessary.
1368 (let ((opt (replace-regexp-in-string
1369 (format " *%s *" paralist-regexp) "" attr)))
1370 (cond ((string= opt "") "")
1371 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1372 (t (format "[%s]" opt))))
1373 contents
1374 latex-type))))
1377 ;;;; Plain Text
1379 (defun org-latex-plain-text (text info)
1380 "Transcode a TEXT string from Org to LaTeX.
1381 TEXT is the string to transcode. INFO is a plist holding
1382 contextual information."
1383 ;; Protect %, #, &, $, ~, ^, _, { and }.
1384 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1385 (setq text
1386 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1387 ;; Protect \
1388 (setq text (replace-regexp-in-string
1389 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1390 "$\\backslash$" text nil t 1))
1391 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1392 (let ((case-fold-search nil)
1393 (start 0))
1394 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1395 (setq text (replace-match
1396 (format "\\%s{}" (match-string 1 text)) nil t text)
1397 start (match-end 0))))
1398 ;; Handle quotation marks
1399 (setq text (org-latex--quotation-marks text info))
1400 ;; Convert special strings.
1401 (when (plist-get info :with-special-strings)
1402 (while (string-match (regexp-quote "...") text)
1403 (setq text (replace-match "\\ldots{}" nil t text))))
1404 ;; Handle break preservation if required.
1405 (when (plist-get info :preserve-breaks)
1406 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1407 text)))
1408 ;; Return value.
1409 text)
1412 ;;;; Property Drawer
1414 (defun org-latex-property-drawer (property-drawer contents info)
1415 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1416 CONTENTS is nil. INFO is a plist holding contextual
1417 information."
1418 ;; The property drawer isn't exported but we want separating blank
1419 ;; lines nonetheless.
1423 ;;;; Quote Block
1425 (defun org-latex-quote-block (quote-block contents info)
1426 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1427 CONTENTS holds the contents of the block. INFO is a plist
1428 holding contextual information."
1429 (org-latex--wrap-label
1430 quote-block
1431 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1434 ;;;; Quote Section
1436 (defun org-latex-quote-section (quote-section contents info)
1437 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1438 CONTENTS is nil. INFO is a plist holding contextual information."
1439 (let ((value (org-remove-indentation
1440 (org-element-get-property :value quote-section))))
1441 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1444 ;;;; Radio Target
1446 (defun org-latex-radio-target (radio-target text info)
1447 "Transcode a RADIO-TARGET object from Org to LaTeX.
1448 TEXT is the text of the target. INFO is a plist holding
1449 contextual information."
1450 (format "\\label{%s}%s"
1451 (org-export-solidify-link-text
1452 (org-element-get-property :raw-value radio-target))
1453 text))
1456 ;;;; Special Block
1458 (defun org-latex-special-block (special-block contents info)
1459 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1460 CONTENTS holds the contents of the block. INFO is a plist
1461 holding contextual information."
1462 (let ((type (downcase (org-element-get-property :type special-block))))
1463 (org-latex--wrap-label
1464 special-block
1465 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1468 ;;;; Src Block
1470 (defun org-latex-src-block (src-block contents info)
1471 "Transcode a SRC-BLOCK element from Org to LaTeX.
1472 CONTENTS holds the contents of the item. INFO is a plist holding
1473 contextual information."
1474 (let* ((lang (org-element-get-property :language src-block))
1475 (code (org-export-handle-code
1476 (org-element-get-property :value src-block)
1477 (org-element-get-property :switches src-block)
1478 info lang))
1479 (caption (org-element-get-property :caption src-block))
1480 (label (org-element-get-property :name src-block))
1481 (custom-env (and lang
1482 (cadr (assq (intern lang)
1483 org-latex-custom-lang-environments)))))
1484 (cond
1485 ;; No source fontification.
1486 ((not org-latex-listings)
1487 (let ((caption-str (org-latex--caption/label-string
1488 caption label info))
1489 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1490 (format (or float-env "%s")
1491 (concat
1492 caption-str
1493 (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
1494 ;; Custom environment.
1495 (custom-env
1496 (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
1497 ;; Use minted package.
1498 ((eq org-latex-listings 'minted)
1499 (let* ((mint-lang (or (cadr (assq (intern lang) org-latex-minted-langs))
1500 lang))
1501 (float-env (when (or label caption)
1502 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1503 (org-latex--caption/label-string
1504 caption label info))))
1505 (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1506 (org-latex--make-option-string
1507 org-latex-minted-options)
1508 mint-lang code)))
1509 (if float-env (format float-env body) body)))
1510 ;; Use listings package.
1512 (let ((lst-lang (or (cadr (assq (intern lang) org-latex-listings-langs))
1513 lang))
1514 (caption-str (and caption
1515 (org-export-secondary-string
1516 (org-element-get-property :caption src-block)
1517 'latex info))))
1518 (concat (format "\\lstset{%s}\n"
1519 (org-latex--make-option-string
1520 (append org-latex-listings-options
1521 `(("language" ,lst-lang))
1522 (when label `(("label" ,label)))
1523 (when caption-str
1524 `(("caption" ,caption-str))))))
1525 (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
1528 ;;;; Statistics Cookie
1530 (defun org-latex-statistics-cookie (statistics-cookie contents info)
1531 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1532 CONTENTS is nil. INFO is a plist holding contextual information."
1533 (org-element-get-property :value statistics-cookie))
1536 ;;;; Subscript
1538 (defun org-latex-subscript (subscript contents info)
1539 "Transcode a SUBSCRIPT object from Org to LaTeX.
1540 CONTENTS is the contents of the object. INFO is a plist holding
1541 contextual information."
1542 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1545 ;;;; Superscript
1547 (defun org-latex-superscript (superscript contents info)
1548 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1549 CONTENTS is the contents of the object. INFO is a plist holding
1550 contextual information."
1551 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1554 ;;;; Table
1556 (defun org-latex-table--format-string (table info)
1557 "Return an appropriate format string for TABLE.
1559 INFO is the plist containing format info about the table, as
1560 returned by `org-export-table-format-info'.
1562 The format string one placeholder for the body of the table."
1563 (let* ((label (org-element-get-property :name table))
1564 (caption (org-latex--caption/label-string
1565 (org-element-get-property :caption table) label info))
1566 (attr (mapconcat #'identity
1567 (org-element-get-property :attr_latex table)
1568 " "))
1569 ;; Determine alignment string.
1570 (alignment (org-latex-table--align-string attr info))
1571 ;; Determine environment for the table: longtable, tabular...
1572 (table-env (cond
1573 ((not attr) org-latex-default-table-environment)
1574 ((string-match "\\<longtable\\>" attr) "longtable")
1575 ((string-match "\\(tabular.\\)" attr)
1576 (org-match-string-no-properties 1 attr))
1577 (t org-latex-default-table-environment)))
1578 ;; If table is a float, determine environment: table or table*.
1579 (float-env (cond
1580 ((string= "longtable" table-env) nil)
1581 ((and attr
1582 (or (string-match (regexp-quote "table*") attr)
1583 (string-match "\\<multicolumn\\>" attr)))
1584 "table*")
1585 ((or (not (string= caption "")) label) "table")))
1586 ;; Extract others display options.
1587 (width (and attr
1588 (string-match "\\<width=\\(\\S-+\\)" attr)
1589 (org-match-string-no-properties 1 attr)))
1590 (placement (if (and attr
1591 (string-match "\\<placement=\\(\\S-+\\)" attr))
1592 (org-match-string-no-properties 1 attr)
1593 (concat "["
1594 org-latex-default-figure-position
1595 "]"))))
1596 ;; Prepare the final format string for the table.
1597 (cond
1598 ;; Longtable.
1599 ((string= "longtable" table-env)
1600 (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1601 alignment
1602 (if (or (not org-latex-table-caption-above)
1603 (string= "" caption))
1605 (concat (org-trim caption) "\\\\"))
1606 (if (or org-latex-table-caption-above
1607 (string= "" caption))
1609 (concat (org-trim caption) "\\\\\n"))))
1610 ;; Others.
1611 (t (concat (when float-env
1612 (concat
1613 (format "\\begin{%s}%s\n" float-env placement)
1614 (if org-latex-table-caption-above caption "")))
1615 (when org-latex-tables-centered "\\begin{center}\n")
1616 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1617 table-env
1618 (if width (format "{%s}" width) "")
1619 alignment
1620 table-env)
1621 (when org-latex-tables-centered "\n\\end{center}")
1622 (when float-env
1623 (concat (if org-latex-table-caption-above "" caption)
1624 (format "\n\\end{%s}" float-env))))))))
1626 (defun org-latex-table--align-string (attr info)
1627 "Return an appropriate LaTeX alignment string.
1629 INFO is the plist containing format info about the table, as
1630 returned by `org-export-table-format-info'."
1631 (or (and attr
1632 (string-match "\\<align=\\(\\S-+\\)" attr)
1633 (match-string 1 attr))
1634 (let* ((align (copy-sequence (plist-get info :alignment)))
1635 (colgroups (copy-sequence (plist-get info :column-groups)))
1636 (cols (length align))
1637 (separators (make-vector (1+ cols) "")))
1638 ;; Ignore the first column if it's special.
1639 (when (plist-get info :special-column-p)
1640 (aset align 0 "") (aset colgroups 0 nil))
1641 (let ((col 0))
1642 (mapc (lambda (el)
1643 (let ((gr (aref colgroups col)))
1644 (when (memq gr '(start start-end))
1645 (aset separators col "|"))
1646 (when (memq gr '(end start-end))
1647 (aset separators (1+ col) "|")))
1648 (incf col))
1649 align))
1650 ;; Build the LaTeX specific alignment string.
1651 (loop for al across align
1652 for sep across separators
1653 concat (concat sep al) into output
1654 finally return (concat output (aref separators cols))))))
1656 (defun org-latex-table (table contents info)
1657 "Transcode a TABLE element from Org to LaTeX.
1658 CONTENTS is nil. INFO is a plist holding contextual information."
1659 (let ((attr (mapconcat #'identity
1660 (org-element-get-property :attr_latex table)
1661 " "))
1662 (raw-table (org-element-get-property :raw-table table)))
1663 (cond
1664 ;; Case 1: verbatim table.
1665 ((or org-latex-tables-verbatim
1666 (and attr (string-match "\\<verbatim\\>" attr)))
1667 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1668 (org-export-clean-table
1669 raw-table
1670 (plist-get (org-export-table-format-info raw-table)
1671 :special-column-p))))
1672 ;; Case 2: table.el table. Convert it using appropriate tools.
1673 ((eq (org-element-get-property :type table) 'table.el)
1674 (require 'table)
1675 ;; Ensure "*org-export-table*" buffer is empty.
1676 (and (get-buffer "*org-export-table*")
1677 (kill-buffer (get-buffer "*org-export-table*")))
1678 (let ((output (with-temp-buffer
1679 (insert raw-table)
1680 (goto-char 1)
1681 (re-search-forward "^[ \t]*|[^|]" nil t)
1682 (table-generate-source 'latex "*org-export-table*")
1683 (with-current-buffer "*org-export-table*"
1684 (org-trim (buffer-string))))))
1685 (kill-buffer (get-buffer "*org-export-table*"))
1686 ;; Remove left out comments.
1687 (while (string-match "^%.*\n" output)
1688 (setq output (replace-match "" t t output)))
1689 ;; When the "rmlines" attribute is provided, remove all hlines
1690 ;; but the the one separating heading from the table body.
1691 (when (and attr (string-match "\\<rmlines\\>" attr))
1692 (let ((n 0) (pos 0))
1693 (while (and (< (length output) pos)
1694 (setq pos (string-match "^\\\\hline\n?" output pos)))
1695 (incf n)
1696 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
1697 (if org-latex-tables-centered
1698 (format "\\begin{center}\n%s\n\\end{center}" output)
1699 output)))
1700 ;; Case 3: Standard table.
1701 (t (let* (
1702 (info (org-export-table-format-info raw-table))
1703 (clean-table (org-export-clean-table
1704 raw-table (plist-get info :special-column-p)))
1705 (columns-number (length (plist-get info :alignment))))
1706 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
1707 ;; particular, send each cell to
1708 ;; `org-element-parse-secondary-string' to expand any Org
1709 ;; object within. Eventually, flesh the format string out with
1710 ;; the table.
1711 (format (org-latex-table--format-string table info)
1712 (orgtbl-to-latex
1713 (mapcar
1714 (lambda (row)
1715 (if (string-match org-table-hline-regexp row)
1716 'hline
1717 (mapcar
1718 (lambda (cell)
1719 (org-export-secondary-string
1720 (org-element-parse-secondary-string
1721 cell
1722 (cdr (assq 'table org-element-string-restrictions)))
1723 'latex info))
1724 (org-split-string row "[ \t]*|[ \t]*"))))
1725 (org-split-string clean-table "\n"))
1726 `(:tstart nil :tend nil
1727 ;; Longtable environment requires specific
1728 ;; header line end.
1729 :hlend ,(and attr
1730 (string-match "\\<longtable\\>" attr)
1731 (format "\\\\
1732 \\hline
1733 \\endhead
1734 \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
1735 \\endfoot
1736 \\endlastfoot"
1737 columns-number))))))))))
1740 ;;;; Target
1742 (defun org-latex-target (target text info)
1743 "Transcode a TARGET object from Org to LaTeX.
1744 TEXT is the text of the target. INFO is a plist holding
1745 contextual information."
1746 (format "\\label{%s}%s"
1747 (org-export-solidify-link-text
1748 (org-element-get-property :raw-value target))
1749 text))
1752 ;;;; Time-stamp
1754 (defun org-latex-time-stamp (time-stamp contents info)
1755 "Transcode a TIME-STAMP object from Org to LaTeX.
1756 CONTENTS is nil. INFO is a plist holding contextual information."
1757 (let ((value (org-element-get-property :value time-stamp))
1758 (type (org-element-get-property :type time-stamp))
1759 (appt-type (org-element-get-property :appt-type time-stamp)))
1760 (concat (cond ((eq appt-type 'scheduled)
1761 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
1762 ((eq appt-type 'deadline)
1763 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
1764 ((eq appt-type 'closed)
1765 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
1766 (cond ((memq type '(active active-range))
1767 (format org-latex-active-timestamp-format value))
1768 ((memq type '(inactive inactive-range))
1769 (format org-latex-inactive-timestamp-format value))
1771 (format org-latex-diary-timestamp-format value))))))
1774 ;;;; Verbatim
1776 (defun org-latex-verbatim (element contents info)
1777 "Return verbatim text in LaTeX."
1778 (let ((fmt (cdr (assoc (org-element-get-property :marker element)
1779 org-latex-emphasis-alist)))
1780 (value (org-element-get-property :value element)))
1781 (cond
1782 ;; Handle the `verb' special case.
1783 ((eq 'verb fmt)
1784 (let ((separator (org-latex--find-verb-separator value)))
1785 (concat "\\verb" separator value separator)))
1786 ;; Handle the `protectedtexttt' special case.
1787 ((eq 'protectedtexttt fmt)
1788 (let ((start 0)
1789 (trans '(("\\" . "\\textbackslash{}")
1790 ("~" . "\\textasciitilde{}")
1791 ("^" . "\\textasciicircum{}")))
1792 (rtn "")
1793 char)
1794 (while (string-match "[\\{}$%&_#~^]" value)
1795 (setq char (match-string 0 value))
1796 (if (> (match-beginning 0) 0)
1797 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
1798 (setq value (substring value (1+ (match-beginning 0))))
1799 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1800 rtn (concat rtn char)))
1801 (setq value (concat rtn value)
1802 fmt "\\texttt{%s}")
1803 (while (string-match "--" value)
1804 (setq value (replace-match "-{}-" t t value)))
1805 (format fmt value)))
1806 ;; Else use format string.
1807 (t (format fmt value)))))
1810 ;;;; Verse Block
1812 (defun org-latex-verse-block (verse-block contents info)
1813 "Transcode a VERSE-BLOCK element from Org to LaTeX.
1814 CONTENTS is nil. INFO is a plist holding contextual information."
1815 (org-latex--wrap-label
1816 verse-block
1817 ;; In a verse environment, add a line break to each newline
1818 ;; character and change each white space at beginning of a line
1819 ;; into a space of 1 em. Also change each blank line with
1820 ;; a vertical space of 1 em.
1821 (progn
1822 (setq contents (replace-regexp-in-string
1823 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1824 (replace-regexp-in-string
1825 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1826 (org-remove-indentation
1827 (org-export-secondary-string
1828 (org-element-get-property :value verse-block)
1829 'latex info)))))
1830 (while (string-match "^[ \t]+" contents)
1831 (let ((new-str (format "\\hspace*{%dem}"
1832 (length (match-string 0 contents)))))
1833 (setq contents (replace-match new-str nil t contents))))
1834 (format "\\begin{verse}\n%s\\end{verse}" contents))))
1837 (provide 'org-latex)
1838 ;;; org-latex.el ends here