org-latex.el 102 KB

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