org-latex.el 73 KB

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