org-latex.el 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800
  1. ;;; org-latex.el --- LaTeX exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-latex.el
  7. ;; Author: Bastien Guerry <bzg AT gnu DOT org>
  8. ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
  9. ;; Keywords: org, wp, tex
  10. ;; Description: Converts an org-mode buffer into LaTeX
  11. ;; This file is part of GNU Emacs.
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;; Commentary:
  23. ;;
  24. ;; This library implements a LaTeX exporter for org-mode.
  25. ;;
  26. ;; It is part of Org and will be autoloaded
  27. ;;
  28. ;; The interactive functions are similar to those of the HTML exporter:
  29. ;;
  30. ;; M-x `org-export-as-latex'
  31. ;; M-x `org-export-as-pdf'
  32. ;; M-x `org-export-as-pdf-and-open'
  33. ;; M-x `org-export-as-latex-batch'
  34. ;; M-x `org-export-as-latex-to-buffer'
  35. ;; M-x `org-export-region-as-latex'
  36. ;; M-x `org-replace-region-by-latex'
  37. ;;
  38. ;;; Code:
  39. (eval-when-compile
  40. (require 'cl))
  41. (require 'footnote)
  42. (require 'org)
  43. (require 'org-exp)
  44. (require 'org-macs)
  45. (require 'org-beamer)
  46. ;;; Variables:
  47. (defvar org-export-latex-class nil)
  48. (defvar org-export-latex-class-options nil)
  49. (defvar org-export-latex-header nil)
  50. (defvar org-export-latex-append-header nil)
  51. (defvar org-export-latex-options-plist nil)
  52. (defvar org-export-latex-todo-keywords-1 nil)
  53. (defvar org-export-latex-complex-heading-re nil)
  54. (defvar org-export-latex-not-done-keywords nil)
  55. (defvar org-export-latex-done-keywords nil)
  56. (defvar org-export-latex-display-custom-times nil)
  57. (defvar org-export-latex-all-targets-re nil)
  58. (defvar org-export-latex-add-level 0)
  59. (defvar org-export-latex-footmark-seen nil
  60. "List of footnotes markers seen so far by exporter.")
  61. (defvar org-export-latex-sectioning "")
  62. (defvar org-export-latex-sectioning-depth 0)
  63. (defvar org-export-latex-special-keyword-regexp
  64. (concat "\\<\\(" org-scheduled-string "\\|"
  65. org-deadline-string "\\|"
  66. org-closed-string"\\)")
  67. "Regexp matching special time planning keywords plus the time after it.")
  68. (defvar org-re-quote) ; dynamically scoped from org.el
  69. (defvar org-commentsp) ; dynamically scoped from org.el
  70. ;;; User variables:
  71. (defgroup org-export-latex nil
  72. "Options for exporting Org-mode files to LaTeX."
  73. :tag "Org Export LaTeX"
  74. :group 'org-export)
  75. (defcustom org-export-latex-default-class "article"
  76. "The default LaTeX class."
  77. :group 'org-export-latex
  78. :type '(string :tag "LaTeX class"))
  79. (defcustom org-export-latex-classes
  80. '(("article"
  81. "\\documentclass[11pt]{article}"
  82. ("\\section{%s}" . "\\section*{%s}")
  83. ("\\subsection{%s}" . "\\subsection*{%s}")
  84. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  85. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  86. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  87. ("report"
  88. "\\documentclass[11pt]{report}"
  89. ("\\part{%s}" . "\\part*{%s}")
  90. ("\\chapter{%s}" . "\\chapter*{%s}")
  91. ("\\section{%s}" . "\\section*{%s}")
  92. ("\\subsection{%s}" . "\\subsection*{%s}")
  93. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  94. ("book"
  95. "\\documentclass[11pt]{book}"
  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. ("beamer"
  102. "\\documentclass{beamer}"
  103. org-beamer-sectioning
  104. ))
  105. "Alist of LaTeX classes and associated header and structure.
  106. If #+LaTeX_CLASS is set in the buffer, use its value and the
  107. associated information. Here is the structure of each cell:
  108. \(class-name
  109. header-string
  110. (numbered-section . unnumbered-section\)
  111. ...\)
  112. The header string
  113. -----------------
  114. The HEADER-STRING is the header that will be inserted into the LaTeX file.
  115. It should contain the \\documentclass macro, and anything else that is needed
  116. for this setup. To this header, the following commands will be added:
  117. - Calls to \\usepackage for all packages mentioned in the variables
  118. `org-export-latex-default-packages-alist' and
  119. `org-export-latex-packages-alist'. Thus, your header definitions should
  120. avoid to also request these packages.
  121. - Lines specified via \"#+LaTeX_HEADER:\"
  122. If you need more control about the sequence in which the header is built
  123. up, or if you want to exclude one of these building blocks for a particular
  124. class, you can use the following macro-like placeholders.
  125. [DEFAULT-PACKAGES] \\usepackage statements for default packages
  126. [NO-DEFAULT-PACKAGES] do not include any of the default packages
  127. [PACKAGES] \\usepackage statements for packages
  128. [NO-PACKAGES] do not include the packages
  129. [EXTRA] the stuff from #+LaTeX_HEADER
  130. [NO-EXTRA] do not include #+LaTeX_HEADER stuff
  131. [BEAMER-HEADER-EXTRA] the beamer extra headers
  132. So a header like
  133. \\documentclass{article}
  134. [NO-DEFAULT-PACKAGES]
  135. [EXTRA]
  136. \\providecommand{\\alert}[1]{\\textbf{#1}}
  137. [PACKAGES]
  138. will omit the default packages, and will include the #+LaTeX_HEADER lines,
  139. then have a call to \\providecommand, and then place \\usepackage commands
  140. based on the content of `org-export-latex-packages-alist'.
  141. If your header or `org-export-latex-default-packages-alist' inserts
  142. \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
  143. a coding system derived from `buffer-file-coding-system'. See also the
  144. variable `org-export-latex-inputenc-alist' for a way to influence this
  145. mechanism.
  146. The sectioning structure
  147. ------------------------
  148. The sectioning structure of the class is given by the elements following
  149. the header string. For each sectioning level, a number of strings is
  150. specified. A %s formatter is mandatory in each section string and will
  151. be replaced by the title of the section.
  152. Instead of a cons cell (numbered . unnumbered), you can also provide a list
  153. of 2 or 4 elements,
  154. (numbered-open numbered-close)
  155. or
  156. (numbered-open numbered-close unnumbered-open unnumbered-close)
  157. providing opening and closing strings for a LaTeX environment that should
  158. represent the document section. The opening clause should have a %s
  159. to represent the section title.
  160. Instead of a list of sectioning commands, you can also specify a
  161. function name. That function will be called with two parameters,
  162. the (reduced) level of the headline, and the headline text. The function
  163. must return a cons cell with the (possibly modified) headline text, and the
  164. sectioning list in the cdr."
  165. :group 'org-export-latex
  166. :type '(repeat
  167. (list (string :tag "LaTeX class")
  168. (string :tag "LaTeX header")
  169. (repeat :tag "Levels" :inline t
  170. (choice
  171. (cons :tag "Heading"
  172. (string :tag " numbered")
  173. (string :tag "unnumbered"))
  174. (list :tag "Environment"
  175. (string :tag "Opening (numbered)")
  176. (string :tag "Closing (numbered)")
  177. (string :tag "Opening (unnumbered)")
  178. (string :tag "Closing (unnumbered)"))
  179. (function :tag "Hook computing sectioning"))))))
  180. (defcustom org-export-latex-inputenc-alist nil
  181. "Alist of inputenc coding system names, and what should really be used.
  182. For example, adding an entry
  183. (\"utf8\" . \"utf8x\")
  184. will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
  185. are written as utf8 files."
  186. :group 'org-export-latex
  187. :type '(repeat
  188. (cons
  189. (string :tag "Derived from buffer")
  190. (string :tag "Use this instead"))))
  191. (defcustom org-export-latex-emphasis-alist
  192. '(("*" "\\textbf{%s}" nil)
  193. ("/" "\\emph{%s}" nil)
  194. ("_" "\\underline{%s}" nil)
  195. ("+" "\\st{%s}" nil)
  196. ("=" "\\protectedtexttt" t)
  197. ("~" "\\verb" t))
  198. "Alist of LaTeX expressions to convert emphasis fontifiers.
  199. Each element of the list is a list of three elements.
  200. The first element is the character used as a marker for fontification.
  201. The second element is a formatting string to wrap fontified text with.
  202. If it is \"\\verb\", Org will automatically select a delimiter
  203. character that is not in the string. \"\\protectedtexttt\" will use \\texttt
  204. to typeset and try to protect special characters.
  205. The third element decides whether to protect converted text from other
  206. conversions."
  207. :group 'org-export-latex
  208. :type 'alist)
  209. (defcustom org-export-latex-title-command "\\maketitle"
  210. "The command used to insert the title just after \\begin{document}.
  211. If this string contains the formatting specification \"%s\" then
  212. it will be used as a formatting string, passing the title as an
  213. argument."
  214. :group 'org-export-latex
  215. :type 'string)
  216. (defcustom org-export-latex-import-inbuffer-stuff nil
  217. "Non-nil means define TeX macros for Org's inbuffer definitions.
  218. For example \orgTITLE for #+TITLE."
  219. :group 'org-export-latex
  220. :type 'boolean)
  221. (defcustom org-export-latex-date-format
  222. "\\today"
  223. "Format string for \\date{...}."
  224. :group 'org-export-latex
  225. :type 'string)
  226. (defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}"
  227. "Markup for TODO keywords, as a printf format.
  228. This can be a single format for all keywords, a cons cell with separate
  229. formats for not-done and done states, or an association list with setup
  230. for individual keywords. If a keyword shows up for which there is no
  231. markup defined, the first one in the association list will be used."
  232. :group 'org-export-latex
  233. :type '(choice
  234. (string :tag "Default")
  235. (cons :tag "Distinguish undone and done"
  236. (string :tag "Not-DONE states")
  237. (string :tag "DONE states"))
  238. (repeat :tag "Per keyword markup"
  239. (cons
  240. (string :tag "Keyword")
  241. (string :tag "Markup")))))
  242. (defcustom org-export-latex-tag-markup "\\textbf{%s}"
  243. "Markup for tags, as a printf format."
  244. :group 'org-export-latex
  245. :type 'string)
  246. (defcustom org-export-latex-timestamp-markup "\\textit{%s}"
  247. "A printf format string to be applied to time stamps."
  248. :group 'org-export-latex
  249. :type 'string)
  250. (defcustom org-export-latex-timestamp-inactive-markup "\\textit{%s}"
  251. "A printf format string to be applied to inactive time stamps."
  252. :group 'org-export-latex
  253. :type 'string)
  254. (defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}"
  255. "A printf format string to be applied to time stamps."
  256. :group 'org-export-latex
  257. :type 'string)
  258. (defcustom org-export-latex-href-format "\\href{%s}{%s}"
  259. "A printf format string to be applied to href links.
  260. The format must contain either two %s instances or just one.
  261. If it contains two %s instances, the first will be filled with
  262. the link, the second with the link description. If it contains
  263. only one, the %s will be filled with the link."
  264. :group 'org-export-latex
  265. :type 'string)
  266. (defcustom org-export-latex-hyperref-format "\\hyperref[%s]{%s}"
  267. "A printf format string to be applied to hyperref links.
  268. The format must contain one or two %s instances. The first one
  269. will be filled with the link, the second with its description."
  270. :group 'org-export-latex
  271. :type 'string)
  272. (defcustom org-export-latex-footnote-separator "\\textsuperscript{,}\\,"
  273. "Text used to separate footnotes."
  274. :group 'org-export-latex
  275. :type 'string)
  276. (defcustom org-export-latex-quotes
  277. '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
  278. ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
  279. "Alist for quotes to use when converting english double-quotes.
  280. The CAR of each item in this alist is the language code.
  281. The CDR of each item in this alist is a list of three CONS:
  282. - the first CONS defines the opening quote;
  283. - the second CONS defines the closing quote;
  284. - the last CONS defines single quotes.
  285. For each item in a CONS, the first string is a regexp
  286. for allowed characters before/after the quote, the second
  287. string defines the replacement string for this quote."
  288. :group 'org-export-latex
  289. :type '(list
  290. (cons :tag "Opening quote"
  291. (string :tag "Regexp for char before")
  292. (string :tag "Replacement quote "))
  293. (cons :tag "Closing quote"
  294. (string :tag "Regexp for char after ")
  295. (string :tag "Replacement quote "))
  296. (cons :tag "Single quote"
  297. (string :tag "Regexp for char before")
  298. (string :tag "Replacement quote "))))
  299. (defcustom org-export-latex-tables-verbatim nil
  300. "When non-nil, tables are exported verbatim."
  301. :group 'org-export-latex
  302. :type 'boolean)
  303. (defcustom org-export-latex-tables-centered t
  304. "When non-nil, tables are exported in a center environment."
  305. :group 'org-export-latex
  306. :type 'boolean)
  307. (defcustom org-export-latex-table-caption-above t
  308. "When non-nil, the caption is set above the table. When nil,
  309. the caption is set below the table."
  310. :group 'org-export-latex
  311. :type 'boolean)
  312. (defcustom org-export-latex-tables-column-borders nil
  313. "When non-nil, grouping columns can cause outer vertical lines in tables.
  314. When nil, grouping causes only separation lines between groups."
  315. :group 'org-export-latex
  316. :type 'boolean)
  317. (defcustom org-export-latex-low-levels 'itemize
  318. "How to convert sections below the current level of sectioning.
  319. This is specified by the `org-export-headline-levels' option or the
  320. value of \"H:\" in Org's #+OPTION line.
  321. This can be either nil (skip the sections), `description', `itemize',
  322. or `enumerate' (convert the sections as the corresponding list type), or
  323. a string to be used instead of \\section{%s}. In this latter case,
  324. the %s stands here for the inserted headline and is mandatory.
  325. It may also be a list of three string to define a user-defined environment
  326. that should be used. The first string should be the like
  327. \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
  328. to two occurrences of %s for the title and a label, respectively. The third
  329. string should be like \"\\end{itemize\"."
  330. :group 'org-export-latex
  331. :type '(choice (const :tag "Ignore" nil)
  332. (const :tag "Convert as descriptive list" description)
  333. (const :tag "Convert as itemized list" itemize)
  334. (const :tag "Convert as enumerated list" enumerate)
  335. (list :tag "User-defined environment"
  336. :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s")
  337. (string :tag "Start")
  338. (string :tag "End")
  339. (string :tag "item"))
  340. (string :tag "Use a section string" :value "\\subparagraph{%s}")))
  341. (defcustom org-export-latex-list-parameters
  342. '(:cbon "$\\boxtimes$" :cboff "$\\Box$" :cbtrans "$\\boxminus$")
  343. "Parameters for the LaTeX list exporter.
  344. These parameters will be passed on to `org-list-to-latex', which in turn
  345. will pass them (combined with the LaTeX default list parameters) to
  346. `org-list-to-generic'."
  347. :group 'org-export-latex
  348. :type 'plist)
  349. (defcustom org-export-latex-verbatim-wrap
  350. '("\\begin{verbatim}\n" . "\\end{verbatim}")
  351. "Environment to be wrapped around a fixed-width section in LaTeX export.
  352. This is a cons with two strings, to be added before and after the
  353. fixed-with text.
  354. Defaults to \\begin{verbatim} and \\end{verbatim}."
  355. :group 'org-export-translation
  356. :group 'org-export-latex
  357. :type '(cons (string :tag "Open")
  358. (string :tag "Close")))
  359. (defcustom org-export-latex-listings nil
  360. "Non-nil means export source code using the listings package.
  361. This package will fontify source code, possibly even with color.
  362. If you want to use this, you also need to make LaTeX use the
  363. listings package, and if you want to have color, the color
  364. package. Just add these to `org-export-latex-packages-alist',
  365. for example using customize, or with something like
  366. (require 'org-latex)
  367. (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
  368. (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))
  369. Alternatively,
  370. (setq org-export-latex-listings 'minted)
  371. causes source code to be exported using the minted package as
  372. opposed to listings. If you want to use minted, you need to add
  373. the minted package to `org-export-latex-packages-alist', for
  374. example using customize, or with
  375. (require 'org-latex)
  376. (add-to-list 'org-export-latex-packages-alist '(\"\" \"minted\"))
  377. In addition, it is necessary to install
  378. pygments (http://pygments.org), and to configure the variable
  379. `org-latex-to-pdf-process' so that the -shell-escape option is
  380. passed to pdflatex.
  381. "
  382. :group 'org-export-latex
  383. :type 'boolean)
  384. (defcustom org-export-latex-listings-langs
  385. '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
  386. (c "C") (cc "C++")
  387. (fortran "fortran")
  388. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  389. (html "HTML") (xml "XML")
  390. (tex "TeX") (latex "TeX")
  391. (shell-script "bash")
  392. (gnuplot "Gnuplot")
  393. (ocaml "Caml") (caml "Caml")
  394. (sql "SQL") (sqlite "sql"))
  395. "Alist mapping languages to their listing language counterpart.
  396. The key is a symbol, the major mode symbol without the \"-mode\".
  397. The value is the string that should be inserted as the language parameter
  398. for the listings package. If the mode name and the listings name are
  399. the same, the language does not need an entry in this list - but it does not
  400. hurt if it is present."
  401. :group 'org-export-latex
  402. :type '(repeat
  403. (list
  404. (symbol :tag "Major mode ")
  405. (string :tag "Listings language"))))
  406. (defcustom org-export-latex-listings-w-names t
  407. "Non-nil means export names of named code blocks.
  408. Code blocks exported with the listings package (controlled by the
  409. `org-export-latex-listings' variable) can be named in the style
  410. of noweb."
  411. :group 'org-export-latex
  412. :type 'boolean)
  413. (defcustom org-export-latex-minted-langs
  414. '((emacs-lisp "common-lisp")
  415. (cc "c++")
  416. (cperl "perl")
  417. (shell-script "bash")
  418. (caml "ocaml"))
  419. "Alist mapping languages to their minted language counterpart.
  420. The key is a symbol, the major mode symbol without the \"-mode\".
  421. The value is the string that should be inserted as the language parameter
  422. for the minted package. If the mode name and the listings name are
  423. the same, the language does not need an entry in this list - but it does not
  424. hurt if it is present.
  425. Note that minted uses all lower case for language identifiers,
  426. and that the full list of language identifiers can be obtained
  427. with:
  428. pygmentize -L lexers
  429. "
  430. :group 'org-export-latex
  431. :type '(repeat
  432. (list
  433. (symbol :tag "Major mode ")
  434. (string :tag "Listings language"))))
  435. (defcustom org-export-latex-listings-options nil
  436. "Association list of options for the latex listings package.
  437. These options are supplied as a comma-separated list to the
  438. \\lstset command. Each element of the association list should be
  439. a list containing two strings: the name of the option, and the
  440. value. For example,
  441. (setq org-export-latex-listings-options
  442. '((\"basicstyle\" \"\\small\")
  443. (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
  444. will typeset the code in a small size font with underlined, bold
  445. black keywords.
  446. Note that the same options will be applied to blocks of all
  447. languages."
  448. :group 'org-export-latex
  449. :type '(repeat
  450. (list
  451. (string :tag "Listings option name ")
  452. (string :tag "Listings option value"))))
  453. (defcustom org-export-latex-minted-options nil
  454. "Association list of options for the latex minted package.
  455. These options are supplied within square brackets in
  456. \\begin{minted} environments. Each element of the alist should be
  457. a list containing two strings: the name of the option, and the
  458. value. For example,
  459. (setq org-export-latex-minted-options
  460. '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
  461. will result in src blocks being exported with
  462. \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
  463. as the start of the minted environment. Note that the same
  464. options will be applied to blocks of all languages."
  465. :group 'org-export-latex
  466. :type '(repeat
  467. (list
  468. (string :tag "Minted option name ")
  469. (string :tag "Minted option value"))))
  470. (defvar org-export-latex-custom-lang-environments nil
  471. "Association list mapping languages to language-specific latex
  472. environments used during export of src blocks by the listings
  473. and minted latex packages. For example,
  474. (setq org-export-latex-custom-lang-environments
  475. '((python \"pythoncode\")))
  476. would have the effect that if org encounters begin_src python
  477. during latex export it will output
  478. \\begin{pythoncode}
  479. <src block body>
  480. \\end{pythoncode}")
  481. (defcustom org-export-latex-remove-from-headlines
  482. '(:todo nil :priority nil :tags nil)
  483. "A plist of keywords to remove from headlines. OBSOLETE.
  484. Non-nil means remove this keyword type from the headline.
  485. Don't remove the keys, just change their values.
  486. Obsolete, this variable is no longer used. Use the separate
  487. variables `org-export-with-todo-keywords', `org-export-with-priority',
  488. and `org-export-with-tags' instead."
  489. :type 'plist
  490. :group 'org-export-latex)
  491. (defcustom org-export-latex-image-default-option "width=.9\\linewidth"
  492. "Default option for images."
  493. :group 'org-export-latex
  494. :type 'string)
  495. (defcustom org-latex-default-figure-position "htb"
  496. "Default position for latex figures."
  497. :group 'org-export-latex
  498. :type 'string)
  499. (defcustom org-export-latex-tabular-environment "tabular"
  500. "Default environment used to build tables."
  501. :group 'org-export-latex
  502. :type 'string)
  503. (defcustom org-export-latex-inline-image-extensions
  504. '("pdf" "jpeg" "jpg" "png" "ps" "eps")
  505. "Extensions of image files that can be inlined into LaTeX.
  506. Note that the image extension *actually* allowed depend on the way the
  507. LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
  508. are OK. When processing through dvi to Postscript, only ps and eps are
  509. allowed. The default we use here encompasses both."
  510. :group 'org-export-latex
  511. :type '(repeat (string :tag "Extension")))
  512. (defcustom org-export-latex-coding-system nil
  513. "Coding system for the exported LaTeX file."
  514. :group 'org-export-latex
  515. :type 'coding-system)
  516. (defgroup org-export-pdf nil
  517. "Options for exporting Org-mode files to PDF, via LaTeX."
  518. :tag "Org Export PDF"
  519. :group 'org-export-latex
  520. :group 'org-export)
  521. (defcustom org-latex-to-pdf-process
  522. '("pdflatex -interaction nonstopmode -output-directory %o %f"
  523. "pdflatex -interaction nonstopmode -output-directory %o %f"
  524. "pdflatex -interaction nonstopmode -output-directory %o %f")
  525. "Commands to process a LaTeX file to a PDF file.
  526. This is a list of strings, each of them will be given to the shell
  527. as a command. %f in the command will be replaced by the full file name, %b
  528. by the file base name (i.e. without extension) and %o by the base directory
  529. of the file.
  530. The reason why this is a list is that it usually takes several runs of
  531. `pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever
  532. mechanism to detect which of these commands have to be run to get to a stable
  533. result, and it also does not do any error checking.
  534. By default, Org uses 3 runs of `pdflatex' to do the processing. If you
  535. have texi2dvi on your system and if that does not cause the infamous
  536. egrep/locale bug:
  537. http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
  538. then `texi2dvi' is the superior choice. Org does offer it as one
  539. of the customize options.
  540. Alternatively, this may be a Lisp function that does the processing, so you
  541. could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
  542. This function should accept the file name as its single argument."
  543. :group 'org-export-pdf
  544. :type '(choice
  545. (repeat :tag "Shell command sequence"
  546. (string :tag "Shell command"))
  547. (const :tag "2 runs of pdflatex"
  548. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  549. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  550. (const :tag "3 runs of pdflatex"
  551. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  552. "pdflatex -interaction nonstopmode -output-directory %o %f"
  553. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  554. (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
  555. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  556. "bibtex %b"
  557. "pdflatex -interaction nonstopmode -output-directory %o %f"
  558. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  559. (const :tag "texi2dvi"
  560. ("texi2dvi -p -b -c -V %f"))
  561. (const :tag "rubber"
  562. ("rubber -d --into %o %f"))
  563. (function)))
  564. (defcustom org-export-pdf-logfiles
  565. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  566. "The list of file extensions to consider as LaTeX logfiles."
  567. :group 'org-export-pdf
  568. :type '(repeat (string :tag "Extension")))
  569. (defcustom org-export-pdf-remove-logfiles t
  570. "Non-nil means remove the logfiles produced by PDF production.
  571. These are the .aux, .log, .out, and .toc files."
  572. :group 'org-export-pdf
  573. :type 'boolean)
  574. ;;; Hooks
  575. (defvar org-export-latex-after-initial-vars-hook nil
  576. "Hook run before LaTeX export.
  577. The exact moment is after the initial variables like org-export-latex-class
  578. have been determined from the environment.")
  579. (defvar org-export-latex-after-blockquotes-hook nil
  580. "Hook run during LaTeX export, after blockquote, verse, center are done.")
  581. (defvar org-export-latex-final-hook nil
  582. "Hook run in the finalized LaTeX buffer.")
  583. (defvar org-export-latex-after-save-hook nil
  584. "Hook run in the finalized LaTeX buffer, after it has been saved.")
  585. ;;; Autoload functions:
  586. ;;;###autoload
  587. (defun org-export-as-latex-batch ()
  588. "Call `org-export-as-latex', may be used in batch processing.
  589. For example:
  590. emacs --batch
  591. --load=$HOME/lib/emacs/org.el
  592. --eval \"(setq org-export-headline-levels 2)\"
  593. --visit=MyFile --funcall org-export-as-latex-batch"
  594. (org-export-as-latex org-export-headline-levels 'hidden))
  595. ;;;###autoload
  596. (defun org-export-as-latex-to-buffer (arg)
  597. "Call `org-export-as-latex` with output to a temporary buffer.
  598. No file is created. The prefix ARG is passed through to `org-export-as-latex'."
  599. (interactive "P")
  600. (org-export-as-latex arg nil nil "*Org LaTeX Export*")
  601. (when org-export-show-temporary-export-buffer
  602. (switch-to-buffer-other-window "*Org LaTeX Export*")))
  603. ;;;###autoload
  604. (defun org-replace-region-by-latex (beg end)
  605. "Replace the region from BEG to END with its LaTeX export.
  606. It assumes the region has `org-mode' syntax, and then convert it to
  607. LaTeX. This can be used in any buffer. For example, you could
  608. write an itemized list in `org-mode' syntax in an LaTeX buffer and
  609. then use this command to convert it."
  610. (interactive "r")
  611. (let (reg latex buf)
  612. (save-window-excursion
  613. (if (eq major-mode 'org-mode)
  614. (setq latex (org-export-region-as-latex
  615. beg end t 'string))
  616. (setq reg (buffer-substring beg end)
  617. buf (get-buffer-create "*Org tmp*"))
  618. (with-current-buffer buf
  619. (erase-buffer)
  620. (insert reg)
  621. (org-mode)
  622. (setq latex (org-export-region-as-latex
  623. (point-min) (point-max) t 'string)))
  624. (kill-buffer buf)))
  625. (delete-region beg end)
  626. (insert latex)))
  627. ;;;###autoload
  628. (defun org-export-region-as-latex (beg end &optional body-only buffer)
  629. "Convert region from BEG to END in `org-mode' buffer to LaTeX.
  630. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  631. contents, and only produce the region of converted text, useful for
  632. cut-and-paste operations.
  633. If BUFFER is a buffer or a string, use/create that buffer as a target
  634. of the converted LaTeX. If BUFFER is the symbol `string', return the
  635. produced LaTeX as a string and leave no buffer behind. For example,
  636. a Lisp program could call this function in the following way:
  637. (setq latex (org-export-region-as-latex beg end t 'string))
  638. When called interactively, the output buffer is selected, and shown
  639. in a window. A non-interactive call will only return the buffer."
  640. (interactive "r\nP")
  641. (when (org-called-interactively-p 'any)
  642. (setq buffer "*Org LaTeX Export*"))
  643. (let ((transient-mark-mode t) (zmacs-regions t)
  644. ext-plist rtn)
  645. (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
  646. (goto-char end)
  647. (set-mark (point)) ;; to activate the region
  648. (goto-char beg)
  649. (setq rtn (org-export-as-latex
  650. nil nil ext-plist
  651. buffer body-only))
  652. (if (fboundp 'deactivate-mark) (deactivate-mark))
  653. (if (and (org-called-interactively-p 'any) (bufferp rtn))
  654. (switch-to-buffer-other-window rtn)
  655. rtn)))
  656. ;;;###autoload
  657. (defun org-export-as-latex (arg &optional hidden ext-plist
  658. to-buffer body-only pub-dir)
  659. "Export current buffer to a LaTeX file.
  660. If there is an active region, export only the region. The prefix
  661. ARG specifies how many levels of the outline should become
  662. headlines. The default is 3. Lower levels will be exported
  663. depending on `org-export-latex-low-levels'. The default is to
  664. convert them as description lists.
  665. HIDDEN is obsolete and does nothing.
  666. EXT-PLIST is a property list with
  667. external parameters overriding org-mode's default settings, but
  668. still inferior to file-local settings. When TO-BUFFER is
  669. non-nil, create a buffer with that name and export to that
  670. buffer. If TO-BUFFER is the symbol `string', don't leave any
  671. buffer behind but just return the resulting LaTeX as a string.
  672. When BODY-ONLY is set, don't produce the file header and footer,
  673. simply return the content of \\begin{document}...\\end{document},
  674. without even the \\begin{document} and \\end{document} commands.
  675. when PUB-DIR is set, use this as the publishing directory."
  676. (interactive "P")
  677. (when (and (not body-only) arg (listp arg)) (setq body-only t))
  678. (run-hooks 'org-export-first-hook)
  679. ;; Make sure we have a file name when we need it.
  680. (when (and (not (or to-buffer body-only))
  681. (not buffer-file-name))
  682. (if (buffer-base-buffer)
  683. (org-set-local 'buffer-file-name
  684. (with-current-buffer (buffer-base-buffer)
  685. buffer-file-name))
  686. (error "Need a file name to be able to export")))
  687. (message "Exporting to LaTeX...")
  688. (org-unmodified
  689. (let ((inhibit-read-only t))
  690. (remove-text-properties (point-min) (point-max)
  691. '(:org-license-to-kill nil))))
  692. (org-update-radio-target-regexp)
  693. (org-export-latex-set-initial-vars ext-plist arg)
  694. (setq org-export-opt-plist org-export-latex-options-plist
  695. org-export-footnotes-data (org-footnote-all-labels 'with-defs)
  696. org-export-footnotes-seen nil
  697. org-export-latex-footmark-seen nil)
  698. (org-install-letbind)
  699. (run-hooks 'org-export-latex-after-initial-vars-hook)
  700. (let* ((wcf (current-window-configuration))
  701. (opt-plist
  702. (org-export-process-option-filters org-export-latex-options-plist))
  703. (region-p (org-region-active-p))
  704. (rbeg (and region-p (region-beginning)))
  705. (rend (and region-p (region-end)))
  706. (subtree-p
  707. (if (plist-get opt-plist :ignore-subtree-p)
  708. nil
  709. (when region-p
  710. (save-excursion
  711. (goto-char rbeg)
  712. (and (org-at-heading-p)
  713. (>= (org-end-of-subtree t t) rend))))))
  714. (opt-plist (setq org-export-opt-plist
  715. (if subtree-p
  716. (org-export-add-subtree-options opt-plist rbeg)
  717. opt-plist)))
  718. ;; Make sure the variable contains the updated values.
  719. (org-export-latex-options-plist (setq org-export-opt-plist opt-plist))
  720. ;; The following two are dynamically scoped into other
  721. ;; routines below.
  722. (org-current-export-dir
  723. (or pub-dir (org-export-directory :html opt-plist)))
  724. (org-current-export-file buffer-file-name)
  725. (title (or (and subtree-p (org-export-get-title-from-subtree))
  726. (plist-get opt-plist :title)
  727. (and (not
  728. (plist-get opt-plist :skip-before-1st-heading))
  729. (org-export-grab-title-from-buffer))
  730. (and buffer-file-name
  731. (file-name-sans-extension
  732. (file-name-nondirectory buffer-file-name)))
  733. "No Title"))
  734. (filename
  735. (and (not to-buffer)
  736. (concat
  737. (file-name-as-directory
  738. (or pub-dir
  739. (org-export-directory :LaTeX ext-plist)))
  740. (file-name-sans-extension
  741. (or (and subtree-p
  742. (org-entry-get rbeg "EXPORT_FILE_NAME" t))
  743. (file-name-nondirectory ;sans-extension
  744. (or buffer-file-name
  745. (error "Don't know which export file to use")))))
  746. ".tex")))
  747. (filename
  748. (and filename
  749. (if (equal (file-truename filename)
  750. (file-truename (or buffer-file-name "dummy.org")))
  751. (concat filename ".tex")
  752. filename)))
  753. (auto-insert nil); Avoid any auto-insert stuff for the new file
  754. (buffer (if to-buffer
  755. (cond
  756. ((eq to-buffer 'string) (get-buffer-create
  757. "*Org LaTeX Export*"))
  758. (t (get-buffer-create to-buffer)))
  759. (find-file-noselect filename)))
  760. (odd org-odd-levels-only)
  761. (header (org-export-latex-make-header title opt-plist))
  762. (skip (cond (subtree-p nil)
  763. (region-p nil)
  764. (t (plist-get opt-plist :skip-before-1st-heading))))
  765. (text (plist-get opt-plist :text))
  766. (org-export-preprocess-hook
  767. (cons
  768. `(lambda () (org-set-local 'org-complex-heading-regexp
  769. ,org-export-latex-complex-heading-re))
  770. org-export-preprocess-hook))
  771. (first-lines (if skip "" (org-export-latex-first-lines
  772. opt-plist
  773. (if subtree-p
  774. (save-excursion
  775. (goto-char rbeg)
  776. (point-at-bol 2))
  777. rbeg)
  778. (if region-p rend))))
  779. (coding-system (and (boundp 'buffer-file-coding-system)
  780. buffer-file-coding-system))
  781. (coding-system-for-write (or org-export-latex-coding-system
  782. coding-system))
  783. (save-buffer-coding-system (or org-export-latex-coding-system
  784. coding-system))
  785. (region (buffer-substring
  786. (if region-p (region-beginning) (point-min))
  787. (if region-p (region-end) (point-max))))
  788. (text
  789. (and text (string-match "\\S-" text)
  790. (org-export-preprocess-string
  791. text
  792. :emph-multiline t
  793. :for-backend 'latex
  794. :comments nil
  795. :tags (plist-get opt-plist :tags)
  796. :priority (plist-get opt-plist :priority)
  797. :footnotes (plist-get opt-plist :footnotes)
  798. :drawers (plist-get opt-plist :drawers)
  799. :timestamps (plist-get opt-plist :timestamps)
  800. :todo-keywords (plist-get opt-plist :todo-keywords)
  801. :tasks (plist-get opt-plist :tasks)
  802. :add-text nil
  803. :skip-before-1st-heading skip
  804. :select-tags nil
  805. :exclude-tags nil
  806. :LaTeX-fragments nil)))
  807. (string-for-export
  808. (org-export-preprocess-string
  809. region
  810. :emph-multiline t
  811. :for-backend 'latex
  812. :comments nil
  813. :tags (plist-get opt-plist :tags)
  814. :priority (plist-get opt-plist :priority)
  815. :footnotes (plist-get opt-plist :footnotes)
  816. :drawers (plist-get opt-plist :drawers)
  817. :timestamps (plist-get opt-plist :timestamps)
  818. :todo-keywords (plist-get opt-plist :todo-keywords)
  819. :tasks (plist-get opt-plist :tasks)
  820. :add-text (if (eq to-buffer 'string) nil text)
  821. :skip-before-1st-heading skip
  822. :select-tags (plist-get opt-plist :select-tags)
  823. :exclude-tags (plist-get opt-plist :exclude-tags)
  824. :LaTeX-fragments nil)))
  825. ;; Avoid the Query for TeX master from AUCTeX
  826. (if (boundp 'TeX-master) (setq TeX-master t))
  827. (set-buffer buffer)
  828. (erase-buffer)
  829. (org-install-letbind)
  830. (and (fboundp 'set-buffer-file-coding-system)
  831. (set-buffer-file-coding-system coding-system-for-write))
  832. ;; insert the header and initial document commands
  833. (unless (or (eq to-buffer 'string) body-only)
  834. (insert header))
  835. ;; insert text found in #+TEXT
  836. (when (and text (not (eq to-buffer 'string)))
  837. (insert (org-export-latex-content
  838. text '(lists tables fixed-width keywords))
  839. "\n\n"))
  840. ;; insert lines before the first headline
  841. (unless (or skip (string-match "^\\*" first-lines))
  842. (insert first-lines))
  843. ;; export the content of headlines
  844. (org-export-latex-global
  845. (with-temp-buffer
  846. (insert string-for-export)
  847. (goto-char (point-min))
  848. (when (re-search-forward "^\\(\\*+\\) " nil t)
  849. (let* ((asters (length (match-string 1)))
  850. (level (if odd (- asters 2) (- asters 1))))
  851. (setq org-export-latex-add-level
  852. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  853. (org-export-latex-parse-global level odd)))))
  854. ;; finalization
  855. (unless body-only (insert "\n\\end{document}"))
  856. ;; Attach description terms to the \item macro
  857. (goto-char (point-min))
  858. (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
  859. (delete-region (match-beginning 1) (match-end 1)))
  860. ;; Relocate the table of contents
  861. (goto-char (point-min))
  862. (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
  863. (goto-char (point-min))
  864. (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
  865. (replace-match ""))
  866. (goto-char (point-min))
  867. (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
  868. (replace-match "\\tableofcontents" t t)))
  869. ;; Cleanup forced line ends in items where they are not needed
  870. (goto-char (point-min))
  871. (while (re-search-forward
  872. "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
  873. nil t)
  874. (delete-region (match-beginning 1) (match-end 1)))
  875. (goto-char (point-min))
  876. (while (re-search-forward
  877. "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
  878. nil t)
  879. (if (looking-at "[\n \t]+")
  880. (replace-match "\n")))
  881. (run-hooks 'org-export-latex-final-hook)
  882. (if to-buffer
  883. (unless (eq major-mode 'latex-mode) (latex-mode))
  884. (save-buffer))
  885. (org-export-latex-fix-inputenc)
  886. (run-hooks 'org-export-latex-after-save-hook)
  887. (goto-char (point-min))
  888. (or (org-export-push-to-kill-ring "LaTeX")
  889. (message "Exporting to LaTeX...done"))
  890. (prog1
  891. (if (eq to-buffer 'string)
  892. (prog1 (buffer-substring (point-min) (point-max))
  893. (kill-buffer (current-buffer)))
  894. (current-buffer))
  895. (set-window-configuration wcf))))
  896. ;;;###autoload
  897. (defun org-export-as-pdf (arg &optional hidden ext-plist
  898. to-buffer body-only pub-dir)
  899. "Export as LaTeX, then process through to PDF."
  900. (interactive "P")
  901. (message "Exporting to PDF...")
  902. (let* ((wconfig (current-window-configuration))
  903. (lbuf (org-export-as-latex arg hidden ext-plist
  904. to-buffer body-only pub-dir))
  905. (file (buffer-file-name lbuf))
  906. (base (file-name-sans-extension (buffer-file-name lbuf)))
  907. (pdffile (concat base ".pdf"))
  908. (cmds (if (eq org-export-latex-listings 'minted)
  909. ;; automatically add -shell-escape when needed
  910. (mapcar (lambda (cmd)
  911. (replace-regexp-in-string
  912. "pdflatex " "pdflatex -shell-escape " cmd))
  913. org-latex-to-pdf-process)
  914. org-latex-to-pdf-process))
  915. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  916. (bibtex-p (with-current-buffer lbuf
  917. (save-excursion
  918. (goto-char (point-min))
  919. (re-search-forward "\\\\bibliography{" nil t))))
  920. cmd output-dir errors)
  921. (with-current-buffer outbuf (erase-buffer))
  922. (message (concat "Processing LaTeX file " file "..."))
  923. (setq output-dir (file-name-directory file))
  924. (with-current-buffer lbuf
  925. (save-excursion
  926. (if (and cmds (symbolp cmds))
  927. (funcall cmds (shell-quote-argument file))
  928. (while cmds
  929. (setq cmd (pop cmds))
  930. (while (string-match "%b" cmd)
  931. (setq cmd (replace-match
  932. (save-match-data
  933. (shell-quote-argument base))
  934. t t cmd)))
  935. (while (string-match "%f" cmd)
  936. (setq cmd (replace-match
  937. (save-match-data
  938. (shell-quote-argument file))
  939. t t cmd)))
  940. (while (string-match "%o" cmd)
  941. (setq cmd (replace-match
  942. (save-match-data
  943. (shell-quote-argument output-dir))
  944. t t cmd)))
  945. (shell-command cmd outbuf)))))
  946. (message (concat "Processing LaTeX file " file "...done"))
  947. (setq errors (org-export-latex-get-error outbuf))
  948. (if (not (file-exists-p pdffile))
  949. (error (concat "PDF file " pdffile " was not produced"
  950. (if errors (concat ":" errors "") "")))
  951. (set-window-configuration wconfig)
  952. (when org-export-pdf-remove-logfiles
  953. (dolist (ext org-export-pdf-logfiles)
  954. (setq file (concat base "." ext))
  955. (and (file-exists-p file) (delete-file file))))
  956. (message (concat
  957. "Exporting to PDF...done"
  958. (if errors
  959. (concat ", with some errors:" errors)
  960. "")))
  961. pdffile)))
  962. (defun org-export-latex-get-error (buf)
  963. "Collect the kinds of errors that remain in pdflatex processing."
  964. (with-current-buffer buf
  965. (save-excursion
  966. (goto-char (point-max))
  967. (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
  968. ;; OK, we are at the location of the final run
  969. (let ((pos (point)) (errors "") (case-fold-search t))
  970. (if (re-search-forward "Reference.*?undefined" nil t)
  971. (setq errors (concat errors " [undefined reference]")))
  972. (goto-char pos)
  973. (if (re-search-forward "Citation.*?undefined" nil t)
  974. (setq errors (concat errors " [undefined citation]")))
  975. (goto-char pos)
  976. (if (re-search-forward "Undefined control sequence" nil t)
  977. (setq errors (concat errors " [undefined control sequence]")))
  978. (and (org-string-nw-p errors) errors))))))
  979. ;;;###autoload
  980. (defun org-export-as-pdf-and-open (arg)
  981. "Export as LaTeX, then process through to PDF, and open."
  982. (interactive "P")
  983. (let ((pdffile (org-export-as-pdf arg)))
  984. (if pdffile
  985. (progn
  986. (org-open-file pdffile)
  987. (when org-export-kill-product-buffer-when-displayed
  988. (kill-buffer (find-buffer-visiting
  989. (concat (file-name-sans-extension (buffer-file-name))
  990. ".tex")))))
  991. (error "PDF file was not produced"))))
  992. ;;; Parsing functions:
  993. (defun org-export-latex-parse-global (level odd)
  994. "Parse the current buffer recursively, starting at LEVEL.
  995. If ODD is non-nil, assume the buffer only contains odd sections.
  996. Return a list reflecting the document structure."
  997. (save-excursion
  998. (goto-char (point-min))
  999. (let* ((cnt 0) output
  1000. (depth org-export-latex-sectioning-depth))
  1001. (while (org-re-search-forward-unprotected
  1002. (concat "^\\(\\(?:\\*\\)\\{"
  1003. (number-to-string (+ (if odd 2 1) level))
  1004. "\\}\\) \\(.*\\)$")
  1005. ;; make sure that there is no upper heading
  1006. (when (> level 0)
  1007. (save-excursion
  1008. (save-match-data
  1009. (org-re-search-forward-unprotected
  1010. (concat "^\\(\\(?:\\*\\)\\{"
  1011. (number-to-string level)
  1012. "\\}\\) \\(.*\\)$") nil t)))) t)
  1013. (setq cnt (1+ cnt))
  1014. (let* ((pos (match-beginning 0))
  1015. (heading (match-string 2))
  1016. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  1017. (save-excursion
  1018. (narrow-to-region
  1019. (point)
  1020. (save-match-data
  1021. (if (org-re-search-forward-unprotected
  1022. (concat "^\\(\\(?:\\*\\)\\{"
  1023. (number-to-string (+ (if odd 2 1) level))
  1024. "\\}\\) \\(.*\\)$") nil t)
  1025. (match-beginning 0)
  1026. (point-max))))
  1027. (goto-char (point-min))
  1028. (setq output
  1029. (append output
  1030. (list
  1031. (list
  1032. `(pos . ,pos)
  1033. `(level . ,nlevel)
  1034. `(occur . ,cnt)
  1035. `(heading . ,heading)
  1036. `(content . ,(org-export-latex-parse-content))
  1037. `(subcontent . ,(org-export-latex-parse-subcontent
  1038. level odd)))))))
  1039. (widen)))
  1040. (list output))))
  1041. (defun org-export-latex-parse-content ()
  1042. "Extract the content of a section."
  1043. (let ((beg (point))
  1044. (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t)
  1045. (progn (beginning-of-line) (point))
  1046. (point-max))))
  1047. (buffer-substring beg end)))
  1048. (defun org-export-latex-parse-subcontent (level odd)
  1049. "Extract the subcontent of a section at LEVEL.
  1050. If ODD Is non-nil, assume subcontent only contains odd sections."
  1051. (if (not (org-re-search-forward-unprotected
  1052. (concat "^\\(\\(?:\\*\\)\\{"
  1053. (number-to-string (+ (if odd 4 2) level))
  1054. "\\}\\) \\(.*\\)$")
  1055. nil t))
  1056. nil ; subcontent is nil
  1057. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  1058. ;;; Rendering functions:
  1059. (defun org-export-latex-global (content)
  1060. "Export CONTENT to LaTeX.
  1061. CONTENT is an element of the list produced by
  1062. `org-export-latex-parse-global'."
  1063. (if (eq (car content) 'subcontent)
  1064. (mapc 'org-export-latex-sub (cdr content))
  1065. (org-export-latex-sub (car content))))
  1066. (defun org-export-latex-sub (subcontent)
  1067. "Export the list SUBCONTENT to LaTeX.
  1068. SUBCONTENT is an alist containing information about the headline
  1069. and its content."
  1070. (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
  1071. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  1072. (defun org-export-latex-subcontent (subcontent num)
  1073. "Export each cell of SUBCONTENT to LaTeX.
  1074. If NUM is non-nil export numbered sections, otherwise use unnumbered
  1075. sections. If NUM is an integer, export the highest NUM levels as
  1076. numbered sections and lower levels as unnumbered sections."
  1077. (let* ((heading (cdr (assoc 'heading subcontent)))
  1078. (level (- (cdr (assoc 'level subcontent))
  1079. org-export-latex-add-level))
  1080. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  1081. (content (cdr (assoc 'content subcontent)))
  1082. (subcontent (cadr (assoc 'subcontent subcontent)))
  1083. (label (org-get-text-property-any 0 'target heading))
  1084. (label-list (cons label (cdr (assoc label
  1085. org-export-target-aliases))))
  1086. (sectioning org-export-latex-sectioning)
  1087. (depth org-export-latex-sectioning-depth)
  1088. main-heading sub-heading)
  1089. (when (symbolp (car sectioning))
  1090. (setq sectioning (funcall (car sectioning) level heading))
  1091. (when sectioning
  1092. (setq heading (car sectioning)
  1093. sectioning (cdr sectioning)
  1094. ;; target property migh have changed...
  1095. label (org-get-text-property-any 0 'target heading)
  1096. label-list (cons label (cdr (assoc label
  1097. org-export-target-aliases)))))
  1098. (if sectioning (setq sectioning (make-list 10 sectioning)))
  1099. (setq depth (if sectioning 10000 0)))
  1100. (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading)
  1101. (setq main-heading (substring heading 0 (match-beginning 0))
  1102. sub-heading (substring heading (match-end 0))))
  1103. (setq heading (org-export-latex-fontify-headline heading)
  1104. sub-heading (and sub-heading
  1105. (org-export-latex-fontify-headline sub-heading))
  1106. main-heading (and main-heading
  1107. (org-export-latex-fontify-headline main-heading)))
  1108. (cond
  1109. ;; Normal conversion
  1110. ((<= level depth)
  1111. (let* ((sec (nth (1- level) sectioning))
  1112. (num (if (integerp num)
  1113. (>= num level)
  1114. num))
  1115. start end)
  1116. (if (consp (cdr sec))
  1117. (setq start (nth (if num 0 2) sec)
  1118. end (nth (if num 1 3) sec))
  1119. (setq start (if num (car sec) (cdr sec))))
  1120. (insert (format start (if main-heading main-heading heading)
  1121. (or sub-heading "")))
  1122. (insert "\n")
  1123. (when label
  1124. (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
  1125. label-list "\n") "\n"))
  1126. (insert (org-export-latex-content content))
  1127. (cond ((stringp subcontent) (insert subcontent))
  1128. ((listp subcontent)
  1129. (while (org-looking-back "\n\n") (backward-delete-char 1))
  1130. (org-export-latex-sub subcontent)))
  1131. (when (and end (string-match "[^ \t]" end))
  1132. (let ((hook (org-get-text-property-any 0 'org-insert-hook end)))
  1133. (and (functionp hook) (funcall hook)))
  1134. (insert end "\n"))))
  1135. ;; At a level under the hl option: we can drop this subsection
  1136. ((> level depth)
  1137. (cond ((eq org-export-latex-low-levels 'description)
  1138. (if (string-match "% ends low level$"
  1139. (buffer-substring (point-at-bol 0) (point)))
  1140. (delete-region (point-at-bol 0) (point))
  1141. (insert "\\begin{description}\n"))
  1142. (insert (format "\n\\item[%s]%s~\n"
  1143. heading
  1144. (if label (format "\\label{%s}" label) "")))
  1145. (insert (org-export-latex-content content))
  1146. (cond ((stringp subcontent) (insert subcontent))
  1147. ((listp subcontent) (org-export-latex-sub subcontent)))
  1148. (insert "\\end{description} % ends low level\n"))
  1149. ((memq org-export-latex-low-levels '(itemize enumerate))
  1150. (if (string-match "% ends low level$"
  1151. (buffer-substring (point-at-bol 0) (point)))
  1152. (delete-region (point-at-bol 0) (point))
  1153. (insert (format "\\begin{%s}\n"
  1154. (symbol-name org-export-latex-low-levels))))
  1155. (insert (format "\n\\item %s\\\\\n%s%%"
  1156. heading
  1157. (if label (format "\\label{%s}" label) "")))
  1158. (insert (org-export-latex-content content))
  1159. (cond ((stringp subcontent) (insert subcontent))
  1160. ((listp subcontent) (org-export-latex-sub subcontent)))
  1161. (insert (format "\\end{%s} %% ends low level\n"
  1162. (symbol-name org-export-latex-low-levels))))
  1163. ((listp org-export-latex-low-levels)
  1164. (if (string-match "% ends low level$"
  1165. (buffer-substring (point-at-bol 0) (point)))
  1166. (delete-region (point-at-bol 0) (point))
  1167. (insert (car org-export-latex-low-levels) "\n"))
  1168. (insert (format (nth 2 org-export-latex-low-levels)
  1169. heading
  1170. (if label (format "\\label{%s}" label) "")))
  1171. (insert (org-export-latex-content content))
  1172. (cond ((stringp subcontent) (insert subcontent))
  1173. ((listp subcontent) (org-export-latex-sub subcontent)))
  1174. (insert (nth 1 org-export-latex-low-levels)
  1175. " %% ends low level\n"))
  1176. ((stringp org-export-latex-low-levels)
  1177. (insert (format org-export-latex-low-levels heading) "\n")
  1178. (when label (insert (format "\\label{%s}\n" label)))
  1179. (insert (org-export-latex-content content))
  1180. (cond ((stringp subcontent) (insert subcontent))
  1181. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  1182. ;;; Exporting internals:
  1183. (defun org-export-latex-set-initial-vars (ext-plist level)
  1184. "Store org local variables required for LaTeX export.
  1185. EXT-PLIST is an optional additional plist.
  1186. LEVEL indicates the default depth for export."
  1187. (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
  1188. org-export-latex-done-keywords org-done-keywords
  1189. org-export-latex-not-done-keywords org-not-done-keywords
  1190. org-export-latex-complex-heading-re org-complex-heading-regexp
  1191. org-export-latex-display-custom-times org-display-custom-times
  1192. org-export-latex-all-targets-re
  1193. (org-make-target-link-regexp (org-all-targets))
  1194. org-export-latex-options-plist
  1195. (org-combine-plists (org-default-export-plist) ext-plist
  1196. (org-infile-export-plist))
  1197. org-export-latex-class
  1198. (or (and (org-region-active-p)
  1199. (save-excursion
  1200. (goto-char (region-beginning))
  1201. (and (looking-at org-complex-heading-regexp)
  1202. (org-entry-get nil "LaTeX_CLASS" 'selective))))
  1203. (save-excursion
  1204. (save-restriction
  1205. (widen)
  1206. (goto-char (point-min))
  1207. (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([-/a-zA-Z]+\\)" nil t)
  1208. (match-string 1))))
  1209. (plist-get org-export-latex-options-plist :latex-class)
  1210. org-export-latex-default-class)
  1211. org-export-latex-class-options
  1212. (or (and (org-region-active-p)
  1213. (save-excursion
  1214. (goto-char (region-beginning))
  1215. (and (looking-at org-complex-heading-regexp)
  1216. (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective))))
  1217. (save-excursion
  1218. (save-restriction
  1219. (widen)
  1220. (goto-char (point-min))
  1221. (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t)
  1222. (match-string 1))))
  1223. (plist-get org-export-latex-options-plist :latex-class-options))
  1224. org-export-latex-class
  1225. (or (car (assoc org-export-latex-class org-export-latex-classes))
  1226. (error "No definition for class `%s' in `org-export-latex-classes'"
  1227. org-export-latex-class))
  1228. org-export-latex-header
  1229. (cadr (assoc org-export-latex-class org-export-latex-classes))
  1230. org-export-latex-sectioning
  1231. (cddr (assoc org-export-latex-class org-export-latex-classes))
  1232. org-export-latex-sectioning-depth
  1233. (or level
  1234. (let ((hl-levels
  1235. (plist-get org-export-latex-options-plist :headline-levels))
  1236. (sec-depth (length org-export-latex-sectioning)))
  1237. (if (> hl-levels sec-depth) sec-depth hl-levels))))
  1238. (when (and org-export-latex-class-options
  1239. (string-match "\\S-" org-export-latex-class-options)
  1240. (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
  1241. org-export-latex-header))
  1242. (setq org-export-latex-header
  1243. (concat (substring org-export-latex-header 0 (match-end 1))
  1244. org-export-latex-class-options
  1245. (substring org-export-latex-header (match-end 0))))))
  1246. (defvar org-export-latex-format-toc-function
  1247. 'org-export-latex-format-toc-default
  1248. "The function formatting returning the string to create the table of contents.
  1249. The function mus take one parameter, the depth of the table of contents.")
  1250. (defun org-export-latex-make-header (title opt-plist)
  1251. "Make the LaTeX header and return it as a string.
  1252. TITLE is the current title from the buffer or region.
  1253. OPT-PLIST is the options plist for current buffer."
  1254. (let ((toc (plist-get opt-plist :table-of-contents))
  1255. (author (org-export-apply-macros-in-string
  1256. (plist-get opt-plist :author)))
  1257. (email (replace-regexp-in-string
  1258. "_" "\\\\_"
  1259. (org-export-apply-macros-in-string
  1260. (plist-get opt-plist :email))))
  1261. (description (org-export-apply-macros-in-string
  1262. (plist-get opt-plist :description)))
  1263. (keywords (org-export-apply-macros-in-string
  1264. (plist-get opt-plist :keywords))))
  1265. (concat
  1266. (if (plist-get opt-plist :time-stamp-file)
  1267. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  1268. ;; insert LaTeX custom header and packages from the list
  1269. (org-splice-latex-header
  1270. (org-export-apply-macros-in-string org-export-latex-header)
  1271. org-export-latex-default-packages-alist
  1272. org-export-latex-packages-alist nil
  1273. (org-export-apply-macros-in-string
  1274. (plist-get opt-plist :latex-header-extra)))
  1275. ;; append another special variable
  1276. (org-export-apply-macros-in-string org-export-latex-append-header)
  1277. ;; define alert if not yet defined
  1278. "\n\\providecommand{\\alert}[1]{\\textbf{#1}}"
  1279. ;; insert the title
  1280. (format
  1281. "\n\n\\title{%s}\n"
  1282. (org-export-latex-fontify-headline title))
  1283. ;; insert author info
  1284. (if (plist-get opt-plist :author-info)
  1285. (format "\\author{%s%s}\n"
  1286. (org-export-latex-fontify-headline (or author user-full-name))
  1287. (if (and (plist-get opt-plist :email-info) email
  1288. (string-match "\\S-" email))
  1289. (format "\\thanks{%s}" email)
  1290. ""))
  1291. (format "%%\\author{%s}\n"
  1292. (org-export-latex-fontify-headline (or author user-full-name))))
  1293. ;; insert the date
  1294. (format "\\date{%s}\n"
  1295. (format-time-string
  1296. (or (plist-get opt-plist :date)
  1297. org-export-latex-date-format)))
  1298. ;; add some hyperref options
  1299. ;; FIXME: let's have a defcustom for this?
  1300. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  1301. (org-export-latex-fontify-headline keywords)
  1302. (org-export-latex-fontify-headline description)
  1303. (concat "Emacs Org-mode version " org-version))
  1304. ;; beginning of the document
  1305. "\n\\begin{document}\n\n"
  1306. ;; insert the title command
  1307. (when (string-match "\\S-" title)
  1308. (if (string-match "%s" org-export-latex-title-command)
  1309. (format org-export-latex-title-command title)
  1310. org-export-latex-title-command))
  1311. "\n\n"
  1312. ;; table of contents
  1313. (when (and org-export-with-toc
  1314. (plist-get opt-plist :section-numbers))
  1315. (funcall org-export-latex-format-toc-function
  1316. (cond ((numberp toc)
  1317. (min toc (plist-get opt-plist :headline-levels)))
  1318. (toc (plist-get opt-plist :headline-levels))))))))
  1319. (defun org-export-latex-format-toc-default (depth)
  1320. (when depth
  1321. (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
  1322. depth)))
  1323. (defun org-export-latex-first-lines (opt-plist &optional beg end)
  1324. "Export the first lines before first headline.
  1325. If BEG is non-nil, it is the beginning of the region.
  1326. If END is non-nil, it is the end of the region."
  1327. (save-excursion
  1328. (goto-char (or beg (point-min)))
  1329. (let* ((pt (point))
  1330. (end (if (re-search-forward
  1331. (concat "^" (org-get-limited-outline-regexp)) end t)
  1332. (goto-char (match-beginning 0))
  1333. (goto-char (or end (point-max))))))
  1334. (prog1
  1335. (org-export-latex-content
  1336. (org-export-preprocess-string
  1337. (buffer-substring pt end)
  1338. :for-backend 'latex
  1339. :emph-multiline t
  1340. :add-text nil
  1341. :comments nil
  1342. :skip-before-1st-heading nil
  1343. :LaTeX-fragments nil
  1344. :timestamps (plist-get opt-plist :timestamps)
  1345. :footnotes (plist-get opt-plist :footnotes)))
  1346. (org-unmodified
  1347. (let ((inhibit-read-only t)
  1348. (limit (max pt (1- end))))
  1349. (add-text-properties pt limit
  1350. '(:org-license-to-kill t))
  1351. (save-excursion
  1352. (goto-char pt)
  1353. (while (re-search-forward "^[ \t]*#\\+.*\n?" limit t)
  1354. (let ((case-fold-search t))
  1355. (unless (org-string-match-p
  1356. "^[ \t]*#\\+\\(attr_\\|caption\\>\\|label\\>\\)"
  1357. (match-string 0))
  1358. (remove-text-properties (match-beginning 0) (match-end 0)
  1359. '(:org-license-to-kill t))))))))))))
  1360. (defvar org-export-latex-header-defs nil
  1361. "The header definitions that might be used in the LaTeX body.")
  1362. (defun org-export-latex-content (content &optional exclude-list)
  1363. "Convert CONTENT string to LaTeX.
  1364. Don't perform conversions that are in EXCLUDE-LIST. Recognized
  1365. conversion types are: quotation-marks, emphasis, sub-superscript,
  1366. links, keywords, lists, tables, fixed-width"
  1367. (with-temp-buffer
  1368. (org-install-letbind)
  1369. (insert content)
  1370. (unless (memq 'timestamps exclude-list)
  1371. (org-export-latex-time-stamps))
  1372. (unless (memq 'quotation-marks exclude-list)
  1373. (org-export-latex-quotation-marks))
  1374. (unless (memq 'emphasis exclude-list)
  1375. (when (plist-get org-export-latex-options-plist :emphasize)
  1376. (org-export-latex-fontify)))
  1377. (unless (memq 'sub-superscript exclude-list)
  1378. (org-export-latex-special-chars
  1379. (plist-get org-export-latex-options-plist :sub-superscript)))
  1380. (unless (memq 'links exclude-list)
  1381. (org-export-latex-links))
  1382. (unless (memq 'keywords exclude-list)
  1383. (org-export-latex-keywords))
  1384. (unless (memq 'lists exclude-list)
  1385. (org-export-latex-lists))
  1386. (unless (memq 'tables exclude-list)
  1387. (org-export-latex-tables
  1388. (plist-get org-export-latex-options-plist :tables)))
  1389. (unless (memq 'fixed-width exclude-list)
  1390. (org-export-latex-fixed-width
  1391. (plist-get org-export-latex-options-plist :fixed-width)))
  1392. ;; return string
  1393. (buffer-substring (point-min) (point-max))))
  1394. (defun org-export-latex-protect-string (s)
  1395. "Add the org-protected property to string S."
  1396. (add-text-properties 0 (length s) '(org-protected t) s) s)
  1397. (defun org-export-latex-protect-char-in-string (char-list string)
  1398. "Add org-protected text-property to char from CHAR-LIST in STRING."
  1399. (with-temp-buffer
  1400. (save-match-data
  1401. (insert string)
  1402. (goto-char (point-min))
  1403. (while (re-search-forward (regexp-opt char-list) nil t)
  1404. (add-text-properties (match-beginning 0)
  1405. (match-end 0) '(org-protected t)))
  1406. (buffer-string))))
  1407. (defun org-export-latex-keywords-maybe (&optional remove-list)
  1408. "Maybe remove keywords depending on rules in REMOVE-LIST."
  1409. (goto-char (point-min))
  1410. (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
  1411. (case-fold-search nil)
  1412. (todo-markup org-export-latex-todo-keyword-markup)
  1413. fmt)
  1414. ;; convert TODO keywords
  1415. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  1416. (if (plist-get remove-list :todo)
  1417. (replace-match "")
  1418. (setq fmt (cond
  1419. ((stringp todo-markup) todo-markup)
  1420. ((and (consp todo-markup) (stringp (car todo-markup)))
  1421. (if (member (match-string 1) org-export-latex-done-keywords)
  1422. (cdr todo-markup) (car todo-markup)))
  1423. (t (cdr (or (assoc (match-string 1) todo-markup)
  1424. (car todo-markup))))))
  1425. (replace-match (org-export-latex-protect-string
  1426. (format fmt (match-string 1))) t t)))
  1427. ;; convert priority string
  1428. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  1429. (if (plist-get remove-list :priority)
  1430. (replace-match "")
  1431. (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
  1432. ;; convert tags
  1433. (when (re-search-forward "\\(:[a-zA-Z0-9_@#%]+\\)+:" nil t)
  1434. (if (or (not org-export-with-tags)
  1435. (plist-get remove-list :tags))
  1436. (replace-match "")
  1437. (replace-match
  1438. (org-export-latex-protect-string
  1439. (format org-export-latex-tag-markup
  1440. (save-match-data
  1441. (replace-regexp-in-string
  1442. "\\([_#]\\)" "\\\\\\1" (match-string 0)))))
  1443. t t)))))
  1444. (defun org-export-latex-fontify-headline (string)
  1445. "Fontify special words in STRING."
  1446. (with-temp-buffer
  1447. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  1448. ;; the beginning of the buffer - inserting "\n" is safe here though.
  1449. (insert "\n" string)
  1450. ;; Preserve math snippets
  1451. (let* ((matchers (plist-get org-format-latex-options :matchers))
  1452. (re-list org-latex-regexps)
  1453. beg end re e m n block off)
  1454. ;; Check the different regular expressions
  1455. (while (setq e (pop re-list))
  1456. (setq m (car e) re (nth 1 e) n (nth 2 e)
  1457. block (if (nth 3 e) "\n\n" ""))
  1458. (setq off (if (member m '("$" "$1")) 1 0))
  1459. (when (and (member m matchers) (not (equal m "begin")))
  1460. (goto-char (point-min))
  1461. (while (re-search-forward re nil t)
  1462. (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
  1463. (add-text-properties beg end
  1464. '(org-protected t org-latex-math t))))))
  1465. ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
  1466. (goto-char (point-min))
  1467. (let ((case-fold-search nil))
  1468. (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
  1469. (unless (eq (char-before (match-beginning 1)) ?\\)
  1470. (org-if-unprotected-1
  1471. (replace-match (org-export-latex-protect-string
  1472. (concat "\\" (match-string 1)
  1473. "{}")) t t)))))
  1474. (goto-char (point-min))
  1475. (let ((re (concat "\\\\\\([a-zA-Z]+\\)"
  1476. "\\(?:<[^<>\n]*>\\)*"
  1477. "\\(?:\\[[^][\n]*?\\]\\)*"
  1478. "\\(?:<[^<>\n]*>\\)*"
  1479. "\\("
  1480. (org-create-multibrace-regexp "{" "}" 3)
  1481. "\\)\\{1,3\\}")))
  1482. (while (re-search-forward re nil t)
  1483. (unless (or
  1484. ;; check for comment line
  1485. (save-excursion (goto-char (match-beginning 0))
  1486. (org-in-indented-comment-line))
  1487. ;; Check if this is a defined entity, so that is may need conversion
  1488. (org-entity-get (match-string 1)))
  1489. (add-text-properties (match-beginning 0) (match-end 0)
  1490. '(org-protected t)))))
  1491. (when (plist-get org-export-latex-options-plist :emphasize)
  1492. (org-export-latex-fontify))
  1493. (org-export-latex-time-stamps)
  1494. (org-export-latex-quotation-marks)
  1495. (org-export-latex-keywords-maybe)
  1496. (org-export-latex-special-chars
  1497. (plist-get org-export-latex-options-plist :sub-superscript))
  1498. (org-export-latex-links)
  1499. (org-trim (buffer-string))))
  1500. (defun org-export-latex-time-stamps ()
  1501. "Format time stamps."
  1502. (goto-char (point-min))
  1503. (let ((org-display-custom-times org-export-latex-display-custom-times))
  1504. (while (re-search-forward org-ts-regexp-both nil t)
  1505. (org-if-unprotected-at (1- (point))
  1506. (replace-match
  1507. (org-export-latex-protect-string
  1508. (format (if (string= "<" (substring (match-string 0) 0 1))
  1509. org-export-latex-timestamp-markup
  1510. org-export-latex-timestamp-inactive-markup)
  1511. (substring (org-translate-time (match-string 0)) 1 -1)))
  1512. t t)))))
  1513. (defun org-export-latex-quotation-marks ()
  1514. "Export quotation marks depending on language conventions."
  1515. (mapc (lambda(l)
  1516. (goto-char (point-min))
  1517. (while (re-search-forward (car l) nil t)
  1518. (let ((rpl (concat (match-string 1)
  1519. (org-export-latex-protect-string
  1520. (copy-sequence (cdr l))))))
  1521. (org-if-unprotected-1
  1522. (replace-match rpl t t)))))
  1523. (cdr (or (assoc (plist-get org-export-latex-options-plist :language)
  1524. org-export-latex-quotes)
  1525. ;; falls back on english
  1526. (assoc "en" org-export-latex-quotes)))))
  1527. (defun org-export-latex-special-chars (sub-superscript)
  1528. "Export special characters to LaTeX.
  1529. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  1530. See the `org-export-latex.el' code for a complete conversion table."
  1531. (goto-char (point-min))
  1532. (mapc (lambda(c)
  1533. (goto-char (point-min))
  1534. (while (re-search-forward c nil t)
  1535. ;; Put the point where to check for org-protected
  1536. (unless (or (get-text-property (match-beginning 2) 'org-protected)
  1537. (save-match-data (org-at-table.el-p)))
  1538. (cond ((member (match-string 2) '("\\$" "$"))
  1539. (if (equal (match-string 2) "\\$")
  1540. nil
  1541. (replace-match "\\$" t t)))
  1542. ((member (match-string 2) '("&" "%" "#"))
  1543. (if (equal (match-string 1) "\\")
  1544. (replace-match (match-string 2) t t)
  1545. (replace-match (concat (match-string 1) "\\"
  1546. (match-string 2)) t t)
  1547. (backward-char 1)))
  1548. ((equal (match-string 2) "...")
  1549. (replace-match
  1550. (concat (match-string 1)
  1551. (org-export-latex-protect-string "\\ldots{}")) t t))
  1552. ((equal (match-string 2) "~")
  1553. (cond ((equal (match-string 1) "\\") nil)
  1554. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  1555. (replace-match (concat (match-string 1) "\\~") t t))
  1556. (t (replace-match
  1557. (org-export-latex-protect-string
  1558. (concat (match-string 1) "\\~{}")) t t))))
  1559. ((member (match-string 2) '("{" "}"))
  1560. (unless (save-match-data (org-inside-latex-math-p))
  1561. (if (equal (match-string 1) "\\")
  1562. (replace-match (match-string 2) t t)
  1563. (replace-match (concat (match-string 1) "\\"
  1564. (match-string 2)) t t)))))
  1565. (unless (save-match-data (org-inside-latex-math-p))
  1566. (cond ((equal (match-string 2) "\\")
  1567. (replace-match (or (save-match-data
  1568. (org-export-latex-treat-backslash-char
  1569. (match-string 1)
  1570. (or (match-string 3) "")))
  1571. "") t t)
  1572. (when (and (get-text-property (1- (point)) 'org-entity)
  1573. (looking-at "{}"))
  1574. ;; OK, this was an entity replacement, and the user
  1575. ;; had terminated the entity with {}. Make sure
  1576. ;; {} is protected as well, and remove the extra {}
  1577. ;; inserted by the conversion.
  1578. (put-text-property (point) (+ 2 (point)) 'org-protected t)
  1579. (if (save-excursion (goto-char (max (- (point) 2) (point-min)))
  1580. (looking-at "{}"))
  1581. (replace-match ""))
  1582. (forward-char 2))
  1583. (backward-char 1))
  1584. ((member (match-string 2) '("_" "^"))
  1585. (replace-match (or (save-match-data
  1586. (org-export-latex-treat-sub-super-char
  1587. sub-superscript
  1588. (match-string 2)
  1589. (match-string 1)
  1590. (match-string 3))) "") t t)
  1591. (backward-char 1)))))))
  1592. '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  1593. "\\(\\(\\\\?\\$\\)\\)"
  1594. "\\([a-zA-Z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-zA-Z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-zA-Z0-9]+}\\|([a-zA-Z0-9]+)\\)"
  1595. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|\\([&#%{}\"]\\|[a-zA-Z][a-zA-Z0-9]*\\)\\)"
  1596. "\\(^\\|.\\)\\([&#%{}~]\\|\\.\\.\\.\\)"
  1597. ;; (?\< . "\\textless{}")
  1598. ;; (?\> . "\\textgreater{}")
  1599. )))
  1600. (defun org-inside-latex-math-p ()
  1601. (get-text-property (point) 'org-latex-math))
  1602. (defun org-export-latex-treat-sub-super-char
  1603. (subsup char string-before string-after)
  1604. "Convert the \"_\" and \"^\" characters to LaTeX.
  1605. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  1606. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  1607. (cond ((equal string-before "\\")
  1608. (concat string-before char string-after))
  1609. ((and (string-match "\\S-+" string-after))
  1610. ;; this is part of a math formula
  1611. (cond ((eq 'org-link (get-text-property 0 'face char))
  1612. (concat string-before "\\" char string-after))
  1613. ((save-match-data (org-inside-latex-math-p))
  1614. (if subsup
  1615. (cond ((eq 1 (length string-after))
  1616. (concat string-before char string-after))
  1617. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  1618. (format "%s%s{%s}" string-before char
  1619. (match-string 1 string-after))))))
  1620. ((and (> (length string-after) 1)
  1621. (or (eq subsup t)
  1622. (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
  1623. (or (string-match "[{]?\\([^}]+\\)[}]?" string-after)
  1624. (string-match "[(]?\\([^)]+\\)[)]?" string-after)))
  1625. (org-export-latex-protect-string
  1626. (format "%s$%s{%s}$" string-before char
  1627. (if (and (> (match-end 1) (1+ (match-beginning 1)))
  1628. (not (equal (substring string-after 0 2) "{\\")))
  1629. (concat "\\mathrm{" (match-string 1 string-after) "}")
  1630. (match-string 1 string-after)))))
  1631. ((eq subsup t) (concat string-before "$" char string-after "$"))
  1632. (t (org-export-latex-protect-string
  1633. (concat string-before "\\" char "{}" string-after)))))
  1634. (t (org-export-latex-protect-string
  1635. (concat string-before "\\" char "{}" string-after)))))
  1636. (defun org-export-latex-treat-backslash-char (string-before string-after)
  1637. "Convert the \"$\" special character to LaTeX.
  1638. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  1639. (let ((ass (org-entity-get string-after)))
  1640. (cond
  1641. (ass (org-add-props
  1642. (if (nth 2 ass)
  1643. (concat string-before
  1644. (org-export-latex-protect-string
  1645. (concat "$" (nth 1 ass) "$")))
  1646. (concat string-before (org-export-latex-protect-string
  1647. (nth 1 ass))))
  1648. nil 'org-entity t))
  1649. ((and (not (string-match "^[ \n\t]" string-after))
  1650. (not (string-match "[ \t]\\'\\|^" string-before)))
  1651. ;; backslash is inside a word
  1652. (concat string-before
  1653. (org-export-latex-protect-string
  1654. (concat "\\textbackslash{}" string-after))))
  1655. ((not (or (equal string-after "")
  1656. (string-match "^[ \t\n]" string-after)))
  1657. ;; backslash might escape a character (like \#) or a user TeX
  1658. ;; macro (like \setcounter)
  1659. (concat string-before
  1660. (org-export-latex-protect-string (concat "\\" string-after))))
  1661. ((and (string-match "^[ \t\n]" string-after)
  1662. (string-match "[ \t\n]\\'" string-before))
  1663. ;; backslash is alone, convert it to $\backslash$
  1664. (org-export-latex-protect-string
  1665. (concat string-before "\\textbackslash{}" string-after)))
  1666. (t (org-export-latex-protect-string
  1667. (concat string-before "\\textbackslash{}" string-after))))))
  1668. (defun org-export-latex-keywords ()
  1669. "Convert special keywords to LaTeX."
  1670. (goto-char (point-min))
  1671. (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
  1672. (replace-match (format org-export-latex-timestamp-keyword-markup
  1673. (match-string 0)) t t)
  1674. (save-excursion
  1675. (beginning-of-line 1)
  1676. (unless (looking-at ".*\n[ \t]*\n")
  1677. (end-of-line 1)
  1678. (insert "\n")))))
  1679. (defun org-export-latex-fixed-width (opt)
  1680. "When OPT is non-nil convert fixed-width sections to LaTeX."
  1681. (goto-char (point-min))
  1682. (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
  1683. (unless (get-text-property (point) 'org-example)
  1684. (if opt
  1685. (progn (goto-char (match-beginning 0))
  1686. (insert "\\begin{verbatim}\n")
  1687. (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
  1688. (replace-match (concat (match-string 1)
  1689. (match-string 2)) t t)
  1690. (forward-line))
  1691. (insert "\\end{verbatim}\n"))
  1692. (progn (goto-char (match-beginning 0))
  1693. (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
  1694. (replace-match (concat "%" (match-string 1)
  1695. (match-string 2)) t t)
  1696. (forward-line)))))))
  1697. (defvar org-table-last-alignment) ; defined in org-table.el
  1698. (defvar org-table-last-column-widths) ; defined in org-table.el
  1699. (declare-function orgtbl-to-latex "org-table" (table params) t)
  1700. (defun org-export-latex-tables (insert)
  1701. "Convert tables to LaTeX and INSERT it."
  1702. ;; First, get the table.el tables
  1703. (goto-char (point-min))
  1704. (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t)
  1705. (org-if-unprotected
  1706. (require 'table)
  1707. (org-export-latex-convert-table.el-table)))
  1708. ;; And now the Org-mode tables
  1709. (goto-char (point-min))
  1710. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  1711. (org-if-unprotected-at (1- (point))
  1712. (org-table-align)
  1713. (let* ((beg (org-table-begin))
  1714. (end (org-table-end))
  1715. (raw-table (buffer-substring beg end))
  1716. (org-table-last-alignment (copy-sequence org-table-last-alignment))
  1717. (org-table-last-column-widths (copy-sequence
  1718. org-table-last-column-widths))
  1719. fnum fields line lines olines gr colgropen line-fmt align
  1720. caption width shortn label attr floatp placement
  1721. longtblp tblenv tabular-env)
  1722. (if org-export-latex-tables-verbatim
  1723. (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
  1724. "\\end{verbatim}\n")))
  1725. (apply 'delete-region (list beg end))
  1726. (insert (org-export-latex-protect-string tbl)))
  1727. (progn
  1728. (setq caption (org-find-text-property-in-string
  1729. 'org-caption raw-table)
  1730. shortn (org-find-text-property-in-string
  1731. 'org-caption-shortn raw-table)
  1732. attr (org-find-text-property-in-string
  1733. 'org-attributes raw-table)
  1734. label (org-find-text-property-in-string
  1735. 'org-label raw-table)
  1736. longtblp (and attr (stringp attr)
  1737. (string-match "\\<longtable\\>" attr))
  1738. tblenv (if (and attr (stringp attr))
  1739. (cond ((string-match "\\<sidewaystable\\>" attr)
  1740. "sidewaystable")
  1741. ((or (string-match (regexp-quote "table*") attr)
  1742. (string-match "\\<multicolumn\\>" attr))
  1743. "table*")
  1744. (t "table"))
  1745. "table")
  1746. tabular-env
  1747. (if (and attr (stringp attr)
  1748. (string-match "\\(tabular.\\)" attr))
  1749. (match-string 1 attr)
  1750. org-export-latex-tabular-environment)
  1751. width (and attr (stringp attr)
  1752. (string-match "\\<width=\\([^ \t\n\r]+\\)" attr)
  1753. (match-string 1 attr))
  1754. align (and attr (stringp attr)
  1755. (string-match "\\<align=\\([^ \t\n\r]+\\)" attr)
  1756. (match-string 1 attr))
  1757. floatp (or caption label (string= "table*" tblenv))
  1758. placement (if (and attr
  1759. (stringp attr)
  1760. (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
  1761. (match-string 1 attr)
  1762. (concat
  1763. "[" org-latex-default-figure-position "]")))
  1764. (setq caption (and caption (org-export-latex-fontify-headline caption)))
  1765. (setq lines (org-split-string raw-table "\n"))
  1766. (apply 'delete-region (list beg end))
  1767. (when org-export-table-remove-special-lines
  1768. (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
  1769. (when org-table-clean-did-remove-column
  1770. (pop org-table-last-alignment)
  1771. (pop org-table-last-column-widths))
  1772. ;; make a formatting string to reflect alignment
  1773. (setq olines lines)
  1774. (while (and (not line-fmt) (setq line (pop olines)))
  1775. (unless (string-match "^[ \t]*|-" line)
  1776. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1777. (setq fnum (make-vector (length fields) 0))
  1778. (setq line-fmt
  1779. (mapconcat
  1780. (lambda (x)
  1781. (setq gr (pop org-table-colgroup-info))
  1782. (format "%s%%s%s"
  1783. (cond ((eq gr :start)
  1784. (prog1 (if colgropen "|" "|")
  1785. (setq colgropen t)))
  1786. ((eq gr :startend)
  1787. (prog1 (if colgropen "|" "|")
  1788. (setq colgropen nil)))
  1789. (t ""))
  1790. (if (memq gr '(:end :startend))
  1791. (progn (setq colgropen nil) "|")
  1792. "")))
  1793. fnum ""))))
  1794. ;; fix double || in line-fmt
  1795. (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
  1796. ;; maybe remove the first and last "|"
  1797. (when (and (not org-export-latex-tables-column-borders)
  1798. (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
  1799. (setq line-fmt (match-string 2 line-fmt)))
  1800. ;; format alignment
  1801. (unless align
  1802. (setq align (apply 'format
  1803. (cons line-fmt
  1804. (mapcar (lambda (x) (if x "r" "l"))
  1805. org-table-last-alignment)))))
  1806. ;; prepare the table to send to orgtbl-to-latex
  1807. (setq lines
  1808. (mapcar
  1809. (lambda(elem)
  1810. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  1811. (org-split-string
  1812. (progn (set-text-properties 0 (length elem) nil elem)
  1813. (org-trim elem)) "|")))
  1814. lines))
  1815. (when insert
  1816. (insert (org-export-latex-protect-string
  1817. (concat
  1818. (if longtblp
  1819. (concat "\\begin{longtable}{" align "}\n")
  1820. (if floatp
  1821. (format "\\begin{%s}%s\n" tblenv placement)))
  1822. (if (and floatp org-export-latex-table-caption-above)
  1823. (format
  1824. "\\caption%s{%s} %s"
  1825. (if shortn (concat "[" shortn "]") "")
  1826. (or caption "")
  1827. (if label (format "\\label{%s}" label) "")))
  1828. (if (and longtblp caption org-export-latex-table-caption-above)
  1829. "\\\\\n" "\n")
  1830. (if (and org-export-latex-tables-centered (not longtblp))
  1831. "\\begin{center}\n")
  1832. (if (not longtblp)
  1833. (format "\\begin{%s}%s{%s}\n"
  1834. tabular-env
  1835. (if width (format "{%s}" width) "")
  1836. align))
  1837. (orgtbl-to-latex
  1838. lines
  1839. `(:tstart nil :tend nil
  1840. :hlend ,(if longtblp
  1841. (format "\\\\
  1842. \\hline
  1843. \\endhead
  1844. \\hline\\multicolumn{%d}{r}{Continued on next page}\\
  1845. \\endfoot
  1846. \\endlastfoot" (length org-table-last-alignment))
  1847. nil)))
  1848. (if (not longtblp) (format "\n\\end{%s}" tabular-env))
  1849. (if longtblp "\n" (if org-export-latex-tables-centered
  1850. "\n\\end{center}\n" "\n"))
  1851. (if (and floatp (not org-export-latex-table-caption-above))
  1852. (format
  1853. "\\caption%s{%s} %s"
  1854. (if shortn (concat "[" shortn "]") "")
  1855. (or caption "")
  1856. (if label (format "\\label{%s}" label) "")))
  1857. (if longtblp
  1858. "\\end{longtable}"
  1859. (if floatp (format "\\end{%s}" tblenv)))))
  1860. "\n\n"))))))))
  1861. (defun org-export-latex-convert-table.el-table ()
  1862. "Replace table.el table at point with LaTeX code."
  1863. (let (tbl caption shortn label line floatp attr align rmlines)
  1864. (setq line (buffer-substring (point-at-bol) (point-at-eol))
  1865. label (org-get-text-property-any 0 'org-label line)
  1866. caption (org-get-text-property-any 0 'org-caption line)
  1867. shortn (org-get-text-property-any 0 'org-caption-shortn line)
  1868. attr (org-get-text-property-any 0 'org-attributes line)
  1869. align (and attr (stringp attr)
  1870. (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
  1871. (match-string 1 attr))
  1872. rmlines (and attr (stringp attr)
  1873. (string-match "\\<rmlines\\>" attr))
  1874. floatp (or label caption))
  1875. (and (get-buffer "*org-export-table*")
  1876. (kill-buffer (get-buffer "*org-export-table*")))
  1877. (table-generate-source 'latex "*org-export-table*" "caption")
  1878. (setq tbl (with-current-buffer "*org-export-table*"
  1879. (buffer-string)))
  1880. (while (string-match "^%.*\n" tbl)
  1881. (setq tbl (replace-match "" t t tbl)))
  1882. ;; fix the hlines
  1883. (when rmlines
  1884. (let ((n 0) lines)
  1885. (setq lines (mapcar (lambda (x)
  1886. (if (string-match "^\\\\hline$" x)
  1887. (progn
  1888. (setq n (1+ n))
  1889. (if (= n 2) x nil))
  1890. x))
  1891. (org-split-string tbl "\n")))
  1892. (setq tbl (mapconcat 'identity (delq nil lines) "\n"))))
  1893. (when (and align (string-match "\\\\begin{tabular}{.*}" tbl))
  1894. (setq tbl (replace-match (concat "\\begin{tabular}{" align "}")
  1895. t t tbl)))
  1896. (and (get-buffer "*org-export-table*")
  1897. (kill-buffer (get-buffer "*org-export-table*")))
  1898. (beginning-of-line 0)
  1899. (while (looking-at "[ \t]*\\(|\\|\\+-\\)")
  1900. (delete-region (point) (1+ (point-at-eol))))
  1901. (when org-export-latex-tables-centered
  1902. (setq tbl (concat "\\begin{center}\n" tbl "\\end{center}")))
  1903. (when floatp
  1904. (setq tbl (concat "\\begin{table}\n"
  1905. (if (not org-export-latex-table-caption-above) tbl)
  1906. (format "\\caption%s{%s%s}\n"
  1907. (if shortn (format "[%s]" shortn) "")
  1908. (if label (format "\\label{%s}" label) "")
  1909. (or caption ""))
  1910. (if org-export-latex-table-caption-above tbl)
  1911. "\n\\end{table}\n")))
  1912. (insert (org-export-latex-protect-string tbl))))
  1913. (defun org-export-latex-fontify ()
  1914. "Convert fontification to LaTeX."
  1915. (goto-char (point-min))
  1916. (while (re-search-forward org-emph-re nil t)
  1917. ;; The match goes one char after the *string*, except at the end of a line
  1918. (let ((emph (assoc (match-string 3)
  1919. org-export-latex-emphasis-alist))
  1920. (beg (match-beginning 0))
  1921. (end (match-end 0))
  1922. rpl s)
  1923. (unless emph
  1924. (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
  1925. (match-string 3)))
  1926. (unless (or (and (get-text-property (- (point) 2) 'org-protected)
  1927. (not (get-text-property
  1928. (- (point) 2) 'org-verbatim-emph)))
  1929. (equal (char-after (match-beginning 3))
  1930. (char-after (1+ (match-beginning 3))))
  1931. (save-excursion
  1932. (goto-char (match-beginning 1))
  1933. (save-match-data
  1934. (and (org-at-table-p)
  1935. (string-match
  1936. "[|\n]" (buffer-substring beg end)))))
  1937. (and (equal (match-string 3) "+")
  1938. (save-match-data
  1939. (string-match "\\`-+\\'" (match-string 4)))))
  1940. (setq s (match-string 4))
  1941. (setq rpl (concat (match-string 1)
  1942. (org-export-latex-emph-format (cadr emph)
  1943. (match-string 4))
  1944. (match-string 5)))
  1945. (if (caddr emph)
  1946. (setq rpl (org-export-latex-protect-string rpl))
  1947. (save-match-data
  1948. (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
  1949. (progn
  1950. (add-text-properties (match-beginning 1) (match-end 1)
  1951. '(org-protected t) rpl)
  1952. (add-text-properties (match-beginning 3) (match-end 3)
  1953. '(org-protected t) rpl)))))
  1954. (replace-match rpl t t)))
  1955. (backward-char)))
  1956. (defun org-export-latex-emph-format (format string)
  1957. "Format an emphasis string and handle the \\verb special case."
  1958. (when (member format '("\\verb" "\\protectedtexttt"))
  1959. (save-match-data
  1960. (if (equal format "\\verb")
  1961. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1962. (catch 'exit
  1963. (loop for i from 0 to (1- (length ll)) do
  1964. (if (not (string-match (regexp-quote (substring ll i (1+ i)))
  1965. string))
  1966. (progn
  1967. (setq format (concat "\\verb" (substring ll i (1+ i))
  1968. "%s" (substring ll i (1+ i))))
  1969. (throw 'exit nil))))))
  1970. (let ((start 0)
  1971. (trans '(("\\" . "\\textbackslash{}")
  1972. ("~" . "\\textasciitilde{}")
  1973. ("^" . "\\textasciicircum{}")))
  1974. (rtn "") char)
  1975. (while (string-match "[\\{}$%&_#~^]" string)
  1976. (setq char (match-string 0 string))
  1977. (if (> (match-beginning 0) 0)
  1978. (setq rtn (concat rtn (substring string
  1979. 0 (match-beginning 0)))))
  1980. (setq string (substring string (1+ (match-beginning 0))))
  1981. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  1982. rtn (concat rtn char)))
  1983. (setq string (concat rtn string) format "\\texttt{%s}")
  1984. (while (string-match "--" string)
  1985. (setq string (replace-match "-{}-" t t string)))))))
  1986. (format format string))
  1987. (defun org-export-latex-links ()
  1988. ;; Make sure to use the LaTeX hyperref and graphicx package
  1989. ;; or send some warnings.
  1990. "Convert links to LaTeX."
  1991. (goto-char (point-min))
  1992. (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
  1993. (org-if-unprotected-1
  1994. (goto-char (match-beginning 0))
  1995. (let* ((re-radio org-export-latex-all-targets-re)
  1996. (remove (list (match-beginning 0) (match-end 0)))
  1997. (raw-path (org-extract-attributes (match-string 3)))
  1998. (full-raw-path (concat (match-string 1) raw-path))
  1999. (desc (match-string 5))
  2000. (type (or (match-string 2)
  2001. (if (or (file-name-absolute-p raw-path)
  2002. (string-match "^\\.\\.?/" raw-path))
  2003. "file")))
  2004. (coderefp (equal type "coderef"))
  2005. (caption (org-find-text-property-in-string 'org-caption raw-path))
  2006. (shortn (org-find-text-property-in-string 'org-caption-shortn raw-path))
  2007. (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
  2008. (plist-get org-export-latex-options-plist :latex-image-options)))
  2009. (label (org-find-text-property-in-string 'org-label raw-path))
  2010. imgp radiop fnc
  2011. ;; define the path of the link
  2012. (path (cond
  2013. ((member type '("coderef"))
  2014. raw-path)
  2015. ((member type '("http" "https" "ftp"))
  2016. (concat type ":" raw-path))
  2017. ((and re-radio (string-match re-radio raw-path))
  2018. (setq radiop t))
  2019. ((equal type "mailto")
  2020. (concat type ":" raw-path))
  2021. ((equal type "file")
  2022. (if (and (org-file-image-p
  2023. (expand-file-name
  2024. raw-path)
  2025. org-export-latex-inline-image-extensions)
  2026. (or (get-text-property 0 'org-no-description
  2027. raw-path)
  2028. (equal desc full-raw-path)))
  2029. (setq imgp t)
  2030. (progn (when (string-match "\\(.+\\)::.+" raw-path)
  2031. (setq raw-path (match-string 1 raw-path)))
  2032. (if (file-exists-p raw-path)
  2033. (concat type "://" (expand-file-name raw-path))
  2034. (concat type "://" (org-export-directory
  2035. :LaTeX org-export-latex-options-plist)
  2036. raw-path))))))))
  2037. ;; process with link inserting
  2038. (apply 'delete-region remove)
  2039. (setq caption (and caption (org-export-latex-fontify-headline caption)))
  2040. (cond ((and imgp
  2041. (plist-get org-export-latex-options-plist :inline-images))
  2042. ;; OK, we need to inline an image
  2043. (insert
  2044. (org-export-latex-format-image raw-path caption label attr shortn)))
  2045. (coderefp
  2046. (insert (format
  2047. (org-export-get-coderef-format path desc)
  2048. (cdr (assoc path org-export-code-refs)))))
  2049. (radiop (insert (format org-export-latex-hyperref-format
  2050. (org-solidify-link-text raw-path) desc)))
  2051. ((not type)
  2052. (insert (format org-export-latex-hyperref-format
  2053. (org-remove-initial-hash
  2054. (org-solidify-link-text raw-path))
  2055. desc)))
  2056. (path
  2057. (when (org-at-table-p)
  2058. ;; There is a strange problem when we have a link in a table,
  2059. ;; ampersands then cause a problem. I think this must be
  2060. ;; a LaTeX issue, but we here implement a work-around anyway.
  2061. (setq path (org-export-latex-protect-amp path)
  2062. desc (org-export-latex-protect-amp desc)))
  2063. (insert
  2064. (if (string-match "%s.*%s" org-export-latex-href-format)
  2065. (format org-export-latex-href-format path desc)
  2066. (format org-export-latex-href-format path))))
  2067. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  2068. ;; The link protocol has a function for formatting the link
  2069. (insert
  2070. (save-match-data
  2071. (funcall fnc (org-link-unescape raw-path) desc 'latex))))
  2072. (t (insert "\\texttt{" desc "}")))))))
  2073. (defun org-export-latex-format-image (path caption label attr &optional shortn)
  2074. "Format the image element, depending on user settings."
  2075. (let (ind floatp wrapp multicolumnp placement figenv)
  2076. (setq floatp (or caption label))
  2077. (setq ind (org-get-text-property-any 0 'original-indentation path))
  2078. (when (and attr (stringp attr))
  2079. (if (string-match "[ \t]*\\<wrap\\>" attr)
  2080. (setq wrapp t floatp nil attr (replace-match "" t t attr)))
  2081. (if (string-match "[ \t]*\\<float\\>" attr)
  2082. (setq wrapp nil floatp t attr (replace-match "" t t attr)))
  2083. (if (string-match "[ \t]*\\<multicolumn\\>" attr)
  2084. (setq multicolumnp t attr (replace-match "" t t attr))))
  2085. (setq placement
  2086. (cond
  2087. (wrapp "{l}{0.5\\textwidth}")
  2088. (floatp (concat "[" org-latex-default-figure-position "]"))
  2089. (t "")))
  2090. (when (and attr (stringp attr)
  2091. (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
  2092. (setq placement (match-string 1 attr)
  2093. attr (replace-match "" t t attr)))
  2094. (setq attr (and attr (org-trim attr)))
  2095. (when (or (not attr) (= (length attr) 0))
  2096. (setq attr (cond (floatp "width=0.7\\textwidth")
  2097. (wrapp "width=0.48\\textwidth")
  2098. (t attr))))
  2099. (setq figenv
  2100. (cond
  2101. (wrapp "\\begin{wrapfigure}%placement
  2102. \\centering
  2103. \\includegraphics[%attr]{%path}
  2104. \\caption%shortn{%labelcmd%caption}
  2105. \\end{wrapfigure}")
  2106. (multicolumnp "\\begin{figure*}%placement
  2107. \\centering
  2108. \\includegraphics[%attr]{%path}
  2109. \\caption%shortn{%labelcmd%caption}
  2110. \\end{figure*}")
  2111. (floatp "\\begin{figure}%placement
  2112. \\centering
  2113. \\includegraphics[%attr]{%path}
  2114. \\caption%shortn{%labelcmd%caption}
  2115. \\end{figure}")
  2116. (t "\\includegraphics[%attr]{%path}")))
  2117. (setq figenv (mapconcat 'identity (split-string figenv "\n")
  2118. (save-excursion (beginning-of-line 1)
  2119. (looking-at "[ \t]*")
  2120. (concat "\n" (match-string 0)))))
  2121. (if (and (not label) (not caption)
  2122. (string-match "^\\\\caption{.*\n" figenv))
  2123. (setq figenv (replace-match "" t t figenv)))
  2124. (org-add-props
  2125. (org-fill-template
  2126. figenv
  2127. (list (cons "path"
  2128. (if (file-name-absolute-p path)
  2129. (expand-file-name path)
  2130. path))
  2131. (cons "attr" attr)
  2132. (cons "shortn" (if shortn (format "[%s]" shortn) ""))
  2133. (cons "labelcmd" (if label (format "\\label{%s}"
  2134. label)""))
  2135. (cons "caption" (or caption ""))
  2136. (cons "placement" (or placement ""))))
  2137. nil 'original-indentation ind)))
  2138. (defun org-export-latex-protect-amp (s)
  2139. (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
  2140. (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
  2141. t t s)))
  2142. s)
  2143. (defun org-remove-initial-hash (s)
  2144. (if (string-match "\\`#" s)
  2145. (substring s 1)
  2146. s))
  2147. (defvar org-latex-entities) ; defined below
  2148. (defvar org-latex-entities-regexp) ; defined below
  2149. (defun org-export-latex-preprocess (parameters)
  2150. "Clean stuff in the LaTeX export."
  2151. ;; Replace footnotes.
  2152. (when (plist-get parameters :footnotes)
  2153. (goto-char (point-min))
  2154. (let (ref)
  2155. (while (setq ref (org-footnote-get-next-reference))
  2156. (let* ((beg (nth 1 ref))
  2157. (lbl (car ref))
  2158. (def (nth 1 (assoc (string-to-number lbl)
  2159. (mapcar (lambda (e) (cdr e))
  2160. org-export-footnotes-seen)))))
  2161. ;; Fix body for footnotes ending on a link or a list and
  2162. ;; remove definition from buffer.
  2163. (setq def
  2164. (concat def
  2165. (if (string-match "ORG-LIST-END-MARKER\\'" def)
  2166. "\n" " ")))
  2167. (org-footnote-delete-definitions lbl)
  2168. ;; Compute string to insert (FNOTE), and protect the outside
  2169. ;; macro from further transformation. When footnote at
  2170. ;; point is referring to a previously defined footnote, use
  2171. ;; \footnotemark. Otherwise, use \footnote.
  2172. (let ((fnote (if (member lbl org-export-latex-footmark-seen)
  2173. (org-export-latex-protect-string
  2174. (format "\\footnotemark[%s]" lbl))
  2175. (push lbl org-export-latex-footmark-seen)
  2176. (concat (org-export-latex-protect-string "\\footnote{")
  2177. def
  2178. (org-export-latex-protect-string "}"))))
  2179. ;; Check if another footnote is immediately following.
  2180. ;; If so, add a separator in-between.
  2181. (sep (org-export-latex-protect-string
  2182. (if (save-excursion (goto-char (1- (nth 2 ref)))
  2183. (let ((next (org-footnote-get-next-reference)))
  2184. (and next (= (nth 1 next) (nth 2 ref)))))
  2185. org-export-latex-footnote-separator ""))))
  2186. (when (org-at-heading-p)
  2187. (setq fnote (concat (org-export-latex-protect-string "\\protect")
  2188. fnote)))
  2189. ;; Ensure a footnote at column 0 cannot end a list
  2190. ;; containing it.
  2191. (put-text-property 0 (length fnote) 'original-indentation 1000 fnote)
  2192. ;; Replace footnote reference with FNOTE and, maybe, SEP.
  2193. ;; `save-excursion' is required if there are two footnotes
  2194. ;; in a row. In that case, point would be left at the
  2195. ;; beginning of the second one, and
  2196. ;; `org-footnote-get-next-reference' would then skip it.
  2197. (goto-char beg)
  2198. (delete-region beg (nth 2 ref))
  2199. (save-excursion (insert fnote sep)))))))
  2200. ;; Remove footnote section tag for LaTeX
  2201. (goto-char (point-min))
  2202. (while (re-search-forward
  2203. (concat "^" footnote-section-tag-regexp) nil t)
  2204. (org-if-unprotected
  2205. (replace-match "")))
  2206. ;; Remove any left-over footnote definition.
  2207. (mapc (lambda (fn) (org-footnote-delete-definitions (car fn)))
  2208. org-export-footnotes-data)
  2209. (mapc (lambda (fn) (org-footnote-delete-definitions fn))
  2210. org-export-latex-footmark-seen)
  2211. ;; Preserve line breaks
  2212. (goto-char (point-min))
  2213. (while (re-search-forward "\\\\\\\\" nil t)
  2214. (add-text-properties (match-beginning 0) (match-end 0)
  2215. '(org-protected t)))
  2216. ;; Preserve latex environments
  2217. (goto-char (point-min))
  2218. (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
  2219. (org-if-unprotected
  2220. (let* ((start (progn (beginning-of-line) (point)))
  2221. (end (and (re-search-forward
  2222. (concat "^[ \t]*\\\\end{"
  2223. (regexp-quote (match-string 1))
  2224. "}") nil t)
  2225. (point-at-eol))))
  2226. (if end
  2227. (add-text-properties start end '(org-protected t))
  2228. (goto-char (point-at-eol))))))
  2229. ;; Preserve math snippets
  2230. (let* ((matchers (plist-get org-format-latex-options :matchers))
  2231. (re-list org-latex-regexps)
  2232. beg end re e m n block off)
  2233. ;; Check the different regular expressions
  2234. (while (setq e (pop re-list))
  2235. (setq m (car e) re (nth 1 e) n (nth 2 e)
  2236. block (if (nth 3 e) "\n\n" ""))
  2237. (setq off (if (member m '("$" "$1")) 1 0))
  2238. (when (and (member m matchers) (not (equal m "begin")))
  2239. (goto-char (point-min))
  2240. (while (re-search-forward re nil t)
  2241. (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
  2242. (add-text-properties beg end '(org-protected t org-latex-math t))))))
  2243. ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
  2244. (goto-char (point-min))
  2245. (let ((case-fold-search nil))
  2246. (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
  2247. (unless (eq (char-before (match-beginning 1)) ?\\)
  2248. (org-if-unprotected-1
  2249. (replace-match (org-export-latex-protect-string
  2250. (concat "\\" (match-string 1)
  2251. "{}")) t t)))))
  2252. ;; Convert blockquotes
  2253. (goto-char (point-min))
  2254. (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
  2255. (org-replace-match-keep-properties "\\begin{quote}" t t))
  2256. (goto-char (point-min))
  2257. (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
  2258. (org-replace-match-keep-properties "\\end{quote}" t t))
  2259. ;; Convert verse
  2260. (goto-char (point-min))
  2261. (while (search-forward "ORG-VERSE-START" nil t)
  2262. (org-replace-match-keep-properties "\\begin{verse}" t t)
  2263. (beginning-of-line 2)
  2264. (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
  2265. (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
  2266. (goto-char (match-end 1))
  2267. (org-replace-match-keep-properties
  2268. (org-export-latex-protect-string
  2269. (concat "\\hspace*{1cm}" (match-string 2))) t t)
  2270. (beginning-of-line 1))
  2271. (if (looking-at "[ \t]*$")
  2272. (insert (org-export-latex-protect-string "\\vspace*{1em}"))
  2273. (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
  2274. (end-of-line 1)
  2275. (insert "\\\\")))
  2276. (beginning-of-line 2))
  2277. (and (looking-at "[ \t]*ORG-VERSE-END.*")
  2278. (org-replace-match-keep-properties "\\end{verse}" t t)))
  2279. ;; Convert #+INDEX to LaTeX \\index.
  2280. (goto-char (point-min))
  2281. (let ((case-fold-search t) entry)
  2282. (while (re-search-forward
  2283. "^[ \t]*#\\+index:[ \t]*\\([^ \t\r\n].*?\\)[ \t]*$"
  2284. nil t)
  2285. (setq entry
  2286. (save-match-data
  2287. (org-export-latex-protect-string
  2288. (org-export-latex-fontify-headline (match-string 1)))))
  2289. (replace-match (format "\\index{%s}" entry) t t)))
  2290. ;; Convert center
  2291. (goto-char (point-min))
  2292. (while (search-forward "ORG-CENTER-START" nil t)
  2293. (org-replace-match-keep-properties "\\begin{center}" t t))
  2294. (goto-char (point-min))
  2295. (while (search-forward "ORG-CENTER-END" nil t)
  2296. (org-replace-match-keep-properties "\\end{center}" t t))
  2297. (run-hooks 'org-export-latex-after-blockquotes-hook)
  2298. ;; Convert horizontal rules
  2299. (goto-char (point-min))
  2300. (while (re-search-forward "^[ \t]*-\\{5,\\}[ \t]*$" nil t)
  2301. (org-if-unprotected
  2302. (replace-match (org-export-latex-protect-string "\\hrule") t t)))
  2303. ;; Protect LaTeX commands like \command[...]{...} or \command{...}
  2304. (goto-char (point-min))
  2305. (let ((re (concat
  2306. "\\\\\\([a-zA-Z]+\\*?\\)"
  2307. "\\(?:<[^<>\n]*>\\)*"
  2308. "\\(?:\\[[^][\n]*?\\]\\)*"
  2309. "\\(?:<[^<>\n]*>\\)*"
  2310. "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
  2311. (while (re-search-forward re nil t)
  2312. (unless (or
  2313. ;; Check for comment line.
  2314. (save-excursion (goto-char (match-beginning 0))
  2315. (org-in-indented-comment-line))
  2316. ;; Check if this is a defined entity, so that is may
  2317. ;; need conversion.
  2318. (org-entity-get (match-string 1))
  2319. ;; Do not protect interior of footnotes. Those have
  2320. ;; already been taken care of earlier in the function.
  2321. ;; Yet, keep looking inside them for more commands.
  2322. (and (equal (match-string 1) "footnote")
  2323. (goto-char (match-end 1))))
  2324. (add-text-properties (match-beginning 0) (match-end 0)
  2325. '(org-protected t)))))
  2326. ;; Special case for \nbsp
  2327. (goto-char (point-min))
  2328. (while (re-search-forward "\\\\nbsp\\({}\\|\\>\\)" nil t)
  2329. (org-if-unprotected
  2330. (replace-match (org-export-latex-protect-string "~"))))
  2331. ;; Protect LaTeX entities
  2332. (goto-char (point-min))
  2333. (while (re-search-forward org-latex-entities-regexp nil t)
  2334. (org-if-unprotected
  2335. (add-text-properties (match-beginning 0) (match-end 0)
  2336. '(org-protected t))))
  2337. ;; Replace radio links
  2338. (goto-char (point-min))
  2339. (while (re-search-forward
  2340. (concat "<<<?" org-export-latex-all-targets-re
  2341. ">>>?\\((INVISIBLE)\\)?") nil t)
  2342. (org-if-unprotected-at (+ (match-beginning 0) 2)
  2343. (replace-match
  2344. (concat
  2345. (org-export-latex-protect-string
  2346. (format "\\label{%s}" (save-match-data (org-solidify-link-text
  2347. (match-string 1)))))
  2348. (if (match-string 2) "" (match-string 1)))
  2349. t t)))
  2350. ;; Delete @<...> constructs
  2351. ;; Thanks to Daniel Clemente for this regexp
  2352. (goto-char (point-min))
  2353. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  2354. (org-if-unprotected
  2355. (replace-match ""))))
  2356. (defun org-export-latex-fix-inputenc ()
  2357. "Set the coding system in inputenc to what the buffer is."
  2358. (let* ((cs buffer-file-coding-system)
  2359. (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs))
  2360. "utf8")))
  2361. (when opt
  2362. ;; Translate if that is requested
  2363. (setq opt (or (cdr (assoc opt org-export-latex-inputenc-alist)) opt))
  2364. ;; find the \usepackage statement and replace the option
  2365. (goto-char (point-min))
  2366. (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  2367. nil t)
  2368. (goto-char (match-beginning 1))
  2369. (delete-region (match-beginning 1) (match-end 1))
  2370. (insert opt))
  2371. (and buffer-file-name
  2372. (save-buffer)))))
  2373. ;;; List handling:
  2374. (defun org-export-latex-lists ()
  2375. "Convert plain text lists in current buffer into LaTeX lists."
  2376. ;; `org-list-end-re' output has changed since preprocess from
  2377. ;; org-exp.el. Make sure it is taken into account.
  2378. (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
  2379. (mapc
  2380. (lambda (e)
  2381. ;; For each type of context allowed for list export (E), find
  2382. ;; every list, parse it, delete it and insert resulting
  2383. ;; conversion to latex (RES), while keeping the same
  2384. ;; `original-indentation' property.
  2385. (let (res)
  2386. (goto-char (point-min))
  2387. (while (re-search-forward (org-item-beginning-re) nil t)
  2388. (when (and (eq (get-text-property (point) 'list-context) e)
  2389. (not (get-text-property (point) 'org-example)))
  2390. (beginning-of-line)
  2391. (setq res
  2392. (org-list-to-latex
  2393. ;; Narrowing is needed because we're converting
  2394. ;; from inner functions to outer ones.
  2395. (save-restriction
  2396. (narrow-to-region (point) (point-max))
  2397. (org-list-parse-list t))
  2398. org-export-latex-list-parameters))
  2399. ;; Extend previous value of original-indentation to the
  2400. ;; whole string
  2401. (insert (org-add-props res nil 'original-indentation
  2402. (org-find-text-property-in-string
  2403. 'original-indentation res)))))))
  2404. ;; List of allowed contexts for export, and the default one.
  2405. (append org-list-export-context '(nil)))))
  2406. (defconst org-latex-entities
  2407. '("\\!"
  2408. "\\'"
  2409. "\\+"
  2410. "\\,"
  2411. "\\-"
  2412. "\\:"
  2413. "\\;"
  2414. "\\<"
  2415. "\\="
  2416. "\\>"
  2417. "\\Huge"
  2418. "\\LARGE"
  2419. "\\Large"
  2420. "\\Styles"
  2421. "\\\\"
  2422. "\\`"
  2423. "\\\""
  2424. "\\addcontentsline"
  2425. "\\address"
  2426. "\\addtocontents"
  2427. "\\addtocounter"
  2428. "\\addtolength"
  2429. "\\addvspace"
  2430. "\\alph"
  2431. "\\appendix"
  2432. "\\arabic"
  2433. "\\author"
  2434. "\\begin{array}"
  2435. "\\begin{center}"
  2436. "\\begin{description}"
  2437. "\\begin{enumerate}"
  2438. "\\begin{eqnarray}"
  2439. "\\begin{equation}"
  2440. "\\begin{figure}"
  2441. "\\begin{flushleft}"
  2442. "\\begin{flushright}"
  2443. "\\begin{itemize}"
  2444. "\\begin{list}"
  2445. "\\begin{minipage}"
  2446. "\\begin{picture}"
  2447. "\\begin{quotation}"
  2448. "\\begin{quote}"
  2449. "\\begin{tabbing}"
  2450. "\\begin{table}"
  2451. "\\begin{tabular}"
  2452. "\\begin{thebibliography}"
  2453. "\\begin{theorem}"
  2454. "\\begin{titlepage}"
  2455. "\\begin{verbatim}"
  2456. "\\begin{verse}"
  2457. "\\bf"
  2458. "\\bf"
  2459. "\\bibitem"
  2460. "\\bigskip"
  2461. "\\cdots"
  2462. "\\centering"
  2463. "\\circle"
  2464. "\\cite"
  2465. "\\cleardoublepage"
  2466. "\\clearpage"
  2467. "\\cline"
  2468. "\\closing"
  2469. "\\dashbox"
  2470. "\\date"
  2471. "\\ddots"
  2472. "\\dotfill"
  2473. "\\em"
  2474. "\\fbox"
  2475. "\\flushbottom"
  2476. "\\fnsymbol"
  2477. "\\footnote"
  2478. "\\footnotemark"
  2479. "\\footnotesize"
  2480. "\\footnotetext"
  2481. "\\frac"
  2482. "\\frame"
  2483. "\\framebox"
  2484. "\\hfill"
  2485. "\\hline"
  2486. "\\hrulespace"
  2487. "\\hspace"
  2488. "\\huge"
  2489. "\\hyphenation"
  2490. "\\include"
  2491. "\\includeonly"
  2492. "\\indent"
  2493. "\\input"
  2494. "\\it"
  2495. "\\kill"
  2496. "\\label"
  2497. "\\large"
  2498. "\\ldots"
  2499. "\\line"
  2500. "\\linebreak"
  2501. "\\linethickness"
  2502. "\\listoffigures"
  2503. "\\listoftables"
  2504. "\\location"
  2505. "\\makebox"
  2506. "\\maketitle"
  2507. "\\mark"
  2508. "\\mbox"
  2509. "\\medskip"
  2510. "\\multicolumn"
  2511. "\\multiput"
  2512. "\\newcommand"
  2513. "\\newcounter"
  2514. "\\newenvironment"
  2515. "\\newfont"
  2516. "\\newlength"
  2517. "\\newline"
  2518. "\\newpage"
  2519. "\\newsavebox"
  2520. "\\newtheorem"
  2521. "\\nocite"
  2522. "\\nofiles"
  2523. "\\noindent"
  2524. "\\nolinebreak"
  2525. "\\nopagebreak"
  2526. "\\normalsize"
  2527. "\\onecolumn"
  2528. "\\opening"
  2529. "\\oval"
  2530. "\\overbrace"
  2531. "\\overline"
  2532. "\\pagebreak"
  2533. "\\pagenumbering"
  2534. "\\pageref"
  2535. "\\pagestyle"
  2536. "\\par"
  2537. "\\parbox"
  2538. "\\put"
  2539. "\\raggedbottom"
  2540. "\\raggedleft"
  2541. "\\raggedright"
  2542. "\\raisebox"
  2543. "\\ref"
  2544. "\\rm"
  2545. "\\roman"
  2546. "\\rule"
  2547. "\\savebox"
  2548. "\\sc"
  2549. "\\scriptsize"
  2550. "\\setcounter"
  2551. "\\setlength"
  2552. "\\settowidth"
  2553. "\\sf"
  2554. "\\shortstack"
  2555. "\\signature"
  2556. "\\sl"
  2557. "\\small"
  2558. "\\smallskip"
  2559. "\\sqrt"
  2560. "\\tableofcontents"
  2561. "\\telephone"
  2562. "\\thanks"
  2563. "\\thispagestyle"
  2564. "\\tiny"
  2565. "\\title"
  2566. "\\tt"
  2567. "\\twocolumn"
  2568. "\\typein"
  2569. "\\typeout"
  2570. "\\underbrace"
  2571. "\\underline"
  2572. "\\usebox"
  2573. "\\usecounter"
  2574. "\\value"
  2575. "\\vdots"
  2576. "\\vector"
  2577. "\\verb"
  2578. "\\vfill"
  2579. "\\vline"
  2580. "\\vspace")
  2581. "A list of LaTeX commands to be protected when performing conversion.")
  2582. (defconst org-latex-entities-regexp
  2583. (let (names rest)
  2584. (dolist (x org-latex-entities)
  2585. (if (string-match "[a-zA-Z]$" x)
  2586. (push x names)
  2587. (push x rest)))
  2588. (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
  2589. "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
  2590. (provide 'org-export-latex)
  2591. (provide 'org-latex)
  2592. ;;; org-latex.el ends here