org-export-latex.el 53 KB

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