org-latex.el 62 KB

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