org-latex.el 62 KB

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