org-latex.el 67 KB

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