org-export-latex.el 52 KB

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