org-export-latex.el 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. ;;; org-export-latex.el --- LaTeX exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-export-latex.el
  7. ;; Version: 6.09a
  8. ;; Author: Bastien Guerry <bzg AT altern DOT org>
  9. ;; Maintainer: Bastien Guerry <bzg AT altern DOT org>
  10. ;; Keywords: org, wp, tex
  11. ;; Description: Converts an org-mode buffer into LaTeX
  12. ;; URL: http://www.cognition.ens.fr/~guerry/u/org-export-latex.el
  13. ;; This file is part of GNU Emacs.
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;; Commentary:
  25. ;;
  26. ;; This library implements a LaTeX exporter for org-mode.
  27. ;;
  28. ;; Put this file into your load-path and the following into your ~/.emacs:
  29. ;; (require 'org-export-latex)
  30. ;;
  31. ;; The interactive functions are similar to those of the HTML exporter:
  32. ;;
  33. ;; M-x `org-export-as-latex'
  34. ;; M-x `org-export-as-latex-batch'
  35. ;; M-x `org-export-as-latex-to-buffer'
  36. ;; M-x `org-export-region-as-latex'
  37. ;; M-x `org-replace-region-by-latex'
  38. ;;
  39. ;;; Code:
  40. (eval-when-compile
  41. (require 'cl))
  42. (require 'footnote)
  43. (require 'org)
  44. (require 'org-exp)
  45. ;;; Variables:
  46. (defvar org-export-latex-class nil)
  47. (defvar org-export-latex-header nil)
  48. (defvar org-export-latex-append-header nil)
  49. (defvar org-export-latex-options-plist nil)
  50. (defvar org-export-latex-todo-keywords-1 nil)
  51. (defvar org-export-latex-all-targets-re nil)
  52. (defvar org-export-latex-add-level 0)
  53. (defvar org-export-latex-sectioning "")
  54. (defvar org-export-latex-sectioning-depth 0)
  55. (defvar org-export-latex-special-string-regexps
  56. '(org-ts-regexp
  57. org-scheduled-string
  58. org-deadline-string
  59. org-clock-string)
  60. "A list of regexps to convert as special keywords.")
  61. (defvar latexp) ; dynamically scoped from org.el
  62. (defvar re-quote) ; dynamically scoped from org.el
  63. (defvar commentsp) ; dynamically scoped from org.el
  64. ;;; User variables:
  65. (defgroup org-export-latex nil
  66. "Options for exporting Org-mode files to LaTeX."
  67. :tag "Org Export LaTeX"
  68. :group 'org-export)
  69. (defcustom org-export-latex-default-class "article"
  70. "The default LaTeX class."
  71. :group 'org-export-latex
  72. :type '(string :tag "LaTeX class"))
  73. (defcustom org-export-latex-classes
  74. '(("article"
  75. "\\documentclass[11pt,a4paper]{article}
  76. \\usepackage[utf8]{inputenc}
  77. \\usepackage[T1]{fontenc}
  78. \\usepackage{hyperref}"
  79. ("\\section{%s}" . "\\section*{%s}")
  80. ("\\subsection{%s}" . "\\subsection*{%s}")
  81. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  82. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  83. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  84. ("report"
  85. "\\documentclass[11pt,a4paper]{report}
  86. \\usepackage[utf8]{inputenc}
  87. \\usepackage[T1]{fontenc}
  88. \\usepackage{hyperref}"
  89. ("\\part{%s}" . "\\part*{%s}")
  90. ("\\chapter{%s}" . "\\chapter*{%s}")
  91. ("\\section{%s}" . "\\section*{%s}")
  92. ("\\subsection{%s}" . "\\subsection*{%s}")
  93. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  94. ("book"
  95. "\\documentclass[11pt,a4paper]{book}
  96. \\usepackage[utf8]{inputenc}
  97. \\usepackage[T1]{fontenc}
  98. \\usepackage{hyperref}"
  99. ("\\part{%s}" . "\\part*{%s}")
  100. ("\\chapter{%s}" . "\\chapter*{%s}")
  101. ("\\section{%s}" . "\\section*{%s}")
  102. ("\\subsection{%s}" . "\\subsection*{%s}")
  103. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  104. "Alist of LaTeX classes and associated header and structure.
  105. If #+LaTeX_CLASS is set in the buffer, use its value and the
  106. associated information. Here is the structure of each cell:
  107. \(class-name
  108. header-string
  109. (numbered-section . unnumbered-section\)
  110. ...\)
  111. A %s formatter is mandatory in each section string and will be
  112. replaced by the title of the section.
  113. Instead of a cons cell (numbered . unnumbered), you can also provide a list
  114. of 2-4 elements,
  115. (numbered-open numbered-close)
  116. or
  117. (numbered-open numbered-close unnumbered-open unnumbered-close)
  118. providing opening and closing strings for an environment that should
  119. represent the document section. The opening clause should have a %s
  120. to represent the section title."
  121. :group 'org-export-latex
  122. :type '(repeat
  123. (list (string :tag "LaTeX class")
  124. (string :tag "LaTeX header")
  125. (repeat :tag "Levels" :inline t
  126. (choice
  127. (cons :tag "Heading"
  128. (string :tag "numbered")
  129. (string :tag "unnumbered)"))
  130. (list :tag "Environment"
  131. (string :tag "Opening (numbered) ")
  132. (string :tag "Closing (numbered) ")
  133. (string :tag "Opening (unnumbered)")
  134. (string :tag "Closing (unnumbered)")))))))
  135. (defcustom org-export-latex-emphasis-alist
  136. '(("*" "\\textbf{%s}" nil)
  137. ("/" "\\emph{%s}" nil)
  138. ("_" "\\underline{%s}" nil)
  139. ("+" "\\texttt{%s}" nil)
  140. ("=" "\\texttt{%s}" nil)
  141. ("~" "\\texttt{%s}" t))
  142. "Alist of LaTeX expressions to convert emphasis fontifiers.
  143. Each element of the list is a list of three elements.
  144. The first element is the character used as a marker for fontification.
  145. The second element is a formatting string to wrap fontified text with.
  146. The third element decides whether to protect converted text from other
  147. conversions."
  148. :group 'org-export-latex
  149. :type 'alist)
  150. (defcustom org-export-latex-title-command "\\maketitle"
  151. "The command used to insert the title just after \\begin{document}.
  152. If this string contains the formatting specification \"%s\" then
  153. it will be used as a formatting string, passing the title as an
  154. argument."
  155. :group 'org-export-latex
  156. :type 'string)
  157. (defcustom org-export-latex-date-format
  158. "%d %B %Y"
  159. "Format string for \\date{...}."
  160. :group 'org-export-latex
  161. :type 'string)
  162. (defcustom org-export-latex-tables-verbatim nil
  163. "When non-nil, tables are exported verbatim."
  164. :group 'org-export-latex
  165. :type 'boolean)
  166. (defcustom org-export-latex-tables-column-borders nil
  167. "When non-nil, group of columns are surrounded with borders."
  168. :group 'org-export-latex
  169. :type 'boolean)
  170. (defcustom org-export-latex-packages-alist nil
  171. "Alist of packages to be inserted in the header.
  172. Each cell is of the forma \( \"option\" . \"package\" \)."
  173. :group 'org-export-latex
  174. :type 'alist)
  175. (defcustom org-export-latex-low-levels 'description
  176. "How to convert sections below the current level of sectioning.
  177. This is specified by the `org-export-headline-levels' option or the
  178. value of \"H:\" in Org's #+OPTION line.
  179. This can be either nil (skip the sections), 'description (convert
  180. the sections as descriptive lists) or a string to be used instead
  181. of \\section{%s}. In this latter case, the %s stands here for the
  182. inserted headline and is mandatory."
  183. :group 'org-export-latex
  184. :type '(choice (const :tag "Ignore" nil)
  185. (symbol :tag "Convert as descriptive list" description)
  186. (string :tag "Use a section string" :value "\\subparagraph{%s}")))
  187. (defcustom org-export-latex-remove-from-headlines
  188. '(:todo t :priority t :tags t)
  189. "A plist of keywords to remove from headlines.
  190. Non-nil means remove this keyword type from the headline.
  191. Don't remove the keys, just change their values."
  192. :type 'plist
  193. :group 'org-export-latex)
  194. (defcustom org-export-latex-image-default-option "width=10em"
  195. "Default option for images."
  196. :group 'org-export-latex
  197. :type 'string)
  198. (defcustom org-export-latex-coding-system nil
  199. "Coding system for the exported LaTex file."
  200. :group 'org-export-latex
  201. :type 'coding-system)
  202. ;;; Autoload functions:
  203. ;;;###autoload
  204. (defun org-export-as-latex-batch ()
  205. "Call `org-export-as-latex', may be used in batch processing.
  206. For example:
  207. emacs --batch
  208. --load=$HOME/lib/emacs/org.el
  209. --eval \"(setq org-export-headline-levels 2)\"
  210. --visit=MyFile --funcall org-export-as-latex-batch"
  211. (org-export-as-latex org-export-headline-levels 'hidden))
  212. ;;;###autoload
  213. (defun org-export-as-latex-to-buffer (arg)
  214. "Call `org-exort-as-latex` with output to a temporary buffer.
  215. No file is created. The prefix ARG is passed through to `org-export-as-latex'."
  216. (interactive "P")
  217. (org-export-as-latex arg nil nil "*Org LaTeX Export*")
  218. (switch-to-buffer-other-window "*Org LaTeX Export*"))
  219. ;;;###autoload
  220. (defun org-replace-region-by-latex (beg end)
  221. "Replace the region from BEG to END with its LaTeX export.
  222. It assumes the region has `org-mode' syntax, and then convert it to
  223. LaTeX. This can be used in any buffer. For example, you could
  224. write an itemized list in `org-mode' syntax in an LaTeX buffer and
  225. then use this command to convert it."
  226. (interactive "r")
  227. (let (reg latex buf)
  228. (save-window-excursion
  229. (if (org-mode-p)
  230. (setq latex (org-export-region-as-latex
  231. beg end t 'string))
  232. (setq reg (buffer-substring beg end)
  233. buf (get-buffer-create "*Org tmp*"))
  234. (save-excursion
  235. (set-buffer buf)
  236. (erase-buffer)
  237. (insert reg)
  238. (org-mode)
  239. (setq latex (org-export-region-as-latex
  240. (point-min) (point-max) t 'string)))
  241. (kill-buffer buf)))
  242. (delete-region beg end)
  243. (insert latex)))
  244. ;;;###autoload
  245. (defun org-export-region-as-latex (beg end &optional body-only buffer)
  246. "Convert region from BEG to END in `org-mode' buffer to LaTeX.
  247. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  248. contents, and only produce the region of converted text, useful for
  249. cut-and-paste operations.
  250. If BUFFER is a buffer or a string, use/create that buffer as a target
  251. of the converted LaTeX. If BUFFER is the symbol `string', return the
  252. produced LaTeX as a string and leave not buffer behind. For example,
  253. a Lisp program could call this function in the following way:
  254. (setq latex (org-export-region-as-latex beg end t 'string))
  255. When called interactively, the output buffer is selected, and shown
  256. in a window. A non-interactive call will only retunr the buffer."
  257. (interactive "r\nP")
  258. (when (interactive-p)
  259. (setq buffer "*Org LaTeX Export*"))
  260. (let ((transient-mark-mode t) (zmacs-regions t)
  261. rtn)
  262. (goto-char end)
  263. (set-mark (point)) ;; to activate the region
  264. (goto-char beg)
  265. (setq rtn (org-export-as-latex
  266. nil nil nil
  267. buffer body-only))
  268. (if (fboundp 'deactivate-mark) (deactivate-mark))
  269. (if (and (interactive-p) (bufferp rtn))
  270. (switch-to-buffer-other-window rtn)
  271. rtn)))
  272. ;;;###autoload
  273. (defun org-export-as-latex (arg &optional hidden ext-plist
  274. to-buffer body-only pub-dir)
  275. "Export current buffer to a LaTeX file.
  276. If there is an active region, export only the region. The prefix
  277. ARG specifies how many levels of the outline should become
  278. headlines. The default is 3. Lower levels will be exported
  279. depending on `org-export-latex-low-levels'. The default is to
  280. convert them as description lists. When HIDDEN is non-nil, don't
  281. display the LaTeX buffer. EXT-PLIST is a property list with
  282. external parameters overriding org-mode's default settings, but
  283. still inferior to file-local settings. When TO-BUFFER is
  284. non-nil, create a buffer with that name and export to that
  285. buffer. If TO-BUFFER is the symbol `string', don't leave any
  286. buffer behind but just return the resulting LaTeX as a string.
  287. When BODY-ONLY is set, don't produce the file header and footer,
  288. simply return the content of \begin{document}...\end{document},
  289. without even the \begin{document} and \end{document} commands.
  290. when PUB-DIR is set, use this as the publishing directory."
  291. (interactive "P")
  292. ;; Make sure we have a file name when we need it.
  293. (when (and (not (or to-buffer body-only))
  294. (not buffer-file-name))
  295. (if (buffer-base-buffer)
  296. (org-set-local 'buffer-file-name
  297. (with-current-buffer (buffer-base-buffer)
  298. buffer-file-name))
  299. (error "Need a file name to be able to export")))
  300. (message "Exporting to LaTeX...")
  301. (org-update-radio-target-regexp)
  302. (org-export-latex-set-initial-vars ext-plist arg)
  303. (let* ((wcf (current-window-configuration))
  304. (opt-plist org-export-latex-options-plist)
  305. (region-p (org-region-active-p))
  306. (rbeg (and region-p (region-beginning)))
  307. (rend (and region-p (region-end)))
  308. (subtree-p
  309. (when region-p
  310. (save-excursion
  311. (goto-char rbeg)
  312. (and (org-at-heading-p)
  313. (>= (org-end-of-subtree t t) rend)))))
  314. (opt-plist (if subtree-p
  315. (org-export-add-subtree-options opt-plist rbeg)
  316. opt-plist))
  317. ;; Make sure the variable contains the updated values.
  318. (org-export-latex-options-plist opt-plist)
  319. (title (or (and subtree-p (org-export-get-title-from-subtree))
  320. (plist-get opt-plist :title)
  321. (and (not
  322. (plist-get opt-plist :skip-before-1st-heading))
  323. (org-export-grab-title-from-buffer))
  324. (file-name-sans-extension
  325. (file-name-nondirectory buffer-file-name))))
  326. (filename (concat (file-name-as-directory
  327. (or pub-dir
  328. (org-export-directory :LaTeX ext-plist)))
  329. (file-name-sans-extension
  330. (or (and subtree-p
  331. (org-entry-get rbeg "EXPORT_FILE_NAME" t))
  332. (file-name-nondirectory ;sans-extension
  333. buffer-file-name)))
  334. ".tex"))
  335. (filename (if (equal (file-truename filename)
  336. (file-truename buffer-file-name))
  337. (concat filename ".tex")
  338. filename))
  339. (buffer (if to-buffer
  340. (cond
  341. ((eq to-buffer 'string) (get-buffer-create
  342. "*Org LaTeX Export*"))
  343. (t (get-buffer-create to-buffer)))
  344. (find-file-noselect filename)))
  345. (odd org-odd-levels-only)
  346. (header (org-export-latex-make-header title opt-plist))
  347. (skip (cond (subtree-p nil)
  348. (region-p t)
  349. ;; never skip first lines when exporting a subtree
  350. (t (plist-get opt-plist :skip-before-1st-heading))))
  351. (text (plist-get opt-plist :text))
  352. (first-lines (if skip "" (org-export-latex-first-lines)))
  353. (coding-system (and (boundp 'buffer-file-coding-system)
  354. buffer-file-coding-system))
  355. (coding-system-for-write (or org-export-latex-coding-system
  356. coding-system))
  357. (save-buffer-coding-system (or org-export-latex-coding-system
  358. coding-system))
  359. (region (buffer-substring
  360. (if region-p (region-beginning) (point-min))
  361. (if region-p (region-end) (point-max))))
  362. (string-for-export
  363. (org-export-preprocess-string
  364. region :emph-multiline t
  365. :for-LaTeX t
  366. :comments nil
  367. :add-text (if (eq to-buffer 'string) nil text)
  368. :skip-before-1st-heading skip
  369. :select-tags (plist-get opt-plist :select-tags)
  370. :exclude-tags (plist-get opt-plist :exclude-tags)
  371. :LaTeX-fragments nil)))
  372. (set-buffer buffer)
  373. (erase-buffer)
  374. (and (fboundp 'set-buffer-file-coding-system)
  375. (set-buffer-file-coding-system coding-system-for-write))
  376. ;; insert the header and initial document commands
  377. (unless (or (eq to-buffer 'string) body-only)
  378. (insert header))
  379. ;; insert text found in #+TEXT
  380. (when (and text (not (eq to-buffer 'string)))
  381. (insert (org-export-latex-content
  382. text '(lists tables fixed-width keywords))
  383. "\n\n"))
  384. ;; insert lines before the first headline
  385. (unless (or skip (eq to-buffer 'string))
  386. (insert first-lines))
  387. ;; handle the case where the region does not begin with a section
  388. (when region-p
  389. (insert (with-temp-buffer
  390. (insert string-for-export)
  391. (org-export-latex-first-lines))))
  392. ;; export the content of headlines
  393. (org-export-latex-global
  394. (with-temp-buffer
  395. (insert string-for-export)
  396. (goto-char (point-min))
  397. (when (re-search-forward "^\\(\\*+\\) " nil t)
  398. (let* ((asters (length (match-string 1)))
  399. (level (if odd (- asters 2) (- asters 1))))
  400. (setq org-export-latex-add-level
  401. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  402. (org-export-latex-parse-global level odd)))))
  403. ;; finalization
  404. (unless body-only (insert "\n\\end{document}"))
  405. (or to-buffer (save-buffer))
  406. (goto-char (point-min))
  407. (message "Exporting to LaTeX...done")
  408. (prog1
  409. (if (eq to-buffer 'string)
  410. (prog1 (buffer-substring (point-min) (point-max))
  411. (kill-buffer (current-buffer)))
  412. (current-buffer))
  413. (set-window-configuration wcf))))
  414. ;;; Parsing functions:
  415. (defun org-export-latex-parse-global (level odd)
  416. "Parse the current buffer recursively, starting at LEVEL.
  417. If ODD is non-nil, assume the buffer only contains odd sections.
  418. Return a list reflecting the document structure."
  419. (save-excursion
  420. (goto-char (point-min))
  421. (let* ((cnt 0) output
  422. (depth org-export-latex-sectioning-depth))
  423. (while (re-search-forward
  424. (concat "^\\(\\(?:\\*\\)\\{"
  425. (number-to-string (+ (if odd 2 1) level))
  426. "\\}\\) \\(.*\\)$")
  427. ;; make sure that there is no upper heading
  428. (when (> level 0)
  429. (save-excursion
  430. (save-match-data
  431. (re-search-forward
  432. (concat "^\\(\\(?:\\*\\)\\{"
  433. (number-to-string level)
  434. "\\}\\) \\(.*\\)$") nil t)))) t)
  435. (setq cnt (1+ cnt))
  436. (let* ((pos (match-beginning 0))
  437. (heading (match-string 2))
  438. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  439. (save-excursion
  440. (narrow-to-region
  441. (point)
  442. (save-match-data
  443. (if (re-search-forward
  444. (concat "^\\(\\(?:\\*\\)\\{"
  445. (number-to-string (+ (if odd 2 1) level))
  446. "\\}\\) \\(.*\\)$") nil t)
  447. (match-beginning 0)
  448. (point-max))))
  449. (goto-char (point-min))
  450. (setq output
  451. (append output
  452. (list
  453. (list
  454. `(pos . ,pos)
  455. `(level . ,nlevel)
  456. `(occur . ,cnt)
  457. `(heading . ,heading)
  458. `(content . ,(org-export-latex-parse-content))
  459. `(subcontent . ,(org-export-latex-parse-subcontent
  460. level odd)))))))
  461. (widen)))
  462. (list output))))
  463. (defun org-export-latex-parse-content ()
  464. "Extract the content of a section."
  465. (let ((beg (point))
  466. (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
  467. (progn (beginning-of-line) (point))
  468. (point-max))))
  469. (buffer-substring beg end)))
  470. (defun org-export-latex-parse-subcontent (level odd)
  471. "Extract the subcontent of a section at LEVEL.
  472. If ODD Is non-nil, assume subcontent only contains odd sections."
  473. (if (not (re-search-forward
  474. (concat "^\\(\\(?:\\*\\)\\{"
  475. (number-to-string (+ (if odd 4 2) level))
  476. "\\}\\) \\(.*\\)$")
  477. nil t))
  478. nil ; subcontent is nil
  479. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  480. ;;; Rendering functions:
  481. (defun org-export-latex-global (content)
  482. "Export CONTENT to LaTeX.
  483. CONTENT is an element of the list produced by
  484. `org-export-latex-parse-global'."
  485. (if (eq (car content) 'subcontent)
  486. (mapc 'org-export-latex-sub (cdr content))
  487. (org-export-latex-sub (car content))))
  488. (defun org-export-latex-sub (subcontent)
  489. "Export the list SUBCONTENT to LaTeX.
  490. SUBCONTENT is an alist containing information about the headline
  491. and its content."
  492. (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
  493. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  494. (defun org-export-latex-subcontent (subcontent num)
  495. "Export each cell of SUBCONTENT to LaTeX.
  496. If NUM, export sections as numerical sections."
  497. (let* ((heading (org-export-latex-fontify-headline
  498. (cdr (assoc 'heading subcontent))))
  499. (level (- (cdr (assoc 'level subcontent))
  500. org-export-latex-add-level))
  501. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  502. (content (cdr (assoc 'content subcontent)))
  503. (subcontent (cadr (assoc 'subcontent subcontent)))
  504. (label (org-get-text-property-any 0 'target heading)))
  505. (cond
  506. ;; Normal conversion
  507. ((<= level org-export-latex-sectioning-depth)
  508. (let* ((sec (nth (1- level) org-export-latex-sectioning))
  509. start end)
  510. (if (consp (cdr sec))
  511. (setq start (nth (if num 0 2) sec)
  512. end (nth (if num 1 3) sec))
  513. (setq start (if num (car sec) (cdr sec))))
  514. (insert (format start heading) "\n")
  515. (when label (insert (format "\\label{%s}\n" label)))
  516. (insert (org-export-latex-content content))
  517. (cond ((stringp subcontent) (insert subcontent))
  518. ((listp subcontent) (org-export-latex-sub subcontent)))
  519. (if end (insert end "\n"))))
  520. ;; At a level under the hl option: we can drop this subsection
  521. ((> level org-export-latex-sectioning-depth)
  522. (cond ((eq org-export-latex-low-levels 'description)
  523. (insert (format "\\begin{description}\n\n\\item[%s]%s\n\n"
  524. heading
  525. (if label (format "\\label{%s}" label) "")))
  526. (insert (org-export-latex-content content))
  527. (cond ((stringp subcontent) (insert subcontent))
  528. ((listp subcontent) (org-export-latex-sub subcontent)))
  529. (insert "\\end{description}\n"))
  530. ((stringp org-export-latex-low-levels)
  531. (insert (format org-export-latex-low-levels heading) "\n")
  532. (when label (insert (format "\\label{%s}\n" label)))
  533. (insert (org-export-latex-content content))
  534. (cond ((stringp subcontent) (insert subcontent))
  535. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  536. ;;; Exporting internals:
  537. (defun org-export-latex-set-initial-vars (ext-plist level)
  538. "Store org local variables required for LaTeX export.
  539. EXT-PLIST is an optional additional plist.
  540. LEVEL indicates the default depth for export."
  541. (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
  542. org-export-latex-all-targets-re
  543. (org-make-target-link-regexp (org-all-targets))
  544. org-export-latex-options-plist
  545. (org-combine-plists (org-default-export-plist) ext-plist
  546. (org-infile-export-plist))
  547. org-export-latex-class
  548. (save-excursion
  549. (goto-char (point-min))
  550. (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
  551. (assoc (match-string 1) org-export-latex-classes))
  552. (match-string 1)
  553. org-export-latex-default-class))
  554. org-export-latex-header
  555. (cadr (assoc org-export-latex-class org-export-latex-classes))
  556. org-export-latex-sectioning
  557. (cddr (assoc org-export-latex-class org-export-latex-classes))
  558. org-export-latex-sectioning-depth
  559. (or level
  560. (let ((hl-levels
  561. (plist-get org-export-latex-options-plist :headline-levels))
  562. (sec-depth (length org-export-latex-sectioning)))
  563. (if (> hl-levels sec-depth) sec-depth hl-levels)))))
  564. (defun org-export-latex-make-header (title opt-plist)
  565. "Make the LaTeX header and return it as a string.
  566. TITLE is the current title from the buffer or region.
  567. OPT-PLIST is the options plist for current buffer."
  568. (let ((toc (plist-get opt-plist :table-of-contents))
  569. (author (plist-get opt-plist :author)))
  570. (concat
  571. (if (plist-get opt-plist :time-stamp-file)
  572. (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
  573. ;; insert LaTeX custom header
  574. org-export-latex-header
  575. "\n"
  576. ;; insert information on LaTeX packages
  577. (when org-export-latex-packages-alist
  578. (mapconcat (lambda(p)
  579. (if (equal "" (car p))
  580. (format "\\usepackage{%s}" (cadr p))
  581. (format "\\usepackage[%s]{%s}"
  582. (car p) (cadr p))))
  583. org-export-latex-packages-alist "\n"))
  584. ;; insert additional commands in the header
  585. (plist-get opt-plist :latex-header-extra)
  586. org-export-latex-append-header
  587. ;; insert the title
  588. (format
  589. "\n\n\\title{%s}\n"
  590. ;; convert the title
  591. (org-export-latex-content
  592. title '(lists tables fixed-width keywords)))
  593. ;; insert author info
  594. (if (plist-get opt-plist :author-info)
  595. (format "\\author{%s}\n"
  596. (or author user-full-name))
  597. (format "%%\\author{%s}\n"
  598. (or author user-full-name)))
  599. ;; insert the date
  600. (format "\\date{%s}\n"
  601. (format-time-string
  602. (or (plist-get opt-plist :date)
  603. org-export-latex-date-format)))
  604. ;; beginning of the document
  605. "\n\\begin{document}\n\n"
  606. ;; insert the title command
  607. (if (string-match "%s" org-export-latex-title-command)
  608. (format org-export-latex-title-command title)
  609. org-export-latex-title-command)
  610. "\n\n"
  611. ;; table of contents
  612. (when (and org-export-with-toc
  613. (plist-get opt-plist :section-numbers))
  614. (cond ((numberp toc)
  615. (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
  616. (min toc (plist-get opt-plist :headline-levels))))
  617. (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
  618. (plist-get opt-plist :headline-levels))))))))
  619. (defun org-export-latex-first-lines (&optional comments)
  620. "Export the first lines before first headline.
  621. COMMENTS is either nil to replace them with the empty string or a
  622. formatting string like %%%%s if we want to comment them out."
  623. (save-excursion
  624. (goto-char (point-min))
  625. (if (org-at-heading-p) (beginning-of-line 2))
  626. (let* ((pt (point))
  627. (end (if (and (re-search-forward "^\\* " nil t)
  628. (not (eq pt (match-beginning 0))))
  629. (goto-char (match-beginning 0))
  630. (goto-char (point-max)))))
  631. (org-export-latex-content
  632. (org-export-preprocess-string
  633. (buffer-substring (point-min) end)
  634. :for-LaTeX t
  635. :emph-multiline t
  636. :add-text nil
  637. :comments nil
  638. :skip-before-1st-heading nil
  639. :LaTeX-fragments nil)))))
  640. (defun org-export-latex-content (content &optional exclude-list)
  641. "Convert CONTENT string to LaTeX.
  642. Don't perform conversions that are in EXCLUDE-LIST. Recognized
  643. conversion types are: quotation-marks, emphasis, sub-superscript,
  644. links, keywords, lists, tables, fixed-width"
  645. (with-temp-buffer
  646. (insert content)
  647. (unless (memq 'quotation-marks exclude-list)
  648. (org-export-latex-quotation-marks))
  649. (unless (memq 'emphasis exclude-list)
  650. (when (plist-get org-export-latex-options-plist :emphasize)
  651. (org-export-latex-fontify)))
  652. (unless (memq 'sub-superscript exclude-list)
  653. (org-export-latex-special-chars
  654. (plist-get org-export-latex-options-plist :sub-superscript)))
  655. (unless (memq 'links exclude-list)
  656. (org-export-latex-links))
  657. (unless (memq 'keywords exclude-list)
  658. (org-export-latex-keywords
  659. (plist-get org-export-latex-options-plist :timestamps)))
  660. (unless (memq 'lists exclude-list)
  661. (org-export-latex-lists))
  662. (unless (memq 'tables exclude-list)
  663. (org-export-latex-tables
  664. (plist-get org-export-latex-options-plist :tables)))
  665. (unless (memq 'fixed-width exclude-list)
  666. (org-export-latex-fixed-width
  667. (plist-get org-export-latex-options-plist :fixed-width)))
  668. ;; return string
  669. (buffer-substring (point-min) (point-max))))
  670. (defun org-export-latex-protect-string (s)
  671. "Add the org-protected property to string S."
  672. (add-text-properties 0 (length s) '(org-protected t) s) s)
  673. (defun org-export-latex-protect-char-in-string (char-list string)
  674. "Add org-protected text-property to char from CHAR-LIST in STRING."
  675. (with-temp-buffer
  676. (save-match-data
  677. (insert string)
  678. (goto-char (point-min))
  679. (while (re-search-forward (regexp-opt char-list) nil t)
  680. (add-text-properties (match-beginning 0)
  681. (match-end 0) '(org-protected t)))
  682. (buffer-string))))
  683. (defun org-export-latex-keywords-maybe (remove-list)
  684. "Maybe remove keywords depending on rules in REMOVE-LIST."
  685. (goto-char (point-min))
  686. (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
  687. (case-fold-search nil))
  688. ;; convert TODO keywords
  689. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  690. (if (plist-get remove-list :todo)
  691. (replace-match "")
  692. (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
  693. ;; convert priority string
  694. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  695. (if (plist-get remove-list :priority)
  696. (replace-match "")
  697. (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
  698. ;; convert tags
  699. (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
  700. (if (or (not org-export-with-tags)
  701. (plist-get remove-list :tags))
  702. (replace-match "")
  703. (replace-match
  704. (org-export-latex-protect-string
  705. (format "\\texttt{%s}"
  706. (save-match-data
  707. (replace-regexp-in-string
  708. "_" "\\\\_" (match-string 0)))))
  709. t t)))))
  710. (defun org-export-latex-fontify-headline (string)
  711. "Fontify special words in STRING."
  712. (with-temp-buffer
  713. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  714. ;; the beginning of the buffer - inserting "\n" is safe here though.
  715. (insert "\n" string)
  716. (goto-char (point-min))
  717. (when (plist-get org-export-latex-options-plist :emphasize)
  718. (org-export-latex-fontify))
  719. (org-export-latex-keywords-maybe
  720. org-export-latex-remove-from-headlines)
  721. (org-export-latex-special-chars
  722. (plist-get org-export-latex-options-plist :sub-superscript))
  723. (org-export-latex-links)
  724. ; (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
  725. (org-trim (buffer-string))))
  726. (defun org-export-latex-quotation-marks ()
  727. "Export quotation marks depending on language conventions."
  728. (let* ((lang (plist-get org-export-latex-options-plist :language))
  729. (quote-rpl (if (equal lang "fr")
  730. '(("\\(\\s-\\)\"" "«~")
  731. ("\\(\\S-\\)\"" "~»")
  732. ("\\(\\s-\\)'" "`"))
  733. '(("\\(\\s-\\)\"" "``")
  734. ("\\(\\S-\\)\"" "''")
  735. ("\\(\\s-\\)'" "`")))))
  736. (mapc (lambda(l) (goto-char (point-min))
  737. (while (re-search-forward (car l) nil t)
  738. (let ((rpl (concat (match-string 1) (cadr l))))
  739. (org-export-latex-protect-string rpl)
  740. (org-if-unprotected
  741. (replace-match rpl t t))))) quote-rpl)))
  742. (defun org-export-latex-special-chars (sub-superscript)
  743. "Export special characters to LaTeX.
  744. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  745. See the `org-export-latex.el' code for a complete conversion table."
  746. (goto-char (point-min))
  747. (mapc (lambda(c)
  748. (goto-char (point-min))
  749. (while (re-search-forward c nil t)
  750. ;; Put the point where to check for org-protected
  751. (unless (or (get-text-property (match-beginning 2) 'org-protected)
  752. (org-at-table-p))
  753. (cond ((member (match-string 2) '("\\$" "$"))
  754. (if (equal (match-string 2) "\\$")
  755. (replace-match (concat (match-string 1) "$"
  756. (match-string 3)) t t)
  757. (replace-match (concat (match-string 1) "\\$"
  758. (match-string 3)) t t)))
  759. ((member (match-string 2) '("&" "%" "#"))
  760. (if (equal (match-string 1) "\\")
  761. (replace-match (match-string 2) t t)
  762. (replace-match (concat (match-string 1) "\\"
  763. (match-string 2)) t t)))
  764. ((equal (match-string 2) "...")
  765. (replace-match
  766. (concat (match-string 1)
  767. (org-export-latex-protect-string "\\ldots{}")) t t))
  768. ((equal (match-string 2) "~")
  769. (cond ((equal (match-string 1) "\\") nil)
  770. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  771. (replace-match (concat (match-string 1) "\\~") t t))
  772. (t (replace-match
  773. (org-export-latex-protect-string
  774. (concat (match-string 1) "\\~{}")) t t))))
  775. ((member (match-string 2) '("{" "}"))
  776. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  777. (if (equal (match-string 1) "\\")
  778. (replace-match (match-string 2) t t)
  779. (replace-match (concat (match-string 1) "\\"
  780. (match-string 2)) t t)))))
  781. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  782. (cond ((equal (match-string 2) "\\")
  783. (replace-match (or (save-match-data
  784. (org-export-latex-treat-backslash-char
  785. (match-string 1)
  786. (match-string 3))) "") t t))
  787. ((member (match-string 2) '("_" "^"))
  788. (replace-match (or (save-match-data
  789. (org-export-latex-treat-sub-super-char
  790. sub-superscript
  791. (match-string 2)
  792. (match-string 1)
  793. (match-string 3))) "") t t)))))))
  794. '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  795. "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
  796. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
  797. "\\(.\\|^\\)\\(&\\)"
  798. "\\(.\\|^\\)\\(#\\)"
  799. "\\(.\\|^\\)\\(%\\)"
  800. "\\(.\\|^\\)\\({\\)"
  801. "\\(.\\|^\\)\\(}\\)"
  802. "\\(.\\|^\\)\\(~\\)"
  803. "\\(.\\|^\\)\\(\\.\\.\\.\\)"
  804. ;; (?\< . "\\textless{}")
  805. ;; (?\> . "\\textgreater{}")
  806. )))
  807. (defun org-export-latex-treat-sub-super-char
  808. (subsup char string-before string-after)
  809. "Convert the \"_\" and \"^\" characters to LaTeX.
  810. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  811. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  812. (cond ((equal string-before "\\")
  813. (concat string-before char string-after))
  814. ;; this is part of a math formula
  815. ((and (string-match "\\S-+" string-before)
  816. (string-match "\\S-+" string-after))
  817. (cond ((eq 'org-link (get-text-property 0 'face char))
  818. (concat string-before "\\" char string-after))
  819. ((save-match-data (org-inside-LaTeX-fragment-p))
  820. (if subsup
  821. (cond ((eq 1 (length string-after))
  822. (concat string-before char string-after))
  823. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  824. (format "%s%s{%s}" string-before char
  825. (match-string 1 string-after))))))
  826. ((and subsup
  827. (> (length string-after) 1)
  828. (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
  829. (format "$%s%s{%s}$" string-before char
  830. (match-string 1 string-after)))
  831. (subsup (concat "$" string-before char string-after "$"))
  832. (t (org-export-latex-protect-string
  833. (concat string-before "\\" char "{}" string-after)))))
  834. (t (org-export-latex-protect-string
  835. (concat string-before "\\" char "{}" string-after)))))
  836. (defun org-export-latex-treat-backslash-char (string-before string-after)
  837. "Convert the \"$\" special character to LaTeX.
  838. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  839. (cond ((member (list string-after) org-html-entities)
  840. ;; backslash is part of a special entity (like "\alpha")
  841. (concat string-before "$\\"
  842. (or (cdar (member (list string-after) org-html-entities))
  843. string-after) "$"))
  844. ((and (not (string-match "^[ \n\t]" string-after))
  845. (not (string-match "[ \t]\\'\\|^" string-before)))
  846. ;; backslash is inside a word
  847. (org-export-latex-protect-string
  848. (concat string-before "\\textbackslash{}" string-after)))
  849. ((not (or (equal string-after "")
  850. (string-match "^[ \t\n]" string-after)))
  851. ;; backslash might escape a character (like \#) or a user TeX
  852. ;; macro (like \setcounter)
  853. (org-export-latex-protect-string
  854. (concat string-before "\\" string-after)))
  855. ((and (string-match "^[ \t\n]" string-after)
  856. (string-match "[ \t\n]\\'" string-before))
  857. ;; backslash is alone, convert it to $\backslash$
  858. (org-export-latex-protect-string
  859. (concat string-before "\\textbackslash{}" string-after)))
  860. (t (org-export-latex-protect-string
  861. (concat string-before "\\textbackslash{}" string-after)))))
  862. (defun org-export-latex-keywords (timestamps)
  863. "Convert special keywords to LaTeX.
  864. Regexps are those from `org-export-latex-special-string-regexps'.
  865. If TIMESTAMPS, convert timestamps, otherwise delete them."
  866. (let ((rg org-export-latex-special-string-regexps) r)
  867. (while (setq r (pop rg))
  868. (goto-char (point-min))
  869. (while (re-search-forward (eval r) nil t)
  870. (if (not timestamps)
  871. (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
  872. (replace-match ""))))))
  873. (defun org-export-latex-fixed-width (opt)
  874. "When OPT is non-nil convert fixed-width sections to LaTeX."
  875. (goto-char (point-min))
  876. (while (re-search-forward "^[ \t]*:" nil t)
  877. (if opt
  878. (progn (goto-char (match-beginning 0))
  879. (insert "\\begin{verbatim}\n")
  880. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  881. (replace-match (concat (match-string 1)
  882. (match-string 2)) t t)
  883. (forward-line))
  884. (insert "\\end{verbatim}\n\n"))
  885. (progn (goto-char (match-beginning 0))
  886. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  887. (replace-match (concat "%" (match-string 1)
  888. (match-string 2)) t t)
  889. (forward-line))))))
  890. (defvar org-table-last-alignment) ; defined in org-table.el
  891. (declare-function orgtbl-to-latex "org-table" (table params) t)
  892. (defun org-export-latex-tables (insert)
  893. "Convert tables to LaTeX and INSERT it."
  894. (goto-char (point-min))
  895. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  896. ;; FIXME really need to save-excursion?
  897. (save-excursion (org-table-align))
  898. (let* ((beg (org-table-begin))
  899. (end (org-table-end))
  900. (raw-table (buffer-substring-no-properties beg end))
  901. fnum fields line lines olines gr colgropen line-fmt align)
  902. (if org-export-latex-tables-verbatim
  903. (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
  904. "\\end{verbatim}\n")))
  905. (apply 'delete-region (list beg end))
  906. (insert (org-export-latex-protect-string tbl)))
  907. (progn
  908. (setq lines (split-string raw-table "\n" t))
  909. (apply 'delete-region (list beg end))
  910. (when org-export-table-remove-special-lines
  911. (setq lines (org-table-clean-before-export lines)))
  912. ;; make a formatting string to reflect aligment
  913. (setq olines lines)
  914. (while (and (not line-fmt) (setq line (pop olines)))
  915. (unless (string-match "^[ \t]*|-" line)
  916. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  917. (setq fnum (make-vector (length fields) 0))
  918. (setq line-fmt
  919. (mapconcat
  920. (lambda (x)
  921. (setq gr (pop org-table-colgroup-info))
  922. (format "%s%%s%s"
  923. (cond ((eq gr ':start)
  924. (prog1 (if colgropen "|" "")
  925. (setq colgropen t)))
  926. ((eq gr ':startend)
  927. (prog1 (if colgropen "|" "|")
  928. (setq colgropen nil)))
  929. (t ""))
  930. (if (memq gr '(:end :startend))
  931. (progn (setq colgropen nil) "|")
  932. "")))
  933. fnum ""))))
  934. ;; fix double || in line-fmt
  935. (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
  936. ;; maybe remove the first and last "|"
  937. (when (and (not org-export-latex-tables-column-borders)
  938. (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
  939. (setq line-fmt (match-string 2 line-fmt)))
  940. ;; format alignment
  941. (setq align (apply 'format
  942. (cons line-fmt
  943. (mapcar (lambda (x) (if x "r" "l"))
  944. org-table-last-alignment))))
  945. ;; prepare the table to send to orgtbl-to-latex
  946. (setq lines
  947. (mapcar
  948. (lambda(elem)
  949. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  950. (split-string (org-trim elem) "|" t)))
  951. lines))
  952. (when insert
  953. (insert (org-export-latex-protect-string
  954. (orgtbl-to-latex
  955. lines `(:tstart ,(concat "\\begin{tabular}{" align "}"))))
  956. "\n\n")))))))
  957. (defun org-export-latex-fontify ()
  958. "Convert fontification to LaTeX."
  959. (goto-char (point-min))
  960. (while (re-search-forward org-emph-re nil t)
  961. ;; The match goes one char after the *string*
  962. (let ((emph (assoc (match-string 3)
  963. org-export-latex-emphasis-alist))
  964. rpl)
  965. (unless (get-text-property (1- (point)) 'org-protected)
  966. (setq rpl (concat (match-string 1)
  967. (format (org-export-latex-protect-char-in-string
  968. '("\\" "{" "}") (cadr emph))
  969. (match-string 4))
  970. (match-string 5)))
  971. (if (caddr emph)
  972. (setq rpl (org-export-latex-protect-string rpl)))
  973. (replace-match rpl t t)))
  974. (backward-char)))
  975. (defun org-export-latex-links ()
  976. ;; Make sure to use the LaTeX hyperref and graphicx package
  977. ;; or send some warnings.
  978. "Convert links to LaTeX."
  979. (goto-char (point-min))
  980. (while (re-search-forward org-bracket-link-analytic-regexp nil t)
  981. (org-if-unprotected
  982. (goto-char (match-beginning 0))
  983. (let* ((re-radio org-export-latex-all-targets-re)
  984. (remove (list (match-beginning 0) (match-end 0)))
  985. (type (match-string 2))
  986. (raw-path (org-extract-attributes (match-string 3)))
  987. (full-raw-path (concat (match-string 1) raw-path))
  988. (desc (match-string 5))
  989. imgp radiop
  990. ;; define the path of the link
  991. (path (cond
  992. ((member type '("http" "https" "ftp"))
  993. (concat type ":" raw-path))
  994. ((and re-radio (string-match re-radio raw-path))
  995. (setq radiop t))
  996. ((equal type "mailto")
  997. (concat type ":" raw-path))
  998. ((equal type "file")
  999. (if (and (or (org-file-image-p (expand-file-name raw-path))
  1000. (string-match "\\.eps$" raw-path))
  1001. (equal desc full-raw-path))
  1002. (setq imgp t)
  1003. (progn (when (string-match "\\(.+\\)::.+" raw-path)
  1004. (setq raw-path (match-string 1 raw-path)))
  1005. (if (file-exists-p raw-path)
  1006. (concat type "://" (expand-file-name raw-path))
  1007. (concat type "://" (org-export-directory
  1008. :LaTeX org-export-latex-options-plist)
  1009. raw-path))))))))
  1010. ;; process with link inserting
  1011. (apply 'delete-region remove)
  1012. (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
  1013. (insert (format "\\includegraphics[%s]{%s}"
  1014. ;; image option should be set be a comment line
  1015. org-export-latex-image-default-option
  1016. (expand-file-name raw-path))))
  1017. (radiop (insert (format "\\hyperref[%s]{%s}"
  1018. (org-solidify-link-text raw-path) desc)))
  1019. ((not type)
  1020. (insert (format "\\hyperref[%s]{%s}"
  1021. (org-solidify-link-text raw-path) desc)))
  1022. (path (insert (format "\\href{%s}{%s}" path desc)))
  1023. (t (insert "\\texttt{" desc "}")))))))
  1024. (defvar org-latex-entities) ; defined below
  1025. (defun org-export-latex-preprocess ()
  1026. "Clean stuff in the LaTeX export."
  1027. ;; Preserve line breaks
  1028. (goto-char (point-min))
  1029. (while (re-search-forward "\\\\\\\\" nil t)
  1030. (add-text-properties (match-beginning 0) (match-end 0)
  1031. '(org-protected t)))
  1032. ;; Preserve latex environments
  1033. (goto-char (point-min))
  1034. (while (re-search-forward "^[ \t]*\\begin{\\([a-zA-Z]+\\)}" nil t)
  1035. (let* ((start (progn (beginning-of-line) (point)))
  1036. (end (or (and (re-search-forward
  1037. (concat "^[ \t]*\\end{" (match-string 1) "}" nil t)
  1038. (point-at-eol)))
  1039. (point-max))))
  1040. (add-text-properties start end '(org-protected t))))
  1041. ;; Convert LaTeX to \LaTeX{}
  1042. (goto-char (point-min))
  1043. (let ((case-fold-search nil) rpl)
  1044. (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
  1045. (replace-match (org-export-latex-protect-string
  1046. (concat (match-string 1) "\\LaTeX{}")) t t)))
  1047. ;; Convert blockquotes
  1048. (goto-char (point-min))
  1049. (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
  1050. (replace-match "\\begin{quote}" t t))
  1051. (goto-char (point-min))
  1052. (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
  1053. (replace-match "\\end{quote}" t t))
  1054. ;; Convert verse
  1055. (goto-char (point-min))
  1056. (while (search-forward "ORG-VERSE-START" nil t)
  1057. (replace-match "\\begin{verse}" t t))
  1058. (goto-char (point-min))
  1059. (while (search-forward "ORG-VERSE-END" nil t)
  1060. (replace-match "\\end{verse}" t t))
  1061. ;; Convert horizontal rules
  1062. (goto-char (point-min))
  1063. (while (re-search-forward "^----+.$" nil t)
  1064. (replace-match (org-export-latex-protect-string "\\hrule") t t))
  1065. ;; Protect LaTeX commands like \commad[...]{...} or \command{...}
  1066. (goto-char (point-min))
  1067. (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
  1068. (add-text-properties (match-beginning 0) (match-end 0)
  1069. '(org-protected t)))
  1070. ;; Protect LaTeX entities
  1071. (goto-char (point-min))
  1072. (while (re-search-forward (regexp-opt org-latex-entities) nil t)
  1073. (add-text-properties (match-beginning 0) (match-end 0)
  1074. '(org-protected t)))
  1075. ;; Replace radio links
  1076. (goto-char (point-min))
  1077. (while (re-search-forward
  1078. (concat "<<<?" org-export-latex-all-targets-re
  1079. ">>>?\\((INVISIBLE)\\)?") nil t)
  1080. (replace-match
  1081. (org-export-latex-protect-string
  1082. (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
  1083. (match-string 1)))
  1084. (if (match-string 2) "" (match-string 1)))) t t))
  1085. ;; Delete @<...> constructs
  1086. ;; Thanks to Daniel Clemente for this regexp
  1087. (goto-char (point-min))
  1088. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  1089. (replace-match ""))
  1090. ;; When converting to LaTeX, replace footnotes
  1091. ;; FIXME: don't protect footnotes from conversion
  1092. (when (plist-get org-export-latex-options-plist :footnotes)
  1093. (goto-char (point-min))
  1094. (while (re-search-forward "\\[[0-9]+\\]" nil t)
  1095. (when (save-match-data
  1096. (save-excursion (beginning-of-line)
  1097. (looking-at "[^:|#]")))
  1098. (let ((foot-beg (match-beginning 0))
  1099. (foot-end (match-end 0))
  1100. (foot-prefix (match-string 0))
  1101. footnote footnote-rpl)
  1102. (save-excursion
  1103. (when (search-forward foot-prefix nil t)
  1104. (replace-match "")
  1105. (let ((end (save-excursion
  1106. (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
  1107. (match-beginning 0) (point-max)))))
  1108. (setq footnote (concat (org-trim (buffer-substring (point) end))
  1109. " ")) ; prevent last } being part of a link
  1110. (delete-region (point) end))
  1111. (goto-char foot-beg)
  1112. (delete-region foot-beg foot-end)
  1113. (unless (null footnote)
  1114. (setq footnote-rpl (format "\\footnote{%s}" footnote))
  1115. (add-text-properties 0 10 '(org-protected t) footnote-rpl)
  1116. (add-text-properties (1- (length footnote-rpl))
  1117. (length footnote-rpl)
  1118. '(org-protected t) footnote-rpl)
  1119. (insert footnote-rpl)))))))
  1120. ;; Replace footnote section tag for LaTeX
  1121. (goto-char (point-min))
  1122. (while (re-search-forward
  1123. (concat "^" footnote-section-tag-regexp) nil t)
  1124. (replace-match ""))))
  1125. ;;; List handling:
  1126. (defun org-export-latex-lists ()
  1127. "Replace plain text lists in current buffer into LaTeX lists."
  1128. "Convert lists to LaTeX."
  1129. (goto-char (point-min))
  1130. (while (re-search-forward org-list-beginning-re nil t)
  1131. (beginning-of-line)
  1132. (insert (org-list-to-latex (org-list-parse-list t)) "\n")))
  1133. (defconst org-latex-entities
  1134. '("\\!"
  1135. "\\'"
  1136. "\\+"
  1137. "\\,"
  1138. "\\-"
  1139. "\\:"
  1140. "\\;"
  1141. "\\<"
  1142. "\\="
  1143. "\\>"
  1144. "\\Huge"
  1145. "\\LARGE"
  1146. "\\Large"
  1147. "\\Styles"
  1148. "\\\\"
  1149. "\\`"
  1150. "\\addcontentsline"
  1151. "\\address"
  1152. "\\addtocontents"
  1153. "\\addtocounter"
  1154. "\\addtolength"
  1155. "\\addvspace"
  1156. "\\alph"
  1157. "\\appendix"
  1158. "\\arabic"
  1159. "\\author"
  1160. "\\begin{array}"
  1161. "\\begin{center}"
  1162. "\\begin{description}"
  1163. "\\begin{enumerate}"
  1164. "\\begin{eqnarray}"
  1165. "\\begin{equation}"
  1166. "\\begin{figure}"
  1167. "\\begin{flushleft}"
  1168. "\\begin{flushright}"
  1169. "\\begin{itemize}"
  1170. "\\begin{list}"
  1171. "\\begin{minipage}"
  1172. "\\begin{picture}"
  1173. "\\begin{quotation}"
  1174. "\\begin{quote}"
  1175. "\\begin{tabbing}"
  1176. "\\begin{table}"
  1177. "\\begin{tabular}"
  1178. "\\begin{thebibliography}"
  1179. "\\begin{theorem}"
  1180. "\\begin{titlepage}"
  1181. "\\begin{verbatim}"
  1182. "\\begin{verse}"
  1183. "\\bf"
  1184. "\\bf"
  1185. "\\bibitem"
  1186. "\\bigskip"
  1187. "\\cdots"
  1188. "\\centering"
  1189. "\\circle"
  1190. "\\cite"
  1191. "\\cleardoublepage"
  1192. "\\clearpage"
  1193. "\\cline"
  1194. "\\closing"
  1195. "\\dashbox"
  1196. "\\date"
  1197. "\\ddots"
  1198. "\\dotfill"
  1199. "\\em"
  1200. "\\fbox"
  1201. "\\flushbottom"
  1202. "\\fnsymbol"
  1203. "\\footnote"
  1204. "\\footnotemark"
  1205. "\\footnotesize"
  1206. "\\footnotetext"
  1207. "\\frac"
  1208. "\\frame"
  1209. "\\framebox"
  1210. "\\hfill"
  1211. "\\hline"
  1212. "\\hrulespace"
  1213. "\\hspace"
  1214. "\\huge"
  1215. "\\hyphenation"
  1216. "\\include"
  1217. "\\includeonly"
  1218. "\\indent"
  1219. "\\input"
  1220. "\\it"
  1221. "\\kill"
  1222. "\\label"
  1223. "\\large"
  1224. "\\ldots"
  1225. "\\line"
  1226. "\\linebreak"
  1227. "\\linethickness"
  1228. "\\listoffigures"
  1229. "\\listoftables"
  1230. "\\location"
  1231. "\\makebox"
  1232. "\\maketitle"
  1233. "\\mark"
  1234. "\\mbox"
  1235. "\\medskip"
  1236. "\\multicolumn"
  1237. "\\multiput"
  1238. "\\newcommand"
  1239. "\\newcounter"
  1240. "\\newenvironment"
  1241. "\\newfont"
  1242. "\\newlength"
  1243. "\\newline"
  1244. "\\newpage"
  1245. "\\newsavebox"
  1246. "\\newtheorem"
  1247. "\\nocite"
  1248. "\\nofiles"
  1249. "\\noindent"
  1250. "\\nolinebreak"
  1251. "\\nopagebreak"
  1252. "\\normalsize"
  1253. "\\onecolumn"
  1254. "\\opening"
  1255. "\\oval"
  1256. "\\overbrace"
  1257. "\\overline"
  1258. "\\pagebreak"
  1259. "\\pagenumbering"
  1260. "\\pageref"
  1261. "\\pagestyle"
  1262. "\\par"
  1263. "\\parbox"
  1264. "\\put"
  1265. "\\raggedbottom"
  1266. "\\raggedleft"
  1267. "\\raggedright"
  1268. "\\raisebox"
  1269. "\\ref"
  1270. "\\rm"
  1271. "\\roman"
  1272. "\\rule"
  1273. "\\savebox"
  1274. "\\sc"
  1275. "\\scriptsize"
  1276. "\\setcounter"
  1277. "\\setlength"
  1278. "\\settowidth"
  1279. "\\sf"
  1280. "\\shortstack"
  1281. "\\signature"
  1282. "\\sl"
  1283. "\\small"
  1284. "\\smallskip"
  1285. "\\sqrt"
  1286. "\\tableofcontents"
  1287. "\\telephone"
  1288. "\\thanks"
  1289. "\\thispagestyle"
  1290. "\\tiny"
  1291. "\\title"
  1292. "\\tt"
  1293. "\\twocolumn"
  1294. "\\typein"
  1295. "\\typeout"
  1296. "\\underbrace"
  1297. "\\underline"
  1298. "\\usebox"
  1299. "\\usecounter"
  1300. "\\value"
  1301. "\\vdots"
  1302. "\\vector"
  1303. "\\verb"
  1304. "\\vfill"
  1305. "\\vline"
  1306. "\\vspace")
  1307. "A list of LaTeX commands to be protected when performing conversion.")
  1308. (provide 'org-export-latex)
  1309. ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
  1310. ;;; org-export-latex.el ends here