org-export-latex.el 50 KB

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