org-latex.el 87 KB

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