Commit | Line | Data |
---|---|---|
c8d0cf5c | 1 | ;;; org-latex.el --- LaTeX exporter for org-mode |
72a81656 | 2 | ;; |
5df4f04c | 3 | ;; Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
15841868 JW |
4 | ;; |
5 | ;; Emacs Lisp Archive Entry | |
c8d0cf5c | 6 | ;; Filename: org-latex.el |
5dec9555 | 7 | ;; Version: 6.33x |
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) |
72a81656 | 53 | |
0b8568f5 | 54 | ;;; Variables: |
db9c3fb1 CD |
55 | (defvar org-export-latex-class nil) |
56 | (defvar org-export-latex-header nil) | |
57 | (defvar org-export-latex-append-header nil) | |
58 | (defvar org-export-latex-options-plist nil) | |
59 | (defvar org-export-latex-todo-keywords-1 nil) | |
c8d0cf5c CD |
60 | (defvar org-export-latex-complex-heading-re nil) |
61 | (defvar org-export-latex-not-done-keywords nil) | |
62 | (defvar org-export-latex-done-keywords nil) | |
63 | (defvar org-export-latex-display-custom-times nil) | |
db9c3fb1 CD |
64 | (defvar org-export-latex-all-targets-re nil) |
65 | (defvar org-export-latex-add-level 0) | |
66 | (defvar org-export-latex-sectioning "") | |
67 | (defvar org-export-latex-sectioning-depth 0) | |
0bd48b37 CD |
68 | (defvar org-export-latex-special-keyword-regexp |
69 | (concat "\\<\\(" org-scheduled-string "\\|" | |
70 | org-deadline-string "\\|" | |
71 | org-closed-string"\\)") | |
72 | "Regexp matching special time planning keywords plus the time after it.") | |
72a81656 | 73 | |
0b8568f5 JW |
74 | (defvar latexp) ; dynamically scoped from org.el |
75 | (defvar re-quote) ; dynamically scoped from org.el | |
76 | (defvar commentsp) ; dynamically scoped from org.el | |
77 | ||
db9c3fb1 | 78 | ;;; User variables: |
72a81656 | 79 | |
621f83e4 CD |
80 | (defgroup org-export-latex nil |
81 | "Options for exporting Org-mode files to LaTeX." | |
82 | :tag "Org Export LaTeX" | |
83 | :group 'org-export) | |
84 | ||
db9c3fb1 CD |
85 | (defcustom org-export-latex-default-class "article" |
86 | "The default LaTeX class." | |
87 | :group 'org-export-latex | |
88 | :type '(string :tag "LaTeX class")) | |
72a81656 | 89 | |
db9c3fb1 CD |
90 | (defcustom org-export-latex-classes |
91 | '(("article" | |
72d06d81 | 92 | "\\documentclass[11pt]{article} |
db9c3fb1 CD |
93 | \\usepackage[utf8]{inputenc} |
94 | \\usepackage[T1]{fontenc} | |
71d35b24 | 95 | \\usepackage{graphicx} |
db55f368 | 96 | \\usepackage{longtable} |
8bfe682a CD |
97 | \\usepackage{float} |
98 | \\usepackage{wrapfig} | |
54a0dee5 | 99 | \\usepackage{soul} |
8bfe682a | 100 | \\usepackage{amssymb} |
db9c3fb1 CD |
101 | \\usepackage{hyperref}" |
102 | ("\\section{%s}" . "\\section*{%s}") | |
103 | ("\\subsection{%s}" . "\\subsection*{%s}") | |
104 | ("\\subsubsection{%s}" . "\\subsubsection*{%s}") | |
105 | ("\\paragraph{%s}" . "\\paragraph*{%s}") | |
106 | ("\\subparagraph{%s}" . "\\subparagraph*{%s}")) | |
107 | ("report" | |
72d06d81 | 108 | "\\documentclass[11pt]{report} |
db9c3fb1 CD |
109 | \\usepackage[utf8]{inputenc} |
110 | \\usepackage[T1]{fontenc} | |
71d35b24 | 111 | \\usepackage{graphicx} |
db55f368 | 112 | \\usepackage{longtable} |
8bfe682a CD |
113 | \\usepackage{float} |
114 | \\usepackage{wrapfig} | |
54a0dee5 | 115 | \\usepackage{soul} |
8bfe682a | 116 | \\usepackage{amssymb} |
db9c3fb1 CD |
117 | \\usepackage{hyperref}" |
118 | ("\\part{%s}" . "\\part*{%s}") | |
119 | ("\\chapter{%s}" . "\\chapter*{%s}") | |
120 | ("\\section{%s}" . "\\section*{%s}") | |
121 | ("\\subsection{%s}" . "\\subsection*{%s}") | |
122 | ("\\subsubsection{%s}" . "\\subsubsection*{%s}")) | |
123 | ("book" | |
72d06d81 | 124 | "\\documentclass[11pt]{book} |
db9c3fb1 CD |
125 | \\usepackage[utf8]{inputenc} |
126 | \\usepackage[T1]{fontenc} | |
71d35b24 | 127 | \\usepackage{graphicx} |
db55f368 | 128 | \\usepackage{longtable} |
8bfe682a CD |
129 | \\usepackage{float} |
130 | \\usepackage{wrapfig} | |
54a0dee5 | 131 | \\usepackage{soul} |
8bfe682a | 132 | \\usepackage{amssymb} |
db9c3fb1 CD |
133 | \\usepackage{hyperref}" |
134 | ("\\part{%s}" . "\\part*{%s}") | |
135 | ("\\chapter{%s}" . "\\chapter*{%s}") | |
136 | ("\\section{%s}" . "\\section*{%s}") | |
137 | ("\\subsection{%s}" . "\\subsection*{%s}") | |
138 | ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))) | |
139 | "Alist of LaTeX classes and associated header and structure. | |
140 | If #+LaTeX_CLASS is set in the buffer, use its value and the | |
141 | associated information. Here is the structure of each cell: | |
142 | ||
143 | \(class-name | |
144 | header-string | |
71d35b24 | 145 | (numbered-section . unnumbered-section\) |
db9c3fb1 CD |
146 | ...\) |
147 | ||
148 | A %s formatter is mandatory in each section string and will be | |
71d35b24 CD |
149 | replaced by the title of the section. |
150 | ||
151 | Instead of a cons cell (numbered . unnumbered), you can also provide a list | |
152 | of 2-4 elements, | |
153 | ||
154 | (numbered-open numbered-close) | |
155 | ||
156 | or | |
157 | ||
158 | (numbered-open numbered-close unnumbered-open unnumbered-close) | |
159 | ||
160 | providing opening and closing strings for an environment that should | |
161 | represent the document section. The opening clause should have a %s | |
162 | to represent the section title." | |
72a81656 | 163 | :group 'org-export-latex |
ff4be292 | 164 | :type '(repeat |
db9c3fb1 CD |
165 | (list (string :tag "LaTeX class") |
166 | (string :tag "LaTeX header") | |
71d35b24 CD |
167 | (repeat :tag "Levels" :inline t |
168 | (choice | |
169 | (cons :tag "Heading" | |
170 | (string :tag "numbered") | |
171 | (string :tag "unnumbered)")) | |
172 | (list :tag "Environment" | |
173 | (string :tag "Opening (numbered) ") | |
174 | (string :tag "Closing (numbered) ") | |
175 | (string :tag "Opening (unnumbered)") | |
176 | (string :tag "Closing (unnumbered)"))))))) | |
72a81656 CD |
177 | |
178 | (defcustom org-export-latex-emphasis-alist | |
15841868 JW |
179 | '(("*" "\\textbf{%s}" nil) |
180 | ("/" "\\emph{%s}" nil) | |
181 | ("_" "\\underline{%s}" nil) | |
54a0dee5 | 182 | ("+" "\\st{%s}" nil) |
c8d0cf5c CD |
183 | ("=" "\\verb" t) |
184 | ("~" "\\verb" t)) | |
15841868 JW |
185 | "Alist of LaTeX expressions to convert emphasis fontifiers. |
186 | Each element of the list is a list of three elements. | |
187 | The first element is the character used as a marker for fontification. | |
188 | The second element is a formatting string to wrap fontified text with. | |
8bfe682a | 189 | If it is \"\\verb\", Org will automatically select a delimiter |
c8d0cf5c | 190 | character that is not in the string. |
15841868 JW |
191 | The third element decides whether to protect converted text from other |
192 | conversions." | |
72a81656 CD |
193 | :group 'org-export-latex |
194 | :type 'alist) | |
195 | ||
15841868 JW |
196 | (defcustom org-export-latex-title-command "\\maketitle" |
197 | "The command used to insert the title just after \\begin{document}. | |
198 | If this string contains the formatting specification \"%s\" then | |
199 | it will be used as a formatting string, passing the title as an | |
200 | argument." | |
201 | :group 'org-export-latex | |
202 | :type 'string) | |
203 | ||
c8d0cf5c CD |
204 | (defcustom org-export-latex-import-inbuffer-stuff nil |
205 | "Non-nil means define TeX macros for Org's inbuffer definitions. | |
206 | For example \orgTITLE for #+TITLE." | |
207 | :group 'org-export-latex | |
208 | :type 'boolean) | |
209 | ||
db9c3fb1 | 210 | (defcustom org-export-latex-date-format |
d5098885 | 211 | "%d %B %Y" |
72a81656 CD |
212 | "Format string for \\date{...}." |
213 | :group 'org-export-latex | |
214 | :type 'string) | |
215 | ||
c8d0cf5c CD |
216 | (defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}" |
217 | "Markup for TODO keywords, as a printf format. | |
218 | This can be a single format for all keywords, a cons cell with separate | |
219 | formats for not-done and done states, or an association list with setup | |
220 | for individual keywords. If a keyword shows up for which there is no | |
221 | markup defined, the first one in the association list will be used." | |
222 | :group 'org-export-latex | |
223 | :type '(choice | |
224 | (string :tag "Default") | |
225 | (cons :tag "Distinguish undone and done" | |
226 | (string :tag "Not-DONE states") | |
227 | (string :tag "DONE states")) | |
228 | (repeat :tag "Per keyword markup" | |
229 | (cons | |
230 | (string :tag "Keyword") | |
231 | (string :tag "Markup"))))) | |
232 | ||
233 | (defcustom org-export-latex-timestamp-markup "\\textit{%s}" | |
234 | "A printf format string to be applied to time stamps." | |
235 | :group 'org-export-latex | |
236 | :type 'string) | |
237 | ||
238 | (defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}" | |
239 | "A printf format string to be applied to time stamps." | |
240 | :group 'org-export-latex | |
241 | :type 'string) | |
242 | ||
d5098885 | 243 | (defcustom org-export-latex-tables-verbatim nil |
621f83e4 | 244 | "When non-nil, tables are exported verbatim." |
d5098885 JW |
245 | :group 'org-export-latex |
246 | :type 'boolean) | |
247 | ||
c8d0cf5c CD |
248 | (defcustom org-export-latex-tables-centered t |
249 | "When non-nil, tables are exported in a center environment." | |
250 | :group 'org-export-latex | |
251 | :type 'boolean) | |
252 | ||
db9c3fb1 | 253 | (defcustom org-export-latex-tables-column-borders nil |
c8d0cf5c CD |
254 | "When non-nil, grouping columns can cause outer vertical lines in tables. |
255 | When nil, grouping causes only separation lines between groups." | |
db9c3fb1 CD |
256 | :group 'org-export-latex |
257 | :type 'boolean) | |
72a81656 | 258 | |
c8d0cf5c | 259 | (defcustom org-export-latex-low-levels 'itemize |
621f83e4 CD |
260 | "How to convert sections below the current level of sectioning. |
261 | This is specified by the `org-export-headline-levels' option or the | |
262 | value of \"H:\" in Org's #+OPTION line. | |
15841868 | 263 | |
c8d0cf5c CD |
264 | This can be either nil (skip the sections), `description', `itemize', |
265 | or `enumerate' (convert the sections as the corresponding list type), or | |
266 | a string to be used instead of \\section{%s}. In this latter case, | |
267 | the %s stands here for the inserted headline and is mandatory. | |
268 | ||
269 | It may also be a list of three string to define a user-defined environment | |
270 | that should be used. The first string should be the like | |
271 | \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up | |
8bfe682a | 272 | to two occurrences of %s for the title and a label, respectively. The third |
c8d0cf5c | 273 | string should be like \"\\end{itemize\"." |
72a81656 CD |
274 | :group 'org-export-latex |
275 | :type '(choice (const :tag "Ignore" nil) | |
c8d0cf5c CD |
276 | (const :tag "Convert as descriptive list" description) |
277 | (const :tag "Convert as itemized list" itemize) | |
278 | (const :tag "Convert as enumerated list" enumerate) | |
279 | (list :tag "User-defined environment" | |
280 | :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s") | |
281 | (string :tag "Start") | |
282 | (string :tag "End") | |
283 | (string :tag "item")) | |
72a81656 CD |
284 | (string :tag "Use a section string" :value "\\subparagraph{%s}"))) |
285 | ||
0bd48b37 | 286 | (defcustom org-export-latex-list-parameters |
8bfe682a | 287 | '(:cbon "$\\boxtimes$" :cboff "$\\Box$") |
0bd48b37 CD |
288 | "Parameters for the LaTeX list exporter. |
289 | These parameters will be passed on to `org-list-to-latex', which in turn | |
290 | will pass them (combined with the LaTeX default list parameters) to | |
291 | `org-list-to-generic'." | |
292 | :group 'org-export-latex | |
293 | :type 'plist) | |
294 | ||
c8d0cf5c CD |
295 | (defcustom org-export-latex-verbatim-wrap |
296 | '("\\begin{verbatim}\n" . "\\end{verbatim}\n") | |
297 | "Environment to be wrapped around a fixed-width section in LaTeX export. | |
298 | This is a cons with two strings, to be added before and after the | |
299 | fixed-with text. | |
300 | ||
301 | Defaults to \\begin{verbatim} and \\end{verbatim}." | |
302 | :group 'org-export-translation | |
303 | :group 'org-export-latex | |
304 | :type '(cons (string :tag "Open") | |
305 | (string :tag "Close"))) | |
306 | ||
54a0dee5 CD |
307 | (defcustom org-export-latex-listings nil |
308 | "Non-nil means, export source code using the listings package. | |
309 | This package will fontify source code, possibly even with color. | |
310 | If you want to use this, you also need to make LaTeX use the | |
311 | listings package, and if you want to have color, the color | |
312 | package. Just add these to `org-export-latex-packages-alist', | |
313 | for example using customize, or with something like | |
314 | ||
315 | (require 'org-latex) | |
316 | (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\")) | |
317 | (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))" | |
318 | :group 'org-export-latex | |
319 | :type 'boolean) | |
320 | ||
321 | (defcustom org-export-latex-listings-langs | |
322 | '((emacs-lisp "Lisp") (lisp "Lisp") | |
323 | (c "C") (cc "C++") | |
324 | (fortran "fortran") | |
325 | (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby") | |
326 | (html "HTML") (xml "XML") | |
327 | (tex "TeX") (latex "TeX") | |
328 | (shell-script "bash") | |
329 | (gnuplot "Gnuplot") | |
330 | (ocaml "Caml") (caml "Caml") | |
331 | (sql "SQL")) | |
332 | "Alist mapping languages to their listing language counterpart. | |
333 | The key is a symbol, the major mode symbol without the \"-mode\". | |
334 | The value is the string that should be inserted as the language parameter | |
335 | for the listings package. If the mode name and the listings name are | |
336 | the same, the language does not need an entry in this list - but it does not | |
337 | hurt if it is present." | |
338 | :group 'org-export-latex | |
339 | :type '(repeat | |
340 | (list | |
341 | (symbol :tag "Major mode ") | |
342 | (string :tag "Listings language")))) | |
343 | ||
d5098885 | 344 | (defcustom org-export-latex-remove-from-headlines |
0bd48b37 CD |
345 | '(:todo nil :priority nil :tags nil) |
346 | "A plist of keywords to remove from headlines. OBSOLETE. | |
72a81656 CD |
347 | Non-nil means remove this keyword type from the headline. |
348 | ||
0bd48b37 CD |
349 | Don't remove the keys, just change their values. |
350 | ||
351 | Obsolete, this variable is no longer used. Use the separate | |
352 | variables `org-export-with-todo-keywords', `org-export-with-priority', | |
353 | and `org-export-with-tags' instead." | |
72a81656 CD |
354 | :type 'plist |
355 | :group 'org-export-latex) | |
356 | ||
72a81656 CD |
357 | (defcustom org-export-latex-image-default-option "width=10em" |
358 | "Default option for images." | |
359 | :group 'org-export-latex | |
db9c3fb1 | 360 | :type 'string) |
72a81656 | 361 | |
0bd48b37 | 362 | (defcustom org-export-latex-inline-image-extensions |
c8d0cf5c | 363 | '("pdf" "jpeg" "jpg" "png" "ps" "eps") |
0bd48b37 | 364 | "Extensions of image files that can be inlined into LaTeX. |
c8d0cf5c CD |
365 | Note that the image extension *actually* allowed depend on the way the |
366 | LaTeX file is processed. When used with pdflatex, pdf, jpg and png images | |
367 | are OK. When processing through dvi to Postscript, only ps and eps are | |
368 | allowed. The default we use here encompasses both." | |
369 | :group 'org-export-latex | |
0bd48b37 CD |
370 | :type '(repeat (string :tag "Extension"))) |
371 | ||
72a81656 CD |
372 | (defcustom org-export-latex-coding-system nil |
373 | "Coding system for the exported LaTex file." | |
374 | :group 'org-export-latex | |
375 | :type 'coding-system) | |
376 | ||
71d35b24 CD |
377 | (defgroup org-export-pdf nil |
378 | "Options for exporting Org-mode files to PDF, via LaTeX." | |
379 | :tag "Org Export LaTeX" | |
380 | :group 'org-export-latex | |
381 | :group 'org-export) | |
382 | ||
c8d0cf5c CD |
383 | (defcustom org-latex-to-pdf-process |
384 | '("pdflatex -interaction nonstopmode %s" | |
385 | "pdflatex -interaction nonstopmode %s") | |
386 | "Commands to process a LaTeX file to a PDF file. | |
387 | This is a list of strings, each of them will be given to the shell | |
388 | as a command. %s in the command will be replaced by the full file name, %b | |
389 | by the file base name (i.e. without extension). | |
390 | The reason why this is a list is that it usually takes several runs of | |
391 | pdflatex, maybe mixed with a call to bibtex. Org does not have a clever | |
8bfe682a | 392 | mechanism to detect which of these commands have to be run to get to a stable |
c8d0cf5c CD |
393 | result, and it also does not do any error checking. |
394 | ||
395 | Alternatively, this may be a Lisp function that does the processing, so you | |
396 | could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode. | |
8bfe682a | 397 | This function should accept the file name as its single argument." |
c8d0cf5c CD |
398 | :group 'org-export-latex |
399 | :type '(choice (repeat :tag "Shell command sequence" | |
400 | (string :tag "Shell command")) | |
401 | (function))) | |
402 | ||
71d35b24 CD |
403 | (defcustom org-export-pdf-remove-logfiles t |
404 | "Non-nil means, remove the logfiles produced by PDF production. | |
405 | These are the .aux, .log, .out, and .toc files." | |
c8d0cf5c | 406 | :group 'org-export-pdf |
71d35b24 CD |
407 | :type 'boolean) |
408 | ||
c8d0cf5c CD |
409 | ;;; Hooks |
410 | ||
411 | (defvar org-export-latex-after-blockquotes-hook nil | |
412 | "Hook run during LaTeX export, after blockquote, verse, center are done.") | |
413 | ||
8d642074 CD |
414 | (defvar org-export-latex-final-hook nil |
415 | "Hook run in the finalized LaTeX buffer.") | |
416 | ||
0b8568f5 | 417 | ;;; Autoload functions: |
db9c3fb1 | 418 | |
72a81656 CD |
419 | ;;;###autoload |
420 | (defun org-export-as-latex-batch () | |
621f83e4 CD |
421 | "Call `org-export-as-latex', may be used in batch processing. |
422 | For example: | |
423 | ||
33306645 CD |
424 | emacs --batch |
425 | --load=$HOME/lib/emacs/org.el | |
426 | --eval \"(setq org-export-headline-levels 2)\" | |
427 | --visit=MyFile --funcall org-export-as-latex-batch" | |
72a81656 CD |
428 | (org-export-as-latex org-export-headline-levels 'hidden)) |
429 | ||
430 | ;;;###autoload | |
431 | (defun org-export-as-latex-to-buffer (arg) | |
33306645 | 432 | "Call `org-export-as-latex` with output to a temporary buffer. |
72a81656 CD |
433 | No file is created. The prefix ARG is passed through to `org-export-as-latex'." |
434 | (interactive "P") | |
435 | (org-export-as-latex arg nil nil "*Org LaTeX Export*") | |
c8d0cf5c CD |
436 | (when org-export-show-temporary-export-buffer |
437 | (switch-to-buffer-other-window "*Org LaTeX Export*"))) | |
72a81656 CD |
438 | |
439 | ;;;###autoload | |
440 | (defun org-replace-region-by-latex (beg end) | |
441 | "Replace the region from BEG to END with its LaTeX export. | |
442 | It assumes the region has `org-mode' syntax, and then convert it to | |
db9c3fb1 | 443 | LaTeX. This can be used in any buffer. For example, you could |
72a81656 CD |
444 | write an itemized list in `org-mode' syntax in an LaTeX buffer and |
445 | then use this command to convert it." | |
446 | (interactive "r") | |
447 | (let (reg latex buf) | |
448 | (save-window-excursion | |
449 | (if (org-mode-p) | |
450 | (setq latex (org-export-region-as-latex | |
451 | beg end t 'string)) | |
452 | (setq reg (buffer-substring beg end) | |
453 | buf (get-buffer-create "*Org tmp*")) | |
81ad75af | 454 | (with-current-buffer buf |
72a81656 CD |
455 | (erase-buffer) |
456 | (insert reg) | |
457 | (org-mode) | |
458 | (setq latex (org-export-region-as-latex | |
459 | (point-min) (point-max) t 'string))) | |
460 | (kill-buffer buf))) | |
461 | (delete-region beg end) | |
462 | (insert latex))) | |
463 | ||
464 | ;;;###autoload | |
465 | (defun org-export-region-as-latex (beg end &optional body-only buffer) | |
466 | "Convert region from BEG to END in `org-mode' buffer to LaTeX. | |
467 | If prefix arg BODY-ONLY is set, omit file header, footer, and table of | |
468 | contents, and only produce the region of converted text, useful for | |
469 | cut-and-paste operations. | |
470 | If BUFFER is a buffer or a string, use/create that buffer as a target | |
471 | of the converted LaTeX. If BUFFER is the symbol `string', return the | |
c8d0cf5c | 472 | produced LaTeX as a string and leave no buffer behind. For example, |
72a81656 CD |
473 | a Lisp program could call this function in the following way: |
474 | ||
475 | (setq latex (org-export-region-as-latex beg end t 'string)) | |
476 | ||
477 | When called interactively, the output buffer is selected, and shown | |
c8d0cf5c | 478 | in a window. A non-interactive call will only return the buffer." |
72a81656 CD |
479 | (interactive "r\nP") |
480 | (when (interactive-p) | |
481 | (setq buffer "*Org LaTeX Export*")) | |
482 | (let ((transient-mark-mode t) (zmacs-regions t) | |
c8d0cf5c | 483 | ext-plist rtn) |
8bfe682a | 484 | (setq ext-plist (plist-put ext-plist :ignore-subtree-p t)) |
72a81656 CD |
485 | (goto-char end) |
486 | (set-mark (point)) ;; to activate the region | |
487 | (goto-char beg) | |
488 | (setq rtn (org-export-as-latex | |
c8d0cf5c | 489 | nil nil ext-plist |
72a81656 CD |
490 | buffer body-only)) |
491 | (if (fboundp 'deactivate-mark) (deactivate-mark)) | |
492 | (if (and (interactive-p) (bufferp rtn)) | |
493 | (switch-to-buffer-other-window rtn) | |
494 | rtn))) | |
495 | ||
496 | ;;;###autoload | |
497 | (defun org-export-as-latex (arg &optional hidden ext-plist | |
4b67ea89 | 498 | to-buffer body-only pub-dir) |
db9c3fb1 CD |
499 | "Export current buffer to a LaTeX file. |
500 | If there is an active region, export only the region. The prefix | |
501 | ARG specifies how many levels of the outline should become | |
502 | headlines. The default is 3. Lower levels will be exported | |
503 | depending on `org-export-latex-low-levels'. The default is to | |
c8d0cf5c CD |
504 | convert them as description lists. |
505 | HIDDEN is obsolete and does nothing. | |
506 | EXT-PLIST is a property list with | |
db9c3fb1 CD |
507 | external parameters overriding org-mode's default settings, but |
508 | still inferior to file-local settings. When TO-BUFFER is | |
509 | non-nil, create a buffer with that name and export to that | |
510 | buffer. If TO-BUFFER is the symbol `string', don't leave any | |
511 | buffer behind but just return the resulting LaTeX as a string. | |
512 | When BODY-ONLY is set, don't produce the file header and footer, | |
513 | simply return the content of \begin{document}...\end{document}, | |
4b67ea89 CD |
514 | without even the \begin{document} and \end{document} commands. |
515 | when PUB-DIR is set, use this as the publishing directory." | |
72a81656 CD |
516 | (interactive "P") |
517 | ;; Make sure we have a file name when we need it. | |
518 | (when (and (not (or to-buffer body-only)) | |
519 | (not buffer-file-name)) | |
520 | (if (buffer-base-buffer) | |
521 | (org-set-local 'buffer-file-name | |
522 | (with-current-buffer (buffer-base-buffer) | |
523 | buffer-file-name)) | |
524 | (error "Need a file name to be able to export"))) | |
525 | ||
526 | (message "Exporting to LaTeX...") | |
c8d0cf5c CD |
527 | (org-unmodified |
528 | (remove-text-properties (point-min) (point-max) | |
529 | '(:org-license-to-kill nil))) | |
72a81656 | 530 | (org-update-radio-target-regexp) |
db9c3fb1 | 531 | (org-export-latex-set-initial-vars ext-plist arg) |
15841868 | 532 | (let* ((wcf (current-window-configuration)) |
db9c3fb1 CD |
533 | (opt-plist org-export-latex-options-plist) |
534 | (region-p (org-region-active-p)) | |
b349f79f CD |
535 | (rbeg (and region-p (region-beginning))) |
536 | (rend (and region-p (region-end))) | |
db9c3fb1 | 537 | (subtree-p |
8bfe682a | 538 | (if (plist-get opt-plist :ignore-subtree-p) |
c8d0cf5c CD |
539 | nil |
540 | (when region-p | |
541 | (save-excursion | |
542 | (goto-char rbeg) | |
543 | (and (org-at-heading-p) | |
544 | (>= (org-end-of-subtree t t) rend)))))) | |
545 | (opt-plist (setq org-export-opt-plist | |
546 | (if subtree-p | |
547 | (org-export-add-subtree-options opt-plist rbeg) | |
548 | opt-plist))) | |
b349f79f CD |
549 | ;; Make sure the variable contains the updated values. |
550 | (org-export-latex-options-plist opt-plist) | |
db9c3fb1 CD |
551 | (title (or (and subtree-p (org-export-get-title-from-subtree)) |
552 | (plist-get opt-plist :title) | |
553 | (and (not | |
554 | (plist-get opt-plist :skip-before-1st-heading)) | |
555 | (org-export-grab-title-from-buffer)) | |
556 | (file-name-sans-extension | |
557 | (file-name-nondirectory buffer-file-name)))) | |
72a81656 | 558 | (filename (concat (file-name-as-directory |
4b67ea89 CD |
559 | (or pub-dir |
560 | (org-export-directory :LaTeX ext-plist))) | |
72a81656 | 561 | (file-name-sans-extension |
b349f79f CD |
562 | (or (and subtree-p |
563 | (org-entry-get rbeg "EXPORT_FILE_NAME" t)) | |
564 | (file-name-nondirectory ;sans-extension | |
565 | buffer-file-name))) | |
566 | ".tex")) | |
72a81656 CD |
567 | (filename (if (equal (file-truename filename) |
568 | (file-truename buffer-file-name)) | |
569 | (concat filename ".tex") | |
570 | filename)) | |
571 | (buffer (if to-buffer | |
572 | (cond | |
573 | ((eq to-buffer 'string) (get-buffer-create | |
574 | "*Org LaTeX Export*")) | |
575 | (t (get-buffer-create to-buffer))) | |
576 | (find-file-noselect filename))) | |
72a81656 | 577 | (odd org-odd-levels-only) |
db9c3fb1 | 578 | (header (org-export-latex-make-header title opt-plist)) |
4b67ea89 | 579 | (skip (cond (subtree-p nil) |
db55f368 | 580 | (region-p nil) |
4b67ea89 | 581 | (t (plist-get opt-plist :skip-before-1st-heading)))) |
72a81656 | 582 | (text (plist-get opt-plist :text)) |
c8d0cf5c CD |
583 | (org-export-preprocess-hook |
584 | (cons | |
585 | `(lambda () (org-set-local 'org-complex-heading-regexp | |
586 | ,org-export-latex-complex-heading-re)) | |
587 | org-export-preprocess-hook)) | |
65c439fd | 588 | (first-lines (if skip "" (org-export-latex-first-lines |
c8d0cf5c CD |
589 | opt-plist |
590 | (if subtree-p | |
591 | (save-excursion | |
592 | (goto-char rbeg) | |
593 | (point-at-bol 2)) | |
594 | rbeg) | |
595 | (if region-p rend)))) | |
72a81656 CD |
596 | (coding-system (and (boundp 'buffer-file-coding-system) |
597 | buffer-file-coding-system)) | |
598 | (coding-system-for-write (or org-export-latex-coding-system | |
599 | coding-system)) | |
600 | (save-buffer-coding-system (or org-export-latex-coding-system | |
601 | coding-system)) | |
33306645 | 602 | (region (buffer-substring |
72a81656 CD |
603 | (if region-p (region-beginning) (point-min)) |
604 | (if region-p (region-end) (point-max)))) | |
605 | (string-for-export | |
20908596 | 606 | (org-export-preprocess-string |
db55f368 CD |
607 | region |
608 | :emph-multiline t | |
609 | :for-LaTeX t | |
610 | :comments nil | |
611 | :tags (plist-get opt-plist :tags) | |
612 | :priority (plist-get opt-plist :priority) | |
0bd48b37 | 613 | :footnotes (plist-get opt-plist :footnotes) |
1bcdebed | 614 | :drawers (plist-get opt-plist :drawers) |
0bd48b37 | 615 | :timestamps (plist-get opt-plist :timestamps) |
db55f368 CD |
616 | :todo-keywords (plist-get opt-plist :todo-keywords) |
617 | :add-text (if (eq to-buffer 'string) nil text) | |
618 | :skip-before-1st-heading skip | |
619 | :select-tags (plist-get opt-plist :select-tags) | |
620 | :exclude-tags (plist-get opt-plist :exclude-tags) | |
621 | :LaTeX-fragments nil))) | |
15841868 | 622 | |
db9c3fb1 | 623 | (set-buffer buffer) |
72a81656 | 624 | (erase-buffer) |
c8d0cf5c | 625 | (org-install-letbind) |
0b8568f5 | 626 | |
15841868 JW |
627 | (and (fboundp 'set-buffer-file-coding-system) |
628 | (set-buffer-file-coding-system coding-system-for-write)) | |
629 | ||
db9c3fb1 | 630 | ;; insert the header and initial document commands |
15841868 | 631 | (unless (or (eq to-buffer 'string) body-only) |
db9c3fb1 | 632 | (insert header)) |
15841868 JW |
633 | |
634 | ;; insert text found in #+TEXT | |
635 | (when (and text (not (eq to-buffer 'string))) | |
db9c3fb1 CD |
636 | (insert (org-export-latex-content |
637 | text '(lists tables fixed-width keywords)) | |
638 | "\n\n")) | |
15841868 JW |
639 | |
640 | ;; insert lines before the first headline | |
c8d0cf5c | 641 | (unless skip |
15841868 | 642 | (insert first-lines)) |
72a81656 | 643 | |
15841868 | 644 | ;; export the content of headlines |
72a81656 CD |
645 | (org-export-latex-global |
646 | (with-temp-buffer | |
647 | (insert string-for-export) | |
648 | (goto-char (point-min)) | |
15841868 JW |
649 | (when (re-search-forward "^\\(\\*+\\) " nil t) |
650 | (let* ((asters (length (match-string 1))) | |
651 | (level (if odd (- asters 2) (- asters 1)))) | |
db9c3fb1 | 652 | (setq org-export-latex-add-level |
15841868 JW |
653 | (if odd (1- (/ (1+ asters) 2)) (1- asters))) |
654 | (org-export-latex-parse-global level odd))))) | |
655 | ||
656 | ;; finalization | |
72a81656 | 657 | (unless body-only (insert "\n\\end{document}")) |
c8d0cf5c CD |
658 | |
659 | ;; Relocate the table of contents | |
660 | (goto-char (point-min)) | |
661 | (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t) | |
662 | (goto-char (point-min)) | |
663 | (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t) | |
664 | (replace-match "")) | |
665 | (goto-char (point-min)) | |
666 | (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t) | |
667 | (replace-match "\\tableofcontents" t t))) | |
668 | ||
8d642074 | 669 | (run-hooks 'org-export-latex-final-hook) |
0b8568f5 | 670 | (or to-buffer (save-buffer)) |
72a81656 | 671 | (goto-char (point-min)) |
c8d0cf5c CD |
672 | (or (org-export-push-to-kill-ring "LaTeX") |
673 | (message "Exporting to LaTeX...done")) | |
15841868 JW |
674 | (prog1 |
675 | (if (eq to-buffer 'string) | |
676 | (prog1 (buffer-substring (point-min) (point-max)) | |
677 | (kill-buffer (current-buffer))) | |
678 | (current-buffer)) | |
679 | (set-window-configuration wcf)))) | |
680 | ||
71d35b24 CD |
681 | ;;;###autoload |
682 | (defun org-export-as-pdf (arg &optional hidden ext-plist | |
683 | to-buffer body-only pub-dir) | |
684 | "Export as LaTeX, then process through to PDF." | |
685 | (interactive "P") | |
686 | (message "Exporting to PDF...") | |
687 | (let* ((wconfig (current-window-configuration)) | |
688 | (lbuf (org-export-as-latex arg hidden ext-plist | |
689 | to-buffer body-only pub-dir)) | |
690 | (file (buffer-file-name lbuf)) | |
691 | (base (file-name-sans-extension (buffer-file-name lbuf))) | |
c8d0cf5c CD |
692 | (pdffile (concat base ".pdf")) |
693 | (cmds org-latex-to-pdf-process) | |
694 | (outbuf (get-buffer-create "*Org PDF LaTeX Output*")) | |
695 | (bibtex-p (with-current-buffer lbuf | |
696 | (save-excursion | |
697 | (goto-char (point-min)) | |
698 | (re-search-forward "\\\\bibliography{" nil t)))) | |
699 | cmd) | |
700 | (with-current-buffer outbuf (erase-buffer)) | |
71d35b24 CD |
701 | (and (file-exists-p pdffile) (delete-file pdffile)) |
702 | (message "Processing LaTeX file...") | |
c8d0cf5c CD |
703 | (if (and cmds (symbolp cmds)) |
704 | (funcall cmds file) | |
705 | (while cmds | |
706 | (setq cmd (pop cmds)) | |
707 | (while (string-match "%b" cmd) | |
708 | (setq cmd (replace-match | |
709 | (save-match-data | |
710 | (shell-quote-argument base)) | |
711 | t t cmd))) | |
712 | (while (string-match "%s" cmd) | |
713 | (setq cmd (replace-match | |
714 | (save-match-data | |
715 | (shell-quote-argument file)) | |
716 | t t cmd))) | |
717 | (shell-command cmd outbuf outbuf))) | |
71d35b24 CD |
718 | (message "Processing LaTeX file...done") |
719 | (if (not (file-exists-p pdffile)) | |
720 | (error "PDF file was not produced") | |
721 | (set-window-configuration wconfig) | |
722 | (when org-export-pdf-remove-logfiles | |
723 | (dolist (ext '("aux" "log" "out" "toc")) | |
724 | (setq file (concat base "." ext)) | |
725 | (and (file-exists-p file) (delete-file file)))) | |
726 | (message "Exporting to PDF...done") | |
727 | pdffile))) | |
728 | ||
729 | ;;;###autoload | |
730 | (defun org-export-as-pdf-and-open (arg) | |
731 | "Export as LaTeX, then process through to PDF, and open." | |
732 | (interactive "P") | |
733 | (let ((pdffile (org-export-as-pdf arg))) | |
734 | (if pdffile | |
735 | (org-open-file pdffile) | |
736 | (error "PDF file was not produced")))) | |
737 | ||
0b8568f5 | 738 | ;;; Parsing functions: |
db9c3fb1 | 739 | |
72a81656 CD |
740 | (defun org-export-latex-parse-global (level odd) |
741 | "Parse the current buffer recursively, starting at LEVEL. | |
742 | If ODD is non-nil, assume the buffer only contains odd sections. | |
db9c3fb1 | 743 | Return a list reflecting the document structure." |
72a81656 CD |
744 | (save-excursion |
745 | (goto-char (point-min)) | |
746 | (let* ((cnt 0) output | |
db9c3fb1 | 747 | (depth org-export-latex-sectioning-depth)) |
72a81656 CD |
748 | (while (re-search-forward |
749 | (concat "^\\(\\(?:\\*\\)\\{" | |
750 | (number-to-string (+ (if odd 2 1) level)) | |
751 | "\\}\\) \\(.*\\)$") | |
752 | ;; make sure that there is no upper heading | |
753 | (when (> level 0) | |
754 | (save-excursion | |
755 | (save-match-data | |
756 | (re-search-forward | |
757 | (concat "^\\(\\(?:\\*\\)\\{" | |
758 | (number-to-string level) | |
759 | "\\}\\) \\(.*\\)$") nil t)))) t) | |
760 | (setq cnt (1+ cnt)) | |
761 | (let* ((pos (match-beginning 0)) | |
762 | (heading (match-string 2)) | |
763 | (nlevel (if odd (/ (+ 3 level) 2) (1+ level)))) | |
764 | (save-excursion | |
765 | (narrow-to-region | |
766 | (point) | |
767 | (save-match-data | |
768 | (if (re-search-forward | |
769 | (concat "^\\(\\(?:\\*\\)\\{" | |
770 | (number-to-string (+ (if odd 2 1) level)) | |
771 | "\\}\\) \\(.*\\)$") nil t) | |
772 | (match-beginning 0) | |
773 | (point-max)))) | |
774 | (goto-char (point-min)) | |
775 | (setq output | |
776 | (append output | |
777 | (list | |
778 | (list | |
779 | `(pos . ,pos) | |
780 | `(level . ,nlevel) | |
781 | `(occur . ,cnt) | |
782 | `(heading . ,heading) | |
783 | `(content . ,(org-export-latex-parse-content)) | |
db9c3fb1 | 784 | `(subcontent . ,(org-export-latex-parse-subcontent |
72a81656 CD |
785 | level odd))))))) |
786 | (widen))) | |
787 | (list output)))) | |
788 | ||
789 | (defun org-export-latex-parse-content () | |
790 | "Extract the content of a section." | |
791 | (let ((beg (point)) | |
792 | (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t) | |
793 | (progn (beginning-of-line) (point)) | |
794 | (point-max)))) | |
795 | (buffer-substring beg end))) | |
796 | ||
797 | (defun org-export-latex-parse-subcontent (level odd) | |
798 | "Extract the subcontent of a section at LEVEL. | |
799 | If ODD Is non-nil, assume subcontent only contains odd sections." | |
800 | (if (not (re-search-forward | |
801 | (concat "^\\(\\(?:\\*\\)\\{" | |
802 | (number-to-string (+ (if odd 4 2) level)) | |
803 | "\\}\\) \\(.*\\)$") | |
804 | nil t)) | |
805 | nil ; subcontent is nil | |
806 | (org-export-latex-parse-global (+ (if odd 2 1) level) odd))) | |
807 | ||
0b8568f5 | 808 | ;;; Rendering functions: |
72a81656 CD |
809 | (defun org-export-latex-global (content) |
810 | "Export CONTENT to LaTeX. | |
811 | CONTENT is an element of the list produced by | |
812 | `org-export-latex-parse-global'." | |
813 | (if (eq (car content) 'subcontent) | |
814 | (mapc 'org-export-latex-sub (cdr content)) | |
815 | (org-export-latex-sub (car content)))) | |
816 | ||
817 | (defun org-export-latex-sub (subcontent) | |
818 | "Export the list SUBCONTENT to LaTeX. | |
819 | SUBCONTENT is an alist containing information about the headline | |
820 | and its content." | |
db9c3fb1 | 821 | (let ((num (plist-get org-export-latex-options-plist :section-numbers))) |
d5098885 | 822 | (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent))) |
72a81656 | 823 | |
d5098885 | 824 | (defun org-export-latex-subcontent (subcontent num) |
621f83e4 CD |
825 | "Export each cell of SUBCONTENT to LaTeX. |
826 | If NUM, export sections as numerical sections." | |
827 | (let* ((heading (org-export-latex-fontify-headline | |
828 | (cdr (assoc 'heading subcontent)))) | |
829 | (level (- (cdr (assoc 'level subcontent)) | |
830 | org-export-latex-add-level)) | |
831 | (occur (number-to-string (cdr (assoc 'occur subcontent)))) | |
832 | (content (cdr (assoc 'content subcontent))) | |
833 | (subcontent (cadr (assoc 'subcontent subcontent))) | |
ce4fdcb9 CD |
834 | (label (org-get-text-property-any 0 'target heading)) |
835 | (label-list (cons label (cdr (assoc label | |
836 | org-export-target-aliases))))) | |
db9c3fb1 | 837 | (cond |
72a81656 | 838 | ;; Normal conversion |
db9c3fb1 | 839 | ((<= level org-export-latex-sectioning-depth) |
71d35b24 CD |
840 | (let* ((sec (nth (1- level) org-export-latex-sectioning)) |
841 | start end) | |
842 | (if (consp (cdr sec)) | |
843 | (setq start (nth (if num 0 2) sec) | |
844 | end (nth (if num 1 3) sec)) | |
845 | (setq start (if num (car sec) (cdr sec)))) | |
846 | (insert (format start heading) "\n") | |
ce4fdcb9 CD |
847 | (when label |
848 | (insert (mapconcat (lambda (l) (format "\\label{%s}" l)) | |
849 | label-list "\n") "\n")) | |
71d35b24 CD |
850 | (insert (org-export-latex-content content)) |
851 | (cond ((stringp subcontent) (insert subcontent)) | |
852 | ((listp subcontent) (org-export-latex-sub subcontent))) | |
853 | (if end (insert end "\n")))) | |
72a81656 | 854 | ;; At a level under the hl option: we can drop this subsection |
db9c3fb1 | 855 | ((> level org-export-latex-sectioning-depth) |
72a81656 | 856 | (cond ((eq org-export-latex-low-levels 'description) |
c8d0cf5c CD |
857 | (if (string-match "% ends low level$" |
858 | (buffer-substring (point-at-bol 0) (point))) | |
859 | (delete-region (point-at-bol 0) (point)) | |
860 | (insert "\\begin{description}\n")) | |
861 | (insert (format "\n\\item[%s]%s~\n\n" | |
621f83e4 CD |
862 | heading |
863 | (if label (format "\\label{%s}" label) ""))) | |
72a81656 CD |
864 | (insert (org-export-latex-content content)) |
865 | (cond ((stringp subcontent) (insert subcontent)) | |
866 | ((listp subcontent) (org-export-latex-sub subcontent))) | |
c8d0cf5c CD |
867 | (insert "\\end{description} % ends low level\n")) |
868 | ((memq org-export-latex-low-levels '(itemize enumerate)) | |
869 | (if (string-match "% ends low level$" | |
870 | (buffer-substring (point-at-bol 0) (point))) | |
871 | (delete-region (point-at-bol 0) (point)) | |
872 | (insert (format "\\begin{%s}\n" | |
873 | (symbol-name org-export-latex-low-levels)))) | |
874 | (insert (format "\n\\item %s\\\\\n%s\n" | |
875 | heading | |
876 | (if label (format "\\label{%s}" label) ""))) | |
877 | (insert (org-export-latex-content content)) | |
878 | (cond ((stringp subcontent) (insert subcontent)) | |
879 | ((listp subcontent) (org-export-latex-sub subcontent))) | |
880 | (insert (format "\\end{%s} %% ends low level\n" | |
881 | (symbol-name org-export-latex-low-levels)))) | |
882 | ||
883 | ((listp org-export-latex-low-levels) | |
884 | (if (string-match "% ends low level$" | |
885 | (buffer-substring (point-at-bol 0) (point))) | |
886 | (delete-region (point-at-bol 0) (point)) | |
887 | (insert (car org-export-latex-low-levels) "\n")) | |
888 | (insert (format (nth 2 org-export-latex-low-levels) | |
889 | heading | |
890 | (if label (format "\\label{%s}" label) ""))) | |
891 | (insert (org-export-latex-content content)) | |
892 | (cond ((stringp subcontent) (insert subcontent)) | |
893 | ((listp subcontent) (org-export-latex-sub subcontent))) | |
894 | (insert (nth 1 org-export-latex-low-levels) | |
895 | " %% ends low level\n")) | |
896 | ||
72a81656 CD |
897 | ((stringp org-export-latex-low-levels) |
898 | (insert (format org-export-latex-low-levels heading) "\n") | |
621f83e4 | 899 | (when label (insert (format "\\label{%s}\n" label))) |
72a81656 CD |
900 | (insert (org-export-latex-content content)) |
901 | (cond ((stringp subcontent) (insert subcontent)) | |
902 | ((listp subcontent) (org-export-latex-sub subcontent))))))))) | |
903 | ||
0b8568f5 | 904 | ;;; Exporting internals: |
db9c3fb1 | 905 | (defun org-export-latex-set-initial-vars (ext-plist level) |
0b8568f5 | 906 | "Store org local variables required for LaTeX export. |
db9c3fb1 CD |
907 | EXT-PLIST is an optional additional plist. |
908 | LEVEL indicates the default depth for export." | |
909 | (setq org-export-latex-todo-keywords-1 org-todo-keywords-1 | |
c8d0cf5c CD |
910 | org-export-latex-done-keywords org-done-keywords |
911 | org-export-latex-not-done-keywords org-not-done-keywords | |
912 | org-export-latex-complex-heading-re org-complex-heading-regexp | |
913 | org-export-latex-display-custom-times org-display-custom-times | |
db9c3fb1 | 914 | org-export-latex-all-targets-re |
0b8568f5 | 915 | (org-make-target-link-regexp (org-all-targets)) |
db9c3fb1 | 916 | org-export-latex-options-plist |
0b8568f5 JW |
917 | (org-combine-plists (org-default-export-plist) ext-plist |
918 | (org-infile-export-plist)) | |
db9c3fb1 | 919 | org-export-latex-class |
0bd48b37 CD |
920 | (or (and (org-region-active-p) |
921 | (save-excursion | |
922 | (goto-char (region-beginning)) | |
923 | (and (looking-at org-complex-heading-regexp) | |
924 | (org-entry-get nil "LaTeX_CLASS" 'selective)))) | |
925 | (save-excursion | |
926 | (save-restriction | |
927 | (widen) | |
928 | (goto-char (point-min)) | |
929 | (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t) | |
930 | (match-string 1)))) | |
8d642074 | 931 | (plist-get org-export-latex-options-plist :latex-class) |
0bd48b37 CD |
932 | org-export-latex-default-class) |
933 | org-export-latex-class | |
934 | (or (car (assoc org-export-latex-class org-export-latex-classes)) | |
935 | (error "No definition for class `%s' in `org-export-latex-classes'" | |
936 | org-export-latex-class)) | |
db9c3fb1 CD |
937 | org-export-latex-header |
938 | (cadr (assoc org-export-latex-class org-export-latex-classes)) | |
939 | org-export-latex-sectioning | |
940 | (cddr (assoc org-export-latex-class org-export-latex-classes)) | |
941 | org-export-latex-sectioning-depth | |
942 | (or level | |
943 | (let ((hl-levels | |
944 | (plist-get org-export-latex-options-plist :headline-levels)) | |
945 | (sec-depth (length org-export-latex-sectioning))) | |
946 | (if (> hl-levels sec-depth) sec-depth hl-levels))))) | |
947 | ||
948 | (defun org-export-latex-make-header (title opt-plist) | |
949 | "Make the LaTeX header and return it as a string. | |
950 | TITLE is the current title from the buffer or region. | |
951 | OPT-PLIST is the options plist for current buffer." | |
952 | (let ((toc (plist-get opt-plist :table-of-contents)) | |
953 | (author (plist-get opt-plist :author))) | |
954 | (concat | |
15841868 | 955 | (if (plist-get opt-plist :time-stamp-file) |
6671980f | 956 | (format-time-string "%% Created %Y-%m-%d %a %H:%M\n")) |
db9c3fb1 | 957 | ;; insert LaTeX custom header |
c8d0cf5c | 958 | (org-export-apply-macros-in-string org-export-latex-header) |
db9c3fb1 | 959 | "\n" |
15841868 JW |
960 | ;; insert information on LaTeX packages |
961 | (when org-export-latex-packages-alist | |
962 | (mapconcat (lambda(p) | |
963 | (if (equal "" (car p)) | |
964 | (format "\\usepackage{%s}" (cadr p)) | |
965 | (format "\\usepackage[%s]{%s}" | |
966 | (car p) (cadr p)))) | |
967 | org-export-latex-packages-alist "\n")) | |
db9c3fb1 | 968 | ;; insert additional commands in the header |
c8d0cf5c CD |
969 | (org-export-apply-macros-in-string |
970 | (plist-get opt-plist :latex-header-extra)) | |
971 | (org-export-apply-macros-in-string org-export-latex-append-header) | |
15841868 | 972 | ;; insert the title |
db9c3fb1 CD |
973 | (format |
974 | "\n\n\\title{%s}\n" | |
03f3cf35 JW |
975 | ;; convert the title |
976 | (org-export-latex-content | |
db9c3fb1 | 977 | title '(lists tables fixed-width keywords))) |
15841868 JW |
978 | ;; insert author info |
979 | (if (plist-get opt-plist :author-info) | |
db9c3fb1 | 980 | (format "\\author{%s}\n" |
8bfe682a | 981 | (org-export-latex-fontify-headline (or author user-full-name))) |
15841868 | 982 | (format "%%\\author{%s}\n" |
db9c3fb1 | 983 | (or author user-full-name))) |
15841868 JW |
984 | ;; insert the date |
985 | (format "\\date{%s}\n" | |
db9c3fb1 | 986 | (format-time-string |
15841868 JW |
987 | (or (plist-get opt-plist :date) |
988 | org-export-latex-date-format))) | |
15841868 JW |
989 | ;; beginning of the document |
990 | "\n\\begin{document}\n\n" | |
15841868 | 991 | ;; insert the title command |
c8d0cf5c CD |
992 | (when (string-match "\\S-" title) |
993 | (if (string-match "%s" org-export-latex-title-command) | |
994 | (format org-export-latex-title-command title) | |
995 | org-export-latex-title-command)) | |
15841868 | 996 | "\n\n" |
15841868 | 997 | ;; table of contents |
db9c3fb1 | 998 | (when (and org-export-with-toc |
15841868 JW |
999 | (plist-get opt-plist :section-numbers)) |
1000 | (cond ((numberp toc) | |
db55f368 | 1001 | (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n" |
15841868 | 1002 | (min toc (plist-get opt-plist :headline-levels)))) |
db55f368 | 1003 | (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n" |
8bfe682a CD |
1004 | (plist-get opt-plist :headline-levels))))) |
1005 | (when (plist-get opt-plist :preserve-breaks) | |
1006 | "\\obeylines\n")))) | |
0b8568f5 | 1007 | |
c8d0cf5c | 1008 | (defun org-export-latex-first-lines (opt-plist &optional beg end) |
0b8568f5 | 1009 | "Export the first lines before first headline. |
c8d0cf5c CD |
1010 | If BEG is non-nil, it is the beginning of the region. |
1011 | If END is non-nil, it is the end of the region." | |
0b8568f5 | 1012 | (save-excursion |
db55f368 | 1013 | (goto-char (or beg (point-min))) |
8d642074 CD |
1014 | (let* ((pt (point)) |
1015 | (end (if (re-search-forward "^\\*+ " end t) | |
1016 | (goto-char (match-beginning 0)) | |
1017 | (goto-char (or end (point-max)))))) | |
db55f368 CD |
1018 | (prog1 |
1019 | (org-export-latex-content | |
1020 | (org-export-preprocess-string | |
1021 | (buffer-substring pt end) | |
1022 | :for-LaTeX t | |
1023 | :emph-multiline t | |
1024 | :add-text nil | |
1025 | :comments nil | |
1026 | :skip-before-1st-heading nil | |
1e4f816a CD |
1027 | :LaTeX-fragments nil |
1028 | :timestamps (plist-get opt-plist :timestamps) | |
1029 | :footnotes (plist-get opt-plist :footnotes))) | |
c8d0cf5c CD |
1030 | (org-unmodified |
1031 | (add-text-properties pt (max pt (1- end)) | |
1032 | '(:org-license-to-kill t))))))) | |
1033 | ||
1034 | (defvar org-export-latex-header-defs nil | |
1035 | "The header definitions that might be used in the LaTeX body.") | |
1036 | (defvar org-export-latex-header-defs-re nil | |
1037 | "The header definitions that might be used in the LaTeX body.") | |
0b8568f5 | 1038 | |
db9c3fb1 CD |
1039 | (defun org-export-latex-content (content &optional exclude-list) |
1040 | "Convert CONTENT string to LaTeX. | |
1041 | Don't perform conversions that are in EXCLUDE-LIST. Recognized | |
1042 | conversion types are: quotation-marks, emphasis, sub-superscript, | |
1043 | links, keywords, lists, tables, fixed-width" | |
1044 | (with-temp-buffer | |
1045 | (insert content) | |
c8d0cf5c CD |
1046 | (unless (memq 'timestamps exclude-list) |
1047 | (org-export-latex-time-stamps)) | |
db9c3fb1 CD |
1048 | (unless (memq 'quotation-marks exclude-list) |
1049 | (org-export-latex-quotation-marks)) | |
1050 | (unless (memq 'emphasis exclude-list) | |
1051 | (when (plist-get org-export-latex-options-plist :emphasize) | |
1052 | (org-export-latex-fontify))) | |
1053 | (unless (memq 'sub-superscript exclude-list) | |
1054 | (org-export-latex-special-chars | |
1055 | (plist-get org-export-latex-options-plist :sub-superscript))) | |
1056 | (unless (memq 'links exclude-list) | |
1057 | (org-export-latex-links)) | |
1058 | (unless (memq 'keywords exclude-list) | |
0bd48b37 | 1059 | (org-export-latex-keywords)) |
db9c3fb1 CD |
1060 | (unless (memq 'lists exclude-list) |
1061 | (org-export-latex-lists)) | |
1062 | (unless (memq 'tables exclude-list) | |
1063 | (org-export-latex-tables | |
1064 | (plist-get org-export-latex-options-plist :tables))) | |
1065 | (unless (memq 'fixed-width exclude-list) | |
1066 | (org-export-latex-fixed-width | |
1067 | (plist-get org-export-latex-options-plist :fixed-width))) | |
1068 | ;; return string | |
1069 | (buffer-substring (point-min) (point-max)))) | |
1070 | ||
1071 | (defun org-export-latex-protect-string (s) | |
621f83e4 | 1072 | "Add the org-protected property to string S." |
db9c3fb1 CD |
1073 | (add-text-properties 0 (length s) '(org-protected t) s) s) |
1074 | ||
1075 | (defun org-export-latex-protect-char-in-string (char-list string) | |
1076 | "Add org-protected text-property to char from CHAR-LIST in STRING." | |
1077 | (with-temp-buffer | |
1078 | (save-match-data | |
1079 | (insert string) | |
1080 | (goto-char (point-min)) | |
1081 | (while (re-search-forward (regexp-opt char-list) nil t) | |
1082 | (add-text-properties (match-beginning 0) | |
1083 | (match-end 0) '(org-protected t))) | |
1084 | (buffer-string)))) | |
1085 | ||
0bd48b37 | 1086 | (defun org-export-latex-keywords-maybe (&optional remove-list) |
72a81656 CD |
1087 | "Maybe remove keywords depending on rules in REMOVE-LIST." |
1088 | (goto-char (point-min)) | |
db9c3fb1 | 1089 | (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|")) |
c8d0cf5c CD |
1090 | (case-fold-search nil) |
1091 | (todo-markup org-export-latex-todo-keyword-markup) | |
1092 | fmt) | |
72a81656 CD |
1093 | ;; convert TODO keywords |
1094 | (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t) | |
1095 | (if (plist-get remove-list :todo) | |
1096 | (replace-match "") | |
c8d0cf5c CD |
1097 | (setq fmt (cond |
1098 | ((stringp todo-markup) todo-markup) | |
1099 | ((and (consp todo-markup) (stringp (car todo-markup))) | |
1100 | (if (member (match-string 1) org-export-latex-done-keywords) | |
1101 | (cdr todo-markup) (car todo-markup))) | |
1102 | (t (cdr (or (assoc (match-string 1) todo-markup) | |
1103 | (car todo-markup)))))) | |
1104 | (replace-match (format fmt (match-string 1)) t t))) | |
72a81656 CD |
1105 | ;; convert priority string |
1106 | (when (re-search-forward "\\[\\\\#.\\]" nil t) | |
1107 | (if (plist-get remove-list :priority) | |
1108 | (replace-match "") | |
0bd48b37 | 1109 | (replace-match (format "\\textbf{%s}" (match-string 0)) t t))) |
72a81656 | 1110 | ;; convert tags |
2c3ad40d | 1111 | (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t) |
0b8568f5 JW |
1112 | (if (or (not org-export-with-tags) |
1113 | (plist-get remove-list :tags)) | |
72a81656 | 1114 | (replace-match "") |
621f83e4 | 1115 | (replace-match |
2c3ad40d | 1116 | (org-export-latex-protect-string |
0bd48b37 | 1117 | (format "\\textbf{%s}" |
33306645 CD |
1118 | (save-match-data |
1119 | (replace-regexp-in-string | |
1120 | "_" "\\\\_" (match-string 0))))) | |
1121 | t t))))) | |
72a81656 | 1122 | |
db9c3fb1 | 1123 | (defun org-export-latex-fontify-headline (string) |
621f83e4 | 1124 | "Fontify special words in STRING." |
72a81656 CD |
1125 | (with-temp-buffer |
1126 | ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at | |
1127 | ;; the beginning of the buffer - inserting "\n" is safe here though. | |
db9c3fb1 | 1128 | (insert "\n" string) |
72a81656 | 1129 | (goto-char (point-min)) |
8bfe682a CD |
1130 | (let ((re (concat "\\\\[a-zA-Z]+\\(?:" |
1131 | "\\[.*\\]" | |
1132 | "\\)?" | |
1133 | (org-create-multibrace-regexp "{" "}" 3)))) | |
1134 | (while (re-search-forward re nil t) | |
1135 | (unless (save-excursion (goto-char (match-beginning 0)) | |
1136 | (equal (char-after (point-at-bol)) ?#)) | |
1137 | (add-text-properties (match-beginning 0) (match-end 0) | |
1138 | '(org-protected t))))) | |
db9c3fb1 | 1139 | (when (plist-get org-export-latex-options-plist :emphasize) |
0b8568f5 | 1140 | (org-export-latex-fontify)) |
0bd48b37 | 1141 | (org-export-latex-keywords-maybe) |
2c3ad40d CD |
1142 | (org-export-latex-special-chars |
1143 | (plist-get org-export-latex-options-plist :sub-superscript)) | |
72a81656 | 1144 | (org-export-latex-links) |
621f83e4 | 1145 | (org-trim (buffer-string)))) |
72a81656 | 1146 | |
c8d0cf5c CD |
1147 | (defun org-export-latex-time-stamps () |
1148 | "Format time stamps." | |
1149 | (goto-char (point-min)) | |
1150 | (let ((org-display-custom-times org-export-latex-display-custom-times)) | |
1151 | (while (re-search-forward org-ts-regexp-both nil t) | |
1152 | (org-if-unprotected-at (1- (point)) | |
1153 | (replace-match | |
1154 | (org-export-latex-protect-string | |
1155 | (format org-export-latex-timestamp-markup | |
1156 | (substring (org-translate-time (match-string 0)) 1 -1))) | |
1157 | t t))))) | |
1158 | ||
72a81656 | 1159 | (defun org-export-latex-quotation-marks () |
621f83e4 | 1160 | "Export quotation marks depending on language conventions." |
db9c3fb1 | 1161 | (let* ((lang (plist-get org-export-latex-options-plist :language)) |
72a81656 | 1162 | (quote-rpl (if (equal lang "fr") |
db55f368 CD |
1163 | '(("\\(\\s-\\)\"" "«~") |
1164 | ("\\(\\S-\\)\"" "~»") | |
72a81656 | 1165 | ("\\(\\s-\\)'" "`")) |
8bfe682a | 1166 | '(("\\(\\s-\\|[[(]\\)\"" "``") |
72a81656 | 1167 | ("\\(\\S-\\)\"" "''") |
c8d0cf5c | 1168 | ("\\(\\s-\\|(\\)'" "`"))))) |
72a81656 CD |
1169 | (mapc (lambda(l) (goto-char (point-min)) |
1170 | (while (re-search-forward (car l) nil t) | |
8bfe682a CD |
1171 | (let ((rpl (concat (match-string 1) |
1172 | (org-export-latex-protect-string | |
1173 | (copy-sequence (cadr l)))))) | |
0bd48b37 | 1174 | (org-if-unprotected-1 |
72a81656 CD |
1175 | (replace-match rpl t t))))) quote-rpl))) |
1176 | ||
72a81656 CD |
1177 | (defun org-export-latex-special-chars (sub-superscript) |
1178 | "Export special characters to LaTeX. | |
1179 | If SUB-SUPERSCRIPT is non-nil, convert \\ and ^. | |
1180 | See the `org-export-latex.el' code for a complete conversion table." | |
1181 | (goto-char (point-min)) | |
1182 | (mapc (lambda(c) | |
1183 | (goto-char (point-min)) | |
1184 | (while (re-search-forward c nil t) | |
1185 | ;; Put the point where to check for org-protected | |
db55f368 | 1186 | (unless (get-text-property (match-beginning 2) 'org-protected) |
72a81656 | 1187 | (cond ((member (match-string 2) '("\\$" "$")) |
15841868 | 1188 | (if (equal (match-string 2) "\\$") |
0bd48b37 CD |
1189 | nil |
1190 | (replace-match "\\$" t t))) | |
15841868 JW |
1191 | ((member (match-string 2) '("&" "%" "#")) |
1192 | (if (equal (match-string 1) "\\") | |
1193 | (replace-match (match-string 2) t t) | |
1194 | (replace-match (concat (match-string 1) "\\" | |
1195 | (match-string 2)) t t))) | |
db9c3fb1 CD |
1196 | ((equal (match-string 2) "...") |
1197 | (replace-match | |
1198 | (concat (match-string 1) | |
1199 | (org-export-latex-protect-string "\\ldots{}")) t t)) | |
15841868 JW |
1200 | ((equal (match-string 2) "~") |
1201 | (cond ((equal (match-string 1) "\\") nil) | |
1202 | ((eq 'org-link (get-text-property 0 'face (match-string 2))) | |
1203 | (replace-match (concat (match-string 1) "\\~") t t)) | |
db9c3fb1 | 1204 | (t (replace-match |
15841868 JW |
1205 | (org-export-latex-protect-string |
1206 | (concat (match-string 1) "\\~{}")) t t)))) | |
1207 | ((member (match-string 2) '("{" "}")) | |
0bd48b37 | 1208 | (unless (save-match-data (org-inside-latex-math-p)) |
15841868 JW |
1209 | (if (equal (match-string 1) "\\") |
1210 | (replace-match (match-string 2) t t) | |
1211 | (replace-match (concat (match-string 1) "\\" | |
1212 | (match-string 2)) t t))))) | |
0bd48b37 | 1213 | (unless (save-match-data (org-inside-latex-math-p)) |
15841868 JW |
1214 | (cond ((equal (match-string 2) "\\") |
1215 | (replace-match (or (save-match-data | |
1216 | (org-export-latex-treat-backslash-char | |
1217 | (match-string 1) | |
e9aab93b CD |
1218 | (or (match-string 3) ""))) |
1219 | "") t t)) | |
15841868 JW |
1220 | ((member (match-string 2) '("_" "^")) |
1221 | (replace-match (or (save-match-data | |
1222 | (org-export-latex-treat-sub-super-char | |
1223 | sub-superscript | |
15841868 | 1224 | (match-string 2) |
621f83e4 | 1225 | (match-string 1) |
c8d0cf5c CD |
1226 | (match-string 3))) "") t t) |
1227 | (backward-char 1))))))) | |
0bd48b37 CD |
1228 | '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$" |
1229 | "\\(\\(\\\\?\\$\\)\\)" | |
c8d0cf5c | 1230 | "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)" |
0b8568f5 | 1231 | "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)" |
72a81656 | 1232 | "\\(.\\|^\\)\\(&\\)" |
33306645 CD |
1233 | "\\(.\\|^\\)\\(#\\)" |
1234 | "\\(.\\|^\\)\\(%\\)" | |
1235 | "\\(.\\|^\\)\\({\\)" | |
72a81656 | 1236 | "\\(.\\|^\\)\\(}\\)" |
15841868 | 1237 | "\\(.\\|^\\)\\(~\\)" |
db9c3fb1 | 1238 | "\\(.\\|^\\)\\(\\.\\.\\.\\)" |
15841868 JW |
1239 | ;; (?\< . "\\textless{}") |
1240 | ;; (?\> . "\\textgreater{}") | |
1241 | ))) | |
72a81656 | 1242 | |
0bd48b37 CD |
1243 | (defun org-inside-latex-math-p () |
1244 | (get-text-property (point) 'org-latex-math)) | |
1245 | ||
72a81656 | 1246 | (defun org-export-latex-treat-sub-super-char |
621f83e4 | 1247 | (subsup char string-before string-after) |
72a81656 CD |
1248 | "Convert the \"_\" and \"^\" characters to LaTeX. |
1249 | SUBSUP corresponds to the ^: option in the #+OPTIONS line. | |
1250 | Convert CHAR depending on STRING-BEFORE and STRING-AFTER." | |
1251 | (cond ((equal string-before "\\") | |
1252 | (concat string-before char string-after)) | |
8d642074 CD |
1253 | ((and (string-match "\\S-+" string-after)) |
1254 | ;; this is part of a math formula | |
0b8568f5 | 1255 | (cond ((eq 'org-link (get-text-property 0 'face char)) |
72a81656 | 1256 | (concat string-before "\\" char string-after)) |
0bd48b37 | 1257 | ((save-match-data (org-inside-latex-math-p)) |
72a81656 CD |
1258 | (if subsup |
1259 | (cond ((eq 1 (length string-after)) | |
1260 | (concat string-before char string-after)) | |
1261 | ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after) | |
db9c3fb1 | 1262 | (format "%s%s{%s}" string-before char |
72a81656 | 1263 | (match-string 1 string-after)))))) |
71d35b24 CD |
1264 | ((and (> (length string-after) 1) |
1265 | (or (eq subsup t) | |
1266 | (and (equal subsup '{}) (eq (string-to-char string-after) ?\{))) | |
72a81656 | 1267 | (string-match "[({]?\\([^)}]+\\)[)}]?" string-after)) |
c8d0cf5c CD |
1268 | (org-export-latex-protect-string |
1269 | (format "%s$%s{%s}$" string-before char | |
1270 | (if (and (> (match-end 1) (1+ (match-beginning 1))) | |
1271 | (not (equal (substring string-after 0 2) "{\\"))) | |
1272 | (concat "\\mathrm{" (match-string 1 string-after) "}") | |
1273 | (match-string 1 string-after))))) | |
71d35b24 | 1274 | ((eq subsup t) (concat string-before "$" char string-after "$")) |
15841868 | 1275 | (t (org-export-latex-protect-string |
d5098885 | 1276 | (concat string-before "\\" char "{}" string-after))))) |
15841868 | 1277 | (t (org-export-latex-protect-string |
d5098885 | 1278 | (concat string-before "\\" char "{}" string-after))))) |
72a81656 CD |
1279 | |
1280 | (defun org-export-latex-treat-backslash-char (string-before string-after) | |
1281 | "Convert the \"$\" special character to LaTeX. | |
1282 | The conversion is made depending of STRING-BEFORE and STRING-AFTER." | |
1283 | (cond ((member (list string-after) org-html-entities) | |
1284 | ;; backslash is part of a special entity (like "\alpha") | |
1285 | (concat string-before "$\\" | |
1286 | (or (cdar (member (list string-after) org-html-entities)) | |
1287 | string-after) "$")) | |
1288 | ((and (not (string-match "^[ \n\t]" string-after)) | |
0b8568f5 | 1289 | (not (string-match "[ \t]\\'\\|^" string-before))) |
72a81656 | 1290 | ;; backslash is inside a word |
15841868 JW |
1291 | (org-export-latex-protect-string |
1292 | (concat string-before "\\textbackslash{}" string-after))) | |
72a81656 CD |
1293 | ((not (or (equal string-after "") |
1294 | (string-match "^[ \t\n]" string-after))) | |
1295 | ;; backslash might escape a character (like \#) or a user TeX | |
1296 | ;; macro (like \setcounter) | |
db9c3fb1 | 1297 | (org-export-latex-protect-string |
15841868 | 1298 | (concat string-before "\\" string-after))) |
72a81656 CD |
1299 | ((and (string-match "^[ \t\n]" string-after) |
1300 | (string-match "[ \t\n]\\'" string-before)) | |
1301 | ;; backslash is alone, convert it to $\backslash$ | |
15841868 JW |
1302 | (org-export-latex-protect-string |
1303 | (concat string-before "\\textbackslash{}" string-after))) | |
1304 | (t (org-export-latex-protect-string | |
1305 | (concat string-before "\\textbackslash{}" string-after))))) | |
72a81656 | 1306 | |
0bd48b37 CD |
1307 | (defun org-export-latex-keywords () |
1308 | "Convert special keywords to LaTeX." | |
1309 | (goto-char (point-min)) | |
c8d0cf5c CD |
1310 | (while (re-search-forward org-export-latex-special-keyword-regexp nil t) |
1311 | (replace-match (format org-export-latex-timestamp-keyword-markup | |
1312 | (match-string 0)) t t) | |
1313 | (save-excursion | |
1314 | (beginning-of-line 1) | |
1315 | (unless (looking-at ".*\\\\newline[ \t]*$") | |
1316 | (end-of-line 1) | |
1317 | (insert "\\newline"))))) | |
db9c3fb1 | 1318 | |
72a81656 CD |
1319 | (defun org-export-latex-fixed-width (opt) |
1320 | "When OPT is non-nil convert fixed-width sections to LaTeX." | |
1321 | (goto-char (point-min)) | |
0bd48b37 | 1322 | (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t) |
72a81656 CD |
1323 | (if opt |
1324 | (progn (goto-char (match-beginning 0)) | |
1325 | (insert "\\begin{verbatim}\n") | |
0bd48b37 | 1326 | (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$") |
72a81656 CD |
1327 | (replace-match (concat (match-string 1) |
1328 | (match-string 2)) t t) | |
1329 | (forward-line)) | |
1330 | (insert "\\end{verbatim}\n\n")) | |
1331 | (progn (goto-char (match-beginning 0)) | |
0bd48b37 | 1332 | (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$") |
72a81656 CD |
1333 | (replace-match (concat "%" (match-string 1) |
1334 | (match-string 2)) t t) | |
1335 | (forward-line)))))) | |
1336 | ||
20908596 CD |
1337 | |
1338 | (defvar org-table-last-alignment) ; defined in org-table.el | |
54a0dee5 | 1339 | (defvar org-table-last-column-widths) ; defined in org-table.el |
20908596 | 1340 | (declare-function orgtbl-to-latex "org-table" (table params) t) |
15841868 JW |
1341 | (defun org-export-latex-tables (insert) |
1342 | "Convert tables to LaTeX and INSERT it." | |
72a81656 CD |
1343 | (goto-char (point-min)) |
1344 | (while (re-search-forward "^\\([ \t]*\\)|" nil t) | |
8bfe682a CD |
1345 | (org-if-unprotected-at (1- (point)) |
1346 | (org-table-align) | |
1347 | (let* ((beg (org-table-begin)) | |
1348 | (end (org-table-end)) | |
1349 | (raw-table (buffer-substring beg end)) | |
1350 | (org-table-last-alignment (copy-sequence org-table-last-alignment)) | |
1351 | (org-table-last-column-widths (copy-sequence | |
1352 | org-table-last-column-widths)) | |
1353 | fnum fields line lines olines gr colgropen line-fmt align | |
1354 | caption label attr floatp longtblp) | |
1355 | (if org-export-latex-tables-verbatim | |
1356 | (let* ((tbl (concat "\\begin{verbatim}\n" raw-table | |
1357 | "\\end{verbatim}\n"))) | |
1358 | (apply 'delete-region (list beg end)) | |
1359 | (insert (org-export-latex-protect-string tbl))) | |
1360 | (progn | |
1361 | (setq caption (org-find-text-property-in-string | |
1362 | 'org-caption raw-table) | |
1363 | attr (org-find-text-property-in-string | |
1364 | 'org-attributes raw-table) | |
1365 | label (org-find-text-property-in-string | |
1366 | 'org-label raw-table) | |
1367 | longtblp (and attr (stringp attr) | |
1368 | (string-match "\\<longtable\\>" attr)) | |
1369 | align (and attr (stringp attr) | |
1370 | (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr) | |
1371 | (match-string 1 attr)) | |
1372 | floatp (or caption label)) | |
1373 | (setq lines (org-split-string raw-table "\n")) | |
1374 | (apply 'delete-region (list beg end)) | |
1375 | (when org-export-table-remove-special-lines | |
1376 | (setq lines (org-table-clean-before-export lines 'maybe-quoted))) | |
1377 | (when org-table-clean-did-remove-column | |
54a0dee5 CD |
1378 | (pop org-table-last-alignment) |
1379 | (pop org-table-last-column-widths)) | |
8bfe682a CD |
1380 | ;; make a formatting string to reflect alignment |
1381 | (setq olines lines) | |
1382 | (while (and (not line-fmt) (setq line (pop olines))) | |
1383 | (unless (string-match "^[ \t]*|-" line) | |
1384 | (setq fields (org-split-string line "[ \t]*|[ \t]*")) | |
1385 | (setq fnum (make-vector (length fields) 0)) | |
1386 | (setq line-fmt | |
1387 | (mapconcat | |
1388 | (lambda (x) | |
1389 | (setq gr (pop org-table-colgroup-info)) | |
1390 | (format "%s%%s%s" | |
1391 | (cond ((eq gr :start) | |
1392 | (prog1 (if colgropen "|" "|") | |
1393 | (setq colgropen t))) | |
1394 | ((eq gr :startend) | |
1395 | (prog1 (if colgropen "|" "|") | |
1396 | (setq colgropen nil))) | |
1397 | (t "")) | |
1398 | (if (memq gr '(:end :startend)) | |
1399 | (progn (setq colgropen nil) "|") | |
1400 | ""))) | |
1401 | fnum "")))) | |
1402 | ;; fix double || in line-fmt | |
1403 | (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt)) | |
1404 | ;; maybe remove the first and last "|" | |
1405 | (when (and (not org-export-latex-tables-column-borders) | |
1406 | (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt)) | |
1407 | (setq line-fmt (match-string 2 line-fmt))) | |
1408 | ;; format alignment | |
1409 | (unless align | |
1410 | (setq align (apply 'format | |
1411 | (cons line-fmt | |
1412 | (mapcar (lambda (x) (if x "r" "l")) | |
1413 | org-table-last-alignment))))) | |
1414 | ;; prepare the table to send to orgtbl-to-latex | |
1415 | (setq lines | |
1416 | (mapcar | |
1417 | (lambda(elem) | |
1418 | (or (and (string-match "[ \t]*|-+" elem) 'hline) | |
1419 | (org-split-string (org-trim elem) "|"))) | |
1420 | lines)) | |
1421 | (when insert | |
1422 | (insert (org-export-latex-protect-string | |
1423 | (concat | |
1424 | (if longtblp | |
1425 | (concat "\\begin{longtable}{" align "}\n") | |
1426 | (if floatp "\\begin{table}[htb]\n")) | |
1427 | (if (or floatp longtblp) | |
1428 | (format | |
1429 | "\\caption{%s%s}" | |
1430 | (if label (concat "\\\label{" label "}") "") | |
1431 | (or caption ""))) | |
1432 | (if longtblp "\\\\\n" "\n") | |
1433 | (if (and org-export-latex-tables-centered (not longtblp)) | |
1434 | "\\begin{center}\n") | |
1435 | (if (not longtblp) (concat "\\begin{tabular}{" align "}\n")) | |
1436 | (orgtbl-to-latex | |
1437 | lines | |
1438 | `(:tstart nil :tend nil | |
1439 | :hlend ,(if longtblp | |
1440 | (format "\\\\ | |
db55f368 CD |
1441 | \\hline |
1442 | \\endhead | |
1443 | \\hline\\multicolumn{%d}{r}{Continued on next page}\\ | |
1444 | \\endfoot | |
1445 | \\endlastfoot" (length org-table-last-alignment)) | |
8bfe682a CD |
1446 | nil))) |
1447 | (if (not longtblp) (concat "\n\\end{tabular}")) | |
1448 | (if longtblp "\n" (if org-export-latex-tables-centered | |
1449 | "\n\\end{center}\n" "\n")) | |
1450 | (if longtblp | |
1451 | "\\end{longtable}" | |
1452 | (if floatp "\\end{table}")))) | |
1453 | "\n\n")))))))) | |
72a81656 CD |
1454 | |
1455 | (defun org-export-latex-fontify () | |
1456 | "Convert fontification to LaTeX." | |
1457 | (goto-char (point-min)) | |
1458 | (while (re-search-forward org-emph-re nil t) | |
5dec9555 | 1459 | ;; The match goes one char after the *string*, except at the end of a line |
db9c3fb1 CD |
1460 | (let ((emph (assoc (match-string 3) |
1461 | org-export-latex-emphasis-alist)) | |
0bd48b37 CD |
1462 | (beg (match-beginning 0)) |
1463 | (end (match-end 0)) | |
8bfe682a | 1464 | rpl s) |
c8d0cf5c CD |
1465 | (unless emph |
1466 | (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\"" | |
1467 | (match-string 3))) | |
8bfe682a CD |
1468 | (unless (or (and (get-text-property (- (point) 2) 'org-protected) |
1469 | (not (get-text-property | |
1470 | (- (point) 2) 'org-verbatim-emph))) | |
0bd48b37 CD |
1471 | (save-excursion |
1472 | (goto-char (match-beginning 1)) | |
1473 | (save-match-data | |
1474 | (and (org-at-table-p) | |
1475 | (string-match | |
1476 | "[|\n]" (buffer-substring beg end)))))) | |
8bfe682a | 1477 | (setq s (match-string 4)) |
15841868 | 1478 | (setq rpl (concat (match-string 1) |
c8d0cf5c CD |
1479 | (org-export-latex-emph-format (cadr emph) |
1480 | (match-string 4)) | |
15841868 | 1481 | (match-string 5))) |
db9c3fb1 | 1482 | (if (caddr emph) |
8bfe682a CD |
1483 | (setq rpl (org-export-latex-protect-string rpl)) |
1484 | (save-match-data | |
5dec9555 | 1485 | (if (string-match "\\`.\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl) |
8bfe682a CD |
1486 | (progn |
1487 | (add-text-properties (match-beginning 1) (match-end 1) | |
1488 | '(org-protected t) rpl) | |
1489 | (add-text-properties (match-beginning 3) (match-end 3) | |
1490 | '(org-protected t) rpl))))) | |
15841868 JW |
1491 | (replace-match rpl t t))) |
1492 | (backward-char))) | |
72a81656 | 1493 | |
c8d0cf5c CD |
1494 | (defvar org-export-latex-use-verb nil) |
1495 | (defun org-export-latex-emph-format (format string) | |
1496 | "Format an emphasis string and handle the \\verb special case." | |
1497 | (when (equal format "\\verb") | |
1498 | (save-match-data | |
1499 | (if org-export-latex-use-verb | |
1500 | (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}")) | |
1501 | (catch 'exit | |
1502 | (loop for i from 0 to (1- (length ll)) do | |
1503 | (if (not (string-match (regexp-quote (substring ll i (1+ i))) | |
1504 | string)) | |
1505 | (progn | |
1506 | (setq format (concat "\\verb" (substring ll i (1+ i)) | |
1507 | "%s" (substring ll i (1+ i)))) | |
1508 | (throw 'exit nil)))))) | |
1509 | (let ((start 0) | |
8d642074 CD |
1510 | (trans '(("\\" . "\\textbackslash{}") |
1511 | ("~" . "\\textasciitilde{}") | |
1512 | ("^" . "\\textasciicircum{}"))) | |
c8d0cf5c CD |
1513 | (rtn "") char) |
1514 | (while (string-match "[\\{}$%&_#~^]" string) | |
1515 | (setq char (match-string 0 string)) | |
1516 | (if (> (match-beginning 0) 0) | |
1517 | (setq rtn (concat rtn (substring string | |
1518 | 0 (match-beginning 0))))) | |
1519 | (setq string (substring string (1+ (match-beginning 0)))) | |
1520 | (setq char (or (cdr (assoc char trans)) (concat "\\" char)) | |
1521 | rtn (concat rtn char))) | |
1522 | (setq string (concat rtn string) format "\\texttt{%s}"))))) | |
8bfe682a | 1523 | (format format string)) |
c8d0cf5c | 1524 | |
72a81656 CD |
1525 | (defun org-export-latex-links () |
1526 | ;; Make sure to use the LaTeX hyperref and graphicx package | |
1527 | ;; or send some warnings. | |
1528 | "Convert links to LaTeX." | |
1529 | (goto-char (point-min)) | |
0bd48b37 | 1530 | (while (re-search-forward org-bracket-link-analytic-regexp++ nil t) |
8bfe682a | 1531 | (org-if-unprotected-1 |
72a81656 | 1532 | (goto-char (match-beginning 0)) |
db9c3fb1 | 1533 | (let* ((re-radio org-export-latex-all-targets-re) |
72a81656 | 1534 | (remove (list (match-beginning 0) (match-end 0))) |
2c3ad40d | 1535 | (raw-path (org-extract-attributes (match-string 3))) |
72a81656 CD |
1536 | (full-raw-path (concat (match-string 1) raw-path)) |
1537 | (desc (match-string 5)) | |
db55f368 CD |
1538 | (type (or (match-string 2) |
1539 | (if (or (file-name-absolute-p raw-path) | |
1540 | (string-match "^\\.\\.?/" raw-path)) | |
1541 | "file"))) | |
0bd48b37 | 1542 | (coderefp (equal type "coderef")) |
db55f368 | 1543 | (caption (org-find-text-property-in-string 'org-caption raw-path)) |
c8d0cf5c CD |
1544 | (attr (or (org-find-text-property-in-string 'org-attributes raw-path) |
1545 | (plist-get org-export-latex-options-plist :latex-image-options))) | |
db55f368 | 1546 | (label (org-find-text-property-in-string 'org-label raw-path)) |
72a81656 CD |
1547 | imgp radiop |
1548 | ;; define the path of the link | |
1549 | (path (cond | |
0bd48b37 CD |
1550 | ((member type '("coderef")) |
1551 | raw-path) | |
72a81656 CD |
1552 | ((member type '("http" "https" "ftp")) |
1553 | (concat type ":" raw-path)) | |
1554 | ((and re-radio (string-match re-radio raw-path)) | |
33306645 | 1555 | (setq radiop t)) |
72a81656 CD |
1556 | ((equal type "mailto") |
1557 | (concat type ":" raw-path)) | |
1558 | ((equal type "file") | |
0bd48b37 CD |
1559 | (if (and (org-file-image-p |
1560 | (expand-file-name | |
1561 | raw-path) | |
1562 | org-export-latex-inline-image-extensions) | |
c8d0cf5c CD |
1563 | (or (get-text-property 0 'org-no-description |
1564 | raw-path) | |
1565 | (equal desc full-raw-path))) | |
72a81656 CD |
1566 | (setq imgp t) |
1567 | (progn (when (string-match "\\(.+\\)::.+" raw-path) | |
1568 | (setq raw-path (match-string 1 raw-path))) | |
1569 | (if (file-exists-p raw-path) | |
1570 | (concat type "://" (expand-file-name raw-path)) | |
1571 | (concat type "://" (org-export-directory | |
db9c3fb1 | 1572 | :LaTeX org-export-latex-options-plist) |
72a81656 CD |
1573 | raw-path)))))))) |
1574 | ;; process with link inserting | |
1575 | (apply 'delete-region remove) | |
8bfe682a CD |
1576 | (cond ((and imgp |
1577 | (plist-get org-export-latex-options-plist :inline-images)) | |
1578 | ;; OK, we need to inline an image | |
db55f368 | 1579 | (insert |
8bfe682a | 1580 | (org-export-latex-format-image raw-path caption label attr))) |
0bd48b37 CD |
1581 | (coderefp |
1582 | (insert (format | |
1583 | (org-export-get-coderef-format path desc) | |
1584 | (cdr (assoc path org-export-code-refs))))) | |
621f83e4 CD |
1585 | (radiop (insert (format "\\hyperref[%s]{%s}" |
1586 | (org-solidify-link-text raw-path) desc))) | |
1587 | ((not type) | |
1588 | (insert (format "\\hyperref[%s]{%s}" | |
c8d0cf5c | 1589 | (org-remove-initial-hash |
54a0dee5 CD |
1590 | (org-solidify-link-text raw-path)) |
1591 | desc))) | |
1592 | (path | |
1593 | (when (org-at-table-p) | |
1594 | ;; There is a strange problem when we have a link in a table, | |
1595 | ;; ampersands then cause a problem. I think this must be | |
1596 | ;; a LaTeX issue, but we here implement a work-around anyway. | |
1597 | (setq path (org-export-latex-protect-amp path) | |
1598 | desc (org-export-latex-protect-amp desc))) | |
1599 | (insert (format "\\href{%s}{%s}" path desc))) | |
72a81656 CD |
1600 | (t (insert "\\texttt{" desc "}"))))))) |
1601 | ||
8bfe682a CD |
1602 | |
1603 | (defun org-export-latex-format-image (path caption label attr) | |
1604 | "Format the image element, depending on user settings." | |
1605 | (let (floatp wrapp placement figenv) | |
1606 | (setq floatp (or caption label)) | |
1607 | (when (and attr (stringp attr)) | |
1608 | (if (string-match "[ \t]*\\<wrap\\>" attr) | |
1609 | (setq wrapp t floatp nil attr (replace-match "" t t attr))) | |
1610 | (if (string-match "[ \t]*\\<float\\>" attr) | |
1611 | (setq wrapp nil floatp t attr (replace-match "" t t attr)))) | |
1612 | ||
1613 | (setq placement | |
1614 | (cond | |
1615 | (wrapp "{l}{0.5\\textwidth}") | |
1616 | (floatp "[htb]") | |
1617 | (t ""))) | |
1618 | ||
1619 | (when (and attr (stringp attr) | |
1620 | (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr)) | |
1621 | (setq placement (match-string 1 attr) | |
1622 | attr (replace-match "" t t attr))) | |
1623 | (setq attr (and attr (org-trim attr))) | |
1624 | (when (or (not attr) (= (length attr) 0)) | |
1625 | (setq attr (cond (floatp "width=0.7\\textwidth") | |
1626 | (wrapp "width=0.48\\textwidth") | |
1627 | (t attr)))) | |
1628 | (setq figenv | |
1629 | (cond | |
1630 | (wrapp "\\begin{wrapfigure}%placement | |
1631 | \\centering | |
1632 | \\includegraphics[%attr]{%path} | |
1633 | \\caption{%labelcmd%caption} | |
1634 | \\end{wrapfigure}") | |
1635 | (floatp "\\begin{figure}%placement | |
1636 | \\centering | |
1637 | \\includegraphics[%attr]{%path} | |
1638 | \\caption{%labelcmd%caption} | |
1639 | \\end{figure}") | |
1640 | (t "\\includegraphics[%attr]{%path}"))) | |
1641 | ||
1642 | (if (and (not label) (not caption) | |
1643 | (string-match "^\\\\caption{.*\n" figenv)) | |
1644 | (setq figenv (replace-match "" t t figenv))) | |
1645 | (org-fill-template | |
1646 | figenv | |
1647 | (list (cons "path" | |
1648 | (if (file-name-absolute-p path) | |
1649 | (expand-file-name path) | |
1650 | path)) | |
1651 | (cons "attr" attr) | |
1652 | (cons "labelcmd" (if label (format "\\label{%s}" | |
1653 | label)"")) | |
1654 | (cons "caption" (or caption "")) | |
1655 | (cons "placement" (or placement "")))))) | |
1656 | ||
54a0dee5 CD |
1657 | (defun org-export-latex-protect-amp (s) |
1658 | (while (string-match "\\([^\\\\]\\)\\(&\\)" s) | |
1659 | (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s)) | |
1660 | t t s))) | |
1661 | s) | |
1662 | ||
c8d0cf5c CD |
1663 | (defun org-remove-initial-hash (s) |
1664 | (if (string-match "\\`#" s) | |
1665 | (substring s 1) | |
1666 | s)) | |
db9c3fb1 | 1667 | (defvar org-latex-entities) ; defined below |
71d35b24 | 1668 | (defvar org-latex-entities-regexp) ; defined below |
c8d0cf5c | 1669 | (defvar org-latex-entities-exceptions) ; defined below |
db9c3fb1 | 1670 | |
c8d0cf5c | 1671 | (defun org-export-latex-preprocess (parameters) |
72a81656 | 1672 | "Clean stuff in the LaTeX export." |
0b8568f5 | 1673 | ;; Preserve line breaks |
72a81656 CD |
1674 | (goto-char (point-min)) |
1675 | (while (re-search-forward "\\\\\\\\" nil t) | |
1676 | (add-text-properties (match-beginning 0) (match-end 0) | |
1677 | '(org-protected t))) | |
1678 | ||
621f83e4 CD |
1679 | ;; Preserve latex environments |
1680 | (goto-char (point-min)) | |
c8d0cf5c | 1681 | (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t) |
71d35b24 | 1682 | (let* ((start (progn (beginning-of-line) (point))) |
c8d0cf5c CD |
1683 | (end (and (re-search-forward |
1684 | (concat "^[ \t]*\\\\end{" | |
1685 | (regexp-quote (match-string 1)) | |
1686 | "}") nil t) | |
1687 | (point-at-eol)))) | |
1688 | (if end | |
1689 | (add-text-properties start end '(org-protected t)) | |
1690 | (goto-char (point-at-eol))))) | |
621f83e4 | 1691 | |
0bd48b37 CD |
1692 | ;; Preserve math snippets |
1693 | ||
1694 | (let* ((matchers (plist-get org-format-latex-options :matchers)) | |
1695 | (re-list org-latex-regexps) | |
1696 | beg end re e m n block off) | |
1697 | ;; Check the different regular expressions | |
1698 | (while (setq e (pop re-list)) | |
1699 | (setq m (car e) re (nth 1 e) n (nth 2 e) | |
1700 | block (if (nth 3 e) "\n\n" "")) | |
1701 | (setq off (if (member m '("$" "$1")) 1 0)) | |
1702 | (when (and (member m matchers) (not (equal m "begin"))) | |
1703 | (goto-char (point-min)) | |
1704 | (while (re-search-forward re nil t) | |
1705 | (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0)) | |
1706 | (add-text-properties beg end '(org-protected t org-latex-math t)))))) | |
1707 | ||
d5098885 | 1708 | ;; Convert LaTeX to \LaTeX{} |
72a81656 | 1709 | (goto-char (point-min)) |
65c439fd | 1710 | (let ((case-fold-search nil)) |
72a81656 | 1711 | (while (re-search-forward "\\([^+_]\\)LaTeX" nil t) |
0bd48b37 CD |
1712 | (org-if-unprotected |
1713 | (replace-match (org-export-latex-protect-string | |
1714 | (concat (match-string 1) "\\LaTeX{}")) t t)))) | |
72a81656 | 1715 | |
b349f79f CD |
1716 | ;; Convert blockquotes |
1717 | (goto-char (point-min)) | |
621f83e4 | 1718 | (while (search-forward "ORG-BLOCKQUOTE-START" nil t) |
c8d0cf5c | 1719 | (org-replace-match-keep-properties "\\begin{quote}" t t)) |
b349f79f | 1720 | (goto-char (point-min)) |
621f83e4 | 1721 | (while (search-forward "ORG-BLOCKQUOTE-END" nil t) |
c8d0cf5c | 1722 | (org-replace-match-keep-properties "\\end{quote}" t t)) |
b349f79f CD |
1723 | |
1724 | ;; Convert verse | |
1725 | (goto-char (point-min)) | |
621f83e4 | 1726 | (while (search-forward "ORG-VERSE-START" nil t) |
c8d0cf5c CD |
1727 | (org-replace-match-keep-properties "\\begin{verse}" t t) |
1728 | (beginning-of-line 2) | |
1729 | (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp))) | |
1730 | (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)") | |
1731 | (goto-char (match-end 1)) | |
1732 | (org-replace-match-keep-properties | |
1733 | (org-export-latex-protect-string | |
1734 | (concat "\\hspace*{1cm}" (match-string 2))) t t) | |
1735 | (beginning-of-line 1)) | |
8d642074 | 1736 | (if (looking-at "[ \t]*$") |
8bfe682a | 1737 | (insert (org-export-latex-protect-string "\\vspace*{1em}")) |
8d642074 CD |
1738 | (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$") |
1739 | (end-of-line 1) | |
1740 | (insert "\\\\"))) | |
c8d0cf5c CD |
1741 | (beginning-of-line 2)) |
1742 | (and (looking-at "[ \t]*ORG-VERSE-END.*") | |
1743 | (org-replace-match-keep-properties "\\end{verse}" t t))) | |
1744 | ||
1745 | ;; Convert center | |
1746 | (goto-char (point-min)) | |
1747 | (while (search-forward "ORG-CENTER-START" nil t) | |
1748 | (org-replace-match-keep-properties "\\begin{center}" t t)) | |
b349f79f | 1749 | (goto-char (point-min)) |
c8d0cf5c CD |
1750 | (while (search-forward "ORG-CENTER-END" nil t) |
1751 | (org-replace-match-keep-properties "\\end{center}" t t)) | |
1752 | ||
1753 | (run-hooks 'org-export-latex-after-blockquotes-hook) | |
b349f79f | 1754 | |
0b8568f5 | 1755 | ;; Convert horizontal rules |
72a81656 CD |
1756 | (goto-char (point-min)) |
1757 | (while (re-search-forward "^----+.$" nil t) | |
0bd48b37 CD |
1758 | (org-if-unprotected |
1759 | (replace-match (org-export-latex-protect-string "\\hrule") t t))) | |
72a81656 | 1760 | |
33306645 | 1761 | ;; Protect LaTeX commands like \command[...]{...} or \command{...} |
c8d0cf5c CD |
1762 | (let ((re (concat "\\\\[a-zA-Z]+\\(?:" |
1763 | "\\[.*\\]" | |
1764 | "\\)?" | |
1765 | (org-create-multibrace-regexp "{" "}" 3)))) | |
1766 | (while (re-search-forward re nil t) | |
8d642074 CD |
1767 | (unless (save-excursion (goto-char (match-beginning 0)) |
1768 | (equal (char-after (point-at-bol)) ?#)) | |
1769 | (add-text-properties (match-beginning 0) (match-end 0) | |
1770 | '(org-protected t))))) | |
d5098885 | 1771 | |
db9c3fb1 CD |
1772 | ;; Protect LaTeX entities |
1773 | (goto-char (point-min)) | |
c8d0cf5c CD |
1774 | (let (a) |
1775 | (while (re-search-forward org-latex-entities-regexp nil t) | |
1776 | (if (setq a (assoc (match-string 0) org-latex-entities-exceptions)) | |
1777 | (replace-match (org-add-props (nth 1 a) nil 'org-protected t) | |
1778 | t t) | |
1779 | (add-text-properties (match-beginning 0) (match-end 0) | |
1780 | '(org-protected t))))) | |
db9c3fb1 | 1781 | |
72a81656 CD |
1782 | ;; Replace radio links |
1783 | (goto-char (point-min)) | |
d5098885 | 1784 | (while (re-search-forward |
db9c3fb1 | 1785 | (concat "<<<?" org-export-latex-all-targets-re |
d5098885 | 1786 | ">>>?\\((INVISIBLE)\\)?") nil t) |
8bfe682a | 1787 | (org-if-unprotected-at (+ (match-beginning 0) 2) |
0bd48b37 CD |
1788 | (replace-match |
1789 | (org-export-latex-protect-string | |
1790 | (format "\\label{%s}%s" (save-match-data (org-solidify-link-text | |
1791 | (match-string 1))) | |
1792 | (if (match-string 2) "" (match-string 1)))) t t))) | |
d5098885 | 1793 | |
0b8568f5 | 1794 | ;; Delete @<...> constructs |
0b8568f5 | 1795 | ;; Thanks to Daniel Clemente for this regexp |
4b67ea89 | 1796 | (goto-char (point-min)) |
0b8568f5 | 1797 | (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t) |
0bd48b37 CD |
1798 | (org-if-unprotected |
1799 | (replace-match ""))) | |
d5098885 | 1800 | |
0b8568f5 JW |
1801 | ;; When converting to LaTeX, replace footnotes |
1802 | ;; FIXME: don't protect footnotes from conversion | |
db9c3fb1 | 1803 | (when (plist-get org-export-latex-options-plist :footnotes) |
0b8568f5 | 1804 | (goto-char (point-min)) |
0bd48b37 CD |
1805 | (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t) |
1806 | (org-if-unprotected | |
8bfe682a CD |
1807 | (when (and (save-match-data |
1808 | (save-excursion (beginning-of-line) | |
1809 | (looking-at "[^:|#]"))) | |
1810 | (not (org-in-verbatim-emphasis))) | |
0bd48b37 CD |
1811 | (let ((foot-beg (match-beginning 0)) |
1812 | (foot-end (match-end 0)) | |
1813 | (foot-prefix (match-string 0)) | |
1814 | footnote footnote-rpl) | |
1815 | (save-excursion | |
1816 | (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix)) | |
1817 | nil t)) | |
5dec9555 CD |
1818 | (replace-match (org-export-latex-protect-string |
1819 | (concat "$^{" (match-string 1) "}$"))) | |
0bd48b37 CD |
1820 | (replace-match "") |
1821 | (let ((end (save-excursion | |
1822 | (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t) | |
1823 | (match-beginning 0) (point-max))))) | |
1824 | (setq footnote (concat (org-trim (buffer-substring (point) end)) | |
1825 | " ")) ; prevent last } being part of a link | |
1826 | (delete-region (point) end)) | |
1827 | (goto-char foot-beg) | |
1828 | (delete-region foot-beg foot-end) | |
1829 | (unless (null footnote) | |
1830 | (setq footnote-rpl (format "\\footnote{%s}" footnote)) | |
1831 | (add-text-properties 0 10 '(org-protected t) footnote-rpl) | |
1832 | (add-text-properties (1- (length footnote-rpl)) | |
1833 | (length footnote-rpl) | |
1834 | '(org-protected t) footnote-rpl) | |
1835 | (insert footnote-rpl))) | |
1836 | ))))) | |
1837 | ||
1838 | ;; Remove footnote section tag for LaTeX | |
0b8568f5 | 1839 | (goto-char (point-min)) |
db9c3fb1 | 1840 | (while (re-search-forward |
0b8568f5 | 1841 | (concat "^" footnote-section-tag-regexp) nil t) |
0bd48b37 CD |
1842 | (org-if-unprotected |
1843 | (replace-match ""))))) | |
72a81656 | 1844 | |
db9c3fb1 CD |
1845 | ;;; List handling: |
1846 | ||
1847 | (defun org-export-latex-lists () | |
0bd48b37 | 1848 | "Convert plain text lists in current buffer into LaTeX lists." |
db9c3fb1 | 1849 | (goto-char (point-min)) |
621f83e4 | 1850 | (while (re-search-forward org-list-beginning-re nil t) |
71d35b24 CD |
1851 | (org-if-unprotected |
1852 | (beginning-of-line) | |
0bd48b37 CD |
1853 | (insert (org-list-to-latex (org-list-parse-list t) |
1854 | org-export-latex-list-parameters)) | |
1855 | "\n"))) | |
db9c3fb1 | 1856 | |
db9c3fb1 CD |
1857 | (defconst org-latex-entities |
1858 | '("\\!" | |
1859 | "\\'" | |
1860 | "\\+" | |
1861 | "\\," | |
1862 | "\\-" | |
1863 | "\\:" | |
1864 | "\\;" | |
1865 | "\\<" | |
1866 | "\\=" | |
1867 | "\\>" | |
1868 | "\\Huge" | |
1869 | "\\LARGE" | |
1870 | "\\Large" | |
1871 | "\\Styles" | |
1872 | "\\\\" | |
1873 | "\\`" | |
1874 | "\\addcontentsline" | |
1875 | "\\address" | |
1876 | "\\addtocontents" | |
1877 | "\\addtocounter" | |
1878 | "\\addtolength" | |
1879 | "\\addvspace" | |
1880 | "\\alph" | |
1881 | "\\appendix" | |
1882 | "\\arabic" | |
1883 | "\\author" | |
1884 | "\\begin{array}" | |
1885 | "\\begin{center}" | |
1886 | "\\begin{description}" | |
1887 | "\\begin{enumerate}" | |
1888 | "\\begin{eqnarray}" | |
1889 | "\\begin{equation}" | |
1890 | "\\begin{figure}" | |
1891 | "\\begin{flushleft}" | |
1892 | "\\begin{flushright}" | |
1893 | "\\begin{itemize}" | |
1894 | "\\begin{list}" | |
1895 | "\\begin{minipage}" | |
1896 | "\\begin{picture}" | |
1897 | "\\begin{quotation}" | |
1898 | "\\begin{quote}" | |
1899 | "\\begin{tabbing}" | |
1900 | "\\begin{table}" | |
1901 | "\\begin{tabular}" | |
1902 | "\\begin{thebibliography}" | |
1903 | "\\begin{theorem}" | |
1904 | "\\begin{titlepage}" | |
1905 | "\\begin{verbatim}" | |
1906 | "\\begin{verse}" | |
1907 | "\\bf" | |
1908 | "\\bf" | |
1909 | "\\bibitem" | |
1910 | "\\bigskip" | |
1911 | "\\cdots" | |
1912 | "\\centering" | |
1913 | "\\circle" | |
1914 | "\\cite" | |
1915 | "\\cleardoublepage" | |
1916 | "\\clearpage" | |
1917 | "\\cline" | |
1918 | "\\closing" | |
1919 | "\\dashbox" | |
1920 | "\\date" | |
1921 | "\\ddots" | |
1922 | "\\dotfill" | |
1923 | "\\em" | |
1924 | "\\fbox" | |
1925 | "\\flushbottom" | |
1926 | "\\fnsymbol" | |
1927 | "\\footnote" | |
1928 | "\\footnotemark" | |
1929 | "\\footnotesize" | |
1930 | "\\footnotetext" | |
1931 | "\\frac" | |
1932 | "\\frame" | |
1933 | "\\framebox" | |
1934 | "\\hfill" | |
1935 | "\\hline" | |
1936 | "\\hrulespace" | |
1937 | "\\hspace" | |
1938 | "\\huge" | |
1939 | "\\hyphenation" | |
1940 | "\\include" | |
1941 | "\\includeonly" | |
1942 | "\\indent" | |
1943 | "\\input" | |
1944 | "\\it" | |
1945 | "\\kill" | |
1946 | "\\label" | |
1947 | "\\large" | |
1948 | "\\ldots" | |
1949 | "\\line" | |
1950 | "\\linebreak" | |
1951 | "\\linethickness" | |
1952 | "\\listoffigures" | |
1953 | "\\listoftables" | |
1954 | "\\location" | |
1955 | "\\makebox" | |
1956 | "\\maketitle" | |
1957 | "\\mark" | |
1958 | "\\mbox" | |
1959 | "\\medskip" | |
1960 | "\\multicolumn" | |
1961 | "\\multiput" | |
c8d0cf5c | 1962 | ("\\nbsp" "~") |
db9c3fb1 CD |
1963 | "\\newcommand" |
1964 | "\\newcounter" | |
1965 | "\\newenvironment" | |
1966 | "\\newfont" | |
1967 | "\\newlength" | |
1968 | "\\newline" | |
1969 | "\\newpage" | |
1970 | "\\newsavebox" | |
1971 | "\\newtheorem" | |
1972 | "\\nocite" | |
1973 | "\\nofiles" | |
1974 | "\\noindent" | |
1975 | "\\nolinebreak" | |
1976 | "\\nopagebreak" | |
1977 | "\\normalsize" | |
1978 | "\\onecolumn" | |
1979 | "\\opening" | |
1980 | "\\oval" | |
1981 | "\\overbrace" | |
1982 | "\\overline" | |
1983 | "\\pagebreak" | |
1984 | "\\pagenumbering" | |
1985 | "\\pageref" | |
1986 | "\\pagestyle" | |
1987 | "\\par" | |
1988 | "\\parbox" | |
1989 | "\\put" | |
1990 | "\\raggedbottom" | |
1991 | "\\raggedleft" | |
1992 | "\\raggedright" | |
1993 | "\\raisebox" | |
1994 | "\\ref" | |
1995 | "\\rm" | |
1996 | "\\roman" | |
1997 | "\\rule" | |
1998 | "\\savebox" | |
1999 | "\\sc" | |
2000 | "\\scriptsize" | |
2001 | "\\setcounter" | |
2002 | "\\setlength" | |
2003 | "\\settowidth" | |
2004 | "\\sf" | |
2005 | "\\shortstack" | |
2006 | "\\signature" | |
2007 | "\\sl" | |
2008 | "\\small" | |
2009 | "\\smallskip" | |
2010 | "\\sqrt" | |
2011 | "\\tableofcontents" | |
2012 | "\\telephone" | |
2013 | "\\thanks" | |
2014 | "\\thispagestyle" | |
2015 | "\\tiny" | |
2016 | "\\title" | |
2017 | "\\tt" | |
2018 | "\\twocolumn" | |
2019 | "\\typein" | |
2020 | "\\typeout" | |
2021 | "\\underbrace" | |
2022 | "\\underline" | |
2023 | "\\usebox" | |
2024 | "\\usecounter" | |
2025 | "\\value" | |
2026 | "\\vdots" | |
2027 | "\\vector" | |
2028 | "\\verb" | |
2029 | "\\vfill" | |
2030 | "\\vline" | |
2031 | "\\vspace") | |
2032 | "A list of LaTeX commands to be protected when performing conversion.") | |
2033 | ||
c8d0cf5c CD |
2034 | (defvar org-latex-entities-exceptions nil) |
2035 | ||
71d35b24 CD |
2036 | (defconst org-latex-entities-regexp |
2037 | (let (names rest) | |
2038 | (dolist (x org-latex-entities) | |
c8d0cf5c CD |
2039 | (when (consp x) |
2040 | (add-to-list 'org-latex-entities-exceptions x) | |
2041 | (setq x (car x))) | |
8bfe682a | 2042 | (if (string-match "[a-zA-Z]$" x) |
71d35b24 CD |
2043 | (push x names) |
2044 | (push x rest))) | |
2045 | (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)" | |
2046 | "\\|\\(" (regexp-opt (nreverse rest)) "\\)"))) | |
2047 | ||
72a81656 | 2048 | (provide 'org-export-latex) |
c8d0cf5c | 2049 | (provide 'org-latex) |
72a81656 | 2050 | |
b1634ed2 | 2051 | ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad |
b349f79f | 2052 | |
c8d0cf5c | 2053 | ;;; org-latex.el ends here |