org-latex.el 74 KB

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