org-latex.el 85 KB

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