org-latex.el 106 KB

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