org-export-latex.el 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. ;;; org-export-latex.el --- latex exporter for org-mode
  2. ;; copyright (c) 2007 free software foundation, inc.
  3. ;;
  4. ;; Author: Bastien Guerry <bzg AT altern DOT org>
  5. ;; Keywords: Org organizer latex export convert
  6. ;; Homepage: http://www.cognition.ens.fr/~guerry/u/org-export-latex.el
  7. ;; Version: 5.10
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by the
  13. ;; Free Software Foundation; either version 3, or (at your option) any
  14. ;; later version.
  15. ;;
  16. ;; GNU Emacs is distributed in the hope that it will be useful, but WITHOUT
  17. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. ;; more details.
  20. ;;
  21. ;; You should have received a copy of the GNU General Public License along
  22. ;; with GNU Emacs; see the file COPYING. If not, write to the Free Software
  23. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. ;; 02110-1301, USA.
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; This library implements a LaTeX exporter for org-mode.
  29. ;;
  30. ;; Put this file into your load-path and the following into your ~/.emacs:
  31. ;; (require 'org-export-latex)
  32. ;;
  33. ;; The interactive functions are similar to those of the HTML exporter:
  34. ;;
  35. ;; M-x `org-export-as-latex'
  36. ;; M-x `org-export-as-latex-batch'
  37. ;; M-x `org-export-as-latex-to-buffer'
  38. ;; M-x `org-export-region-as-latex'
  39. ;; M-x `org-replace-region-by-latex'
  40. ;;
  41. ;;; Code:
  42. (eval-when-compile
  43. (require 'cl))
  44. (require 'footnote)
  45. (require 'org)
  46. ;;; Variables:
  47. (defvar org-latex-options-plist nil)
  48. (defvar org-latex-todo-keywords-1 nil)
  49. (defvar org-latex-all-targets-regexp nil)
  50. (defvar org-latex-add-level 0)
  51. (defvar org-latex-sectioning-depth 0)
  52. (defvar org-export-latex-list-beginning-re
  53. "^\\([ \t]*\\)\\([-+]\\|[0-9]+\\(?:\\.\\|)\\)\\) *?")
  54. (defvar org-latex-special-string-regexps
  55. '(org-ts-regexp
  56. org-scheduled-string
  57. org-deadline-string
  58. org-clock-string)
  59. "A list of regexps to convert as special keywords.")
  60. (defvar latexp) ; dynamically scoped from org.el
  61. (defvar re-quote) ; dynamically scoped from org.el
  62. (defvar commentsp) ; dynamically scoped from org.el
  63. ;;; Custom variables:
  64. (defcustom org-export-latex-sectioning-alist
  65. '((1 "\\section{%s}" "\\section*{%s}")
  66. (2 "\\subsection{%s}" "\\subsection*{%s}")
  67. (3 "\\subsubsection{%s}" "\\subsubsection*{%s}")
  68. (4 "\\paragraph{%s}" "\\paragraph*{%s}")
  69. (5 "\\subparagraph{%s}" "\\subparagraph*{%s}"))
  70. "Alist of LaTeX commands for inserting sections.
  71. Here is the structure of each cell:
  72. \(level unnumbered-section numbered-section\)
  73. The %s formatter will be replaced by the title of the section."
  74. :group 'org-export-latex
  75. :type 'alist)
  76. (defcustom org-export-latex-emphasis-alist
  77. '(("*" "\\textbf{%s}" nil)
  78. ("/" "\\emph{%s}" nil)
  79. ("_" "\\underline{%s}" nil)
  80. ("+" "\\texttt{%s}" nil)
  81. ("=" "\\texttt{%s}" t))
  82. "Alist of LaTeX expressions to convert emphasis fontifiers.
  83. Each element of the list is a list of three elements.
  84. The first element is the character used as a marker for fontification.
  85. The second element is a formatting string to wrap fontified text with.
  86. If non-nil the third element tells to protect converted text from other
  87. conversions."
  88. :group 'org-export-latex
  89. :type 'alist)
  90. (defcustom org-export-latex-preamble
  91. "\\documentclass[11pt,a4paper]{article}
  92. \\usepackage[utf8]{inputenc}
  93. \\usepackage[T1]{fontenc}
  94. \\usepackage{hyperref}"
  95. "Preamble to be inserted at the very beginning of the LaTeX export."
  96. :group 'org-export-latex
  97. :type 'string)
  98. (defcustom org-export-latex-date-format
  99. "%d %B %Y"
  100. "Format string for \\date{...}."
  101. :group 'org-export-latex
  102. :type 'string)
  103. (defcustom org-export-latex-tables-verbatim nil
  104. "When non-nil, export tables as verbatim."
  105. :group 'org-export-latex
  106. :type 'boolean)
  107. (defcustom org-export-latex-packages-alist nil
  108. "Alist of packages to be inserted in the preamble.
  109. Each cell is of the forma \( option . package \).
  110. For example:
  111. \(setq org-export-latex-packages-alist
  112. '((\"french\" \"babel\"))"
  113. :group 'org-export-latex
  114. :type 'alist)
  115. (defcustom org-export-latex-low-levels 'description
  116. "Choice for converting sections that are below the current
  117. admitted level of sectioning. This can be either nil (ignore the
  118. sections), 'description (convert them as description lists) or a
  119. string to be used instead of \\section{%s} (a %s for inserted the
  120. headline is mandatory)."
  121. :group 'org-export-latex
  122. :type '(choice (const :tag "Ignore" nil)
  123. (symbol :tag "Convert as descriptive list" description)
  124. (string :tag "Use a section string" :value "\\subparagraph{%s}")))
  125. (defcustom org-export-latex-remove-from-headlines
  126. '(:todo t :priority t :tags t)
  127. "A plist of keywords to remove from headlines.
  128. Non-nil means remove this keyword type from the headline.
  129. Don't remove the keys, just change their values."
  130. :type 'plist
  131. :group 'org-export-latex)
  132. (defcustom org-export-latex-image-default-option "width=10em"
  133. "Default option for images."
  134. :group 'org-export-latex
  135. :type '(string))
  136. (defcustom org-export-latex-coding-system nil
  137. "Coding system for the exported LaTex file."
  138. :group 'org-export-latex
  139. :type 'coding-system)
  140. ;; FIXME Do we want this one?
  141. ;; (defun org-export-as-latex-and-open (arg) ...)
  142. ;;; Autoload functions:
  143. ;;;###autoload
  144. (defun org-export-as-latex-batch ()
  145. "Call `org-export-as-latex', may be used in batch processing as
  146. emacs --batch
  147. --load=$HOME/lib/emacs/org.el
  148. --eval \"(setq org-export-headline-levels 2)\"
  149. --visit=MyFile --funcall org-export-as-latex-batch"
  150. (org-export-as-latex org-export-headline-levels 'hidden))
  151. ;;;###autoload
  152. (defun org-export-as-latex-to-buffer (arg)
  153. "Call `org-exort-as-latex` with output to a temporary buffer.
  154. No file is created. The prefix ARG is passed through to `org-export-as-latex'."
  155. (interactive "P")
  156. (org-export-as-latex arg nil nil "*Org LaTeX Export*")
  157. (switch-to-buffer-other-window "*Org LaTeX Export*"))
  158. ;;;###autoload
  159. (defun org-replace-region-by-latex (beg end)
  160. "Replace the region from BEG to END with its LaTeX export.
  161. It assumes the region has `org-mode' syntax, and then convert it to
  162. LaTeX. This can be used in any buffer. For example, you could
  163. write an itemized list in `org-mode' syntax in an LaTeX buffer and
  164. then use this command to convert it."
  165. (interactive "r")
  166. (let (reg latex buf)
  167. (save-window-excursion
  168. (if (org-mode-p)
  169. (setq latex (org-export-region-as-latex
  170. beg end t 'string))
  171. (setq reg (buffer-substring beg end)
  172. buf (get-buffer-create "*Org tmp*"))
  173. (save-excursion
  174. (set-buffer buf)
  175. (erase-buffer)
  176. (insert reg)
  177. (org-mode)
  178. (setq latex (org-export-region-as-latex
  179. (point-min) (point-max) t 'string)))
  180. (kill-buffer buf)))
  181. (delete-region beg end)
  182. (insert latex)))
  183. ;;;###autoload
  184. (defun org-export-region-as-latex (beg end &optional body-only buffer)
  185. "Convert region from BEG to END in `org-mode' buffer to LaTeX.
  186. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  187. contents, and only produce the region of converted text, useful for
  188. cut-and-paste operations.
  189. If BUFFER is a buffer or a string, use/create that buffer as a target
  190. of the converted LaTeX. If BUFFER is the symbol `string', return the
  191. produced LaTeX as a string and leave not buffer behind. For example,
  192. a Lisp program could call this function in the following way:
  193. (setq latex (org-export-region-as-latex beg end t 'string))
  194. When called interactively, the output buffer is selected, and shown
  195. in a window. A non-interactive call will only retunr the buffer."
  196. (interactive "r\nP")
  197. (when (interactive-p)
  198. (setq buffer "*Org LaTeX Export*"))
  199. (let ((transient-mark-mode t) (zmacs-regions t)
  200. rtn)
  201. (goto-char end)
  202. (set-mark (point)) ;; to activate the region
  203. (goto-char beg)
  204. (setq rtn (org-export-as-latex
  205. nil nil nil
  206. buffer body-only))
  207. (if (fboundp 'deactivate-mark) (deactivate-mark))
  208. (if (and (interactive-p) (bufferp rtn))
  209. (switch-to-buffer-other-window rtn)
  210. rtn)))
  211. ;;;###autoload
  212. (defun org-export-as-latex (arg &optional hidden ext-plist
  213. to-buffer body-only)
  214. "Export current buffer to a LaTeX file."
  215. (interactive "P")
  216. ;; Make sure we have a file name when we need it.
  217. (when (and (not (or to-buffer body-only))
  218. (not buffer-file-name))
  219. (if (buffer-base-buffer)
  220. (org-set-local 'buffer-file-name
  221. (with-current-buffer (buffer-base-buffer)
  222. buffer-file-name))
  223. (error "Need a file name to be able to export")))
  224. (message "Exporting to LaTeX...")
  225. (org-update-radio-target-regexp)
  226. (org-export-latex-set-initial-vars ext-plist)
  227. (let* ((opt-plist org-latex-options-plist)
  228. (filename (concat (file-name-as-directory
  229. (org-export-directory :LaTeX ext-plist))
  230. (file-name-sans-extension
  231. (file-name-nondirectory ;sans-extension
  232. buffer-file-name)) ".tex"))
  233. (filename (if (equal (file-truename filename)
  234. (file-truename buffer-file-name))
  235. (concat filename ".tex")
  236. filename))
  237. (buffer (if to-buffer
  238. (cond
  239. ((eq to-buffer 'string) (get-buffer-create
  240. "*Org LaTeX Export*"))
  241. (t (get-buffer-create to-buffer)))
  242. (find-file-noselect filename)))
  243. (region-p (org-region-active-p))
  244. (odd org-odd-levels-only)
  245. (preamble (org-export-latex-make-preamble opt-plist))
  246. (skip (plist-get opt-plist :skip-before-1st-heading))
  247. (text (plist-get opt-plist :text))
  248. (first-lines (if skip "" (org-export-latex-first-lines)))
  249. (coding-system (and (boundp 'buffer-file-coding-system)
  250. buffer-file-coding-system))
  251. (coding-system-for-write (or org-export-latex-coding-system
  252. coding-system))
  253. (save-buffer-coding-system (or org-export-latex-coding-system
  254. coding-system))
  255. (region (buffer-substring
  256. (if region-p (region-beginning) (point-min))
  257. (if region-p (region-end) (point-max))))
  258. (string-for-export
  259. (org-cleaned-string-for-export
  260. region :emph-multiline t
  261. :for-LaTeX t
  262. :comments nil
  263. :add-text text
  264. :skip-before-1st-heading skip
  265. :LaTeX-fragments nil)))
  266. (set-buffer buffer)
  267. (erase-buffer)
  268. (unless body-only (insert preamble))
  269. (when text (insert (org-export-latex-content text) "\n\n"))
  270. (unless skip (insert first-lines))
  271. ;; handle the case where the region does not begin with a section
  272. (when region-p
  273. (insert (with-temp-buffer
  274. (insert string-for-export)
  275. (org-export-latex-first-lines))))
  276. (org-export-latex-global
  277. (with-temp-buffer
  278. (insert string-for-export)
  279. (goto-char (point-min))
  280. (re-search-forward "^\\(\\*+\\) " nil t)
  281. (let* ((asters (length (match-string 1)))
  282. (level (if odd (- asters 2) (- asters 1))))
  283. (setq org-latex-add-level
  284. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  285. (org-export-latex-parse-global level odd))))
  286. (unless body-only (insert "\n\\end{document}"))
  287. (or to-buffer (save-buffer))
  288. (goto-char (point-min))
  289. (message "Exporting to LaTeX...done")
  290. (if (eq to-buffer 'string)
  291. (prog1 (buffer-substring (point-min) (point-max))
  292. (kill-buffer (current-buffer)))
  293. (current-buffer))))
  294. ;;; Parsing functions:
  295. (defun org-export-latex-parse-global (level odd)
  296. "Parse the current buffer recursively, starting at LEVEL.
  297. If ODD is non-nil, assume the buffer only contains odd sections.
  298. Return A list reflecting the document structure."
  299. (save-excursion
  300. (goto-char (point-min))
  301. (let* ((cnt 0) output
  302. (depth org-latex-sectioning-depth))
  303. (while (re-search-forward
  304. (concat "^\\(\\(?:\\*\\)\\{"
  305. (number-to-string (+ (if odd 2 1) level))
  306. "\\}\\) \\(.*\\)$")
  307. ;; make sure that there is no upper heading
  308. (when (> level 0)
  309. (save-excursion
  310. (save-match-data
  311. (re-search-forward
  312. (concat "^\\(\\(?:\\*\\)\\{"
  313. (number-to-string level)
  314. "\\}\\) \\(.*\\)$") nil t)))) t)
  315. (setq cnt (1+ cnt))
  316. (let* ((pos (match-beginning 0))
  317. (heading (match-string 2))
  318. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  319. (save-excursion
  320. (narrow-to-region
  321. (point)
  322. (save-match-data
  323. (if (re-search-forward
  324. (concat "^\\(\\(?:\\*\\)\\{"
  325. (number-to-string (+ (if odd 2 1) level))
  326. "\\}\\) \\(.*\\)$") nil t)
  327. (match-beginning 0)
  328. (point-max))))
  329. (goto-char (point-min))
  330. (setq output
  331. (append output
  332. (list
  333. (list
  334. `(pos . ,pos)
  335. `(level . ,nlevel)
  336. `(occur . ,cnt)
  337. `(heading . ,heading)
  338. `(content . ,(org-export-latex-parse-content))
  339. `(subcontent . ,(org-export-latex-parse-subcontent
  340. level odd)))))))
  341. (widen)))
  342. (list output))))
  343. (defun org-export-latex-parse-list (&optional delete)
  344. "Parse the list at point.
  345. Return a list containing first level items as strings and
  346. sublevels as list of strings."
  347. (let ((start (point))
  348. ;; Find the end of the list
  349. (end (save-excursion
  350. (catch 'exit
  351. (while (or (looking-at org-export-latex-list-beginning-re)
  352. (looking-at "^[ \t]+\\|^$"))
  353. (if (eq (point) (point-max))
  354. (throw 'exit (point-max)))
  355. (forward-line 1))) (point)))
  356. output itemsep)
  357. (while (re-search-forward org-export-latex-list-beginning-re end t)
  358. (setq itemsep (if (save-match-data
  359. (string-match "^[0-9]" (match-string 2)))
  360. "[0-9]+\\(?:\\.\\|)\\)" "[-+]"))
  361. (let* ((indent1 (match-string 1))
  362. (nextitem (save-excursion
  363. (save-match-data
  364. (or (and (re-search-forward
  365. (concat "^" indent1 itemsep " *?") end t)
  366. (match-beginning 0)) end))))
  367. (item (buffer-substring
  368. (point)
  369. (or (and (re-search-forward
  370. org-export-latex-list-beginning-re end t)
  371. (goto-char (match-beginning 0)))
  372. (goto-char end))))
  373. (nextindent (match-string 1))
  374. (item (org-trim item))
  375. (item (if (string-match "^\\[.+\\]" item)
  376. (replace-match "\\\\texttt{\\&}"
  377. t nil item) item)))
  378. (push item output)
  379. (when (> (length nextindent)
  380. (length indent1))
  381. (narrow-to-region (point) nextitem)
  382. (push (org-export-latex-parse-list) output)
  383. (widen))))
  384. (when delete (delete-region start end))
  385. (setq output (nreverse output))
  386. (push (if (string-match "^\\[0" itemsep)
  387. 'ordered 'unordered) output)))
  388. (defun org-export-latex-parse-content ()
  389. "Extract the content of a section."
  390. (let ((beg (point))
  391. (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
  392. (progn (beginning-of-line) (point))
  393. (point-max))))
  394. (buffer-substring beg end)))
  395. (defun org-export-latex-parse-subcontent (level odd)
  396. "Extract the subcontent of a section at LEVEL.
  397. If ODD Is non-nil, assume subcontent only contains odd sections."
  398. (if (not (re-search-forward
  399. (concat "^\\(\\(?:\\*\\)\\{"
  400. (number-to-string (+ (if odd 4 2) level))
  401. "\\}\\) \\(.*\\)$")
  402. nil t))
  403. nil ; subcontent is nil
  404. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  405. ;;; Rendering functions:
  406. (defun org-export-latex-global (content)
  407. "Export CONTENT to LaTeX.
  408. CONTENT is an element of the list produced by
  409. `org-export-latex-parse-global'."
  410. (if (eq (car content) 'subcontent)
  411. (mapc 'org-export-latex-sub (cdr content))
  412. (org-export-latex-sub (car content))))
  413. (defun org-export-latex-sub (subcontent)
  414. "Export the list SUBCONTENT to LaTeX.
  415. SUBCONTENT is an alist containing information about the headline
  416. and its content."
  417. (let ((num (plist-get org-latex-options-plist :section-numbers)))
  418. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  419. (defun org-export-latex-subcontent (subcontent num)
  420. "Export each cell of SUBCONTENT to LaTeX."
  421. (let ((heading (org-export-latex-fontify-headline
  422. (cdr (assoc 'heading subcontent))))
  423. (level (- (cdr (assoc 'level subcontent))
  424. org-latex-add-level))
  425. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  426. (content (cdr (assoc 'content subcontent)))
  427. (subcontent (cadr (assoc 'subcontent subcontent))))
  428. (cond
  429. ;; Normal conversion
  430. ((<= level org-latex-sectioning-depth)
  431. (let ((sec (assoc level org-export-latex-sectioning-alist)))
  432. (insert (format (if num (cadr sec) (caddr sec)) heading) "\n"))
  433. (insert (org-export-latex-content content))
  434. (cond ((stringp subcontent) (insert subcontent))
  435. ((listp subcontent) (org-export-latex-sub subcontent))))
  436. ;; At a level under the hl option: we can drop this subsection
  437. ((> level org-latex-sectioning-depth)
  438. (cond ((eq org-export-latex-low-levels 'description)
  439. (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading))
  440. (insert (org-export-latex-content content))
  441. (cond ((stringp subcontent) (insert subcontent))
  442. ((listp subcontent) (org-export-latex-sub subcontent)))
  443. (insert "\\end{description}\n"))
  444. ((stringp org-export-latex-low-levels)
  445. (insert (format org-export-latex-low-levels heading) "\n")
  446. (insert (org-export-latex-content content))
  447. (cond ((stringp subcontent) (insert subcontent))
  448. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  449. ;;; Exporting internals:
  450. (defun org-latex-protect (string)
  451. (add-text-properties 0 (length string) '(org-protected t) string) string)
  452. (defun org-export-latex-protect-char-in-string (char-list string)
  453. "Add org-protected text-property to char from CHAR-LIST in STRING."
  454. (with-temp-buffer
  455. (save-match-data
  456. (insert string)
  457. (goto-char (point-min))
  458. (while (re-search-forward (regexp-opt char-list) nil t)
  459. (add-text-properties (match-beginning 0)
  460. (match-end 0) '(org-protected t)))
  461. (buffer-string))))
  462. (defun org-export-latex-set-initial-vars (ext-plist)
  463. "Store org local variables required for LaTeX export.
  464. EXT-PLIST is an optional additional plist."
  465. (setq org-latex-todo-keywords-1 org-todo-keywords-1
  466. org-latex-all-targets-regexp
  467. (org-make-target-link-regexp (org-all-targets))
  468. org-latex-options-plist
  469. (org-combine-plists (org-default-export-plist) ext-plist
  470. (org-infile-export-plist))
  471. org-latex-sectioning-depth
  472. (let ((hl-levels (plist-get org-latex-options-plist :headline-levels))
  473. (sec-depth (length org-export-latex-sectioning-alist)))
  474. ;; Fall back on org-export-latex-sectioning-alist length if
  475. ;; headline-levels goes beyond it
  476. (if (> hl-levels sec-depth) sec-depth hl-levels))))
  477. (defun org-export-latex-make-preamble (opt-plist)
  478. "Make the LaTeX preamble and return it as a string.
  479. Argument OPT-PLIST is the options plist for current buffer."
  480. (let ((toc (plist-get opt-plist :table-of-contents)))
  481. (concat (if (plist-get opt-plist :time-stamp-file)
  482. (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
  483. ;; LaTeX custom preamble
  484. org-export-latex-preamble "\n"
  485. ;; LaTeX packages
  486. (if org-export-latex-packages-alist
  487. (mapconcat (lambda(p)
  488. (if (equal "" (car p))
  489. (format "\\usepackage{%s}" (cadr p))
  490. (format "\\usepackage[%s]{%s}"
  491. (car p) (cadr p))))
  492. org-export-latex-packages-alist "\n") "")
  493. "\n\\begin{document}\n\n"
  494. ;; title
  495. (format
  496. "\\title{%s}\n"
  497. (or (plist-get opt-plist :title)
  498. (and (not
  499. (plist-get opt-plist :skip-before-1st-heading))
  500. (org-export-grab-title-from-buffer))
  501. (and buffer-file-name
  502. (file-name-sans-extension
  503. (file-name-nondirectory buffer-file-name)))
  504. "UNTITLED"))
  505. ;; author info
  506. (if (plist-get opt-plist :author-info)
  507. (format "\\author{%s}\n"
  508. (or (plist-get opt-plist :author) user-full-name))
  509. (format "%%\\author{%s}\n"
  510. (or (plist-get opt-plist :author) user-full-name)))
  511. ;; date
  512. (format "\\date{%s}\n"
  513. (format-time-string
  514. (or (plist-get opt-plist :date)
  515. org-export-latex-date-format)))
  516. "\\maketitle\n\n"
  517. ;; table of contents
  518. (if (and (plist-get opt-plist :section-numbers) toc)
  519. (format "\\setcounter{tocdepth}{%s}\n"
  520. (min toc (plist-get opt-plist :headline-levels))) "")
  521. (if (and (plist-get opt-plist :section-numbers) toc)
  522. "\\tableofcontents\n" "\n"))))
  523. (defun org-export-latex-first-lines (&optional comments)
  524. "Export the first lines before first headline.
  525. COMMENTS is either nil to replace them with the empty string or a
  526. formatting string like %%%%s if we want to comment them out."
  527. (save-excursion
  528. (goto-char (point-min))
  529. (let* ((end (if (re-search-forward "^\\*" nil t)
  530. (goto-char (match-beginning 0))
  531. (goto-char (point-max)))))
  532. (org-export-latex-content
  533. (org-cleaned-string-for-export
  534. (buffer-substring (point-min) end)
  535. :for-LaTeX t
  536. :emph-multiline t
  537. :add-text nil
  538. :comments nil
  539. :skip-before-1st-heading nil
  540. :LaTeX-fragments nil)))))
  541. (defun org-export-latex-keywords-maybe (remove-list)
  542. "Maybe remove keywords depending on rules in REMOVE-LIST."
  543. (goto-char (point-min))
  544. (let ((re-todo (mapconcat 'identity org-latex-todo-keywords-1 "\\|")))
  545. ;; convert TODO keywords
  546. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  547. (if (plist-get remove-list :todo)
  548. (replace-match "")
  549. (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
  550. ;; convert priority string
  551. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  552. (if (plist-get remove-list :priority)
  553. (replace-match "")
  554. (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
  555. ;; convert tags
  556. (when (re-search-forward "\\(:[a-zA-Z0-9]+\\)+:" nil t)
  557. (if (or (not org-export-with-tags)
  558. (plist-get remove-list :tags))
  559. (replace-match "")
  560. (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))))
  561. (defun org-export-latex-fontify-headline (headline)
  562. "Fontify special words in a HEADLINE."
  563. (with-temp-buffer
  564. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  565. ;; the beginning of the buffer - inserting "\n" is safe here though.
  566. (insert "\n" headline)
  567. (goto-char (point-min))
  568. (when (plist-get org-latex-options-plist :emphasize)
  569. (org-export-latex-fontify))
  570. (org-export-latex-special-chars
  571. (plist-get org-latex-options-plist :sub-superscript))
  572. (org-export-latex-keywords-maybe
  573. org-export-latex-remove-from-headlines)
  574. (org-export-latex-links)
  575. (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
  576. (defun org-export-latex-content (content)
  577. "Convert CONTENT string to LaTeX."
  578. (with-temp-buffer
  579. (insert content)
  580. (org-export-latex-quotation-marks)
  581. (when (plist-get org-latex-options-plist :emphasize)
  582. (org-export-latex-fontify))
  583. (org-export-latex-special-chars
  584. (plist-get org-latex-options-plist :sub-superscript))
  585. (org-export-latex-links)
  586. (org-export-latex-keywords
  587. (plist-get org-latex-options-plist :timestamps))
  588. (org-export-latex-lists)
  589. (org-export-latex-tables
  590. (plist-get org-latex-options-plist :tables))
  591. (org-export-latex-fixed-width
  592. (plist-get org-latex-options-plist :fixed-width))
  593. (buffer-substring (point-min) (point-max))))
  594. (defun org-export-latex-quotation-marks ()
  595. "Export question marks depending on language conventions.
  596. Local definition of the language overrides
  597. `org-export-latex-quotation-marks-convention' which overrides
  598. `org-export-default-language'."
  599. (let* ((lang (plist-get org-latex-options-plist :language))
  600. (quote-rpl (if (equal lang "fr")
  601. '(("\\(\\s-\\)\"" "«~")
  602. ("\\(\\S-\\)\"" "~»")
  603. ("\\(\\s-\\)'" "`"))
  604. '(("\\(\\s-\\)\"" "``")
  605. ("\\(\\S-\\)\"" "''")
  606. ("\\(\\s-\\)'" "`")))))
  607. (mapc (lambda(l) (goto-char (point-min))
  608. (while (re-search-forward (car l) nil t)
  609. (let ((rpl (concat (match-string 1) (cadr l))))
  610. (org-latex-protect rpl)
  611. (org-if-unprotected
  612. (replace-match rpl t t))))) quote-rpl)))
  613. ;; | chars/string in Org | normal environment | math environment |
  614. ;; |-----------------------+-----------------------+-----------------------|
  615. ;; | & # % $ | \& \# \% \$ | \& \# \% \$ |
  616. ;; | { } _ ^ \ | \{ \} \_ \^ \\ | { } _ ^ \ |
  617. ;; |-----------------------+-----------------------+-----------------------|
  618. ;; | a_b and a^b | $a_b$ and $a^b$ | a_b and a^b |
  619. ;; | a_abc and a_{abc} | $a_a$bc and $a_{abc}$ | a_abc and a_{abc} |
  620. ;; | \tau and \mu | $\tau$ and $\mu$ | \tau and \mu |
  621. ;; |-----------------------+-----------------------+-----------------------|
  622. ;; | \_ \^ | \_ \^ | \_ \^ |
  623. ;; | \(a=\mu\mbox{m}\) | \(a=\mu\mbox{m}\) | \(a=\mu\mbox{m}\) |
  624. ;; | \[\beta^2-a=0\] | \[\beta^2-a=0\] | \[\beta^2-a=0\] |
  625. ;; | $x=22\tau$ | $x=22\tau$ | $x=22\tau$ |
  626. ;; | $$\alpha=\sqrt{a^3}$$ | $$\alpha=\sqrt{a^3}$$ | $$\alpha=\sqrt{a^3}$$ |
  627. (defun org-export-latex-special-chars (sub-superscript)
  628. "Export special characters to LaTeX.
  629. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  630. See the `org-export-latex.el' code for a complete conversion table."
  631. (goto-char (point-min))
  632. (mapc (lambda(c)
  633. (goto-char (point-min))
  634. (while (re-search-forward c nil t)
  635. ;; Put the point where to check for org-protected
  636. (unless (get-text-property (match-beginning 2) 'org-protected)
  637. (cond ((member (match-string 2) '("\\$" "$"))
  638. (if (equal (match-string 2) "\\$")
  639. (replace-match (concat (match-string 1) "$"
  640. (match-string 3)) t t)
  641. (replace-match (concat (match-string 1) "\\$"
  642. (match-string 3)) t t)))
  643. ((member (match-string 2) '("&" "#" "%"))
  644. (if (equal (match-string 1) "\\")
  645. (replace-match (match-string 2) t t)
  646. (replace-match (concat (match-string 1) "\\"
  647. (match-string 2)) t t)))
  648. ((equal (match-string 2) "~")
  649. (cond ((equal (match-string 1) "\\") nil)
  650. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  651. (replace-match (concat (match-string 1) "\\~") t t))
  652. (t (replace-match
  653. (org-latex-protect
  654. (concat (match-string 1) "\\~{}")) t t))))
  655. ((member (match-string 2) '("{" "}"))
  656. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  657. (if (equal (match-string 1) "\\")
  658. (replace-match (match-string 2) t t)
  659. (replace-match (concat (match-string 1) "\\"
  660. (match-string 2)) t t)))))
  661. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  662. (cond ((equal (match-string 2) "\\")
  663. (replace-match (or (save-match-data
  664. (org-export-latex-treat-backslash-char
  665. (match-string 1)
  666. (match-string 3))) "") t t))
  667. ((member (match-string 2) '("_" "^"))
  668. (replace-match (or (save-match-data
  669. (org-export-latex-treat-sub-super-char
  670. sub-superscript
  671. (match-string 1)
  672. (match-string 2)
  673. (match-string 3))) "") t t)))))))
  674. '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  675. "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
  676. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
  677. "\\(.\\|^\\)\\(&\\)"
  678. "\\(.\\|^\\)\\(#\\)"
  679. "\\(.\\|^\\)\\(%\\)"
  680. "\\(.\\|^\\)\\({\\)"
  681. "\\(.\\|^\\)\\(}\\)"
  682. "\\(.\\|^\\)\\(~\\)")))
  683. (defun org-export-latex-treat-sub-super-char
  684. (subsup string-before char string-after)
  685. "Convert the \"_\" and \"^\" characters to LaTeX.
  686. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  687. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  688. (cond ((equal string-before "\\")
  689. (concat string-before char string-after))
  690. ;; this is part of a math formula
  691. ((and (string-match "\\S-+" string-before)
  692. (string-match "\\S-+" string-after))
  693. (cond ((eq 'org-link (get-text-property 0 'face char))
  694. (concat string-before "\\" char string-after))
  695. ((save-match-data (org-inside-LaTeX-fragment-p))
  696. (if subsup
  697. (cond ((eq 1 (length string-after))
  698. (concat string-before char string-after))
  699. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  700. (format "%s%s{%s}" string-before char
  701. (match-string 1 string-after))))))
  702. ((and subsup
  703. (> (length string-after) 1)
  704. (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
  705. (format "$%s%s{%s}$" string-before char
  706. (match-string 1 string-after)))
  707. (subsup (concat "$" string-before char string-after "$"))
  708. (t (org-latex-protect
  709. (concat string-before "\\" char "{}" string-after)))))
  710. (t (org-latex-protect
  711. (concat string-before "\\" char "{}" string-after)))))
  712. (defun org-export-latex-treat-backslash-char (string-before string-after)
  713. "Convert the \"$\" special character to LaTeX.
  714. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  715. (cond ((member (list string-after) org-html-entities)
  716. ;; backslash is part of a special entity (like "\alpha")
  717. (concat string-before "$\\"
  718. (or (cdar (member (list string-after) org-html-entities))
  719. string-after) "$"))
  720. ((and (not (string-match "^[ \n\t]" string-after))
  721. (not (string-match "[ \t]\\'\\|^" string-before)))
  722. ;; backslash is inside a word
  723. (concat string-before "$\\backslash$" string-after))
  724. ((not (or (equal string-after "")
  725. (string-match "^[ \t\n]" string-after)))
  726. ;; backslash might escape a character (like \#) or a user TeX
  727. ;; macro (like \setcounter)
  728. (concat string-before "\\" string-after))
  729. ((and (string-match "^[ \t\n]" string-after)
  730. (string-match "[ \t\n]\\'" string-before))
  731. ;; backslash is alone, convert it to $\backslash$
  732. (concat string-before "$\\backslash$" string-after))
  733. (t (concat string-before "$\\backslash$" string-after))))
  734. (defun org-export-latex-keywords (timestamps)
  735. "Convert special keywords to LaTeX.
  736. Regexps are those from `org-latex-special-string-regexps'."
  737. (let ((rg org-latex-special-string-regexps) r)
  738. (while (setq r (pop rg))
  739. (goto-char (point-min))
  740. (while (re-search-forward (eval r) nil t)
  741. (if (not timestamps)
  742. (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
  743. (replace-match ""))))))
  744. (defun org-export-latex-fixed-width (opt)
  745. "When OPT is non-nil convert fixed-width sections to LaTeX."
  746. (goto-char (point-min))
  747. (while (re-search-forward "^[ \t]*:" nil t)
  748. (if opt
  749. (progn (goto-char (match-beginning 0))
  750. (insert "\\begin{verbatim}\n")
  751. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  752. (replace-match (concat (match-string 1)
  753. (match-string 2)) t t)
  754. (forward-line))
  755. (insert "\\end{verbatim}\n\n"))
  756. (progn (goto-char (match-beginning 0))
  757. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  758. (replace-match (concat "%" (match-string 1)
  759. (match-string 2)) t t)
  760. (forward-line))))))
  761. ;; FIXME Use org-export-highlight-first-table-line ?
  762. (defun org-export-latex-lists ()
  763. "Convert lists to LaTeX."
  764. (goto-char (point-min))
  765. (while (re-search-forward org-export-latex-list-beginning-re nil t)
  766. (beginning-of-line)
  767. (org-export-list-to-latex
  768. (org-export-latex-parse-list t))))
  769. (defun org-export-list-to-generic (list params)
  770. "Convert a LIST parsed through `org-export-latex-parse-list' to other formats.
  771. Valid parameters are
  772. :ustart String to start an unordered list
  773. :uend String to end an unordered list
  774. :ostart String to start an ordered list
  775. :oend String to end an ordered list
  776. :splice When set to t, return only list body lines, don't wrap
  777. them into :[u/o]start and :[u/o]end. Default is nil.
  778. :istart String to start a list item
  779. :iend String to end a list item
  780. :isep String to separate items
  781. :lsep String to separate sublists"
  782. (interactive)
  783. (let* ((p params) sublist
  784. (splicep (plist-get p :splice))
  785. (ostart (plist-get p :ostart))
  786. (oend (plist-get p :oend))
  787. (ustart (plist-get p :ustart))
  788. (uend (plist-get p :uend))
  789. (istart (plist-get p :istart))
  790. (iend (plist-get p :iend))
  791. (isep (plist-get p :isep))
  792. (lsep (plist-get p :lsep)))
  793. (let ((wrapper
  794. (cond ((eq (car list) 'ordered)
  795. (concat ostart "\n%s" oend "\n"))
  796. ((eq (car list) 'unordered)
  797. (concat ustart "\n%s" uend "\n"))))
  798. rtn)
  799. (while (setq sublist (pop list))
  800. (cond ((symbolp sublist) nil)
  801. ((stringp sublist)
  802. (setq rtn (concat rtn istart sublist iend isep)))
  803. (t
  804. (setq rtn (concat rtn ;; previous list
  805. lsep ;; list separator
  806. (org-export-list-to-generic sublist p)
  807. lsep ;; list separator
  808. )))))
  809. (format wrapper rtn))))
  810. (defun org-export-list-to-latex (list)
  811. "Convert LIST into a LaTeX list."
  812. (insert
  813. (org-export-list-to-generic
  814. list '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  815. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  816. :istart "\\item " :iend ""
  817. :isep "\n" :lsep "\n"))
  818. ;; Add a trailing \n after list conversion
  819. "\n"))
  820. (defun org-export-latex-tables (opt)
  821. "When OPT is non-nil convert tables to LaTeX."
  822. (goto-char (point-min))
  823. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  824. ;; Re-align the table to update org-table-last-alignment
  825. (save-excursion (save-match-data (org-table-align)))
  826. (let (tbl-list
  827. (beg (match-beginning 0))
  828. (end (save-excursion
  829. (re-search-forward
  830. (concat "^" (regexp-quote (match-string 1))
  831. "[^|]\\|\\'") nil t) (match-beginning 0))))
  832. (beginning-of-line)
  833. (if org-export-latex-tables-verbatim
  834. (let* ((raw-table (buffer-substring beg end))
  835. (tbl (concat "\\begin{verbatim}\n" raw-table
  836. "\\end{verbatim}\n")))
  837. (apply 'delete-region (list beg end))
  838. (insert tbl))
  839. (progn
  840. (while (not (eq end (point)))
  841. (if (looking-at "[ \t]*|\\([^-|].+\\)|[ \t]*$")
  842. (push (split-string (org-trim (match-string 1)) "|") tbl-list)
  843. (push 'hline tbl-list))
  844. (forward-line))
  845. ;; comment region out instead of deleting it ?
  846. (apply 'delete-region (list beg end))
  847. (when opt (insert (orgtbl-to-latex (nreverse tbl-list)
  848. nil) "\n\n")))))))
  849. (defun org-export-latex-fontify ()
  850. "Convert fontification to LaTeX."
  851. (goto-char (point-min))
  852. (while (re-search-forward org-emph-re nil t)
  853. ;; The match goes one char after the *string*
  854. (let ((emph (assoc (match-string 3)
  855. org-export-latex-emphasis-alist))
  856. rpl)
  857. (unless (get-text-property (1- (point)) 'org-protected)
  858. (setq rpl (concat (match-string 1)
  859. (format (org-export-latex-protect-char-in-string
  860. '("\\" "{" "}") (cadr emph))
  861. (match-string 4))
  862. (match-string 5)))
  863. (if (caddr emph) (setq rpl (org-latex-protect rpl)))
  864. (replace-match rpl t t)))
  865. (backward-char)))
  866. (defun org-export-latex-links ()
  867. ;; Make sure to use the LaTeX hyperref and graphicx package
  868. ;; or send some warnings.
  869. "Convert links to LaTeX."
  870. (goto-char (point-min))
  871. (while (re-search-forward org-bracket-link-analytic-regexp nil t)
  872. (org-if-unprotected
  873. (goto-char (match-beginning 0))
  874. (let* ((re-radio org-latex-all-targets-regexp)
  875. (remove (list (match-beginning 0) (match-end 0)))
  876. (type (match-string 2))
  877. (raw-path (match-string 3))
  878. (full-raw-path (concat (match-string 1) raw-path))
  879. (desc (match-string 5))
  880. imgp radiop
  881. ;; define the path of the link
  882. (path (cond
  883. ((member type '("http" "https" "ftp"))
  884. (concat type ":" raw-path))
  885. ((and re-radio (string-match re-radio raw-path))
  886. (setq radiop t))
  887. ((equal type "mailto")
  888. (concat type ":" raw-path))
  889. ((equal type "file")
  890. (if (and (or (org-file-image-p (expand-file-name raw-path))
  891. (string-match "\\.eps$" raw-path))
  892. (equal desc full-raw-path))
  893. (setq imgp t)
  894. (progn (when (string-match "\\(.+\\)::.+" raw-path)
  895. (setq raw-path (match-string 1 raw-path)))
  896. (if (file-exists-p raw-path)
  897. (concat type "://" (expand-file-name raw-path))
  898. (concat type "://" (org-export-directory
  899. :LaTeX org-latex-options-plist)
  900. raw-path))))))))
  901. ;; process with link inserting
  902. (apply 'delete-region remove)
  903. (cond ((and imgp (plist-get org-latex-options-plist :inline-images))
  904. (insert (format "\\includegraphics[%s]{%s}"
  905. ;; image option should be set be a comment line
  906. org-export-latex-image-default-option
  907. (expand-file-name raw-path))))
  908. ;; FIXME: what about caption? image properties?
  909. (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc)))
  910. (path (insert (format "\\href{%s}{%s}" path desc)))
  911. (t (insert "\\texttt{" desc "}")))))))
  912. (defun org-export-latex-cleaned-string
  913. ;; FIXME remove commentsp call in org.el and here
  914. (&optional commentsp)
  915. "Clean stuff in the LaTeX export."
  916. ;; Preserve line breaks
  917. (goto-char (point-min))
  918. (while (re-search-forward "\\\\\\\\" nil t)
  919. (add-text-properties (match-beginning 0) (match-end 0)
  920. '(org-protected t)))
  921. ;; Convert LaTeX to \LaTeX{}
  922. (goto-char (point-min))
  923. (let ((case-fold-search nil) rpl)
  924. (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
  925. (replace-match (org-latex-protect
  926. (concat (match-string 1) "\\LaTeX{}")) t t)))
  927. ;; Convert horizontal rules
  928. (goto-char (point-min))
  929. (while (re-search-forward "^----+.$" nil t)
  930. (replace-match (org-latex-protect "\\hrule") t t))
  931. ;; Protect LaTeX \commands{...}
  932. (goto-char (point-min))
  933. (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
  934. (add-text-properties (match-beginning 0) (match-end 0)
  935. '(org-protected t)))
  936. ;; Replace radio links
  937. (goto-char (point-min))
  938. (while (re-search-forward
  939. (concat "<<<?" org-latex-all-targets-regexp
  940. ">>>?\\((INVISIBLE)\\)?") nil t)
  941. (replace-match
  942. (org-latex-protect
  943. (format "\\label{%s}%s"(match-string 1)
  944. (if (match-string 2) "" (match-string 1)))) t t))
  945. ;; Delete @<...> constructs
  946. (goto-char (point-min))
  947. ;; Thanks to Daniel Clemente for this regexp
  948. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  949. (replace-match ""))
  950. ;; When converting to LaTeX, replace footnotes
  951. ;; FIXME: don't protect footnotes from conversion
  952. (when (plist-get org-latex-options-plist :footnotes)
  953. (goto-char (point-min))
  954. (while (re-search-forward "\\[[0-9]+\\]" nil t)
  955. (when (save-match-data
  956. (save-excursion (beginning-of-line)
  957. (looking-at "[^:|#]")))
  958. (let ((foot-beg (match-beginning 0))
  959. (foot-end (match-end 0))
  960. (foot-prefix (match-string 0))
  961. footnote footnote-rpl)
  962. (when (and (re-search-forward (regexp-quote foot-prefix) nil t))
  963. (replace-match "")
  964. (let ((end (save-excursion
  965. (if (re-search-forward "^$\\|\\[[0-9]+\\]" nil t)
  966. (match-beginning 0) (point-max)))))
  967. (setq footnote
  968. (concat
  969. (org-trim (buffer-substring (point) end))
  970. ;; FIXME stupid workaround for cases where
  971. ;; `org-bracket-link-analytic-regexp' matches
  972. ;; }. as part of the link.
  973. " "))
  974. (delete-region (point) end)))
  975. (goto-char foot-beg)
  976. (delete-region foot-beg foot-end)
  977. (setq footnote-rpl (format "\\footnote{%s}" footnote))
  978. (add-text-properties 0 10 '(org-protected t) footnote-rpl)
  979. (add-text-properties (1- (length footnote-rpl))
  980. (length footnote-rpl)
  981. '(org-protected t) footnote-rpl)
  982. (insert footnote-rpl))))
  983. ;; Replace footnote section tag for LaTeX
  984. (goto-char (point-min))
  985. (while (re-search-forward
  986. (concat "^" footnote-section-tag-regexp) nil t)
  987. (replace-match ""))))
  988. (provide 'org-export-latex)
  989. ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
  990. ;;; org-export-latex.el ends here