org-latex.el 89 KB

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