org-export-latex.el 50 KB

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