org-latex.el 87 KB

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