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/>.
23 ;; This library implements a LaTeX back-end for Org generic exporter.
27 ;; M-: (org-export-to-buffer 'e-latex "*Test e-LaTeX") RET
29 ;; in an org-mode buffer then switch to the buffer to see the LaTeX
30 ;; export. See contrib/lisp/org-export.el for more details on how
31 ;; this exporter works.
33 ;; It introduces three new buffer keywords: "LATEX_CLASS",
34 ;; "LATEX_CLASS_OPTIONS" and "LATEX_HEADER".
38 (eval-when-compile (require 'cl
))
39 (require 'org-element
)
44 ;;; Internal Variables
46 (defconst org-e-latex-option-alist
47 '((:date
"DATE" nil org-e-latex-date-format t
)
48 (:latex-class
"LATEX_CLASS" nil org-e-latex-default-class t
)
49 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
50 (:latex-header-extra
"LATEX_HEADER" nil nil newline
))
51 "Alist between LaTeX export properties and ways to set them.
52 See `org-export-option-alist' for more information on the
53 structure of the value.")
57 ;;; User Configurable Variables
59 (defgroup org-export-latex nil
60 "Options for exporting Org mode files to LaTeX."
61 :tag
"Org Export LaTeX"
67 (defcustom org-e-latex-default-class
"article"
68 "The default LaTeX class."
69 :group
'org-export-latex
70 :type
'(string :tag
"LaTeX class"))
72 (defcustom org-e-latex-classes
74 "\\documentclass[11pt]{article}"
75 ("\\section{%s}" .
"\\section*{%s}")
76 ("\\subsection{%s}" .
"\\subsection*{%s}")
77 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
78 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
79 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
81 "\\documentclass[11pt]{report}"
82 ("\\part{%s}" .
"\\part*{%s}")
83 ("\\chapter{%s}" .
"\\chapter*{%s}")
84 ("\\section{%s}" .
"\\section*{%s}")
85 ("\\subsection{%s}" .
"\\subsection*{%s}")
86 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
88 "\\documentclass[11pt]{book}"
89 ("\\part{%s}" .
"\\part*{%s}")
90 ("\\chapter{%s}" .
"\\chapter*{%s}")
91 ("\\section{%s}" .
"\\section*{%s}")
92 ("\\subsection{%s}" .
"\\subsection*{%s}")
93 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
94 "Alist of LaTeX classes and associated header and structure.
95 If #+LaTeX_CLASS is set in the buffer, use its value and the
96 associated information. Here is the structure of each cell:
100 \(numbered-section . unnumbered-section\)
106 The HEADER-STRING is the header that will be inserted into the LaTeX file.
107 It should contain the \\documentclass macro, and anything else that is needed
108 for this setup. To this header, the following commands will be added:
110 - Calls to \\usepackage for all packages mentioned in the variables
111 `org-e-latex-default-packages-alist' and
112 `org-e-latex-packages-alist'. Thus, your header definitions should
113 avoid to also request these packages.
115 - Lines specified via \"#+LaTeX_HEADER:\"
117 If you need more control about the sequence in which the header is built
118 up, or if you want to exclude one of these building blocks for a particular
119 class, you can use the following macro-like placeholders.
121 [DEFAULT-PACKAGES] \\usepackage statements for default packages
122 [NO-DEFAULT-PACKAGES] do not include any of the default packages
123 [PACKAGES] \\usepackage statements for packages
124 [NO-PACKAGES] do not include the packages
125 [EXTRA] the stuff from #+LaTeX_HEADER
126 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
127 [BEAMER-HEADER-EXTRA] the beamer extra headers
131 \\documentclass{article}
132 [NO-DEFAULT-PACKAGES]
134 \\providecommand{\\alert}[1]{\\textbf{#1}}
137 will omit the default packages, and will include the #+LaTeX_HEADER lines,
138 then have a call to \\providecommand, and then place \\usepackage commands
139 based on the content of `org-e-latex-packages-alist'.
141 If your header or `org-e-latex-default-packages-alist' inserts
142 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
143 a coding system derived from `buffer-file-coding-system'. See also the
144 variable `org-e-latex-inputenc-alist' for a way to influence this
147 The sectioning structure
148 ------------------------
150 The sectioning structure of the class is given by the elements following
151 the header string. For each sectioning level, a number of strings is
152 specified. A %s formatter is mandatory in each section string and will
153 be replaced by the title of the section.
155 Instead of a cons cell \(numbered . unnumbered\), you can also
156 provide a list of 2 or 4 elements,
158 \(numbered-open numbered-close\)
162 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
164 providing opening and closing strings for a LaTeX environment that should
165 represent the document section. The opening clause should have a %s
166 to represent the section title.
168 Instead of a list of sectioning commands, you can also specify a
169 function name. That function will be called with two parameters,
170 the (reduced) level of the headline, and a predicate non-nil when
171 the headline should be numbered. It must return a format string in
172 which the section title will be added."
173 :group
'org-export-latex
175 (list (string :tag
"LaTeX class")
176 (string :tag
"LaTeX header")
177 (repeat :tag
"Levels" :inline t
180 (string :tag
" numbered")
181 (string :tag
"unnumbered"))
182 (list :tag
"Environment"
183 (string :tag
"Opening (numbered)")
184 (string :tag
"Closing (numbered)")
185 (string :tag
"Opening (unnumbered)")
186 (string :tag
"Closing (unnumbered)"))
187 (function :tag
"Hook computing sectioning"))))))
189 (defcustom org-e-latex-inputenc-alist nil
190 "Alist of inputenc coding system names, and what should really be used.
191 For example, adding an entry
193 (\"utf8\" . \"utf8x\")
195 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
196 are written as utf8 files."
197 :group
'org-export-latex
200 (string :tag
"Derived from buffer")
201 (string :tag
"Use this instead"))))
203 (defcustom org-e-latex-date-format
205 "Format string for \\date{...}."
206 :group
'org-export-latex
209 (defcustom org-e-latex-title-command
"\\maketitle"
210 "The command used to insert the title just after \\begin{document}.
211 If this string contains the formatting specification \"%s\" then
212 it will be used as a formatting string, passing the title as an
214 :group
'org-export-latex
220 (defcustom org-e-latex-format-headline-function nil
221 "Function to format headline text.
223 This function will be called with 5 arguments:
224 TODO the todo keyword \(string or nil\).
225 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
226 PRIORITY the priority of the headline \(integer or nil\)
227 TEXT the main headline text \(string\).
228 TAGS the tags string, separated with colons \(string or nil\).
230 The function result will be used in the section format string.
232 As an example, one could set the variable to the following, in
233 order to reproduce the default set-up:
235 \(defun org-e-latex-format-headline-default \(todo todo-type priority text tags\)
236 \"Default format function for an headline.\"
237 \(concat \(when todo \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
238 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
240 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
241 :group
'org-export-latex
247 (defcustom org-e-latex-emphasis-alist
248 '(("*" .
"\\textbf{%s}")
250 ("_" .
"\\underline{%s}")
252 ("=" . protectedtexttt
)
254 "Alist of LaTeX expressions to convert emphasis fontifiers.
256 The key is the character used as a marker for fontification. The
257 value is a formatting string to wrap fontified text with.
259 Value can also be set to the following symbols: `verb' and
260 `protectedtexttt'. For the former, Org will use \"\\verb\" to
261 create a format string and select a delimiter character that
262 isn't in the string. For the latter, Org will use \"\\texttt\"
263 to typeset and try to protect special characters."
264 :group
'org-export-latex
270 (defcustom org-e-latex-footnote-separator
"\\textsuperscript{,}\\,"
271 "Text used to separate footnotes."
272 :group
'org-export-latex
278 (defcustom org-e-latex-active-timestamp-format
"\\textit{%s}"
279 "A printf format string to be applied to active time-stamps."
280 :group
'org-export-latex
283 (defcustom org-e-latex-inactive-timestamp-format
"\\textit{%s}"
284 "A printf format string to be applied to inactive time-stamps."
285 :group
'org-export-latex
288 (defcustom org-e-latex-diary-timestamp-format
"\\textit{%s}"
289 "A printf format string to be applied to diary time-stamps."
290 :group
'org-export-latex
296 (defcustom org-e-latex-image-default-option
"width=.9\\linewidth"
297 "Default option for images."
298 :group
'org-export-latex
301 (defcustom org-e-latex-default-figure-position
"htb"
302 "Default position for latex figures."
303 :group
'org-export-latex
306 (defcustom org-e-latex-inline-image-extensions
307 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
308 "Extensions of image files that can be inlined into LaTeX.
310 Note that the image extension *actually* allowed depend on the
311 way the LaTeX file is processed. When used with pdflatex, pdf,
312 jpg and png images are OK. When processing through dvi to
313 Postscript, only ps and eps are allowed. The default we use here
315 :group
'org-export-latex
316 :type
'(repeat (string :tag
"Extension")))
321 (defcustom org-e-latex-default-table-environment
"tabular"
322 "Default environment used to build tables."
323 :group
'org-export-latex
326 (defcustom org-e-latex-tables-centered t
327 "When non-nil, tables are exported in a center environment."
328 :group
'org-export-latex
331 (defcustom org-e-latex-tables-verbatim nil
332 "When non-nil, tables are exported verbatim."
333 :group
'org-export-latex
336 (defcustom org-e-latex-table-caption-above t
337 "When non-nil, place caption string at the beginning of the table.
338 Otherwise, place it near the end."
339 :group
'org-export-latex
345 (defcustom org-e-latex-format-drawer-function nil
346 "Function called to format a drawer in LaTeX code.
348 The function must accept two parameters:
349 NAME the drawer name, like \"LOGBOOK\"
350 CONTENTS the contents of the drawer.
352 The function should return the string to be exported.
354 For example, the variable could be set to the following function
355 in order to mimic default behaviour:
357 \(defun org-e-latex-format-drawer-default \(name contents\)
358 \"Format a drawer element for LaTeX export.\"
360 :group
'org-export-latex
366 (defcustom org-e-latex-format-inlinetask-function nil
367 "Function called to format an inlinetask in LaTeX code.
369 The function must accept six parameters:
370 TODO the todo keyword, as a string
371 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
372 PRIORITY the inlinetask priority, as a string
373 NAME the inlinetask name, as a string.
374 TAGS the inlinetask tags, as a string.
375 CONTENTS the contents of the inlinetask, as a string.
377 The function should return the string to be exported.
379 For example, the variable could be set to the following function
380 in order to mimic default behaviour:
382 \(defun org-e-latex-format-inlinetask-default \(todo type priority name tags contents\)
383 \"Format an inline task element for LaTeX export.\"
386 \(when todo \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
387 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
389 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
390 \(format \(concat \"\\\\begin{center}\\n\"
392 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
394 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
396 \"\\\\end{minipage}}\"
397 \"\\\\end{center}\"\)
398 full-title contents\)\)"
399 :group
'org-export-latex
405 (defcustom org-e-latex-listings nil
406 "Non-nil means export source code using the listings package.
407 This package will fontify source code, possibly even with color.
408 If you want to use this, you also need to make LaTeX use the
409 listings package, and if you want to have color, the color
410 package. Just add these to `org-e-latex-packages-alist',
411 for example using customize, or with something like
413 (require 'org-e-latex)
414 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
415 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))
419 (setq org-e-latex-listings 'minted)
421 causes source code to be exported using the minted package as
422 opposed to listings. If you want to use minted, you need to add
423 the minted package to `org-e-latex-packages-alist', for
424 example using customize, or with
426 (require 'org-e-latex)
427 (add-to-list 'org-e-latex-packages-alist '(\"\" \"minted\"))
429 In addition, it is necessary to install
430 pygments (http://pygments.org), and to configure the variable
431 `org-e-latex-to-pdf-process' so that the -shell-escape option is
433 :group
'org-export-latex
435 (const :tag
"Use listings" t
)
436 (const :tag
"Use minted" 'minted
)
437 (const :tag
"Export verbatim" nil
)))
439 (defcustom org-e-latex-listings-langs
440 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
443 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
444 (html "HTML") (xml "XML")
445 (tex "TeX") (latex "TeX")
446 (shell-script "bash")
448 (ocaml "Caml") (caml "Caml")
449 (sql "SQL") (sqlite "sql"))
450 "Alist mapping languages to their listing language counterpart.
451 The key is a symbol, the major mode symbol without the \"-mode\".
452 The value is the string that should be inserted as the language parameter
453 for the listings package. If the mode name and the listings name are
454 the same, the language does not need an entry in this list - but it does not
455 hurt if it is present."
456 :group
'org-export-latex
459 (symbol :tag
"Major mode ")
460 (string :tag
"Listings language"))))
462 (defcustom org-e-latex-listings-options nil
463 "Association list of options for the latex listings package.
465 These options are supplied as a comma-separated list to the
466 \\lstset command. Each element of the association list should be
467 a list containing two strings: the name of the option, and the
470 (setq org-export-latex-listings-options
471 '((\"basicstyle\" \"\\small\")
472 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
474 will typeset the code in a small size font with underlined, bold
477 Note that the same options will be applied to blocks of all
479 :group
'org-export-latex
482 (string :tag
"Listings option name ")
483 (string :tag
"Listings option value"))))
485 (defcustom org-e-latex-minted-langs
486 '((emacs-lisp "common-lisp")
489 (shell-script "bash")
491 "Alist mapping languages to their minted language counterpart.
492 The key is a symbol, the major mode symbol without the \"-mode\".
493 The value is the string that should be inserted as the language parameter
494 for the minted package. If the mode name and the listings name are
495 the same, the language does not need an entry in this list - but it does not
496 hurt if it is present.
498 Note that minted uses all lower case for language identifiers,
499 and that the full list of language identifiers can be obtained
501 pygmentize -L lexers"
502 :group
'org-export-latex
505 (symbol :tag
"Major mode ")
506 (string :tag
"Minted language"))))
508 (defcustom org-e-latex-minted-options nil
509 "Association list of options for the latex minted package.
511 These options are supplied within square brackets in
512 \\begin{minted} environments. Each element of the alist should be
513 a list containing two strings: the name of the option, and the
516 (setq org-export-latex-minted-options
517 '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
519 will result in src blocks being exported with
521 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
523 as the start of the minted environment. Note that the same
524 options will be applied to blocks of all languages."
525 :group
'org-export-latex
528 (string :tag
"Minted option name ")
529 (string :tag
"Minted option value"))))
531 (defvar org-e-latex-custom-lang-environments nil
532 "Association list mapping languages to language-specific latex
533 environments used during export of src blocks by the listings and
534 minted latex packages. For example,
536 (setq org-export-latex-custom-lang-environments
537 '((python \"pythoncode\")))
539 would have the effect that if org encounters begin_src python
540 during latex export it will output
549 (defcustom org-e-latex-quotes
550 '(("fr" ("\\(\\s-\\|[[(]\\)\"" .
"«~") ("\\(\\S-\\)\"" .
"~»") ("\\(\\s-\\|(\\)'" .
"'"))
551 ("en" ("\\(\\s-\\|[[(]\\)\"" .
"``") ("\\(\\S-\\)\"" .
"''") ("\\(\\s-\\|(\\)'" .
"`")))
552 "Alist for quotes to use when converting english double-quotes.
554 The CAR of each item in this alist is the language code.
555 The CDR of each item in this alist is a list of three CONS:
556 - the first CONS defines the opening quote;
557 - the second CONS defines the closing quote;
558 - the last CONS defines single quotes.
560 For each item in a CONS, the first string is a regexp
561 for allowed characters before/after the quote, the second
562 string defines the replacement string for this quote."
563 :group
'org-export-latex
565 (cons :tag
"Opening quote"
566 (string :tag
"Regexp for char before")
567 (string :tag
"Replacement quote "))
568 (cons :tag
"Closing quote"
569 (string :tag
"Regexp for char after ")
570 (string :tag
"Replacement quote "))
571 (cons :tag
"Single quote"
572 (string :tag
"Regexp for char before")
573 (string :tag
"Replacement quote "))))
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
) "")))
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
)
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
))
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
636 (mapconcat (lambda (pair)
638 (when (> (length (second pair
)) 0)
639 (concat "=" (second pair
)))))
643 (defun org-e-latex--quotation-marks (text info
)
644 "Export quotation marks depending on language conventions."
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
))))
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
""))
662 (concat (format "\\label{%s}\n" label
) output
))))
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
)))
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
)
686 (replace-regexp-in-string
687 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
688 class-options header t nil
1)
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"
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
))
711 (let ((date (plist-get info
:date
)))
712 (and date
(format "\\date{%s}\n" date
)))
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
)))
721 ((not creator-info
) "")
722 ((eq creator-info
'comment
) "")
723 (t (plist-get info
:creator
)))))
724 ;; 7. Document start.
725 "\\begin{document}\n\n"
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
)))
737 (concat (when (wholenump depth
)
738 (format "\\setcounter{tocdepth}{%d}\n" depth
))
739 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
740 ;; 10. Document's body.
743 (let ((creator-info (plist-get info
:with-creator
)))
746 ((eq creator-info
'comment
)
747 (format "%% %s\n" (plist-get info
:creator
)))
748 (t (concat (plist-get info
:creator
) "\n"))))
754 ;;; Transcode Functions
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
764 (format "\\begin{center}\n%s\\end{center}" contents
)))
769 ;; Comments are ignored.
774 ;; Comment Blocks are ignored.
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
787 ;; If there's no user defined function: simply
788 ;; display contents of the drawer.
790 (org-e-latex--wrap-label drawer output
)))
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
800 (org-e-latex--wrap-label dynamic-block contents
))
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
))
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
)
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
)))
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
))
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
))))
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
862 (org-element-get-property :value fixed-width
)))))
863 (org-e-latex--wrap-label
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."
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.
885 ((not (org-export-footnote-first-reference-p footnote-reference info
))
886 (format "\\footnotemark[%s]"
887 (org-export-get-footnote-number footnote-reference info
)))
888 ;; Inline definitions are secondary strings.
889 ((eq (org-element-get-property :type footnote-reference
) 'inline
)
890 (format "\\footnote{%s}"
892 (org-export-secondary-string
893 (org-export-get-footnote-definition footnote-reference info
)
895 ;; Non-inline footnotes definitions are full Org data.
897 (format "\\footnote{%s}"
900 (org-export-get-footnote-definition footnote-reference info
)
906 (defun org-e-latex-headline (headline contents info
)
907 "Transcode an HEADLINE element from Org to LaTeX.
908 CONTENTS holds the contents of the headline. INFO is a plist
909 holding contextual information."
910 (let* ((class (plist-get info
:latex-class
))
911 (numberedp (plist-get info
:section-numbers
))
912 ;; Get level relative to current parsed data.
913 (level (+ (org-element-get-property :level headline
)
914 (plist-get info
:headline-offset
)))
915 (class-sectionning (assoc class org-e-latex-classes
))
916 ;; Section formatting will set two placeholders: one for the
917 ;; title and the other for the contents.
919 (let ((sec (if (and (symbolp (nth 2 class-sectionning
))
920 (fboundp (nth 2 class-sectionning
)))
921 (funcall (nth 2 class-sectionning
) level numberedp
)
922 (nth (1+ level
) class-sectionning
))))
924 ;; No section available for that LEVEL.
926 ;; Section format directly returned by a function.
928 ;; (numbered-section . unnumbered-section)
929 ((not (consp (cdr sec
)))
930 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
931 ;; (numbered-open numbered-close)
933 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
934 ;; (num-in num-out no-num-in no-num-out)
937 (concat (car sec
) "\n%s" (nth 1 sec
))
938 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
939 (text (org-export-secondary-string
940 (org-element-get-property :title headline
) 'latex info
))
941 (todo (and (plist-get info
:with-todo-keywords
)
942 (let ((todo (org-element-get-property
943 :todo-keyword headline
)))
945 (org-export-secondary-string todo
'latex info
)))))
946 (todo-type (and todo
(org-element-get-property :todo-type headline
)))
947 (tags (and (plist-get info
:with-tags
)
948 (org-element-get-property :tags headline
)))
949 (priority (and (plist-get info
:with-priority
)
950 (org-element-get-property :priority headline
)))
951 ;; Create the headline text.
952 (full-text (if (functionp org-e-latex-format-headline-function
)
953 ;; User-defined formatting function.
954 (funcall org-e-latex-format-headline-function
955 todo todo-type priority text tags
)
956 ;; Default formatting.
959 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
960 (when priority
(format "\\framebox{\\#%c} " priority
))
962 (when tags
(format "\\hfill{}\\textsc{%s}" tags
)))))
963 ;; Associate some \label to the headline for internal links.
964 (headline-labels (mapconcat
966 (let ((val (org-element-get-property p headline
)))
967 (when val
(format "\\label{%s}\n"
969 (format "headline-%s" val
)
971 '(:custom-id
:id
:begin
) ""))
972 (pre-blanks (make-string (org-element-get-property :pre-blank headline
)
975 ;; Case 1: This is a footnote section: ignore it.
976 ((org-element-get-property :footnote-section-p headline
) nil
)
977 ;; Case 2. This is a deep sub-tree: export it as a list item.
978 ;; Also export as items headlines for which no section
979 ;; format has been found.
980 ((or (not section-fmt
)
981 (and (wholenump (plist-get info
:headline-levels
))
982 (> level
(plist-get info
:headline-levels
))))
983 ;; Build the real contents of the sub-tree.
984 (let ((low-level-body
986 ;; If the headline is the first sibling, start a list.
987 (when (org-export-first-sibling-p headline info
)
988 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
990 "\\item " full-text
"\n" headline-labels pre-blanks contents
)))
991 ;; If headline in the last sibling, close the list, before any
992 ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
993 (if (org-export-last-sibling-p headline info
)
994 (replace-regexp-in-string
996 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
999 ;; Case 3. Standard headline. Export it as a section.
1000 (t (format section-fmt full-text
1001 (concat headline-labels pre-blanks contents
))))))
1004 ;;;; Horizontal Rule
1006 (defun org-e-latex-horizontal-rule (horizontal-rule contents info
)
1007 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1008 CONTENTS is nil. INFO is a plist holding contextual information."
1009 (let ((attr (mapconcat #'identity
1010 (org-element-get-property :attr_latex horizontal-rule
)
1012 (org-e-latex--wrap-label horizontal-rule
(concat "\\hrule " attr
))))
1015 ;;;; Inline Babel Call
1017 ;; Inline Babel Calls are ignored.
1020 ;;;; Inline Src Block
1022 (defun org-e-latex-inline-src-block (inline-src-block contents info
)
1023 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1024 CONTENTS holds the contents of the item. INFO is a plist holding
1025 contextual information."
1026 (let* ((code (org-element-get-property :value inline-src-block
))
1027 (separator (org-e-latex--find-verb-separator code
)))
1029 ;; Do not use a special package: transcode it verbatim.
1030 ((not org-e-latex-listings
)
1031 (concat "\\verb" separator code separator
))
1032 ;; Use minted package.
1033 ((eq org-e-latex-listings
'minted
)
1034 (let* ((org-lang (org-element-get-property :language inline-src-block
))
1035 (mint-lang (or (cadr (assq (intern org-lang
)
1036 org-e-latex-minted-langs
))
1038 (options (org-e-latex--make-option-string
1039 org-e-latex-minted-options
)))
1040 (concat (format "\\mint%s{%s}"
1041 (if (string= options
"") "" (format "[%s]" options
))
1043 separator code separator
)))
1044 ;; Use listings package.
1046 ;; Maybe translate language's name.
1047 (let* ((org-lang (org-element-get-property :language inline-src-block
))
1048 (lst-lang (or (cadr (assq (intern org-lang
)
1049 org-e-latex-listings-langs
))
1051 (options (org-e-latex--make-option-string
1052 (append org-e-latex-listings-options
1053 `(("language" ,lst-lang
))))))
1054 (concat (format "\\lstinline[%s]" options
)
1055 separator code separator
))))))
1060 (defun org-e-latex-inlinetask (inlinetask contents info
)
1061 "Transcode an INLINETASK element from Org to LaTeX.
1062 CONTENTS holds the contents of the block. INFO is a plist
1063 holding contextual information."
1064 (let ((title (org-export-secondary-string
1065 (org-element-get-property :title inlinetask
) 'latex info
))
1066 (todo (and (plist-get info
:with-todo-keywords
)
1067 (let ((todo (org-element-get-property
1068 :todo-keyword inlinetask
)))
1070 (org-export-secondary-string todo
'latex info
)))))
1071 (todo-type (org-element-get-property :todo-type inlinetask
))
1072 (tags (and (plist-get info
:with-tags
)
1073 (org-element-get-property :tags inlinetask
)))
1074 (priority (and (plist-get info
:with-priority
)
1075 (org-element-get-property :priority inlinetask
))))
1076 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1077 ;; with appropriate arguments.
1078 (if (functionp org-e-latex-format-inlinetask-function
)
1079 (funcall org-e-latex-format-inlinetask-function
1080 todo todo-type priority title tags contents
)
1081 ;; Otherwise, use a default template.
1082 (org-e-latex--wrap-label
1086 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1087 (when priority
(format "\\framebox{\\#%c} " priority
))
1089 (when tags
(format "\\hfill{}\\textsc{%s}" tags
)))))
1090 (format (concat "\\begin{center}\n"
1092 "\\begin{minipage}[c]{.6\\textwidth}\n"
1094 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1099 full-title contents
))))))
1104 (defun org-e-latex-item (item contents info
)
1105 "Transcode an ITEM element from Org to LaTeX.
1106 CONTENTS holds the contents of the item. INFO is a plist holding
1107 contextual information."
1108 (let* ((level (plist-get (plist-get info
:parent-properties
) :level
))
1109 (counter (let ((count (org-element-get-property :counter item
)))
1112 (format "\\setcounter{enum%s}{%s}\n"
1113 (nth level
'("i" "ii" "iii" "iv"))
1115 (checkbox (let ((checkbox (org-element-get-property :checkbox item
)))
1116 (cond ((eq checkbox
'on
) "$\\boxtimes$ ")
1117 ((eq checkbox
'off
) "$\\Box$ ")
1118 ((eq checkbox
'trans
) "$\\boxminus$ "))))
1119 (tag (let ((tag (org-element-get-property :tag item
)))
1121 (format "[%s]" (org-export-secondary-string
1122 tag
'latex info
))))))
1123 (concat counter
"\\item" tag
" " checkbox contents
)))
1128 (defun org-e-latex-keyword (keyword contents info
)
1129 "Transcode a KEYWORD element from Org to LaTeX.
1130 CONTENTS is nil. INFO is a plist holding contextual information."
1131 (let ((key (downcase (org-element-get-property :key keyword
)))
1132 (value (org-element-get-property :value keyword
)))
1134 ((string= key
"latex") value
)
1135 ((string= key
"index") (format "\\index{%s}" value
))
1136 ((string= key
"target")
1137 (format "\\label{%s}" (org-export-solidify-link-text value
)))
1138 ((string= key
"toc")
1139 (let ((value (downcase value
)))
1141 ((string-match "\\<headlines\\>" value
)
1142 (let ((depth (or (and (string-match "[0-9]+" value
)
1143 (string-to-number (match-string 0 value
)))
1144 (plist-get info
:with-toc
))))
1146 (when (wholenump depth
)
1147 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1148 "\\tableofcontents")))
1149 ((string= "tables" value
) "\\listoftables")
1150 ((string= "figures" value
) "\\listoffigures")
1151 ((string= "listings" value
) "\\listoflistings"))))
1152 ((string= key
"include")
1153 (org-export-included-file keyword
'latex info
)))))
1156 ;;;; Latex Environment
1158 (defun org-e-latex-latex-environment (latex-environment contents info
)
1159 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1160 CONTENTS is nil. INFO is a plist holding contextual information."
1161 (org-e-latex--wrap-label
1163 (org-remove-indentation (org-element-get-property :value latex-environment
))))
1168 (defun org-e-latex-latex-fragment (latex-fragment contents info
)
1169 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1170 CONTENTS is nil. INFO is a plist holding contextual information."
1171 (org-element-get-property :value latex-fragment
))
1176 (defun org-e-latex-line-break (line-break contents info
)
1177 "Transcode a LINE-BREAK object from Org to LaTeX.
1178 CONTENTS is nil. INFO is a plist holding contextual information."
1184 (defun org-e-latex-link--inline-image (path info
)
1185 "Return LaTeX code for an image at PATH.
1186 INFO is a plist containing export options."
1187 (let* ((parent-props (plist-get info
:parent-properties
))
1188 (caption (org-e-latex--caption/label-string
1189 (plist-get parent-props
:caption
)
1190 (plist-get parent-props
:name
)
1192 ;; Retrieve latex attributes from the element around.
1193 (attr (let ((raw-attr
1194 (mapconcat #'identity
1195 (plist-get parent-props
:attr_latex
) " ")))
1196 (unless (string= raw-attr
"") raw-attr
)))
1199 ((and attr
(string-match "\\<wrap\\>" attr
)) 'wrap
)
1200 ((and attr
(string-match "\\<multicolumn\\>" attr
)) 'multicolumn
)
1201 ((or (and attr
(string-match "\\<float\\>" attr
))
1202 (not (string= caption
"")))
1206 ((and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
1207 (org-match-string-no-properties 1 attr
))
1208 ((eq disposition
'wrap
) "{l}{0.5\\textwidth}")
1209 ((eq disposition
'float
)
1210 (concat "[" org-e-latex-default-figure-position
"]"))
1212 ;; Now clear ATTR from any special keyword and set a default
1213 ;; value if nothing is left.
1216 (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
1218 (replace-match "" nil nil attr
))
1219 (setq attr
(org-trim attr
)))
1220 (setq attr
(cond ((not (string= attr
"")) attr
)
1221 ((eq disposition
'float
) "width=0.7\\textwidth")
1222 ((eq disposition
'wrap
) "width=0.48\\textwidth")
1223 (t (or org-e-latex-image-default-option
""))))
1224 ;; Return proper string, depending on DISPOSITION.
1226 ('wrap
(format "\\begin{wrapfigure}%s
1228 \\includegraphics[%s]{%s}
1229 %s\\end{wrapfigure}" placement attr path caption
))
1230 ('mulicolumn
(format "\\begin{figure*}%s
1232 \\includegraphics[%s]{%s}
1233 %s\\end{figure*}" placement attr path caption
))
1234 ('float
(format "\\begin{figure}%s
1236 \\includegraphics[%s]{%s}
1237 %s\\end{figure}" placement attr path caption
))
1238 (t (format "\\includegraphics[%s]{%s}" attr path
)))))
1240 (defun org-e-latex-link (link desc info
)
1241 "Transcode a LINK object from Org to LaTeX.
1243 DESC is the description part of the link, or the empty string.
1244 INFO is a plist holding contextual information. See
1246 (let* ((type (org-element-get-property :type link
))
1247 (raw-path (org-element-get-property :path link
))
1248 ;; Ensure DESC really exists, or set it to nil.
1249 (desc (and (not (string= desc
"")) desc
))
1250 (imagep (org-export-inline-image-p
1251 link org-e-latex-inline-image-extensions
))
1253 ((member type
'("http" "https" "ftp" "mailto"))
1254 (concat type
":" raw-path
))
1255 ((and (not imagep
) (string= type
"file"))
1256 (when (string-match "\\(.+\\)::.+" raw-path
)
1257 (setq raw-path
(match-string 1 raw-path
)))
1258 (if (file-name-absolute-p raw-path
)
1259 (concat "file://" (expand-file-name raw-path
))
1260 ;; TODO: Not implemented yet. Concat also:
1261 ;; (org-export-directory :LaTeX info)
1262 (concat "file://" raw-path
)))
1267 (imagep (org-e-latex-link--inline-image path info
))
1268 ;; Id: for now, assume it's an internal link. TODO: do something
1269 ;; to check if it isn't in the current file.
1270 ((string= type
"id")
1271 (format "\\hyperref[%s]{%s}" path
(or desc path
)))
1272 ;; Custom-id, target or radioed target: replace link with the
1273 ;; normalized custom-id/target name.
1274 ((member type
'("custom-id" "target" "radio"))
1275 (format "\\hyperref[%s]{%s}"
1276 (org-export-solidify-link-text path
)
1277 (or desc
(org-export-secondary-string path
'latex info
))))
1278 ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
1279 ;; the destination of the link.
1280 ((string= type
"fuzzy")
1281 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1284 ((stringp destination
)
1285 (format "\\hyperref[%s]{%s}"
1286 (org-export-solidify-link-text destination
)
1288 (org-export-secondary-string
1289 (org-element-get-property :raw-link link
) 'latex info
))))
1291 ((integerp destination
)
1292 (format "\\hyperref[headline-%d]{%s}"
1295 (org-export-secondary-string
1296 (org-element-get-property :raw-link link
) 'latex info
))))
1298 (t (format "\\texttt{%s}"
1300 (org-export-secondary-string
1301 (org-element-get-property :raw-link link
)
1303 ;; Coderef: replace link with the reference name or the
1304 ;; equivalent line number.
1305 ((string= type
"coderef")
1306 (format (org-export-get-coderef-format path
(or desc
""))
1307 (cdr (assoc path
(plist-get info
:code-refs
)))))
1308 ;; Link type is handled by a special function.
1309 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1310 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1311 ;; External link with a description part.
1312 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1313 ;; External link without a description part.
1314 (path (format "\\url{%s}" path
))
1315 ;; No path, only description. Try to do something useful.
1316 (t (format "\\texttt{%s}" desc
)))))
1321 ;; Babel Calls are ignored.
1326 (defun org-e-latex-macro (macro contents info
)
1327 "Transcode a MACRO element from Org to LaTeX.
1328 CONTENTS is nil. INFO is a plist holding contextual information."
1329 ;; Use available tools.
1330 (org-export-expand-macro macro info
))
1335 (defun org-e-latex-paragraph (paragraph contents info
)
1336 "Transcode a PARAGRAPH element from Org to LaTeX.
1337 CONTENTS is the contents of the paragraph, as a string. INFO is
1338 the plist used as a communication channel."
1344 (defun org-e-latex-plain-list (plain-list contents info
)
1345 "Transcode a PLAIN-LIST element from Org to LaTeX.
1346 CONTENTS is the contents of the list. INFO is a plist holding
1347 contextual information."
1348 (let* ((type (org-element-get-property :type plain-list
))
1349 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1350 "inparadesc" "asparadesc"))
1351 (paralist-regexp (concat
1353 (mapconcat 'identity paralist-types
"\\|")
1355 (attr (mapconcat #'identity
1356 (org-element-get-property :attr_latex plain-list
)
1361 (format "\\<%s\\>" paralist-regexp
) attr
))
1362 (match-string 1 attr
))
1363 ((eq type
'ordered
) "enumerate")
1364 ((eq type
'unordered
) "itemize")
1365 ((eq type
'descriptive
) "description"))))
1366 (org-e-latex--wrap-label
1368 (format "\\begin{%s}%s\n%s\\end{%s}"
1370 ;; Once special environment, if any, has been removed, the
1371 ;; rest of the attributes will be optional arguments.
1372 ;; They will be put inside square brackets if necessary.
1373 (let ((opt (replace-regexp-in-string
1374 (format " *%s *" paralist-regexp
) "" attr
)))
1375 (cond ((string= opt
"") "")
1376 ((string-match "\\`\\[[^][]+\\]\\'" opt
) opt
)
1377 (t (format "[%s]" opt
))))
1384 (defun org-e-latex-plain-text (text info
)
1385 "Transcode a TEXT string from Org to LaTeX.
1386 TEXT is the string to transcode. INFO is a plist holding
1387 contextual information."
1388 ;; Protect %, #, &, $, ~, ^, _, { and }.
1389 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text
)
1391 (replace-match (format "\\%s" (match-string 2 text
)) nil t text
2)))
1393 (setq text
(replace-regexp-in-string
1394 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1395 "$\\backslash$" text nil t
1))
1396 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1397 (let ((case-fold-search nil
)
1399 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start
)
1400 (setq text
(replace-match
1401 (format "\\%s{}" (match-string 1 text
)) nil t text
)
1402 start
(match-end 0))))
1403 ;; Handle quotation marks
1404 (setq text
(org-e-latex--quotation-marks text info
))
1405 ;; Convert special strings.
1406 (when (plist-get info
:with-special-strings
)
1407 (while (string-match (regexp-quote "...") text
)
1408 (setq text
(replace-match "\\ldots{}" nil t text
))))
1409 ;; Handle break preservation if required.
1410 (when (plist-get info
:preserve-breaks
)
1411 (setq text
(replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1417 ;;;; Property Drawer
1419 (defun org-e-latex-property-drawer (property-drawer contents info
)
1420 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1421 CONTENTS is nil. INFO is a plist holding contextual
1423 ;; The property drawer isn't exported but we want separating blank
1424 ;; lines nonetheless.
1430 (defun org-e-latex-quote-block (quote-block contents info
)
1431 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1432 CONTENTS holds the contents of the block. INFO is a plist
1433 holding contextual information."
1434 (org-e-latex--wrap-label
1436 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
1441 (defun org-e-latex-quote-section (quote-section contents info
)
1442 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1443 CONTENTS is nil. INFO is a plist holding contextual information."
1444 (let ((value (org-remove-indentation
1445 (org-element-get-property :value quote-section
))))
1446 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
1451 (defun org-e-latex-radio-target (radio-target text info
)
1452 "Transcode a RADIO-TARGET object from Org to LaTeX.
1453 TEXT is the text of the target. INFO is a plist holding
1454 contextual information."
1455 (format "\\label{%s}%s"
1456 (org-export-solidify-link-text
1457 (org-element-get-property :raw-value radio-target
))
1463 (defun org-e-latex-special-block (special-block contents info
)
1464 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1465 CONTENTS holds the contents of the block. INFO is a plist
1466 holding contextual information."
1467 (let ((type (downcase (org-element-get-property :type special-block
))))
1468 (org-e-latex--wrap-label
1470 (format "\\begin{%s}\n%s\\end{%s}" type contents type
))))
1475 (defun org-e-latex-src-block (src-block contents info
)
1476 "Transcode a SRC-BLOCK element from Org to LaTeX.
1477 CONTENTS holds the contents of the item. INFO is a plist holding
1478 contextual information."
1479 (let* ((lang (org-element-get-property :language src-block
))
1480 (code (org-export-handle-code
1481 (org-element-get-property :value src-block
)
1482 (org-element-get-property :switches src-block
)
1484 (caption (org-element-get-property :caption src-block
))
1485 (label (org-element-get-property :name src-block
))
1486 (custom-env (and lang
1487 (cadr (assq (intern lang
)
1488 org-e-latex-custom-lang-environments
)))))
1490 ;; No source fontification.
1491 ((not org-e-latex-listings
)
1492 (let ((caption-str (org-e-latex--caption/label-string
1493 caption label info
))
1494 (float-env (when caption
"\\begin{figure}[H]\n%s\n\\end{figure}")))
1495 (format (or float-env
"%s")
1498 (format "\\begin{verbatim}\n%s\\end{verbatim}" code
)))))
1499 ;; Custom environment.
1501 (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env
))
1502 ;; Use minted package.
1503 ((eq org-e-latex-listings
'minted
)
1504 (let* ((mint-lang (or (cadr (assq (intern lang
) org-e-latex-minted-langs
))
1506 (float-env (when (or label caption
)
1507 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1508 (org-e-latex--caption/label-string
1509 caption label info
))))
1510 (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1511 (org-e-latex--make-option-string
1512 org-e-latex-minted-options
)
1514 (if float-env
(format float-env body
) body
)))
1515 ;; Use listings package.
1517 (let ((lst-lang (or (cadr (assq (intern lang
) org-e-latex-listings-langs
))
1519 (caption-str (and caption
1520 (org-export-secondary-string
1521 (org-element-get-property :caption src-block
)
1523 (concat (format "\\lstset{%s}\n"
1524 (org-e-latex--make-option-string
1525 (append org-e-latex-listings-options
1526 `(("language" ,lst-lang
))
1527 (when label
`(("label" ,label
)))
1529 `(("caption" ,caption-str
))))))
1530 (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code
)))))))
1533 ;;;; Statistics Cookie
1535 (defun org-e-latex-statistics-cookie (statistics-cookie contents info
)
1536 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1537 CONTENTS is nil. INFO is a plist holding contextual information."
1538 (org-element-get-property :value statistics-cookie
))
1543 (defun org-e-latex-subscript (subscript contents info
)
1544 "Transcode a SUBSCRIPT object from Org to LaTeX.
1545 CONTENTS is the contents of the object. INFO is a plist holding
1546 contextual information."
1547 (format (if (= (length contents
) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents
))
1552 (defun org-e-latex-superscript (superscript contents info
)
1553 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1554 CONTENTS is the contents of the object. INFO is a plist holding
1555 contextual information."
1556 (format (if (= (length contents
) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents
))
1561 (defun org-e-latex-table--format-string (table info
)
1562 "Return an appropriate format string for TABLE.
1564 INFO is the plist containing format info about the table, as
1565 returned by `org-export-table-format-info'.
1567 The format string one placeholder for the body of the table."
1568 (let* ((label (org-element-get-property :name table
))
1569 (caption (org-e-latex--caption/label-string
1570 (org-element-get-property :caption table
) label info
))
1571 (attr (mapconcat #'identity
1572 (org-element-get-property :attr_latex table
)
1574 ;; Determine alignment string.
1575 (alignment (org-e-latex-table--align-string attr info
))
1576 ;; Determine environment for the table: longtable, tabular...
1578 ((not attr
) org-e-latex-default-table-environment
)
1579 ((string-match "\\<longtable\\>" attr
) "longtable")
1580 ((string-match "\\(tabular.\\)" attr
)
1581 (org-match-string-no-properties 1 attr
))
1582 (t org-e-latex-default-table-environment
)))
1583 ;; If table is a float, determine environment: table or table*.
1585 ((string= "longtable" table-env
) nil
)
1587 (or (string-match (regexp-quote "table*") attr
)
1588 (string-match "\\<multicolumn\\>" attr
)))
1590 ((or (not (string= caption
"")) label
) "table")))
1591 ;; Extract others display options.
1593 (string-match "\\<width=\\(\\S-+\\)" attr
)
1594 (org-match-string-no-properties 1 attr
)))
1595 (placement (if (and attr
1596 (string-match "\\<placement=\\(\\S-+\\)" attr
))
1597 (org-match-string-no-properties 1 attr
)
1599 org-e-latex-default-figure-position
1601 ;; Prepare the final format string for the table.
1604 ((string= "longtable" table-env
)
1605 (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1607 (if (or (not org-e-latex-table-caption-above
)
1608 (string= "" caption
))
1610 (concat (org-trim caption
) "\\\\"))
1611 (if (or org-e-latex-table-caption-above
1612 (string= "" caption
))
1614 (concat (org-trim caption
) "\\\\\n"))))
1616 (t (concat (when float-env
1618 (format "\\begin{%s}%s\n" float-env placement
)
1619 (if org-e-latex-table-caption-above caption
"")))
1620 (when org-e-latex-tables-centered
"\\begin{center}\n")
1621 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1623 (if width
(format "{%s}" width
) "")
1626 (when org-e-latex-tables-centered
"\n\\end{center}")
1628 (concat (if org-e-latex-table-caption-above
"" caption
)
1629 (format "\n\\end{%s}" float-env
))))))))
1631 (defun org-e-latex-table--align-string (attr info
)
1632 "Return an appropriate LaTeX alignment string.
1634 INFO is the plist containing format info about the table, as
1635 returned by `org-export-table-format-info'."
1637 (string-match "\\<align=\\(\\S-+\\)" attr
)
1638 (match-string 1 attr
))
1639 (let* ((align (copy-sequence (plist-get info
:alignment
)))
1640 (colgroups (copy-sequence (plist-get info
:column-groups
)))
1641 (cols (length align
))
1642 (separators (make-vector (1+ cols
) "")))
1643 ;; Ignore the first column if it's special.
1644 (when (plist-get info
:special-column-p
)
1645 (aset align
0 "") (aset colgroups
0 nil
))
1648 (let ((gr (aref colgroups col
)))
1649 (when (memq gr
'(start start-end
))
1650 (aset separators col
"|"))
1651 (when (memq gr
'(end start-end
))
1652 (aset separators
(1+ col
) "|")))
1655 ;; Build the LaTeX specific alignment string.
1656 (loop for al across align
1657 for sep across separators
1658 concat
(concat sep al
) into output
1659 finally return
(concat output
(aref separators cols
))))))
1661 (defun org-e-latex-table (table contents info
)
1662 "Transcode a TABLE element from Org to LaTeX.
1663 CONTENTS is nil. INFO is a plist holding contextual information."
1664 (let ((attr (mapconcat #'identity
1665 (org-element-get-property :attr_latex table
)
1667 (raw-table (org-element-get-property :raw-table table
)))
1669 ;; Case 1: verbatim table.
1670 ((or org-e-latex-tables-verbatim
1671 (and attr
(string-match "\\<verbatim\\>" attr
)))
1672 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1673 (org-export-clean-table
1675 (plist-get (org-export-table-format-info raw-table
)
1676 :special-column-p
))))
1677 ;; Case 2: table.el table. Convert it using appropriate tools.
1678 ((eq (org-element-get-property :type table
) 'table.el
)
1680 ;; Ensure "*org-export-table*" buffer is empty.
1681 (and (get-buffer "*org-export-table*")
1682 (kill-buffer (get-buffer "*org-export-table*")))
1683 (let ((output (with-temp-buffer
1686 (re-search-forward "^[ \t]*|[^|]" nil t
)
1687 (table-generate-source 'latex
"*org-export-table*")
1688 (with-current-buffer "*org-export-table*"
1689 (org-trim (buffer-string))))))
1690 (kill-buffer (get-buffer "*org-export-table*"))
1691 ;; Remove left out comments.
1692 (while (string-match "^%.*\n" output
)
1693 (setq output
(replace-match "" t t output
)))
1694 ;; When the "rmlines" attribute is provided, remove all hlines
1695 ;; but the the one separating heading from the table body.
1696 (when (and attr
(string-match "\\<rmlines\\>" attr
))
1697 (let ((n 0) (pos 0))
1698 (while (and (< (length output
) pos
)
1699 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
1701 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
1702 (if org-e-latex-tables-centered
1703 (format "\\begin{center}\n%s\n\\end{center}" output
)
1705 ;; Case 3: Standard table.
1707 (let* ((table-info (org-export-table-format-info raw-table
))
1708 (clean-table (org-export-clean-table
1709 raw-table
(plist-get table-info
:special-column-p
)))
1710 (columns-number (length (plist-get table-info
:alignment
))))
1711 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
1712 ;; particular, send each cell to
1713 ;; `org-element-parse-secondary-string' to expand any Org
1714 ;; object within. Eventually, flesh the format string out with
1716 (format (org-e-latex-table--format-string table table-info
)
1720 (if (string-match org-table-hline-regexp row
)
1724 (org-export-secondary-string
1725 (org-element-parse-secondary-string
1727 (cdr (assq 'table org-element-string-restrictions
)))
1729 (org-split-string row
"[ \t]*|[ \t]*"))))
1730 (org-split-string clean-table
"\n"))
1731 `(:tstart nil
:tend nil
1732 ;; Longtable environment requires specific
1735 (string-match "\\<longtable\\>" attr
)
1739 \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
1742 columns-number
))))))))))
1747 (defun org-e-latex-target (target text info
)
1748 "Transcode a TARGET object from Org to LaTeX.
1749 TEXT is the text of the target. INFO is a plist holding
1750 contextual information."
1751 (format "\\label{%s}%s"
1752 (org-export-solidify-link-text
1753 (org-element-get-property :raw-value target
))
1759 (defun org-e-latex-time-stamp (time-stamp contents info
)
1760 "Transcode a TIME-STAMP object from Org to LaTeX.
1761 CONTENTS is nil. INFO is a plist holding contextual information."
1762 (let ((value (org-element-get-property :value time-stamp
))
1763 (type (org-element-get-property :type time-stamp
))
1764 (appt-type (org-element-get-property :appt-type time-stamp
)))
1765 (concat (cond ((eq appt-type
'scheduled
)
1766 (format "\\textbf{\\textsc{%s}} " org-scheduled-string
))
1767 ((eq appt-type
'deadline
)
1768 (format "\\textbf{\\textsc{%s}} " org-deadline-string
))
1769 ((eq appt-type
'closed
)
1770 (format "\\textbf{\\textsc{%s}} " org-closed-string
)))
1771 (cond ((memq type
'(active active-range
))
1772 (format org-e-latex-active-timestamp-format value
))
1773 ((memq type
'(inactive inactive-range
))
1774 (format org-e-latex-inactive-timestamp-format value
))
1776 (format org-e-latex-diary-timestamp-format value
))))))
1781 (defun org-e-latex-verbatim (element contents info
)
1782 "Return verbatim text in LaTeX."
1783 (let ((fmt (cdr (assoc (org-element-get-property :marker element
)
1784 org-e-latex-emphasis-alist
)))
1785 (value (org-element-get-property :value element
)))
1787 ;; Handle the `verb' special case.
1789 (let ((separator (org-e-latex--find-verb-separator value
)))
1790 (concat "\\verb" separator value separator
)))
1791 ;; Handle the `protectedtexttt' special case.
1792 ((eq 'protectedtexttt fmt
)
1794 (trans '(("\\" .
"\\textbackslash{}")
1795 ("~" .
"\\textasciitilde{}")
1796 ("^" .
"\\textasciicircum{}")))
1799 (while (string-match "[\\{}$%&_#~^]" value
)
1800 (setq char
(match-string 0 value
))
1801 (if (> (match-beginning 0) 0)
1802 (setq rtn
(concat rtn
(substring value
0 (match-beginning 0)))))
1803 (setq value
(substring value
(1+ (match-beginning 0))))
1804 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1805 rtn
(concat rtn char
)))
1806 (setq value
(concat rtn value
)
1808 (while (string-match "--" value
)
1809 (setq value
(replace-match "-{}-" t t value
)))
1810 (format fmt value
)))
1811 ;; Else use format string.
1812 (t (format fmt value
)))))
1817 (defun org-e-latex-verse-block (verse-block contents info
)
1818 "Transcode a VERSE-BLOCK element from Org to LaTeX.
1819 CONTENTS is nil. INFO is a plist holding contextual information."
1820 (org-e-latex--wrap-label
1822 ;; In a verse environment, add a line break to each newline
1823 ;; character and change each white space at beginning of a line
1824 ;; into a space of 1 em. Also change each blank line with
1825 ;; a vertical space of 1 em.
1827 (setq contents
(replace-regexp-in-string
1828 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1829 (replace-regexp-in-string
1830 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1831 (org-remove-indentation
1832 (org-export-secondary-string
1833 (org-element-get-property :value verse-block
)
1835 (while (string-match "^[ \t]+" contents
)
1836 (let ((new-str (format "\\hspace*{%dem}"
1837 (length (match-string 0 contents
)))))
1838 (setq contents
(replace-match new-str nil t contents
))))
1839 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
1842 (provide 'org-e-latex
)
1843 ;;; org-e-latex.el ends here