org-latex.el 88 KB

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