org-latex.el 63 KB

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