org-export-latex.el 52 KB

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