org-export-latex.el 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. ;;; org-export-latex.el --- LaTeX exporter for org-mode
  2. ;;
  3. ;; copyright (c) 2007 free software foundation, inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-export-latex.el
  7. ;; Version: 5.12
  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. ;;
  14. ;; This file is part of GNU Emacs.
  15. ;;
  16. ;; GNU Emacs is free software; you can redistribute it and/or modify it
  17. ;; under the terms of the GNU General Public License as published by the
  18. ;; Free Software Foundation; either version 3, or (at your option) any
  19. ;; later version.
  20. ;;
  21. ;; GNU Emacs is distributed in the hope that it will be useful, but WITHOUT
  22. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  23. ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  24. ;; more details.
  25. ;;
  26. ;; You should have received a copy of the GNU General Public License along
  27. ;; with GNU Emacs; see the file COPYING. If not, write to the Free Software
  28. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  29. ;; 02110-1301, USA.
  30. ;;
  31. ;;; Commentary:
  32. ;;
  33. ;; This library implements a LaTeX exporter for org-mode.
  34. ;;
  35. ;; Put this file into your load-path and the following into your ~/.emacs:
  36. ;; (require 'org-export-latex)
  37. ;;
  38. ;; The interactive functions are similar to those of the HTML exporter:
  39. ;;
  40. ;; M-x `org-export-as-latex'
  41. ;; M-x `org-export-as-latex-batch'
  42. ;; M-x `org-export-as-latex-to-buffer'
  43. ;; M-x `org-export-region-as-latex'
  44. ;; M-x `org-replace-region-by-latex'
  45. ;;
  46. ;;; Code:
  47. (eval-when-compile
  48. (require 'cl))
  49. (require 'footnote)
  50. (require 'org)
  51. ;;; Variables:
  52. (defvar org-latex-options-plist nil)
  53. (defvar org-latex-todo-keywords-1 nil)
  54. (defvar org-latex-all-targets-regexp nil)
  55. (defvar org-latex-add-level 0)
  56. (defvar org-latex-sectioning-depth 0)
  57. (defvar org-export-latex-list-beginning-re
  58. "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +?")
  59. (defvar org-latex-special-string-regexps
  60. '(org-ts-regexp
  61. org-scheduled-string
  62. org-deadline-string
  63. org-clock-string)
  64. "A list of regexps to convert as special keywords.")
  65. (defvar latexp) ; dynamically scoped from org.el
  66. (defvar re-quote) ; dynamically scoped from org.el
  67. (defvar commentsp) ; dynamically scoped from org.el
  68. ;;; Custom variables:
  69. (defcustom org-export-latex-sectioning-alist
  70. '((1 "\\section{%s}" "\\section*{%s}")
  71. (2 "\\subsection{%s}" "\\subsection*{%s}")
  72. (3 "\\subsubsection{%s}" "\\subsubsection*{%s}")
  73. (4 "\\paragraph{%s}" "\\paragraph*{%s}")
  74. (5 "\\subparagraph{%s}" "\\subparagraph*{%s}"))
  75. "Alist of LaTeX commands for inserting sections.
  76. Here is the structure of each cell:
  77. \(level unnumbered-section numbered-section\)
  78. The %s formatter will be replaced by the title of the section."
  79. :group 'org-export-latex
  80. :type 'alist)
  81. (defcustom org-export-latex-emphasis-alist
  82. '(("*" "\\textbf{%s}" nil)
  83. ("/" "\\emph{%s}" nil)
  84. ("_" "\\underline{%s}" nil)
  85. ("+" "\\texttt{%s}" nil)
  86. ("=" "\\texttt{%s}" nil))
  87. "Alist of LaTeX expressions to convert emphasis fontifiers.
  88. Each element of the list is a list of three elements.
  89. The first element is the character used as a marker for fontification.
  90. The second element is a formatting string to wrap fontified text with.
  91. The third element decides whether to protect converted text from other
  92. conversions."
  93. :group 'org-export-latex
  94. :type 'alist)
  95. (defcustom org-export-latex-preamble
  96. "\\documentclass[11pt,a4paper]{article}
  97. \\usepackage[utf8]{inputenc}
  98. \\usepackage[T1]{fontenc}
  99. \\usepackage{hyperref}"
  100. "Preamble to be inserted at the very beginning of the LaTeX export."
  101. :group 'org-export-latex
  102. :type 'string)
  103. (defcustom org-export-latex-title-command "\\maketitle"
  104. "The command used to insert the title just after \\begin{document}.
  105. If this string contains the formatting specification \"%s\" then
  106. it will be used as a formatting string, passing the title as an
  107. argument."
  108. :group 'org-export-latex
  109. :type 'string)
  110. (defcustom org-export-latex-date-format
  111. "%d %B %Y"
  112. "Format string for \\date{...}."
  113. :group 'org-export-latex
  114. :type 'string)
  115. (defcustom org-export-latex-tables-verbatim nil
  116. "When non-nil, export tables as verbatim."
  117. :group 'org-export-latex
  118. :type 'boolean)
  119. (defcustom org-export-latex-packages-alist nil
  120. "Alist of packages to be inserted in the preamble.
  121. Each cell is of the forma \( option . package \).
  122. For example:
  123. \(setq org-export-latex-packages-alist
  124. '((\"french\" \"babel\"))"
  125. :group 'org-export-latex
  126. :type 'alist)
  127. (defcustom org-export-latex-low-levels 'description
  128. "How to convert sections below the current level of sectioning,
  129. as specified by `org-export-headline-levels' or the value of \"H:\"
  130. in Org's #+OPTION line.
  131. This can be either nil (skip the sections), 'description (convert
  132. the sections as descriptive lists) or a string to be used instead
  133. of \\section{%s}. In this latter case, the %s stands here for the
  134. inserted headline and is mandatory."
  135. :group 'org-export-latex
  136. :type '(choice (const :tag "Ignore" nil)
  137. (symbol :tag "Convert as descriptive list" description)
  138. (string :tag "Use a section string" :value "\\subparagraph{%s}")))
  139. (defcustom org-export-latex-remove-from-headlines
  140. '(:todo t :priority t :tags t)
  141. "A plist of keywords to remove from headlines.
  142. Non-nil means remove this keyword type from the headline.
  143. Don't remove the keys, just change their values."
  144. :type 'plist
  145. :group 'org-export-latex)
  146. (defcustom org-export-latex-image-default-option "width=10em"
  147. "Default option for images."
  148. :group 'org-export-latex
  149. :type '(string))
  150. (defcustom org-export-latex-coding-system nil
  151. "Coding system for the exported LaTex file."
  152. :group 'org-export-latex
  153. :type 'coding-system)
  154. ;; FIXME Do we want this one?
  155. ;; (defun org-export-as-latex-and-open (arg) ...)
  156. ;;; Autoload functions:
  157. ;;;###autoload
  158. (defun org-export-as-latex-batch ()
  159. "Call `org-export-as-latex', may be used in batch processing as
  160. emacs --batch
  161. --load=$HOME/lib/emacs/org.el
  162. --eval \"(setq org-export-headline-levels 2)\"
  163. --visit=MyFile --funcall org-export-as-latex-batch"
  164. (org-export-as-latex org-export-headline-levels 'hidden))
  165. ;;;###autoload
  166. (defun org-export-as-latex-to-buffer (arg)
  167. "Call `org-exort-as-latex` with output to a temporary buffer.
  168. No file is created. The prefix ARG is passed through to `org-export-as-latex'."
  169. (interactive "P")
  170. (org-export-as-latex arg nil nil "*Org LaTeX Export*")
  171. (switch-to-buffer-other-window "*Org LaTeX Export*"))
  172. ;;;###autoload
  173. (defun org-replace-region-by-latex (beg end)
  174. "Replace the region from BEG to END with its LaTeX export.
  175. It assumes the region has `org-mode' syntax, and then convert it to
  176. LaTeX. This can be used in any buffer. For example, you could
  177. write an itemized list in `org-mode' syntax in an LaTeX buffer and
  178. then use this command to convert it."
  179. (interactive "r")
  180. (let (reg latex buf)
  181. (save-window-excursion
  182. (if (org-mode-p)
  183. (setq latex (org-export-region-as-latex
  184. beg end t 'string))
  185. (setq reg (buffer-substring beg end)
  186. buf (get-buffer-create "*Org tmp*"))
  187. (save-excursion
  188. (set-buffer buf)
  189. (erase-buffer)
  190. (insert reg)
  191. (org-mode)
  192. (setq latex (org-export-region-as-latex
  193. (point-min) (point-max) t 'string)))
  194. (kill-buffer buf)))
  195. (delete-region beg end)
  196. (insert latex)))
  197. ;;;###autoload
  198. (defun org-export-region-as-latex (beg end &optional body-only buffer)
  199. "Convert region from BEG to END in `org-mode' buffer to LaTeX.
  200. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  201. contents, and only produce the region of converted text, useful for
  202. cut-and-paste operations.
  203. If BUFFER is a buffer or a string, use/create that buffer as a target
  204. of the converted LaTeX. If BUFFER is the symbol `string', return the
  205. produced LaTeX as a string and leave not buffer behind. For example,
  206. a Lisp program could call this function in the following way:
  207. (setq latex (org-export-region-as-latex beg end t 'string))
  208. When called interactively, the output buffer is selected, and shown
  209. in a window. A non-interactive call will only retunr the buffer."
  210. (interactive "r\nP")
  211. (when (interactive-p)
  212. (setq buffer "*Org LaTeX Export*"))
  213. (let ((transient-mark-mode t) (zmacs-regions t)
  214. rtn)
  215. (goto-char end)
  216. (set-mark (point)) ;; to activate the region
  217. (goto-char beg)
  218. (setq rtn (org-export-as-latex
  219. nil nil nil
  220. buffer body-only))
  221. (if (fboundp 'deactivate-mark) (deactivate-mark))
  222. (if (and (interactive-p) (bufferp rtn))
  223. (switch-to-buffer-other-window rtn)
  224. rtn)))
  225. ;;;###autoload
  226. (defun org-export-as-latex (arg &optional hidden ext-plist
  227. to-buffer body-only)
  228. "Export current buffer to a LaTeX file."
  229. (interactive "P")
  230. ;; Make sure we have a file name when we need it.
  231. (when (and (not (or to-buffer body-only))
  232. (not buffer-file-name))
  233. (if (buffer-base-buffer)
  234. (org-set-local 'buffer-file-name
  235. (with-current-buffer (buffer-base-buffer)
  236. buffer-file-name))
  237. (error "Need a file name to be able to export")))
  238. (message "Exporting to LaTeX...")
  239. (org-update-radio-target-regexp)
  240. (org-export-latex-set-initial-vars ext-plist)
  241. (let* ((wcf (current-window-configuration))
  242. (opt-plist org-latex-options-plist)
  243. (filename (concat (file-name-as-directory
  244. (org-export-directory :LaTeX ext-plist))
  245. (file-name-sans-extension
  246. (file-name-nondirectory ;sans-extension
  247. buffer-file-name)) ".tex"))
  248. (filename (if (equal (file-truename filename)
  249. (file-truename buffer-file-name))
  250. (concat filename ".tex")
  251. filename))
  252. (buffer (if to-buffer
  253. (cond
  254. ((eq to-buffer 'string) (get-buffer-create
  255. "*Org LaTeX Export*"))
  256. (t (get-buffer-create to-buffer)))
  257. (find-file-noselect filename)))
  258. (region-p (org-region-active-p))
  259. (odd org-odd-levels-only)
  260. (preamble (org-export-latex-make-preamble opt-plist))
  261. (skip (plist-get opt-plist :skip-before-1st-heading))
  262. (text (plist-get opt-plist :text))
  263. (first-lines (if skip "" (org-export-latex-first-lines)))
  264. (coding-system (and (boundp 'buffer-file-coding-system)
  265. buffer-file-coding-system))
  266. (coding-system-for-write (or org-export-latex-coding-system
  267. coding-system))
  268. (save-buffer-coding-system (or org-export-latex-coding-system
  269. coding-system))
  270. (region (buffer-substring
  271. (if region-p (region-beginning) (point-min))
  272. (if region-p (region-end) (point-max))))
  273. (string-for-export
  274. (org-cleaned-string-for-export
  275. region :emph-multiline t
  276. :for-LaTeX t
  277. :comments nil
  278. :add-text (if (eq to-buffer 'string) nil text)
  279. :skip-before-1st-heading skip
  280. :LaTeX-fragments nil)))
  281. (set-buffer buffer)
  282. (erase-buffer)
  283. (and (fboundp 'set-buffer-file-coding-system)
  284. (set-buffer-file-coding-system coding-system-for-write))
  285. ;; insert the preamble and initial document commands
  286. (unless (or (eq to-buffer 'string) body-only)
  287. (insert preamble))
  288. ;; insert text found in #+TEXT
  289. (when (and text (not (eq to-buffer 'string)))
  290. (insert (org-export-latex-content text) "\n\n"))
  291. ;; insert lines before the first headline
  292. (unless (or skip (eq to-buffer 'string))
  293. (insert first-lines))
  294. ;; handle the case where the region does not begin with a section
  295. (when region-p
  296. (insert (with-temp-buffer
  297. (insert string-for-export)
  298. (org-export-latex-first-lines))))
  299. ;; export the content of headlines
  300. (org-export-latex-global
  301. (with-temp-buffer
  302. (insert string-for-export)
  303. (goto-char (point-min))
  304. (when (re-search-forward "^\\(\\*+\\) " nil t)
  305. (let* ((asters (length (match-string 1)))
  306. (level (if odd (- asters 2) (- asters 1))))
  307. (setq org-latex-add-level
  308. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  309. (org-export-latex-parse-global level odd)))))
  310. ;; finalization
  311. (unless body-only (insert "\n\\end{document}"))
  312. (or to-buffer (save-buffer))
  313. (goto-char (point-min))
  314. (message "Exporting to LaTeX...done")
  315. (prog1
  316. (if (eq to-buffer 'string)
  317. (prog1 (buffer-substring (point-min) (point-max))
  318. (kill-buffer (current-buffer)))
  319. (current-buffer))
  320. (set-window-configuration wcf))))
  321. ;;; Parsing functions:
  322. (defun org-export-latex-parse-global (level odd)
  323. "Parse the current buffer recursively, starting at LEVEL.
  324. If ODD is non-nil, assume the buffer only contains odd sections.
  325. Return A list reflecting the document structure."
  326. (save-excursion
  327. (goto-char (point-min))
  328. (let* ((cnt 0) output
  329. (depth org-latex-sectioning-depth))
  330. (while (re-search-forward
  331. (concat "^\\(\\(?:\\*\\)\\{"
  332. (number-to-string (+ (if odd 2 1) level))
  333. "\\}\\) \\(.*\\)$")
  334. ;; make sure that there is no upper heading
  335. (when (> level 0)
  336. (save-excursion
  337. (save-match-data
  338. (re-search-forward
  339. (concat "^\\(\\(?:\\*\\)\\{"
  340. (number-to-string level)
  341. "\\}\\) \\(.*\\)$") nil t)))) t)
  342. (setq cnt (1+ cnt))
  343. (let* ((pos (match-beginning 0))
  344. (heading (match-string 2))
  345. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  346. (save-excursion
  347. (narrow-to-region
  348. (point)
  349. (save-match-data
  350. (if (re-search-forward
  351. (concat "^\\(\\(?:\\*\\)\\{"
  352. (number-to-string (+ (if odd 2 1) level))
  353. "\\}\\) \\(.*\\)$") nil t)
  354. (match-beginning 0)
  355. (point-max))))
  356. (goto-char (point-min))
  357. (setq output
  358. (append output
  359. (list
  360. (list
  361. `(pos . ,pos)
  362. `(level . ,nlevel)
  363. `(occur . ,cnt)
  364. `(heading . ,heading)
  365. `(content . ,(org-export-latex-parse-content))
  366. `(subcontent . ,(org-export-latex-parse-subcontent
  367. level odd)))))))
  368. (widen)))
  369. (list output))))
  370. (defun org-export-latex-parse-list (&optional delete)
  371. "Parse the list at point.
  372. Return a list containing first level items as strings and
  373. sublevels as list of strings."
  374. (let ((start (point))
  375. ;; Find the end of the list
  376. (end (save-excursion
  377. (catch 'exit
  378. (while (or (looking-at org-export-latex-list-beginning-re)
  379. (looking-at "^[ \t]+\\|^$"))
  380. (if (eq (point) (point-max))
  381. (throw 'exit (point-max)))
  382. (forward-line 1))) (point)))
  383. output itemsep)
  384. (while (re-search-forward org-export-latex-list-beginning-re end t)
  385. (setq itemsep (if (save-match-data
  386. (string-match "^[0-9]" (match-string 2)))
  387. "[0-9]+\\(?:\\.\\|)\\)" "[-+]"))
  388. (let* ((indent1 (match-string 1))
  389. (nextitem (save-excursion
  390. (save-match-data
  391. (or (and (re-search-forward
  392. (concat "^" indent1 itemsep " *?") end t)
  393. (match-beginning 0)) end))))
  394. (item (buffer-substring
  395. (point)
  396. (or (and (re-search-forward
  397. org-export-latex-list-beginning-re end t)
  398. (goto-char (match-beginning 0)))
  399. (goto-char end))))
  400. (nextindent (match-string 1))
  401. (item (org-trim item))
  402. (item (if (string-match "^\\[.+\\]" item)
  403. (replace-match "\\\\texttt{\\&}"
  404. t nil item) item)))
  405. (push item output)
  406. (when (> (length nextindent)
  407. (length indent1))
  408. (narrow-to-region (point) nextitem)
  409. (push (org-export-latex-parse-list) output)
  410. (widen))))
  411. (when delete (delete-region start end))
  412. (setq output (nreverse output))
  413. (push (if (string-match "^\\[0" itemsep)
  414. 'ordered 'unordered) output)))
  415. (defun org-export-latex-parse-content ()
  416. "Extract the content of a section."
  417. (let ((beg (point))
  418. (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
  419. (progn (beginning-of-line) (point))
  420. (point-max))))
  421. (buffer-substring beg end)))
  422. (defun org-export-latex-parse-subcontent (level odd)
  423. "Extract the subcontent of a section at LEVEL.
  424. If ODD Is non-nil, assume subcontent only contains odd sections."
  425. (if (not (re-search-forward
  426. (concat "^\\(\\(?:\\*\\)\\{"
  427. (number-to-string (+ (if odd 4 2) level))
  428. "\\}\\) \\(.*\\)$")
  429. nil t))
  430. nil ; subcontent is nil
  431. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  432. ;;; Rendering functions:
  433. (defun org-export-latex-global (content)
  434. "Export CONTENT to LaTeX.
  435. CONTENT is an element of the list produced by
  436. `org-export-latex-parse-global'."
  437. (if (eq (car content) 'subcontent)
  438. (mapc 'org-export-latex-sub (cdr content))
  439. (org-export-latex-sub (car content))))
  440. (defun org-export-latex-sub (subcontent)
  441. "Export the list SUBCONTENT to LaTeX.
  442. SUBCONTENT is an alist containing information about the headline
  443. and its content."
  444. (let ((num (plist-get org-latex-options-plist :section-numbers)))
  445. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  446. (defun org-export-latex-subcontent (subcontent num)
  447. "Export each cell of SUBCONTENT to LaTeX."
  448. (let ((heading (org-export-latex-fontify-headline
  449. (cdr (assoc 'heading subcontent))))
  450. (level (- (cdr (assoc 'level subcontent))
  451. org-latex-add-level))
  452. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  453. (content (cdr (assoc 'content subcontent)))
  454. (subcontent (cadr (assoc 'subcontent subcontent))))
  455. (cond
  456. ;; Normal conversion
  457. ((<= level org-latex-sectioning-depth)
  458. (let ((sec (assoc level org-export-latex-sectioning-alist)))
  459. (insert (format (if num (cadr sec) (caddr sec)) heading) "\n"))
  460. (insert (org-export-latex-content content))
  461. (cond ((stringp subcontent) (insert subcontent))
  462. ((listp subcontent) (org-export-latex-sub subcontent))))
  463. ;; At a level under the hl option: we can drop this subsection
  464. ((> level org-latex-sectioning-depth)
  465. (cond ((eq org-export-latex-low-levels 'description)
  466. (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading))
  467. (insert (org-export-latex-content content))
  468. (cond ((stringp subcontent) (insert subcontent))
  469. ((listp subcontent) (org-export-latex-sub subcontent)))
  470. (insert "\\end{description}\n"))
  471. ((stringp org-export-latex-low-levels)
  472. (insert (format org-export-latex-low-levels heading) "\n")
  473. (insert (org-export-latex-content content))
  474. (cond ((stringp subcontent) (insert subcontent))
  475. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  476. ;;; Exporting internals:
  477. (defun org-export-latex-protect-string (string)
  478. "Prevent further conversion for STRING by adding the
  479. org-protect property."
  480. (add-text-properties
  481. 0 (length string) '(org-protected t) string) string)
  482. (defun org-export-latex-protect-char-in-string (char-list string)
  483. "Add org-protected text-property to char from CHAR-LIST in STRING."
  484. (with-temp-buffer
  485. (save-match-data
  486. (insert string)
  487. (goto-char (point-min))
  488. (while (re-search-forward (regexp-opt char-list) nil t)
  489. (add-text-properties (match-beginning 0)
  490. (match-end 0) '(org-protected t)))
  491. (buffer-string))))
  492. (defun org-export-latex-set-initial-vars (ext-plist)
  493. "Store org local variables required for LaTeX export.
  494. EXT-PLIST is an optional additional plist."
  495. (setq org-latex-todo-keywords-1 org-todo-keywords-1
  496. org-latex-all-targets-regexp
  497. (org-make-target-link-regexp (org-all-targets))
  498. org-latex-options-plist
  499. (org-combine-plists (org-default-export-plist) ext-plist
  500. (org-infile-export-plist))
  501. org-latex-sectioning-depth
  502. (let ((hl-levels (plist-get org-latex-options-plist :headline-levels))
  503. (sec-depth (length org-export-latex-sectioning-alist)))
  504. ;; Fall back on org-export-latex-sectioning-alist length if
  505. ;; headline-levels goes beyond it
  506. (if (> hl-levels sec-depth) sec-depth hl-levels))))
  507. (defun org-export-latex-make-preamble (opt-plist)
  508. "Make the LaTeX preamble and return it as a string.
  509. Argument OPT-PLIST is the options plist for current buffer."
  510. (let ((toc (plist-get opt-plist :table-of-contents)))
  511. (concat
  512. (if (plist-get opt-plist :time-stamp-file)
  513. (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
  514. ;; insert LaTeX custom preamble
  515. org-export-latex-preamble "\n"
  516. ;; insert information on LaTeX packages
  517. (when org-export-latex-packages-alist
  518. (mapconcat (lambda(p)
  519. (if (equal "" (car p))
  520. (format "\\usepackage{%s}" (cadr p))
  521. (format "\\usepackage[%s]{%s}"
  522. (car p) (cadr p))))
  523. org-export-latex-packages-alist "\n"))
  524. ;; insert the title
  525. (format
  526. "\\title{%s}\n"
  527. ;; convert the title
  528. (org-export-latex-content
  529. (or (plist-get opt-plist :title)
  530. (and (not
  531. (plist-get opt-plist :skip-before-1st-heading))
  532. (org-export-grab-title-from-buffer))
  533. (and buffer-file-name
  534. (file-name-sans-extension
  535. (file-name-nondirectory buffer-file-name)))
  536. "UNTITLED")))
  537. ;; insert author info
  538. (if (plist-get opt-plist :author-info)
  539. (format "\\author{%s}\n"
  540. (or (plist-get opt-plist :author) user-full-name))
  541. (format "%%\\author{%s}\n"
  542. (or (plist-get opt-plist :author) user-full-name)))
  543. ;; insert the date
  544. (format "\\date{%s}\n"
  545. (format-time-string
  546. (or (plist-get opt-plist :date)
  547. org-export-latex-date-format)))
  548. ;; beginning of the document
  549. "\n\\begin{document}\n\n"
  550. ;; insert the title command
  551. (if (string-match "%s" org-export-latex-title-command)
  552. (format org-export-latex-title-command
  553. (plist-get opt-plist :title))
  554. org-export-latex-title-command)
  555. "\n\n"
  556. ;; table of contents
  557. (when (and org-export-with-toc
  558. (plist-get opt-plist :section-numbers))
  559. (cond ((numberp toc)
  560. (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
  561. (min toc (plist-get opt-plist :headline-levels))))
  562. (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
  563. (plist-get opt-plist :headline-levels))))))))
  564. (defun org-export-latex-first-lines (&optional comments)
  565. "Export the first lines before first headline.
  566. COMMENTS is either nil to replace them with the empty string or a
  567. formatting string like %%%%s if we want to comment them out."
  568. (save-excursion
  569. (goto-char (point-min))
  570. (let* ((pt (point))
  571. (end (if (and (re-search-forward "^\\*" nil t)
  572. (not (eq pt (match-beginning 0))))
  573. (goto-char (match-beginning 0))
  574. (goto-char (point-max)))))
  575. (org-export-latex-content
  576. (org-cleaned-string-for-export
  577. (buffer-substring (point-min) end)
  578. :for-LaTeX t
  579. :emph-multiline t
  580. :add-text nil
  581. :comments nil
  582. :skip-before-1st-heading nil
  583. :LaTeX-fragments nil)))))
  584. (defun org-export-latex-keywords-maybe (remove-list)
  585. "Maybe remove keywords depending on rules in REMOVE-LIST."
  586. (goto-char (point-min))
  587. (let ((re-todo (mapconcat 'identity org-latex-todo-keywords-1 "\\|")))
  588. ;; convert TODO keywords
  589. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  590. (if (plist-get remove-list :todo)
  591. (replace-match "")
  592. (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
  593. ;; convert priority string
  594. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  595. (if (plist-get remove-list :priority)
  596. (replace-match "")
  597. (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
  598. ;; convert tags
  599. (when (re-search-forward "\\(:[a-zA-Z0-9]+\\)+:" nil t)
  600. (if (or (not org-export-with-tags)
  601. (plist-get remove-list :tags))
  602. (replace-match "")
  603. (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))))
  604. (defun org-export-latex-fontify-headline (headline)
  605. "Fontify special words in a HEADLINE."
  606. (with-temp-buffer
  607. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  608. ;; the beginning of the buffer - inserting "\n" is safe here though.
  609. (insert "\n" headline)
  610. (goto-char (point-min))
  611. (when (plist-get org-latex-options-plist :emphasize)
  612. (org-export-latex-fontify))
  613. (org-export-latex-special-chars
  614. (plist-get org-latex-options-plist :sub-superscript))
  615. (org-export-latex-keywords-maybe
  616. org-export-latex-remove-from-headlines)
  617. (org-export-latex-links)
  618. (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
  619. (defun org-export-latex-content (content)
  620. "Convert CONTENT string to LaTeX."
  621. (with-temp-buffer
  622. (insert content)
  623. (org-export-latex-quotation-marks)
  624. (when (plist-get org-latex-options-plist :emphasize)
  625. (org-export-latex-fontify))
  626. (org-export-latex-special-chars
  627. (plist-get org-latex-options-plist :sub-superscript))
  628. (org-export-latex-links)
  629. (org-export-latex-keywords
  630. (plist-get org-latex-options-plist :timestamps))
  631. (org-export-latex-lists)
  632. (org-export-latex-tables
  633. (plist-get org-latex-options-plist :tables))
  634. (org-export-latex-fixed-width
  635. (plist-get org-latex-options-plist :fixed-width))
  636. ;; return string
  637. (buffer-substring (point-min) (point-max))))
  638. (defun org-export-latex-quotation-marks ()
  639. "Export question marks depending on language conventions.
  640. Local definition of the language overrides
  641. `org-export-latex-quotation-marks-convention' which overrides
  642. `org-export-default-language'."
  643. (let* ((lang (plist-get org-latex-options-plist :language))
  644. (quote-rpl (if (equal lang "fr")
  645. '(("\\(\\s-\\)\"" "«~")
  646. ("\\(\\S-\\)\"" "~»")
  647. ("\\(\\s-\\)'" "`"))
  648. '(("\\(\\s-\\)\"" "``")
  649. ("\\(\\S-\\)\"" "''")
  650. ("\\(\\s-\\)'" "`")))))
  651. (mapc (lambda(l) (goto-char (point-min))
  652. (while (re-search-forward (car l) nil t)
  653. (let ((rpl (concat (match-string 1) (cadr l))))
  654. (org-export-latex-protect-string rpl)
  655. (org-if-unprotected
  656. (replace-match rpl t t))))) quote-rpl)))
  657. ;; | chars/string in Org | normal environment | math environment |
  658. ;; |-----------------------+-----------------------+-----------------------|
  659. ;; | & # % $ | \& \# \% \$ | \& \# \% \$ |
  660. ;; | { } _ ^ \ | \{ \} \_ \^ \\ | { } _ ^ \ |
  661. ;; |-----------------------+-----------------------+-----------------------|
  662. ;; | a_b and a^b | $a_b$ and $a^b$ | a_b and a^b |
  663. ;; | a_abc and a_{abc} | $a_a$bc and $a_{abc}$ | a_abc and a_{abc} |
  664. ;; | \tau and \mu | $\tau$ and $\mu$ | \tau and \mu |
  665. ;; |-----------------------+-----------------------+-----------------------|
  666. ;; | \_ \^ | \_ \^ | \_ \^ |
  667. ;; | \(a=\mu\mbox{m}\) | \(a=\mu\mbox{m}\) | \(a=\mu\mbox{m}\) |
  668. ;; | \[\beta^2-a=0\] | \[\beta^2-a=0\] | \[\beta^2-a=0\] |
  669. ;; | $x=22\tau$ | $x=22\tau$ | $x=22\tau$ |
  670. ;; | $$\alpha=\sqrt{a^3}$$ | $$\alpha=\sqrt{a^3}$$ | $$\alpha=\sqrt{a^3}$$ |
  671. (defun org-export-latex-special-chars (sub-superscript)
  672. "Export special characters to LaTeX.
  673. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  674. See the `org-export-latex.el' code for a complete conversion table."
  675. (goto-char (point-min))
  676. (mapc (lambda(c)
  677. (goto-char (point-min))
  678. (while (re-search-forward c nil t)
  679. ;; Put the point where to check for org-protected
  680. (unless (get-text-property (match-beginning 2) 'org-protected)
  681. (cond ((member (match-string 2) '("\\$" "$"))
  682. (if (equal (match-string 2) "\\$")
  683. (replace-match (concat (match-string 1) "$"
  684. (match-string 3)) t t)
  685. (replace-match (concat (match-string 1) "\\$"
  686. (match-string 3)) t t)))
  687. ((member (match-string 2) '("&" "%" "#"))
  688. (if (equal (match-string 1) "\\")
  689. (replace-match (match-string 2) t t)
  690. (replace-match (concat (match-string 1) "\\"
  691. (match-string 2)) t t)))
  692. ((equal (match-string 2) "~")
  693. (cond ((equal (match-string 1) "\\") nil)
  694. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  695. (replace-match (concat (match-string 1) "\\~") t t))
  696. (t (replace-match
  697. (org-export-latex-protect-string
  698. (concat (match-string 1) "\\~{}")) t t))))
  699. ((member (match-string 2) '("{" "}"))
  700. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  701. (if (equal (match-string 1) "\\")
  702. (replace-match (match-string 2) t t)
  703. (replace-match (concat (match-string 1) "\\"
  704. (match-string 2)) t t)))))
  705. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  706. (cond ((equal (match-string 2) "\\")
  707. (replace-match (or (save-match-data
  708. (org-export-latex-treat-backslash-char
  709. (match-string 1)
  710. (match-string 3))) "") t t))
  711. ((member (match-string 2) '("_" "^"))
  712. (replace-match (or (save-match-data
  713. (org-export-latex-treat-sub-super-char
  714. sub-superscript
  715. (match-string 1)
  716. (match-string 2)
  717. (match-string 3))) "") t t)))))))
  718. '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  719. "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
  720. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
  721. "\\(.\\|^\\)\\(&\\)"
  722. "\\(.\\|^\\)\\(#\\)"
  723. "\\(.\\|^\\)\\(%\\)"
  724. "\\(.\\|^\\)\\({\\)"
  725. "\\(.\\|^\\)\\(}\\)"
  726. "\\(.\\|^\\)\\(~\\)"
  727. ;; (?\< . "\\textless{}")
  728. ;; (?\> . "\\textgreater{}")
  729. )))
  730. (defun org-export-latex-treat-sub-super-char
  731. (subsup string-before char string-after)
  732. "Convert the \"_\" and \"^\" characters to LaTeX.
  733. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  734. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  735. (cond ((equal string-before "\\")
  736. (concat string-before char string-after))
  737. ;; this is part of a math formula
  738. ((and (string-match "\\S-+" string-before)
  739. (string-match "\\S-+" string-after))
  740. (cond ((eq 'org-link (get-text-property 0 'face char))
  741. (concat string-before "\\" char string-after))
  742. ((save-match-data (org-inside-LaTeX-fragment-p))
  743. (if subsup
  744. (cond ((eq 1 (length string-after))
  745. (concat string-before char string-after))
  746. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  747. (format "%s%s{%s}" string-before char
  748. (match-string 1 string-after))))))
  749. ((and subsup
  750. (> (length string-after) 1)
  751. (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
  752. (format "$%s%s{%s}$" string-before char
  753. (match-string 1 string-after)))
  754. (subsup (concat "$" string-before char string-after "$"))
  755. (t (org-export-latex-protect-string
  756. (concat string-before "\\" char "{}" string-after)))))
  757. (t (org-export-latex-protect-string
  758. (concat string-before "\\" char "{}" string-after)))))
  759. (defun org-export-latex-treat-backslash-char (string-before string-after)
  760. "Convert the \"$\" special character to LaTeX.
  761. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  762. (cond ((member (list string-after) org-html-entities)
  763. ;; backslash is part of a special entity (like "\alpha")
  764. (concat string-before "$\\"
  765. (or (cdar (member (list string-after) org-html-entities))
  766. string-after) "$"))
  767. ((and (not (string-match "^[ \n\t]" string-after))
  768. (not (string-match "[ \t]\\'\\|^" string-before)))
  769. ;; backslash is inside a word
  770. (org-export-latex-protect-string
  771. (concat string-before "\\textbackslash{}" string-after)))
  772. ((not (or (equal string-after "")
  773. (string-match "^[ \t\n]" string-after)))
  774. ;; backslash might escape a character (like \#) or a user TeX
  775. ;; macro (like \setcounter)
  776. (org-export-latex-protect-string
  777. (concat string-before "\\" string-after)))
  778. ((and (string-match "^[ \t\n]" string-after)
  779. (string-match "[ \t\n]\\'" string-before))
  780. ;; backslash is alone, convert it to $\backslash$
  781. (org-export-latex-protect-string
  782. (concat string-before "\\textbackslash{}" string-after)))
  783. (t (org-export-latex-protect-string
  784. (concat string-before "\\textbackslash{}" string-after)))))
  785. (defun org-export-latex-keywords (timestamps)
  786. "Convert special keywords to LaTeX.
  787. Regexps are those from `org-latex-special-string-regexps'."
  788. (let ((rg org-latex-special-string-regexps) r)
  789. (while (setq r (pop rg))
  790. (goto-char (point-min))
  791. (while (re-search-forward (eval r) nil t)
  792. (if (not timestamps)
  793. (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
  794. (replace-match ""))))))
  795. (defun org-export-latex-fixed-width (opt)
  796. "When OPT is non-nil convert fixed-width sections to LaTeX."
  797. (goto-char (point-min))
  798. ;; FIXME the search shouldn't be performed on already converted text
  799. (while (re-search-forward "^[ \t]*:" nil t)
  800. (if opt
  801. (progn (goto-char (match-beginning 0))
  802. (insert "\\begin{verbatim}\n")
  803. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  804. (replace-match (concat (match-string 1)
  805. (match-string 2)) t t)
  806. (forward-line))
  807. (insert "\\end{verbatim}\n\n"))
  808. (progn (goto-char (match-beginning 0))
  809. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  810. (replace-match (concat "%" (match-string 1)
  811. (match-string 2)) t t)
  812. (forward-line))))))
  813. (defun org-export-latex-lists ()
  814. "Convert lists to LaTeX."
  815. (goto-char (point-min))
  816. (while (re-search-forward org-export-latex-list-beginning-re nil t)
  817. (beginning-of-line)
  818. (org-export-list-to-latex
  819. (org-export-latex-parse-list t))))
  820. (defun org-export-list-to-generic (list params)
  821. "Convert a LIST parsed through `org-export-latex-parse-list' to other formats.
  822. Valid parameters are
  823. :ustart String to start an unordered list
  824. :uend String to end an unordered list
  825. :ostart String to start an ordered list
  826. :oend String to end an ordered list
  827. :splice When set to t, return only list body lines, don't wrap
  828. them into :[u/o]start and :[u/o]end. Default is nil.
  829. :istart String to start a list item
  830. :iend String to end a list item
  831. :isep String to separate items
  832. :lsep String to separate sublists"
  833. (interactive)
  834. (let* ((p params) sublist
  835. (splicep (plist-get p :splice))
  836. (ostart (plist-get p :ostart))
  837. (oend (plist-get p :oend))
  838. (ustart (plist-get p :ustart))
  839. (uend (plist-get p :uend))
  840. (istart (plist-get p :istart))
  841. (iend (plist-get p :iend))
  842. (isep (plist-get p :isep))
  843. (lsep (plist-get p :lsep)))
  844. (let ((wrapper
  845. (cond ((eq (car list) 'ordered)
  846. (concat ostart "\n%s" oend "\n"))
  847. ((eq (car list) 'unordered)
  848. (concat ustart "\n%s" uend "\n"))))
  849. rtn)
  850. (while (setq sublist (pop list))
  851. (cond ((symbolp sublist) nil)
  852. ((stringp sublist)
  853. (setq rtn (concat rtn istart sublist iend isep)))
  854. (t
  855. (setq rtn (concat rtn ;; previous list
  856. lsep ;; list separator
  857. (org-export-list-to-generic sublist p)
  858. lsep ;; list separator
  859. )))))
  860. (format wrapper rtn))))
  861. (defun org-export-list-to-latex (list)
  862. "Convert LIST into a LaTeX list."
  863. (insert
  864. (org-export-list-to-generic
  865. list '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  866. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  867. :istart "\\item " :iend ""
  868. :isep "\n" :lsep "\n"))
  869. ;; Add a trailing \n after list conversion
  870. "\n"))
  871. ;; FIXME Use org-export-highlight-first-table-line ?
  872. (defun org-export-latex-tables (insert)
  873. "Convert tables to LaTeX and INSERT it."
  874. (goto-char (point-min))
  875. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  876. ;; FIXME really need to save-excursion?
  877. (save-excursion (org-table-align))
  878. (let* ((beg (org-table-begin))
  879. (end (org-table-end))
  880. (raw-table (buffer-substring-no-properties beg end))
  881. fnum fields line lines olines gr colgropen line-fmt align)
  882. (if org-export-latex-tables-verbatim
  883. (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
  884. "\\end{verbatim}\n")))
  885. (apply 'delete-region (list beg end))
  886. (insert tbl))
  887. (progn
  888. (setq lines (split-string raw-table "\n" t))
  889. (apply 'delete-region (list beg end))
  890. (when org-export-table-remove-special-lines
  891. (setq lines (org-table-clean-before-export lines)))
  892. ;; make a formatting string to reflect aligment
  893. (setq olines lines)
  894. (while (and (not line-fmt) (setq line (pop olines)))
  895. (unless (string-match "^[ \t]*|-" line)
  896. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  897. (setq fnum (make-vector (length fields) 0))
  898. (setq line-fmt
  899. (mapconcat
  900. (lambda (x)
  901. (setq gr (pop org-table-colgroup-info))
  902. (format "%s%%s%s"
  903. (cond ((eq gr ':start)
  904. (prog1 (if colgropen "|" "")
  905. (setq colgropen t)))
  906. ((eq gr ':startend)
  907. (prog1 (if colgropen "|" "|")
  908. (setq colgropen nil)))
  909. (t ""))
  910. (if (memq gr '(:end :startend))
  911. (progn (setq colgropen nil) "|")
  912. "")))
  913. fnum ""))))
  914. ;; maybe remove the first and last "|"
  915. (when (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt)
  916. (setq line-fmt (match-string 2 line-fmt)))
  917. ;; format alignment
  918. (setq align (apply 'format
  919. (cons line-fmt
  920. (mapcar (lambda (x) (if x "r" "l"))
  921. org-table-last-alignment))))
  922. ;; prepare the table to send to orgtbl-to-latex
  923. (setq lines
  924. (mapcar
  925. (lambda(elem)
  926. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  927. (split-string (org-trim elem) "|" t)))
  928. lines))
  929. (when insert
  930. (insert (orgtbl-to-latex
  931. lines `(:tstart ,(concat "\\begin{tabular}{" align "}")))
  932. "\n\n")))))))
  933. (defun org-export-latex-fontify ()
  934. "Convert fontification to LaTeX."
  935. (goto-char (point-min))
  936. (while (re-search-forward org-emph-re nil t)
  937. ;; The match goes one char after the *string*
  938. (let ((emph (assoc (match-string 3)
  939. org-export-latex-emphasis-alist))
  940. rpl)
  941. (unless (get-text-property (1- (point)) 'org-protected)
  942. (setq rpl (concat (match-string 1)
  943. (format (org-export-latex-protect-char-in-string
  944. '("\\" "{" "}") (cadr emph))
  945. (match-string 4))
  946. (match-string 5)))
  947. (if (caddr emph)
  948. (setq rpl (org-export-latex-protect-string rpl)))
  949. (replace-match rpl t t)))
  950. (backward-char)))
  951. (defun org-export-latex-links ()
  952. ;; Make sure to use the LaTeX hyperref and graphicx package
  953. ;; or send some warnings.
  954. "Convert links to LaTeX."
  955. (goto-char (point-min))
  956. (while (re-search-forward org-bracket-link-analytic-regexp nil t)
  957. (org-if-unprotected
  958. (goto-char (match-beginning 0))
  959. (let* ((re-radio org-latex-all-targets-regexp)
  960. (remove (list (match-beginning 0) (match-end 0)))
  961. (type (match-string 2))
  962. (raw-path (match-string 3))
  963. (full-raw-path (concat (match-string 1) raw-path))
  964. (desc (match-string 5))
  965. imgp radiop
  966. ;; define the path of the link
  967. (path (cond
  968. ((member type '("http" "https" "ftp"))
  969. (concat type ":" raw-path))
  970. ((and re-radio (string-match re-radio raw-path))
  971. (setq radiop t))
  972. ((equal type "mailto")
  973. (concat type ":" raw-path))
  974. ((equal type "file")
  975. (if (and (or (org-file-image-p (expand-file-name raw-path))
  976. (string-match "\\.eps$" raw-path))
  977. (equal desc full-raw-path))
  978. (setq imgp t)
  979. (progn (when (string-match "\\(.+\\)::.+" raw-path)
  980. (setq raw-path (match-string 1 raw-path)))
  981. (if (file-exists-p raw-path)
  982. (concat type "://" (expand-file-name raw-path))
  983. (concat type "://" (org-export-directory
  984. :LaTeX org-latex-options-plist)
  985. raw-path))))))))
  986. ;; process with link inserting
  987. (apply 'delete-region remove)
  988. (cond ((and imgp (plist-get org-latex-options-plist :inline-images))
  989. (insert (format "\\includegraphics[%s]{%s}"
  990. ;; image option should be set be a comment line
  991. org-export-latex-image-default-option
  992. (expand-file-name raw-path))))
  993. ;; FIXME: what about caption? image properties?
  994. (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc)))
  995. (path (insert (format "\\href{%s}{%s}" path desc)))
  996. (t (insert "\\texttt{" desc "}")))))))
  997. (defun org-export-latex-cleaned-string
  998. ;; FIXME remove commentsp call in org.el and here
  999. (&optional commentsp)
  1000. "Clean stuff in the LaTeX export."
  1001. ;; Preserve line breaks
  1002. (goto-char (point-min))
  1003. (while (re-search-forward "\\\\\\\\" nil t)
  1004. (add-text-properties (match-beginning 0) (match-end 0)
  1005. '(org-protected t)))
  1006. ;; Convert LaTeX to \LaTeX{}
  1007. (goto-char (point-min))
  1008. (let ((case-fold-search nil) rpl)
  1009. (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
  1010. (replace-match (org-export-latex-protect-string
  1011. (concat (match-string 1) "\\LaTeX{}")) t t)))
  1012. ;; Convert horizontal rules
  1013. (goto-char (point-min))
  1014. (while (re-search-forward "^----+.$" nil t)
  1015. (replace-match (org-export-latex-protect-string "\\hrule") t t))
  1016. ;; Protect LaTeX \commands{...}
  1017. (goto-char (point-min))
  1018. (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
  1019. (add-text-properties (match-beginning 0) (match-end 0)
  1020. '(org-protected t)))
  1021. ;; Replace radio links
  1022. (goto-char (point-min))
  1023. (while (re-search-forward
  1024. (concat "<<<?" org-latex-all-targets-regexp
  1025. ">>>?\\((INVISIBLE)\\)?") nil t)
  1026. (replace-match
  1027. (org-export-latex-protect-string
  1028. (format "\\label{%s}%s"(match-string 1)
  1029. (if (match-string 2) "" (match-string 1)))) t t))
  1030. ;; Delete @<...> constructs
  1031. (goto-char (point-min))
  1032. ;; Thanks to Daniel Clemente for this regexp
  1033. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  1034. (replace-match ""))
  1035. ;; When converting to LaTeX, replace footnotes
  1036. ;; FIXME: don't protect footnotes from conversion
  1037. (when (plist-get org-latex-options-plist :footnotes)
  1038. (goto-char (point-min))
  1039. (while (re-search-forward "\\[[0-9]+\\]" nil t)
  1040. (when (save-match-data
  1041. (save-excursion (beginning-of-line)
  1042. (looking-at "[^:|#]")))
  1043. (let ((foot-beg (match-beginning 0))
  1044. (foot-end (match-end 0))
  1045. (foot-prefix (match-string 0))
  1046. footnote footnote-rpl)
  1047. (when (and (re-search-forward (regexp-quote foot-prefix) nil t))
  1048. (replace-match "")
  1049. (let ((end (save-excursion
  1050. (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
  1051. (match-beginning 0) (point-max)))))
  1052. (setq footnote
  1053. (concat
  1054. (org-trim (buffer-substring (point) end))
  1055. ;; FIXME stupid workaround for cases where
  1056. ;; `org-bracket-link-analytic-regexp' matches
  1057. ;; }. as part of the link.
  1058. " "))
  1059. (delete-region (point) end)))
  1060. (goto-char foot-beg)
  1061. (delete-region foot-beg foot-end)
  1062. (setq footnote-rpl (format "\\footnote{%s}" footnote))
  1063. (add-text-properties 0 10 '(org-protected t) footnote-rpl)
  1064. (add-text-properties (1- (length footnote-rpl))
  1065. (length footnote-rpl)
  1066. '(org-protected t) footnote-rpl)
  1067. (insert footnote-rpl))))
  1068. ;; Replace footnote section tag for LaTeX
  1069. (goto-char (point-min))
  1070. (while (re-search-forward
  1071. (concat "^" footnote-section-tag-regexp) nil t)
  1072. (replace-match ""))))
  1073. (provide 'org-export-latex)
  1074. ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
  1075. ;;; org-export-latex.el ends here