org-latex.el 83 KB

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