org-latex.el 87 KB

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