org-latex.el 104 KB

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