org-latex.el 102 KB

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