org-latex.el 62 KB

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