org-latex.el 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  1. ;;; org-latex.el --- LaTeX exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-latex.el
  7. ;; Version: 6.28trans
  8. ;; Author: Bastien Guerry <bzg AT altern DOT org>
  9. ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
  10. ;; Keywords: org, wp, tex
  11. ;; Description: Converts an org-mode buffer into LaTeX
  12. ;; This file is part of GNU Emacs.
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;; Commentary:
  24. ;;
  25. ;; This library implements a LaTeX exporter for org-mode.
  26. ;;
  27. ;; It is part of Org and will be autoloaded
  28. ;;
  29. ;; The interactive functions are similar to those of the HTML exporter:
  30. ;;
  31. ;; M-x `org-export-as-latex'
  32. ;; M-x `org-export-as-pdf'
  33. ;; M-x `org-export-as-pdf-and-open'
  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-complex-heading-re nil)
  52. (defvar org-export-latex-not-done-keywords nil)
  53. (defvar org-export-latex-done-keywords nil)
  54. (defvar org-export-latex-display-custom-times nil)
  55. (defvar org-export-latex-all-targets-re nil)
  56. (defvar org-export-latex-add-level 0)
  57. (defvar org-export-latex-sectioning "")
  58. (defvar org-export-latex-sectioning-depth 0)
  59. (defvar org-export-latex-special-keyword-regexp
  60. (concat "\\<\\(" org-scheduled-string "\\|"
  61. org-deadline-string "\\|"
  62. org-closed-string"\\)")
  63. "Regexp matching special time planning keywords plus the time after it.")
  64. (defvar latexp) ; dynamically scoped from org.el
  65. (defvar re-quote) ; dynamically scoped from org.el
  66. (defvar commentsp) ; dynamically scoped from org.el
  67. ;;; User variables:
  68. (defgroup org-export-latex nil
  69. "Options for exporting Org-mode files to LaTeX."
  70. :tag "Org Export LaTeX"
  71. :group 'org-export)
  72. (defcustom org-export-latex-default-class "article"
  73. "The default LaTeX class."
  74. :group 'org-export-latex
  75. :type '(string :tag "LaTeX class"))
  76. (defcustom org-export-latex-classes
  77. '(("article"
  78. "\\documentclass[11pt]{article}
  79. \\usepackage[utf8]{inputenc}
  80. \\usepackage[T1]{fontenc}
  81. \\usepackage{graphicx}
  82. \\usepackage{longtable}
  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]{report}
  91. \\usepackage[utf8]{inputenc}
  92. \\usepackage[T1]{fontenc}
  93. \\usepackage{graphicx}
  94. \\usepackage{longtable}
  95. \\usepackage{hyperref}"
  96. ("\\part{%s}" . "\\part*{%s}")
  97. ("\\chapter{%s}" . "\\chapter*{%s}")
  98. ("\\section{%s}" . "\\section*{%s}")
  99. ("\\subsection{%s}" . "\\subsection*{%s}")
  100. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  101. ("book"
  102. "\\documentclass[11pt]{book}
  103. \\usepackage[utf8]{inputenc}
  104. \\usepackage[T1]{fontenc}
  105. \\usepackage{graphicx}
  106. \\usepackage{longtable}
  107. \\usepackage{hyperref}"
  108. ("\\part{%s}" . "\\part*{%s}")
  109. ("\\chapter{%s}" . "\\chapter*{%s}")
  110. ("\\section{%s}" . "\\section*{%s}")
  111. ("\\subsection{%s}" . "\\subsection*{%s}")
  112. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  113. "Alist of LaTeX classes and associated header and structure.
  114. If #+LaTeX_CLASS is set in the buffer, use its value and the
  115. associated information. Here is the structure of each cell:
  116. \(class-name
  117. header-string
  118. (numbered-section . unnumbered-section\)
  119. ...\)
  120. A %s formatter is mandatory in each section string and will be
  121. replaced by the title of the section.
  122. Instead of a cons cell (numbered . unnumbered), you can also provide a list
  123. of 2-4 elements,
  124. (numbered-open numbered-close)
  125. or
  126. (numbered-open numbered-close unnumbered-open unnumbered-close)
  127. providing opening and closing strings for an environment that should
  128. represent the document section. The opening clause should have a %s
  129. to represent the section title."
  130. :group 'org-export-latex
  131. :type '(repeat
  132. (list (string :tag "LaTeX class")
  133. (string :tag "LaTeX header")
  134. (repeat :tag "Levels" :inline t
  135. (choice
  136. (cons :tag "Heading"
  137. (string :tag "numbered")
  138. (string :tag "unnumbered)"))
  139. (list :tag "Environment"
  140. (string :tag "Opening (numbered) ")
  141. (string :tag "Closing (numbered) ")
  142. (string :tag "Opening (unnumbered)")
  143. (string :tag "Closing (unnumbered)")))))))
  144. (defcustom org-export-latex-emphasis-alist
  145. '(("*" "\\textbf{%s}" nil)
  146. ("/" "\\emph{%s}" nil)
  147. ("_" "\\underline{%s}" nil)
  148. ("+" "\\texttt{%s}" nil)
  149. ("=" "\\verb" t)
  150. ("~" "\\verb" t))
  151. "Alist of LaTeX expressions to convert emphasis fontifiers.
  152. Each element of the list is a list of three elements.
  153. The first element is the character used as a marker for fontification.
  154. The second element is a formatting string to wrap fontified text with.
  155. If it is \"\\verb\", Org will automatically select a deimiter
  156. character that is not in the string.
  157. The third element decides whether to protect converted text from other
  158. conversions."
  159. :group 'org-export-latex
  160. :type 'alist)
  161. (defcustom org-export-latex-title-command "\\maketitle"
  162. "The command used to insert the title just after \\begin{document}.
  163. If this string contains the formatting specification \"%s\" then
  164. it will be used as a formatting string, passing the title as an
  165. argument."
  166. :group 'org-export-latex
  167. :type 'string)
  168. (defcustom org-export-latex-import-inbuffer-stuff nil
  169. "Non-nil means define TeX macros for Org's inbuffer definitions.
  170. For example \orgTITLE for #+TITLE."
  171. :group 'org-export-latex
  172. :type 'boolean)
  173. (defcustom org-export-latex-date-format
  174. "%d %B %Y"
  175. "Format string for \\date{...}."
  176. :group 'org-export-latex
  177. :type 'string)
  178. (defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}"
  179. "Markup for TODO keywords, as a printf format.
  180. This can be a single format for all keywords, a cons cell with separate
  181. formats for not-done and done states, or an association list with setup
  182. for individual keywords. If a keyword shows up for which there is no
  183. markup defined, the first one in the association list will be used."
  184. :group 'org-export-latex
  185. :type '(choice
  186. (string :tag "Default")
  187. (cons :tag "Distinguish undone and done"
  188. (string :tag "Not-DONE states")
  189. (string :tag "DONE states"))
  190. (repeat :tag "Per keyword markup"
  191. (cons
  192. (string :tag "Keyword")
  193. (string :tag "Markup")))))
  194. (defcustom org-export-latex-timestamp-markup "\\textit{%s}"
  195. "A printf format string to be applied to time stamps."
  196. :group 'org-export-latex
  197. :type 'string)
  198. (defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}"
  199. "A printf format string to be applied to time stamps."
  200. :group 'org-export-latex
  201. :type 'string)
  202. (defcustom org-export-latex-tables-verbatim nil
  203. "When non-nil, tables are exported verbatim."
  204. :group 'org-export-latex
  205. :type 'boolean)
  206. (defcustom org-export-latex-tables-centered t
  207. "When non-nil, tables are exported in a center environment."
  208. :group 'org-export-latex
  209. :type 'boolean)
  210. (defcustom org-export-latex-tables-column-borders nil
  211. "When non-nil, grouping columns can cause outer vertical lines in tables.
  212. When nil, grouping causes only separation lines between groups."
  213. :group 'org-export-latex
  214. :type 'boolean)
  215. (defcustom org-export-latex-packages-alist nil
  216. "Alist of packages to be inserted in the header.
  217. Each cell is of the forma \( \"option\" . \"package\" \)."
  218. :group 'org-export-latex
  219. :type 'alist)
  220. (defcustom org-export-latex-low-levels 'itemize
  221. "How to convert sections below the current level of sectioning.
  222. This is specified by the `org-export-headline-levels' option or the
  223. value of \"H:\" in Org's #+OPTION line.
  224. This can be either nil (skip the sections), `description', `itemize',
  225. or `enumerate' (convert the sections as the corresponding list type), or
  226. a string to be used instead of \\section{%s}. In this latter case,
  227. the %s stands here for the inserted headline and is mandatory.
  228. It may also be a list of three string to define a user-defined environment
  229. that should be used. The first string should be the like
  230. \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
  231. to two occurrences of %s for the title and a lable, respectively. The third
  232. string should be like \"\\end{itemize\"."
  233. :group 'org-export-latex
  234. :type '(choice (const :tag "Ignore" nil)
  235. (const :tag "Convert as descriptive list" description)
  236. (const :tag "Convert as itemized list" itemize)
  237. (const :tag "Convert as enumerated list" enumerate)
  238. (list :tag "User-defined environment"
  239. :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s")
  240. (string :tag "Start")
  241. (string :tag "End")
  242. (string :tag "item"))
  243. (string :tag "Use a section string" :value "\\subparagraph{%s}")))
  244. (defcustom org-export-latex-list-parameters
  245. '(:cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  246. "Parameters for the LaTeX list exporter.
  247. These parameters will be passed on to `org-list-to-latex', which in turn
  248. will pass them (combined with the LaTeX default list parameters) to
  249. `org-list-to-generic'."
  250. :group 'org-export-latex
  251. :type 'plist)
  252. (defcustom org-export-latex-verbatim-wrap
  253. '("\\begin{verbatim}\n" . "\\end{verbatim}\n")
  254. "Environment to be wrapped around a fixed-width section in LaTeX export.
  255. This is a cons with two strings, to be added before and after the
  256. fixed-with text.
  257. Defaults to \\begin{verbatim} and \\end{verbatim}."
  258. :group 'org-export_translation
  259. :group 'org-export-latex
  260. :type '(cons (string :tag "Open")
  261. (string :tag "Close")))
  262. (defcustom org-export-latex-remove-from-headlines
  263. '(:todo nil :priority nil :tags nil)
  264. "A plist of keywords to remove from headlines. OBSOLETE.
  265. Non-nil means remove this keyword type from the headline.
  266. Don't remove the keys, just change their values.
  267. Obsolete, this variable is no longer used. Use the separate
  268. variables `org-export-with-todo-keywords', `org-export-with-priority',
  269. and `org-export-with-tags' instead."
  270. :type 'plist
  271. :group 'org-export-latex)
  272. (defcustom org-export-latex-image-default-option "width=10em"
  273. "Default option for images."
  274. :group 'org-export-latex
  275. :type 'string)
  276. (defcustom org-export-latex-inline-image-extensions
  277. '("pdf" "jpeg" "jpg" "png" "ps" "eps")
  278. "Extensions of image files that can be inlined into LaTeX.
  279. Note that the image extension *actually* allowed depend on the way the
  280. LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
  281. are OK. When processing through dvi to Postscript, only ps and eps are
  282. allowed. The default we use here encompasses both."
  283. :group 'org-export-latex
  284. :type '(repeat (string :tag "Extension")))
  285. (defcustom org-export-latex-coding-system nil
  286. "Coding system for the exported LaTex file."
  287. :group 'org-export-latex
  288. :type 'coding-system)
  289. (defgroup org-export-pdf nil
  290. "Options for exporting Org-mode files to PDF, via LaTeX."
  291. :tag "Org Export LaTeX"
  292. :group 'org-export-latex
  293. :group 'org-export)
  294. (defcustom org-latex-to-pdf-process
  295. '("pdflatex -interaction nonstopmode %s"
  296. "pdflatex -interaction nonstopmode %s")
  297. "Commands to process a LaTeX file to a PDF file.
  298. This is a list of strings, each of them will be given to the shell
  299. as a command. %s in the command will be replaced by the full file name, %b
  300. by the file base name (i.e. without extension).
  301. The reason why this is a list is that it usually takes several runs of
  302. pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
  303. mechanism to detect whihc of these commands have to be run to get to a stable
  304. result, and it also does not do any error checking.
  305. Alternatively, this may be a Lisp function that does the processing, so you
  306. could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
  307. THis function should accept the file name as its single argument."
  308. :group 'org-export-latex
  309. :type '(choice (repeat :tag "Shell command sequence"
  310. (string :tag "Shell command"))
  311. (function)))
  312. (defcustom org-export-pdf-remove-logfiles t
  313. "Non-nil means, remove the logfiles produced by PDF production.
  314. These are the .aux, .log, .out, and .toc files."
  315. :group 'org-export-pdf
  316. :type 'boolean)
  317. ;;; Hooks
  318. (defvar org-export-latex-after-blockquotes-hook nil
  319. "Hook run during LaTeX export, after blockquote, verse, center are done.")
  320. ;;; Autoload functions:
  321. ;;;###autoload
  322. (defun org-export-as-latex-batch ()
  323. "Call `org-export-as-latex', may be used in batch processing.
  324. For example:
  325. emacs --batch
  326. --load=$HOME/lib/emacs/org.el
  327. --eval \"(setq org-export-headline-levels 2)\"
  328. --visit=MyFile --funcall org-export-as-latex-batch"
  329. (org-export-as-latex org-export-headline-levels 'hidden))
  330. ;;;###autoload
  331. (defun org-export-as-latex-to-buffer (arg)
  332. "Call `org-export-as-latex` with output to a temporary buffer.
  333. No file is created. The prefix ARG is passed through to `org-export-as-latex'."
  334. (interactive "P")
  335. (org-export-as-latex arg nil nil "*Org LaTeX Export*")
  336. (when org-export-show-temporary-export-buffer
  337. (switch-to-buffer-other-window "*Org LaTeX Export*")))
  338. ;;;###autoload
  339. (defun org-replace-region-by-latex (beg end)
  340. "Replace the region from BEG to END with its LaTeX export.
  341. It assumes the region has `org-mode' syntax, and then convert it to
  342. LaTeX. This can be used in any buffer. For example, you could
  343. write an itemized list in `org-mode' syntax in an LaTeX buffer and
  344. then use this command to convert it."
  345. (interactive "r")
  346. (let (reg latex buf)
  347. (save-window-excursion
  348. (if (org-mode-p)
  349. (setq latex (org-export-region-as-latex
  350. beg end t 'string))
  351. (setq reg (buffer-substring beg end)
  352. buf (get-buffer-create "*Org tmp*"))
  353. (save-excursion
  354. (set-buffer buf)
  355. (erase-buffer)
  356. (insert reg)
  357. (org-mode)
  358. (setq latex (org-export-region-as-latex
  359. (point-min) (point-max) t 'string)))
  360. (kill-buffer buf)))
  361. (delete-region beg end)
  362. (insert latex)))
  363. ;;;###autoload
  364. (defun org-export-region-as-latex (beg end &optional body-only buffer)
  365. "Convert region from BEG to END in `org-mode' buffer to LaTeX.
  366. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  367. contents, and only produce the region of converted text, useful for
  368. cut-and-paste operations.
  369. If BUFFER is a buffer or a string, use/create that buffer as a target
  370. of the converted LaTeX. If BUFFER is the symbol `string', return the
  371. produced LaTeX as a string and leave no buffer behind. For example,
  372. a Lisp program could call this function in the following way:
  373. (setq latex (org-export-region-as-latex beg end t 'string))
  374. When called interactively, the output buffer is selected, and shown
  375. in a window. A non-interactive call will only return the buffer."
  376. (interactive "r\nP")
  377. (when (interactive-p)
  378. (setq buffer "*Org LaTeX Export*"))
  379. (let ((transient-mark-mode t) (zmacs-regions t)
  380. ext-plist rtn)
  381. (setq ext-plist (plist-put ext-plist :ignore-subree-p t))
  382. (goto-char end)
  383. (set-mark (point)) ;; to activate the region
  384. (goto-char beg)
  385. (setq rtn (org-export-as-latex
  386. nil nil ext-plist
  387. buffer body-only))
  388. (if (fboundp 'deactivate-mark) (deactivate-mark))
  389. (if (and (interactive-p) (bufferp rtn))
  390. (switch-to-buffer-other-window rtn)
  391. rtn)))
  392. ;;;###autoload
  393. (defun org-export-as-latex (arg &optional hidden ext-plist
  394. to-buffer body-only pub-dir)
  395. "Export current buffer to a LaTeX file.
  396. If there is an active region, export only the region. The prefix
  397. ARG specifies how many levels of the outline should become
  398. headlines. The default is 3. Lower levels will be exported
  399. depending on `org-export-latex-low-levels'. The default is to
  400. convert them as description lists.
  401. HIDDEN is obsolete and does nothing.
  402. EXT-PLIST is a property list with
  403. external parameters overriding org-mode's default settings, but
  404. still inferior to file-local settings. When TO-BUFFER is
  405. non-nil, create a buffer with that name and export to that
  406. buffer. If TO-BUFFER is the symbol `string', don't leave any
  407. buffer behind but just return the resulting LaTeX as a string.
  408. When BODY-ONLY is set, don't produce the file header and footer,
  409. simply return the content of \begin{document}...\end{document},
  410. without even the \begin{document} and \end{document} commands.
  411. when PUB-DIR is set, use this as the publishing directory."
  412. (interactive "P")
  413. ;; Make sure we have a file name when we need it.
  414. (when (and (not (or to-buffer body-only))
  415. (not buffer-file-name))
  416. (if (buffer-base-buffer)
  417. (org-set-local 'buffer-file-name
  418. (with-current-buffer (buffer-base-buffer)
  419. buffer-file-name))
  420. (error "Need a file name to be able to export")))
  421. (message "Exporting to LaTeX...")
  422. (org-unmodified
  423. (remove-text-properties (point-min) (point-max)
  424. '(:org-license-to-kill nil)))
  425. (org-update-radio-target-regexp)
  426. (org-export-latex-set-initial-vars ext-plist arg)
  427. (let* ((wcf (current-window-configuration))
  428. (opt-plist org-export-latex-options-plist)
  429. (region-p (org-region-active-p))
  430. (rbeg (and region-p (region-beginning)))
  431. (rend (and region-p (region-end)))
  432. (subtree-p
  433. (if (plist-get opt-plist :ignore-subree-p)
  434. nil
  435. (when region-p
  436. (save-excursion
  437. (goto-char rbeg)
  438. (and (org-at-heading-p)
  439. (>= (org-end-of-subtree t t) rend))))))
  440. (opt-plist (setq org-export-opt-plist
  441. (if subtree-p
  442. (org-export-add-subtree-options opt-plist rbeg)
  443. opt-plist)))
  444. ;; Make sure the variable contains the updated values.
  445. (org-export-latex-options-plist opt-plist)
  446. (title (or (and subtree-p (org-export-get-title-from-subtree))
  447. (plist-get opt-plist :title)
  448. (and (not
  449. (plist-get opt-plist :skip-before-1st-heading))
  450. (org-export-grab-title-from-buffer))
  451. (file-name-sans-extension
  452. (file-name-nondirectory buffer-file-name))))
  453. (filename (concat (file-name-as-directory
  454. (or pub-dir
  455. (org-export-directory :LaTeX ext-plist)))
  456. (file-name-sans-extension
  457. (or (and subtree-p
  458. (org-entry-get rbeg "EXPORT_FILE_NAME" t))
  459. (file-name-nondirectory ;sans-extension
  460. buffer-file-name)))
  461. ".tex"))
  462. (filename (if (equal (file-truename filename)
  463. (file-truename buffer-file-name))
  464. (concat filename ".tex")
  465. filename))
  466. (buffer (if to-buffer
  467. (cond
  468. ((eq to-buffer 'string) (get-buffer-create
  469. "*Org LaTeX Export*"))
  470. (t (get-buffer-create to-buffer)))
  471. (find-file-noselect filename)))
  472. (odd org-odd-levels-only)
  473. (header (org-export-latex-make-header title opt-plist))
  474. (skip (cond (subtree-p nil)
  475. (region-p nil)
  476. (t (plist-get opt-plist :skip-before-1st-heading))))
  477. (text (plist-get opt-plist :text))
  478. (org-export-preprocess-hook
  479. (cons
  480. `(lambda () (org-set-local 'org-complex-heading-regexp
  481. ,org-export-latex-complex-heading-re))
  482. org-export-preprocess-hook))
  483. (first-lines (if skip "" (org-export-latex-first-lines
  484. opt-plist
  485. (if subtree-p
  486. (save-excursion
  487. (goto-char rbeg)
  488. (point-at-bol 2))
  489. rbeg)
  490. (if region-p rend))))
  491. (coding-system (and (boundp 'buffer-file-coding-system)
  492. buffer-file-coding-system))
  493. (coding-system-for-write (or org-export-latex-coding-system
  494. coding-system))
  495. (save-buffer-coding-system (or org-export-latex-coding-system
  496. coding-system))
  497. (region (buffer-substring
  498. (if region-p (region-beginning) (point-min))
  499. (if region-p (region-end) (point-max))))
  500. (string-for-export
  501. (org-export-preprocess-string
  502. region
  503. :emph-multiline t
  504. :for-LaTeX t
  505. :comments nil
  506. :tags (plist-get opt-plist :tags)
  507. :priority (plist-get opt-plist :priority)
  508. :footnotes (plist-get opt-plist :footnotes)
  509. :timestamps (plist-get opt-plist :timestamps)
  510. :todo-keywords (plist-get opt-plist :todo-keywords)
  511. :add-text (if (eq to-buffer 'string) nil text)
  512. :skip-before-1st-heading skip
  513. :select-tags (plist-get opt-plist :select-tags)
  514. :exclude-tags (plist-get opt-plist :exclude-tags)
  515. :LaTeX-fragments nil)))
  516. (set-buffer buffer)
  517. (erase-buffer)
  518. (and (fboundp 'set-buffer-file-coding-system)
  519. (set-buffer-file-coding-system coding-system-for-write))
  520. ;; insert the header and initial document commands
  521. (unless (or (eq to-buffer 'string) body-only)
  522. (insert header))
  523. ;; insert text found in #+TEXT
  524. (when (and text (not (eq to-buffer 'string)))
  525. (insert (org-export-latex-content
  526. text '(lists tables fixed-width keywords))
  527. "\n\n"))
  528. ;; insert lines before the first headline
  529. (unless skip
  530. (insert first-lines))
  531. ;; export the content of headlines
  532. (org-export-latex-global
  533. (with-temp-buffer
  534. (insert string-for-export)
  535. (goto-char (point-min))
  536. (when (re-search-forward "^\\(\\*+\\) " nil t)
  537. (let* ((asters (length (match-string 1)))
  538. (level (if odd (- asters 2) (- asters 1))))
  539. (setq org-export-latex-add-level
  540. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  541. (org-export-latex-parse-global level odd)))))
  542. ;; finalization
  543. (unless body-only (insert "\n\\end{document}"))
  544. ;; Relocate the table of contents
  545. (goto-char (point-min))
  546. (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
  547. (goto-char (point-min))
  548. (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
  549. (replace-match ""))
  550. (goto-char (point-min))
  551. (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
  552. (replace-match "\\tableofcontents" t t)))
  553. (or to-buffer (save-buffer))
  554. (goto-char (point-min))
  555. (or (org-export-push-to-kill-ring "LaTeX")
  556. (message "Exporting to LaTeX...done"))
  557. (prog1
  558. (if (eq to-buffer 'string)
  559. (prog1 (buffer-substring (point-min) (point-max))
  560. (kill-buffer (current-buffer)))
  561. (current-buffer))
  562. (set-window-configuration wcf))))
  563. ;;;###autoload
  564. (defun org-export-as-pdf (arg &optional hidden ext-plist
  565. to-buffer body-only pub-dir)
  566. "Export as LaTeX, then process through to PDF."
  567. (interactive "P")
  568. (message "Exporting to PDF...")
  569. (let* ((wconfig (current-window-configuration))
  570. (lbuf (org-export-as-latex arg hidden ext-plist
  571. to-buffer body-only pub-dir))
  572. (file (buffer-file-name lbuf))
  573. (base (file-name-sans-extension (buffer-file-name lbuf)))
  574. (pdffile (concat base ".pdf"))
  575. (cmds org-latex-to-pdf-process)
  576. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  577. (bibtex-p (with-current-buffer lbuf
  578. (save-excursion
  579. (goto-char (point-min))
  580. (re-search-forward "\\\\bibliography{" nil t))))
  581. cmd)
  582. (with-current-buffer outbuf (erase-buffer))
  583. (and (file-exists-p pdffile) (delete-file pdffile))
  584. (message "Processing LaTeX file...")
  585. (if (and cmds (symbolp cmds))
  586. (funcall cmds file)
  587. (while cmds
  588. (setq cmd (pop cmds))
  589. (while (string-match "%b" cmd)
  590. (setq cmd (replace-match
  591. (save-match-data
  592. (shell-quote-argument base))
  593. t t cmd)))
  594. (while (string-match "%s" cmd)
  595. (setq cmd (replace-match
  596. (save-match-data
  597. (shell-quote-argument file))
  598. t t cmd)))
  599. (shell-command cmd outbuf outbuf)))
  600. (message "Processing LaTeX file...done")
  601. (if (not (file-exists-p pdffile))
  602. (error "PDF file was not produced")
  603. (set-window-configuration wconfig)
  604. (when org-export-pdf-remove-logfiles
  605. (dolist (ext '("aux" "log" "out" "toc"))
  606. (setq file (concat base "." ext))
  607. (and (file-exists-p file) (delete-file file))))
  608. (message "Exporting to PDF...done")
  609. pdffile)))
  610. ;;;###autoload
  611. (defun org-export-as-pdf-and-open (arg)
  612. "Export as LaTeX, then process through to PDF, and open."
  613. (interactive "P")
  614. (let ((pdffile (org-export-as-pdf arg)))
  615. (if pdffile
  616. (org-open-file pdffile)
  617. (error "PDF file was not produced"))))
  618. ;;; Parsing functions:
  619. (defun org-export-latex-parse-global (level odd)
  620. "Parse the current buffer recursively, starting at LEVEL.
  621. If ODD is non-nil, assume the buffer only contains odd sections.
  622. Return a list reflecting the document structure."
  623. (save-excursion
  624. (goto-char (point-min))
  625. (let* ((cnt 0) output
  626. (depth org-export-latex-sectioning-depth))
  627. (while (re-search-forward
  628. (concat "^\\(\\(?:\\*\\)\\{"
  629. (number-to-string (+ (if odd 2 1) level))
  630. "\\}\\) \\(.*\\)$")
  631. ;; make sure that there is no upper heading
  632. (when (> level 0)
  633. (save-excursion
  634. (save-match-data
  635. (re-search-forward
  636. (concat "^\\(\\(?:\\*\\)\\{"
  637. (number-to-string level)
  638. "\\}\\) \\(.*\\)$") nil t)))) t)
  639. (setq cnt (1+ cnt))
  640. (let* ((pos (match-beginning 0))
  641. (heading (match-string 2))
  642. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  643. (save-excursion
  644. (narrow-to-region
  645. (point)
  646. (save-match-data
  647. (if (re-search-forward
  648. (concat "^\\(\\(?:\\*\\)\\{"
  649. (number-to-string (+ (if odd 2 1) level))
  650. "\\}\\) \\(.*\\)$") nil t)
  651. (match-beginning 0)
  652. (point-max))))
  653. (goto-char (point-min))
  654. (setq output
  655. (append output
  656. (list
  657. (list
  658. `(pos . ,pos)
  659. `(level . ,nlevel)
  660. `(occur . ,cnt)
  661. `(heading . ,heading)
  662. `(content . ,(org-export-latex-parse-content))
  663. `(subcontent . ,(org-export-latex-parse-subcontent
  664. level odd)))))))
  665. (widen)))
  666. (list output))))
  667. (defun org-export-latex-parse-content ()
  668. "Extract the content of a section."
  669. (let ((beg (point))
  670. (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
  671. (progn (beginning-of-line) (point))
  672. (point-max))))
  673. (buffer-substring beg end)))
  674. (defun org-export-latex-parse-subcontent (level odd)
  675. "Extract the subcontent of a section at LEVEL.
  676. If ODD Is non-nil, assume subcontent only contains odd sections."
  677. (if (not (re-search-forward
  678. (concat "^\\(\\(?:\\*\\)\\{"
  679. (number-to-string (+ (if odd 4 2) level))
  680. "\\}\\) \\(.*\\)$")
  681. nil t))
  682. nil ; subcontent is nil
  683. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  684. ;;; Rendering functions:
  685. (defun org-export-latex-global (content)
  686. "Export CONTENT to LaTeX.
  687. CONTENT is an element of the list produced by
  688. `org-export-latex-parse-global'."
  689. (if (eq (car content) 'subcontent)
  690. (mapc 'org-export-latex-sub (cdr content))
  691. (org-export-latex-sub (car content))))
  692. (defun org-export-latex-sub (subcontent)
  693. "Export the list SUBCONTENT to LaTeX.
  694. SUBCONTENT is an alist containing information about the headline
  695. and its content."
  696. (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
  697. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  698. (defun org-export-latex-subcontent (subcontent num)
  699. "Export each cell of SUBCONTENT to LaTeX.
  700. If NUM, export sections as numerical sections."
  701. (let* ((heading (org-export-latex-fontify-headline
  702. (cdr (assoc 'heading subcontent))))
  703. (level (- (cdr (assoc 'level subcontent))
  704. org-export-latex-add-level))
  705. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  706. (content (cdr (assoc 'content subcontent)))
  707. (subcontent (cadr (assoc 'subcontent subcontent)))
  708. (label (org-get-text-property-any 0 'target heading))
  709. (label-list (cons label (cdr (assoc label
  710. org-export-target-aliases)))))
  711. (cond
  712. ;; Normal conversion
  713. ((<= level org-export-latex-sectioning-depth)
  714. (let* ((sec (nth (1- level) org-export-latex-sectioning))
  715. start end)
  716. (if (consp (cdr sec))
  717. (setq start (nth (if num 0 2) sec)
  718. end (nth (if num 1 3) sec))
  719. (setq start (if num (car sec) (cdr sec))))
  720. (insert (format start heading) "\n")
  721. (when label
  722. (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
  723. label-list "\n") "\n"))
  724. (insert (org-export-latex-content content))
  725. (cond ((stringp subcontent) (insert subcontent))
  726. ((listp subcontent) (org-export-latex-sub subcontent)))
  727. (if end (insert end "\n"))))
  728. ;; At a level under the hl option: we can drop this subsection
  729. ((> level org-export-latex-sectioning-depth)
  730. (cond ((eq org-export-latex-low-levels 'description)
  731. (if (string-match "% ends low level$"
  732. (buffer-substring (point-at-bol 0) (point)))
  733. (delete-region (point-at-bol 0) (point))
  734. (insert "\\begin{description}\n"))
  735. (insert (format "\n\\item[%s]%s~\n\n"
  736. heading
  737. (if label (format "\\label{%s}" label) "")))
  738. (insert (org-export-latex-content content))
  739. (cond ((stringp subcontent) (insert subcontent))
  740. ((listp subcontent) (org-export-latex-sub subcontent)))
  741. (insert "\\end{description} % ends low level\n"))
  742. ((memq org-export-latex-low-levels '(itemize enumerate))
  743. (if (string-match "% ends low level$"
  744. (buffer-substring (point-at-bol 0) (point)))
  745. (delete-region (point-at-bol 0) (point))
  746. (insert (format "\\begin{%s}\n"
  747. (symbol-name org-export-latex-low-levels))))
  748. (insert (format "\n\\item %s\\\\\n%s\n"
  749. heading
  750. (if label (format "\\label{%s}" label) "")))
  751. (insert (org-export-latex-content content))
  752. (cond ((stringp subcontent) (insert subcontent))
  753. ((listp subcontent) (org-export-latex-sub subcontent)))
  754. (insert (format "\\end{%s} %% ends low level\n"
  755. (symbol-name org-export-latex-low-levels))))
  756. ((listp org-export-latex-low-levels)
  757. (if (string-match "% ends low level$"
  758. (buffer-substring (point-at-bol 0) (point)))
  759. (delete-region (point-at-bol 0) (point))
  760. (insert (car org-export-latex-low-levels) "\n"))
  761. (insert (format (nth 2 org-export-latex-low-levels)
  762. heading
  763. (if label (format "\\label{%s}" label) "")))
  764. (insert (org-export-latex-content content))
  765. (cond ((stringp subcontent) (insert subcontent))
  766. ((listp subcontent) (org-export-latex-sub subcontent)))
  767. (insert (nth 1 org-export-latex-low-levels)
  768. " %% ends low level\n"))
  769. ((stringp org-export-latex-low-levels)
  770. (insert (format org-export-latex-low-levels heading) "\n")
  771. (when label (insert (format "\\label{%s}\n" label)))
  772. (insert (org-export-latex-content content))
  773. (cond ((stringp subcontent) (insert subcontent))
  774. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  775. ;;; Exporting internals:
  776. (defun org-export-latex-set-initial-vars (ext-plist level)
  777. "Store org local variables required for LaTeX export.
  778. EXT-PLIST is an optional additional plist.
  779. LEVEL indicates the default depth for export."
  780. (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
  781. org-export-latex-done-keywords org-done-keywords
  782. org-export-latex-not-done-keywords org-not-done-keywords
  783. org-export-latex-complex-heading-re org-complex-heading-regexp
  784. org-export-latex-display-custom-times org-display-custom-times
  785. org-export-latex-all-targets-re
  786. (org-make-target-link-regexp (org-all-targets))
  787. org-export-latex-options-plist
  788. (org-combine-plists (org-default-export-plist) ext-plist
  789. (org-infile-export-plist))
  790. org-export-latex-class
  791. (or (and (org-region-active-p)
  792. (save-excursion
  793. (goto-char (region-beginning))
  794. (and (looking-at org-complex-heading-regexp)
  795. (org-entry-get nil "LaTeX_CLASS" 'selective))))
  796. (save-excursion
  797. (save-restriction
  798. (widen)
  799. (goto-char (point-min))
  800. (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
  801. (match-string 1))))
  802. org-export-latex-default-class)
  803. org-export-latex-class
  804. (or (car (assoc org-export-latex-class org-export-latex-classes))
  805. (error "No definition for class `%s' in `org-export-latex-classes'"
  806. org-export-latex-class))
  807. org-export-latex-header
  808. (cadr (assoc org-export-latex-class org-export-latex-classes))
  809. org-export-latex-sectioning
  810. (cddr (assoc org-export-latex-class org-export-latex-classes))
  811. org-export-latex-sectioning-depth
  812. (or level
  813. (let ((hl-levels
  814. (plist-get org-export-latex-options-plist :headline-levels))
  815. (sec-depth (length org-export-latex-sectioning)))
  816. (if (> hl-levels sec-depth) sec-depth hl-levels)))))
  817. (defun org-export-latex-make-header (title opt-plist)
  818. "Make the LaTeX header and return it as a string.
  819. TITLE is the current title from the buffer or region.
  820. OPT-PLIST is the options plist for current buffer."
  821. (let ((toc (plist-get opt-plist :table-of-contents))
  822. (author (plist-get opt-plist :author)))
  823. (concat
  824. (if (plist-get opt-plist :time-stamp-file)
  825. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  826. ;; insert LaTeX custom header
  827. (org-export-apply-macros-in-string org-export-latex-header)
  828. "\n"
  829. ;; insert information on LaTeX packages
  830. (when org-export-latex-packages-alist
  831. (mapconcat (lambda(p)
  832. (if (equal "" (car p))
  833. (format "\\usepackage{%s}" (cadr p))
  834. (format "\\usepackage[%s]{%s}"
  835. (car p) (cadr p))))
  836. org-export-latex-packages-alist "\n"))
  837. ;; insert additional commands in the header
  838. (org-export-apply-macros-in-string
  839. (plist-get opt-plist :latex-header-extra))
  840. (org-export-apply-macros-in-string org-export-latex-append-header)
  841. ;; insert the title
  842. (format
  843. "\n\n\\title{%s}\n"
  844. ;; convert the title
  845. (org-export-latex-content
  846. title '(lists tables fixed-width keywords)))
  847. ;; insert author info
  848. (if (plist-get opt-plist :author-info)
  849. (format "\\author{%s}\n"
  850. (or author user-full-name))
  851. (format "%%\\author{%s}\n"
  852. (or author user-full-name)))
  853. ;; insert the date
  854. (format "\\date{%s}\n"
  855. (format-time-string
  856. (or (plist-get opt-plist :date)
  857. org-export-latex-date-format)))
  858. ;; beginning of the document
  859. "\n\\begin{document}\n\n"
  860. ;; insert the title command
  861. (if (string-match "%s" org-export-latex-title-command)
  862. (format org-export-latex-title-command title)
  863. org-export-latex-title-command)
  864. "\n\n"
  865. ;; table of contents
  866. (when (and org-export-with-toc
  867. (plist-get opt-plist :section-numbers))
  868. (cond ((numberp toc)
  869. (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
  870. (min toc (plist-get opt-plist :headline-levels))))
  871. (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
  872. (plist-get opt-plist :headline-levels))))))))
  873. (defun org-export-latex-first-lines (opt-plist &optional beg end)
  874. "Export the first lines before first headline.
  875. If BEG is non-nil, it is the beginning of the region.
  876. If END is non-nil, it is the end of the region."
  877. (save-excursion
  878. (goto-char (or beg (point-min)))
  879. (let* ((pt (point))
  880. (end (if (re-search-forward "^\\*+ " end t)
  881. (goto-char (match-beginning 0))
  882. (goto-char (point-max)))))
  883. (prog1
  884. (org-export-latex-content
  885. (org-export-preprocess-string
  886. (buffer-substring pt end)
  887. :for-LaTeX t
  888. :emph-multiline t
  889. :add-text nil
  890. :comments nil
  891. :skip-before-1st-heading nil
  892. :LaTeX-fragments nil
  893. :timestamps (plist-get opt-plist :timestamps)
  894. :footnotes (plist-get opt-plist :footnotes)))
  895. (org-unmodified
  896. (add-text-properties pt (max pt (1- end))
  897. '(:org-license-to-kill t)))))))
  898. (defvar org-export-latex-header-defs nil
  899. "The header definitions that might be used in the LaTeX body.")
  900. (defvar org-export-latex-header-defs-re nil
  901. "The header definitions that might be used in the LaTeX body.")
  902. (defun org-export-latex-content (content &optional exclude-list)
  903. "Convert CONTENT string to LaTeX.
  904. Don't perform conversions that are in EXCLUDE-LIST. Recognized
  905. conversion types are: quotation-marks, emphasis, sub-superscript,
  906. links, keywords, lists, tables, fixed-width"
  907. (with-temp-buffer
  908. (insert content)
  909. (unless (memq 'timestamps exclude-list)
  910. (org-export-latex-time-stamps))
  911. (unless (memq 'quotation-marks exclude-list)
  912. (org-export-latex-quotation-marks))
  913. (unless (memq 'emphasis exclude-list)
  914. (when (plist-get org-export-latex-options-plist :emphasize)
  915. (org-export-latex-fontify)))
  916. (unless (memq 'sub-superscript exclude-list)
  917. (org-export-latex-special-chars
  918. (plist-get org-export-latex-options-plist :sub-superscript)))
  919. (unless (memq 'links exclude-list)
  920. (org-export-latex-links))
  921. (unless (memq 'keywords exclude-list)
  922. (org-export-latex-keywords))
  923. (unless (memq 'lists exclude-list)
  924. (org-export-latex-lists))
  925. (unless (memq 'tables exclude-list)
  926. (org-export-latex-tables
  927. (plist-get org-export-latex-options-plist :tables)))
  928. (unless (memq 'fixed-width exclude-list)
  929. (org-export-latex-fixed-width
  930. (plist-get org-export-latex-options-plist :fixed-width)))
  931. ;; return string
  932. (buffer-substring (point-min) (point-max))))
  933. (defun org-export-latex-protect-string (s)
  934. "Add the org-protected property to string S."
  935. (add-text-properties 0 (length s) '(org-protected t) s) s)
  936. (defun org-export-latex-protect-char-in-string (char-list string)
  937. "Add org-protected text-property to char from CHAR-LIST in STRING."
  938. (with-temp-buffer
  939. (save-match-data
  940. (insert string)
  941. (goto-char (point-min))
  942. (while (re-search-forward (regexp-opt char-list) nil t)
  943. (add-text-properties (match-beginning 0)
  944. (match-end 0) '(org-protected t)))
  945. (buffer-string))))
  946. (defun org-export-latex-keywords-maybe (&optional remove-list)
  947. "Maybe remove keywords depending on rules in REMOVE-LIST."
  948. (goto-char (point-min))
  949. (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
  950. (case-fold-search nil)
  951. (todo-markup org-export-latex-todo-keyword-markup)
  952. fmt)
  953. ;; convert TODO keywords
  954. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  955. (if (plist-get remove-list :todo)
  956. (replace-match "")
  957. (setq fmt (cond
  958. ((stringp todo-markup) todo-markup)
  959. ((and (consp todo-markup) (stringp (car todo-markup)))
  960. (if (member (match-string 1) org-export-latex-done-keywords)
  961. (cdr todo-markup) (car todo-markup)))
  962. (t (cdr (or (assoc (match-string 1) todo-markup)
  963. (car todo-markup))))))
  964. (replace-match (format fmt (match-string 1)) t t)))
  965. ;; convert priority string
  966. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  967. (if (plist-get remove-list :priority)
  968. (replace-match "")
  969. (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
  970. ;; convert tags
  971. (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
  972. (if (or (not org-export-with-tags)
  973. (plist-get remove-list :tags))
  974. (replace-match "")
  975. (replace-match
  976. (org-export-latex-protect-string
  977. (format "\\textbf{%s}"
  978. (save-match-data
  979. (replace-regexp-in-string
  980. "_" "\\\\_" (match-string 0)))))
  981. t t)))))
  982. (defun org-export-latex-fontify-headline (string)
  983. "Fontify special words in STRING."
  984. (with-temp-buffer
  985. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  986. ;; the beginning of the buffer - inserting "\n" is safe here though.
  987. (insert "\n" string)
  988. (goto-char (point-min))
  989. (when (plist-get org-export-latex-options-plist :emphasize)
  990. (org-export-latex-fontify))
  991. (org-export-latex-keywords-maybe)
  992. (org-export-latex-special-chars
  993. (plist-get org-export-latex-options-plist :sub-superscript))
  994. (org-export-latex-links)
  995. (org-trim (buffer-string))))
  996. (defun org-export-latex-time-stamps ()
  997. "Format time stamps."
  998. (goto-char (point-min))
  999. (let ((org-display-custom-times org-export-latex-display-custom-times))
  1000. (while (re-search-forward org-ts-regexp-both nil t)
  1001. (org-if-unprotected-at (1- (point))
  1002. (replace-match
  1003. (org-export-latex-protect-string
  1004. (format org-export-latex-timestamp-markup
  1005. (substring (org-translate-time (match-string 0)) 1 -1)))
  1006. t t)))))
  1007. (defun org-export-latex-quotation-marks ()
  1008. "Export quotation marks depending on language conventions."
  1009. (let* ((lang (plist-get org-export-latex-options-plist :language))
  1010. (quote-rpl (if (equal lang "fr")
  1011. '(("\\(\\s-\\)\"" "«~")
  1012. ("\\(\\S-\\)\"" "~»")
  1013. ("\\(\\s-\\)'" "`"))
  1014. '(("\\(\\s-\\|(\\)\"" "``")
  1015. ("\\(\\S-\\)\"" "''")
  1016. ("\\(\\s-\\|(\\)'" "`")))))
  1017. (mapc (lambda(l) (goto-char (point-min))
  1018. (while (re-search-forward (car l) nil t)
  1019. (let ((rpl (concat (match-string 1) (cadr l))))
  1020. (org-export-latex-protect-string rpl)
  1021. (org-if-unprotected-1
  1022. (replace-match rpl t t))))) quote-rpl)))
  1023. (defun org-export-latex-special-chars (sub-superscript)
  1024. "Export special characters to LaTeX.
  1025. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  1026. See the `org-export-latex.el' code for a complete conversion table."
  1027. (goto-char (point-min))
  1028. (mapc (lambda(c)
  1029. (goto-char (point-min))
  1030. (while (re-search-forward c nil t)
  1031. ;; Put the point where to check for org-protected
  1032. (unless (get-text-property (match-beginning 2) 'org-protected)
  1033. (cond ((member (match-string 2) '("\\$" "$"))
  1034. (if (equal (match-string 2) "\\$")
  1035. nil
  1036. (replace-match "\\$" t t)))
  1037. ((member (match-string 2) '("&" "%" "#"))
  1038. (if (equal (match-string 1) "\\")
  1039. (replace-match (match-string 2) t t)
  1040. (replace-match (concat (match-string 1) "\\"
  1041. (match-string 2)) t t)))
  1042. ((equal (match-string 2) "...")
  1043. (replace-match
  1044. (concat (match-string 1)
  1045. (org-export-latex-protect-string "\\ldots{}")) t t))
  1046. ((equal (match-string 2) "~")
  1047. (cond ((equal (match-string 1) "\\") nil)
  1048. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  1049. (replace-match (concat (match-string 1) "\\~") t t))
  1050. (t (replace-match
  1051. (org-export-latex-protect-string
  1052. (concat (match-string 1) "\\~{}")) t t))))
  1053. ((member (match-string 2) '("{" "}"))
  1054. (unless (save-match-data (org-inside-latex-math-p))
  1055. (if (equal (match-string 1) "\\")
  1056. (replace-match (match-string 2) t t)
  1057. (replace-match (concat (match-string 1) "\\"
  1058. (match-string 2)) t t)))))
  1059. (unless (save-match-data (org-inside-latex-math-p))
  1060. (cond ((equal (match-string 2) "\\")
  1061. (replace-match (or (save-match-data
  1062. (org-export-latex-treat-backslash-char
  1063. (match-string 1)
  1064. (or (match-string 3) "")))
  1065. "") t t))
  1066. ((member (match-string 2) '("_" "^"))
  1067. (replace-match (or (save-match-data
  1068. (org-export-latex-treat-sub-super-char
  1069. sub-superscript
  1070. (match-string 2)
  1071. (match-string 1)
  1072. (match-string 3))) "") t t)))))))
  1073. '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  1074. "\\(\\(\\\\?\\$\\)\\)"
  1075. "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
  1076. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
  1077. "\\(.\\|^\\)\\(&\\)"
  1078. "\\(.\\|^\\)\\(#\\)"
  1079. "\\(.\\|^\\)\\(%\\)"
  1080. "\\(.\\|^\\)\\({\\)"
  1081. "\\(.\\|^\\)\\(}\\)"
  1082. "\\(.\\|^\\)\\(~\\)"
  1083. "\\(.\\|^\\)\\(\\.\\.\\.\\)"
  1084. ;; (?\< . "\\textless{}")
  1085. ;; (?\> . "\\textgreater{}")
  1086. )))
  1087. (defun org-inside-latex-math-p ()
  1088. (get-text-property (point) 'org-latex-math))
  1089. (defun org-export-latex-treat-sub-super-char
  1090. (subsup char string-before string-after)
  1091. "Convert the \"_\" and \"^\" characters to LaTeX.
  1092. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  1093. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  1094. (cond ((equal string-before "\\")
  1095. (concat string-before char string-after))
  1096. ;; this is part of a math formula
  1097. ((and (string-match "\\S-+" string-before)
  1098. (string-match "\\S-+" string-after))
  1099. (cond ((eq 'org-link (get-text-property 0 'face char))
  1100. (concat string-before "\\" char string-after))
  1101. ((save-match-data (org-inside-latex-math-p))
  1102. (if subsup
  1103. (cond ((eq 1 (length string-after))
  1104. (concat string-before char string-after))
  1105. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  1106. (format "%s%s{%s}" string-before char
  1107. (match-string 1 string-after))))))
  1108. ((and (> (length string-after) 1)
  1109. (or (eq subsup t)
  1110. (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
  1111. (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
  1112. (org-export-latex-protect-string
  1113. (format "%s$%s{%s}$" string-before char
  1114. (if (and (> (match-end 1) (1+ (match-beginning 1)))
  1115. (not (equal (substring string-after 0 2) "{\\")))
  1116. (concat "\\mathrm{" (match-string 1 string-after) "}")
  1117. (match-string 1 string-after)))))
  1118. ((eq subsup t) (concat string-before "$" char string-after "$"))
  1119. (t (org-export-latex-protect-string
  1120. (concat string-before "\\" char "{}" string-after)))))
  1121. (t (org-export-latex-protect-string
  1122. (concat string-before "\\" char "{}" string-after)))))
  1123. (defun org-export-latex-treat-backslash-char (string-before string-after)
  1124. "Convert the \"$\" special character to LaTeX.
  1125. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  1126. (cond ((member (list string-after) org-html-entities)
  1127. ;; backslash is part of a special entity (like "\alpha")
  1128. (concat string-before "$\\"
  1129. (or (cdar (member (list string-after) org-html-entities))
  1130. string-after) "$"))
  1131. ((and (not (string-match "^[ \n\t]" string-after))
  1132. (not (string-match "[ \t]\\'\\|^" string-before)))
  1133. ;; backslash is inside a word
  1134. (org-export-latex-protect-string
  1135. (concat string-before "\\textbackslash{}" string-after)))
  1136. ((not (or (equal string-after "")
  1137. (string-match "^[ \t\n]" string-after)))
  1138. ;; backslash might escape a character (like \#) or a user TeX
  1139. ;; macro (like \setcounter)
  1140. (org-export-latex-protect-string
  1141. (concat string-before "\\" string-after)))
  1142. ((and (string-match "^[ \t\n]" string-after)
  1143. (string-match "[ \t\n]\\'" string-before))
  1144. ;; backslash is alone, convert it to $\backslash$
  1145. (org-export-latex-protect-string
  1146. (concat string-before "\\textbackslash{}" string-after)))
  1147. (t (org-export-latex-protect-string
  1148. (concat string-before "\\textbackslash{}" string-after)))))
  1149. (defun org-export-latex-keywords ()
  1150. "Convert special keywords to LaTeX."
  1151. (goto-char (point-min))
  1152. (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
  1153. (replace-match (format org-export-latex-timestamp-keyword-markup
  1154. (match-string 0)) t t)
  1155. (save-excursion
  1156. (beginning-of-line 1)
  1157. (unless (looking-at ".*\\\\newline[ \t]*$")
  1158. (end-of-line 1)
  1159. (insert "\\newline")))))
  1160. (defun org-export-latex-fixed-width (opt)
  1161. "When OPT is non-nil convert fixed-width sections to LaTeX."
  1162. (goto-char (point-min))
  1163. (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
  1164. (if opt
  1165. (progn (goto-char (match-beginning 0))
  1166. (insert "\\begin{verbatim}\n")
  1167. (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
  1168. (replace-match (concat (match-string 1)
  1169. (match-string 2)) t t)
  1170. (forward-line))
  1171. (insert "\\end{verbatim}\n\n"))
  1172. (progn (goto-char (match-beginning 0))
  1173. (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
  1174. (replace-match (concat "%" (match-string 1)
  1175. (match-string 2)) t t)
  1176. (forward-line))))))
  1177. (defvar org-table-last-alignment) ; defined in org-table.el
  1178. (declare-function orgtbl-to-latex "org-table" (table params) t)
  1179. (defun org-export-latex-tables (insert)
  1180. "Convert tables to LaTeX and INSERT it."
  1181. (goto-char (point-min))
  1182. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  1183. ;; FIXME really need to save-excursion?
  1184. (save-excursion (org-table-align))
  1185. (let* ((beg (org-table-begin))
  1186. (end (org-table-end))
  1187. (raw-table (buffer-substring beg end))
  1188. fnum fields line lines olines gr colgropen line-fmt align
  1189. caption label attr floatp longtblp)
  1190. (if org-export-latex-tables-verbatim
  1191. (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
  1192. "\\end{verbatim}\n")))
  1193. (apply 'delete-region (list beg end))
  1194. (insert (org-export-latex-protect-string tbl)))
  1195. (progn
  1196. (setq caption (org-find-text-property-in-string
  1197. 'org-caption raw-table)
  1198. attr (org-find-text-property-in-string
  1199. 'org-attributes raw-table)
  1200. label (org-find-text-property-in-string
  1201. 'org-label raw-table)
  1202. longtblp (and attr (stringp attr)
  1203. (string-match "\\<longtable\\>" attr))
  1204. align (and attr (stringp attr)
  1205. (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
  1206. (match-string 1 attr))
  1207. floatp (or caption label))
  1208. (setq lines (org-split-string raw-table "\n"))
  1209. (apply 'delete-region (list beg end))
  1210. (when org-export-table-remove-special-lines
  1211. (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
  1212. ;; make a formatting string to reflect aligment
  1213. (setq olines lines)
  1214. (while (and (not line-fmt) (setq line (pop olines)))
  1215. (unless (string-match "^[ \t]*|-" line)
  1216. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1217. (setq fnum (make-vector (length fields) 0))
  1218. (setq line-fmt
  1219. (mapconcat
  1220. (lambda (x)
  1221. (setq gr (pop org-table-colgroup-info))
  1222. (format "%s%%s%s"
  1223. (cond ((eq gr :start)
  1224. (prog1 (if colgropen "|" "|")
  1225. (setq colgropen t)))
  1226. ((eq gr :startend)
  1227. (prog1 (if colgropen "|" "|")
  1228. (setq colgropen nil)))
  1229. (t ""))
  1230. (if (memq gr '(:end :startend))
  1231. (progn (setq colgropen nil) "|")
  1232. "")))
  1233. fnum ""))))
  1234. ;; fix double || in line-fmt
  1235. (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
  1236. ;; maybe remove the first and last "|"
  1237. (when (and (not org-export-latex-tables-column-borders)
  1238. (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
  1239. (setq line-fmt (match-string 2 line-fmt)))
  1240. ;; format alignment
  1241. (unless align
  1242. (setq align (apply 'format
  1243. (cons line-fmt
  1244. (mapcar (lambda (x) (if x "r" "l"))
  1245. org-table-last-alignment)))))
  1246. ;; prepare the table to send to orgtbl-to-latex
  1247. (setq lines
  1248. (mapcar
  1249. (lambda(elem)
  1250. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  1251. (org-split-string (org-trim elem) "|")))
  1252. lines))
  1253. (when insert
  1254. (insert (org-export-latex-protect-string
  1255. (concat
  1256. (if longtblp
  1257. (concat "\\begin{longtable}{" align "}\n")
  1258. (if floatp "\\begin{table}[htb]\n"))
  1259. (if (or floatp longtblp)
  1260. (format
  1261. "\\caption{%s%s}"
  1262. (if label (concat "\\\label{" label "}") "")
  1263. (or caption "")))
  1264. (if longtblp "\\\\\n" "\n")
  1265. (if (and org-export-latex-tables-centered (not longtblp))
  1266. "\\begin{center}\n")
  1267. (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
  1268. (orgtbl-to-latex
  1269. lines
  1270. `(:tstart nil :tend nil
  1271. :hlend ,(if longtblp
  1272. (format "\\\\
  1273. \\hline
  1274. \\endhead
  1275. \\hline\\multicolumn{%d}{r}{Continued on next page}\\
  1276. \\endfoot
  1277. \\endlastfoot" (length org-table-last-alignment))
  1278. nil)))
  1279. (if (not longtblp) (concat "\n\\end{tabular}"))
  1280. (if longtblp "\n" (if org-export-latex-tables-centered
  1281. "\n\\end{center}\n" "\n"))
  1282. (if longtblp
  1283. "\\end{longtable}"
  1284. (if floatp "\\end{table}"))))
  1285. "\n\n")))))))
  1286. (defun org-export-latex-fontify ()
  1287. "Convert fontification to LaTeX."
  1288. (goto-char (point-min))
  1289. (while (re-search-forward org-emph-re nil t)
  1290. ;; The match goes one char after the *string*
  1291. (let ((emph (assoc (match-string 3)
  1292. org-export-latex-emphasis-alist))
  1293. (beg (match-beginning 0))
  1294. (end (match-end 0))
  1295. rpl)
  1296. (unless emph
  1297. (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
  1298. (match-string 3)))
  1299. (unless (or (get-text-property (1- (point)) 'org-protected)
  1300. (save-excursion
  1301. (goto-char (match-beginning 1))
  1302. (save-match-data
  1303. (and (org-at-table-p)
  1304. (string-match
  1305. "[|\n]" (buffer-substring beg end))))))
  1306. (setq rpl (concat (match-string 1)
  1307. (org-export-latex-emph-format (cadr emph)
  1308. (match-string 4))
  1309. (match-string 5)))
  1310. (if (caddr emph)
  1311. (setq rpl (org-export-latex-protect-string rpl)))
  1312. (replace-match rpl t t)))
  1313. (backward-char)))
  1314. (defvar org-export-latex-use-verb nil)
  1315. (defun org-export-latex-emph-format (format string)
  1316. "Format an emphasis string and handle the \\verb special case."
  1317. (when (equal format "\\verb")
  1318. (save-match-data
  1319. (if org-export-latex-use-verb
  1320. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1321. (catch 'exit
  1322. (loop for i from 0 to (1- (length ll)) do
  1323. (if (not (string-match (regexp-quote (substring ll i (1+ i)))
  1324. string))
  1325. (progn
  1326. (setq format (concat "\\verb" (substring ll i (1+ i))
  1327. "%s" (substring ll i (1+ i))))
  1328. (throw 'exit nil))))))
  1329. (let ((start 0)
  1330. (trans '(("\\" . "\\backslash")
  1331. ("~" . "\\ensuremath{\\sim}")
  1332. ("^" . "\\ensuremath{\\wedge}")))
  1333. (rtn "") char)
  1334. (while (string-match "[\\{}$%&_#~^]" string)
  1335. (setq char (match-string 0 string))
  1336. (if (> (match-beginning 0) 0)
  1337. (setq rtn (concat rtn (substring string
  1338. 0 (match-beginning 0)))))
  1339. (setq string (substring string (1+ (match-beginning 0))))
  1340. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  1341. rtn (concat rtn char)))
  1342. (setq string (concat rtn string) format "\\texttt{%s}")))))
  1343. (setq string (org-export-latex-protect-string
  1344. (format format string))))
  1345. (defun org-export-latex-links ()
  1346. ;; Make sure to use the LaTeX hyperref and graphicx package
  1347. ;; or send some warnings.
  1348. "Convert links to LaTeX."
  1349. (goto-char (point-min))
  1350. (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
  1351. (org-if-unprotected
  1352. (goto-char (match-beginning 0))
  1353. (let* ((re-radio org-export-latex-all-targets-re)
  1354. (remove (list (match-beginning 0) (match-end 0)))
  1355. (raw-path (org-extract-attributes (match-string 3)))
  1356. (full-raw-path (concat (match-string 1) raw-path))
  1357. (desc (match-string 5))
  1358. (type (or (match-string 2)
  1359. (if (or (file-name-absolute-p raw-path)
  1360. (string-match "^\\.\\.?/" raw-path))
  1361. "file")))
  1362. (coderefp (equal type "coderef"))
  1363. (caption (org-find-text-property-in-string 'org-caption raw-path))
  1364. (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
  1365. (plist-get org-export-latex-options-plist :latex-image-options)))
  1366. (label (org-find-text-property-in-string 'org-label raw-path))
  1367. (floatp (or label caption))
  1368. imgp radiop
  1369. ;; define the path of the link
  1370. (path (cond
  1371. ((member type '("coderef"))
  1372. raw-path)
  1373. ((member type '("http" "https" "ftp"))
  1374. (concat type ":" raw-path))
  1375. ((and re-radio (string-match re-radio raw-path))
  1376. (setq radiop t))
  1377. ((equal type "mailto")
  1378. (concat type ":" raw-path))
  1379. ((equal type "file")
  1380. (if (and (org-file-image-p
  1381. (expand-file-name
  1382. raw-path)
  1383. org-export-latex-inline-image-extensions)
  1384. (or (get-text-property 0 'org-no-description
  1385. raw-path)
  1386. (equal desc full-raw-path)))
  1387. (setq imgp t)
  1388. (progn (when (string-match "\\(.+\\)::.+" raw-path)
  1389. (setq raw-path (match-string 1 raw-path)))
  1390. (if (file-exists-p raw-path)
  1391. (concat type "://" (expand-file-name raw-path))
  1392. (concat type "://" (org-export-directory
  1393. :LaTeX org-export-latex-options-plist)
  1394. raw-path))))))))
  1395. ;; process with link inserting
  1396. (apply 'delete-region remove)
  1397. (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
  1398. (insert
  1399. (concat
  1400. (if floatp "\\begin{figure}[htb]\n")
  1401. (format "\\centerline{\\includegraphics[%s]{%s}}\n"
  1402. attr
  1403. (if (file-name-absolute-p raw-path)
  1404. (expand-file-name raw-path)
  1405. raw-path))
  1406. (if floatp
  1407. (format "\\caption{%s%s}\n"
  1408. (if label (concat "\\label{" label "}") "")
  1409. (or caption "")))
  1410. (if floatp "\\end{figure}\n"))))
  1411. (coderefp
  1412. (insert (format
  1413. (org-export-get-coderef-format path desc)
  1414. (cdr (assoc path org-export-code-refs)))))
  1415. (radiop (insert (format "\\hyperref[%s]{%s}"
  1416. (org-solidify-link-text raw-path) desc)))
  1417. ((not type)
  1418. (insert (format "\\hyperref[%s]{%s}"
  1419. (org-solidify-link-text raw-path) desc)))
  1420. (path (insert (format "\\href{%s}{%s}" path desc)))
  1421. (t (insert "\\texttt{" desc "}")))))))
  1422. (defvar org-latex-entities) ; defined below
  1423. (defvar org-latex-entities-regexp) ; defined below
  1424. (defvar org-latex-entities-exceptions) ; defined below
  1425. (defun org-export-latex-preprocess (parameters)
  1426. "Clean stuff in the LaTeX export."
  1427. ;; Preserve line breaks
  1428. (goto-char (point-min))
  1429. (while (re-search-forward "\\\\\\\\" nil t)
  1430. (add-text-properties (match-beginning 0) (match-end 0)
  1431. '(org-protected t)))
  1432. ;; Preserve latex environments
  1433. (goto-char (point-min))
  1434. (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
  1435. (let* ((start (progn (beginning-of-line) (point)))
  1436. (end (or (and (re-search-forward
  1437. (concat "^[ \t]*\\\\end{"
  1438. (regexp-quote (match-string 1))
  1439. "}") nil t)
  1440. (point-at-eol))
  1441. (point-max))))
  1442. (add-text-properties start end '(org-protected t))))
  1443. ;; Preserve math snippets
  1444. (let* ((matchers (plist-get org-format-latex-options :matchers))
  1445. (re-list org-latex-regexps)
  1446. beg end re e m n block off)
  1447. ;; Check the different regular expressions
  1448. (while (setq e (pop re-list))
  1449. (setq m (car e) re (nth 1 e) n (nth 2 e)
  1450. block (if (nth 3 e) "\n\n" ""))
  1451. (setq off (if (member m '("$" "$1")) 1 0))
  1452. (when (and (member m matchers) (not (equal m "begin")))
  1453. (goto-char (point-min))
  1454. (while (re-search-forward re nil t)
  1455. (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
  1456. (add-text-properties beg end '(org-protected t org-latex-math t))))))
  1457. ;; Convert LaTeX to \LaTeX{}
  1458. (goto-char (point-min))
  1459. (let ((case-fold-search nil))
  1460. (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
  1461. (org-if-unprotected
  1462. (replace-match (org-export-latex-protect-string
  1463. (concat (match-string 1) "\\LaTeX{}")) t t))))
  1464. ;; Convert blockquotes
  1465. (goto-char (point-min))
  1466. (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
  1467. (org-replace-match-keep-properties "\\begin{quote}" t t))
  1468. (goto-char (point-min))
  1469. (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
  1470. (org-replace-match-keep-properties "\\end{quote}" t t))
  1471. ;; Convert verse
  1472. (goto-char (point-min))
  1473. (while (search-forward "ORG-VERSE-START" nil t)
  1474. (org-replace-match-keep-properties "\\begin{verse}" t t)
  1475. (beginning-of-line 2)
  1476. (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
  1477. (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
  1478. (goto-char (match-end 1))
  1479. (org-replace-match-keep-properties
  1480. (org-export-latex-protect-string
  1481. (concat "\\hspace*{1cm}" (match-string 2))) t t)
  1482. (beginning-of-line 1))
  1483. (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
  1484. (end-of-line 1)
  1485. (insert "\\\\"))
  1486. (beginning-of-line 2))
  1487. (and (looking-at "[ \t]*ORG-VERSE-END.*")
  1488. (org-replace-match-keep-properties "\\end{verse}" t t)))
  1489. ;; Convert center
  1490. (goto-char (point-min))
  1491. (while (search-forward "ORG-CENTER-START" nil t)
  1492. (org-replace-match-keep-properties "\\begin{center}" t t))
  1493. (goto-char (point-min))
  1494. (while (search-forward "ORG-CENTER-END" nil t)
  1495. (org-replace-match-keep-properties "\\end{center}" t t))
  1496. (run-hooks 'org-export-latex-after-blockquotes-hook)
  1497. ;; Convert horizontal rules
  1498. (goto-char (point-min))
  1499. (while (re-search-forward "^----+.$" nil t)
  1500. (org-if-unprotected
  1501. (replace-match (org-export-latex-protect-string "\\hrule") t t)))
  1502. ;; Protect LaTeX commands like \command[...]{...} or \command{...}
  1503. (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
  1504. "\\[.*\\]"
  1505. "\\)?"
  1506. (org-create-multibrace-regexp "{" "}" 3))))
  1507. (while (re-search-forward re nil t)
  1508. (add-text-properties (match-beginning 0) (match-end 0)
  1509. '(org-protected t))))
  1510. ;; Protect LaTeX entities
  1511. (goto-char (point-min))
  1512. (let (a)
  1513. (while (re-search-forward org-latex-entities-regexp nil t)
  1514. (if (setq a (assoc (match-string 0) org-latex-entities-exceptions))
  1515. (replace-match (org-add-props (nth 1 a) nil 'org-protected t)
  1516. t t)
  1517. (add-text-properties (match-beginning 0) (match-end 0)
  1518. '(org-protected t)))))
  1519. ;; Replace radio links
  1520. (goto-char (point-min))
  1521. (while (re-search-forward
  1522. (concat "<<<?" org-export-latex-all-targets-re
  1523. ">>>?\\((INVISIBLE)\\)?") nil t)
  1524. (org-if-unprotected
  1525. (replace-match
  1526. (org-export-latex-protect-string
  1527. (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
  1528. (match-string 1)))
  1529. (if (match-string 2) "" (match-string 1)))) t t)))
  1530. ;; Delete @<...> constructs
  1531. ;; Thanks to Daniel Clemente for this regexp
  1532. (goto-char (point-min))
  1533. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  1534. (org-if-unprotected
  1535. (replace-match "")))
  1536. ;; When converting to LaTeX, replace footnotes
  1537. ;; FIXME: don't protect footnotes from conversion
  1538. (when (plist-get org-export-latex-options-plist :footnotes)
  1539. (goto-char (point-min))
  1540. (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
  1541. (org-if-unprotected
  1542. (when (save-match-data
  1543. (save-excursion (beginning-of-line)
  1544. (looking-at "[^:|#]")))
  1545. (let ((foot-beg (match-beginning 0))
  1546. (foot-end (match-end 0))
  1547. (foot-prefix (match-string 0))
  1548. footnote footnote-rpl)
  1549. (save-excursion
  1550. (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
  1551. nil t))
  1552. (replace-match "$^{\\1}$")
  1553. (replace-match "")
  1554. (let ((end (save-excursion
  1555. (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
  1556. (match-beginning 0) (point-max)))))
  1557. (setq footnote (concat (org-trim (buffer-substring (point) end))
  1558. " ")) ; prevent last } being part of a link
  1559. (delete-region (point) end))
  1560. (goto-char foot-beg)
  1561. (delete-region foot-beg foot-end)
  1562. (unless (null footnote)
  1563. (setq footnote-rpl (format "\\footnote{%s}" footnote))
  1564. (add-text-properties 0 10 '(org-protected t) footnote-rpl)
  1565. (add-text-properties (1- (length footnote-rpl))
  1566. (length footnote-rpl)
  1567. '(org-protected t) footnote-rpl)
  1568. (insert footnote-rpl)))
  1569. )))))
  1570. ;; Remove footnote section tag for LaTeX
  1571. (goto-char (point-min))
  1572. (while (re-search-forward
  1573. (concat "^" footnote-section-tag-regexp) nil t)
  1574. (org-if-unprotected
  1575. (replace-match "")))))
  1576. ;;; List handling:
  1577. (defun org-export-latex-lists ()
  1578. "Convert plain text lists in current buffer into LaTeX lists."
  1579. (goto-char (point-min))
  1580. (while (re-search-forward org-list-beginning-re nil t)
  1581. (org-if-unprotected
  1582. (beginning-of-line)
  1583. (insert (org-list-to-latex (org-list-parse-list t)
  1584. org-export-latex-list-parameters))
  1585. "\n")))
  1586. (defconst org-latex-entities
  1587. '("\\!"
  1588. "\\'"
  1589. "\\+"
  1590. "\\,"
  1591. "\\-"
  1592. "\\:"
  1593. "\\;"
  1594. "\\<"
  1595. "\\="
  1596. "\\>"
  1597. "\\Huge"
  1598. "\\LARGE"
  1599. "\\Large"
  1600. "\\Styles"
  1601. "\\\\"
  1602. "\\`"
  1603. "\\addcontentsline"
  1604. "\\address"
  1605. "\\addtocontents"
  1606. "\\addtocounter"
  1607. "\\addtolength"
  1608. "\\addvspace"
  1609. "\\alph"
  1610. "\\appendix"
  1611. "\\arabic"
  1612. "\\author"
  1613. "\\begin{array}"
  1614. "\\begin{center}"
  1615. "\\begin{description}"
  1616. "\\begin{enumerate}"
  1617. "\\begin{eqnarray}"
  1618. "\\begin{equation}"
  1619. "\\begin{figure}"
  1620. "\\begin{flushleft}"
  1621. "\\begin{flushright}"
  1622. "\\begin{itemize}"
  1623. "\\begin{list}"
  1624. "\\begin{minipage}"
  1625. "\\begin{picture}"
  1626. "\\begin{quotation}"
  1627. "\\begin{quote}"
  1628. "\\begin{tabbing}"
  1629. "\\begin{table}"
  1630. "\\begin{tabular}"
  1631. "\\begin{thebibliography}"
  1632. "\\begin{theorem}"
  1633. "\\begin{titlepage}"
  1634. "\\begin{verbatim}"
  1635. "\\begin{verse}"
  1636. "\\bf"
  1637. "\\bf"
  1638. "\\bibitem"
  1639. "\\bigskip"
  1640. "\\cdots"
  1641. "\\centering"
  1642. "\\circle"
  1643. "\\cite"
  1644. "\\cleardoublepage"
  1645. "\\clearpage"
  1646. "\\cline"
  1647. "\\closing"
  1648. "\\dashbox"
  1649. "\\date"
  1650. "\\ddots"
  1651. "\\dotfill"
  1652. "\\em"
  1653. "\\fbox"
  1654. "\\flushbottom"
  1655. "\\fnsymbol"
  1656. "\\footnote"
  1657. "\\footnotemark"
  1658. "\\footnotesize"
  1659. "\\footnotetext"
  1660. "\\frac"
  1661. "\\frame"
  1662. "\\framebox"
  1663. "\\hfill"
  1664. "\\hline"
  1665. "\\hrulespace"
  1666. "\\hspace"
  1667. "\\huge"
  1668. "\\hyphenation"
  1669. "\\include"
  1670. "\\includeonly"
  1671. "\\indent"
  1672. "\\input"
  1673. "\\it"
  1674. "\\kill"
  1675. "\\label"
  1676. "\\large"
  1677. "\\ldots"
  1678. "\\line"
  1679. "\\linebreak"
  1680. "\\linethickness"
  1681. "\\listoffigures"
  1682. "\\listoftables"
  1683. "\\location"
  1684. "\\makebox"
  1685. "\\maketitle"
  1686. "\\mark"
  1687. "\\mbox"
  1688. "\\medskip"
  1689. "\\multicolumn"
  1690. "\\multiput"
  1691. ("\\nbsp" "~")
  1692. "\\newcommand"
  1693. "\\newcounter"
  1694. "\\newenvironment"
  1695. "\\newfont"
  1696. "\\newlength"
  1697. "\\newline"
  1698. "\\newpage"
  1699. "\\newsavebox"
  1700. "\\newtheorem"
  1701. "\\nocite"
  1702. "\\nofiles"
  1703. "\\noindent"
  1704. "\\nolinebreak"
  1705. "\\nopagebreak"
  1706. "\\normalsize"
  1707. "\\onecolumn"
  1708. "\\opening"
  1709. "\\oval"
  1710. "\\overbrace"
  1711. "\\overline"
  1712. "\\pagebreak"
  1713. "\\pagenumbering"
  1714. "\\pageref"
  1715. "\\pagestyle"
  1716. "\\par"
  1717. "\\parbox"
  1718. "\\put"
  1719. "\\raggedbottom"
  1720. "\\raggedleft"
  1721. "\\raggedright"
  1722. "\\raisebox"
  1723. "\\ref"
  1724. "\\rm"
  1725. "\\roman"
  1726. "\\rule"
  1727. "\\savebox"
  1728. "\\sc"
  1729. "\\scriptsize"
  1730. "\\setcounter"
  1731. "\\setlength"
  1732. "\\settowidth"
  1733. "\\sf"
  1734. "\\shortstack"
  1735. "\\signature"
  1736. "\\sl"
  1737. "\\small"
  1738. "\\smallskip"
  1739. "\\sqrt"
  1740. "\\tableofcontents"
  1741. "\\telephone"
  1742. "\\thanks"
  1743. "\\thispagestyle"
  1744. "\\tiny"
  1745. "\\title"
  1746. "\\tt"
  1747. "\\twocolumn"
  1748. "\\typein"
  1749. "\\typeout"
  1750. "\\underbrace"
  1751. "\\underline"
  1752. "\\usebox"
  1753. "\\usecounter"
  1754. "\\value"
  1755. "\\vdots"
  1756. "\\vector"
  1757. "\\verb"
  1758. "\\vfill"
  1759. "\\vline"
  1760. "\\vspace")
  1761. "A list of LaTeX commands to be protected when performing conversion.")
  1762. (defvar org-latex-entities-exceptions nil)
  1763. (defconst org-latex-entities-regexp
  1764. (let (names rest)
  1765. (dolist (x org-latex-entities)
  1766. (when (consp x)
  1767. (add-to-list 'org-latex-entities-exceptions x)
  1768. (setq x (car x)))
  1769. (if (string-match "[a-z][A-Z]$" x)
  1770. (push x names)
  1771. (push x rest)))
  1772. (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
  1773. "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
  1774. (provide 'org-export-latex)
  1775. (provide 'org-latex)
  1776. ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
  1777. ;;; org-latex.el ends here