org-latex.el 88 KB

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