org-export-latex.el 54 KB

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