org-latex.el 81 KB

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