org-latex.el 86 KB

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