org-latex.el 67 KB

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