org-latex.el 73 KB

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