org-export-latex.el 50 KB

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