org-latex.el 66 KB

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