org-latex.el 89 KB

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