org-latex.el 100 KB

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