org-latex.el 73 KB

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