org-latex.el 67 KB

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