org-latex.el 84 KB

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