org-latex.el 60 KB

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