org-latex.el 67 KB

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