org-latex.el 65 KB

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