Commit | Line | Data |
---|---|---|
c8d0cf5c | 1 | ;;; org-latex.el --- LaTeX exporter for org-mode |
72a81656 | 2 | ;; |
73b0cd50 | 3 | ;; Copyright (C) 2007-2011 Free Software Foundation, Inc. |
15841868 JW |
4 | ;; |
5 | ;; Emacs Lisp Archive Entry | |
c8d0cf5c | 6 | ;; Filename: org-latex.el |
acedf35c | 7 | ;; Version: 7.4 |
72a81656 | 8 | ;; Author: Bastien Guerry <bzg AT altern DOT org> |
c8d0cf5c | 9 | ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com> |
15841868 JW |
10 | ;; Keywords: org, wp, tex |
11 | ;; Description: Converts an org-mode buffer into LaTeX | |
b1fc2b50 | 12 | |
72a81656 | 13 | ;; This file is part of GNU Emacs. |
b1fc2b50 GM |
14 | |
15 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
16 | ;; it under the terms of the GNU General Public License as published by | |
17 | ;; the Free Software Foundation, either version 3 of the License, or | |
18 | ;; (at your option) any later version. | |
19 | ||
20 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
21 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | ;; GNU General Public License for more details. | |
24 | ||
db9c3fb1 | 25 | ;; You should have received a copy of the GNU General Public License |
b1fc2b50 GM |
26 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
27 | ||
72a81656 | 28 | ;;; Commentary: |
0b8568f5 JW |
29 | ;; |
30 | ;; This library implements a LaTeX exporter for org-mode. | |
db9c3fb1 | 31 | ;; |
c8d0cf5c | 32 | ;; It is part of Org and will be autoloaded |
db9c3fb1 | 33 | ;; |
72a81656 | 34 | ;; The interactive functions are similar to those of the HTML exporter: |
db9c3fb1 | 35 | ;; |
72a81656 | 36 | ;; M-x `org-export-as-latex' |
71d35b24 CD |
37 | ;; M-x `org-export-as-pdf' |
38 | ;; M-x `org-export-as-pdf-and-open' | |
72a81656 CD |
39 | ;; M-x `org-export-as-latex-batch' |
40 | ;; M-x `org-export-as-latex-to-buffer' | |
41 | ;; M-x `org-export-region-as-latex' | |
42 | ;; M-x `org-replace-region-by-latex' | |
4b67ea89 | 43 | ;; |
72a81656 CD |
44 | ;;; Code: |
45 | ||
0b8568f5 JW |
46 | (eval-when-compile |
47 | (require 'cl)) | |
48 | ||
72a81656 | 49 | (require 'footnote) |
0b8568f5 | 50 | (require 'org) |
20908596 | 51 | (require 'org-exp) |
8d642074 | 52 | (require 'org-macs) |
ed21c5c8 | 53 | (require 'org-beamer) |
72a81656 | 54 | |
0b8568f5 | 55 | ;;; Variables: |
db9c3fb1 | 56 | (defvar org-export-latex-class nil) |
ed21c5c8 | 57 | (defvar org-export-latex-class-options nil) |
db9c3fb1 CD |
58 | (defvar org-export-latex-header nil) |
59 | (defvar org-export-latex-append-header nil) | |
60 | (defvar org-export-latex-options-plist nil) | |
61 | (defvar org-export-latex-todo-keywords-1 nil) | |
c8d0cf5c CD |
62 | (defvar org-export-latex-complex-heading-re nil) |
63 | (defvar org-export-latex-not-done-keywords nil) | |
64 | (defvar org-export-latex-done-keywords nil) | |
65 | (defvar org-export-latex-display-custom-times nil) | |
db9c3fb1 CD |
66 | (defvar org-export-latex-all-targets-re nil) |
67 | (defvar org-export-latex-add-level 0) | |
68 | (defvar org-export-latex-sectioning "") | |
69 | (defvar org-export-latex-sectioning-depth 0) | |
0bd48b37 CD |
70 | (defvar org-export-latex-special-keyword-regexp |
71 | (concat "\\<\\(" org-scheduled-string "\\|" | |
72 | org-deadline-string "\\|" | |
73 | org-closed-string"\\)") | |
74 | "Regexp matching special time planning keywords plus the time after it.") | |
72a81656 | 75 | |
0b8568f5 JW |
76 | (defvar latexp) ; dynamically scoped from org.el |
77 | (defvar re-quote) ; dynamically scoped from org.el | |
78 | (defvar commentsp) ; dynamically scoped from org.el | |
79 | ||
db9c3fb1 | 80 | ;;; User variables: |
72a81656 | 81 | |
621f83e4 CD |
82 | (defgroup org-export-latex nil |
83 | "Options for exporting Org-mode files to LaTeX." | |
84 | :tag "Org Export LaTeX" | |
85 | :group 'org-export) | |
86 | ||
db9c3fb1 CD |
87 | (defcustom org-export-latex-default-class "article" |
88 | "The default LaTeX class." | |
89 | :group 'org-export-latex | |
90 | :type '(string :tag "LaTeX class")) | |
72a81656 | 91 | |
db9c3fb1 CD |
92 | (defcustom org-export-latex-classes |
93 | '(("article" | |
ed21c5c8 | 94 | "\\documentclass[11pt]{article}" |
db9c3fb1 CD |
95 | ("\\section{%s}" . "\\section*{%s}") |
96 | ("\\subsection{%s}" . "\\subsection*{%s}") | |
97 | ("\\subsubsection{%s}" . "\\subsubsection*{%s}") | |
98 | ("\\paragraph{%s}" . "\\paragraph*{%s}") | |
99 | ("\\subparagraph{%s}" . "\\subparagraph*{%s}")) | |
100 | ("report" | |
ed21c5c8 | 101 | "\\documentclass[11pt]{report}" |
db9c3fb1 CD |
102 | ("\\part{%s}" . "\\part*{%s}") |
103 | ("\\chapter{%s}" . "\\chapter*{%s}") | |
104 | ("\\section{%s}" . "\\section*{%s}") | |
105 | ("\\subsection{%s}" . "\\subsection*{%s}") | |
106 | ("\\subsubsection{%s}" . "\\subsubsection*{%s}")) | |
107 | ("book" | |
ed21c5c8 | 108 | "\\documentclass[11pt]{book}" |
db9c3fb1 CD |
109 | ("\\part{%s}" . "\\part*{%s}") |
110 | ("\\chapter{%s}" . "\\chapter*{%s}") | |
111 | ("\\section{%s}" . "\\section*{%s}") | |
112 | ("\\subsection{%s}" . "\\subsection*{%s}") | |
ed21c5c8 CD |
113 | ("\\subsubsection{%s}" . "\\subsubsection*{%s}")) |
114 | ("beamer" | |
115 | "\\documentclass{beamer}" | |
116 | org-beamer-sectioning | |
117 | )) | |
db9c3fb1 CD |
118 | "Alist of LaTeX classes and associated header and structure. |
119 | If #+LaTeX_CLASS is set in the buffer, use its value and the | |
120 | associated information. Here is the structure of each cell: | |
121 | ||
122 | \(class-name | |
123 | header-string | |
71d35b24 | 124 | (numbered-section . unnumbered-section\) |
db9c3fb1 CD |
125 | ...\) |
126 | ||
ed21c5c8 CD |
127 | The header string |
128 | ----------------- | |
129 | ||
130 | The HEADER-STRING is the header that will be inserted into the LaTeX file. | |
131 | It should contain the \\documentclass macro, and anything else that is needed | |
132 | for this setup. To this header, the following commands will be added: | |
133 | ||
134 | - Calls to \\usepackage for all packages mentioned in the variables | |
135 | `org-export-latex-default-packages-alist' and | |
136 | `org-export-latex-packages-alist'. Thus, your header definitions should | |
137 | avoid to also request these packages. | |
138 | ||
139 | - Lines specified via \"#+LaTeX_HEADER:\" | |
140 | ||
141 | If you need more control about the sequence in which the header is built | |
142 | up, or if you want to exclude one of these building blocks for a particular | |
143 | class, you can use the following macro-like placeholders. | |
144 | ||
145 | [DEFAULT-PACKAGES] \\usepackage statements for default packages | |
146 | [NO-DEFAULT-PACKAGES] do not include any of the default packages | |
86fbb8ca | 147 | [PACKAGES] \\usepackage statements for packages |
ed21c5c8 CD |
148 | [NO-PACKAGES] do not include the packages |
149 | [EXTRA] the stuff from #+LaTeX_HEADER | |
150 | [NO-EXTRA] do not include #+LaTeX_HEADER stuff | |
86fbb8ca | 151 | [BEAMER-HEADER-EXTRA] the beamer extra headers |
ed21c5c8 CD |
152 | |
153 | So a header like | |
154 | ||
155 | \\documentclass{article} | |
156 | [NO-DEFAULT-PACKAGES] | |
157 | [EXTRA] | |
158 | \\providecommand{\\alert}[1]{\\textbf{#1}} | |
159 | [PACKAGES] | |
160 | ||
161 | will omit the default packages, and will include the #+LaTeX_HEADER lines, | |
162 | then have a call to \\providecommand, and then place \\usepackage commands | |
163 | based on the content of `org-export-latex-packages-alist'. | |
164 | ||
165 | If your header or `org-export-latex-default-packages-alist' inserts | |
166 | \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with | |
167 | a coding system derived from `buffer-file-coding-system'. See also the | |
168 | variable `org-export-latex-inputenc-alist' for a way to influence this | |
169 | mechanism. | |
170 | ||
171 | The sectioning structure | |
172 | ------------------------ | |
173 | ||
174 | The sectioning structure of the class is given by the elements following | |
175 | the header string. For each sectioning level, a number of strings is | |
176 | specified. A %s formatter is mandatory in each section string and will | |
177 | be replaced by the title of the section. | |
71d35b24 CD |
178 | |
179 | Instead of a cons cell (numbered . unnumbered), you can also provide a list | |
ed21c5c8 | 180 | of 2 or 4 elements, |
71d35b24 CD |
181 | |
182 | (numbered-open numbered-close) | |
183 | ||
184 | or | |
185 | ||
186 | (numbered-open numbered-close unnumbered-open unnumbered-close) | |
187 | ||
ed21c5c8 | 188 | providing opening and closing strings for a LaTeX environment that should |
71d35b24 | 189 | represent the document section. The opening clause should have a %s |
ed21c5c8 CD |
190 | to represent the section title. |
191 | ||
192 | Instead of a list of sectioning commands, you can also specify a | |
193 | function name. That function will be called with two parameters, | |
194 | the (reduced) level of the headline, and the headline text. The function | |
195 | must return a cons cell with the (possibly modified) headline text, and the | |
196 | sectioning list in the cdr." | |
72a81656 | 197 | :group 'org-export-latex |
ff4be292 | 198 | :type '(repeat |
db9c3fb1 CD |
199 | (list (string :tag "LaTeX class") |
200 | (string :tag "LaTeX header") | |
71d35b24 CD |
201 | (repeat :tag "Levels" :inline t |
202 | (choice | |
203 | (cons :tag "Heading" | |
ed21c5c8 CD |
204 | (string :tag " numbered") |
205 | (string :tag "unnumbered")) | |
71d35b24 | 206 | (list :tag "Environment" |
ed21c5c8 CD |
207 | (string :tag "Opening (numbered)") |
208 | (string :tag "Closing (numbered)") | |
71d35b24 | 209 | (string :tag "Opening (unnumbered)") |
ed21c5c8 CD |
210 | (string :tag "Closing (unnumbered)")) |
211 | (function :tag "Hook computing sectioning")))))) | |
212 | ||
213 | (defcustom org-export-latex-inputenc-alist nil | |
214 | "Alist of inputenc coding system names, and what should really be used. | |
215 | For example, adding an entry | |
216 | ||
217 | (\"utf8\" . \"utf8x\") | |
218 | ||
219 | will cause \\usepackage[utf8x]{inputenc} to be used for buffers that | |
220 | are written as utf8 files." | |
221 | :group 'org-export-latex | |
222 | :type '(repeat | |
223 | (cons | |
224 | (string :tag "Derived from buffer") | |
225 | (string :tag "Use this instead")))) | |
226 | ||
72a81656 CD |
227 | |
228 | (defcustom org-export-latex-emphasis-alist | |
15841868 JW |
229 | '(("*" "\\textbf{%s}" nil) |
230 | ("/" "\\emph{%s}" nil) | |
231 | ("_" "\\underline{%s}" nil) | |
54a0dee5 | 232 | ("+" "\\st{%s}" nil) |
c8d0cf5c CD |
233 | ("=" "\\verb" t) |
234 | ("~" "\\verb" t)) | |
15841868 JW |
235 | "Alist of LaTeX expressions to convert emphasis fontifiers. |
236 | Each element of the list is a list of three elements. | |
237 | The first element is the character used as a marker for fontification. | |
238 | The second element is a formatting string to wrap fontified text with. | |
8bfe682a | 239 | If it is \"\\verb\", Org will automatically select a delimiter |
c8d0cf5c | 240 | character that is not in the string. |
15841868 JW |
241 | The third element decides whether to protect converted text from other |
242 | conversions." | |
72a81656 CD |
243 | :group 'org-export-latex |
244 | :type 'alist) | |
245 | ||
15841868 JW |
246 | (defcustom org-export-latex-title-command "\\maketitle" |
247 | "The command used to insert the title just after \\begin{document}. | |
248 | If this string contains the formatting specification \"%s\" then | |
249 | it will be used as a formatting string, passing the title as an | |
250 | argument." | |
251 | :group 'org-export-latex | |
252 | :type 'string) | |
253 | ||
c8d0cf5c CD |
254 | (defcustom org-export-latex-import-inbuffer-stuff nil |
255 | "Non-nil means define TeX macros for Org's inbuffer definitions. | |
256 | For example \orgTITLE for #+TITLE." | |
257 | :group 'org-export-latex | |
258 | :type 'boolean) | |
259 | ||
db9c3fb1 | 260 | (defcustom org-export-latex-date-format |
d5098885 | 261 | "%d %B %Y" |
72a81656 CD |
262 | "Format string for \\date{...}." |
263 | :group 'org-export-latex | |
264 | :type 'string) | |
265 | ||
c8d0cf5c CD |
266 | (defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}" |
267 | "Markup for TODO keywords, as a printf format. | |
268 | This can be a single format for all keywords, a cons cell with separate | |
269 | formats for not-done and done states, or an association list with setup | |
270 | for individual keywords. If a keyword shows up for which there is no | |
271 | markup defined, the first one in the association list will be used." | |
272 | :group 'org-export-latex | |
273 | :type '(choice | |
274 | (string :tag "Default") | |
275 | (cons :tag "Distinguish undone and done" | |
276 | (string :tag "Not-DONE states") | |
277 | (string :tag "DONE states")) | |
278 | (repeat :tag "Per keyword markup" | |
279 | (cons | |
280 | (string :tag "Keyword") | |
281 | (string :tag "Markup"))))) | |
282 | ||
afe98dfa CD |
283 | (defcustom org-export-latex-tag-markup "\\textbf{%s}" |
284 | "Markup for tags, as a printf format." | |
285 | :group 'org-export-latex | |
286 | :type 'string) | |
287 | ||
c8d0cf5c CD |
288 | (defcustom org-export-latex-timestamp-markup "\\textit{%s}" |
289 | "A printf format string to be applied to time stamps." | |
290 | :group 'org-export-latex | |
291 | :type 'string) | |
292 | ||
293 | (defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}" | |
294 | "A printf format string to be applied to time stamps." | |
295 | :group 'org-export-latex | |
296 | :type 'string) | |
297 | ||
acedf35c CD |
298 | (defcustom org-export-latex-href-format "\\href{%s}{%s}" |
299 | "A printf format string to be applied to href links. | |
300 | The format must contain two %s instances. The first will be filled with | |
301 | the link, the second with the link description." | |
302 | :group 'org-export-latex | |
303 | :type 'string) | |
304 | ||
305 | (defcustom org-export-latex-hyperref-format "\\hyperref[%s]{%s}" | |
ed21c5c8 CD |
306 | "A printf format string to be applied to hyperref links. |
307 | The format must contain two %s instances. The first will be filled with | |
308 | the link, the second with the link description." | |
309 | :group 'org-export-latex | |
310 | :type 'string) | |
311 | ||
d5098885 | 312 | (defcustom org-export-latex-tables-verbatim nil |
621f83e4 | 313 | "When non-nil, tables are exported verbatim." |
d5098885 JW |
314 | :group 'org-export-latex |
315 | :type 'boolean) | |
316 | ||
c8d0cf5c CD |
317 | (defcustom org-export-latex-tables-centered t |
318 | "When non-nil, tables are exported in a center environment." | |
319 | :group 'org-export-latex | |
320 | :type 'boolean) | |
321 | ||
db9c3fb1 | 322 | (defcustom org-export-latex-tables-column-borders nil |
c8d0cf5c CD |
323 | "When non-nil, grouping columns can cause outer vertical lines in tables. |
324 | When nil, grouping causes only separation lines between groups." | |
db9c3fb1 CD |
325 | :group 'org-export-latex |
326 | :type 'boolean) | |
72a81656 | 327 | |
c8d0cf5c | 328 | (defcustom org-export-latex-low-levels 'itemize |
621f83e4 CD |
329 | "How to convert sections below the current level of sectioning. |
330 | This is specified by the `org-export-headline-levels' option or the | |
331 | value of \"H:\" in Org's #+OPTION line. | |
15841868 | 332 | |
c8d0cf5c CD |
333 | This can be either nil (skip the sections), `description', `itemize', |
334 | or `enumerate' (convert the sections as the corresponding list type), or | |
335 | a string to be used instead of \\section{%s}. In this latter case, | |
336 | the %s stands here for the inserted headline and is mandatory. | |
337 | ||
338 | It may also be a list of three string to define a user-defined environment | |
339 | that should be used. The first string should be the like | |
340 | \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up | |
8bfe682a | 341 | to two occurrences of %s for the title and a label, respectively. The third |
c8d0cf5c | 342 | string should be like \"\\end{itemize\"." |
72a81656 CD |
343 | :group 'org-export-latex |
344 | :type '(choice (const :tag "Ignore" nil) | |
c8d0cf5c CD |
345 | (const :tag "Convert as descriptive list" description) |
346 | (const :tag "Convert as itemized list" itemize) | |
347 | (const :tag "Convert as enumerated list" enumerate) | |
348 | (list :tag "User-defined environment" | |
349 | :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s") | |
350 | (string :tag "Start") | |
351 | (string :tag "End") | |
352 | (string :tag "item")) | |
72a81656 CD |
353 | (string :tag "Use a section string" :value "\\subparagraph{%s}"))) |
354 | ||
0bd48b37 | 355 | (defcustom org-export-latex-list-parameters |
8bfe682a | 356 | '(:cbon "$\\boxtimes$" :cboff "$\\Box$") |
0bd48b37 CD |
357 | "Parameters for the LaTeX list exporter. |
358 | These parameters will be passed on to `org-list-to-latex', which in turn | |
359 | will pass them (combined with the LaTeX default list parameters) to | |
360 | `org-list-to-generic'." | |
361 | :group 'org-export-latex | |
362 | :type 'plist) | |
363 | ||
c8d0cf5c CD |
364 | (defcustom org-export-latex-verbatim-wrap |
365 | '("\\begin{verbatim}\n" . "\\end{verbatim}\n") | |
366 | "Environment to be wrapped around a fixed-width section in LaTeX export. | |
367 | This is a cons with two strings, to be added before and after the | |
368 | fixed-with text. | |
369 | ||
370 | Defaults to \\begin{verbatim} and \\end{verbatim}." | |
371 | :group 'org-export-translation | |
372 | :group 'org-export-latex | |
373 | :type '(cons (string :tag "Open") | |
374 | (string :tag "Close"))) | |
375 | ||
54a0dee5 | 376 | (defcustom org-export-latex-listings nil |
ed21c5c8 | 377 | "Non-nil means export source code using the listings package. |
54a0dee5 CD |
378 | This package will fontify source code, possibly even with color. |
379 | If you want to use this, you also need to make LaTeX use the | |
380 | listings package, and if you want to have color, the color | |
381 | package. Just add these to `org-export-latex-packages-alist', | |
382 | for example using customize, or with something like | |
383 | ||
384 | (require 'org-latex) | |
385 | (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\")) | |
afe98dfa CD |
386 | (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\")) |
387 | ||
388 | Alternatively, | |
389 | ||
390 | (setq org-export-latex-listings 'minted) | |
391 | ||
392 | causes source code to be exported using the minted package as | |
393 | opposed to listings. If you want to use minted, you need to add | |
394 | the minted package to `org-export-latex-packages-alist', for | |
395 | example using customize, or with | |
396 | ||
397 | (require 'org-latex) | |
398 | (add-to-list 'org-export-latex-packages-alist '(\"\" \"minted\")) | |
399 | ||
400 | In addition, it is neccessary to install | |
401 | pygments (http://pygments.org), and to configure | |
402 | `org-latex-to-pdf-process' so that the -shell-escape option is | |
403 | passed to pdflatex. | |
404 | " | |
54a0dee5 CD |
405 | :group 'org-export-latex |
406 | :type 'boolean) | |
407 | ||
408 | (defcustom org-export-latex-listings-langs | |
afe98dfa | 409 | '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp") |
54a0dee5 CD |
410 | (c "C") (cc "C++") |
411 | (fortran "fortran") | |
412 | (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby") | |
413 | (html "HTML") (xml "XML") | |
414 | (tex "TeX") (latex "TeX") | |
415 | (shell-script "bash") | |
416 | (gnuplot "Gnuplot") | |
417 | (ocaml "Caml") (caml "Caml") | |
86fbb8ca | 418 | (sql "SQL") (sqlite "sql")) |
54a0dee5 CD |
419 | "Alist mapping languages to their listing language counterpart. |
420 | The key is a symbol, the major mode symbol without the \"-mode\". | |
421 | The value is the string that should be inserted as the language parameter | |
422 | for the listings package. If the mode name and the listings name are | |
423 | the same, the language does not need an entry in this list - but it does not | |
424 | hurt if it is present." | |
425 | :group 'org-export-latex | |
426 | :type '(repeat | |
427 | (list | |
428 | (symbol :tag "Major mode ") | |
429 | (string :tag "Listings language")))) | |
430 | ||
afe98dfa CD |
431 | (defcustom org-export-latex-listings-w-names t |
432 | "Non-nil means export names of named code blocks. | |
433 | Code blocks exported with the listings package (controlled by the | |
434 | `org-export-latex-listings' variable) can be named in the style | |
435 | of noweb." | |
436 | :group 'org-export-latex | |
437 | :type 'boolean) | |
438 | ||
439 | (defcustom org-export-latex-minted-langs | |
440 | '((emacs-lisp "common-lisp") | |
441 | (cc "c++") | |
442 | (cperl "perl") | |
443 | (shell-script "bash") | |
444 | (caml "ocaml")) | |
445 | "Alist mapping languages to their minted language counterpart. | |
446 | The key is a symbol, the major mode symbol without the \"-mode\". | |
447 | The value is the string that should be inserted as the language parameter | |
448 | for the minted package. If the mode name and the listings name are | |
449 | the same, the language does not need an entry in this list - but it does not | |
450 | hurt if it is present. | |
451 | ||
452 | Note that minted uses all lower case for language identifiers, | |
453 | and that the full list of language identifiers can be obtained | |
454 | with: | |
455 | pygmentize -L lexers | |
456 | " | |
457 | :group 'org-export-latex | |
458 | :type '(repeat | |
459 | (list | |
460 | (symbol :tag "Major mode ") | |
461 | (string :tag "Listings language")))) | |
462 | ||
d5098885 | 463 | (defcustom org-export-latex-remove-from-headlines |
0bd48b37 | 464 | '(:todo nil :priority nil :tags nil) |
86fbb8ca | 465 | "A plist of keywords to remove from headlines. OBSOLETE. |
72a81656 CD |
466 | Non-nil means remove this keyword type from the headline. |
467 | ||
0bd48b37 CD |
468 | Don't remove the keys, just change their values. |
469 | ||
470 | Obsolete, this variable is no longer used. Use the separate | |
471 | variables `org-export-with-todo-keywords', `org-export-with-priority', | |
472 | and `org-export-with-tags' instead." | |
72a81656 CD |
473 | :type 'plist |
474 | :group 'org-export-latex) | |
475 | ||
72a81656 CD |
476 | (defcustom org-export-latex-image-default-option "width=10em" |
477 | "Default option for images." | |
478 | :group 'org-export-latex | |
db9c3fb1 | 479 | :type 'string) |
72a81656 | 480 | |
86fbb8ca CD |
481 | (defcustom org-export-latex-tabular-environment "tabular" |
482 | "Default environment used to build tables." | |
483 | :group 'org-export-latex | |
484 | :type 'string) | |
485 | ||
0bd48b37 | 486 | (defcustom org-export-latex-inline-image-extensions |
c8d0cf5c | 487 | '("pdf" "jpeg" "jpg" "png" "ps" "eps") |
0bd48b37 | 488 | "Extensions of image files that can be inlined into LaTeX. |
c8d0cf5c CD |
489 | Note that the image extension *actually* allowed depend on the way the |
490 | LaTeX file is processed. When used with pdflatex, pdf, jpg and png images | |
491 | are OK. When processing through dvi to Postscript, only ps and eps are | |
492 | allowed. The default we use here encompasses both." | |
493 | :group 'org-export-latex | |
0bd48b37 CD |
494 | :type '(repeat (string :tag "Extension"))) |
495 | ||
72a81656 | 496 | (defcustom org-export-latex-coding-system nil |
86fbb8ca | 497 | "Coding system for the exported LaTeX file." |
72a81656 CD |
498 | :group 'org-export-latex |
499 | :type 'coding-system) | |
500 | ||
71d35b24 CD |
501 | (defgroup org-export-pdf nil |
502 | "Options for exporting Org-mode files to PDF, via LaTeX." | |
ed21c5c8 | 503 | :tag "Org Export PDF" |
71d35b24 CD |
504 | :group 'org-export-latex |
505 | :group 'org-export) | |
506 | ||
c8d0cf5c | 507 | (defcustom org-latex-to-pdf-process |
afe98dfa CD |
508 | '("pdflatex -interaction nonstopmode -output-directory %o %f" |
509 | "pdflatex -interaction nonstopmode -output-directory %o %f" | |
510 | "pdflatex -interaction nonstopmode -output-directory %o %f") | |
c8d0cf5c CD |
511 | "Commands to process a LaTeX file to a PDF file. |
512 | This is a list of strings, each of them will be given to the shell | |
afe98dfa CD |
513 | as a command. %f in the command will be replaced by the full file name, %b |
514 | by the file base name (i.e. without extension) and %o by the base directory | |
515 | of the file. | |
516 | ||
c8d0cf5c | 517 | The reason why this is a list is that it usually takes several runs of |
afe98dfa | 518 | `pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever |
8bfe682a | 519 | mechanism to detect which of these commands have to be run to get to a stable |
c8d0cf5c CD |
520 | result, and it also does not do any error checking. |
521 | ||
afe98dfa CD |
522 | By default, Org uses 3 runs of `pdflatex' to do the processing. If you |
523 | have texi2dvi on your system and if that does not cause the infamous | |
524 | egrep/locale bug: | |
525 | ||
526 | http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html | |
527 | ||
528 | then `texi2dvi' is the superior choice. Org does offer it as one | |
529 | of the customize options. | |
530 | ||
c8d0cf5c CD |
531 | Alternatively, this may be a Lisp function that does the processing, so you |
532 | could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode. | |
8bfe682a | 533 | This function should accept the file name as its single argument." |
ed21c5c8 | 534 | :group 'org-export-pdf |
afe98dfa CD |
535 | :type '(choice |
536 | (repeat :tag "Shell command sequence" | |
c8d0cf5c | 537 | (string :tag "Shell command")) |
afe98dfa CD |
538 | (const :tag "2 runs of pdflatex" |
539 | ("pdflatex -interaction nonstopmode -output-directory %o %f" | |
540 | "pdflatex -interaction nonstopmode -output-directory %o %f")) | |
541 | (const :tag "3 runs of pdflatex" | |
542 | ("pdflatex -interaction nonstopmode -output-directory %o %f" | |
543 | "pdflatex -interaction nonstopmode -output-directory %o %f" | |
544 | "pdflatex -interaction nonstopmode -output-directory %o %f")) | |
545 | (const :tag "pdflatex,bibtex,pdflatex,pdflatex" | |
546 | ("pdflatex -interaction nonstopmode -output-directory %o %f" | |
547 | "bibtex %b" | |
548 | "pdflatex -interaction nonstopmode -output-directory %o %f" | |
549 | "pdflatex -interaction nonstopmode -output-directory %o %f")) | |
550 | (const :tag "texi2dvi" | |
551 | ("texi2dvi -p -b -c -V %f")) | |
552 | (const :tag "rubber" | |
553 | ("rubber -d --into %o %f")) | |
554 | (function))) | |
c8d0cf5c | 555 | |
ed21c5c8 CD |
556 | (defcustom org-export-pdf-logfiles |
557 | '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb") | |
558 | "The list of file extensions to consider as LaTeX logfiles." | |
559 | :group 'org-export-pdf | |
560 | :type '(repeat (string :tag "Extension"))) | |
561 | ||
71d35b24 | 562 | (defcustom org-export-pdf-remove-logfiles t |
ed21c5c8 | 563 | "Non-nil means remove the logfiles produced by PDF production. |
71d35b24 | 564 | These are the .aux, .log, .out, and .toc files." |
c8d0cf5c | 565 | :group 'org-export-pdf |
71d35b24 CD |
566 | :type 'boolean) |
567 | ||
c8d0cf5c CD |
568 | ;;; Hooks |
569 | ||
ed21c5c8 CD |
570 | (defvar org-export-latex-after-initial-vars-hook nil |
571 | "Hook run before LaTeX export. | |
572 | The exact moment is after the initial variables like org-export-latex-class | |
573 | have been determined from the environment.") | |
574 | ||
c8d0cf5c CD |
575 | (defvar org-export-latex-after-blockquotes-hook nil |
576 | "Hook run during LaTeX export, after blockquote, verse, center are done.") | |
577 | ||
8d642074 CD |
578 | (defvar org-export-latex-final-hook nil |
579 | "Hook run in the finalized LaTeX buffer.") | |
580 | ||
ed21c5c8 CD |
581 | (defvar org-export-latex-after-save-hook nil |
582 | "Hook run in the finalized LaTeX buffer, after it has been saved.") | |
583 | ||
0b8568f5 | 584 | ;;; Autoload functions: |
db9c3fb1 | 585 | |
72a81656 CD |
586 | ;;;###autoload |
587 | (defun org-export-as-latex-batch () | |
621f83e4 CD |
588 | "Call `org-export-as-latex', may be used in batch processing. |
589 | For example: | |
590 | ||
33306645 CD |
591 | emacs --batch |
592 | --load=$HOME/lib/emacs/org.el | |
593 | --eval \"(setq org-export-headline-levels 2)\" | |
594 | --visit=MyFile --funcall org-export-as-latex-batch" | |
72a81656 CD |
595 | (org-export-as-latex org-export-headline-levels 'hidden)) |
596 | ||
597 | ;;;###autoload | |
598 | (defun org-export-as-latex-to-buffer (arg) | |
33306645 | 599 | "Call `org-export-as-latex` with output to a temporary buffer. |
72a81656 CD |
600 | No file is created. The prefix ARG is passed through to `org-export-as-latex'." |
601 | (interactive "P") | |
602 | (org-export-as-latex arg nil nil "*Org LaTeX Export*") | |
c8d0cf5c CD |
603 | (when org-export-show-temporary-export-buffer |
604 | (switch-to-buffer-other-window "*Org LaTeX Export*"))) | |
72a81656 CD |
605 | |
606 | ;;;###autoload | |
607 | (defun org-replace-region-by-latex (beg end) | |
608 | "Replace the region from BEG to END with its LaTeX export. | |
609 | It assumes the region has `org-mode' syntax, and then convert it to | |
db9c3fb1 | 610 | LaTeX. This can be used in any buffer. For example, you could |
72a81656 CD |
611 | write an itemized list in `org-mode' syntax in an LaTeX buffer and |
612 | then use this command to convert it." | |
613 | (interactive "r") | |
614 | (let (reg latex buf) | |
615 | (save-window-excursion | |
616 | (if (org-mode-p) | |
617 | (setq latex (org-export-region-as-latex | |
618 | beg end t 'string)) | |
619 | (setq reg (buffer-substring beg end) | |
620 | buf (get-buffer-create "*Org tmp*")) | |
81ad75af | 621 | (with-current-buffer buf |
72a81656 CD |
622 | (erase-buffer) |
623 | (insert reg) | |
624 | (org-mode) | |
625 | (setq latex (org-export-region-as-latex | |
626 | (point-min) (point-max) t 'string))) | |
627 | (kill-buffer buf))) | |
628 | (delete-region beg end) | |
629 | (insert latex))) | |
630 | ||
631 | ;;;###autoload | |
632 | (defun org-export-region-as-latex (beg end &optional body-only buffer) | |
633 | "Convert region from BEG to END in `org-mode' buffer to LaTeX. | |
634 | If prefix arg BODY-ONLY is set, omit file header, footer, and table of | |
635 | contents, and only produce the region of converted text, useful for | |
636 | cut-and-paste operations. | |
637 | If BUFFER is a buffer or a string, use/create that buffer as a target | |
638 | of the converted LaTeX. If BUFFER is the symbol `string', return the | |
c8d0cf5c | 639 | produced LaTeX as a string and leave no buffer behind. For example, |
72a81656 CD |
640 | a Lisp program could call this function in the following way: |
641 | ||
642 | (setq latex (org-export-region-as-latex beg end t 'string)) | |
643 | ||
644 | When called interactively, the output buffer is selected, and shown | |
c8d0cf5c | 645 | in a window. A non-interactive call will only return the buffer." |
72a81656 CD |
646 | (interactive "r\nP") |
647 | (when (interactive-p) | |
648 | (setq buffer "*Org LaTeX Export*")) | |
649 | (let ((transient-mark-mode t) (zmacs-regions t) | |
c8d0cf5c | 650 | ext-plist rtn) |
8bfe682a | 651 | (setq ext-plist (plist-put ext-plist :ignore-subtree-p t)) |
72a81656 CD |
652 | (goto-char end) |
653 | (set-mark (point)) ;; to activate the region | |
654 | (goto-char beg) | |
655 | (setq rtn (org-export-as-latex | |
c8d0cf5c | 656 | nil nil ext-plist |
72a81656 CD |
657 | buffer body-only)) |
658 | (if (fboundp 'deactivate-mark) (deactivate-mark)) | |
659 | (if (and (interactive-p) (bufferp rtn)) | |
660 | (switch-to-buffer-other-window rtn) | |
661 | rtn))) | |
662 | ||
663 | ;;;###autoload | |
664 | (defun org-export-as-latex (arg &optional hidden ext-plist | |
4b67ea89 | 665 | to-buffer body-only pub-dir) |
db9c3fb1 CD |
666 | "Export current buffer to a LaTeX file. |
667 | If there is an active region, export only the region. The prefix | |
668 | ARG specifies how many levels of the outline should become | |
669 | headlines. The default is 3. Lower levels will be exported | |
670 | depending on `org-export-latex-low-levels'. The default is to | |
c8d0cf5c CD |
671 | convert them as description lists. |
672 | HIDDEN is obsolete and does nothing. | |
673 | EXT-PLIST is a property list with | |
db9c3fb1 CD |
674 | external parameters overriding org-mode's default settings, but |
675 | still inferior to file-local settings. When TO-BUFFER is | |
676 | non-nil, create a buffer with that name and export to that | |
677 | buffer. If TO-BUFFER is the symbol `string', don't leave any | |
678 | buffer behind but just return the resulting LaTeX as a string. | |
679 | When BODY-ONLY is set, don't produce the file header and footer, | |
86fbb8ca CD |
680 | simply return the content of \\begin{document}...\\end{document}, |
681 | without even the \\begin{document} and \\end{document} commands. | |
4b67ea89 | 682 | when PUB-DIR is set, use this as the publishing directory." |
72a81656 | 683 | (interactive "P") |
86fbb8ca | 684 | (when (and (not body-only) arg (listp arg)) (setq body-only t)) |
ed21c5c8 CD |
685 | (run-hooks 'org-export-first-hook) |
686 | ||
72a81656 CD |
687 | ;; Make sure we have a file name when we need it. |
688 | (when (and (not (or to-buffer body-only)) | |
689 | (not buffer-file-name)) | |
690 | (if (buffer-base-buffer) | |
691 | (org-set-local 'buffer-file-name | |
692 | (with-current-buffer (buffer-base-buffer) | |
693 | buffer-file-name)) | |
694 | (error "Need a file name to be able to export"))) | |
695 | ||
696 | (message "Exporting to LaTeX...") | |
c8d0cf5c | 697 | (org-unmodified |
ed21c5c8 CD |
698 | (let ((inhibit-read-only t)) |
699 | (remove-text-properties (point-min) (point-max) | |
700 | '(:org-license-to-kill nil)))) | |
72a81656 | 701 | (org-update-radio-target-regexp) |
db9c3fb1 | 702 | (org-export-latex-set-initial-vars ext-plist arg) |
ed21c5c8 CD |
703 | (setq org-export-opt-plist org-export-latex-options-plist) |
704 | (org-install-letbind) | |
705 | (run-hooks 'org-export-latex-after-initial-vars-hook) | |
15841868 | 706 | (let* ((wcf (current-window-configuration)) |
db9c3fb1 CD |
707 | (opt-plist org-export-latex-options-plist) |
708 | (region-p (org-region-active-p)) | |
b349f79f CD |
709 | (rbeg (and region-p (region-beginning))) |
710 | (rend (and region-p (region-end))) | |
db9c3fb1 | 711 | (subtree-p |
8bfe682a | 712 | (if (plist-get opt-plist :ignore-subtree-p) |
c8d0cf5c CD |
713 | nil |
714 | (when region-p | |
715 | (save-excursion | |
716 | (goto-char rbeg) | |
717 | (and (org-at-heading-p) | |
718 | (>= (org-end-of-subtree t t) rend)))))) | |
719 | (opt-plist (setq org-export-opt-plist | |
720 | (if subtree-p | |
721 | (org-export-add-subtree-options opt-plist rbeg) | |
722 | opt-plist))) | |
b349f79f | 723 | ;; Make sure the variable contains the updated values. |
ed21c5c8 CD |
724 | (org-export-latex-options-plist (setq org-export-opt-plist opt-plist)) |
725 | ;; The following two are dynamically scoped into other | |
726 | ;; routines below. | |
727 | (org-current-export-dir | |
728 | (or pub-dir (org-export-directory :html opt-plist))) | |
729 | (org-current-export-file buffer-file-name) | |
db9c3fb1 CD |
730 | (title (or (and subtree-p (org-export-get-title-from-subtree)) |
731 | (plist-get opt-plist :title) | |
732 | (and (not | |
733 | (plist-get opt-plist :skip-before-1st-heading)) | |
734 | (org-export-grab-title-from-buffer)) | |
ed21c5c8 CD |
735 | (and buffer-file-name |
736 | (file-name-sans-extension | |
737 | (file-name-nondirectory buffer-file-name))) | |
738 | "No Title")) | |
739 | (filename | |
740 | (and (not to-buffer) | |
741 | (concat | |
742 | (file-name-as-directory | |
743 | (or pub-dir | |
744 | (org-export-directory :LaTeX ext-plist))) | |
745 | (file-name-sans-extension | |
746 | (or (and subtree-p | |
747 | (org-entry-get rbeg "EXPORT_FILE_NAME" t)) | |
748 | (file-name-nondirectory ;sans-extension | |
749 | (or buffer-file-name | |
86fbb8ca | 750 | (error "Don't know which export file to use"))))) |
ed21c5c8 CD |
751 | ".tex"))) |
752 | (filename | |
753 | (and filename | |
754 | (if (equal (file-truename filename) | |
755 | (file-truename (or buffer-file-name "dummy.org"))) | |
756 | (concat filename ".tex") | |
757 | filename))) | |
72a81656 CD |
758 | (buffer (if to-buffer |
759 | (cond | |
760 | ((eq to-buffer 'string) (get-buffer-create | |
761 | "*Org LaTeX Export*")) | |
762 | (t (get-buffer-create to-buffer))) | |
763 | (find-file-noselect filename))) | |
72a81656 | 764 | (odd org-odd-levels-only) |
db9c3fb1 | 765 | (header (org-export-latex-make-header title opt-plist)) |
4b67ea89 | 766 | (skip (cond (subtree-p nil) |
db55f368 | 767 | (region-p nil) |
4b67ea89 | 768 | (t (plist-get opt-plist :skip-before-1st-heading)))) |
72a81656 | 769 | (text (plist-get opt-plist :text)) |
c8d0cf5c CD |
770 | (org-export-preprocess-hook |
771 | (cons | |
772 | `(lambda () (org-set-local 'org-complex-heading-regexp | |
773 | ,org-export-latex-complex-heading-re)) | |
774 | org-export-preprocess-hook)) | |
65c439fd | 775 | (first-lines (if skip "" (org-export-latex-first-lines |
c8d0cf5c CD |
776 | opt-plist |
777 | (if subtree-p | |
778 | (save-excursion | |
779 | (goto-char rbeg) | |
780 | (point-at-bol 2)) | |
781 | rbeg) | |
782 | (if region-p rend)))) | |
72a81656 CD |
783 | (coding-system (and (boundp 'buffer-file-coding-system) |
784 | buffer-file-coding-system)) | |
785 | (coding-system-for-write (or org-export-latex-coding-system | |
786 | coding-system)) | |
787 | (save-buffer-coding-system (or org-export-latex-coding-system | |
788 | coding-system)) | |
33306645 | 789 | (region (buffer-substring |
72a81656 CD |
790 | (if region-p (region-beginning) (point-min)) |
791 | (if region-p (region-end) (point-max)))) | |
ed21c5c8 CD |
792 | (text |
793 | (and text (string-match "\\S-" text) | |
794 | (org-export-preprocess-string | |
795 | text | |
796 | :emph-multiline t | |
797 | :for-LaTeX t | |
798 | :comments nil | |
799 | :tags (plist-get opt-plist :tags) | |
800 | :priority (plist-get opt-plist :priority) | |
801 | :footnotes (plist-get opt-plist :footnotes) | |
802 | :drawers (plist-get opt-plist :drawers) | |
803 | :timestamps (plist-get opt-plist :timestamps) | |
804 | :todo-keywords (plist-get opt-plist :todo-keywords) | |
805 | :add-text nil | |
806 | :skip-before-1st-heading skip | |
807 | :select-tags nil | |
808 | :exclude-tags nil | |
809 | :LaTeX-fragments nil))) | |
72a81656 | 810 | (string-for-export |
20908596 | 811 | (org-export-preprocess-string |
db55f368 CD |
812 | region |
813 | :emph-multiline t | |
814 | :for-LaTeX t | |
815 | :comments nil | |
816 | :tags (plist-get opt-plist :tags) | |
817 | :priority (plist-get opt-plist :priority) | |
0bd48b37 | 818 | :footnotes (plist-get opt-plist :footnotes) |
1bcdebed | 819 | :drawers (plist-get opt-plist :drawers) |
0bd48b37 | 820 | :timestamps (plist-get opt-plist :timestamps) |
db55f368 CD |
821 | :todo-keywords (plist-get opt-plist :todo-keywords) |
822 | :add-text (if (eq to-buffer 'string) nil text) | |
823 | :skip-before-1st-heading skip | |
824 | :select-tags (plist-get opt-plist :select-tags) | |
825 | :exclude-tags (plist-get opt-plist :exclude-tags) | |
826 | :LaTeX-fragments nil))) | |
15841868 | 827 | |
db9c3fb1 | 828 | (set-buffer buffer) |
72a81656 | 829 | (erase-buffer) |
c8d0cf5c | 830 | (org-install-letbind) |
0b8568f5 | 831 | |
15841868 JW |
832 | (and (fboundp 'set-buffer-file-coding-system) |
833 | (set-buffer-file-coding-system coding-system-for-write)) | |
834 | ||
db9c3fb1 | 835 | ;; insert the header and initial document commands |
15841868 | 836 | (unless (or (eq to-buffer 'string) body-only) |
db9c3fb1 | 837 | (insert header)) |
15841868 JW |
838 | |
839 | ;; insert text found in #+TEXT | |
840 | (when (and text (not (eq to-buffer 'string))) | |
db9c3fb1 CD |
841 | (insert (org-export-latex-content |
842 | text '(lists tables fixed-width keywords)) | |
843 | "\n\n")) | |
15841868 JW |
844 | |
845 | ;; insert lines before the first headline | |
c8d0cf5c | 846 | (unless skip |
15841868 | 847 | (insert first-lines)) |
72a81656 | 848 | |
15841868 | 849 | ;; export the content of headlines |
72a81656 CD |
850 | (org-export-latex-global |
851 | (with-temp-buffer | |
852 | (insert string-for-export) | |
853 | (goto-char (point-min)) | |
15841868 JW |
854 | (when (re-search-forward "^\\(\\*+\\) " nil t) |
855 | (let* ((asters (length (match-string 1))) | |
856 | (level (if odd (- asters 2) (- asters 1)))) | |
db9c3fb1 | 857 | (setq org-export-latex-add-level |
15841868 JW |
858 | (if odd (1- (/ (1+ asters) 2)) (1- asters))) |
859 | (org-export-latex-parse-global level odd))))) | |
860 | ||
861 | ;; finalization | |
72a81656 | 862 | (unless body-only (insert "\n\\end{document}")) |
c8d0cf5c | 863 | |
ed21c5c8 CD |
864 | ;; Attach description terms to the \item macro |
865 | (goto-char (point-min)) | |
866 | (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t) | |
867 | (delete-region (match-beginning 1) (match-end 1))) | |
868 | ||
c8d0cf5c CD |
869 | ;; Relocate the table of contents |
870 | (goto-char (point-min)) | |
871 | (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t) | |
872 | (goto-char (point-min)) | |
873 | (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t) | |
874 | (replace-match "")) | |
875 | (goto-char (point-min)) | |
876 | (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t) | |
877 | (replace-match "\\tableofcontents" t t))) | |
878 | ||
ed21c5c8 CD |
879 | ;; Cleanup forced line ends in items where they are not needed |
880 | (goto-char (point-min)) | |
881 | (while (re-search-forward | |
882 | "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin" | |
883 | nil t) | |
884 | (delete-region (match-beginning 1) (match-end 1))) | |
885 | (goto-char (point-min)) | |
886 | (while (re-search-forward | |
887 | "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*" | |
888 | nil t) | |
889 | (if (looking-at "[\n \t]+") | |
890 | (replace-match "\n"))) | |
891 | ||
8d642074 | 892 | (run-hooks 'org-export-latex-final-hook) |
86fbb8ca CD |
893 | (if to-buffer |
894 | (unless (eq major-mode 'latex-mode) (latex-mode)) | |
895 | (save-buffer)) | |
ed21c5c8 CD |
896 | (org-export-latex-fix-inputenc) |
897 | (run-hooks 'org-export-latex-after-save-hook) | |
72a81656 | 898 | (goto-char (point-min)) |
c8d0cf5c CD |
899 | (or (org-export-push-to-kill-ring "LaTeX") |
900 | (message "Exporting to LaTeX...done")) | |
15841868 JW |
901 | (prog1 |
902 | (if (eq to-buffer 'string) | |
903 | (prog1 (buffer-substring (point-min) (point-max)) | |
904 | (kill-buffer (current-buffer))) | |
905 | (current-buffer)) | |
906 | (set-window-configuration wcf)))) | |
907 | ||
71d35b24 CD |
908 | ;;;###autoload |
909 | (defun org-export-as-pdf (arg &optional hidden ext-plist | |
910 | to-buffer body-only pub-dir) | |
911 | "Export as LaTeX, then process through to PDF." | |
912 | (interactive "P") | |
913 | (message "Exporting to PDF...") | |
914 | (let* ((wconfig (current-window-configuration)) | |
915 | (lbuf (org-export-as-latex arg hidden ext-plist | |
916 | to-buffer body-only pub-dir)) | |
917 | (file (buffer-file-name lbuf)) | |
918 | (base (file-name-sans-extension (buffer-file-name lbuf))) | |
c8d0cf5c CD |
919 | (pdffile (concat base ".pdf")) |
920 | (cmds org-latex-to-pdf-process) | |
921 | (outbuf (get-buffer-create "*Org PDF LaTeX Output*")) | |
922 | (bibtex-p (with-current-buffer lbuf | |
923 | (save-excursion | |
924 | (goto-char (point-min)) | |
925 | (re-search-forward "\\\\bibliography{" nil t)))) | |
afe98dfa | 926 | cmd output-dir errors) |
c8d0cf5c | 927 | (with-current-buffer outbuf (erase-buffer)) |
afe98dfa CD |
928 | (message (concat "Processing LaTeX file " file "...")) |
929 | (setq output-dir (file-name-directory file)) | |
c8d0cf5c | 930 | (if (and cmds (symbolp cmds)) |
86fbb8ca | 931 | (funcall cmds (shell-quote-argument file)) |
c8d0cf5c CD |
932 | (while cmds |
933 | (setq cmd (pop cmds)) | |
934 | (while (string-match "%b" cmd) | |
935 | (setq cmd (replace-match | |
936 | (save-match-data | |
937 | (shell-quote-argument base)) | |
938 | t t cmd))) | |
afe98dfa | 939 | (while (string-match "%f" cmd) |
c8d0cf5c CD |
940 | (setq cmd (replace-match |
941 | (save-match-data | |
942 | (shell-quote-argument file)) | |
943 | t t cmd))) | |
afe98dfa CD |
944 | (while (string-match "%o" cmd) |
945 | (setq cmd (replace-match | |
946 | (save-match-data | |
947 | (shell-quote-argument output-dir)) | |
948 | t t cmd))) | |
949 | (shell-command cmd outbuf))) | |
950 | (message (concat "Processing LaTeX file " file "...done")) | |
951 | (setq errors (org-export-latex-get-error outbuf)) | |
71d35b24 | 952 | (if (not (file-exists-p pdffile)) |
afe98dfa CD |
953 | (error (concat "PDF file " pdffile " was not produced" |
954 | (if errors (concat ":" errors "") ""))) | |
71d35b24 CD |
955 | (set-window-configuration wconfig) |
956 | (when org-export-pdf-remove-logfiles | |
ed21c5c8 | 957 | (dolist (ext org-export-pdf-logfiles) |
71d35b24 CD |
958 | (setq file (concat base "." ext)) |
959 | (and (file-exists-p file) (delete-file file)))) | |
afe98dfa CD |
960 | (message (concat |
961 | "Exporting to PDF...done" | |
962 | (if errors | |
963 | (concat ", with some errors:" errors) | |
964 | ""))) | |
71d35b24 CD |
965 | pdffile))) |
966 | ||
afe98dfa CD |
967 | (defun org-export-latex-get-error (buf) |
968 | "Collect the kinds of errors that remain in pdflatex processing." | |
969 | (with-current-buffer buf | |
970 | (save-excursion | |
971 | (goto-char (point-max)) | |
972 | (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t) | |
973 | ;; OK, we are at the location of the final run | |
974 | (let ((pos (point)) (errors "") (case-fold-search t)) | |
975 | (if (re-search-forward "Reference.*?undefined" nil t) | |
976 | (setq errors (concat errors " [undefined reference]"))) | |
977 | (goto-char pos) | |
978 | (if (re-search-forward "Citation.*?undefined" nil t) | |
979 | (setq errors (concat errors " [undefined citation]"))) | |
980 | (goto-char pos) | |
981 | (if (re-search-forward "Undefined control sequence" nil t) | |
982 | (setq errors (concat errors " [undefined control sequence]"))) | |
983 | (and (org-string-nw-p errors) errors)))))) | |
984 | ||
71d35b24 CD |
985 | ;;;###autoload |
986 | (defun org-export-as-pdf-and-open (arg) | |
987 | "Export as LaTeX, then process through to PDF, and open." | |
988 | (interactive "P") | |
989 | (let ((pdffile (org-export-as-pdf arg))) | |
990 | (if pdffile | |
ed21c5c8 CD |
991 | (progn |
992 | (org-open-file pdffile) | |
993 | (when org-export-kill-product-buffer-when-displayed | |
994 | (kill-buffer (find-buffer-visiting | |
995 | (concat (file-name-sans-extension (buffer-file-name)) | |
996 | ".tex"))))) | |
71d35b24 CD |
997 | (error "PDF file was not produced")))) |
998 | ||
0b8568f5 | 999 | ;;; Parsing functions: |
db9c3fb1 | 1000 | |
72a81656 CD |
1001 | (defun org-export-latex-parse-global (level odd) |
1002 | "Parse the current buffer recursively, starting at LEVEL. | |
1003 | If ODD is non-nil, assume the buffer only contains odd sections. | |
db9c3fb1 | 1004 | Return a list reflecting the document structure." |
72a81656 CD |
1005 | (save-excursion |
1006 | (goto-char (point-min)) | |
1007 | (let* ((cnt 0) output | |
db9c3fb1 | 1008 | (depth org-export-latex-sectioning-depth)) |
ed21c5c8 | 1009 | (while (org-re-search-forward-unprotected |
72a81656 CD |
1010 | (concat "^\\(\\(?:\\*\\)\\{" |
1011 | (number-to-string (+ (if odd 2 1) level)) | |
1012 | "\\}\\) \\(.*\\)$") | |
1013 | ;; make sure that there is no upper heading | |
1014 | (when (> level 0) | |
1015 | (save-excursion | |
1016 | (save-match-data | |
ed21c5c8 | 1017 | (org-re-search-forward-unprotected |
72a81656 CD |
1018 | (concat "^\\(\\(?:\\*\\)\\{" |
1019 | (number-to-string level) | |
1020 | "\\}\\) \\(.*\\)$") nil t)))) t) | |
1021 | (setq cnt (1+ cnt)) | |
1022 | (let* ((pos (match-beginning 0)) | |
1023 | (heading (match-string 2)) | |
1024 | (nlevel (if odd (/ (+ 3 level) 2) (1+ level)))) | |
1025 | (save-excursion | |
1026 | (narrow-to-region | |
1027 | (point) | |
1028 | (save-match-data | |
ed21c5c8 | 1029 | (if (org-re-search-forward-unprotected |
72a81656 CD |
1030 | (concat "^\\(\\(?:\\*\\)\\{" |
1031 | (number-to-string (+ (if odd 2 1) level)) | |
1032 | "\\}\\) \\(.*\\)$") nil t) | |
1033 | (match-beginning 0) | |
1034 | (point-max)))) | |
1035 | (goto-char (point-min)) | |
1036 | (setq output | |
1037 | (append output | |
1038 | (list | |
1039 | (list | |
1040 | `(pos . ,pos) | |
1041 | `(level . ,nlevel) | |
1042 | `(occur . ,cnt) | |
1043 | `(heading . ,heading) | |
1044 | `(content . ,(org-export-latex-parse-content)) | |
db9c3fb1 | 1045 | `(subcontent . ,(org-export-latex-parse-subcontent |
72a81656 CD |
1046 | level odd))))))) |
1047 | (widen))) | |
1048 | (list output)))) | |
1049 | ||
1050 | (defun org-export-latex-parse-content () | |
1051 | "Extract the content of a section." | |
1052 | (let ((beg (point)) | |
ed21c5c8 | 1053 | (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t) |
72a81656 CD |
1054 | (progn (beginning-of-line) (point)) |
1055 | (point-max)))) | |
1056 | (buffer-substring beg end))) | |
1057 | ||
1058 | (defun org-export-latex-parse-subcontent (level odd) | |
1059 | "Extract the subcontent of a section at LEVEL. | |
1060 | If ODD Is non-nil, assume subcontent only contains odd sections." | |
ed21c5c8 | 1061 | (if (not (org-re-search-forward-unprotected |
72a81656 CD |
1062 | (concat "^\\(\\(?:\\*\\)\\{" |
1063 | (number-to-string (+ (if odd 4 2) level)) | |
1064 | "\\}\\) \\(.*\\)$") | |
1065 | nil t)) | |
1066 | nil ; subcontent is nil | |
1067 | (org-export-latex-parse-global (+ (if odd 2 1) level) odd))) | |
1068 | ||
0b8568f5 | 1069 | ;;; Rendering functions: |
72a81656 CD |
1070 | (defun org-export-latex-global (content) |
1071 | "Export CONTENT to LaTeX. | |
1072 | CONTENT is an element of the list produced by | |
1073 | `org-export-latex-parse-global'." | |
1074 | (if (eq (car content) 'subcontent) | |
1075 | (mapc 'org-export-latex-sub (cdr content)) | |
1076 | (org-export-latex-sub (car content)))) | |
1077 | ||
1078 | (defun org-export-latex-sub (subcontent) | |
1079 | "Export the list SUBCONTENT to LaTeX. | |
1080 | SUBCONTENT is an alist containing information about the headline | |
1081 | and its content." | |
db9c3fb1 | 1082 | (let ((num (plist-get org-export-latex-options-plist :section-numbers))) |
d5098885 | 1083 | (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent))) |
72a81656 | 1084 | |
d5098885 | 1085 | (defun org-export-latex-subcontent (subcontent num) |
621f83e4 CD |
1086 | "Export each cell of SUBCONTENT to LaTeX. |
1087 | If NUM, export sections as numerical sections." | |
ed21c5c8 | 1088 | (let* ((heading (cdr (assoc 'heading subcontent))) |
621f83e4 CD |
1089 | (level (- (cdr (assoc 'level subcontent)) |
1090 | org-export-latex-add-level)) | |
1091 | (occur (number-to-string (cdr (assoc 'occur subcontent)))) | |
1092 | (content (cdr (assoc 'content subcontent))) | |
1093 | (subcontent (cadr (assoc 'subcontent subcontent))) | |
ce4fdcb9 CD |
1094 | (label (org-get-text-property-any 0 'target heading)) |
1095 | (label-list (cons label (cdr (assoc label | |
ed21c5c8 CD |
1096 | org-export-target-aliases)))) |
1097 | (sectioning org-export-latex-sectioning) | |
1098 | (depth org-export-latex-sectioning-depth) | |
1099 | main-heading sub-heading) | |
1100 | (when (symbolp (car sectioning)) | |
1101 | (setq sectioning (funcall (car sectioning) level heading)) | |
1102 | (when sectioning | |
1103 | (setq heading (car sectioning) | |
1104 | sectioning (cdr sectioning) | |
1105 | ;; target property migh have changed... | |
1106 | label (org-get-text-property-any 0 'target heading) | |
1107 | label-list (cons label (cdr (assoc label | |
1108 | org-export-target-aliases))))) | |
1109 | (if sectioning (setq sectioning (make-list 10 sectioning))) | |
1110 | (setq depth (if sectioning 10000 0))) | |
1111 | (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading) | |
1112 | (setq main-heading (substring heading 0 (match-beginning 0)) | |
1113 | sub-heading (substring heading (match-end 0)))) | |
1114 | (setq heading (org-export-latex-fontify-headline heading) | |
1115 | sub-heading (and sub-heading | |
1116 | (org-export-latex-fontify-headline sub-heading)) | |
1117 | main-heading (and main-heading | |
1118 | (org-export-latex-fontify-headline main-heading))) | |
db9c3fb1 | 1119 | (cond |
72a81656 | 1120 | ;; Normal conversion |
ed21c5c8 CD |
1121 | ((<= level depth) |
1122 | (let* ((sec (nth (1- level) sectioning)) | |
71d35b24 CD |
1123 | start end) |
1124 | (if (consp (cdr sec)) | |
1125 | (setq start (nth (if num 0 2) sec) | |
1126 | end (nth (if num 1 3) sec)) | |
1127 | (setq start (if num (car sec) (cdr sec)))) | |
ed21c5c8 CD |
1128 | (insert (format start (if main-heading main-heading heading) |
1129 | (or sub-heading ""))) | |
1130 | (insert "\n") | |
ce4fdcb9 CD |
1131 | (when label |
1132 | (insert (mapconcat (lambda (l) (format "\\label{%s}" l)) | |
1133 | label-list "\n") "\n")) | |
71d35b24 CD |
1134 | (insert (org-export-latex-content content)) |
1135 | (cond ((stringp subcontent) (insert subcontent)) | |
ed21c5c8 CD |
1136 | ((listp subcontent) |
1137 | (while (org-looking-back "\n\n") (backward-delete-char 1)) | |
1138 | (org-export-latex-sub subcontent))) | |
1139 | (when (and end (string-match "[^ \t]" end)) | |
1140 | (let ((hook (org-get-text-property-any 0 'org-insert-hook end))) | |
1141 | (and (functionp hook) (funcall hook))) | |
1142 | (insert end "\n")))) | |
72a81656 | 1143 | ;; At a level under the hl option: we can drop this subsection |
ed21c5c8 | 1144 | ((> level depth) |
72a81656 | 1145 | (cond ((eq org-export-latex-low-levels 'description) |
c8d0cf5c CD |
1146 | (if (string-match "% ends low level$" |
1147 | (buffer-substring (point-at-bol 0) (point))) | |
1148 | (delete-region (point-at-bol 0) (point)) | |
1149 | (insert "\\begin{description}\n")) | |
ed21c5c8 | 1150 | (insert (format "\n\\item[%s]%s~\n" |
621f83e4 CD |
1151 | heading |
1152 | (if label (format "\\label{%s}" label) ""))) | |
72a81656 CD |
1153 | (insert (org-export-latex-content content)) |
1154 | (cond ((stringp subcontent) (insert subcontent)) | |
1155 | ((listp subcontent) (org-export-latex-sub subcontent))) | |
c8d0cf5c CD |
1156 | (insert "\\end{description} % ends low level\n")) |
1157 | ((memq org-export-latex-low-levels '(itemize enumerate)) | |
1158 | (if (string-match "% ends low level$" | |
1159 | (buffer-substring (point-at-bol 0) (point))) | |
1160 | (delete-region (point-at-bol 0) (point)) | |
1161 | (insert (format "\\begin{%s}\n" | |
1162 | (symbol-name org-export-latex-low-levels)))) | |
ed21c5c8 | 1163 | (insert (format "\n\\item %s\\\\\n%s%%" |
c8d0cf5c CD |
1164 | heading |
1165 | (if label (format "\\label{%s}" label) ""))) | |
1166 | (insert (org-export-latex-content content)) | |
1167 | (cond ((stringp subcontent) (insert subcontent)) | |
1168 | ((listp subcontent) (org-export-latex-sub subcontent))) | |
1169 | (insert (format "\\end{%s} %% ends low level\n" | |
1170 | (symbol-name org-export-latex-low-levels)))) | |
1171 | ||
1172 | ((listp org-export-latex-low-levels) | |
1173 | (if (string-match "% ends low level$" | |
1174 | (buffer-substring (point-at-bol 0) (point))) | |
1175 | (delete-region (point-at-bol 0) (point)) | |
1176 | (insert (car org-export-latex-low-levels) "\n")) | |
1177 | (insert (format (nth 2 org-export-latex-low-levels) | |
1178 | heading | |
1179 | (if label (format "\\label{%s}" label) ""))) | |
1180 | (insert (org-export-latex-content content)) | |
1181 | (cond ((stringp subcontent) (insert subcontent)) | |
1182 | ((listp subcontent) (org-export-latex-sub subcontent))) | |
1183 | (insert (nth 1 org-export-latex-low-levels) | |
1184 | " %% ends low level\n")) | |
1185 | ||
72a81656 CD |
1186 | ((stringp org-export-latex-low-levels) |
1187 | (insert (format org-export-latex-low-levels heading) "\n") | |
621f83e4 | 1188 | (when label (insert (format "\\label{%s}\n" label))) |
72a81656 CD |
1189 | (insert (org-export-latex-content content)) |
1190 | (cond ((stringp subcontent) (insert subcontent)) | |
1191 | ((listp subcontent) (org-export-latex-sub subcontent))))))))) | |
1192 | ||
0b8568f5 | 1193 | ;;; Exporting internals: |
db9c3fb1 | 1194 | (defun org-export-latex-set-initial-vars (ext-plist level) |
0b8568f5 | 1195 | "Store org local variables required for LaTeX export. |
db9c3fb1 CD |
1196 | EXT-PLIST is an optional additional plist. |
1197 | LEVEL indicates the default depth for export." | |
1198 | (setq org-export-latex-todo-keywords-1 org-todo-keywords-1 | |
c8d0cf5c CD |
1199 | org-export-latex-done-keywords org-done-keywords |
1200 | org-export-latex-not-done-keywords org-not-done-keywords | |
1201 | org-export-latex-complex-heading-re org-complex-heading-regexp | |
1202 | org-export-latex-display-custom-times org-display-custom-times | |
db9c3fb1 | 1203 | org-export-latex-all-targets-re |
0b8568f5 | 1204 | (org-make-target-link-regexp (org-all-targets)) |
db9c3fb1 | 1205 | org-export-latex-options-plist |
0b8568f5 JW |
1206 | (org-combine-plists (org-default-export-plist) ext-plist |
1207 | (org-infile-export-plist)) | |
db9c3fb1 | 1208 | org-export-latex-class |
0bd48b37 CD |
1209 | (or (and (org-region-active-p) |
1210 | (save-excursion | |
1211 | (goto-char (region-beginning)) | |
1212 | (and (looking-at org-complex-heading-regexp) | |
1213 | (org-entry-get nil "LaTeX_CLASS" 'selective)))) | |
1214 | (save-excursion | |
1215 | (save-restriction | |
1216 | (widen) | |
1217 | (goto-char (point-min)) | |
86fbb8ca | 1218 | (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\(-[a-zA-Z]+\\)" nil t) |
0bd48b37 | 1219 | (match-string 1)))) |
8d642074 | 1220 | (plist-get org-export-latex-options-plist :latex-class) |
0bd48b37 | 1221 | org-export-latex-default-class) |
ed21c5c8 CD |
1222 | org-export-latex-class-options |
1223 | (or (and (org-region-active-p) | |
1224 | (save-excursion | |
1225 | (goto-char (region-beginning)) | |
1226 | (and (looking-at org-complex-heading-regexp) | |
1227 | (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective)))) | |
1228 | (save-excursion | |
1229 | (save-restriction | |
1230 | (widen) | |
1231 | (goto-char (point-min)) | |
1232 | (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t) | |
1233 | (match-string 1)))) | |
1234 | (plist-get org-export-latex-options-plist :latex-class-options)) | |
0bd48b37 CD |
1235 | org-export-latex-class |
1236 | (or (car (assoc org-export-latex-class org-export-latex-classes)) | |
1237 | (error "No definition for class `%s' in `org-export-latex-classes'" | |
1238 | org-export-latex-class)) | |
db9c3fb1 CD |
1239 | org-export-latex-header |
1240 | (cadr (assoc org-export-latex-class org-export-latex-classes)) | |
1241 | org-export-latex-sectioning | |
1242 | (cddr (assoc org-export-latex-class org-export-latex-classes)) | |
1243 | org-export-latex-sectioning-depth | |
1244 | (or level | |
1245 | (let ((hl-levels | |
1246 | (plist-get org-export-latex-options-plist :headline-levels)) | |
1247 | (sec-depth (length org-export-latex-sectioning))) | |
ed21c5c8 CD |
1248 | (if (> hl-levels sec-depth) sec-depth hl-levels)))) |
1249 | (when (and org-export-latex-class-options | |
1250 | (string-match "\\S-" org-export-latex-class-options) | |
1251 | (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?" | |
1252 | org-export-latex-header)) | |
1253 | (setq org-export-latex-header | |
1254 | (concat (substring org-export-latex-header 0 (match-end 1)) | |
1255 | org-export-latex-class-options | |
1256 | (substring org-export-latex-header (match-end 0)))))) | |
1257 | ||
1258 | (defvar org-export-latex-format-toc-function | |
1259 | 'org-export-latex-format-toc-default | |
86fbb8ca | 1260 | "The function formatting returning the string to create the table of contents. |
ed21c5c8 | 1261 | The function mus take one parameter, the depth of the table of contents.") |
db9c3fb1 CD |
1262 | |
1263 | (defun org-export-latex-make-header (title opt-plist) | |
1264 | "Make the LaTeX header and return it as a string. | |
1265 | TITLE is the current title from the buffer or region. | |
1266 | OPT-PLIST is the options plist for current buffer." | |
1267 | (let ((toc (plist-get opt-plist :table-of-contents)) | |
ed21c5c8 CD |
1268 | (author (org-export-apply-macros-in-string |
1269 | (plist-get opt-plist :author)))) | |
db9c3fb1 | 1270 | (concat |
15841868 | 1271 | (if (plist-get opt-plist :time-stamp-file) |
6671980f | 1272 | (format-time-string "%% Created %Y-%m-%d %a %H:%M\n")) |
ed21c5c8 CD |
1273 | ;; insert LaTeX custom header and packages from the list |
1274 | (org-splice-latex-header | |
1275 | (org-export-apply-macros-in-string org-export-latex-header) | |
1276 | org-export-latex-default-packages-alist | |
86fbb8ca | 1277 | org-export-latex-packages-alist nil |
ed21c5c8 CD |
1278 | (org-export-apply-macros-in-string |
1279 | (plist-get opt-plist :latex-header-extra))) | |
1280 | ;; append another special variable | |
c8d0cf5c | 1281 | (org-export-apply-macros-in-string org-export-latex-append-header) |
afe98dfa | 1282 | ;; define alert if not yet defined |
ed21c5c8 | 1283 | "\n\\providecommand{\\alert}[1]{\\textbf{#1}}" |
acedf35c CD |
1284 | ;; beginning of the document |
1285 | "\n\\begin{document}\n\n" | |
15841868 | 1286 | ;; insert the title |
db9c3fb1 CD |
1287 | (format |
1288 | "\n\n\\title{%s}\n" | |
03f3cf35 | 1289 | ;; convert the title |
acedf35c | 1290 | (org-export-latex-fontify-headline title)) |
15841868 JW |
1291 | ;; insert author info |
1292 | (if (plist-get opt-plist :author-info) | |
db9c3fb1 | 1293 | (format "\\author{%s}\n" |
8bfe682a | 1294 | (org-export-latex-fontify-headline (or author user-full-name))) |
15841868 | 1295 | (format "%%\\author{%s}\n" |
ed21c5c8 | 1296 | (org-export-latex-fontify-headline (or author user-full-name)))) |
15841868 JW |
1297 | ;; insert the date |
1298 | (format "\\date{%s}\n" | |
db9c3fb1 | 1299 | (format-time-string |
15841868 JW |
1300 | (or (plist-get opt-plist :date) |
1301 | org-export-latex-date-format))) | |
15841868 | 1302 | ;; insert the title command |
c8d0cf5c CD |
1303 | (when (string-match "\\S-" title) |
1304 | (if (string-match "%s" org-export-latex-title-command) | |
1305 | (format org-export-latex-title-command title) | |
1306 | org-export-latex-title-command)) | |
15841868 | 1307 | "\n\n" |
15841868 | 1308 | ;; table of contents |
db9c3fb1 | 1309 | (when (and org-export-with-toc |
15841868 | 1310 | (plist-get opt-plist :section-numbers)) |
ed21c5c8 CD |
1311 | (funcall org-export-latex-format-toc-function |
1312 | (cond ((numberp toc) | |
1313 | (min toc (plist-get opt-plist :headline-levels))) | |
1314 | (toc (plist-get opt-plist :headline-levels)))))))) | |
1315 | ||
1316 | (defun org-export-latex-format-toc-default (depth) | |
1317 | (when depth | |
1318 | (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n" | |
1319 | depth))) | |
0b8568f5 | 1320 | |
c8d0cf5c | 1321 | (defun org-export-latex-first-lines (opt-plist &optional beg end) |
0b8568f5 | 1322 | "Export the first lines before first headline. |
c8d0cf5c CD |
1323 | If BEG is non-nil, it is the beginning of the region. |
1324 | If END is non-nil, it is the end of the region." | |
0b8568f5 | 1325 | (save-excursion |
db55f368 | 1326 | (goto-char (or beg (point-min))) |
8d642074 | 1327 | (let* ((pt (point)) |
acedf35c | 1328 | (end (if (re-search-forward (org-get-limited-outline-regexp) end t) |
8d642074 CD |
1329 | (goto-char (match-beginning 0)) |
1330 | (goto-char (or end (point-max)))))) | |
db55f368 CD |
1331 | (prog1 |
1332 | (org-export-latex-content | |
1333 | (org-export-preprocess-string | |
1334 | (buffer-substring pt end) | |
1335 | :for-LaTeX t | |
1336 | :emph-multiline t | |
1337 | :add-text nil | |
1338 | :comments nil | |
1339 | :skip-before-1st-heading nil | |
1e4f816a CD |
1340 | :LaTeX-fragments nil |
1341 | :timestamps (plist-get opt-plist :timestamps) | |
1342 | :footnotes (plist-get opt-plist :footnotes))) | |
c8d0cf5c | 1343 | (org-unmodified |
86fbb8ca CD |
1344 | (let ((inhibit-read-only t) |
1345 | (limit (max pt (1- end)))) | |
1346 | (add-text-properties pt limit | |
1347 | '(:org-license-to-kill t)) | |
1348 | (save-excursion | |
1349 | (goto-char pt) | |
afe98dfa CD |
1350 | (while (re-search-forward "^[ \t]*#\\+.*\n?" limit t) |
1351 | (let ((case-fold-search t)) | |
1352 | (unless (org-string-match-p | |
1353 | "^[ \t]*#\\+\\(attr_\\|caption\\>\\|label\\>\\)" | |
1354 | (match-string 0)) | |
1355 | (remove-text-properties (match-beginning 0) (match-end 0) | |
1356 | '(:org-license-to-kill t)))))))))))) | |
86fbb8ca | 1357 | |
c8d0cf5c CD |
1358 | |
1359 | (defvar org-export-latex-header-defs nil | |
1360 | "The header definitions that might be used in the LaTeX body.") | |
1361 | (defvar org-export-latex-header-defs-re nil | |
1362 | "The header definitions that might be used in the LaTeX body.") | |
0b8568f5 | 1363 | |
db9c3fb1 CD |
1364 | (defun org-export-latex-content (content &optional exclude-list) |
1365 | "Convert CONTENT string to LaTeX. | |
1366 | Don't perform conversions that are in EXCLUDE-LIST. Recognized | |
1367 | conversion types are: quotation-marks, emphasis, sub-superscript, | |
1368 | links, keywords, lists, tables, fixed-width" | |
1369 | (with-temp-buffer | |
1370 | (insert content) | |
c8d0cf5c CD |
1371 | (unless (memq 'timestamps exclude-list) |
1372 | (org-export-latex-time-stamps)) | |
db9c3fb1 CD |
1373 | (unless (memq 'quotation-marks exclude-list) |
1374 | (org-export-latex-quotation-marks)) | |
1375 | (unless (memq 'emphasis exclude-list) | |
1376 | (when (plist-get org-export-latex-options-plist :emphasize) | |
1377 | (org-export-latex-fontify))) | |
1378 | (unless (memq 'sub-superscript exclude-list) | |
1379 | (org-export-latex-special-chars | |
1380 | (plist-get org-export-latex-options-plist :sub-superscript))) | |
1381 | (unless (memq 'links exclude-list) | |
1382 | (org-export-latex-links)) | |
1383 | (unless (memq 'keywords exclude-list) | |
0bd48b37 | 1384 | (org-export-latex-keywords)) |
db9c3fb1 CD |
1385 | (unless (memq 'lists exclude-list) |
1386 | (org-export-latex-lists)) | |
1387 | (unless (memq 'tables exclude-list) | |
1388 | (org-export-latex-tables | |
1389 | (plist-get org-export-latex-options-plist :tables))) | |
1390 | (unless (memq 'fixed-width exclude-list) | |
1391 | (org-export-latex-fixed-width | |
1392 | (plist-get org-export-latex-options-plist :fixed-width))) | |
1393 | ;; return string | |
1394 | (buffer-substring (point-min) (point-max)))) | |
1395 | ||
1396 | (defun org-export-latex-protect-string (s) | |
621f83e4 | 1397 | "Add the org-protected property to string S." |
db9c3fb1 CD |
1398 | (add-text-properties 0 (length s) '(org-protected t) s) s) |
1399 | ||
1400 | (defun org-export-latex-protect-char-in-string (char-list string) | |
1401 | "Add org-protected text-property to char from CHAR-LIST in STRING." | |
1402 | (with-temp-buffer | |
1403 | (save-match-data | |
1404 | (insert string) | |
1405 | (goto-char (point-min)) | |
1406 | (while (re-search-forward (regexp-opt char-list) nil t) | |
1407 | (add-text-properties (match-beginning 0) | |
1408 | (match-end 0) '(org-protected t))) | |
1409 | (buffer-string)))) | |
1410 | ||
0bd48b37 | 1411 | (defun org-export-latex-keywords-maybe (&optional remove-list) |
72a81656 CD |
1412 | "Maybe remove keywords depending on rules in REMOVE-LIST." |
1413 | (goto-char (point-min)) | |
db9c3fb1 | 1414 | (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|")) |
c8d0cf5c CD |
1415 | (case-fold-search nil) |
1416 | (todo-markup org-export-latex-todo-keyword-markup) | |
1417 | fmt) | |
72a81656 CD |
1418 | ;; convert TODO keywords |
1419 | (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t) | |
1420 | (if (plist-get remove-list :todo) | |
1421 | (replace-match "") | |
c8d0cf5c CD |
1422 | (setq fmt (cond |
1423 | ((stringp todo-markup) todo-markup) | |
1424 | ((and (consp todo-markup) (stringp (car todo-markup))) | |
1425 | (if (member (match-string 1) org-export-latex-done-keywords) | |
1426 | (cdr todo-markup) (car todo-markup))) | |
1427 | (t (cdr (or (assoc (match-string 1) todo-markup) | |
1428 | (car todo-markup)))))) | |
86fbb8ca CD |
1429 | (replace-match (org-export-latex-protect-string |
1430 | (format fmt (match-string 1))) t t))) | |
72a81656 CD |
1431 | ;; convert priority string |
1432 | (when (re-search-forward "\\[\\\\#.\\]" nil t) | |
1433 | (if (plist-get remove-list :priority) | |
1434 | (replace-match "") | |
0bd48b37 | 1435 | (replace-match (format "\\textbf{%s}" (match-string 0)) t t))) |
72a81656 | 1436 | ;; convert tags |
afe98dfa | 1437 | (when (re-search-forward "\\(:[a-zA-Z0-9_@#%]+\\)+:" nil t) |
0b8568f5 JW |
1438 | (if (or (not org-export-with-tags) |
1439 | (plist-get remove-list :tags)) | |
72a81656 | 1440 | (replace-match "") |
621f83e4 | 1441 | (replace-match |
2c3ad40d | 1442 | (org-export-latex-protect-string |
afe98dfa | 1443 | (format org-export-latex-tag-markup |
33306645 CD |
1444 | (save-match-data |
1445 | (replace-regexp-in-string | |
1446 | "_" "\\\\_" (match-string 0))))) | |
1447 | t t))))) | |
72a81656 | 1448 | |
db9c3fb1 | 1449 | (defun org-export-latex-fontify-headline (string) |
621f83e4 | 1450 | "Fontify special words in STRING." |
72a81656 CD |
1451 | (with-temp-buffer |
1452 | ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at | |
1453 | ;; the beginning of the buffer - inserting "\n" is safe here though. | |
db9c3fb1 | 1454 | (insert "\n" string) |
acedf35c CD |
1455 | |
1456 | ;; Preserve math snippets | |
1457 | ||
1458 | (let* ((matchers (plist-get org-format-latex-options :matchers)) | |
1459 | (re-list org-latex-regexps) | |
1460 | beg end re e m n block off) | |
1461 | ;; Check the different regular expressions | |
1462 | (while (setq e (pop re-list)) | |
1463 | (setq m (car e) re (nth 1 e) n (nth 2 e) | |
1464 | block (if (nth 3 e) "\n\n" "")) | |
1465 | (setq off (if (member m '("$" "$1")) 1 0)) | |
1466 | (when (and (member m matchers) (not (equal m "begin"))) | |
1467 | (goto-char (point-min)) | |
1468 | (while (re-search-forward re nil t) | |
1469 | (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0)) | |
1470 | (add-text-properties beg end | |
1471 | '(org-protected t org-latex-math t)))))) | |
1472 | ||
1473 | ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{} | |
1474 | (goto-char (point-min)) | |
1475 | (let ((case-fold-search nil)) | |
1476 | (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t) | |
1477 | (unless (eq (char-before (match-beginning 1)) ?\\) | |
1478 | (org-if-unprotected-1 | |
1479 | (replace-match (org-export-latex-protect-string | |
1480 | (concat "\\" (match-string 1) | |
1481 | "{}")) t t))))) | |
72a81656 | 1482 | (goto-char (point-min)) |
ed21c5c8 CD |
1483 | (let ((re (concat "\\\\\\([a-zA-Z]+\\)" |
1484 | "\\(?:<[^<>\n]*>\\)*" | |
1485 | "\\(?:\\[[^][\n]*?\\]\\)*" | |
1486 | "\\(?:<[^<>\n]*>\\)*" | |
1487 | "\\(" | |
1488 | (org-create-multibrace-regexp "{" "}" 3) | |
1489 | "\\)\\{1,3\\}"))) | |
8bfe682a | 1490 | (while (re-search-forward re nil t) |
ed21c5c8 CD |
1491 | (unless (or |
1492 | ;; check for comment line | |
1493 | (save-excursion (goto-char (match-beginning 0)) | |
86fbb8ca | 1494 | (org-in-indented-comment-line)) |
ed21c5c8 CD |
1495 | ;; Check if this is a defined entity, so that is may need conversion |
1496 | (org-entity-get (match-string 1))) | |
8bfe682a CD |
1497 | (add-text-properties (match-beginning 0) (match-end 0) |
1498 | '(org-protected t))))) | |
db9c3fb1 | 1499 | (when (plist-get org-export-latex-options-plist :emphasize) |
0b8568f5 | 1500 | (org-export-latex-fontify)) |
0bd48b37 | 1501 | (org-export-latex-keywords-maybe) |
2c3ad40d CD |
1502 | (org-export-latex-special-chars |
1503 | (plist-get org-export-latex-options-plist :sub-superscript)) | |
72a81656 | 1504 | (org-export-latex-links) |
621f83e4 | 1505 | (org-trim (buffer-string)))) |
72a81656 | 1506 | |
c8d0cf5c CD |
1507 | (defun org-export-latex-time-stamps () |
1508 | "Format time stamps." | |
1509 | (goto-char (point-min)) | |
1510 | (let ((org-display-custom-times org-export-latex-display-custom-times)) | |
1511 | (while (re-search-forward org-ts-regexp-both nil t) | |
1512 | (org-if-unprotected-at (1- (point)) | |
1513 | (replace-match | |
1514 | (org-export-latex-protect-string | |
1515 | (format org-export-latex-timestamp-markup | |
1516 | (substring (org-translate-time (match-string 0)) 1 -1))) | |
1517 | t t))))) | |
1518 | ||
72a81656 | 1519 | (defun org-export-latex-quotation-marks () |
621f83e4 | 1520 | "Export quotation marks depending on language conventions." |
db9c3fb1 | 1521 | (let* ((lang (plist-get org-export-latex-options-plist :language)) |
72a81656 | 1522 | (quote-rpl (if (equal lang "fr") |
db55f368 CD |
1523 | '(("\\(\\s-\\)\"" "«~") |
1524 | ("\\(\\S-\\)\"" "~»") | |
72a81656 | 1525 | ("\\(\\s-\\)'" "`")) |
8bfe682a | 1526 | '(("\\(\\s-\\|[[(]\\)\"" "``") |
72a81656 | 1527 | ("\\(\\S-\\)\"" "''") |
c8d0cf5c | 1528 | ("\\(\\s-\\|(\\)'" "`"))))) |
72a81656 CD |
1529 | (mapc (lambda(l) (goto-char (point-min)) |
1530 | (while (re-search-forward (car l) nil t) | |
8bfe682a CD |
1531 | (let ((rpl (concat (match-string 1) |
1532 | (org-export-latex-protect-string | |
1533 | (copy-sequence (cadr l)))))) | |
0bd48b37 | 1534 | (org-if-unprotected-1 |
72a81656 CD |
1535 | (replace-match rpl t t))))) quote-rpl))) |
1536 | ||
72a81656 CD |
1537 | (defun org-export-latex-special-chars (sub-superscript) |
1538 | "Export special characters to LaTeX. | |
1539 | If SUB-SUPERSCRIPT is non-nil, convert \\ and ^. | |
1540 | See the `org-export-latex.el' code for a complete conversion table." | |
1541 | (goto-char (point-min)) | |
1542 | (mapc (lambda(c) | |
1543 | (goto-char (point-min)) | |
1544 | (while (re-search-forward c nil t) | |
1545 | ;; Put the point where to check for org-protected | |
db55f368 | 1546 | (unless (get-text-property (match-beginning 2) 'org-protected) |
72a81656 | 1547 | (cond ((member (match-string 2) '("\\$" "$")) |
15841868 | 1548 | (if (equal (match-string 2) "\\$") |
0bd48b37 CD |
1549 | nil |
1550 | (replace-match "\\$" t t))) | |
15841868 JW |
1551 | ((member (match-string 2) '("&" "%" "#")) |
1552 | (if (equal (match-string 1) "\\") | |
1553 | (replace-match (match-string 2) t t) | |
1554 | (replace-match (concat (match-string 1) "\\" | |
ed21c5c8 CD |
1555 | (match-string 2)) t t) |
1556 | (backward-char 1))) | |
db9c3fb1 CD |
1557 | ((equal (match-string 2) "...") |
1558 | (replace-match | |
1559 | (concat (match-string 1) | |
1560 | (org-export-latex-protect-string "\\ldots{}")) t t)) | |
15841868 JW |
1561 | ((equal (match-string 2) "~") |
1562 | (cond ((equal (match-string 1) "\\") nil) | |
1563 | ((eq 'org-link (get-text-property 0 'face (match-string 2))) | |
1564 | (replace-match (concat (match-string 1) "\\~") t t)) | |
db9c3fb1 | 1565 | (t (replace-match |
15841868 JW |
1566 | (org-export-latex-protect-string |
1567 | (concat (match-string 1) "\\~{}")) t t)))) | |
1568 | ((member (match-string 2) '("{" "}")) | |
0bd48b37 | 1569 | (unless (save-match-data (org-inside-latex-math-p)) |
15841868 JW |
1570 | (if (equal (match-string 1) "\\") |
1571 | (replace-match (match-string 2) t t) | |
1572 | (replace-match (concat (match-string 1) "\\" | |
1573 | (match-string 2)) t t))))) | |
0bd48b37 | 1574 | (unless (save-match-data (org-inside-latex-math-p)) |
15841868 JW |
1575 | (cond ((equal (match-string 2) "\\") |
1576 | (replace-match (or (save-match-data | |
1577 | (org-export-latex-treat-backslash-char | |
1578 | (match-string 1) | |
e9aab93b | 1579 | (or (match-string 3) ""))) |
ed21c5c8 CD |
1580 | "") t t) |
1581 | (when (and (get-text-property (1- (point)) 'org-entity) | |
1582 | (looking-at "{}")) | |
1583 | ;; OK, this was an entity replacement, and the user | |
1584 | ;; had terminated the entity with {}. Make sure | |
1585 | ;; {} is protected as well, and remove the extra {} | |
1586 | ;; inserted by the conversion. | |
1587 | (put-text-property (point) (+ 2 (point)) 'org-protected t) | |
1588 | (if (save-excursion (goto-char (max (- (point) 2) (point-min))) | |
1589 | (looking-at "{}")) | |
1590 | (replace-match "")) | |
1591 | (forward-char 2)) | |
1592 | (backward-char 1)) | |
15841868 JW |
1593 | ((member (match-string 2) '("_" "^")) |
1594 | (replace-match (or (save-match-data | |
1595 | (org-export-latex-treat-sub-super-char | |
1596 | sub-superscript | |
15841868 | 1597 | (match-string 2) |
621f83e4 | 1598 | (match-string 1) |
c8d0cf5c CD |
1599 | (match-string 3))) "") t t) |
1600 | (backward-char 1))))))) | |
0bd48b37 CD |
1601 | '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$" |
1602 | "\\(\\(\\\\?\\$\\)\\)" | |
ed21c5c8 CD |
1603 | "\\([a-zA-Z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-zA-Z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-zA-Z0-9]+}\\|([a-zA-Z0-9]+)\\)" |
1604 | "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|\\([&#%{}\"]\\|[a-zA-Z][a-zA-Z0-9]*\\)\\)" | |
72a81656 | 1605 | "\\(.\\|^\\)\\(&\\)" |
33306645 CD |
1606 | "\\(.\\|^\\)\\(#\\)" |
1607 | "\\(.\\|^\\)\\(%\\)" | |
1608 | "\\(.\\|^\\)\\({\\)" | |
72a81656 | 1609 | "\\(.\\|^\\)\\(}\\)" |
15841868 | 1610 | "\\(.\\|^\\)\\(~\\)" |
db9c3fb1 | 1611 | "\\(.\\|^\\)\\(\\.\\.\\.\\)" |
15841868 JW |
1612 | ;; (?\< . "\\textless{}") |
1613 | ;; (?\> . "\\textgreater{}") | |
1614 | ))) | |
72a81656 | 1615 | |
0bd48b37 CD |
1616 | (defun org-inside-latex-math-p () |
1617 | (get-text-property (point) 'org-latex-math)) | |
1618 | ||
72a81656 | 1619 | (defun org-export-latex-treat-sub-super-char |
621f83e4 | 1620 | (subsup char string-before string-after) |
72a81656 CD |
1621 | "Convert the \"_\" and \"^\" characters to LaTeX. |
1622 | SUBSUP corresponds to the ^: option in the #+OPTIONS line. | |
1623 | Convert CHAR depending on STRING-BEFORE and STRING-AFTER." | |
1624 | (cond ((equal string-before "\\") | |
1625 | (concat string-before char string-after)) | |
8d642074 CD |
1626 | ((and (string-match "\\S-+" string-after)) |
1627 | ;; this is part of a math formula | |
0b8568f5 | 1628 | (cond ((eq 'org-link (get-text-property 0 'face char)) |
72a81656 | 1629 | (concat string-before "\\" char string-after)) |
0bd48b37 | 1630 | ((save-match-data (org-inside-latex-math-p)) |
72a81656 CD |
1631 | (if subsup |
1632 | (cond ((eq 1 (length string-after)) | |
1633 | (concat string-before char string-after)) | |
1634 | ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after) | |
db9c3fb1 | 1635 | (format "%s%s{%s}" string-before char |
72a81656 | 1636 | (match-string 1 string-after)))))) |
71d35b24 CD |
1637 | ((and (> (length string-after) 1) |
1638 | (or (eq subsup t) | |
1639 | (and (equal subsup '{}) (eq (string-to-char string-after) ?\{))) | |
86fbb8ca CD |
1640 | (or (string-match "[{]?\\([^}]+\\)[}]?" string-after) |
1641 | (string-match "[(]?\\([^)]+\\)[)]?" string-after))) | |
1642 | ||
c8d0cf5c CD |
1643 | (org-export-latex-protect-string |
1644 | (format "%s$%s{%s}$" string-before char | |
1645 | (if (and (> (match-end 1) (1+ (match-beginning 1))) | |
1646 | (not (equal (substring string-after 0 2) "{\\"))) | |
1647 | (concat "\\mathrm{" (match-string 1 string-after) "}") | |
1648 | (match-string 1 string-after))))) | |
71d35b24 | 1649 | ((eq subsup t) (concat string-before "$" char string-after "$")) |
15841868 | 1650 | (t (org-export-latex-protect-string |
d5098885 | 1651 | (concat string-before "\\" char "{}" string-after))))) |
15841868 | 1652 | (t (org-export-latex-protect-string |
d5098885 | 1653 | (concat string-before "\\" char "{}" string-after))))) |
72a81656 CD |
1654 | |
1655 | (defun org-export-latex-treat-backslash-char (string-before string-after) | |
1656 | "Convert the \"$\" special character to LaTeX. | |
1657 | The conversion is made depending of STRING-BEFORE and STRING-AFTER." | |
ed21c5c8 CD |
1658 | (let ((ass (org-entity-get string-after))) |
1659 | (cond | |
1660 | (ass (org-add-props | |
1661 | (if (nth 2 ass) | |
1662 | (concat string-before | |
1663 | (org-export-latex-protect-string | |
1664 | (concat "$" (nth 1 ass) "$"))) | |
1665 | (concat string-before (org-export-latex-protect-string | |
1666 | (nth 1 ass)))) | |
1667 | nil 'org-entity t)) | |
1668 | ((and (not (string-match "^[ \n\t]" string-after)) | |
1669 | (not (string-match "[ \t]\\'\\|^" string-before))) | |
1670 | ;; backslash is inside a word | |
1671 | (concat string-before | |
1672 | (org-export-latex-protect-string | |
1673 | (concat "\\textbackslash{}" string-after)))) | |
1674 | ((not (or (equal string-after "") | |
1675 | (string-match "^[ \t\n]" string-after))) | |
1676 | ;; backslash might escape a character (like \#) or a user TeX | |
1677 | ;; macro (like \setcounter) | |
1678 | (concat string-before | |
1679 | (org-export-latex-protect-string (concat "\\" string-after)))) | |
1680 | ((and (string-match "^[ \t\n]" string-after) | |
1681 | (string-match "[ \t\n]\\'" string-before)) | |
1682 | ;; backslash is alone, convert it to $\backslash$ | |
1683 | (org-export-latex-protect-string | |
1684 | (concat string-before "\\textbackslash{}" string-after))) | |
1685 | (t (org-export-latex-protect-string | |
1686 | (concat string-before "\\textbackslash{}" string-after)))))) | |
72a81656 | 1687 | |
0bd48b37 CD |
1688 | (defun org-export-latex-keywords () |
1689 | "Convert special keywords to LaTeX." | |
1690 | (goto-char (point-min)) | |
c8d0cf5c CD |
1691 | (while (re-search-forward org-export-latex-special-keyword-regexp nil t) |
1692 | (replace-match (format org-export-latex-timestamp-keyword-markup | |
1693 | (match-string 0)) t t) | |
1694 | (save-excursion | |
1695 | (beginning-of-line 1) | |
ed21c5c8 | 1696 | (unless (looking-at ".*\n[ \t]*\n") |
c8d0cf5c | 1697 | (end-of-line 1) |
ed21c5c8 | 1698 | (insert "\n"))))) |
db9c3fb1 | 1699 | |
72a81656 CD |
1700 | (defun org-export-latex-fixed-width (opt) |
1701 | "When OPT is non-nil convert fixed-width sections to LaTeX." | |
1702 | (goto-char (point-min)) | |
0bd48b37 | 1703 | (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t) |
86fbb8ca CD |
1704 | (unless (get-text-property (point) 'org-example) |
1705 | (if opt | |
1706 | (progn (goto-char (match-beginning 0)) | |
1707 | (insert "\\begin{verbatim}\n") | |
1708 | (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$") | |
1709 | (replace-match (concat (match-string 1) | |
1710 | (match-string 2)) t t) | |
1711 | (forward-line)) | |
1712 | (insert "\\end{verbatim}\n\n")) | |
1713 | (progn (goto-char (match-beginning 0)) | |
1714 | (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$") | |
1715 | (replace-match (concat "%" (match-string 1) | |
1716 | (match-string 2)) t t) | |
1717 | (forward-line))))))) | |
20908596 CD |
1718 | |
1719 | (defvar org-table-last-alignment) ; defined in org-table.el | |
54a0dee5 | 1720 | (defvar org-table-last-column-widths) ; defined in org-table.el |
20908596 | 1721 | (declare-function orgtbl-to-latex "org-table" (table params) t) |
15841868 JW |
1722 | (defun org-export-latex-tables (insert) |
1723 | "Convert tables to LaTeX and INSERT it." | |
ed21c5c8 CD |
1724 | ;; First, get the table.el tables |
1725 | (goto-char (point-min)) | |
1726 | (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t) | |
1727 | (org-if-unprotected | |
1728 | (require 'table) | |
1729 | (org-export-latex-convert-table.el-table))) | |
1730 | ||
1731 | ;; And now the Org-mode tables | |
72a81656 CD |
1732 | (goto-char (point-min)) |
1733 | (while (re-search-forward "^\\([ \t]*\\)|" nil t) | |
8bfe682a CD |
1734 | (org-if-unprotected-at (1- (point)) |
1735 | (org-table-align) | |
1736 | (let* ((beg (org-table-begin)) | |
1737 | (end (org-table-end)) | |
1738 | (raw-table (buffer-substring beg end)) | |
1739 | (org-table-last-alignment (copy-sequence org-table-last-alignment)) | |
1740 | (org-table-last-column-widths (copy-sequence | |
1741 | org-table-last-column-widths)) | |
1742 | fnum fields line lines olines gr colgropen line-fmt align | |
afe98dfa | 1743 | caption shortn label attr floatp placement longtblp) |
8bfe682a CD |
1744 | (if org-export-latex-tables-verbatim |
1745 | (let* ((tbl (concat "\\begin{verbatim}\n" raw-table | |
1746 | "\\end{verbatim}\n"))) | |
1747 | (apply 'delete-region (list beg end)) | |
1748 | (insert (org-export-latex-protect-string tbl))) | |
1749 | (progn | |
1750 | (setq caption (org-find-text-property-in-string | |
1751 | 'org-caption raw-table) | |
86fbb8ca CD |
1752 | shortn (org-find-text-property-in-string |
1753 | 'org-caption-shortn raw-table) | |
8bfe682a CD |
1754 | attr (org-find-text-property-in-string |
1755 | 'org-attributes raw-table) | |
1756 | label (org-find-text-property-in-string | |
1757 | 'org-label raw-table) | |
1758 | longtblp (and attr (stringp attr) | |
1759 | (string-match "\\<longtable\\>" attr)) | |
1760 | align (and attr (stringp attr) | |
86fbb8ca | 1761 | (string-match "\\<align=\\([^ \t\n\r]+\\)" attr) |
8bfe682a | 1762 | (match-string 1 attr)) |
afe98dfa CD |
1763 | floatp (or caption label) |
1764 | placement (if (and attr | |
1765 | (stringp attr) | |
1766 | (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr)) | |
1767 | (match-string 1 attr) | |
1768 | "[htb]")) | |
ed21c5c8 | 1769 | (setq caption (and caption (org-export-latex-fontify-headline caption))) |
8bfe682a CD |
1770 | (setq lines (org-split-string raw-table "\n")) |
1771 | (apply 'delete-region (list beg end)) | |
1772 | (when org-export-table-remove-special-lines | |
1773 | (setq lines (org-table-clean-before-export lines 'maybe-quoted))) | |
1774 | (when org-table-clean-did-remove-column | |
54a0dee5 CD |
1775 | (pop org-table-last-alignment) |
1776 | (pop org-table-last-column-widths)) | |
8bfe682a CD |
1777 | ;; make a formatting string to reflect alignment |
1778 | (setq olines lines) | |
1779 | (while (and (not line-fmt) (setq line (pop olines))) | |
1780 | (unless (string-match "^[ \t]*|-" line) | |
1781 | (setq fields (org-split-string line "[ \t]*|[ \t]*")) | |
1782 | (setq fnum (make-vector (length fields) 0)) | |
1783 | (setq line-fmt | |
1784 | (mapconcat | |
1785 | (lambda (x) | |
1786 | (setq gr (pop org-table-colgroup-info)) | |
1787 | (format "%s%%s%s" | |
1788 | (cond ((eq gr :start) | |
1789 | (prog1 (if colgropen "|" "|") | |
1790 | (setq colgropen t))) | |
1791 | ((eq gr :startend) | |
1792 | (prog1 (if colgropen "|" "|") | |
1793 | (setq colgropen nil))) | |
1794 | (t "")) | |
1795 | (if (memq gr '(:end :startend)) | |
1796 | (progn (setq colgropen nil) "|") | |
1797 | ""))) | |
1798 | fnum "")))) | |
1799 | ;; fix double || in line-fmt | |
1800 | (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt)) | |
1801 | ;; maybe remove the first and last "|" | |
1802 | (when (and (not org-export-latex-tables-column-borders) | |
1803 | (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt)) | |
1804 | (setq line-fmt (match-string 2 line-fmt))) | |
1805 | ;; format alignment | |
1806 | (unless align | |
1807 | (setq align (apply 'format | |
1808 | (cons line-fmt | |
1809 | (mapcar (lambda (x) (if x "r" "l")) | |
1810 | org-table-last-alignment))))) | |
1811 | ;; prepare the table to send to orgtbl-to-latex | |
1812 | (setq lines | |
1813 | (mapcar | |
1814 | (lambda(elem) | |
1815 | (or (and (string-match "[ \t]*|-+" elem) 'hline) | |
1816 | (org-split-string (org-trim elem) "|"))) | |
1817 | lines)) | |
1818 | (when insert | |
1819 | (insert (org-export-latex-protect-string | |
1820 | (concat | |
1821 | (if longtblp | |
1822 | (concat "\\begin{longtable}{" align "}\n") | |
afe98dfa | 1823 | (if floatp (format "\\begin{table}%s\n" placement))) |
ed21c5c8 | 1824 | (if floatp |
8bfe682a | 1825 | (format |
afe98dfa | 1826 | "\\caption%s{%s} %s" |
86fbb8ca | 1827 | (if shortn (concat "[" shortn "]") "") |
afe98dfa CD |
1828 | (or caption "") |
1829 | (if label (format "\\label{%s}" label) ""))) | |
ed21c5c8 | 1830 | (if (and longtblp caption) "\\\\\n" "\n") |
8bfe682a CD |
1831 | (if (and org-export-latex-tables-centered (not longtblp)) |
1832 | "\\begin{center}\n") | |
86fbb8ca CD |
1833 | (if (not longtblp) |
1834 | (format "\\begin{%s}{%s}\n" | |
1835 | org-export-latex-tabular-environment align)) | |
8bfe682a CD |
1836 | (orgtbl-to-latex |
1837 | lines | |
1838 | `(:tstart nil :tend nil | |
1839 | :hlend ,(if longtblp | |
1840 | (format "\\\\ | |
db55f368 CD |
1841 | \\hline |
1842 | \\endhead | |
1843 | \\hline\\multicolumn{%d}{r}{Continued on next page}\\ | |
1844 | \\endfoot | |
1845 | \\endlastfoot" (length org-table-last-alignment)) | |
8bfe682a | 1846 | nil))) |
86fbb8ca CD |
1847 | (if (not longtblp) |
1848 | (format "\n\\end{%s}" | |
1849 | org-export-latex-tabular-environment)) | |
8bfe682a CD |
1850 | (if longtblp "\n" (if org-export-latex-tables-centered |
1851 | "\n\\end{center}\n" "\n")) | |
1852 | (if longtblp | |
1853 | "\\end{longtable}" | |
1854 | (if floatp "\\end{table}")))) | |
1855 | "\n\n")))))))) | |
72a81656 | 1856 | |
ed21c5c8 CD |
1857 | (defun org-export-latex-convert-table.el-table () |
1858 | "Replace table.el table at point with LaTeX code." | |
86fbb8ca | 1859 | (let (tbl caption shortn label line floatp attr align rmlines) |
ed21c5c8 CD |
1860 | (setq line (buffer-substring (point-at-bol) (point-at-eol)) |
1861 | label (org-get-text-property-any 0 'org-label line) | |
1862 | caption (org-get-text-property-any 0 'org-caption line) | |
86fbb8ca | 1863 | shortn (org-get-text-property-any 0 'org-caption-shortn line) |
ed21c5c8 CD |
1864 | attr (org-get-text-property-any 0 'org-attributes line) |
1865 | align (and attr (stringp attr) | |
1866 | (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr) | |
1867 | (match-string 1 attr)) | |
1868 | rmlines (and attr (stringp attr) | |
1869 | (string-match "\\<rmlines\\>" attr)) | |
1870 | floatp (or label caption)) | |
1871 | (and (get-buffer "*org-export-table*") | |
1872 | (kill-buffer (get-buffer "*org-export-table*"))) | |
1873 | (table-generate-source 'latex "*org-export-table*" "caption") | |
1874 | (setq tbl (with-current-buffer "*org-export-table*" | |
1875 | (buffer-string))) | |
1876 | (while (string-match "^%.*\n" tbl) | |
1877 | (setq tbl (replace-match "" t t tbl))) | |
1878 | ;; fix the hlines | |
1879 | (when rmlines | |
1880 | (let ((n 0) lines) | |
1881 | (setq lines (mapcar (lambda (x) | |
1882 | (if (string-match "^\\\\hline$" x) | |
1883 | (progn | |
1884 | (setq n (1+ n)) | |
1885 | (if (= n 2) x nil)) | |
1886 | x)) | |
1887 | (org-split-string tbl "\n"))) | |
1888 | (setq tbl (mapconcat 'identity (delq nil lines) "\n")))) | |
1889 | (when (and align (string-match "\\\\begin{tabular}{.*}" tbl)) | |
1890 | (setq tbl (replace-match (concat "\\begin{tabular}{" align "}") | |
1891 | t t tbl))) | |
1892 | (and (get-buffer "*org-export-table*") | |
1893 | (kill-buffer (get-buffer "*org-export-table*"))) | |
1894 | (beginning-of-line 0) | |
1895 | (while (looking-at "[ \t]*\\(|\\|\\+-\\)") | |
1896 | (delete-region (point) (1+ (point-at-eol)))) | |
1897 | (when org-export-latex-tables-centered | |
1898 | (setq tbl (concat "\\begin{center}\n" tbl "\\end{center}"))) | |
1899 | (when floatp | |
1900 | (setq tbl (concat "\\begin{table}\n" | |
afe98dfa | 1901 | (format "\\caption%s{%s}%s\n" |
86fbb8ca | 1902 | (if shortn (format "[%s]" shortn) "") |
ed21c5c8 CD |
1903 | (if label (format "\\label{%s}" label) "") |
1904 | (or caption "")) | |
1905 | tbl | |
1906 | "\n\\end{table}\n"))) | |
1907 | (insert (org-export-latex-protect-string tbl)))) | |
1908 | ||
72a81656 CD |
1909 | (defun org-export-latex-fontify () |
1910 | "Convert fontification to LaTeX." | |
1911 | (goto-char (point-min)) | |
1912 | (while (re-search-forward org-emph-re nil t) | |
5dec9555 | 1913 | ;; The match goes one char after the *string*, except at the end of a line |
db9c3fb1 CD |
1914 | (let ((emph (assoc (match-string 3) |
1915 | org-export-latex-emphasis-alist)) | |
0bd48b37 CD |
1916 | (beg (match-beginning 0)) |
1917 | (end (match-end 0)) | |
8bfe682a | 1918 | rpl s) |
c8d0cf5c CD |
1919 | (unless emph |
1920 | (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\"" | |
1921 | (match-string 3))) | |
8bfe682a CD |
1922 | (unless (or (and (get-text-property (- (point) 2) 'org-protected) |
1923 | (not (get-text-property | |
1924 | (- (point) 2) 'org-verbatim-emph))) | |
86fbb8ca CD |
1925 | (equal (char-after (match-beginning 3)) |
1926 | (char-after (1+ (match-beginning 3)))) | |
0bd48b37 CD |
1927 | (save-excursion |
1928 | (goto-char (match-beginning 1)) | |
1929 | (save-match-data | |
1930 | (and (org-at-table-p) | |
1931 | (string-match | |
ed21c5c8 CD |
1932 | "[|\n]" (buffer-substring beg end))))) |
1933 | (and (equal (match-string 3) "+") | |
1934 | (save-match-data | |
1935 | (string-match "\\`-+\\'" (match-string 4))))) | |
8bfe682a | 1936 | (setq s (match-string 4)) |
15841868 | 1937 | (setq rpl (concat (match-string 1) |
c8d0cf5c CD |
1938 | (org-export-latex-emph-format (cadr emph) |
1939 | (match-string 4)) | |
15841868 | 1940 | (match-string 5))) |
db9c3fb1 | 1941 | (if (caddr emph) |
8bfe682a CD |
1942 | (setq rpl (org-export-latex-protect-string rpl)) |
1943 | (save-match-data | |
ed21c5c8 | 1944 | (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl) |
8bfe682a CD |
1945 | (progn |
1946 | (add-text-properties (match-beginning 1) (match-end 1) | |
1947 | '(org-protected t) rpl) | |
1948 | (add-text-properties (match-beginning 3) (match-end 3) | |
1949 | '(org-protected t) rpl))))) | |
15841868 JW |
1950 | (replace-match rpl t t))) |
1951 | (backward-char))) | |
72a81656 | 1952 | |
c8d0cf5c CD |
1953 | (defvar org-export-latex-use-verb nil) |
1954 | (defun org-export-latex-emph-format (format string) | |
1955 | "Format an emphasis string and handle the \\verb special case." | |
1956 | (when (equal format "\\verb") | |
1957 | (save-match-data | |
1958 | (if org-export-latex-use-verb | |
1959 | (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}")) | |
1960 | (catch 'exit | |
1961 | (loop for i from 0 to (1- (length ll)) do | |
1962 | (if (not (string-match (regexp-quote (substring ll i (1+ i))) | |
1963 | string)) | |
1964 | (progn | |
1965 | (setq format (concat "\\verb" (substring ll i (1+ i)) | |
1966 | "%s" (substring ll i (1+ i)))) | |
1967 | (throw 'exit nil)))))) | |
1968 | (let ((start 0) | |
8d642074 CD |
1969 | (trans '(("\\" . "\\textbackslash{}") |
1970 | ("~" . "\\textasciitilde{}") | |
1971 | ("^" . "\\textasciicircum{}"))) | |
c8d0cf5c CD |
1972 | (rtn "") char) |
1973 | (while (string-match "[\\{}$%&_#~^]" string) | |
1974 | (setq char (match-string 0 string)) | |
1975 | (if (> (match-beginning 0) 0) | |
1976 | (setq rtn (concat rtn (substring string | |
1977 | 0 (match-beginning 0))))) | |
1978 | (setq string (substring string (1+ (match-beginning 0)))) | |
1979 | (setq char (or (cdr (assoc char trans)) (concat "\\" char)) | |
1980 | rtn (concat rtn char))) | |
1981 | (setq string (concat rtn string) format "\\texttt{%s}"))))) | |
8bfe682a | 1982 | (format format string)) |
c8d0cf5c | 1983 | |
72a81656 CD |
1984 | (defun org-export-latex-links () |
1985 | ;; Make sure to use the LaTeX hyperref and graphicx package | |
1986 | ;; or send some warnings. | |
1987 | "Convert links to LaTeX." | |
1988 | (goto-char (point-min)) | |
0bd48b37 | 1989 | (while (re-search-forward org-bracket-link-analytic-regexp++ nil t) |
8bfe682a | 1990 | (org-if-unprotected-1 |
72a81656 | 1991 | (goto-char (match-beginning 0)) |
db9c3fb1 | 1992 | (let* ((re-radio org-export-latex-all-targets-re) |
72a81656 | 1993 | (remove (list (match-beginning 0) (match-end 0))) |
2c3ad40d | 1994 | (raw-path (org-extract-attributes (match-string 3))) |
72a81656 CD |
1995 | (full-raw-path (concat (match-string 1) raw-path)) |
1996 | (desc (match-string 5)) | |
db55f368 CD |
1997 | (type (or (match-string 2) |
1998 | (if (or (file-name-absolute-p raw-path) | |
1999 | (string-match "^\\.\\.?/" raw-path)) | |
2000 | "file"))) | |
0bd48b37 | 2001 | (coderefp (equal type "coderef")) |
db55f368 | 2002 | (caption (org-find-text-property-in-string 'org-caption raw-path)) |
86fbb8ca | 2003 | (shortn (org-find-text-property-in-string 'org-caption-shortn raw-path)) |
c8d0cf5c CD |
2004 | (attr (or (org-find-text-property-in-string 'org-attributes raw-path) |
2005 | (plist-get org-export-latex-options-plist :latex-image-options))) | |
db55f368 | 2006 | (label (org-find-text-property-in-string 'org-label raw-path)) |
86fbb8ca | 2007 | imgp radiop fnc |
72a81656 CD |
2008 | ;; define the path of the link |
2009 | (path (cond | |
0bd48b37 CD |
2010 | ((member type '("coderef")) |
2011 | raw-path) | |
72a81656 CD |
2012 | ((member type '("http" "https" "ftp")) |
2013 | (concat type ":" raw-path)) | |
2014 | ((and re-radio (string-match re-radio raw-path)) | |
33306645 | 2015 | (setq radiop t)) |
72a81656 CD |
2016 | ((equal type "mailto") |
2017 | (concat type ":" raw-path)) | |
2018 | ((equal type "file") | |
0bd48b37 CD |
2019 | (if (and (org-file-image-p |
2020 | (expand-file-name | |
2021 | raw-path) | |
2022 | org-export-latex-inline-image-extensions) | |
c8d0cf5c CD |
2023 | (or (get-text-property 0 'org-no-description |
2024 | raw-path) | |
2025 | (equal desc full-raw-path))) | |
72a81656 CD |
2026 | (setq imgp t) |
2027 | (progn (when (string-match "\\(.+\\)::.+" raw-path) | |
2028 | (setq raw-path (match-string 1 raw-path))) | |
2029 | (if (file-exists-p raw-path) | |
2030 | (concat type "://" (expand-file-name raw-path)) | |
2031 | (concat type "://" (org-export-directory | |
db9c3fb1 | 2032 | :LaTeX org-export-latex-options-plist) |
72a81656 CD |
2033 | raw-path)))))))) |
2034 | ;; process with link inserting | |
2035 | (apply 'delete-region remove) | |
ed21c5c8 | 2036 | (setq caption (and caption (org-export-latex-fontify-headline caption))) |
8bfe682a CD |
2037 | (cond ((and imgp |
2038 | (plist-get org-export-latex-options-plist :inline-images)) | |
2039 | ;; OK, we need to inline an image | |
db55f368 | 2040 | (insert |
86fbb8ca | 2041 | (org-export-latex-format-image raw-path caption label attr shortn))) |
0bd48b37 CD |
2042 | (coderefp |
2043 | (insert (format | |
2044 | (org-export-get-coderef-format path desc) | |
2045 | (cdr (assoc path org-export-code-refs))))) | |
acedf35c | 2046 | (radiop (insert (format org-export-latex-hyperref-format |
621f83e4 CD |
2047 | (org-solidify-link-text raw-path) desc))) |
2048 | ((not type) | |
acedf35c | 2049 | (insert (format org-export-latex-hyperref-format |
c8d0cf5c | 2050 | (org-remove-initial-hash |
54a0dee5 CD |
2051 | (org-solidify-link-text raw-path)) |
2052 | desc))) | |
ed21c5c8 | 2053 | (path |
54a0dee5 CD |
2054 | (when (org-at-table-p) |
2055 | ;; There is a strange problem when we have a link in a table, | |
2056 | ;; ampersands then cause a problem. I think this must be | |
2057 | ;; a LaTeX issue, but we here implement a work-around anyway. | |
2058 | (setq path (org-export-latex-protect-amp path) | |
2059 | desc (org-export-latex-protect-amp desc))) | |
acedf35c | 2060 | (insert (format org-export-latex-href-format path desc))) |
86fbb8ca CD |
2061 | |
2062 | ((functionp (setq fnc (nth 2 (assoc type org-link-protocols)))) | |
2063 | ;; The link protocol has a function for formatting the link | |
2064 | (insert | |
2065 | (save-match-data | |
2066 | (funcall fnc (org-link-unescape raw-path) desc 'latex)))) | |
2067 | ||
72a81656 CD |
2068 | (t (insert "\\texttt{" desc "}"))))))) |
2069 | ||
8bfe682a | 2070 | |
86fbb8ca | 2071 | (defun org-export-latex-format-image (path caption label attr &optional shortn) |
8bfe682a | 2072 | "Format the image element, depending on user settings." |
86fbb8ca | 2073 | (let (ind floatp wrapp multicolumnp placement figenv) |
8bfe682a | 2074 | (setq floatp (or caption label)) |
ed21c5c8 | 2075 | (setq ind (org-get-text-property-any 0 'original-indentation path)) |
8bfe682a CD |
2076 | (when (and attr (stringp attr)) |
2077 | (if (string-match "[ \t]*\\<wrap\\>" attr) | |
2078 | (setq wrapp t floatp nil attr (replace-match "" t t attr))) | |
2079 | (if (string-match "[ \t]*\\<float\\>" attr) | |
86fbb8ca CD |
2080 | (setq wrapp nil floatp t attr (replace-match "" t t attr))) |
2081 | (if (string-match "[ \t]*\\<multicolumn\\>" attr) | |
2082 | (setq multicolumnp t attr (replace-match "" t t attr)))) | |
ed21c5c8 | 2083 | |
8bfe682a CD |
2084 | (setq placement |
2085 | (cond | |
2086 | (wrapp "{l}{0.5\\textwidth}") | |
2087 | (floatp "[htb]") | |
2088 | (t ""))) | |
2089 | ||
2090 | (when (and attr (stringp attr) | |
2091 | (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr)) | |
2092 | (setq placement (match-string 1 attr) | |
2093 | attr (replace-match "" t t attr))) | |
2094 | (setq attr (and attr (org-trim attr))) | |
2095 | (when (or (not attr) (= (length attr) 0)) | |
2096 | (setq attr (cond (floatp "width=0.7\\textwidth") | |
2097 | (wrapp "width=0.48\\textwidth") | |
2098 | (t attr)))) | |
2099 | (setq figenv | |
2100 | (cond | |
2101 | (wrapp "\\begin{wrapfigure}%placement | |
2102 | \\centering | |
2103 | \\includegraphics[%attr]{%path} | |
86fbb8ca | 2104 | \\caption%shortn{%labelcmd%caption} |
8bfe682a | 2105 | \\end{wrapfigure}") |
86fbb8ca CD |
2106 | (multicolumnp "\\begin{figure*}%placement |
2107 | \\centering | |
2108 | \\includegraphics[%attr]{%path} | |
2109 | \\caption{%labelcmd%caption} | |
2110 | \\end{figure*}") | |
8bfe682a CD |
2111 | (floatp "\\begin{figure}%placement |
2112 | \\centering | |
2113 | \\includegraphics[%attr]{%path} | |
2114 | \\caption{%labelcmd%caption} | |
2115 | \\end{figure}") | |
2116 | (t "\\includegraphics[%attr]{%path}"))) | |
2117 | ||
ed21c5c8 CD |
2118 | |
2119 | (setq figenv (mapconcat 'identity (split-string figenv "\n") | |
2120 | (save-excursion (beginning-of-line 1) | |
2121 | (looking-at "[ \t]*") | |
2122 | (concat "\n" (match-string 0))))) | |
2123 | ||
8bfe682a CD |
2124 | (if (and (not label) (not caption) |
2125 | (string-match "^\\\\caption{.*\n" figenv)) | |
2126 | (setq figenv (replace-match "" t t figenv))) | |
ed21c5c8 CD |
2127 | (org-add-props |
2128 | (org-fill-template | |
2129 | figenv | |
2130 | (list (cons "path" | |
2131 | (if (file-name-absolute-p path) | |
2132 | (expand-file-name path) | |
2133 | path)) | |
2134 | (cons "attr" attr) | |
86fbb8ca | 2135 | (cons "shortn" (if shortn (format "[%s]" shortn) "")) |
ed21c5c8 CD |
2136 | (cons "labelcmd" (if label (format "\\label{%s}" |
2137 | label)"")) | |
2138 | (cons "caption" (or caption "")) | |
2139 | (cons "placement" (or placement "")))) | |
2140 | nil 'original-indentation ind))) | |
8bfe682a | 2141 | |
54a0dee5 CD |
2142 | (defun org-export-latex-protect-amp (s) |
2143 | (while (string-match "\\([^\\\\]\\)\\(&\\)" s) | |
2144 | (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s)) | |
2145 | t t s))) | |
2146 | s) | |
2147 | ||
c8d0cf5c CD |
2148 | (defun org-remove-initial-hash (s) |
2149 | (if (string-match "\\`#" s) | |
2150 | (substring s 1) | |
2151 | s)) | |
db9c3fb1 | 2152 | (defvar org-latex-entities) ; defined below |
71d35b24 | 2153 | (defvar org-latex-entities-regexp) ; defined below |
db9c3fb1 | 2154 | |
c8d0cf5c | 2155 | (defun org-export-latex-preprocess (parameters) |
72a81656 | 2156 | "Clean stuff in the LaTeX export." |
0b8568f5 | 2157 | ;; Preserve line breaks |
72a81656 CD |
2158 | (goto-char (point-min)) |
2159 | (while (re-search-forward "\\\\\\\\" nil t) | |
2160 | (add-text-properties (match-beginning 0) (match-end 0) | |
2161 | '(org-protected t))) | |
2162 | ||
621f83e4 CD |
2163 | ;; Preserve latex environments |
2164 | (goto-char (point-min)) | |
c8d0cf5c | 2165 | (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t) |
86fbb8ca CD |
2166 | (org-if-unprotected |
2167 | (let* ((start (progn (beginning-of-line) (point))) | |
c8d0cf5c CD |
2168 | (end (and (re-search-forward |
2169 | (concat "^[ \t]*\\\\end{" | |
2170 | (regexp-quote (match-string 1)) | |
2171 | "}") nil t) | |
2172 | (point-at-eol)))) | |
2173 | (if end | |
2174 | (add-text-properties start end '(org-protected t)) | |
86fbb8ca | 2175 | (goto-char (point-at-eol)))))) |
621f83e4 | 2176 | |
0bd48b37 CD |
2177 | ;; Preserve math snippets |
2178 | ||
2179 | (let* ((matchers (plist-get org-format-latex-options :matchers)) | |
2180 | (re-list org-latex-regexps) | |
2181 | beg end re e m n block off) | |
2182 | ;; Check the different regular expressions | |
2183 | (while (setq e (pop re-list)) | |
2184 | (setq m (car e) re (nth 1 e) n (nth 2 e) | |
2185 | block (if (nth 3 e) "\n\n" "")) | |
2186 | (setq off (if (member m '("$" "$1")) 1 0)) | |
2187 | (when (and (member m matchers) (not (equal m "begin"))) | |
2188 | (goto-char (point-min)) | |
2189 | (while (re-search-forward re nil t) | |
2190 | (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0)) | |
2191 | (add-text-properties beg end '(org-protected t org-latex-math t)))))) | |
2192 | ||
ed21c5c8 | 2193 | ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{} |
72a81656 | 2194 | (goto-char (point-min)) |
65c439fd | 2195 | (let ((case-fold-search nil)) |
ed21c5c8 CD |
2196 | (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t) |
2197 | (unless (eq (char-before (match-beginning 1)) ?\\) | |
2198 | (org-if-unprotected-1 | |
2199 | (replace-match (org-export-latex-protect-string | |
2200 | (concat "\\" (match-string 1) | |
2201 | "{}")) t t))))) | |
72a81656 | 2202 | |
b349f79f CD |
2203 | ;; Convert blockquotes |
2204 | (goto-char (point-min)) | |
621f83e4 | 2205 | (while (search-forward "ORG-BLOCKQUOTE-START" nil t) |
c8d0cf5c | 2206 | (org-replace-match-keep-properties "\\begin{quote}" t t)) |
b349f79f | 2207 | (goto-char (point-min)) |
621f83e4 | 2208 | (while (search-forward "ORG-BLOCKQUOTE-END" nil t) |
c8d0cf5c | 2209 | (org-replace-match-keep-properties "\\end{quote}" t t)) |
b349f79f CD |
2210 | |
2211 | ;; Convert verse | |
2212 | (goto-char (point-min)) | |
621f83e4 | 2213 | (while (search-forward "ORG-VERSE-START" nil t) |
c8d0cf5c CD |
2214 | (org-replace-match-keep-properties "\\begin{verse}" t t) |
2215 | (beginning-of-line 2) | |
2216 | (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp))) | |
2217 | (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)") | |
2218 | (goto-char (match-end 1)) | |
2219 | (org-replace-match-keep-properties | |
2220 | (org-export-latex-protect-string | |
2221 | (concat "\\hspace*{1cm}" (match-string 2))) t t) | |
2222 | (beginning-of-line 1)) | |
8d642074 | 2223 | (if (looking-at "[ \t]*$") |
8bfe682a | 2224 | (insert (org-export-latex-protect-string "\\vspace*{1em}")) |
8d642074 CD |
2225 | (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$") |
2226 | (end-of-line 1) | |
2227 | (insert "\\\\"))) | |
c8d0cf5c CD |
2228 | (beginning-of-line 2)) |
2229 | (and (looking-at "[ \t]*ORG-VERSE-END.*") | |
2230 | (org-replace-match-keep-properties "\\end{verse}" t t))) | |
2231 | ||
2232 | ;; Convert center | |
2233 | (goto-char (point-min)) | |
2234 | (while (search-forward "ORG-CENTER-START" nil t) | |
2235 | (org-replace-match-keep-properties "\\begin{center}" t t)) | |
b349f79f | 2236 | (goto-char (point-min)) |
c8d0cf5c CD |
2237 | (while (search-forward "ORG-CENTER-END" nil t) |
2238 | (org-replace-match-keep-properties "\\end{center}" t t)) | |
2239 | ||
2240 | (run-hooks 'org-export-latex-after-blockquotes-hook) | |
b349f79f | 2241 | |
0b8568f5 | 2242 | ;; Convert horizontal rules |
72a81656 CD |
2243 | (goto-char (point-min)) |
2244 | (while (re-search-forward "^----+.$" nil t) | |
0bd48b37 CD |
2245 | (org-if-unprotected |
2246 | (replace-match (org-export-latex-protect-string "\\hrule") t t))) | |
72a81656 | 2247 | |
33306645 | 2248 | ;; Protect LaTeX commands like \command[...]{...} or \command{...} |
ed21c5c8 CD |
2249 | (goto-char (point-min)) |
2250 | (let ((re (concat | |
2251 | "\\\\\\([a-zA-Z]+\\)" | |
2252 | "\\(?:<[^<>\n]*>\\)*" | |
2253 | "\\(?:\\[[^][\n]*?\\]\\)*" | |
2254 | "\\(?:<[^<>\n]*>\\)*" | |
2255 | "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}"))) | |
c8d0cf5c | 2256 | (while (re-search-forward re nil t) |
86fbb8ca | 2257 | (unless (or |
ed21c5c8 CD |
2258 | ;; check for comment line |
2259 | (save-excursion (goto-char (match-beginning 0)) | |
86fbb8ca | 2260 | (org-in-indented-comment-line)) |
ed21c5c8 CD |
2261 | ;; Check if this is a defined entity, so that is may need conversion |
2262 | (org-entity-get (match-string 1)) | |
2263 | ) | |
8d642074 CD |
2264 | (add-text-properties (match-beginning 0) (match-end 0) |
2265 | '(org-protected t))))) | |
d5098885 | 2266 | |
86fbb8ca CD |
2267 | ;; Special case for \nbsp |
2268 | (goto-char (point-min)) | |
2269 | (while (re-search-forward "\\\\nbsp\\({}\\|\\>\\)" nil t) | |
2270 | (org-if-unprotected | |
2271 | (replace-match (org-export-latex-protect-string "~")))) | |
2272 | ||
db9c3fb1 CD |
2273 | ;; Protect LaTeX entities |
2274 | (goto-char (point-min)) | |
86fbb8ca CD |
2275 | (while (re-search-forward org-latex-entities-regexp nil t) |
2276 | (org-if-unprotected | |
2277 | (add-text-properties (match-beginning 0) (match-end 0) | |
2278 | '(org-protected t)))) | |
db9c3fb1 | 2279 | |
72a81656 CD |
2280 | ;; Replace radio links |
2281 | (goto-char (point-min)) | |
d5098885 | 2282 | (while (re-search-forward |
db9c3fb1 | 2283 | (concat "<<<?" org-export-latex-all-targets-re |
d5098885 | 2284 | ">>>?\\((INVISIBLE)\\)?") nil t) |
8bfe682a | 2285 | (org-if-unprotected-at (+ (match-beginning 0) 2) |
0bd48b37 | 2286 | (replace-match |
ed21c5c8 CD |
2287 | (concat |
2288 | (org-export-latex-protect-string | |
2289 | (format "\\label{%s}" (save-match-data (org-solidify-link-text | |
2290 | (match-string 1))))) | |
2291 | (if (match-string 2) "" (match-string 1))) | |
2292 | t t))) | |
d5098885 | 2293 | |
0b8568f5 | 2294 | ;; Delete @<...> constructs |
0b8568f5 | 2295 | ;; Thanks to Daniel Clemente for this regexp |
4b67ea89 | 2296 | (goto-char (point-min)) |
0b8568f5 | 2297 | (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t) |
0bd48b37 CD |
2298 | (org-if-unprotected |
2299 | (replace-match ""))) | |
d5098885 | 2300 | |
0b8568f5 JW |
2301 | ;; When converting to LaTeX, replace footnotes |
2302 | ;; FIXME: don't protect footnotes from conversion | |
db9c3fb1 | 2303 | (when (plist-get org-export-latex-options-plist :footnotes) |
0b8568f5 | 2304 | (goto-char (point-min)) |
0bd48b37 CD |
2305 | (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t) |
2306 | (org-if-unprotected | |
8bfe682a CD |
2307 | (when (and (save-match-data |
2308 | (save-excursion (beginning-of-line) | |
2309 | (looking-at "[^:|#]"))) | |
2310 | (not (org-in-verbatim-emphasis))) | |
0bd48b37 CD |
2311 | (let ((foot-beg (match-beginning 0)) |
2312 | (foot-end (match-end 0)) | |
2313 | (foot-prefix (match-string 0)) | |
2314 | footnote footnote-rpl) | |
2315 | (save-excursion | |
2316 | (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix)) | |
2317 | nil t)) | |
5dec9555 CD |
2318 | (replace-match (org-export-latex-protect-string |
2319 | (concat "$^{" (match-string 1) "}$"))) | |
0bd48b37 CD |
2320 | (replace-match "") |
2321 | (let ((end (save-excursion | |
2322 | (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t) | |
2323 | (match-beginning 0) (point-max))))) | |
2324 | (setq footnote (concat (org-trim (buffer-substring (point) end)) | |
2325 | " ")) ; prevent last } being part of a link | |
2326 | (delete-region (point) end)) | |
2327 | (goto-char foot-beg) | |
2328 | (delete-region foot-beg foot-end) | |
2329 | (unless (null footnote) | |
2330 | (setq footnote-rpl (format "\\footnote{%s}" footnote)) | |
2331 | (add-text-properties 0 10 '(org-protected t) footnote-rpl) | |
2332 | (add-text-properties (1- (length footnote-rpl)) | |
2333 | (length footnote-rpl) | |
2334 | '(org-protected t) footnote-rpl) | |
ed21c5c8 CD |
2335 | (if (org-on-heading-p) |
2336 | (setq footnote-rpl | |
2337 | (concat (org-export-latex-protect-string "\\protect") | |
2338 | footnote-rpl))) | |
0bd48b37 CD |
2339 | (insert footnote-rpl))) |
2340 | ))))) | |
2341 | ||
2342 | ;; Remove footnote section tag for LaTeX | |
0b8568f5 | 2343 | (goto-char (point-min)) |
db9c3fb1 | 2344 | (while (re-search-forward |
0b8568f5 | 2345 | (concat "^" footnote-section-tag-regexp) nil t) |
0bd48b37 CD |
2346 | (org-if-unprotected |
2347 | (replace-match ""))))) | |
72a81656 | 2348 | |
ed21c5c8 | 2349 | (defun org-export-latex-fix-inputenc () |
86fbb8ca | 2350 | "Set the coding system in inputenc to what the buffer is." |
ed21c5c8 CD |
2351 | (let* ((cs buffer-file-coding-system) |
2352 | (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs)) | |
2353 | "utf8"))) | |
2354 | (when opt | |
2355 | ;; Translate if that is requested | |
2356 | (setq opt (or (cdr (assoc opt org-export-latex-inputenc-alist)) opt)) | |
2357 | ;; find the \usepackage statement and replace the option | |
2358 | (goto-char (point-min)) | |
2359 | (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}" | |
2360 | nil t) | |
2361 | (goto-char (match-beginning 1)) | |
2362 | (delete-region (match-beginning 1) (match-end 1)) | |
2363 | (insert opt)) | |
2364 | (and buffer-file-name | |
2365 | (save-buffer))))) | |
2366 | ||
db9c3fb1 CD |
2367 | ;;; List handling: |
2368 | ||
2369 | (defun org-export-latex-lists () | |
0bd48b37 | 2370 | "Convert plain text lists in current buffer into LaTeX lists." |
86fbb8ca CD |
2371 | (let (res) |
2372 | (goto-char (point-min)) | |
afe98dfa | 2373 | (while (org-search-forward-unenclosed org-item-beginning-re nil t) |
86fbb8ca CD |
2374 | (beginning-of-line) |
2375 | (setq res (org-list-to-latex (org-list-parse-list t) | |
2376 | org-export-latex-list-parameters)) | |
afe98dfa | 2377 | (while (string-match "^\\(\\\\item[ \t]+\\)\\[@\\(?:start:\\)?\\([0-9]+\\)\\]" |
86fbb8ca CD |
2378 | res) |
2379 | (setq res (replace-match | |
2380 | (concat (format "\\setcounter{enumi}{%d}" | |
2381 | (1- (string-to-number | |
2382 | (match-string 2 res)))) | |
2383 | "\n" | |
2384 | (match-string 1 res)) | |
2385 | t t res))) | |
acedf35c | 2386 | (insert res)))) |
db9c3fb1 | 2387 | |
db9c3fb1 CD |
2388 | (defconst org-latex-entities |
2389 | '("\\!" | |
2390 | "\\'" | |
2391 | "\\+" | |
2392 | "\\," | |
2393 | "\\-" | |
2394 | "\\:" | |
2395 | "\\;" | |
2396 | "\\<" | |
2397 | "\\=" | |
2398 | "\\>" | |
2399 | "\\Huge" | |
2400 | "\\LARGE" | |
2401 | "\\Large" | |
2402 | "\\Styles" | |
2403 | "\\\\" | |
2404 | "\\`" | |
2405 | "\\addcontentsline" | |
2406 | "\\address" | |
2407 | "\\addtocontents" | |
2408 | "\\addtocounter" | |
2409 | "\\addtolength" | |
2410 | "\\addvspace" | |
2411 | "\\alph" | |
2412 | "\\appendix" | |
2413 | "\\arabic" | |
2414 | "\\author" | |
2415 | "\\begin{array}" | |
2416 | "\\begin{center}" | |
2417 | "\\begin{description}" | |
2418 | "\\begin{enumerate}" | |
2419 | "\\begin{eqnarray}" | |
2420 | "\\begin{equation}" | |
2421 | "\\begin{figure}" | |
2422 | "\\begin{flushleft}" | |
2423 | "\\begin{flushright}" | |
2424 | "\\begin{itemize}" | |
2425 | "\\begin{list}" | |
2426 | "\\begin{minipage}" | |
2427 | "\\begin{picture}" | |
2428 | "\\begin{quotation}" | |
2429 | "\\begin{quote}" | |
2430 | "\\begin{tabbing}" | |
2431 | "\\begin{table}" | |
2432 | "\\begin{tabular}" | |
2433 | "\\begin{thebibliography}" | |
2434 | "\\begin{theorem}" | |
2435 | "\\begin{titlepage}" | |
2436 | "\\begin{verbatim}" | |
2437 | "\\begin{verse}" | |
2438 | "\\bf" | |
2439 | "\\bf" | |
2440 | "\\bibitem" | |
2441 | "\\bigskip" | |
2442 | "\\cdots" | |
2443 | "\\centering" | |
2444 | "\\circle" | |
2445 | "\\cite" | |
2446 | "\\cleardoublepage" | |
2447 | "\\clearpage" | |
2448 | "\\cline" | |
2449 | "\\closing" | |
2450 | "\\dashbox" | |
2451 | "\\date" | |
2452 | "\\ddots" | |
2453 | "\\dotfill" | |
2454 | "\\em" | |
2455 | "\\fbox" | |
2456 | "\\flushbottom" | |
2457 | "\\fnsymbol" | |
2458 | "\\footnote" | |
2459 | "\\footnotemark" | |
2460 | "\\footnotesize" | |
2461 | "\\footnotetext" | |
2462 | "\\frac" | |
2463 | "\\frame" | |
2464 | "\\framebox" | |
2465 | "\\hfill" | |
2466 | "\\hline" | |
2467 | "\\hrulespace" | |
2468 | "\\hspace" | |
2469 | "\\huge" | |
2470 | "\\hyphenation" | |
2471 | "\\include" | |
2472 | "\\includeonly" | |
2473 | "\\indent" | |
2474 | "\\input" | |
2475 | "\\it" | |
2476 | "\\kill" | |
2477 | "\\label" | |
2478 | "\\large" | |
2479 | "\\ldots" | |
2480 | "\\line" | |
2481 | "\\linebreak" | |
2482 | "\\linethickness" | |
2483 | "\\listoffigures" | |
2484 | "\\listoftables" | |
2485 | "\\location" | |
2486 | "\\makebox" | |
2487 | "\\maketitle" | |
2488 | "\\mark" | |
2489 | "\\mbox" | |
2490 | "\\medskip" | |
2491 | "\\multicolumn" | |
2492 | "\\multiput" | |
2493 | "\\newcommand" | |
2494 | "\\newcounter" | |
2495 | "\\newenvironment" | |
2496 | "\\newfont" | |
2497 | "\\newlength" | |
2498 | "\\newline" | |
2499 | "\\newpage" | |
2500 | "\\newsavebox" | |
2501 | "\\newtheorem" | |
2502 | "\\nocite" | |
2503 | "\\nofiles" | |
2504 | "\\noindent" | |
2505 | "\\nolinebreak" | |
2506 | "\\nopagebreak" | |
2507 | "\\normalsize" | |
2508 | "\\onecolumn" | |
2509 | "\\opening" | |
2510 | "\\oval" | |
2511 | "\\overbrace" | |
2512 | "\\overline" | |
2513 | "\\pagebreak" | |
2514 | "\\pagenumbering" | |
2515 | "\\pageref" | |
2516 | "\\pagestyle" | |
2517 | "\\par" | |
2518 | "\\parbox" | |
2519 | "\\put" | |
2520 | "\\raggedbottom" | |
2521 | "\\raggedleft" | |
2522 | "\\raggedright" | |
2523 | "\\raisebox" | |
2524 | "\\ref" | |
2525 | "\\rm" | |
2526 | "\\roman" | |
2527 | "\\rule" | |
2528 | "\\savebox" | |
2529 | "\\sc" | |
2530 | "\\scriptsize" | |
2531 | "\\setcounter" | |
2532 | "\\setlength" | |
2533 | "\\settowidth" | |
2534 | "\\sf" | |
2535 | "\\shortstack" | |
2536 | "\\signature" | |
2537 | "\\sl" | |
2538 | "\\small" | |
2539 | "\\smallskip" | |
2540 | "\\sqrt" | |
2541 | "\\tableofcontents" | |
2542 | "\\telephone" | |
2543 | "\\thanks" | |
2544 | "\\thispagestyle" | |
2545 | "\\tiny" | |
2546 | "\\title" | |
2547 | "\\tt" | |
2548 | "\\twocolumn" | |
2549 | "\\typein" | |
2550 | "\\typeout" | |
2551 | "\\underbrace" | |
2552 | "\\underline" | |
2553 | "\\usebox" | |
2554 | "\\usecounter" | |
2555 | "\\value" | |
2556 | "\\vdots" | |
2557 | "\\vector" | |
2558 | "\\verb" | |
2559 | "\\vfill" | |
2560 | "\\vline" | |
2561 | "\\vspace") | |
2562 | "A list of LaTeX commands to be protected when performing conversion.") | |
2563 | ||
71d35b24 CD |
2564 | (defconst org-latex-entities-regexp |
2565 | (let (names rest) | |
2566 | (dolist (x org-latex-entities) | |
8bfe682a | 2567 | (if (string-match "[a-zA-Z]$" x) |
71d35b24 CD |
2568 | (push x names) |
2569 | (push x rest))) | |
2570 | (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)" | |
2571 | "\\|\\(" (regexp-opt (nreverse rest)) "\\)"))) | |
2572 | ||
72a81656 | 2573 | (provide 'org-export-latex) |
c8d0cf5c | 2574 | (provide 'org-latex) |
72a81656 | 2575 | |
b349f79f | 2576 | |
c8d0cf5c | 2577 | ;;; org-latex.el ends here |