org-latex.el 84 KB

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