org-latex.el 103 KB

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