org-export-latex.el 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. ;;; org-export-latex.el --- LaTeX exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-export-latex.el
  7. ;; Version: 6.09a
  8. ;; Author: Bastien Guerry <bzg AT altern DOT org>
  9. ;; Maintainer: Bastien Guerry <bzg AT altern DOT org>
  10. ;; Keywords: org, wp, tex
  11. ;; Description: Converts an org-mode buffer into LaTeX
  12. ;; URL: http://www.cognition.ens.fr/~guerry/u/org-export-latex.el
  13. ;; This file is part of GNU Emacs.
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;; Commentary:
  25. ;;
  26. ;; This library implements a LaTeX exporter for org-mode.
  27. ;;
  28. ;; Put this file into your load-path and the following into your ~/.emacs:
  29. ;; (require 'org-export-latex)
  30. ;;
  31. ;; The interactive functions are similar to those of the HTML exporter:
  32. ;;
  33. ;; M-x `org-export-as-latex'
  34. ;; M-x `org-export-as-pdf'
  35. ;; M-x `org-export-as-pdf-and-open'
  36. ;; M-x `org-export-as-latex-batch'
  37. ;; M-x `org-export-as-latex-to-buffer'
  38. ;; M-x `org-export-region-as-latex'
  39. ;; M-x `org-replace-region-by-latex'
  40. ;;
  41. ;;; Code:
  42. (eval-when-compile
  43. (require 'cl))
  44. (require 'footnote)
  45. (require 'org)
  46. (require 'org-exp)
  47. ;;; Variables:
  48. (defvar org-export-latex-class nil)
  49. (defvar org-export-latex-header nil)
  50. (defvar org-export-latex-append-header nil)
  51. (defvar org-export-latex-options-plist nil)
  52. (defvar org-export-latex-todo-keywords-1 nil)
  53. (defvar org-export-latex-all-targets-re nil)
  54. (defvar org-export-latex-add-level 0)
  55. (defvar org-export-latex-sectioning "")
  56. (defvar org-export-latex-sectioning-depth 0)
  57. (defvar org-export-latex-special-string-regexps
  58. '(org-ts-regexp
  59. org-scheduled-string
  60. org-deadline-string
  61. org-clock-string)
  62. "A list of regexps to convert as special keywords.")
  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,a4paper]{article}
  78. \\usepackage[utf8]{inputenc}
  79. \\usepackage[T1]{fontenc}
  80. \\usepackage{hyperref}"
  81. ("\\section{%s}" . "\\section*{%s}")
  82. ("\\subsection{%s}" . "\\subsection*{%s}")
  83. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  84. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  85. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  86. ("report"
  87. "\\documentclass[11pt,a4paper]{report}
  88. \\usepackage[utf8]{inputenc}
  89. \\usepackage[T1]{fontenc}
  90. \\usepackage{hyperref}"
  91. ("\\part{%s}" . "\\part*{%s}")
  92. ("\\chapter{%s}" . "\\chapter*{%s}")
  93. ("\\section{%s}" . "\\section*{%s}")
  94. ("\\subsection{%s}" . "\\subsection*{%s}")
  95. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  96. ("book"
  97. "\\documentclass[11pt,a4paper]{book}
  98. \\usepackage[utf8]{inputenc}
  99. \\usepackage[T1]{fontenc}
  100. \\usepackage{hyperref}"
  101. ("\\part{%s}" . "\\part*{%s}")
  102. ("\\chapter{%s}" . "\\chapter*{%s}")
  103. ("\\section{%s}" . "\\section*{%s}")
  104. ("\\subsection{%s}" . "\\subsection*{%s}")
  105. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  106. "Alist of LaTeX classes and associated header and structure.
  107. If #+LaTeX_CLASS is set in the buffer, use its value and the
  108. associated information. Here is the structure of each cell:
  109. \(class-name
  110. header-string
  111. (numbered-section . unnumbered-section\)
  112. ...\)
  113. A %s formatter is mandatory in each section string and will be
  114. replaced by the title of the section.
  115. Instead of a cons cell (numbered . unnumbered), you can also provide a list
  116. of 2-4 elements,
  117. (numbered-open numbered-close)
  118. or
  119. (numbered-open numbered-close unnumbered-open unnumbered-close)
  120. providing opening and closing strings for an environment that should
  121. represent the document section. The opening clause should have a %s
  122. to represent the section title."
  123. :group 'org-export-latex
  124. :type '(repeat
  125. (list (string :tag "LaTeX class")
  126. (string :tag "LaTeX header")
  127. (repeat :tag "Levels" :inline t
  128. (choice
  129. (cons :tag "Heading"
  130. (string :tag "numbered")
  131. (string :tag "unnumbered)"))
  132. (list :tag "Environment"
  133. (string :tag "Opening (numbered) ")
  134. (string :tag "Closing (numbered) ")
  135. (string :tag "Opening (unnumbered)")
  136. (string :tag "Closing (unnumbered)")))))))
  137. (defcustom org-export-latex-emphasis-alist
  138. '(("*" "\\textbf{%s}" nil)
  139. ("/" "\\emph{%s}" nil)
  140. ("_" "\\underline{%s}" nil)
  141. ("+" "\\texttt{%s}" nil)
  142. ("=" "\\texttt{%s}" nil)
  143. ("~" "\\texttt{%s}" t))
  144. "Alist of LaTeX expressions to convert emphasis fontifiers.
  145. Each element of the list is a list of three elements.
  146. The first element is the character used as a marker for fontification.
  147. The second element is a formatting string to wrap fontified text with.
  148. The third element decides whether to protect converted text from other
  149. conversions."
  150. :group 'org-export-latex
  151. :type 'alist)
  152. (defcustom org-export-latex-title-command "\\maketitle"
  153. "The command used to insert the title just after \\begin{document}.
  154. If this string contains the formatting specification \"%s\" then
  155. it will be used as a formatting string, passing the title as an
  156. argument."
  157. :group 'org-export-latex
  158. :type 'string)
  159. (defcustom org-export-latex-date-format
  160. "%d %B %Y"
  161. "Format string for \\date{...}."
  162. :group 'org-export-latex
  163. :type 'string)
  164. (defcustom org-export-latex-tables-verbatim nil
  165. "When non-nil, tables are exported verbatim."
  166. :group 'org-export-latex
  167. :type 'boolean)
  168. (defcustom org-export-latex-tables-column-borders nil
  169. "When non-nil, group of columns are surrounded with borders."
  170. :group 'org-export-latex
  171. :type 'boolean)
  172. (defcustom org-export-latex-packages-alist nil
  173. "Alist of packages to be inserted in the header.
  174. Each cell is of the forma \( \"option\" . \"package\" \)."
  175. :group 'org-export-latex
  176. :type 'alist)
  177. (defcustom org-export-latex-low-levels 'description
  178. "How to convert sections below the current level of sectioning.
  179. This is specified by the `org-export-headline-levels' option or the
  180. value of \"H:\" in Org's #+OPTION line.
  181. This can be either nil (skip the sections), 'description (convert
  182. the sections as descriptive lists) or a string to be used instead
  183. of \\section{%s}. In this latter case, the %s stands here for the
  184. inserted headline and is mandatory."
  185. :group 'org-export-latex
  186. :type '(choice (const :tag "Ignore" nil)
  187. (symbol :tag "Convert as descriptive list" description)
  188. (string :tag "Use a section string" :value "\\subparagraph{%s}")))
  189. (defcustom org-export-latex-remove-from-headlines
  190. '(:todo t :priority t :tags t)
  191. "A plist of keywords to remove from headlines.
  192. Non-nil means remove this keyword type from the headline.
  193. Don't remove the keys, just change their values."
  194. :type 'plist
  195. :group 'org-export-latex)
  196. (defcustom org-export-latex-image-default-option "width=10em"
  197. "Default option for images."
  198. :group 'org-export-latex
  199. :type 'string)
  200. (defcustom org-export-latex-coding-system nil
  201. "Coding system for the exported LaTex file."
  202. :group 'org-export-latex
  203. :type 'coding-system)
  204. (defgroup org-export-pdf nil
  205. "Options for exporting Org-mode files to PDF, via LaTeX."
  206. :tag "Org Export LaTeX"
  207. :group 'org-export-latex
  208. :group 'org-export)
  209. (defcustom org-export-pdf-remove-logfiles t
  210. "Non-nil means, remove the logfiles produced by PDF production.
  211. These are the .aux, .log, .out, and .toc files."
  212. :group 'org-export-latex
  213. :type 'boolean)
  214. ;;; Autoload functions:
  215. ;;;###autoload
  216. (defun org-export-as-latex-batch ()
  217. "Call `org-export-as-latex', may be used in batch processing.
  218. For example:
  219. emacs --batch
  220. --load=$HOME/lib/emacs/org.el
  221. --eval \"(setq org-export-headline-levels 2)\"
  222. --visit=MyFile --funcall org-export-as-latex-batch"
  223. (org-export-as-latex org-export-headline-levels 'hidden))
  224. ;;;###autoload
  225. (defun org-export-as-latex-to-buffer (arg)
  226. "Call `org-exort-as-latex` with output to a temporary buffer.
  227. No file is created. The prefix ARG is passed through to `org-export-as-latex'."
  228. (interactive "P")
  229. (org-export-as-latex arg nil nil "*Org LaTeX Export*")
  230. (switch-to-buffer-other-window "*Org LaTeX Export*"))
  231. ;;;###autoload
  232. (defun org-replace-region-by-latex (beg end)
  233. "Replace the region from BEG to END with its LaTeX export.
  234. It assumes the region has `org-mode' syntax, and then convert it to
  235. LaTeX. This can be used in any buffer. For example, you could
  236. write an itemized list in `org-mode' syntax in an LaTeX buffer and
  237. then use this command to convert it."
  238. (interactive "r")
  239. (let (reg latex buf)
  240. (save-window-excursion
  241. (if (org-mode-p)
  242. (setq latex (org-export-region-as-latex
  243. beg end t 'string))
  244. (setq reg (buffer-substring beg end)
  245. buf (get-buffer-create "*Org tmp*"))
  246. (save-excursion
  247. (set-buffer buf)
  248. (erase-buffer)
  249. (insert reg)
  250. (org-mode)
  251. (setq latex (org-export-region-as-latex
  252. (point-min) (point-max) t 'string)))
  253. (kill-buffer buf)))
  254. (delete-region beg end)
  255. (insert latex)))
  256. ;;;###autoload
  257. (defun org-export-region-as-latex (beg end &optional body-only buffer)
  258. "Convert region from BEG to END in `org-mode' buffer to LaTeX.
  259. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  260. contents, and only produce the region of converted text, useful for
  261. cut-and-paste operations.
  262. If BUFFER is a buffer or a string, use/create that buffer as a target
  263. of the converted LaTeX. If BUFFER is the symbol `string', return the
  264. produced LaTeX as a string and leave not buffer behind. For example,
  265. a Lisp program could call this function in the following way:
  266. (setq latex (org-export-region-as-latex beg end t 'string))
  267. When called interactively, the output buffer is selected, and shown
  268. in a window. A non-interactive call will only retunr the buffer."
  269. (interactive "r\nP")
  270. (when (interactive-p)
  271. (setq buffer "*Org LaTeX Export*"))
  272. (let ((transient-mark-mode t) (zmacs-regions t)
  273. rtn)
  274. (goto-char end)
  275. (set-mark (point)) ;; to activate the region
  276. (goto-char beg)
  277. (setq rtn (org-export-as-latex
  278. nil nil nil
  279. buffer body-only))
  280. (if (fboundp 'deactivate-mark) (deactivate-mark))
  281. (if (and (interactive-p) (bufferp rtn))
  282. (switch-to-buffer-other-window rtn)
  283. rtn)))
  284. ;;;###autoload
  285. (defun org-export-as-latex (arg &optional hidden ext-plist
  286. to-buffer body-only pub-dir)
  287. "Export current buffer to a LaTeX file.
  288. If there is an active region, export only the region. The prefix
  289. ARG specifies how many levels of the outline should become
  290. headlines. The default is 3. Lower levels will be exported
  291. depending on `org-export-latex-low-levels'. The default is to
  292. convert them as description lists. When HIDDEN is non-nil, don't
  293. display the LaTeX buffer. EXT-PLIST is a property list with
  294. external parameters overriding org-mode's default settings, but
  295. still inferior to file-local settings. When TO-BUFFER is
  296. non-nil, create a buffer with that name and export to that
  297. buffer. If TO-BUFFER is the symbol `string', don't leave any
  298. buffer behind but just return the resulting LaTeX as a string.
  299. When BODY-ONLY is set, don't produce the file header and footer,
  300. simply return the content of \begin{document}...\end{document},
  301. without even the \begin{document} and \end{document} commands.
  302. when PUB-DIR is set, use this as the publishing directory."
  303. (interactive "P")
  304. ;; Make sure we have a file name when we need it.
  305. (when (and (not (or to-buffer body-only))
  306. (not buffer-file-name))
  307. (if (buffer-base-buffer)
  308. (org-set-local 'buffer-file-name
  309. (with-current-buffer (buffer-base-buffer)
  310. buffer-file-name))
  311. (error "Need a file name to be able to export")))
  312. (message "Exporting to LaTeX...")
  313. (org-update-radio-target-regexp)
  314. (org-export-latex-set-initial-vars ext-plist arg)
  315. (let* ((wcf (current-window-configuration))
  316. (opt-plist org-export-latex-options-plist)
  317. (region-p (org-region-active-p))
  318. (rbeg (and region-p (region-beginning)))
  319. (rend (and region-p (region-end)))
  320. (subtree-p
  321. (when region-p
  322. (save-excursion
  323. (goto-char rbeg)
  324. (and (org-at-heading-p)
  325. (>= (org-end-of-subtree t t) rend)))))
  326. (opt-plist (if subtree-p
  327. (org-export-add-subtree-options opt-plist rbeg)
  328. opt-plist))
  329. ;; Make sure the variable contains the updated values.
  330. (org-export-latex-options-plist opt-plist)
  331. (title (or (and subtree-p (org-export-get-title-from-subtree))
  332. (plist-get opt-plist :title)
  333. (and (not
  334. (plist-get opt-plist :skip-before-1st-heading))
  335. (org-export-grab-title-from-buffer))
  336. (file-name-sans-extension
  337. (file-name-nondirectory buffer-file-name))))
  338. (filename (concat (file-name-as-directory
  339. (or pub-dir
  340. (org-export-directory :LaTeX ext-plist)))
  341. (file-name-sans-extension
  342. (or (and subtree-p
  343. (org-entry-get rbeg "EXPORT_FILE_NAME" t))
  344. (file-name-nondirectory ;sans-extension
  345. buffer-file-name)))
  346. ".tex"))
  347. (filename (if (equal (file-truename filename)
  348. (file-truename buffer-file-name))
  349. (concat filename ".tex")
  350. filename))
  351. (buffer (if to-buffer
  352. (cond
  353. ((eq to-buffer 'string) (get-buffer-create
  354. "*Org LaTeX Export*"))
  355. (t (get-buffer-create to-buffer)))
  356. (find-file-noselect filename)))
  357. (odd org-odd-levels-only)
  358. (header (org-export-latex-make-header title opt-plist))
  359. (skip (cond (subtree-p nil)
  360. (region-p t)
  361. ;; never skip first lines when exporting a subtree
  362. (t (plist-get opt-plist :skip-before-1st-heading))))
  363. (text (plist-get opt-plist :text))
  364. (first-lines (if skip "" (org-export-latex-first-lines)))
  365. (coding-system (and (boundp 'buffer-file-coding-system)
  366. buffer-file-coding-system))
  367. (coding-system-for-write (or org-export-latex-coding-system
  368. coding-system))
  369. (save-buffer-coding-system (or org-export-latex-coding-system
  370. coding-system))
  371. (region (buffer-substring
  372. (if region-p (region-beginning) (point-min))
  373. (if region-p (region-end) (point-max))))
  374. (string-for-export
  375. (org-export-preprocess-string
  376. region :emph-multiline t
  377. :for-LaTeX t
  378. :comments nil
  379. :add-text (if (eq to-buffer 'string) nil text)
  380. :skip-before-1st-heading skip
  381. :select-tags (plist-get opt-plist :select-tags)
  382. :exclude-tags (plist-get opt-plist :exclude-tags)
  383. :LaTeX-fragments nil)))
  384. (set-buffer buffer)
  385. (erase-buffer)
  386. (and (fboundp 'set-buffer-file-coding-system)
  387. (set-buffer-file-coding-system coding-system-for-write))
  388. ;; insert the header and initial document commands
  389. (unless (or (eq to-buffer 'string) body-only)
  390. (insert header))
  391. ;; insert text found in #+TEXT
  392. (when (and text (not (eq to-buffer 'string)))
  393. (insert (org-export-latex-content
  394. text '(lists tables fixed-width keywords))
  395. "\n\n"))
  396. ;; insert lines before the first headline
  397. (unless (or skip (eq to-buffer 'string))
  398. (insert first-lines))
  399. ;; handle the case where the region does not begin with a section
  400. (when region-p
  401. (insert (with-temp-buffer
  402. (insert string-for-export)
  403. (org-export-latex-first-lines))))
  404. ;; export the content of headlines
  405. (org-export-latex-global
  406. (with-temp-buffer
  407. (insert string-for-export)
  408. (goto-char (point-min))
  409. (when (re-search-forward "^\\(\\*+\\) " nil t)
  410. (let* ((asters (length (match-string 1)))
  411. (level (if odd (- asters 2) (- asters 1))))
  412. (setq org-export-latex-add-level
  413. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  414. (org-export-latex-parse-global level odd)))))
  415. ;; finalization
  416. (unless body-only (insert "\n\\end{document}"))
  417. (or to-buffer (save-buffer))
  418. (goto-char (point-min))
  419. (message "Exporting to LaTeX...done")
  420. (prog1
  421. (if (eq to-buffer 'string)
  422. (prog1 (buffer-substring (point-min) (point-max))
  423. (kill-buffer (current-buffer)))
  424. (current-buffer))
  425. (set-window-configuration wcf))))
  426. ;;;###autoload
  427. (defun org-export-as-pdf (arg &optional hidden ext-plist
  428. to-buffer body-only pub-dir)
  429. "Export as LaTeX, then process through to PDF."
  430. (interactive "P")
  431. (message "Exporting to PDF...")
  432. (let* ((wconfig (current-window-configuration))
  433. (lbuf (org-export-as-latex arg hidden ext-plist
  434. to-buffer body-only pub-dir))
  435. (file (buffer-file-name lbuf))
  436. (base (file-name-sans-extension (buffer-file-name lbuf)))
  437. (pdffile (concat base ".pdf")))
  438. (and (file-exists-p pdffile) (delete-file pdffile))
  439. (message "Processing LaTeX file...")
  440. (shell-command (format "pdflatex -interaction nonstopmode %s;pdflatex -interaction nonstopmode %s" file file))
  441. (message "Processing LaTeX file...done")
  442. (if (not (file-exists-p pdffile))
  443. (error "PDF file was not produced")
  444. (set-window-configuration wconfig)
  445. (when org-export-pdf-remove-logfiles
  446. (dolist (ext '("aux" "log" "out" "toc"))
  447. (setq file (concat base "." ext))
  448. (and (file-exists-p file) (delete-file file))))
  449. (message "Exporting to PDF...done")
  450. pdffile)))
  451. ;;;###autoload
  452. (defun org-export-as-pdf-and-open (arg)
  453. "Export as LaTeX, then process through to PDF, and open."
  454. (interactive "P")
  455. (let ((pdffile (org-export-as-pdf arg)))
  456. (if pdffile
  457. (org-open-file pdffile)
  458. (error "PDF file was not produced"))))
  459. ;;; Parsing functions:
  460. (defun org-export-latex-parse-global (level odd)
  461. "Parse the current buffer recursively, starting at LEVEL.
  462. If ODD is non-nil, assume the buffer only contains odd sections.
  463. Return a list reflecting the document structure."
  464. (save-excursion
  465. (goto-char (point-min))
  466. (let* ((cnt 0) output
  467. (depth org-export-latex-sectioning-depth))
  468. (while (re-search-forward
  469. (concat "^\\(\\(?:\\*\\)\\{"
  470. (number-to-string (+ (if odd 2 1) level))
  471. "\\}\\) \\(.*\\)$")
  472. ;; make sure that there is no upper heading
  473. (when (> level 0)
  474. (save-excursion
  475. (save-match-data
  476. (re-search-forward
  477. (concat "^\\(\\(?:\\*\\)\\{"
  478. (number-to-string level)
  479. "\\}\\) \\(.*\\)$") nil t)))) t)
  480. (setq cnt (1+ cnt))
  481. (let* ((pos (match-beginning 0))
  482. (heading (match-string 2))
  483. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  484. (save-excursion
  485. (narrow-to-region
  486. (point)
  487. (save-match-data
  488. (if (re-search-forward
  489. (concat "^\\(\\(?:\\*\\)\\{"
  490. (number-to-string (+ (if odd 2 1) level))
  491. "\\}\\) \\(.*\\)$") nil t)
  492. (match-beginning 0)
  493. (point-max))))
  494. (goto-char (point-min))
  495. (setq output
  496. (append output
  497. (list
  498. (list
  499. `(pos . ,pos)
  500. `(level . ,nlevel)
  501. `(occur . ,cnt)
  502. `(heading . ,heading)
  503. `(content . ,(org-export-latex-parse-content))
  504. `(subcontent . ,(org-export-latex-parse-subcontent
  505. level odd)))))))
  506. (widen)))
  507. (list output))))
  508. (defun org-export-latex-parse-content ()
  509. "Extract the content of a section."
  510. (let ((beg (point))
  511. (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
  512. (progn (beginning-of-line) (point))
  513. (point-max))))
  514. (buffer-substring beg end)))
  515. (defun org-export-latex-parse-subcontent (level odd)
  516. "Extract the subcontent of a section at LEVEL.
  517. If ODD Is non-nil, assume subcontent only contains odd sections."
  518. (if (not (re-search-forward
  519. (concat "^\\(\\(?:\\*\\)\\{"
  520. (number-to-string (+ (if odd 4 2) level))
  521. "\\}\\) \\(.*\\)$")
  522. nil t))
  523. nil ; subcontent is nil
  524. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  525. ;;; Rendering functions:
  526. (defun org-export-latex-global (content)
  527. "Export CONTENT to LaTeX.
  528. CONTENT is an element of the list produced by
  529. `org-export-latex-parse-global'."
  530. (if (eq (car content) 'subcontent)
  531. (mapc 'org-export-latex-sub (cdr content))
  532. (org-export-latex-sub (car content))))
  533. (defun org-export-latex-sub (subcontent)
  534. "Export the list SUBCONTENT to LaTeX.
  535. SUBCONTENT is an alist containing information about the headline
  536. and its content."
  537. (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
  538. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  539. (defun org-export-latex-subcontent (subcontent num)
  540. "Export each cell of SUBCONTENT to LaTeX.
  541. If NUM, export sections as numerical sections."
  542. (let* ((heading (org-export-latex-fontify-headline
  543. (cdr (assoc 'heading subcontent))))
  544. (level (- (cdr (assoc 'level subcontent))
  545. org-export-latex-add-level))
  546. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  547. (content (cdr (assoc 'content subcontent)))
  548. (subcontent (cadr (assoc 'subcontent subcontent)))
  549. (label (org-get-text-property-any 0 'target heading)))
  550. (cond
  551. ;; Normal conversion
  552. ((<= level org-export-latex-sectioning-depth)
  553. (let* ((sec (nth (1- level) org-export-latex-sectioning))
  554. start end)
  555. (if (consp (cdr sec))
  556. (setq start (nth (if num 0 2) sec)
  557. end (nth (if num 1 3) sec))
  558. (setq start (if num (car sec) (cdr sec))))
  559. (insert (format start heading) "\n")
  560. (when label (insert (format "\\label{%s}\n" label)))
  561. (insert (org-export-latex-content content))
  562. (cond ((stringp subcontent) (insert subcontent))
  563. ((listp subcontent) (org-export-latex-sub subcontent)))
  564. (if end (insert end "\n"))))
  565. ;; At a level under the hl option: we can drop this subsection
  566. ((> level org-export-latex-sectioning-depth)
  567. (cond ((eq org-export-latex-low-levels 'description)
  568. (insert (format "\\begin{description}\n\n\\item[%s]%s\n\n"
  569. heading
  570. (if label (format "\\label{%s}" label) "")))
  571. (insert (org-export-latex-content content))
  572. (cond ((stringp subcontent) (insert subcontent))
  573. ((listp subcontent) (org-export-latex-sub subcontent)))
  574. (insert "\\end{description}\n"))
  575. ((stringp org-export-latex-low-levels)
  576. (insert (format org-export-latex-low-levels heading) "\n")
  577. (when label (insert (format "\\label{%s}\n" label)))
  578. (insert (org-export-latex-content content))
  579. (cond ((stringp subcontent) (insert subcontent))
  580. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  581. ;;; Exporting internals:
  582. (defun org-export-latex-set-initial-vars (ext-plist level)
  583. "Store org local variables required for LaTeX export.
  584. EXT-PLIST is an optional additional plist.
  585. LEVEL indicates the default depth for export."
  586. (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
  587. org-export-latex-all-targets-re
  588. (org-make-target-link-regexp (org-all-targets))
  589. org-export-latex-options-plist
  590. (org-combine-plists (org-default-export-plist) ext-plist
  591. (org-infile-export-plist))
  592. org-export-latex-class
  593. (save-excursion
  594. (goto-char (point-min))
  595. (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
  596. (assoc (match-string 1) org-export-latex-classes))
  597. (match-string 1)
  598. org-export-latex-default-class))
  599. org-export-latex-header
  600. (cadr (assoc org-export-latex-class org-export-latex-classes))
  601. org-export-latex-sectioning
  602. (cddr (assoc org-export-latex-class org-export-latex-classes))
  603. org-export-latex-sectioning-depth
  604. (or level
  605. (let ((hl-levels
  606. (plist-get org-export-latex-options-plist :headline-levels))
  607. (sec-depth (length org-export-latex-sectioning)))
  608. (if (> hl-levels sec-depth) sec-depth hl-levels)))))
  609. (defun org-export-latex-make-header (title opt-plist)
  610. "Make the LaTeX header and return it as a string.
  611. TITLE is the current title from the buffer or region.
  612. OPT-PLIST is the options plist for current buffer."
  613. (let ((toc (plist-get opt-plist :table-of-contents))
  614. (author (plist-get opt-plist :author)))
  615. (concat
  616. (if (plist-get opt-plist :time-stamp-file)
  617. (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
  618. ;; insert LaTeX custom header
  619. org-export-latex-header
  620. "\n"
  621. ;; insert information on LaTeX packages
  622. (when org-export-latex-packages-alist
  623. (mapconcat (lambda(p)
  624. (if (equal "" (car p))
  625. (format "\\usepackage{%s}" (cadr p))
  626. (format "\\usepackage[%s]{%s}"
  627. (car p) (cadr p))))
  628. org-export-latex-packages-alist "\n"))
  629. ;; insert additional commands in the header
  630. (plist-get opt-plist :latex-header-extra)
  631. org-export-latex-append-header
  632. ;; insert the title
  633. (format
  634. "\n\n\\title{%s}\n"
  635. ;; convert the title
  636. (org-export-latex-content
  637. title '(lists tables fixed-width keywords)))
  638. ;; insert author info
  639. (if (plist-get opt-plist :author-info)
  640. (format "\\author{%s}\n"
  641. (or author user-full-name))
  642. (format "%%\\author{%s}\n"
  643. (or author user-full-name)))
  644. ;; insert the date
  645. (format "\\date{%s}\n"
  646. (format-time-string
  647. (or (plist-get opt-plist :date)
  648. org-export-latex-date-format)))
  649. ;; beginning of the document
  650. "\n\\begin{document}\n\n"
  651. ;; insert the title command
  652. (if (string-match "%s" org-export-latex-title-command)
  653. (format org-export-latex-title-command title)
  654. org-export-latex-title-command)
  655. "\n\n"
  656. ;; table of contents
  657. (when (and org-export-with-toc
  658. (plist-get opt-plist :section-numbers))
  659. (cond ((numberp toc)
  660. (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
  661. (min toc (plist-get opt-plist :headline-levels))))
  662. (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
  663. (plist-get opt-plist :headline-levels))))))))
  664. (defun org-export-latex-first-lines (&optional comments)
  665. "Export the first lines before first headline.
  666. COMMENTS is either nil to replace them with the empty string or a
  667. formatting string like %%%%s if we want to comment them out."
  668. (save-excursion
  669. (goto-char (point-min))
  670. (if (org-at-heading-p) (beginning-of-line 2))
  671. (let* ((pt (point))
  672. (end (if (and (re-search-forward "^\\* " nil t)
  673. (not (eq pt (match-beginning 0))))
  674. (goto-char (match-beginning 0))
  675. (goto-char (point-max)))))
  676. (org-export-latex-content
  677. (org-export-preprocess-string
  678. (buffer-substring (point-min) end)
  679. :for-LaTeX t
  680. :emph-multiline t
  681. :add-text nil
  682. :comments nil
  683. :skip-before-1st-heading nil
  684. :LaTeX-fragments nil)))))
  685. (defun org-export-latex-content (content &optional exclude-list)
  686. "Convert CONTENT string to LaTeX.
  687. Don't perform conversions that are in EXCLUDE-LIST. Recognized
  688. conversion types are: quotation-marks, emphasis, sub-superscript,
  689. links, keywords, lists, tables, fixed-width"
  690. (with-temp-buffer
  691. (insert content)
  692. (unless (memq 'quotation-marks exclude-list)
  693. (org-export-latex-quotation-marks))
  694. (unless (memq 'emphasis exclude-list)
  695. (when (plist-get org-export-latex-options-plist :emphasize)
  696. (org-export-latex-fontify)))
  697. (unless (memq 'sub-superscript exclude-list)
  698. (org-export-latex-special-chars
  699. (plist-get org-export-latex-options-plist :sub-superscript)))
  700. (unless (memq 'links exclude-list)
  701. (org-export-latex-links))
  702. (unless (memq 'keywords exclude-list)
  703. (org-export-latex-keywords
  704. (plist-get org-export-latex-options-plist :timestamps)))
  705. (unless (memq 'lists exclude-list)
  706. (org-export-latex-lists))
  707. (unless (memq 'tables exclude-list)
  708. (org-export-latex-tables
  709. (plist-get org-export-latex-options-plist :tables)))
  710. (unless (memq 'fixed-width exclude-list)
  711. (org-export-latex-fixed-width
  712. (plist-get org-export-latex-options-plist :fixed-width)))
  713. ;; return string
  714. (buffer-substring (point-min) (point-max))))
  715. (defun org-export-latex-protect-string (s)
  716. "Add the org-protected property to string S."
  717. (add-text-properties 0 (length s) '(org-protected t) s) s)
  718. (defun org-export-latex-protect-char-in-string (char-list string)
  719. "Add org-protected text-property to char from CHAR-LIST in STRING."
  720. (with-temp-buffer
  721. (save-match-data
  722. (insert string)
  723. (goto-char (point-min))
  724. (while (re-search-forward (regexp-opt char-list) nil t)
  725. (add-text-properties (match-beginning 0)
  726. (match-end 0) '(org-protected t)))
  727. (buffer-string))))
  728. (defun org-export-latex-keywords-maybe (remove-list)
  729. "Maybe remove keywords depending on rules in REMOVE-LIST."
  730. (goto-char (point-min))
  731. (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
  732. (case-fold-search nil))
  733. ;; convert TODO keywords
  734. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  735. (if (plist-get remove-list :todo)
  736. (replace-match "")
  737. (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
  738. ;; convert priority string
  739. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  740. (if (plist-get remove-list :priority)
  741. (replace-match "")
  742. (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
  743. ;; convert tags
  744. (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
  745. (if (or (not org-export-with-tags)
  746. (plist-get remove-list :tags))
  747. (replace-match "")
  748. (replace-match
  749. (org-export-latex-protect-string
  750. (format "\\texttt{%s}"
  751. (save-match-data
  752. (replace-regexp-in-string
  753. "_" "\\\\_" (match-string 0)))))
  754. t t)))))
  755. (defun org-export-latex-fontify-headline (string)
  756. "Fontify special words in STRING."
  757. (with-temp-buffer
  758. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  759. ;; the beginning of the buffer - inserting "\n" is safe here though.
  760. (insert "\n" string)
  761. (goto-char (point-min))
  762. (when (plist-get org-export-latex-options-plist :emphasize)
  763. (org-export-latex-fontify))
  764. (org-export-latex-keywords-maybe
  765. org-export-latex-remove-from-headlines)
  766. (org-export-latex-special-chars
  767. (plist-get org-export-latex-options-plist :sub-superscript))
  768. (org-export-latex-links)
  769. ; (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
  770. (org-trim (buffer-string))))
  771. (defun org-export-latex-quotation-marks ()
  772. "Export quotation marks depending on language conventions."
  773. (let* ((lang (plist-get org-export-latex-options-plist :language))
  774. (quote-rpl (if (equal lang "fr")
  775. '(("\\(\\s-\\)\"" "«~")
  776. ("\\(\\S-\\)\"" "~»")
  777. ("\\(\\s-\\)'" "`"))
  778. '(("\\(\\s-\\)\"" "``")
  779. ("\\(\\S-\\)\"" "''")
  780. ("\\(\\s-\\)'" "`")))))
  781. (mapc (lambda(l) (goto-char (point-min))
  782. (while (re-search-forward (car l) nil t)
  783. (let ((rpl (concat (match-string 1) (cadr l))))
  784. (org-export-latex-protect-string rpl)
  785. (org-if-unprotected
  786. (replace-match rpl t t))))) quote-rpl)))
  787. (defun org-export-latex-special-chars (sub-superscript)
  788. "Export special characters to LaTeX.
  789. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  790. See the `org-export-latex.el' code for a complete conversion table."
  791. (goto-char (point-min))
  792. (mapc (lambda(c)
  793. (goto-char (point-min))
  794. (while (re-search-forward c nil t)
  795. ;; Put the point where to check for org-protected
  796. (unless (or (get-text-property (match-beginning 2) 'org-protected)
  797. (org-at-table-p))
  798. (cond ((member (match-string 2) '("\\$" "$"))
  799. (if (equal (match-string 2) "\\$")
  800. (replace-match (concat (match-string 1) "$"
  801. (match-string 3)) t t)
  802. (replace-match (concat (match-string 1) "\\$"
  803. (match-string 3)) t t)))
  804. ((member (match-string 2) '("&" "%" "#"))
  805. (if (equal (match-string 1) "\\")
  806. (replace-match (match-string 2) t t)
  807. (replace-match (concat (match-string 1) "\\"
  808. (match-string 2)) t t)))
  809. ((equal (match-string 2) "...")
  810. (replace-match
  811. (concat (match-string 1)
  812. (org-export-latex-protect-string "\\ldots{}")) t t))
  813. ((equal (match-string 2) "~")
  814. (cond ((equal (match-string 1) "\\") nil)
  815. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  816. (replace-match (concat (match-string 1) "\\~") t t))
  817. (t (replace-match
  818. (org-export-latex-protect-string
  819. (concat (match-string 1) "\\~{}")) t t))))
  820. ((member (match-string 2) '("{" "}"))
  821. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  822. (if (equal (match-string 1) "\\")
  823. (replace-match (match-string 2) t t)
  824. (replace-match (concat (match-string 1) "\\"
  825. (match-string 2)) t t)))))
  826. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  827. (cond ((equal (match-string 2) "\\")
  828. (replace-match (or (save-match-data
  829. (org-export-latex-treat-backslash-char
  830. (match-string 1)
  831. (match-string 3))) "") t t))
  832. ((member (match-string 2) '("_" "^"))
  833. (replace-match (or (save-match-data
  834. (org-export-latex-treat-sub-super-char
  835. sub-superscript
  836. (match-string 2)
  837. (match-string 1)
  838. (match-string 3))) "") t t)))))))
  839. '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  840. "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
  841. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
  842. "\\(.\\|^\\)\\(&\\)"
  843. "\\(.\\|^\\)\\(#\\)"
  844. "\\(.\\|^\\)\\(%\\)"
  845. "\\(.\\|^\\)\\({\\)"
  846. "\\(.\\|^\\)\\(}\\)"
  847. "\\(.\\|^\\)\\(~\\)"
  848. "\\(.\\|^\\)\\(\\.\\.\\.\\)"
  849. ;; (?\< . "\\textless{}")
  850. ;; (?\> . "\\textgreater{}")
  851. )))
  852. (defun org-export-latex-treat-sub-super-char
  853. (subsup char string-before string-after)
  854. "Convert the \"_\" and \"^\" characters to LaTeX.
  855. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  856. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  857. (cond ((equal string-before "\\")
  858. (concat string-before char string-after))
  859. ;; this is part of a math formula
  860. ((and (string-match "\\S-+" string-before)
  861. (string-match "\\S-+" string-after))
  862. (cond ((eq 'org-link (get-text-property 0 'face char))
  863. (concat string-before "\\" char string-after))
  864. ((save-match-data (org-inside-LaTeX-fragment-p))
  865. (if subsup
  866. (cond ((eq 1 (length string-after))
  867. (concat string-before char string-after))
  868. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  869. (format "%s%s{%s}" string-before char
  870. (match-string 1 string-after))))))
  871. ((and (> (length string-after) 1)
  872. (or (eq subsup t)
  873. (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
  874. (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
  875. (format "%s$%s{%s}$" string-before char
  876. (if (> (match-end 1) (1+ (match-beginning 1)))
  877. (concat "\\mathrm{" (match-string 1 string-after) "}")
  878. (match-string 1 string-after))))
  879. ((eq subsup t) (concat string-before "$" char string-after "$"))
  880. (t (org-export-latex-protect-string
  881. (concat string-before "\\" char "{}" string-after)))))
  882. (t (org-export-latex-protect-string
  883. (concat string-before "\\" char "{}" string-after)))))
  884. (defun org-export-latex-treat-backslash-char (string-before string-after)
  885. "Convert the \"$\" special character to LaTeX.
  886. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  887. (cond ((member (list string-after) org-html-entities)
  888. ;; backslash is part of a special entity (like "\alpha")
  889. (concat string-before "$\\"
  890. (or (cdar (member (list string-after) org-html-entities))
  891. string-after) "$"))
  892. ((and (not (string-match "^[ \n\t]" string-after))
  893. (not (string-match "[ \t]\\'\\|^" string-before)))
  894. ;; backslash is inside a word
  895. (org-export-latex-protect-string
  896. (concat string-before "\\textbackslash{}" string-after)))
  897. ((not (or (equal string-after "")
  898. (string-match "^[ \t\n]" string-after)))
  899. ;; backslash might escape a character (like \#) or a user TeX
  900. ;; macro (like \setcounter)
  901. (org-export-latex-protect-string
  902. (concat string-before "\\" string-after)))
  903. ((and (string-match "^[ \t\n]" string-after)
  904. (string-match "[ \t\n]\\'" string-before))
  905. ;; backslash is alone, convert it to $\backslash$
  906. (org-export-latex-protect-string
  907. (concat string-before "\\textbackslash{}" string-after)))
  908. (t (org-export-latex-protect-string
  909. (concat string-before "\\textbackslash{}" string-after)))))
  910. (defun org-export-latex-keywords (timestamps)
  911. "Convert special keywords to LaTeX.
  912. Regexps are those from `org-export-latex-special-string-regexps'.
  913. If TIMESTAMPS, convert timestamps, otherwise delete them."
  914. (let ((rg org-export-latex-special-string-regexps) r)
  915. (while (setq r (pop rg))
  916. (goto-char (point-min))
  917. (while (re-search-forward (eval r) nil t)
  918. (if (not timestamps)
  919. (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
  920. (replace-match ""))))))
  921. (defun org-export-latex-fixed-width (opt)
  922. "When OPT is non-nil convert fixed-width sections to LaTeX."
  923. (goto-char (point-min))
  924. (while (re-search-forward "^[ \t]*:" nil t)
  925. (if opt
  926. (progn (goto-char (match-beginning 0))
  927. (insert "\\begin{verbatim}\n")
  928. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  929. (replace-match (concat (match-string 1)
  930. (match-string 2)) t t)
  931. (forward-line))
  932. (insert "\\end{verbatim}\n\n"))
  933. (progn (goto-char (match-beginning 0))
  934. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  935. (replace-match (concat "%" (match-string 1)
  936. (match-string 2)) t t)
  937. (forward-line))))))
  938. (defvar org-table-last-alignment) ; defined in org-table.el
  939. (declare-function orgtbl-to-latex "org-table" (table params) t)
  940. (defun org-export-latex-tables (insert)
  941. "Convert tables to LaTeX and INSERT it."
  942. (goto-char (point-min))
  943. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  944. ;; FIXME really need to save-excursion?
  945. (save-excursion (org-table-align))
  946. (let* ((beg (org-table-begin))
  947. (end (org-table-end))
  948. (raw-table (buffer-substring-no-properties beg end))
  949. fnum fields line lines olines gr colgropen line-fmt align)
  950. (if org-export-latex-tables-verbatim
  951. (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
  952. "\\end{verbatim}\n")))
  953. (apply 'delete-region (list beg end))
  954. (insert (org-export-latex-protect-string tbl)))
  955. (progn
  956. (setq lines (split-string raw-table "\n" t))
  957. (apply 'delete-region (list beg end))
  958. (when org-export-table-remove-special-lines
  959. (setq lines (org-table-clean-before-export lines)))
  960. ;; make a formatting string to reflect aligment
  961. (setq olines lines)
  962. (while (and (not line-fmt) (setq line (pop olines)))
  963. (unless (string-match "^[ \t]*|-" line)
  964. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  965. (setq fnum (make-vector (length fields) 0))
  966. (setq line-fmt
  967. (mapconcat
  968. (lambda (x)
  969. (setq gr (pop org-table-colgroup-info))
  970. (format "%s%%s%s"
  971. (cond ((eq gr ':start)
  972. (prog1 (if colgropen "|" "")
  973. (setq colgropen t)))
  974. ((eq gr ':startend)
  975. (prog1 (if colgropen "|" "|")
  976. (setq colgropen nil)))
  977. (t ""))
  978. (if (memq gr '(:end :startend))
  979. (progn (setq colgropen nil) "|")
  980. "")))
  981. fnum ""))))
  982. ;; fix double || in line-fmt
  983. (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
  984. ;; maybe remove the first and last "|"
  985. (when (and (not org-export-latex-tables-column-borders)
  986. (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
  987. (setq line-fmt (match-string 2 line-fmt)))
  988. ;; format alignment
  989. (setq align (apply 'format
  990. (cons line-fmt
  991. (mapcar (lambda (x) (if x "r" "l"))
  992. org-table-last-alignment))))
  993. ;; prepare the table to send to orgtbl-to-latex
  994. (setq lines
  995. (mapcar
  996. (lambda(elem)
  997. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  998. (split-string (org-trim elem) "|" t)))
  999. lines))
  1000. (when insert
  1001. (insert (org-export-latex-protect-string
  1002. (orgtbl-to-latex
  1003. lines `(:tstart ,(concat "\\begin{tabular}{" align "}"))))
  1004. "\n\n")))))))
  1005. (defun org-export-latex-fontify ()
  1006. "Convert fontification to LaTeX."
  1007. (goto-char (point-min))
  1008. (while (re-search-forward org-emph-re nil t)
  1009. ;; The match goes one char after the *string*
  1010. (let ((emph (assoc (match-string 3)
  1011. org-export-latex-emphasis-alist))
  1012. rpl)
  1013. (unless (get-text-property (1- (point)) 'org-protected)
  1014. (setq rpl (concat (match-string 1)
  1015. (format (org-export-latex-protect-char-in-string
  1016. '("\\" "{" "}") (cadr emph))
  1017. (match-string 4))
  1018. (match-string 5)))
  1019. (if (caddr emph)
  1020. (setq rpl (org-export-latex-protect-string rpl)))
  1021. (replace-match rpl t t)))
  1022. (backward-char)))
  1023. (defun org-export-latex-links ()
  1024. ;; Make sure to use the LaTeX hyperref and graphicx package
  1025. ;; or send some warnings.
  1026. "Convert links to LaTeX."
  1027. (goto-char (point-min))
  1028. (while (re-search-forward org-bracket-link-analytic-regexp nil t)
  1029. (org-if-unprotected
  1030. (goto-char (match-beginning 0))
  1031. (let* ((re-radio org-export-latex-all-targets-re)
  1032. (remove (list (match-beginning 0) (match-end 0)))
  1033. (type (match-string 2))
  1034. (raw-path (org-extract-attributes (match-string 3)))
  1035. (full-raw-path (concat (match-string 1) raw-path))
  1036. (desc (match-string 5))
  1037. imgp radiop
  1038. ;; define the path of the link
  1039. (path (cond
  1040. ((member type '("http" "https" "ftp"))
  1041. (concat type ":" raw-path))
  1042. ((and re-radio (string-match re-radio raw-path))
  1043. (setq radiop t))
  1044. ((equal type "mailto")
  1045. (concat type ":" raw-path))
  1046. ((equal type "file")
  1047. (if (and (or (org-file-image-p (expand-file-name raw-path))
  1048. (string-match "\\.eps$" raw-path))
  1049. (equal desc full-raw-path))
  1050. (setq imgp t)
  1051. (progn (when (string-match "\\(.+\\)::.+" raw-path)
  1052. (setq raw-path (match-string 1 raw-path)))
  1053. (if (file-exists-p raw-path)
  1054. (concat type "://" (expand-file-name raw-path))
  1055. (concat type "://" (org-export-directory
  1056. :LaTeX org-export-latex-options-plist)
  1057. raw-path))))))))
  1058. ;; process with link inserting
  1059. (apply 'delete-region remove)
  1060. (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
  1061. (insert (format "\\includegraphics[%s]{%s}"
  1062. ;; image option should be set be a comment line
  1063. org-export-latex-image-default-option
  1064. (expand-file-name raw-path))))
  1065. (radiop (insert (format "\\hyperref[%s]{%s}"
  1066. (org-solidify-link-text raw-path) desc)))
  1067. ((not type)
  1068. (insert (format "\\hyperref[%s]{%s}"
  1069. (org-solidify-link-text raw-path) desc)))
  1070. (path (insert (format "\\href{%s}{%s}" path desc)))
  1071. (t (insert "\\texttt{" desc "}")))))))
  1072. (defvar org-latex-entities) ; defined below
  1073. (defun org-export-latex-preprocess ()
  1074. "Clean stuff in the LaTeX export."
  1075. ;; Preserve line breaks
  1076. (goto-char (point-min))
  1077. (while (re-search-forward "\\\\\\\\" nil t)
  1078. (add-text-properties (match-beginning 0) (match-end 0)
  1079. '(org-protected t)))
  1080. ;; Preserve latex environments
  1081. (goto-char (point-min))
  1082. (while (re-search-forward "^[ \t]*\\begin{\\([a-zA-Z]+\\)}" nil t)
  1083. (let* ((start (progn (beginning-of-line) (point)))
  1084. (end (or (and (re-search-forward
  1085. (concat "^[ \t]*\\end{" (match-string 1) "}" nil t)
  1086. (point-at-eol)))
  1087. (point-max))))
  1088. (add-text-properties start end '(org-protected t))))
  1089. ;; Convert LaTeX to \LaTeX{}
  1090. (goto-char (point-min))
  1091. (let ((case-fold-search nil) rpl)
  1092. (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
  1093. (replace-match (org-export-latex-protect-string
  1094. (concat (match-string 1) "\\LaTeX{}")) t t)))
  1095. ;; Convert blockquotes
  1096. (goto-char (point-min))
  1097. (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
  1098. (replace-match "\\begin{quote}" t t))
  1099. (goto-char (point-min))
  1100. (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
  1101. (replace-match "\\end{quote}" t t))
  1102. ;; Convert verse
  1103. (goto-char (point-min))
  1104. (while (search-forward "ORG-VERSE-START" nil t)
  1105. (replace-match "\\begin{verse}" t t))
  1106. (goto-char (point-min))
  1107. (while (search-forward "ORG-VERSE-END" nil t)
  1108. (replace-match "\\end{verse}" t t))
  1109. ;; Convert horizontal rules
  1110. (goto-char (point-min))
  1111. (while (re-search-forward "^----+.$" nil t)
  1112. (replace-match (org-export-latex-protect-string "\\hrule") t t))
  1113. ;; Protect LaTeX commands like \commad[...]{...} or \command{...}
  1114. (goto-char (point-min))
  1115. (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
  1116. (add-text-properties (match-beginning 0) (match-end 0)
  1117. '(org-protected t)))
  1118. ;; Protect LaTeX entities
  1119. (goto-char (point-min))
  1120. (while (re-search-forward org-latex-entities-regexp nil t)
  1121. (add-text-properties (match-beginning 0) (match-end 0)
  1122. '(org-protected t)))
  1123. ;; Replace radio links
  1124. (goto-char (point-min))
  1125. (while (re-search-forward
  1126. (concat "<<<?" org-export-latex-all-targets-re
  1127. ">>>?\\((INVISIBLE)\\)?") nil t)
  1128. (replace-match
  1129. (org-export-latex-protect-string
  1130. (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
  1131. (match-string 1)))
  1132. (if (match-string 2) "" (match-string 1)))) t t))
  1133. ;; Delete @<...> constructs
  1134. ;; Thanks to Daniel Clemente for this regexp
  1135. (goto-char (point-min))
  1136. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  1137. (replace-match ""))
  1138. ;; When converting to LaTeX, replace footnotes
  1139. ;; FIXME: don't protect footnotes from conversion
  1140. (when (plist-get org-export-latex-options-plist :footnotes)
  1141. (goto-char (point-min))
  1142. (while (re-search-forward "\\[[0-9]+\\]" nil t)
  1143. (when (save-match-data
  1144. (save-excursion (beginning-of-line)
  1145. (looking-at "[^:|#]")))
  1146. (let ((foot-beg (match-beginning 0))
  1147. (foot-end (match-end 0))
  1148. (foot-prefix (match-string 0))
  1149. footnote footnote-rpl)
  1150. (save-excursion
  1151. (when (search-forward foot-prefix nil t)
  1152. (replace-match "")
  1153. (let ((end (save-excursion
  1154. (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
  1155. (match-beginning 0) (point-max)))))
  1156. (setq footnote (concat (org-trim (buffer-substring (point) end))
  1157. " ")) ; prevent last } being part of a link
  1158. (delete-region (point) end))
  1159. (goto-char foot-beg)
  1160. (delete-region foot-beg foot-end)
  1161. (unless (null footnote)
  1162. (setq footnote-rpl (format "\\footnote{%s}" footnote))
  1163. (add-text-properties 0 10 '(org-protected t) footnote-rpl)
  1164. (add-text-properties (1- (length footnote-rpl))
  1165. (length footnote-rpl)
  1166. '(org-protected t) footnote-rpl)
  1167. (insert footnote-rpl)))))))
  1168. ;; Replace footnote section tag for LaTeX
  1169. (goto-char (point-min))
  1170. (while (re-search-forward
  1171. (concat "^" footnote-section-tag-regexp) nil t)
  1172. (replace-match ""))))
  1173. ;;; List handling:
  1174. (defun org-export-latex-lists ()
  1175. "Replace plain text lists in current buffer into LaTeX lists."
  1176. "Convert lists to LaTeX."
  1177. (goto-char (point-min))
  1178. (while (re-search-forward org-list-beginning-re nil t)
  1179. (org-if-unprotected
  1180. (beginning-of-line)
  1181. (insert (org-list-to-latex (org-list-parse-list t)) "\n"))))
  1182. (defconst org-latex-entities
  1183. '("\\!"
  1184. "\\'"
  1185. "\\+"
  1186. "\\,"
  1187. "\\-"
  1188. "\\:"
  1189. "\\;"
  1190. "\\<"
  1191. "\\="
  1192. "\\>"
  1193. "\\Huge"
  1194. "\\LARGE"
  1195. "\\Large"
  1196. "\\Styles"
  1197. "\\\\"
  1198. "\\`"
  1199. "\\addcontentsline"
  1200. "\\address"
  1201. "\\addtocontents"
  1202. "\\addtocounter"
  1203. "\\addtolength"
  1204. "\\addvspace"
  1205. "\\alph"
  1206. "\\appendix"
  1207. "\\arabic"
  1208. "\\author"
  1209. "\\begin{array}"
  1210. "\\begin{center}"
  1211. "\\begin{description}"
  1212. "\\begin{enumerate}"
  1213. "\\begin{eqnarray}"
  1214. "\\begin{equation}"
  1215. "\\begin{figure}"
  1216. "\\begin{flushleft}"
  1217. "\\begin{flushright}"
  1218. "\\begin{itemize}"
  1219. "\\begin{list}"
  1220. "\\begin{minipage}"
  1221. "\\begin{picture}"
  1222. "\\begin{quotation}"
  1223. "\\begin{quote}"
  1224. "\\begin{tabbing}"
  1225. "\\begin{table}"
  1226. "\\begin{tabular}"
  1227. "\\begin{thebibliography}"
  1228. "\\begin{theorem}"
  1229. "\\begin{titlepage}"
  1230. "\\begin{verbatim}"
  1231. "\\begin{verse}"
  1232. "\\bf"
  1233. "\\bf"
  1234. "\\bibitem"
  1235. "\\bigskip"
  1236. "\\cdots"
  1237. "\\centering"
  1238. "\\circle"
  1239. "\\cite"
  1240. "\\cleardoublepage"
  1241. "\\clearpage"
  1242. "\\cline"
  1243. "\\closing"
  1244. "\\dashbox"
  1245. "\\date"
  1246. "\\ddots"
  1247. "\\dotfill"
  1248. "\\em"
  1249. "\\fbox"
  1250. "\\flushbottom"
  1251. "\\fnsymbol"
  1252. "\\footnote"
  1253. "\\footnotemark"
  1254. "\\footnotesize"
  1255. "\\footnotetext"
  1256. "\\frac"
  1257. "\\frame"
  1258. "\\framebox"
  1259. "\\hfill"
  1260. "\\hline"
  1261. "\\hrulespace"
  1262. "\\hspace"
  1263. "\\huge"
  1264. "\\hyphenation"
  1265. "\\include"
  1266. "\\includeonly"
  1267. "\\indent"
  1268. "\\input"
  1269. "\\it"
  1270. "\\kill"
  1271. "\\label"
  1272. "\\large"
  1273. "\\ldots"
  1274. "\\line"
  1275. "\\linebreak"
  1276. "\\linethickness"
  1277. "\\listoffigures"
  1278. "\\listoftables"
  1279. "\\location"
  1280. "\\makebox"
  1281. "\\maketitle"
  1282. "\\mark"
  1283. "\\mbox"
  1284. "\\medskip"
  1285. "\\multicolumn"
  1286. "\\multiput"
  1287. "\\newcommand"
  1288. "\\newcounter"
  1289. "\\newenvironment"
  1290. "\\newfont"
  1291. "\\newlength"
  1292. "\\newline"
  1293. "\\newpage"
  1294. "\\newsavebox"
  1295. "\\newtheorem"
  1296. "\\nocite"
  1297. "\\nofiles"
  1298. "\\noindent"
  1299. "\\nolinebreak"
  1300. "\\nopagebreak"
  1301. "\\normalsize"
  1302. "\\onecolumn"
  1303. "\\opening"
  1304. "\\oval"
  1305. "\\overbrace"
  1306. "\\overline"
  1307. "\\pagebreak"
  1308. "\\pagenumbering"
  1309. "\\pageref"
  1310. "\\pagestyle"
  1311. "\\par"
  1312. "\\parbox"
  1313. "\\put"
  1314. "\\raggedbottom"
  1315. "\\raggedleft"
  1316. "\\raggedright"
  1317. "\\raisebox"
  1318. "\\ref"
  1319. "\\rm"
  1320. "\\roman"
  1321. "\\rule"
  1322. "\\savebox"
  1323. "\\sc"
  1324. "\\scriptsize"
  1325. "\\setcounter"
  1326. "\\setlength"
  1327. "\\settowidth"
  1328. "\\sf"
  1329. "\\shortstack"
  1330. "\\signature"
  1331. "\\sl"
  1332. "\\small"
  1333. "\\smallskip"
  1334. "\\sqrt"
  1335. "\\tableofcontents"
  1336. "\\telephone"
  1337. "\\thanks"
  1338. "\\thispagestyle"
  1339. "\\tiny"
  1340. "\\title"
  1341. "\\tt"
  1342. "\\twocolumn"
  1343. "\\typein"
  1344. "\\typeout"
  1345. "\\underbrace"
  1346. "\\underline"
  1347. "\\usebox"
  1348. "\\usecounter"
  1349. "\\value"
  1350. "\\vdots"
  1351. "\\vector"
  1352. "\\verb"
  1353. "\\vfill"
  1354. "\\vline"
  1355. "\\vspace")
  1356. "A list of LaTeX commands to be protected when performing conversion.")
  1357. (defconst org-latex-entities-regexp
  1358. (let (names rest)
  1359. (dolist (x org-latex-entities)
  1360. (if (string-match "[a-z][A-Z]$" x)
  1361. (push x names)
  1362. (push x rest)))
  1363. (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
  1364. "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
  1365. (provide 'org-export-latex)
  1366. ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
  1367. ;;; org-export-latex.el ends here