org-latex.el 106 KB

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