]> code.delx.au - gnu-emacs/blob - lisp/org/ox-latex.el
Spelling fixes.
[gnu-emacs] / lisp / org / ox-latex.el
1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
2
3 ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
4
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24 ;;
25 ;; See Org manual for details.
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30 (require 'ox)
31 (require 'ox-publish)
32
33 (defvar org-latex-default-packages-alist)
34 (defvar org-latex-packages-alist)
35 (defvar orgtbl-exp-regexp)
36
37
38 \f
39 ;;; Define Back-End
40
41 (org-export-define-backend 'latex
42 '((bold . org-latex-bold)
43 (center-block . org-latex-center-block)
44 (clock . org-latex-clock)
45 (code . org-latex-code)
46 (comment . (lambda (&rest args) ""))
47 (comment-block . (lambda (&rest args) ""))
48 (drawer . org-latex-drawer)
49 (dynamic-block . org-latex-dynamic-block)
50 (entity . org-latex-entity)
51 (example-block . org-latex-example-block)
52 (export-block . org-latex-export-block)
53 (export-snippet . org-latex-export-snippet)
54 (fixed-width . org-latex-fixed-width)
55 (footnote-definition . org-latex-footnote-definition)
56 (footnote-reference . org-latex-footnote-reference)
57 (headline . org-latex-headline)
58 (horizontal-rule . org-latex-horizontal-rule)
59 (inline-src-block . org-latex-inline-src-block)
60 (inlinetask . org-latex-inlinetask)
61 (italic . org-latex-italic)
62 (item . org-latex-item)
63 (keyword . org-latex-keyword)
64 (latex-environment . org-latex-latex-environment)
65 (latex-fragment . org-latex-latex-fragment)
66 (line-break . org-latex-line-break)
67 (link . org-latex-link)
68 (paragraph . org-latex-paragraph)
69 (plain-list . org-latex-plain-list)
70 (plain-text . org-latex-plain-text)
71 (planning . org-latex-planning)
72 (property-drawer . (lambda (&rest args) ""))
73 (quote-block . org-latex-quote-block)
74 (quote-section . org-latex-quote-section)
75 (radio-target . org-latex-radio-target)
76 (section . org-latex-section)
77 (special-block . org-latex-special-block)
78 (src-block . org-latex-src-block)
79 (statistics-cookie . org-latex-statistics-cookie)
80 (strike-through . org-latex-strike-through)
81 (subscript . org-latex-subscript)
82 (superscript . org-latex-superscript)
83 (table . org-latex-table)
84 (table-cell . org-latex-table-cell)
85 (table-row . org-latex-table-row)
86 (target . org-latex-target)
87 (template . org-latex-template)
88 (timestamp . org-latex-timestamp)
89 (underline . org-latex-underline)
90 (verbatim . org-latex-verbatim)
91 (verse-block . org-latex-verse-block))
92 :export-block '("LATEX" "TEX")
93 :menu-entry
94 '(?l "Export to LaTeX"
95 ((?L "As LaTeX buffer" org-latex-export-as-latex)
96 (?l "As LaTeX file" org-latex-export-to-latex)
97 (?p "As PDF file" org-latex-export-to-pdf)
98 (?o "As PDF file and open"
99 (lambda (a s v b)
100 (if a (org-latex-export-to-pdf t s v b)
101 (org-open-file (org-latex-export-to-pdf nil s v b)))))))
102 :options-alist '((:latex-class "LATEX_CLASS" nil org-latex-default-class t)
103 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
104 (:latex-header "LATEX_HEADER" nil nil newline)
105 (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
106 (:latex-hyperref-p nil "texht" org-latex-with-hyperref t)
107 ;; Redefine regular options.
108 (:date "DATE" nil "\\today" t)))
109
110
111 \f
112 ;;; Internal Variables
113
114 (defconst org-latex-babel-language-alist
115 '(("af" . "afrikaans")
116 ("bg" . "bulgarian")
117 ("bt-br" . "brazilian")
118 ("ca" . "catalan")
119 ("cs" . "czech")
120 ("cy" . "welsh")
121 ("da" . "danish")
122 ("de" . "germanb")
123 ("de-at" . "naustrian")
124 ("de-de" . "ngerman")
125 ("el" . "greek")
126 ("en" . "english")
127 ("en-au" . "australian")
128 ("en-ca" . "canadian")
129 ("en-gb" . "british")
130 ("en-ie" . "irish")
131 ("en-nz" . "newzealand")
132 ("en-us" . "american")
133 ("es" . "spanish")
134 ("et" . "estonian")
135 ("eu" . "basque")
136 ("fi" . "finnish")
137 ("fr" . "frenchb")
138 ("fr-ca" . "canadien")
139 ("gl" . "galician")
140 ("hr" . "croatian")
141 ("hu" . "hungarian")
142 ("id" . "indonesian")
143 ("is" . "icelandic")
144 ("it" . "italian")
145 ("la" . "latin")
146 ("ms" . "malay")
147 ("nl" . "dutch")
148 ("nb" . "norsk")
149 ("nn" . "nynorsk")
150 ("no" . "norsk")
151 ("pl" . "polish")
152 ("pt" . "portuguese")
153 ("ro" . "romanian")
154 ("ru" . "russian")
155 ("sa" . "sanskrit")
156 ("sb" . "uppersorbian")
157 ("sk" . "slovak")
158 ("sl" . "slovene")
159 ("sq" . "albanian")
160 ("sr" . "serbian")
161 ("sv" . "swedish")
162 ("ta" . "tamil")
163 ("tr" . "turkish")
164 ("uk" . "ukrainian"))
165 "Alist between language code and corresponding Babel option.")
166
167 (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
168 ("qbordermatrix" . "\\cr")
169 ("kbordermatrix" . "\\\\"))
170 "Alist between matrix macros and their row ending.")
171
172
173 \f
174 ;;; User Configurable Variables
175
176 (defgroup org-export-latex nil
177 "Options for exporting Org mode files to LaTeX."
178 :tag "Org Export LaTeX"
179 :group 'org-export)
180
181
182 ;;;; Preamble
183
184 (defcustom org-latex-default-class "article"
185 "The default LaTeX class."
186 :group 'org-export-latex
187 :type '(string :tag "LaTeX class"))
188
189 (defcustom org-latex-classes
190 '(("article"
191 "\\documentclass[11pt]{article}"
192 ("\\section{%s}" . "\\section*{%s}")
193 ("\\subsection{%s}" . "\\subsection*{%s}")
194 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
195 ("\\paragraph{%s}" . "\\paragraph*{%s}")
196 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
197 ("report"
198 "\\documentclass[11pt]{report}"
199 ("\\part{%s}" . "\\part*{%s}")
200 ("\\chapter{%s}" . "\\chapter*{%s}")
201 ("\\section{%s}" . "\\section*{%s}")
202 ("\\subsection{%s}" . "\\subsection*{%s}")
203 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
204 ("book"
205 "\\documentclass[11pt]{book}"
206 ("\\part{%s}" . "\\part*{%s}")
207 ("\\chapter{%s}" . "\\chapter*{%s}")
208 ("\\section{%s}" . "\\section*{%s}")
209 ("\\subsection{%s}" . "\\subsection*{%s}")
210 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
211 "Alist of LaTeX classes and associated header and structure.
212 If #+LATEX_CLASS is set in the buffer, use its value and the
213 associated information. Here is the structure of each cell:
214
215 \(class-name
216 header-string
217 \(numbered-section . unnumbered-section)
218 ...)
219
220 The header string
221 -----------------
222
223 The HEADER-STRING is the header that will be inserted into the
224 LaTeX file. It should contain the \\documentclass macro, and
225 anything else that is needed for this setup. To this header, the
226 following commands will be added:
227
228 - Calls to \\usepackage for all packages mentioned in the
229 variables `org-latex-default-packages-alist' and
230 `org-latex-packages-alist'. Thus, your header definitions
231 should avoid to also request these packages.
232
233 - Lines specified via \"#+LATEX_HEADER:\" and
234 \"#+LATEX_HEADER_EXTRA:\" keywords.
235
236 If you need more control about the sequence in which the header
237 is built up, or if you want to exclude one of these building
238 blocks for a particular class, you can use the following
239 macro-like placeholders.
240
241 [DEFAULT-PACKAGES] \\usepackage statements for default packages
242 [NO-DEFAULT-PACKAGES] do not include any of the default packages
243 [PACKAGES] \\usepackage statements for packages
244 [NO-PACKAGES] do not include the packages
245 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
246 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
247
248 So a header like
249
250 \\documentclass{article}
251 [NO-DEFAULT-PACKAGES]
252 [EXTRA]
253 \\providecommand{\\alert}[1]{\\textbf{#1}}
254 [PACKAGES]
255
256 will omit the default packages, and will include the
257 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
258 to \\providecommand, and then place \\usepackage commands based
259 on the content of `org-latex-packages-alist'.
260
261 If your header, `org-latex-default-packages-alist' or
262 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
263 AUTO will automatically be replaced with a coding system derived
264 from `buffer-file-coding-system'. See also the variable
265 `org-latex-inputenc-alist' for a way to influence this mechanism.
266
267 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
268 AUTO will be replaced with the language related to the language
269 code specified by `org-export-default-language', which see. Note
270 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
271 are permitted.
272
273 The sectioning structure
274 ------------------------
275
276 The sectioning structure of the class is given by the elements
277 following the header string. For each sectioning level, a number
278 of strings is specified. A %s formatter is mandatory in each
279 section string and will be replaced by the title of the section.
280
281 Instead of a cons cell (numbered . unnumbered), you can also
282 provide a list of 2 or 4 elements,
283
284 \(numbered-open numbered-close)
285
286 or
287
288 \(numbered-open numbered-close unnumbered-open unnumbered-close)
289
290 providing opening and closing strings for a LaTeX environment
291 that should represent the document section. The opening clause
292 should have a %s to represent the section title.
293
294 Instead of a list of sectioning commands, you can also specify
295 a function name. That function will be called with two
296 parameters, the (reduced) level of the headline, and a predicate
297 non-nil when the headline should be numbered. It must return
298 a format string in which the section title will be added."
299 :group 'org-export-latex
300 :type '(repeat
301 (list (string :tag "LaTeX class")
302 (string :tag "LaTeX header")
303 (repeat :tag "Levels" :inline t
304 (choice
305 (cons :tag "Heading"
306 (string :tag " numbered")
307 (string :tag "unnumbered"))
308 (list :tag "Environment"
309 (string :tag "Opening (numbered)")
310 (string :tag "Closing (numbered)")
311 (string :tag "Opening (unnumbered)")
312 (string :tag "Closing (unnumbered)"))
313 (function :tag "Hook computing sectioning"))))))
314
315 (defcustom org-latex-inputenc-alist nil
316 "Alist of inputenc coding system names, and what should really be used.
317 For example, adding an entry
318
319 (\"utf8\" . \"utf8x\")
320
321 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
322 are written as utf8 files."
323 :group 'org-export-latex
324 :type '(repeat
325 (cons
326 (string :tag "Derived from buffer")
327 (string :tag "Use this instead"))))
328
329 (defcustom org-latex-title-command "\\maketitle"
330 "The command used to insert the title just after \\begin{document}.
331 If this string contains the formatting specification \"%s\" then
332 it will be used as a formatting string, passing the title as an
333 argument."
334 :group 'org-export-latex
335 :type 'string)
336
337 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
338 "LaTeX command to set the table of contents, list of figures, etc.
339 This command only applies to the table of contents generated with
340 the toc:nil option, not to those generated with #+TOC keyword."
341 :group 'org-export-latex
342 :type 'string)
343
344 (defcustom org-latex-with-hyperref t
345 "Toggle insertion of \\hypersetup{...} in the preamble."
346 :group 'org-export-latex
347 :type 'boolean)
348
349 ;;;; Headline
350
351 (defcustom org-latex-format-headline-function
352 'org-latex-format-headline-default-function
353 "Function for formatting the headline's text.
354
355 This function will be called with 5 arguments:
356 TODO the todo keyword (string or nil).
357 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
358 PRIORITY the priority of the headline (integer or nil)
359 TEXT the main headline text (string).
360 TAGS the tags as a list of strings (list of strings or nil).
361
362 The function result will be used in the section format string.
363
364 Use `org-latex-format-headline-default-function' by default,
365 which format headlines like for Org version prior to 8.0."
366 :group 'org-export-latex
367 :version "24.4"
368 :package-version '(Org . "8.0")
369 :type 'function)
370
371
372 ;;;; Footnotes
373
374 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
375 "Text used to separate footnotes."
376 :group 'org-export-latex
377 :type 'string)
378
379
380 ;;;; Timestamps
381
382 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
383 "A printf format string to be applied to active timestamps."
384 :group 'org-export-latex
385 :type 'string)
386
387 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
388 "A printf format string to be applied to inactive timestamps."
389 :group 'org-export-latex
390 :type 'string)
391
392 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
393 "A printf format string to be applied to diary timestamps."
394 :group 'org-export-latex
395 :type 'string)
396
397
398 ;;;; Links
399
400 (defcustom org-latex-image-default-option ""
401 "Default option for images."
402 :group 'org-export-latex
403 :version "24.4"
404 :package-version '(Org . "8.0")
405 :type 'string)
406
407 (defcustom org-latex-image-default-width ".9\\linewidth"
408 "Default width for images.
409 This value will not be used if a height is provided."
410 :group 'org-export-latex
411 :version "24.4"
412 :package-version '(Org . "8.0")
413 :type 'string)
414
415 (defcustom org-latex-image-default-height ""
416 "Default height for images.
417 This value will not be used if a width is provided, or if the
418 image is wrapped within a \"figure\" or \"wrapfigure\"
419 environment."
420 :group 'org-export-latex
421 :version "24.4"
422 :package-version '(Org . "8.0")
423 :type 'string)
424
425 (defcustom org-latex-default-figure-position "htb"
426 "Default position for latex figures."
427 :group 'org-export-latex
428 :type 'string)
429
430 (defcustom org-latex-inline-image-rules
431 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
432 "Rules characterizing image files that can be inlined into LaTeX.
433
434 A rule consists in an association whose key is the type of link
435 to consider, and value is a regexp that will be matched against
436 link's path.
437
438 Note that, by default, the image extension *actually* allowed
439 depend on the way the LaTeX file is processed. When used with
440 pdflatex, pdf, jpg and png images are OK. When processing
441 through dvi to Postscript, only ps and eps are allowed. The
442 default we use here encompasses both."
443 :group 'org-export-latex
444 :version "24.4"
445 :package-version '(Org . "8.0")
446 :type '(alist :key-type (string :tag "Type")
447 :value-type (regexp :tag "Path")))
448
449 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
450 "Format string for links with unknown path type."
451 :group 'org-export-latex
452 :type 'string)
453
454
455 ;;;; Tables
456
457 (defcustom org-latex-default-table-environment "tabular"
458 "Default environment used to build tables."
459 :group 'org-export-latex
460 :version "24.4"
461 :package-version '(Org . "8.0")
462 :type 'string)
463
464 (defcustom org-latex-default-table-mode 'table
465 "Default mode for tables.
466
467 Value can be a symbol among:
468
469 `table' Regular LaTeX table.
470
471 `math' In this mode, every cell is considered as being in math
472 mode and the complete table will be wrapped within a math
473 environment. It is particularly useful to write matrices.
474
475 `inline-math' This mode is almost the same as `math', but the
476 math environment will be inlined.
477
478 `verbatim' The table is exported as it appears in the Org
479 buffer, within a verbatim environment.
480
481 This value can be overridden locally with, i.e. \":mode math\" in
482 LaTeX attributes.
483
484 When modifying this variable, it may be useful to change
485 `org-latex-default-table-environment' accordingly."
486 :group 'org-export-latex
487 :version "24.4"
488 :package-version '(Org . "8.0")
489 :type '(choice (const :tag "Table" table)
490 (const :tag "Matrix" math)
491 (const :tag "Inline matrix" inline-math)
492 (const :tag "Verbatim" verbatim)))
493
494 (defcustom org-latex-tables-centered t
495 "When non-nil, tables are exported in a center environment."
496 :group 'org-export-latex
497 :type 'boolean)
498
499 (defcustom org-latex-tables-booktabs nil
500 "When non-nil, display tables in a formal \"booktabs\" style.
501 This option assumes that the \"booktabs\" package is properly
502 loaded in the header of the document. This value can be ignored
503 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
504 attributes."
505 :group 'org-export-latex
506 :version "24.4"
507 :package-version '(Org . "8.0")
508 :type 'boolean)
509
510 (defcustom org-latex-table-caption-above t
511 "When non-nil, place caption string at the beginning of the table.
512 Otherwise, place it near the end."
513 :group 'org-export-latex
514 :type 'boolean)
515
516 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
517 "Format string to display numbers in scientific notation.
518 The format should have \"%s\" twice, for mantissa and exponent
519 \(i.e., \"%s\\\\times10^{%s}\").
520
521 When nil, no transformation is made."
522 :group 'org-export-latex
523 :version "24.4"
524 :package-version '(Org . "8.0")
525 :type '(choice
526 (string :tag "Format string")
527 (const :tag "No formatting")))
528
529
530 ;;;; Text markup
531
532 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
533 (code . verb)
534 (italic . "\\emph{%s}")
535 (strike-through . "\\sout{%s}")
536 (underline . "\\uline{%s}")
537 (verbatim . protectedtexttt))
538 "Alist of LaTeX expressions to convert text markup.
539
540 The key must be a symbol among `bold', `code', `italic',
541 `strike-through', `underline' and `verbatim'. The value is
542 a formatting string to wrap fontified text with.
543
544 Value can also be set to the following symbols: `verb' and
545 `protectedtexttt'. For the former, Org will use \"\\verb\" to
546 create a format string and select a delimiter character that
547 isn't in the string. For the latter, Org will use \"\\texttt\"
548 to typeset and try to protect special characters.
549
550 If no association can be found for a given markup, text will be
551 returned as-is."
552 :group 'org-export-latex
553 :type 'alist
554 :options '(bold code italic strike-through underline verbatim))
555
556
557 ;;;; Drawers
558
559 (defcustom org-latex-format-drawer-function nil
560 "Function called to format a drawer in LaTeX code.
561
562 The function must accept two parameters:
563 NAME the drawer name, like \"LOGBOOK\"
564 CONTENTS the contents of the drawer.
565
566 The function should return the string to be exported.
567
568 For example, the variable could be set to the following function
569 in order to mimic default behaviour:
570
571 \(defun org-latex-format-drawer-default \(name contents\)
572 \"Format a drawer element for LaTeX export.\"
573 contents\)"
574 :group 'org-export-latex
575 :type 'function)
576
577
578 ;;;; Inlinetasks
579
580 (defcustom org-latex-format-inlinetask-function nil
581 "Function called to format an inlinetask in LaTeX code.
582
583 The function must accept six parameters:
584 TODO the todo keyword, as a string
585 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
586 PRIORITY the inlinetask priority, as a string
587 NAME the inlinetask name, as a string.
588 TAGS the inlinetask tags, as a list of strings.
589 CONTENTS the contents of the inlinetask, as a string.
590
591 The function should return the string to be exported.
592
593 For example, the variable could be set to the following function
594 in order to mimic default behaviour:
595
596 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
597 \"Format an inline task element for LaTeX export.\"
598 \(let ((full-title
599 \(concat
600 \(when todo
601 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
602 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
603 title
604 \(when tags
605 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
606 \(mapconcat 'identity tags \":\")))))
607 \(format (concat \"\\\\begin{center}\\n\"
608 \"\\\\fbox{\\n\"
609 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
610 \"%s\\n\\n\"
611 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
612 \"%s\"
613 \"\\\\end{minipage}}\"
614 \"\\\\end{center}\")
615 full-title contents))"
616 :group 'org-export-latex
617 :type 'function)
618
619
620 ;; Src blocks
621
622 (defcustom org-latex-listings nil
623 "Non-nil means export source code using the listings package.
624
625 This package will fontify source code, possibly even with color.
626 If you want to use this, you also need to make LaTeX use the
627 listings package, and if you want to have color, the color
628 package. Just add these to `org-latex-packages-alist', for
629 example using customize, or with something like:
630
631 \(require 'ox-latex)
632 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
633 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
634
635 Alternatively,
636
637 \(setq org-latex-listings 'minted)
638
639 causes source code to be exported using the minted package as
640 opposed to listings. If you want to use minted, you need to add
641 the minted package to `org-latex-packages-alist', for example
642 using customize, or with
643
644 \(require 'ox-latex)
645 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
646
647 In addition, it is necessary to install pygments
648 \(http://pygments.org), and to configure the variable
649 `org-latex-pdf-process' so that the -shell-escape option is
650 passed to pdflatex.
651
652 The minted choice has possible repercussions on the preview of
653 latex fragments (see `org-preview-latex-fragment'). If you run
654 into previewing problems, please consult
655
656 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
657 :group 'org-export-latex
658 :type '(choice
659 (const :tag "Use listings" t)
660 (const :tag "Use minted" minted)
661 (const :tag "Export verbatim" nil)))
662
663 (defcustom org-latex-listings-langs
664 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
665 (c "C") (cc "C++")
666 (fortran "fortran")
667 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
668 (html "HTML") (xml "XML")
669 (tex "TeX") (latex "[LaTeX]TeX")
670 (shell-script "bash")
671 (gnuplot "Gnuplot")
672 (ocaml "Caml") (caml "Caml")
673 (sql "SQL") (sqlite "sql"))
674 "Alist mapping languages to their listing language counterpart.
675 The key is a symbol, the major mode symbol without the \"-mode\".
676 The value is the string that should be inserted as the language
677 parameter for the listings package. If the mode name and the
678 listings name are the same, the language does not need an entry
679 in this list - but it does not hurt if it is present."
680 :group 'org-export-latex
681 :type '(repeat
682 (list
683 (symbol :tag "Major mode ")
684 (string :tag "Listings language"))))
685
686 (defcustom org-latex-listings-options nil
687 "Association list of options for the latex listings package.
688
689 These options are supplied as a comma-separated list to the
690 \\lstset command. Each element of the association list should be
691 a list containing two strings: the name of the option, and the
692 value. For example,
693
694 (setq org-latex-listings-options
695 '((\"basicstyle\" \"\\\\small\")
696 (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
697
698 will typeset the code in a small size font with underlined, bold
699 black keywords.
700
701 Note that the same options will be applied to blocks of all
702 languages."
703 :group 'org-export-latex
704 :type '(repeat
705 (list
706 (string :tag "Listings option name ")
707 (string :tag "Listings option value"))))
708
709 (defcustom org-latex-minted-langs
710 '((emacs-lisp "common-lisp")
711 (cc "c++")
712 (cperl "perl")
713 (shell-script "bash")
714 (caml "ocaml"))
715 "Alist mapping languages to their minted language counterpart.
716 The key is a symbol, the major mode symbol without the \"-mode\".
717 The value is the string that should be inserted as the language
718 parameter for the minted package. If the mode name and the
719 listings name are the same, the language does not need an entry
720 in this list - but it does not hurt if it is present.
721
722 Note that minted uses all lower case for language identifiers,
723 and that the full list of language identifiers can be obtained
724 with:
725
726 pygmentize -L lexers"
727 :group 'org-export-latex
728 :type '(repeat
729 (list
730 (symbol :tag "Major mode ")
731 (string :tag "Minted language"))))
732
733 (defcustom org-latex-minted-options nil
734 "Association list of options for the latex minted package.
735
736 These options are supplied within square brackets in
737 \\begin{minted} environments. Each element of the alist should
738 be a list containing two strings: the name of the option, and the
739 value. For example,
740
741 \(setq org-latex-minted-options
742 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
743
744 will result in src blocks being exported with
745
746 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
747
748 as the start of the minted environment. Note that the same
749 options will be applied to blocks of all languages."
750 :group 'org-export-latex
751 :type '(repeat
752 (list
753 (string :tag "Minted option name ")
754 (string :tag "Minted option value"))))
755
756 (defvar org-latex-custom-lang-environments nil
757 "Alist mapping languages to language-specific LaTeX environments.
758
759 It is used during export of src blocks by the listings and minted
760 latex packages. For example,
761
762 \(setq org-latex-custom-lang-environments
763 '\(\(python \"pythoncode\"\)\)\)
764
765 would have the effect that if org encounters begin_src python
766 during latex export it will output
767
768 \\begin{pythoncode}
769 <src block body>
770 \\end{pythoncode}")
771
772
773 ;;;; Compilation
774
775 (defcustom org-latex-pdf-process
776 '("pdflatex -interaction nonstopmode -output-directory %o %f"
777 "pdflatex -interaction nonstopmode -output-directory %o %f"
778 "pdflatex -interaction nonstopmode -output-directory %o %f")
779 "Commands to process a LaTeX file to a PDF file.
780 This is a list of strings, each of them will be given to the
781 shell as a command. %f in the command will be replaced by the
782 full file name, %b by the file base name (i.e. without directory
783 and extension parts) and %o by the base directory of the file.
784
785 The reason why this is a list is that it usually takes several
786 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
787 does not have a clever mechanism to detect which of these
788 commands have to be run to get to a stable result, and it also
789 does not do any error checking.
790
791 By default, Org uses 3 runs of `pdflatex' to do the processing.
792 If you have texi2dvi on your system and if that does not cause
793 the infamous egrep/locale bug:
794
795 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
796
797 then `texi2dvi' is the superior choice as it automates the LaTeX
798 build process by calling the \"correct\" combinations of
799 auxiliary programs. Org does offer `texi2dvi' as one of the
800 customize options. Alternatively, `rubber' and `latexmk' also
801 provide similar functionality. The latter supports `biber' out
802 of the box.
803
804 Alternatively, this may be a Lisp function that does the
805 processing, so you could use this to apply the machinery of
806 AUCTeX or the Emacs LaTeX mode. This function should accept the
807 file name as its single argument."
808 :group 'org-export-pdf
809 :type '(choice
810 (repeat :tag "Shell command sequence"
811 (string :tag "Shell command"))
812 (const :tag "2 runs of pdflatex"
813 ("pdflatex -interaction nonstopmode -output-directory %o %f"
814 "pdflatex -interaction nonstopmode -output-directory %o %f"))
815 (const :tag "3 runs of pdflatex"
816 ("pdflatex -interaction nonstopmode -output-directory %o %f"
817 "pdflatex -interaction nonstopmode -output-directory %o %f"
818 "pdflatex -interaction nonstopmode -output-directory %o %f"))
819 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
820 ("pdflatex -interaction nonstopmode -output-directory %o %f"
821 "bibtex %b"
822 "pdflatex -interaction nonstopmode -output-directory %o %f"
823 "pdflatex -interaction nonstopmode -output-directory %o %f"))
824 (const :tag "2 runs of xelatex"
825 ("xelatex -interaction nonstopmode -output-directory %o %f"
826 "xelatex -interaction nonstopmode -output-directory %o %f"))
827 (const :tag "3 runs of xelatex"
828 ("xelatex -interaction nonstopmode -output-directory %o %f"
829 "xelatex -interaction nonstopmode -output-directory %o %f"
830 "xelatex -interaction nonstopmode -output-directory %o %f"))
831 (const :tag "xelatex,bibtex,xelatex,xelatex"
832 ("xelatex -interaction nonstopmode -output-directory %o %f"
833 "bibtex %b"
834 "xelatex -interaction nonstopmode -output-directory %o %f"
835 "xelatex -interaction nonstopmode -output-directory %o %f"))
836 (const :tag "texi2dvi"
837 ("texi2dvi -p -b -V %f"))
838 (const :tag "rubber"
839 ("rubber -d --into %o %f"))
840 (const :tag "latexmk"
841 ("latexmk -g -pdf %f"))
842 (function)))
843
844 (defcustom org-latex-logfiles-extensions
845 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
846 "The list of file extensions to consider as LaTeX logfiles.
847 The logfiles will be remove if `org-latex-remove-logfiles' is
848 non-nil."
849 :group 'org-export-latex
850 :type '(repeat (string :tag "Extension")))
851
852 (defcustom org-latex-remove-logfiles t
853 "Non-nil means remove the logfiles produced by PDF production.
854 By default, logfiles are files with these extensions: .aux, .idx,
855 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
856 logfiles to remove, set `org-latex-logfiles-extensions'."
857 :group 'org-export-latex
858 :type 'boolean)
859
860 (defcustom org-latex-known-errors
861 '(("Reference.*?undefined" . "[undefined reference]")
862 ("Citation.*?undefined" . "[undefined citation]")
863 ("Undefined control sequence" . "[undefined control sequence]")
864 ("^! LaTeX.*?Error" . "[LaTeX error]")
865 ("^! Package.*?Error" . "[package error]")
866 ("Runaway argument" . "Runaway argument"))
867 "Alist of regular expressions and associated messages for the user.
868 The regular expressions are used to find possible errors in the
869 log of a latex-run."
870 :group 'org-export-latex
871 :version "24.4"
872 :package-version '(Org . "8.0")
873 :type '(repeat
874 (cons
875 (string :tag "Regexp")
876 (string :tag "Message"))))
877
878
879 \f
880 ;;; Internal Functions
881
882 (defun org-latex--caption/label-string (element info)
883 "Return caption and label LaTeX string for ELEMENT.
884
885 INFO is a plist holding contextual information. If there's no
886 caption nor label, return the empty string.
887
888 For non-floats, see `org-latex--wrap-label'."
889 (let* ((label (org-element-property :name element))
890 (label-str (if (not (org-string-nw-p label)) ""
891 (format "\\label{%s}"
892 (org-export-solidify-link-text label))))
893 (main (org-export-get-caption element))
894 (short (org-export-get-caption element t))
895 (caption-from-attr-latex (org-export-read-attribute :attr_latex element :caption)))
896 (cond
897 ((org-string-nw-p caption-from-attr-latex)
898 (concat caption-from-attr-latex "\n"))
899 ((and (not main) (equal label-str "")) "")
900 ((not main) (concat label-str "\n"))
901 ;; Option caption format with short name.
902 (short (format "\\caption[%s]{%s%s}\n"
903 (org-export-data short info)
904 label-str
905 (org-export-data main info)))
906 ;; Standard caption format.
907 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
908
909 (defun org-latex-guess-inputenc (header)
910 "Set the coding system in inputenc to what the buffer is.
911
912 HEADER is the LaTeX header string. This function only applies
913 when specified inputenc option is \"AUTO\".
914
915 Return the new header, as a string."
916 (let* ((cs (or (ignore-errors
917 (latexenc-coding-system-to-inputenc
918 (or org-export-coding-system buffer-file-coding-system)))
919 "utf8")))
920 (if (not cs) header
921 ;; First translate if that is requested.
922 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
923 ;; Then find the \usepackage statement and replace the option.
924 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
925 cs header t nil 1))))
926
927 (defun org-latex-guess-babel-language (header info)
928 "Set Babel's language according to LANGUAGE keyword.
929
930 HEADER is the LaTeX header string. INFO is the plist used as
931 a communication channel.
932
933 Insertion of guessed language only happens when Babel package has
934 explicitly been loaded. Then it is added to the rest of
935 package's options.
936
937 The argument to Babel may be \"AUTO\" which is then replaced with
938 the language of the document or `org-export-default-language'
939 unless language in question is already loaded.
940
941 Return the new header."
942 (let ((language-code (plist-get info :language)))
943 ;; If no language is set or Babel package is not loaded, return
944 ;; HEADER as-is.
945 (if (or (not (stringp language-code))
946 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
947 header
948 (let ((options (save-match-data
949 (org-split-string (match-string 1 header) ",[ \t]*")))
950 (language (cdr (assoc language-code
951 org-latex-babel-language-alist))))
952 ;; If LANGUAGE is already loaded, return header without AUTO.
953 ;; Otherwise, replace AUTO with language or append language if
954 ;; AUTO is not present.
955 (replace-match
956 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
957 (cond ((member language options) (delete "AUTO" options))
958 ((member "AUTO" options) options)
959 (t (append options (list language))))
960 ", ")
961 t nil header 1)))))
962
963 (defun org-latex--find-verb-separator (s)
964 "Return a character not used in string S.
965 This is used to choose a separator for constructs like \\verb."
966 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
967 (loop for c across ll
968 when (not (string-match (regexp-quote (char-to-string c)) s))
969 return (char-to-string c))))
970
971 (defun org-latex--make-option-string (options)
972 "Return a comma separated string of keywords and values.
973 OPTIONS is an alist where the key is the options keyword as
974 a string, and the value a list containing the keyword value, or
975 nil."
976 (mapconcat (lambda (pair)
977 (concat (first pair)
978 (when (> (length (second pair)) 0)
979 (concat "=" (second pair)))))
980 options
981 ","))
982
983 (defun org-latex--wrap-label (element output)
984 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
985 This function shouldn't be used for floats. See
986 `org-latex--caption/label-string'."
987 (let ((label (org-element-property :name element)))
988 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
989 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
990 output))))
991
992 (defun org-latex--text-markup (text markup)
993 "Format TEXT depending on MARKUP text markup.
994 See `org-latex-text-markup-alist' for details."
995 (let ((fmt (cdr (assq markup org-latex-text-markup-alist))))
996 (cond
997 ;; No format string: Return raw text.
998 ((not fmt) text)
999 ;; Handle the `verb' special case: Find and appropriate separator
1000 ;; and use "\\verb" command.
1001 ((eq 'verb fmt)
1002 (let ((separator (org-latex--find-verb-separator text)))
1003 (concat "\\verb" separator text separator)))
1004 ;; Handle the `protectedtexttt' special case: Protect some
1005 ;; special chars and use "\texttt{%s}" format string.
1006 ((eq 'protectedtexttt fmt)
1007 (let ((start 0)
1008 (trans '(("\\" . "\\textbackslash{}")
1009 ("~" . "\\textasciitilde{}")
1010 ("^" . "\\textasciicircum{}")))
1011 (rtn "")
1012 char)
1013 (while (string-match "[\\{}$%&_#~^]" text)
1014 (setq char (match-string 0 text))
1015 (if (> (match-beginning 0) 0)
1016 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1017 (setq text (substring text (1+ (match-beginning 0))))
1018 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1019 rtn (concat rtn char)))
1020 (setq text (concat rtn text)
1021 fmt "\\texttt{%s}")
1022 (while (string-match "--" text)
1023 (setq text (replace-match "-{}-" t t text)))
1024 (format fmt text)))
1025 ;; Else use format string.
1026 (t (format fmt text)))))
1027
1028 (defun org-latex--delayed-footnotes-definitions (element info)
1029 "Return footnotes definitions in ELEMENT as a string.
1030
1031 INFO is a plist used as a communication channel.
1032
1033 Footnotes definitions are returned within \"\\footnotetxt{}\"
1034 commands.
1035
1036 This function is used within constructs that don't support
1037 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1038 \"\\footnotemark\" is used within the construct and the function
1039 just outside of it."
1040 (mapconcat
1041 (lambda (ref)
1042 (format
1043 "\\footnotetext[%s]{%s}"
1044 (org-export-get-footnote-number ref info)
1045 (org-trim
1046 (org-export-data
1047 (org-export-get-footnote-definition ref info) info))))
1048 ;; Find every footnote reference in ELEMENT.
1049 (let* (all-refs
1050 search-refs ; For byte-compiler.
1051 (search-refs
1052 (function
1053 (lambda (data)
1054 ;; Return a list of all footnote references never seen
1055 ;; before in DATA.
1056 (org-element-map data 'footnote-reference
1057 (lambda (ref)
1058 (when (org-export-footnote-first-reference-p ref info)
1059 (push ref all-refs)
1060 (when (eq (org-element-property :type ref) 'standard)
1061 (funcall search-refs
1062 (org-export-get-footnote-definition ref info)))))
1063 info)
1064 (reverse all-refs)))))
1065 (funcall search-refs element))
1066 ""))
1067
1068
1069 \f
1070 ;;; Template
1071
1072 (defun org-latex-template (contents info)
1073 "Return complete document string after LaTeX conversion.
1074 CONTENTS is the transcoded contents string. INFO is a plist
1075 holding export options."
1076 (let ((title (org-export-data (plist-get info :title) info)))
1077 (concat
1078 ;; Time-stamp.
1079 (and (plist-get info :time-stamp-file)
1080 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1081 ;; Document class and packages.
1082 (let* ((class (plist-get info :latex-class))
1083 (class-options (plist-get info :latex-class-options))
1084 (header (nth 1 (assoc class org-latex-classes)))
1085 (document-class-string
1086 (and (stringp header)
1087 (if (not class-options) header
1088 (replace-regexp-in-string
1089 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1090 class-options header t nil 1)))))
1091 (if (not document-class-string)
1092 (user-error "Unknown LaTeX class `%s'" class)
1093 (org-latex-guess-babel-language
1094 (org-latex-guess-inputenc
1095 (org-element-normalize-string
1096 (org-splice-latex-header
1097 document-class-string
1098 org-latex-default-packages-alist
1099 org-latex-packages-alist nil
1100 (concat (org-element-normalize-string
1101 (plist-get info :latex-header))
1102 (plist-get info :latex-header-extra)))))
1103 info)))
1104 ;; Possibly limit depth for headline numbering.
1105 (let ((sec-num (plist-get info :section-numbers)))
1106 (when (integerp sec-num)
1107 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1108 ;; Author.
1109 (let ((author (and (plist-get info :with-author)
1110 (let ((auth (plist-get info :author)))
1111 (and auth (org-export-data auth info)))))
1112 (email (and (plist-get info :with-email)
1113 (org-export-data (plist-get info :email) info))))
1114 (cond ((and author email (not (string= "" email)))
1115 (format "\\author{%s\\thanks{%s}}\n" author email))
1116 ((or author email) (format "\\author{%s}\n" (or author email)))))
1117 ;; Date.
1118 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1119 (format "\\date{%s}\n" (org-export-data date info)))
1120 ;; Title
1121 (format "\\title{%s}\n" title)
1122 ;; Hyperref options.
1123 (when (plist-get info :latex-hyperref-p)
1124 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1125 (or (plist-get info :keywords) "")
1126 (or (plist-get info :description) "")
1127 (if (not (plist-get info :with-creator)) ""
1128 (plist-get info :creator))))
1129 ;; Document start.
1130 "\\begin{document}\n\n"
1131 ;; Title command.
1132 (org-element-normalize-string
1133 (cond ((string= "" title) nil)
1134 ((not (stringp org-latex-title-command)) nil)
1135 ((string-match "\\(?:[^%]\\|^\\)%s"
1136 org-latex-title-command)
1137 (format org-latex-title-command title))
1138 (t org-latex-title-command)))
1139 ;; Table of contents.
1140 (let ((depth (plist-get info :with-toc)))
1141 (when depth
1142 (concat (when (wholenump depth)
1143 (format "\\setcounter{tocdepth}{%d}\n" depth))
1144 org-latex-toc-command)))
1145 ;; Document's body.
1146 contents
1147 ;; Creator.
1148 (let ((creator-info (plist-get info :with-creator)))
1149 (cond
1150 ((not creator-info) "")
1151 ((eq creator-info 'comment)
1152 (format "%% %s\n" (plist-get info :creator)))
1153 (t (concat (plist-get info :creator) "\n"))))
1154 ;; Document end.
1155 "\\end{document}")))
1156
1157
1158 \f
1159 ;;; Transcode Functions
1160
1161 ;;;; Bold
1162
1163 (defun org-latex-bold (bold contents info)
1164 "Transcode BOLD from Org to LaTeX.
1165 CONTENTS is the text with bold markup. INFO is a plist holding
1166 contextual information."
1167 (org-latex--text-markup contents 'bold))
1168
1169
1170 ;;;; Center Block
1171
1172 (defun org-latex-center-block (center-block contents info)
1173 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1174 CONTENTS holds the contents of the center block. INFO is a plist
1175 holding contextual information."
1176 (org-latex--wrap-label
1177 center-block
1178 (format "\\begin{center}\n%s\\end{center}" contents)))
1179
1180
1181 ;;;; Clock
1182
1183 (defun org-latex-clock (clock contents info)
1184 "Transcode a CLOCK element from Org to LaTeX.
1185 CONTENTS is nil. INFO is a plist holding contextual
1186 information."
1187 (concat
1188 "\\noindent"
1189 (format "\\textbf{%s} " org-clock-string)
1190 (format org-latex-inactive-timestamp-format
1191 (concat (org-translate-time
1192 (org-element-property :raw-value
1193 (org-element-property :value clock)))
1194 (let ((time (org-element-property :duration clock)))
1195 (and time (format " (%s)" time)))))
1196 "\\\\"))
1197
1198
1199 ;;;; Code
1200
1201 (defun org-latex-code (code contents info)
1202 "Transcode a CODE object from Org to LaTeX.
1203 CONTENTS is nil. INFO is a plist used as a communication
1204 channel."
1205 (org-latex--text-markup (org-element-property :value code) 'code))
1206
1207
1208 ;;;; Drawer
1209
1210 (defun org-latex-drawer (drawer contents info)
1211 "Transcode a DRAWER element from Org to LaTeX.
1212 CONTENTS holds the contents of the block. INFO is a plist
1213 holding contextual information."
1214 (let* ((name (org-element-property :drawer-name drawer))
1215 (output (if (functionp org-latex-format-drawer-function)
1216 (funcall org-latex-format-drawer-function
1217 name contents)
1218 ;; If there's no user defined function: simply
1219 ;; display contents of the drawer.
1220 contents)))
1221 (org-latex--wrap-label drawer output)))
1222
1223
1224 ;;;; Dynamic Block
1225
1226 (defun org-latex-dynamic-block (dynamic-block contents info)
1227 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1228 CONTENTS holds the contents of the block. INFO is a plist
1229 holding contextual information. See `org-export-data'."
1230 (org-latex--wrap-label dynamic-block contents))
1231
1232
1233 ;;;; Entity
1234
1235 (defun org-latex-entity (entity contents info)
1236 "Transcode an ENTITY object from Org to LaTeX.
1237 CONTENTS are the definition itself. INFO is a plist holding
1238 contextual information."
1239 (let ((ent (org-element-property :latex entity)))
1240 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1241
1242
1243 ;;;; Example Block
1244
1245 (defun org-latex-example-block (example-block contents info)
1246 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1247 CONTENTS is nil. INFO is a plist holding contextual
1248 information."
1249 (when (org-string-nw-p (org-element-property :value example-block))
1250 (org-latex--wrap-label
1251 example-block
1252 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1253 (org-export-format-code-default example-block info)))))
1254
1255
1256 ;;;; Export Block
1257
1258 (defun org-latex-export-block (export-block contents info)
1259 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1260 CONTENTS is nil. INFO is a plist holding contextual information."
1261 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1262 (org-remove-indentation (org-element-property :value export-block))))
1263
1264
1265 ;;;; Export Snippet
1266
1267 (defun org-latex-export-snippet (export-snippet contents info)
1268 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1269 CONTENTS is nil. INFO is a plist holding contextual information."
1270 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1271 (org-element-property :value export-snippet)))
1272
1273
1274 ;;;; Fixed Width
1275
1276 (defun org-latex-fixed-width (fixed-width contents info)
1277 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1278 CONTENTS is nil. INFO is a plist holding contextual information."
1279 (org-latex--wrap-label
1280 fixed-width
1281 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1282 (org-remove-indentation
1283 (org-element-property :value fixed-width)))))
1284
1285
1286 ;;;; Footnote Reference
1287
1288 (defun org-latex-footnote-reference (footnote-reference contents info)
1289 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1290 CONTENTS is nil. INFO is a plist holding contextual information."
1291 (concat
1292 ;; Insert separator between two footnotes in a row.
1293 (let ((prev (org-export-get-previous-element footnote-reference info)))
1294 (when (eq (org-element-type prev) 'footnote-reference)
1295 org-latex-footnote-separator))
1296 (cond
1297 ;; Use \footnotemark if the footnote has already been defined.
1298 ((not (org-export-footnote-first-reference-p footnote-reference info))
1299 (format "\\footnotemark[%s]{}"
1300 (org-export-get-footnote-number footnote-reference info)))
1301 ;; Use \footnotemark if reference is within another footnote
1302 ;; reference, footnote definition or table cell.
1303 ((loop for parent in (org-export-get-genealogy footnote-reference)
1304 thereis (memq (org-element-type parent)
1305 '(footnote-reference footnote-definition table-cell)))
1306 "\\footnotemark")
1307 ;; Otherwise, define it with \footnote command.
1308 (t
1309 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1310 (concat
1311 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1312 ;; Retrieve all footnote references within the footnote and
1313 ;; add their definition after it, since LaTeX doesn't support
1314 ;; them inside.
1315 (org-latex--delayed-footnotes-definitions def info)))))))
1316
1317
1318 ;;;; Headline
1319
1320 (defun org-latex-headline (headline contents info)
1321 "Transcode a HEADLINE element from Org to LaTeX.
1322 CONTENTS holds the contents of the headline. INFO is a plist
1323 holding contextual information."
1324 (unless (org-element-property :footnote-section-p headline)
1325 (let* ((class (plist-get info :latex-class))
1326 (level (org-export-get-relative-level headline info))
1327 (numberedp (org-export-numbered-headline-p headline info))
1328 (class-sectioning (assoc class org-latex-classes))
1329 ;; Section formatting will set two placeholders: one for
1330 ;; the title and the other for the contents.
1331 (section-fmt
1332 (let ((sec (if (functionp (nth 2 class-sectioning))
1333 (funcall (nth 2 class-sectioning) level numberedp)
1334 (nth (1+ level) class-sectioning))))
1335 (cond
1336 ;; No section available for that LEVEL.
1337 ((not sec) nil)
1338 ;; Section format directly returned by a function. Add
1339 ;; placeholder for contents.
1340 ((stringp sec) (concat sec "\n%s"))
1341 ;; (numbered-section . unnumbered-section)
1342 ((not (consp (cdr sec)))
1343 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1344 ;; (numbered-open numbered-close)
1345 ((= (length sec) 2)
1346 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1347 ;; (num-in num-out no-num-in no-num-out)
1348 ((= (length sec) 4)
1349 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1350 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1351 (text (org-export-data (org-element-property :title headline) info))
1352 (todo
1353 (and (plist-get info :with-todo-keywords)
1354 (let ((todo (org-element-property :todo-keyword headline)))
1355 (and todo (org-export-data todo info)))))
1356 (todo-type (and todo (org-element-property :todo-type headline)))
1357 (tags (and (plist-get info :with-tags)
1358 (org-export-get-tags headline info)))
1359 (priority (and (plist-get info :with-priority)
1360 (org-element-property :priority headline)))
1361 ;; Create the headline text along with a no-tag version.
1362 ;; The latter is required to remove tags from toc.
1363 (full-text (funcall org-latex-format-headline-function
1364 todo todo-type priority text tags))
1365 ;; Associate \label to the headline for internal links.
1366 (headline-label
1367 (format "\\label{sec-%s}\n"
1368 (mapconcat 'number-to-string
1369 (org-export-get-headline-number headline info)
1370 "-")))
1371 (pre-blanks
1372 (make-string (org-element-property :pre-blank headline) 10)))
1373 (if (or (not section-fmt) (org-export-low-level-p headline info))
1374 ;; This is a deep sub-tree: export it as a list item. Also
1375 ;; export as items headlines for which no section format has
1376 ;; been found.
1377 (let ((low-level-body
1378 (concat
1379 ;; If headline is the first sibling, start a list.
1380 (when (org-export-first-sibling-p headline info)
1381 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1382 ;; Itemize headline
1383 "\\item " full-text "\n" headline-label pre-blanks contents)))
1384 ;; If headline is not the last sibling simply return
1385 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1386 ;; any blank line.
1387 (if (not (org-export-last-sibling-p headline info)) low-level-body
1388 (replace-regexp-in-string
1389 "[ \t\n]*\\'"
1390 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1391 low-level-body)))
1392 ;; This is a standard headline. Export it as a section. Add
1393 ;; an alternative heading when possible, and when this is not
1394 ;; identical to the usual heading.
1395 (let ((opt-title
1396 (funcall org-latex-format-headline-function
1397 todo todo-type priority
1398 (org-export-data
1399 (org-export-get-alt-title headline info) info)
1400 (and (eq (plist-get info :with-tags) t) tags))))
1401 (if (and numberedp opt-title
1402 (not (equal opt-title full-text))
1403 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1404 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1405 ;; Replace square brackets with parenthesis
1406 ;; since square brackets are not supported in
1407 ;; optional arguments.
1408 (replace-regexp-in-string
1409 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1410 full-text
1411 (concat headline-label pre-blanks contents))
1412 ;; Impossible to add an alternative heading. Fallback to
1413 ;; regular sectioning format string.
1414 (format section-fmt full-text
1415 (concat headline-label pre-blanks contents))))))))
1416
1417 (defun org-latex-format-headline-default-function
1418 (todo todo-type priority text tags)
1419 "Default format function for a headline.
1420 See `org-latex-format-headline-function' for details."
1421 (concat
1422 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1423 (and priority (format "\\framebox{\\#%c} " priority))
1424 text
1425 (and tags
1426 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
1427
1428
1429 ;;;; Horizontal Rule
1430
1431 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1432 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1433 CONTENTS is nil. INFO is a plist holding contextual information."
1434 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1435 (prev (org-export-get-previous-element horizontal-rule info)))
1436 (concat
1437 ;; Make sure the rule doesn't start at the end of the current
1438 ;; line by separating it with a blank line from previous element.
1439 (when (and prev
1440 (let ((prev-blank (org-element-property :post-blank prev)))
1441 (or (not prev-blank) (zerop prev-blank))))
1442 "\n")
1443 (org-latex--wrap-label
1444 horizontal-rule
1445 (format "\\rule{%s}{%s}"
1446 (or (plist-get attr :width) "\\linewidth")
1447 (or (plist-get attr :thickness) "0.5pt"))))))
1448
1449
1450 ;;;; Inline Src Block
1451
1452 (defun org-latex-inline-src-block (inline-src-block contents info)
1453 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1454 CONTENTS holds the contents of the item. INFO is a plist holding
1455 contextual information."
1456 (let* ((code (org-element-property :value inline-src-block))
1457 (separator (org-latex--find-verb-separator code)))
1458 (cond
1459 ;; Do not use a special package: transcode it verbatim.
1460 ((not org-latex-listings)
1461 (concat "\\verb" separator code separator))
1462 ;; Use minted package.
1463 ((eq org-latex-listings 'minted)
1464 (let* ((org-lang (org-element-property :language inline-src-block))
1465 (mint-lang (or (cadr (assq (intern org-lang)
1466 org-latex-minted-langs))
1467 org-lang))
1468 (options (org-latex--make-option-string
1469 org-latex-minted-options)))
1470 (concat (format "\\mint%s{%s}"
1471 (if (string= options "") "" (format "[%s]" options))
1472 mint-lang)
1473 separator code separator)))
1474 ;; Use listings package.
1475 (t
1476 ;; Maybe translate language's name.
1477 (let* ((org-lang (org-element-property :language inline-src-block))
1478 (lst-lang (or (cadr (assq (intern org-lang)
1479 org-latex-listings-langs))
1480 org-lang))
1481 (options (org-latex--make-option-string
1482 (append org-latex-listings-options
1483 `(("language" ,lst-lang))))))
1484 (concat (format "\\lstinline[%s]" options)
1485 separator code separator))))))
1486
1487
1488 ;;;; Inlinetask
1489
1490 (defun org-latex-inlinetask (inlinetask contents info)
1491 "Transcode an INLINETASK element from Org to LaTeX.
1492 CONTENTS holds the contents of the block. INFO is a plist
1493 holding contextual information."
1494 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1495 (todo (and (plist-get info :with-todo-keywords)
1496 (let ((todo (org-element-property :todo-keyword inlinetask)))
1497 (and todo (org-export-data todo info)))))
1498 (todo-type (org-element-property :todo-type inlinetask))
1499 (tags (and (plist-get info :with-tags)
1500 (org-export-get-tags inlinetask info)))
1501 (priority (and (plist-get info :with-priority)
1502 (org-element-property :priority inlinetask))))
1503 ;; If `org-latex-format-inlinetask-function' is provided, call it
1504 ;; with appropriate arguments.
1505 (if (functionp org-latex-format-inlinetask-function)
1506 (funcall org-latex-format-inlinetask-function
1507 todo todo-type priority title tags contents)
1508 ;; Otherwise, use a default template.
1509 (org-latex--wrap-label
1510 inlinetask
1511 (let ((full-title
1512 (concat
1513 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1514 (when priority (format "\\framebox{\\#%c} " priority))
1515 title
1516 (when tags (format "\\hfill{}\\textsc{:%s:}"
1517 (mapconcat 'identity tags ":"))))))
1518 (format (concat "\\begin{center}\n"
1519 "\\fbox{\n"
1520 "\\begin{minipage}[c]{.6\\textwidth}\n"
1521 "%s\n\n"
1522 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1523 "%s"
1524 "\\end{minipage}\n"
1525 "}\n"
1526 "\\end{center}")
1527 full-title contents))))))
1528
1529
1530 ;;;; Italic
1531
1532 (defun org-latex-italic (italic contents info)
1533 "Transcode ITALIC from Org to LaTeX.
1534 CONTENTS is the text with italic markup. INFO is a plist holding
1535 contextual information."
1536 (org-latex--text-markup contents 'italic))
1537
1538
1539 ;;;; Item
1540
1541 (defun org-latex-item (item contents info)
1542 "Transcode an ITEM element from Org to LaTeX.
1543 CONTENTS holds the contents of the item. INFO is a plist holding
1544 contextual information."
1545 (let* ((counter
1546 (let ((count (org-element-property :counter item))
1547 (level
1548 ;; Determine level of current item to determine the
1549 ;; correct LaTeX counter to use (enumi, enumii...).
1550 (let ((parent item) (level 0))
1551 (while (memq (org-element-type
1552 (setq parent (org-export-get-parent parent)))
1553 '(plain-list item))
1554 (when (and (eq (org-element-type parent) 'plain-list)
1555 (eq (org-element-property :type parent)
1556 'ordered))
1557 (incf level)))
1558 level)))
1559 (and count
1560 (< level 5)
1561 (format "\\setcounter{enum%s}{%s}\n"
1562 (nth (1- level) '("i" "ii" "iii" "iv"))
1563 (1- count)))))
1564 (checkbox (case (org-element-property :checkbox item)
1565 (on "$\\boxtimes$ ")
1566 (off "$\\square$ ")
1567 (trans "$\\boxminus$ ")))
1568 (tag (let ((tag (org-element-property :tag item)))
1569 ;; Check-boxes must belong to the tag.
1570 (and tag (format "[{%s}] "
1571 (concat checkbox
1572 (org-export-data tag info)))))))
1573 (concat counter "\\item" (or tag (concat " " checkbox))
1574 (and contents (org-trim contents))
1575 ;; If there are footnotes references in tag, be sure to
1576 ;; add their definition at the end of the item. This
1577 ;; workaround is necessary since "\footnote{}" command is
1578 ;; not supported in tags.
1579 (and tag
1580 (org-latex--delayed-footnotes-definitions
1581 (org-element-property :tag item) info)))))
1582
1583
1584 ;;;; Keyword
1585
1586 (defun org-latex-keyword (keyword contents info)
1587 "Transcode a KEYWORD element from Org to LaTeX.
1588 CONTENTS is nil. INFO is a plist holding contextual information."
1589 (let ((key (org-element-property :key keyword))
1590 (value (org-element-property :value keyword)))
1591 (cond
1592 ((string= key "LATEX") value)
1593 ((string= key "INDEX") (format "\\index{%s}" value))
1594 ((string= key "TOC")
1595 (let ((value (downcase value)))
1596 (cond
1597 ((string-match "\\<headlines\\>" value)
1598 (let ((depth (or (and (string-match "[0-9]+" value)
1599 (string-to-number (match-string 0 value)))
1600 (plist-get info :with-toc))))
1601 (concat
1602 (when (wholenump depth)
1603 (format "\\setcounter{tocdepth}{%s}\n" depth))
1604 "\\tableofcontents")))
1605 ((string= "tables" value) "\\listoftables")
1606 ((string= "listings" value)
1607 (cond
1608 ((eq org-latex-listings 'minted) "\\listoflistings")
1609 (org-latex-listings "\\lstlistoflistings")
1610 ;; At the moment, src blocks with a caption are wrapped
1611 ;; into a figure environment.
1612 (t "\\listoffigures")))))))))
1613
1614
1615 ;;;; Latex Environment
1616
1617 (defun org-latex-latex-environment (latex-environment contents info)
1618 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1619 CONTENTS is nil. INFO is a plist holding contextual information."
1620 (when (plist-get info :with-latex)
1621 (let ((label (org-element-property :name latex-environment))
1622 (value (org-remove-indentation
1623 (org-element-property :value latex-environment))))
1624 (if (not (org-string-nw-p label)) value
1625 ;; Environment is labeled: label must be within the environment
1626 ;; (otherwise, a reference pointing to that element will count
1627 ;; the section instead).
1628 (with-temp-buffer
1629 (insert value)
1630 (goto-char (point-min))
1631 (forward-line)
1632 (insert
1633 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1634 (buffer-string))))))
1635
1636
1637 ;;;; Latex Fragment
1638
1639 (defun org-latex-latex-fragment (latex-fragment contents info)
1640 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1641 CONTENTS is nil. INFO is a plist holding contextual information."
1642 (when (plist-get info :with-latex)
1643 (org-element-property :value latex-fragment)))
1644
1645
1646 ;;;; Line Break
1647
1648 (defun org-latex-line-break (line-break contents info)
1649 "Transcode a LINE-BREAK object from Org to LaTeX.
1650 CONTENTS is nil. INFO is a plist holding contextual information."
1651 "\\\\\n")
1652
1653
1654 ;;;; Link
1655
1656 (defun org-latex--inline-image (link info)
1657 "Return LaTeX code for an inline image.
1658 LINK is the link pointing to the inline image. INFO is a plist
1659 used as a communication channel."
1660 (let* ((parent (org-export-get-parent-element link))
1661 (path (let ((raw-path (org-element-property :path link)))
1662 (if (not (file-name-absolute-p raw-path)) raw-path
1663 (expand-file-name raw-path))))
1664 (filetype (file-name-extension path))
1665 (caption (org-latex--caption/label-string parent info))
1666 ;; Retrieve latex attributes from the element around.
1667 (attr (org-export-read-attribute :attr_latex parent))
1668 (float (let ((float (plist-get attr :float)))
1669 (cond ((and (not float) (plist-member attr :float)) nil)
1670 ((string= float "wrap") 'wrap)
1671 ((string= float "multicolumn") 'multicolumn)
1672 ((or float
1673 (org-element-property :caption parent)
1674 (org-string-nw-p (plist-get attr :caption)))
1675 'figure))))
1676 (placement
1677 (let ((place (plist-get attr :placement)))
1678 (cond (place (format "%s" place))
1679 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1680 ((eq float 'figure)
1681 (format "[%s]" org-latex-default-figure-position))
1682 (t ""))))
1683 (comment-include (if (plist-get attr :comment-include) "%" ""))
1684 ;; It is possible to specify width and height in the
1685 ;; ATTR_LATEX line, and also via default variables.
1686 (width (cond ((plist-get attr :width))
1687 ((plist-get attr :height) "")
1688 ((eq float 'wrap) "0.48\\textwidth")
1689 (t org-latex-image-default-width)))
1690 (height (cond ((plist-get attr :height))
1691 ((or (plist-get attr :width)
1692 (memq float '(figure wrap))) "")
1693 (t org-latex-image-default-height)))
1694 (options (let ((opt (or (plist-get attr :options)
1695 org-latex-image-default-option)))
1696 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1697 (match-string 1 opt))))
1698 image-code)
1699 (if (member filetype '("tikz" "pgf"))
1700 ;; For tikz images:
1701 ;; - use \input to read in image file.
1702 ;; - if options are present, wrap in a tikzpicture environment.
1703 ;; - if width or height are present, use \resizebox to change
1704 ;; the image size.
1705 (progn
1706 (setq image-code (format "\\input{%s}" path))
1707 (when (org-string-nw-p options)
1708 (setq image-code
1709 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1710 options
1711 image-code)))
1712 (when (or (org-string-nw-p width) (org-string-nw-p height))
1713 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1714 (if (org-string-nw-p width) width "!")
1715 (if (org-string-nw-p height) height "!")
1716 image-code))))
1717 ;; For other images:
1718 ;; - add width and height to options.
1719 ;; - include the image with \includegraphics.
1720 (when (org-string-nw-p width)
1721 (setq options (concat options ",width=" width)))
1722 (when (org-string-nw-p height)
1723 (setq options (concat options ",height=" height)))
1724 (setq image-code
1725 (format "\\includegraphics%s{%s}"
1726 (cond ((not (org-string-nw-p options)) "")
1727 ((= (aref options 0) ?,)
1728 (format "[%s]"(substring options 1)))
1729 (t (format "[%s]" options)))
1730 path))
1731 (when (equal filetype "svg")
1732 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
1733 "\\includesvg"
1734 image-code
1735 nil t))
1736 (setq image-code (replace-regexp-in-string "\\.svg}"
1737 "}"
1738 image-code
1739 nil t))))
1740 ;; Return proper string, depending on FLOAT.
1741 (case float
1742 (wrap (format "\\begin{wrapfigure}%s
1743 \\centering
1744 %s%s
1745 %s\\end{wrapfigure}" placement comment-include image-code caption))
1746 (multicolumn (format "\\begin{figure*}%s
1747 \\centering
1748 %s%s
1749 %s\\end{figure*}" placement comment-include image-code caption))
1750 (figure (format "\\begin{figure}%s
1751 \\centering
1752 %s%s
1753 %s\\end{figure}" placement comment-include image-code caption))
1754 (otherwise image-code))))
1755
1756 (defun org-latex-link (link desc info)
1757 "Transcode a LINK object from Org to LaTeX.
1758
1759 DESC is the description part of the link, or the empty string.
1760 INFO is a plist holding contextual information. See
1761 `org-export-data'."
1762 (let* ((type (org-element-property :type link))
1763 (raw-path (org-element-property :path link))
1764 ;; Ensure DESC really exists, or set it to nil.
1765 (desc (and (not (string= desc "")) desc))
1766 (imagep (org-export-inline-image-p
1767 link org-latex-inline-image-rules))
1768 (path (cond
1769 ((member type '("http" "https" "ftp" "mailto"))
1770 (concat type ":" raw-path))
1771 ((string= type "file")
1772 (if (not (file-name-absolute-p raw-path)) raw-path
1773 (concat "file://" (expand-file-name raw-path))))
1774 (t raw-path)))
1775 protocol)
1776 (cond
1777 ;; Image file.
1778 (imagep (org-latex--inline-image link info))
1779 ;; Radio link: Transcode target's contents and use them as link's
1780 ;; description.
1781 ((string= type "radio")
1782 (let ((destination (org-export-resolve-radio-link link info)))
1783 (when destination
1784 (format "\\hyperref[%s]{%s}"
1785 (org-export-solidify-link-text path)
1786 (org-export-data (org-element-contents destination) info)))))
1787 ;; Links pointing to a headline: Find destination and build
1788 ;; appropriate referencing command.
1789 ((member type '("custom-id" "fuzzy" "id"))
1790 (let ((destination (if (string= type "fuzzy")
1791 (org-export-resolve-fuzzy-link link info)
1792 (org-export-resolve-id-link link info))))
1793 (case (org-element-type destination)
1794 ;; Id link points to an external file.
1795 (plain-text
1796 (if desc (format "\\href{%s}{%s}" destination desc)
1797 (format "\\url{%s}" destination)))
1798 ;; Fuzzy link points nowhere.
1799 ('nil
1800 (format org-latex-link-with-unknown-path-format
1801 (or desc
1802 (org-export-data
1803 (org-element-property :raw-link link) info))))
1804 ;; LINK points to a headline. If headlines are numbered
1805 ;; and the link has no description, display headline's
1806 ;; number. Otherwise, display description or headline's
1807 ;; title.
1808 (headline
1809 (let ((label
1810 (format "sec-%s"
1811 (mapconcat
1812 'number-to-string
1813 (org-export-get-headline-number destination info)
1814 "-"))))
1815 (if (and (plist-get info :section-numbers) (not desc))
1816 (format "\\ref{%s}" label)
1817 (format "\\hyperref[%s]{%s}" label
1818 (or desc
1819 (org-export-data
1820 (org-element-property :title destination) info))))))
1821 ;; Fuzzy link points to a target. Do as above.
1822 (otherwise
1823 (let ((path (org-export-solidify-link-text path)))
1824 (if (not desc) (format "\\ref{%s}" path)
1825 (format "\\hyperref[%s]{%s}" path desc)))))))
1826 ;; Coderef: replace link with the reference name or the
1827 ;; equivalent line number.
1828 ((string= type "coderef")
1829 (format (org-export-get-coderef-format path desc)
1830 (org-export-resolve-coderef path info)))
1831 ;; Link type is handled by a special function.
1832 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1833 (funcall protocol (org-link-unescape path) desc 'latex))
1834 ;; External link with a description part.
1835 ((and path desc) (format "\\href{%s}{%s}" path desc))
1836 ;; External link without a description part.
1837 (path (format "\\url{%s}" path))
1838 ;; No path, only description. Try to do something useful.
1839 (t (format org-latex-link-with-unknown-path-format desc)))))
1840
1841
1842 ;;;; Paragraph
1843
1844 (defun org-latex-paragraph (paragraph contents info)
1845 "Transcode a PARAGRAPH element from Org to LaTeX.
1846 CONTENTS is the contents of the paragraph, as a string. INFO is
1847 the plist used as a communication channel."
1848 contents)
1849
1850
1851 ;;;; Plain List
1852
1853 (defun org-latex-plain-list (plain-list contents info)
1854 "Transcode a PLAIN-LIST element from Org to LaTeX.
1855 CONTENTS is the contents of the list. INFO is a plist holding
1856 contextual information."
1857 (let* ((type (org-element-property :type plain-list))
1858 (attr (org-export-read-attribute :attr_latex plain-list))
1859 (latex-type (let ((env (plist-get attr :environment)))
1860 (cond (env (format "%s" env))
1861 ((eq type 'ordered) "enumerate")
1862 ((eq type 'unordered) "itemize")
1863 ((eq type 'descriptive) "description")))))
1864 (org-latex--wrap-label
1865 plain-list
1866 (format "\\begin{%s}%s\n%s\\end{%s}"
1867 latex-type
1868 ;; Put optional arguments, if any inside square brackets
1869 ;; when necessary.
1870 (let ((options (format "%s" (or (plist-get attr :options) ""))))
1871 (cond ((equal options "") "")
1872 ((string-match "\\`\\[.*\\]\\'" options) options)
1873 (t (format "[%s]" options))))
1874 contents
1875 latex-type))))
1876
1877
1878 ;;;; Plain Text
1879
1880 (defun org-latex-plain-text (text info)
1881 "Transcode a TEXT string from Org to LaTeX.
1882 TEXT is the string to transcode. INFO is a plist holding
1883 contextual information."
1884 (let ((specialp (plist-get info :with-special-strings))
1885 (output text))
1886 ;; Protect %, #, &, $, _, { and }.
1887 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
1888 (setq output
1889 (replace-match
1890 (format "\\%s" (match-string 2 output)) nil t output 2)))
1891 ;; Protect ^.
1892 (setq output
1893 (replace-regexp-in-string
1894 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
1895 ;; Protect \. If special strings are used, be careful not to
1896 ;; protect "\" in "\-" constructs.
1897 (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
1898 (setq output
1899 (replace-regexp-in-string
1900 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
1901 "$\\backslash$" output nil t 1)))
1902 ;; Protect ~.
1903 (setq output
1904 (replace-regexp-in-string
1905 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
1906 ;; Activate smart quotes. Be sure to provide original TEXT string
1907 ;; since OUTPUT may have been modified.
1908 (when (plist-get info :with-smart-quotes)
1909 (setq output (org-export-activate-smart-quotes output :latex info text)))
1910 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1911 (let ((case-fold-search nil)
1912 (start 0))
1913 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
1914 (setq output (replace-match
1915 (format "\\%s{}" (match-string 1 output)) nil t output)
1916 start (match-end 0))))
1917 ;; Convert special strings.
1918 (when specialp
1919 (setq output
1920 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
1921 ;; Handle break preservation if required.
1922 (when (plist-get info :preserve-breaks)
1923 (setq output (replace-regexp-in-string
1924 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
1925 ;; Return value.
1926 output))
1927
1928
1929 ;;;; Planning
1930
1931 (defun org-latex-planning (planning contents info)
1932 "Transcode a PLANNING element from Org to LaTeX.
1933 CONTENTS is nil. INFO is a plist holding contextual
1934 information."
1935 (concat
1936 "\\noindent"
1937 (mapconcat
1938 'identity
1939 (delq nil
1940 (list
1941 (let ((closed (org-element-property :closed planning)))
1942 (when closed
1943 (concat
1944 (format "\\textbf{%s} " org-closed-string)
1945 (format org-latex-inactive-timestamp-format
1946 (org-translate-time
1947 (org-element-property :raw-value closed))))))
1948 (let ((deadline (org-element-property :deadline planning)))
1949 (when deadline
1950 (concat
1951 (format "\\textbf{%s} " org-deadline-string)
1952 (format org-latex-active-timestamp-format
1953 (org-translate-time
1954 (org-element-property :raw-value deadline))))))
1955 (let ((scheduled (org-element-property :scheduled planning)))
1956 (when scheduled
1957 (concat
1958 (format "\\textbf{%s} " org-scheduled-string)
1959 (format org-latex-active-timestamp-format
1960 (org-translate-time
1961 (org-element-property :raw-value scheduled))))))))
1962 " ")
1963 "\\\\"))
1964
1965
1966 ;;;; Quote Block
1967
1968 (defun org-latex-quote-block (quote-block contents info)
1969 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1970 CONTENTS holds the contents of the block. INFO is a plist
1971 holding contextual information."
1972 (org-latex--wrap-label
1973 quote-block
1974 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1975
1976
1977 ;;;; Quote Section
1978
1979 (defun org-latex-quote-section (quote-section contents info)
1980 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1981 CONTENTS is nil. INFO is a plist holding contextual information."
1982 (let ((value (org-remove-indentation
1983 (org-element-property :value quote-section))))
1984 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1985
1986
1987 ;;;; Radio Target
1988
1989 (defun org-latex-radio-target (radio-target text info)
1990 "Transcode a RADIO-TARGET object from Org to LaTeX.
1991 TEXT is the text of the target. INFO is a plist holding
1992 contextual information."
1993 (format "\\label{%s}%s"
1994 (org-export-solidify-link-text
1995 (org-element-property :value radio-target))
1996 text))
1997
1998
1999 ;;;; Section
2000
2001 (defun org-latex-section (section contents info)
2002 "Transcode a SECTION element from Org to LaTeX.
2003 CONTENTS holds the contents of the section. INFO is a plist
2004 holding contextual information."
2005 contents)
2006
2007
2008 ;;;; Special Block
2009
2010 (defun org-latex-special-block (special-block contents info)
2011 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2012 CONTENTS holds the contents of the block. INFO is a plist
2013 holding contextual information."
2014 (let ((type (downcase (org-element-property :type special-block)))
2015 (opt (org-export-read-attribute :attr_latex special-block :options)))
2016 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2017 ;; Insert any label or caption within the block
2018 ;; (otherwise, a reference pointing to that element will
2019 ;; count the section instead).
2020 (org-latex--caption/label-string special-block info)
2021 contents
2022 (format "\\end{%s}" type))))
2023
2024
2025 ;;;; Src Block
2026
2027 (defun org-latex-src-block (src-block contents info)
2028 "Transcode a SRC-BLOCK element from Org to LaTeX.
2029 CONTENTS holds the contents of the item. INFO is a plist holding
2030 contextual information."
2031 (when (org-string-nw-p (org-element-property :value src-block))
2032 (let* ((lang (org-element-property :language src-block))
2033 (caption (org-element-property :caption src-block))
2034 (label (org-element-property :name src-block))
2035 (custom-env (and lang
2036 (cadr (assq (intern lang)
2037 org-latex-custom-lang-environments))))
2038 (num-start (case (org-element-property :number-lines src-block)
2039 (continued (org-export-get-loc src-block info))
2040 (new 0)))
2041 (retain-labels (org-element-property :retain-labels src-block))
2042 (attributes (org-export-read-attribute :attr_latex src-block))
2043 (float (plist-get attributes :float)))
2044 (cond
2045 ;; Case 1. No source fontification.
2046 ((not org-latex-listings)
2047 (let* ((caption-str (org-latex--caption/label-string src-block info))
2048 (float-env
2049 (cond ((and (not float) (plist-member attributes :float)) "%s")
2050 ((string= "multicolumn" float)
2051 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2052 org-latex-default-figure-position
2053 caption-str))
2054 ((or caption float)
2055 (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
2056 caption-str))
2057 (t "%s"))))
2058 (format
2059 float-env
2060 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2061 (org-export-format-code-default src-block info))))))
2062 ;; Case 2. Custom environment.
2063 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2064 custom-env
2065 (org-export-format-code-default src-block info)
2066 custom-env))
2067 ;; Case 3. Use minted package.
2068 ((eq org-latex-listings 'minted)
2069 (let* ((caption-str (org-latex--caption/label-string src-block info))
2070 (float-env
2071 (cond ((and (not float) (plist-member attributes :float)) "%s")
2072 ((string= "multicolumn" float)
2073 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2074 caption-str))
2075 ((or caption float)
2076 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2077 caption-str))
2078 (t "%s")))
2079 (body
2080 (format
2081 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2082 ;; Options.
2083 (org-latex--make-option-string
2084 (if (or (not num-start)
2085 (assoc "linenos" org-latex-minted-options))
2086 org-latex-minted-options
2087 (append
2088 `(("linenos")
2089 ("firstnumber" ,(number-to-string (1+ num-start))))
2090 org-latex-minted-options)))
2091 ;; Language.
2092 (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
2093 ;; Source code.
2094 (let* ((code-info (org-export-unravel-code src-block))
2095 (max-width
2096 (apply 'max
2097 (mapcar 'length
2098 (org-split-string (car code-info)
2099 "\n")))))
2100 (org-export-format-code
2101 (car code-info)
2102 (lambda (loc num ref)
2103 (concat
2104 loc
2105 (when ref
2106 ;; Ensure references are flushed to the right,
2107 ;; separated with 6 spaces from the widest line
2108 ;; of code.
2109 (concat (make-string (+ (- max-width (length loc)) 6)
2110 ?\s)
2111 (format "(%s)" ref)))))
2112 nil (and retain-labels (cdr code-info)))))))
2113 ;; Return value.
2114 (format float-env body)))
2115 ;; Case 4. Use listings package.
2116 (t
2117 (let ((lst-lang
2118 (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
2119 (caption-str
2120 (when caption
2121 (let ((main (org-export-get-caption src-block))
2122 (secondary (org-export-get-caption src-block t)))
2123 (if (not secondary)
2124 (format "{%s}" (org-export-data main info))
2125 (format "{[%s]%s}"
2126 (org-export-data secondary info)
2127 (org-export-data main info)))))))
2128 (concat
2129 ;; Options.
2130 (format
2131 "\\lstset{%s}\n"
2132 (org-latex--make-option-string
2133 (append
2134 org-latex-listings-options
2135 (cond
2136 ((and (not float) (plist-member attributes :float)) nil)
2137 ((string= "multicolumn" float) '(("float" "*")))
2138 ((and float (not (assoc "float" org-latex-listings-options)))
2139 `(("float" ,org-latex-default-figure-position))))
2140 `(("language" ,lst-lang))
2141 (when label `(("label" ,label)))
2142 (when caption-str `(("caption" ,caption-str)))
2143 (cond ((assoc "numbers" org-latex-listings-options) nil)
2144 ((not num-start) '(("numbers" "none")))
2145 ((zerop num-start) '(("numbers" "left")))
2146 (t `(("numbers" "left")
2147 ("firstnumber"
2148 ,(number-to-string (1+ num-start)))))))))
2149 ;; Source code.
2150 (format
2151 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2152 (let* ((code-info (org-export-unravel-code src-block))
2153 (max-width
2154 (apply 'max
2155 (mapcar 'length
2156 (org-split-string (car code-info) "\n")))))
2157 (org-export-format-code
2158 (car code-info)
2159 (lambda (loc num ref)
2160 (concat
2161 loc
2162 (when ref
2163 ;; Ensure references are flushed to the right,
2164 ;; separated with 6 spaces from the widest line of
2165 ;; code
2166 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2167 (format "(%s)" ref)))))
2168 nil (and retain-labels (cdr code-info))))))))))))
2169
2170
2171 ;;;; Statistics Cookie
2172
2173 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2174 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2175 CONTENTS is nil. INFO is a plist holding contextual information."
2176 (replace-regexp-in-string
2177 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2178
2179
2180 ;;;; Strike-Through
2181
2182 (defun org-latex-strike-through (strike-through contents info)
2183 "Transcode STRIKE-THROUGH from Org to LaTeX.
2184 CONTENTS is the text with strike-through markup. INFO is a plist
2185 holding contextual information."
2186 (org-latex--text-markup contents 'strike-through))
2187
2188
2189 ;;;; Subscript
2190
2191 (defun org-latex--script-size (object info)
2192 "Transcode a subscript or superscript object.
2193 OBJECT is an Org object. INFO is a plist used as a communication
2194 channel."
2195 (let ((in-script-p
2196 ;; Non-nil if object is already in a sub/superscript.
2197 (let ((parent object))
2198 (catch 'exit
2199 (while (setq parent (org-export-get-parent parent))
2200 (let ((type (org-element-type parent)))
2201 (cond ((memq type '(subscript superscript))
2202 (throw 'exit t))
2203 ((memq type org-element-all-elements)
2204 (throw 'exit nil))))))))
2205 (type (org-element-type object))
2206 (output ""))
2207 (org-element-map (org-element-contents object)
2208 (cons 'plain-text org-element-all-objects)
2209 (lambda (obj)
2210 (case (org-element-type obj)
2211 ((entity latex-fragment)
2212 (let ((data (org-trim (org-export-data obj info))))
2213 (string-match
2214 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2215 data)
2216 (setq output
2217 (concat output
2218 (match-string 1 data)
2219 (let ((blank (org-element-property :post-blank obj)))
2220 (and blank (> blank 0) "\\ "))))))
2221 (plain-text
2222 (setq output
2223 (format "%s\\text{%s}" output (org-export-data obj info))))
2224 (otherwise
2225 (setq output
2226 (concat output
2227 (org-export-data obj info)
2228 (let ((blank (org-element-property :post-blank obj)))
2229 (and blank (> blank 0) "\\ ")))))))
2230 info nil org-element-recursive-objects)
2231 ;; Result. Do not wrap into math mode if already in a subscript
2232 ;; or superscript. Do not wrap into curly brackets if OUTPUT is
2233 ;; a single character. Also merge consecutive subscript and
2234 ;; superscript into the same math snippet.
2235 (concat (and (not in-script-p)
2236 (let ((prev (org-export-get-previous-element object info)))
2237 (or (not prev)
2238 (not (eq (org-element-type prev)
2239 (if (eq type 'subscript) 'superscript
2240 'subscript)))
2241 (let ((blank (org-element-property :post-blank prev)))
2242 (and blank (> blank 0)))))
2243 "$")
2244 (if (eq (org-element-type object) 'subscript) "_" "^")
2245 (and (> (length output) 1) "{")
2246 output
2247 (and (> (length output) 1) "}")
2248 (and (not in-script-p)
2249 (or (let ((blank (org-element-property :post-blank object)))
2250 (and blank (> blank 0)))
2251 (not (eq (org-element-type
2252 (org-export-get-next-element object info))
2253 (if (eq type 'subscript) 'superscript
2254 'subscript))))
2255 "$"))))
2256
2257 (defun org-latex-subscript (subscript contents info)
2258 "Transcode a SUBSCRIPT object from Org to LaTeX.
2259 CONTENTS is the contents of the object. INFO is a plist holding
2260 contextual information."
2261 (org-latex--script-size subscript info))
2262
2263
2264 ;;;; Superscript
2265
2266 (defun org-latex-superscript (superscript contents info)
2267 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2268 CONTENTS is the contents of the object. INFO is a plist holding
2269 contextual information."
2270 (org-latex--script-size superscript info))
2271
2272
2273 ;;;; Table
2274 ;;
2275 ;; `org-latex-table' is the entry point for table transcoding. It
2276 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2277 ;; delegates the job to either `org-latex--table.el-table',
2278 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2279 ;; depending of the type of the table and the mode requested.
2280 ;;
2281 ;; `org-latex--align-string' is a subroutine used to build alignment
2282 ;; string for Org tables.
2283
2284 (defun org-latex-table (table contents info)
2285 "Transcode a TABLE element from Org to LaTeX.
2286 CONTENTS is the contents of the table. INFO is a plist holding
2287 contextual information."
2288 (if (eq (org-element-property :type table) 'table.el)
2289 ;; "table.el" table. Convert it using appropriate tools.
2290 (org-latex--table.el-table table info)
2291 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2292 org-latex-default-table-mode)))
2293 (cond
2294 ;; Case 1: Verbatim table.
2295 ((string= type "verbatim")
2296 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2297 ;; Re-create table, without affiliated keywords.
2298 (org-trim (org-element-interpret-data
2299 `(table nil ,@(org-element-contents table))))))
2300 ;; Case 2: Matrix.
2301 ((or (string= type "math") (string= type "inline-math"))
2302 (org-latex--math-table table info))
2303 ;; Case 3: Standard table.
2304 (t (concat (org-latex--org-table table contents info)
2305 ;; When there are footnote references within the
2306 ;; table, insert their definition just after it.
2307 (org-latex--delayed-footnotes-definitions table info)))))))
2308
2309 (defun org-latex--align-string (table info)
2310 "Return an appropriate LaTeX alignment string.
2311 TABLE is the considered table. INFO is a plist used as
2312 a communication channel."
2313 (or (org-export-read-attribute :attr_latex table :align)
2314 (let (align)
2315 ;; Extract column groups and alignment from first (non-rule)
2316 ;; row.
2317 (org-element-map
2318 (org-element-map table 'table-row
2319 (lambda (row)
2320 (and (eq (org-element-property :type row) 'standard) row))
2321 info 'first-match)
2322 'table-cell
2323 (lambda (cell)
2324 (let ((borders (org-export-table-cell-borders cell info)))
2325 ;; Check left border for the first cell only.
2326 (when (and (memq 'left borders) (not align))
2327 (push "|" align))
2328 (push (case (org-export-table-cell-alignment cell info)
2329 (left "l")
2330 (right "r")
2331 (center "c"))
2332 align)
2333 (when (memq 'right borders) (push "|" align))))
2334 info)
2335 (apply 'concat (nreverse align)))))
2336
2337 (defun org-latex--org-table (table contents info)
2338 "Return appropriate LaTeX code for an Org table.
2339
2340 TABLE is the table type element to transcode. CONTENTS is its
2341 contents, as a string. INFO is a plist used as a communication
2342 channel.
2343
2344 This function assumes TABLE has `org' as its `:type' property and
2345 `table' as its `:mode' attribute."
2346 (let* ((caption (org-latex--caption/label-string table info))
2347 (attr (org-export-read-attribute :attr_latex table))
2348 ;; Determine alignment string.
2349 (alignment (org-latex--align-string table info))
2350 ;; Determine environment for the table: longtable, tabular...
2351 (table-env (or (plist-get attr :environment)
2352 org-latex-default-table-environment))
2353 ;; If table is a float, determine environment: table, table*
2354 ;; or sidewaystable.
2355 (float-env (unless (member table-env '("longtable" "longtabu"))
2356 (let ((float (plist-get attr :float)))
2357 (cond
2358 ((and (not float) (plist-member attr :float)) nil)
2359 ((string= float "sidewaystable") "sidewaystable")
2360 ((string= float "multicolumn") "table*")
2361 ((or float
2362 (org-element-property :caption table)
2363 (org-string-nw-p (plist-get attr :caption)))
2364 "table")))))
2365 ;; Extract others display options.
2366 (fontsize (let ((font (plist-get attr :font)))
2367 (and font (concat font "\n"))))
2368 (width (plist-get attr :width))
2369 (spreadp (plist-get attr :spread))
2370 (placement (or (plist-get attr :placement)
2371 (format "[%s]" org-latex-default-figure-position)))
2372 (centerp (if (plist-member attr :center) (plist-get attr :center)
2373 org-latex-tables-centered)))
2374 ;; Prepare the final format string for the table.
2375 (cond
2376 ;; Longtable.
2377 ((equal "longtable" table-env)
2378 (concat (and fontsize (concat "{" fontsize))
2379 (format "\\begin{longtable}{%s}\n" alignment)
2380 (and org-latex-table-caption-above
2381 (org-string-nw-p caption)
2382 (concat caption "\\\\\n"))
2383 contents
2384 (and (not org-latex-table-caption-above)
2385 (org-string-nw-p caption)
2386 (concat caption "\\\\\n"))
2387 "\\end{longtable}\n"
2388 (and fontsize "}")))
2389 ;; Longtabu
2390 ((equal "longtabu" table-env)
2391 (concat (and fontsize (concat "{" fontsize))
2392 (format "\\begin{longtabu}%s{%s}\n"
2393 (if width
2394 (format " %s %s "
2395 (if spreadp "spread" "to") width) "")
2396 alignment)
2397 (and org-latex-table-caption-above
2398 (org-string-nw-p caption)
2399 (concat caption "\\\\\n"))
2400 contents
2401 (and (not org-latex-table-caption-above)
2402 (org-string-nw-p caption)
2403 (concat caption "\\\\\n"))
2404 "\\end{longtabu}\n"
2405 (and fontsize "}")))
2406 ;; Others.
2407 (t (concat (cond
2408 (float-env
2409 (concat (format "\\begin{%s}%s\n" float-env placement)
2410 (if org-latex-table-caption-above caption "")
2411 (when centerp "\\centering\n")
2412 fontsize))
2413 (centerp (concat "\\begin{center}\n" fontsize))
2414 (fontsize (concat "{" fontsize)))
2415 (cond ((equal "tabu" table-env)
2416 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2417 (if width (format
2418 (if spreadp " spread %s " " to %s ")
2419 width) "")
2420 alignment
2421 contents))
2422 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2423 table-env
2424 (if width (format "{%s}" width) "")
2425 alignment
2426 contents
2427 table-env)))
2428 (cond
2429 (float-env
2430 (concat (if org-latex-table-caption-above "" caption)
2431 (format "\n\\end{%s}" float-env)))
2432 (centerp "\n\\end{center}")
2433 (fontsize "}")))))))
2434
2435 (defun org-latex--table.el-table (table info)
2436 "Return appropriate LaTeX code for a table.el table.
2437
2438 TABLE is the table type element to transcode. INFO is a plist
2439 used as a communication channel.
2440
2441 This function assumes TABLE has `table.el' as its `:type'
2442 property."
2443 (require 'table)
2444 ;; Ensure "*org-export-table*" buffer is empty.
2445 (with-current-buffer (get-buffer-create "*org-export-table*")
2446 (erase-buffer))
2447 (let ((output (with-temp-buffer
2448 (insert (org-element-property :value table))
2449 (goto-char 1)
2450 (re-search-forward "^[ \t]*|[^|]" nil t)
2451 (table-generate-source 'latex "*org-export-table*")
2452 (with-current-buffer "*org-export-table*"
2453 (org-trim (buffer-string))))))
2454 (kill-buffer (get-buffer "*org-export-table*"))
2455 ;; Remove left out comments.
2456 (while (string-match "^%.*\n" output)
2457 (setq output (replace-match "" t t output)))
2458 (let ((attr (org-export-read-attribute :attr_latex table)))
2459 (when (plist-get attr :rmlines)
2460 ;; When the "rmlines" attribute is provided, remove all hlines
2461 ;; but the the one separating heading from the table body.
2462 (let ((n 0) (pos 0))
2463 (while (and (< (length output) pos)
2464 (setq pos (string-match "^\\\\hline\n?" output pos)))
2465 (incf n)
2466 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2467 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2468 org-latex-tables-centered)))
2469 (if (not centerp) output
2470 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2471
2472 (defun org-latex--math-table (table info)
2473 "Return appropriate LaTeX code for a matrix.
2474
2475 TABLE is the table type element to transcode. INFO is a plist
2476 used as a communication channel.
2477
2478 This function assumes TABLE has `org' as its `:type' property and
2479 `inline-math' or `math' as its `:mode' attribute.."
2480 (let* ((caption (org-latex--caption/label-string table info))
2481 (attr (org-export-read-attribute :attr_latex table))
2482 (inlinep (equal (plist-get attr :mode) "inline-math"))
2483 (env (or (plist-get attr :environment)
2484 org-latex-default-table-environment))
2485 (contents
2486 (mapconcat
2487 (lambda (row)
2488 ;; Ignore horizontal rules.
2489 (when (eq (org-element-property :type row) 'standard)
2490 ;; Return each cell unmodified.
2491 (concat
2492 (mapconcat
2493 (lambda (cell)
2494 (substring (org-element-interpret-data cell) 0 -1))
2495 (org-element-map row 'table-cell 'identity info) "&")
2496 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2497 "\n")))
2498 (org-element-map table 'table-row 'identity info) ""))
2499 ;; Variables related to math clusters (contiguous math tables
2500 ;; of the same type).
2501 (mode (org-export-read-attribute :attr_latex table :mode))
2502 (prev (org-export-get-previous-element table info))
2503 (next (org-export-get-next-element table info))
2504 (same-mode-p
2505 (lambda (table)
2506 ;; Non-nil when TABLE has the same mode as current table.
2507 (string= (or (org-export-read-attribute :attr_latex table :mode)
2508 org-latex-default-table-mode)
2509 mode))))
2510 (concat
2511 ;; Opening string. If TABLE is in the middle of a table cluster,
2512 ;; do not insert any.
2513 (cond ((and prev
2514 (eq (org-element-type prev) 'table)
2515 (memq (org-element-property :post-blank prev) '(0 nil))
2516 (funcall same-mode-p prev))
2517 nil)
2518 (inlinep "\\(")
2519 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2520 (t "\\["))
2521 ;; Prefix.
2522 (or (plist-get attr :math-prefix) "")
2523 ;; Environment. Also treat special cases.
2524 (cond ((equal env "array")
2525 (let ((align (org-latex--align-string table info)))
2526 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2527 ((assoc env org-latex-table-matrix-macros)
2528 (format "\\%s%s{\n%s}"
2529 env
2530 (or (plist-get attr :math-arguments) "")
2531 contents))
2532 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2533 ;; Suffix.
2534 (or (plist-get attr :math-suffix) "")
2535 ;; Closing string. If TABLE is in the middle of a table cluster,
2536 ;; do not insert any. If it closes such a cluster, be sure to
2537 ;; close the cluster with a string matching the opening string.
2538 (cond ((and next
2539 (eq (org-element-type next) 'table)
2540 (memq (org-element-property :post-blank table) '(0 nil))
2541 (funcall same-mode-p next))
2542 nil)
2543 (inlinep "\\)")
2544 ;; Find cluster beginning to know which environment to use.
2545 ((let ((cluster-beg table) prev)
2546 (while (and (setq prev (org-export-get-previous-element
2547 cluster-beg info))
2548 (memq (org-element-property :post-blank prev)
2549 '(0 nil))
2550 (funcall same-mode-p prev))
2551 (setq cluster-beg prev))
2552 (and (or (org-element-property :caption cluster-beg)
2553 (org-element-property :name cluster-beg))
2554 "\n\\end{equation}")))
2555 (t "\\]")))))
2556
2557
2558 ;;;; Table Cell
2559
2560 (defun org-latex-table-cell (table-cell contents info)
2561 "Transcode a TABLE-CELL element from Org to LaTeX.
2562 CONTENTS is the cell contents. INFO is a plist used as
2563 a communication channel."
2564 (concat (if (and contents
2565 org-latex-table-scientific-notation
2566 (string-match orgtbl-exp-regexp contents))
2567 ;; Use appropriate format string for scientific
2568 ;; notation.
2569 (format org-latex-table-scientific-notation
2570 (match-string 1 contents)
2571 (match-string 2 contents))
2572 contents)
2573 (when (org-export-get-next-element table-cell info) " & ")))
2574
2575
2576 ;;;; Table Row
2577
2578 (defun org-latex-table-row (table-row contents info)
2579 "Transcode a TABLE-ROW element from Org to LaTeX.
2580 CONTENTS is the contents of the row. INFO is a plist used as
2581 a communication channel."
2582 ;; Rules are ignored since table separators are deduced from
2583 ;; borders of the current row.
2584 (when (eq (org-element-property :type table-row) 'standard)
2585 (let* ((attr (org-export-read-attribute :attr_latex
2586 (org-export-get-parent table-row)))
2587 (longtablep (member (or (plist-get attr :environment)
2588 org-latex-default-table-environment)
2589 '("longtable" "longtabu")))
2590 (booktabsp (if (plist-member attr :booktabs)
2591 (plist-get attr :booktabs)
2592 org-latex-tables-booktabs))
2593 ;; TABLE-ROW's borders are extracted from its first cell.
2594 (borders (org-export-table-cell-borders
2595 (car (org-element-contents table-row)) info)))
2596 (concat
2597 ;; When BOOKTABS are activated enforce top-rule even when no
2598 ;; hline was specifically marked.
2599 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2600 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2601 contents "\\\\\n"
2602 (cond
2603 ;; Special case for long tables. Define header and footers.
2604 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2605 (format "%s
2606 \\endhead
2607 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2608 \\endfoot
2609 \\endlastfoot"
2610 (if booktabsp "\\midrule" "\\hline")
2611 (if booktabsp "\\midrule" "\\hline")
2612 ;; Number of columns.
2613 (cdr (org-export-table-dimensions
2614 (org-export-get-parent-table table-row) info))))
2615 ;; When BOOKTABS are activated enforce bottom rule even when
2616 ;; no hline was specifically marked.
2617 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2618 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2619 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2620
2621
2622 ;;;; Target
2623
2624 (defun org-latex-target (target contents info)
2625 "Transcode a TARGET object from Org to LaTeX.
2626 CONTENTS is nil. INFO is a plist holding contextual
2627 information."
2628 (format "\\label{%s}"
2629 (org-export-solidify-link-text (org-element-property :value target))))
2630
2631
2632 ;;;; Timestamp
2633
2634 (defun org-latex-timestamp (timestamp contents info)
2635 "Transcode a TIMESTAMP object from Org to LaTeX.
2636 CONTENTS is nil. INFO is a plist holding contextual
2637 information."
2638 (let ((value (org-latex-plain-text
2639 (org-timestamp-translate timestamp) info)))
2640 (case (org-element-property :type timestamp)
2641 ((active active-range) (format org-latex-active-timestamp-format value))
2642 ((inactive inactive-range)
2643 (format org-latex-inactive-timestamp-format value))
2644 (otherwise (format org-latex-diary-timestamp-format value)))))
2645
2646
2647 ;;;; Underline
2648
2649 (defun org-latex-underline (underline contents info)
2650 "Transcode UNDERLINE from Org to LaTeX.
2651 CONTENTS is the text with underline markup. INFO is a plist
2652 holding contextual information."
2653 (org-latex--text-markup contents 'underline))
2654
2655
2656 ;;;; Verbatim
2657
2658 (defun org-latex-verbatim (verbatim contents info)
2659 "Transcode a VERBATIM object from Org to LaTeX.
2660 CONTENTS is nil. INFO is a plist used as a communication
2661 channel."
2662 (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2663
2664
2665 ;;;; Verse Block
2666
2667 (defun org-latex-verse-block (verse-block contents info)
2668 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2669 CONTENTS is verse block contents. INFO is a plist holding
2670 contextual information."
2671 (org-latex--wrap-label
2672 verse-block
2673 ;; In a verse environment, add a line break to each newline
2674 ;; character and change each white space at beginning of a line
2675 ;; into a space of 1 em. Also change each blank line with
2676 ;; a vertical space of 1 em.
2677 (progn
2678 (setq contents (replace-regexp-in-string
2679 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2680 (replace-regexp-in-string
2681 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2682 (while (string-match "^[ \t]+" contents)
2683 (let ((new-str (format "\\hspace*{%dem}"
2684 (length (match-string 0 contents)))))
2685 (setq contents (replace-match new-str nil t contents))))
2686 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2687
2688
2689 \f
2690 ;;; End-user functions
2691
2692 ;;;###autoload
2693 (defun org-latex-export-as-latex
2694 (&optional async subtreep visible-only body-only ext-plist)
2695 "Export current buffer as a LaTeX buffer.
2696
2697 If narrowing is active in the current buffer, only export its
2698 narrowed part.
2699
2700 If a region is active, export that region.
2701
2702 A non-nil optional argument ASYNC means the process should happen
2703 asynchronously. The resulting buffer should be accessible
2704 through the `org-export-stack' interface.
2705
2706 When optional argument SUBTREEP is non-nil, export the sub-tree
2707 at point, extracting information from the headline properties
2708 first.
2709
2710 When optional argument VISIBLE-ONLY is non-nil, don't export
2711 contents of hidden elements.
2712
2713 When optional argument BODY-ONLY is non-nil, only write code
2714 between \"\\begin{document}\" and \"\\end{document}\".
2715
2716 EXT-PLIST, when provided, is a property list with external
2717 parameters overriding Org default settings, but still inferior to
2718 file-local settings.
2719
2720 Export is done in a buffer named \"*Org LATEX Export*\", which
2721 will be displayed when `org-export-show-temporary-export-buffer'
2722 is non-nil."
2723 (interactive)
2724 (org-export-to-buffer 'latex "*Org LATEX Export*"
2725 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
2726
2727 ;;;###autoload
2728 (defun org-latex-convert-region-to-latex ()
2729 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2730 This can be used in any buffer. For example, you can write an
2731 itemized list in org-mode syntax in an LaTeX buffer and use this
2732 command to convert it."
2733 (interactive)
2734 (org-export-replace-region-by 'latex))
2735
2736 ;;;###autoload
2737 (defun org-latex-export-to-latex
2738 (&optional async subtreep visible-only body-only ext-plist)
2739 "Export current buffer to a LaTeX file.
2740
2741 If narrowing is active in the current buffer, only export its
2742 narrowed part.
2743
2744 If a region is active, export that region.
2745
2746 A non-nil optional argument ASYNC means the process should happen
2747 asynchronously. The resulting file should be accessible through
2748 the `org-export-stack' interface.
2749
2750 When optional argument SUBTREEP is non-nil, export the sub-tree
2751 at point, extracting information from the headline properties
2752 first.
2753
2754 When optional argument VISIBLE-ONLY is non-nil, don't export
2755 contents of hidden elements.
2756
2757 When optional argument BODY-ONLY is non-nil, only write code
2758 between \"\\begin{document}\" and \"\\end{document}\".
2759
2760 EXT-PLIST, when provided, is a property list with external
2761 parameters overriding Org default settings, but still inferior to
2762 file-local settings."
2763 (interactive)
2764 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2765 (org-export-to-file 'latex outfile
2766 async subtreep visible-only body-only ext-plist)))
2767
2768 ;;;###autoload
2769 (defun org-latex-export-to-pdf
2770 (&optional async subtreep visible-only body-only ext-plist)
2771 "Export current buffer to LaTeX then process through to PDF.
2772
2773 If narrowing is active in the current buffer, only export its
2774 narrowed part.
2775
2776 If a region is active, export that region.
2777
2778 A non-nil optional argument ASYNC means the process should happen
2779 asynchronously. The resulting file should be accessible through
2780 the `org-export-stack' interface.
2781
2782 When optional argument SUBTREEP is non-nil, export the sub-tree
2783 at point, extracting information from the headline properties
2784 first.
2785
2786 When optional argument VISIBLE-ONLY is non-nil, don't export
2787 contents of hidden elements.
2788
2789 When optional argument BODY-ONLY is non-nil, only write code
2790 between \"\\begin{document}\" and \"\\end{document}\".
2791
2792 EXT-PLIST, when provided, is a property list with external
2793 parameters overriding Org default settings, but still inferior to
2794 file-local settings.
2795
2796 Return PDF file's name."
2797 (interactive)
2798 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2799 (org-export-to-file 'latex outfile
2800 async subtreep visible-only body-only ext-plist
2801 (lambda (file) (org-latex-compile file)))))
2802
2803 (defun org-latex-compile (texfile &optional snippet)
2804 "Compile a TeX file.
2805
2806 TEXFILE is the name of the file being compiled. Processing is
2807 done through the command specified in `org-latex-pdf-process'.
2808
2809 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2810 file used to preview a LaTeX snippet. In this case, do not
2811 create a log buffer and do not bother removing log files.
2812
2813 Return PDF file name or an error if it couldn't be produced."
2814 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
2815 (full-name (file-truename texfile))
2816 (out-dir (file-name-directory texfile))
2817 ;; Properly set working directory for compilation.
2818 (default-directory (if (file-name-absolute-p texfile)
2819 (file-name-directory full-name)
2820 default-directory))
2821 errors)
2822 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
2823 (save-window-excursion
2824 (cond
2825 ;; A function is provided: Apply it.
2826 ((functionp org-latex-pdf-process)
2827 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
2828 ;; A list is provided: Replace %b, %f and %o with appropriate
2829 ;; values in each command before applying it. Output is
2830 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2831 ((consp org-latex-pdf-process)
2832 (let ((outbuf (and (not snippet)
2833 (get-buffer-create "*Org PDF LaTeX Output*"))))
2834 (mapc
2835 (lambda (command)
2836 (shell-command
2837 (replace-regexp-in-string
2838 "%b" (shell-quote-argument base-name)
2839 (replace-regexp-in-string
2840 "%f" (shell-quote-argument full-name)
2841 (replace-regexp-in-string
2842 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2843 outbuf))
2844 org-latex-pdf-process)
2845 ;; Collect standard errors from output buffer.
2846 (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
2847 (t (error "No valid command to process to PDF")))
2848 (let ((pdffile (concat out-dir base-name ".pdf")))
2849 ;; Check for process failure. Provide collected errors if
2850 ;; possible.
2851 (if (not (file-exists-p pdffile))
2852 (error (concat (format "PDF file %s wasn't produced" pdffile)
2853 (when errors (concat ": " errors))))
2854 ;; Else remove log files, when specified, and signal end of
2855 ;; process to user, along with any error encountered.
2856 (when (and (not snippet) org-latex-remove-logfiles)
2857 (dolist (file (directory-files
2858 out-dir t
2859 (concat (regexp-quote base-name)
2860 "\\(?:\\.[0-9]+\\)?"
2861 "\\."
2862 (regexp-opt org-latex-logfiles-extensions))))
2863 (delete-file file)))
2864 (message (concat "Process completed"
2865 (if (not errors) "."
2866 (concat " with errors: " errors)))))
2867 ;; Return output file name.
2868 pdffile))))
2869
2870 (defun org-latex--collect-errors (buffer)
2871 "Collect some kind of errors from \"pdflatex\" command output.
2872
2873 BUFFER is the buffer containing output.
2874
2875 Return collected error types as a string, or nil if there was
2876 none."
2877 (with-current-buffer buffer
2878 (save-excursion
2879 (goto-char (point-max))
2880 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
2881 (let ((case-fold-search t)
2882 (errors ""))
2883 (dolist (latex-error org-latex-known-errors)
2884 (when (save-excursion (re-search-forward (car latex-error) nil t))
2885 (setq errors (concat errors " " (cdr latex-error)))))
2886 (and (org-string-nw-p errors) (org-trim errors)))))))
2887
2888 ;;;###autoload
2889 (defun org-latex-publish-to-latex (plist filename pub-dir)
2890 "Publish an Org file to LaTeX.
2891
2892 FILENAME is the filename of the Org file to be published. PLIST
2893 is the property list for the given project. PUB-DIR is the
2894 publishing directory.
2895
2896 Return output file name."
2897 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
2898
2899 ;;;###autoload
2900 (defun org-latex-publish-to-pdf (plist filename pub-dir)
2901 "Publish an Org file to PDF (via LaTeX).
2902
2903 FILENAME is the filename of the Org file to be published. PLIST
2904 is the property list for the given project. PUB-DIR is the
2905 publishing directory.
2906
2907 Return output file name."
2908 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
2909 ;; in working directory and then moved to publishing directory.
2910 (org-publish-attachment
2911 plist
2912 (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
2913 pub-dir))
2914
2915
2916 (provide 'ox-latex)
2917
2918 ;; Local variables:
2919 ;; generated-autoload-file: "org-loaddefs.el"
2920 ;; End:
2921
2922 ;;; ox-latex.el ends here