org-export-latex.el 47 KB

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