org-latex.el 106 KB

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