org-latex.el 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912
  1. ;;; org-latex.el --- LaTeX exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-latex.el
  7. ;; Author: Bastien Guerry <bzg AT gnu DOT org>
  8. ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
  9. ;; Keywords: org, wp, tex
  10. ;; Description: Converts an org-mode buffer into LaTeX
  11. ;; This file is part of GNU Emacs.
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;; Commentary:
  23. ;;
  24. ;; This library implements a LaTeX exporter for org-mode.
  25. ;;
  26. ;; It is part of Org and will be autoloaded
  27. ;;
  28. ;; The interactive functions are similar to those of the HTML exporter:
  29. ;;
  30. ;; M-x `org-export-as-latex'
  31. ;; M-x `org-export-as-pdf'
  32. ;; M-x `org-export-as-pdf-and-open'
  33. ;; M-x `org-export-as-latex-batch'
  34. ;; M-x `org-export-as-latex-to-buffer'
  35. ;; M-x `org-export-region-as-latex'
  36. ;; M-x `org-replace-region-by-latex'
  37. ;;
  38. ;;; Code:
  39. (eval-when-compile
  40. (require 'cl))
  41. (require 'footnote)
  42. (require 'org)
  43. (require 'org-exp)
  44. (require 'org-macs)
  45. (require 'org-beamer)
  46. ;;; Variables:
  47. (defvar org-export-latex-class nil)
  48. (defvar org-export-latex-class-options nil)
  49. (defvar org-export-latex-header nil)
  50. (defvar org-export-latex-append-header nil)
  51. (defvar org-export-latex-options-plist nil)
  52. (defvar org-export-latex-todo-keywords-1 nil)
  53. (defvar org-export-latex-complex-heading-re nil)
  54. (defvar org-export-latex-not-done-keywords nil)
  55. (defvar org-export-latex-done-keywords nil)
  56. (defvar org-export-latex-display-custom-times nil)
  57. (defvar org-export-latex-all-targets-re nil)
  58. (defvar org-export-latex-add-level 0)
  59. (defvar org-export-latex-footmark-seen nil
  60. "List of footnotes markers seen so far by exporter.")
  61. (defvar org-export-latex-sectioning "")
  62. (defvar org-export-latex-sectioning-depth 0)
  63. (defvar org-export-latex-special-keyword-regexp
  64. (concat "\\<\\(" org-scheduled-string "\\|"
  65. org-deadline-string "\\|"
  66. org-closed-string"\\)")
  67. "Regexp matching special time planning keywords plus the time after it.")
  68. (defvar org-re-quote) ; dynamically scoped from org.el
  69. (defvar org-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. :version "24.1"
  188. :type '(repeat
  189. (cons
  190. (string :tag "Derived from buffer")
  191. (string :tag "Use this instead"))))
  192. (defcustom org-export-latex-emphasis-alist
  193. '(("*" "\\textbf{%s}" nil)
  194. ("/" "\\emph{%s}" nil)
  195. ("_" "\\underline{%s}" nil)
  196. ("+" "\\st{%s}" nil)
  197. ("=" "\\protectedtexttt" t)
  198. ("~" "\\verb" t))
  199. "Alist of LaTeX expressions to convert emphasis fontifiers.
  200. Each element of the list is a list of three elements.
  201. The first element is the character used as a marker for fontification.
  202. The second element is a format string to wrap fontified text with.
  203. If it is \"\\verb\", Org will automatically select a delimiter
  204. character that is not in the string. \"\\protectedtexttt\" will use \\texttt
  205. to typeset and try to protect special characters.
  206. The third element decides whether to protect converted text from other
  207. conversions."
  208. :group 'org-export-latex
  209. :type 'alist)
  210. (defcustom org-export-latex-title-command "\\maketitle"
  211. "The command used to insert the title just after \\begin{document}.
  212. If this string contains the formatting specification \"%s\" then
  213. it will be used as a format string, passing the title as an
  214. argument."
  215. :group 'org-export-latex
  216. :type 'string)
  217. (defcustom org-export-latex-import-inbuffer-stuff nil
  218. "Non-nil means define TeX macros for Org's inbuffer definitions.
  219. For example \orgTITLE for #+TITLE."
  220. :group 'org-export-latex
  221. :type 'boolean)
  222. (defcustom org-export-latex-date-format
  223. "\\today"
  224. "Format string for \\date{...}."
  225. :group 'org-export-latex
  226. :type 'string)
  227. (defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}"
  228. "Markup for TODO keywords, as a printf format.
  229. This can be a single format for all keywords, a cons cell with separate
  230. formats for not-done and done states, or an association list with setup
  231. for individual keywords. If a keyword shows up for which there is no
  232. markup defined, the first one in the association list will be used."
  233. :group 'org-export-latex
  234. :type '(choice
  235. (string :tag "Default")
  236. (cons :tag "Distinguish undone and done"
  237. (string :tag "Not-DONE states")
  238. (string :tag "DONE states"))
  239. (repeat :tag "Per keyword markup"
  240. (cons
  241. (string :tag "Keyword")
  242. (string :tag "Markup")))))
  243. (defcustom org-export-latex-tag-markup "\\textbf{%s}"
  244. "Markup for tags, as a printf format."
  245. :group 'org-export-latex
  246. :version "24.1"
  247. :type 'string)
  248. (defcustom org-export-latex-timestamp-markup "\\textit{%s}"
  249. "A printf format string to be applied to time stamps."
  250. :group 'org-export-latex
  251. :type 'string)
  252. (defcustom org-export-latex-timestamp-inactive-markup "\\textit{%s}"
  253. "A printf format string to be applied to inactive time stamps."
  254. :group 'org-export-latex
  255. :version "24.1"
  256. :type 'string)
  257. (defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}"
  258. "A printf format string to be applied to time stamps."
  259. :group 'org-export-latex
  260. :type 'string)
  261. (defcustom org-export-latex-href-format "\\href{%s}{%s}"
  262. "A printf format string to be applied to href links.
  263. The format must contain either two %s instances or just one.
  264. If it contains two %s instances, the first will be filled with
  265. the link, the second with the link description. If it contains
  266. only one, the %s will be filled with the link."
  267. :group 'org-export-latex
  268. :version "24.1"
  269. :type 'string)
  270. (defcustom org-export-latex-hyperref-format "\\hyperref[%s]{%s}"
  271. "A printf format string to be applied to hyperref links.
  272. The format must contain one or two %s instances. The first one
  273. will be filled with the link, the second with its description."
  274. :group 'org-export-latex
  275. :version "24.1"
  276. :type 'string)
  277. (defcustom org-export-latex-hyperref-options-format
  278. "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={Emacs Org-mode version %s}}\n"
  279. "A format string for hyperref options.
  280. When non-nil, it must contain three %s format specifications
  281. which will respectively be replaced by the document's keywords,
  282. its description and the Org's version number, as a string. Set
  283. this option to the empty string if you don't want to include
  284. hyperref options altogether."
  285. :type 'string
  286. :version "24.3"
  287. :group 'org-export-latex)
  288. (defcustom org-export-latex-footnote-separator "\\textsuperscript{,}\\,"
  289. "Text used to separate footnotes."
  290. :group 'org-export-latex
  291. :version "24.1"
  292. :type 'string)
  293. (defcustom org-export-latex-quotes
  294. '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
  295. ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
  296. "Alist for quotes to use when converting english double-quotes.
  297. The CAR of each item in this alist is the language code.
  298. The CDR of each item in this alist is a list of three CONS:
  299. - the first CONS defines the opening quote;
  300. - the second CONS defines the closing quote;
  301. - the last CONS defines single quotes.
  302. For each item in a CONS, the first string is a regexp
  303. for allowed characters before/after the quote, the second
  304. string defines the replacement string for this quote."
  305. :group 'org-export-latex
  306. :version "24.1"
  307. :type '(list
  308. (cons :tag "Opening quote"
  309. (string :tag "Regexp for char before")
  310. (string :tag "Replacement quote "))
  311. (cons :tag "Closing quote"
  312. (string :tag "Regexp for char after ")
  313. (string :tag "Replacement quote "))
  314. (cons :tag "Single quote"
  315. (string :tag "Regexp for char before")
  316. (string :tag "Replacement quote "))))
  317. (defcustom org-export-latex-tables-verbatim nil
  318. "When non-nil, tables are exported verbatim."
  319. :group 'org-export-latex
  320. :type 'boolean)
  321. (defcustom org-export-latex-tables-centered t
  322. "When non-nil, tables are exported in a center environment."
  323. :group 'org-export-latex
  324. :type 'boolean)
  325. (defcustom org-export-latex-table-caption-above t
  326. "When non-nil, the caption is set above the table. When nil,
  327. the caption is set below the table."
  328. :group 'org-export-latex
  329. :version "24.1"
  330. :type 'boolean)
  331. (defcustom org-export-latex-tables-column-borders nil
  332. "When non-nil, grouping columns can cause outer vertical lines in tables.
  333. When nil, grouping causes only separation lines between groups."
  334. :group 'org-export-latex
  335. :type 'boolean)
  336. (defcustom org-export-latex-tables-tstart nil
  337. "LaTeX command for top rule for tables."
  338. :group 'org-export-latex
  339. :version "24.1"
  340. :type '(choice
  341. (const :tag "Nothing" nil)
  342. (string :tag "String")
  343. (const :tag "Booktabs default: \\toprule" "\\toprule")))
  344. (defcustom org-export-latex-tables-hline "\\hline"
  345. "LaTeX command to use for a rule somewhere in the middle of a table."
  346. :group 'org-export-latex
  347. :version "24.1"
  348. :type '(choice
  349. (string :tag "String")
  350. (const :tag "Standard: \\hline" "\\hline")
  351. (const :tag "Booktabs default: \\midrule" "\\midrule")))
  352. (defcustom org-export-latex-tables-tend nil
  353. "LaTeX command for bottom rule for tables."
  354. :group 'org-export-latex
  355. :version "24.1"
  356. :type '(choice
  357. (const :tag "Nothing" nil)
  358. (string :tag "String")
  359. (const :tag "Booktabs default: \\bottomrule" "\\bottomrule")))
  360. (defcustom org-export-latex-low-levels 'itemize
  361. "How to convert sections below the current level of sectioning.
  362. This is specified by the `org-export-headline-levels' option or the
  363. value of \"H:\" in Org's #+OPTION line.
  364. This can be either nil (skip the sections), `description', `itemize',
  365. or `enumerate' (convert the sections as the corresponding list type), or
  366. a string to be used instead of \\section{%s}. In this latter case,
  367. the %s stands here for the inserted headline and is mandatory.
  368. It may also be a list of three string to define a user-defined environment
  369. that should be used. The first string should be the like
  370. \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
  371. to two occurrences of %s for the title and a label, respectively. The third
  372. string should be like \"\\end{itemize\"."
  373. :group 'org-export-latex
  374. :type '(choice (const :tag "Ignore" nil)
  375. (const :tag "Convert as descriptive list" description)
  376. (const :tag "Convert as itemized list" itemize)
  377. (const :tag "Convert as enumerated list" enumerate)
  378. (list :tag "User-defined environment"
  379. :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s")
  380. (string :tag "Start")
  381. (string :tag "End")
  382. (string :tag "item"))
  383. (string :tag "Use a section string" :value "\\subparagraph{%s}")))
  384. (defcustom org-export-latex-list-parameters
  385. '(:cbon "$\\boxtimes$" :cboff "$\\Box$" :cbtrans "$\\boxminus$")
  386. "Parameters for the LaTeX list exporter.
  387. These parameters will be passed on to `org-list-to-latex', which in turn
  388. will pass them (combined with the LaTeX default list parameters) to
  389. `org-list-to-generic'."
  390. :group 'org-export-latex
  391. :type 'plist)
  392. (defcustom org-export-latex-verbatim-wrap
  393. '("\\begin{verbatim}\n" . "\\end{verbatim}")
  394. "Environment to be wrapped around a fixed-width section in LaTeX export.
  395. This is a cons with two strings, to be added before and after the
  396. fixed-with text.
  397. Defaults to \\begin{verbatim} and \\end{verbatim}."
  398. :group 'org-export-translation
  399. :group 'org-export-latex
  400. :type '(cons (string :tag "Open")
  401. (string :tag "Close")))
  402. (defcustom org-export-latex-listings nil
  403. "Non-nil means export source code using the listings package.
  404. This package will fontify source code, possibly even with color.
  405. If you want to use this, you also need to make LaTeX use the
  406. listings package, and if you want to have color, the color
  407. package. Just add these to `org-export-latex-packages-alist',
  408. for example using customize, or with something like
  409. (require 'org-latex)
  410. (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
  411. (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))
  412. Alternatively,
  413. (setq org-export-latex-listings 'minted)
  414. causes source code to be exported using the minted package as
  415. opposed to listings. If you want to use minted, you need to add
  416. the minted package to `org-export-latex-packages-alist', for
  417. example using customize, or with
  418. (require 'org-latex)
  419. (add-to-list 'org-export-latex-packages-alist '(\"\" \"minted\"))
  420. In addition, it is necessary to install
  421. pygments (http://pygments.org), and to configure the variable
  422. `org-latex-to-pdf-process' so that the -shell-escape option is
  423. passed to pdflatex.
  424. "
  425. :group 'org-export-latex
  426. :type 'boolean)
  427. (defcustom org-export-latex-listings-langs
  428. '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
  429. (c "C") (cc "C++")
  430. (fortran "fortran")
  431. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  432. (html "HTML") (xml "XML")
  433. (tex "TeX") (latex "TeX")
  434. (shell-script "bash")
  435. (gnuplot "Gnuplot")
  436. (ocaml "Caml") (caml "Caml")
  437. (sql "SQL") (sqlite "sql"))
  438. "Alist mapping languages to their listing language counterpart.
  439. The key is a symbol, the major mode symbol without the \"-mode\".
  440. The value is the string that should be inserted as the language parameter
  441. for the listings package. If the mode name and the listings name are
  442. the same, the language does not need an entry in this list - but it does not
  443. hurt if it is present."
  444. :group 'org-export-latex
  445. :type '(repeat
  446. (list
  447. (symbol :tag "Major mode ")
  448. (string :tag "Listings language"))))
  449. (defcustom org-export-latex-listings-w-names t
  450. "Non-nil means export names of named code blocks.
  451. Code blocks exported with the listings package (controlled by the
  452. `org-export-latex-listings' variable) can be named in the style
  453. of noweb."
  454. :group 'org-export-latex
  455. :version "24.1"
  456. :type 'boolean)
  457. (defcustom org-export-latex-minted-langs
  458. '((emacs-lisp "common-lisp")
  459. (cc "c++")
  460. (cperl "perl")
  461. (shell-script "bash")
  462. (caml "ocaml"))
  463. "Alist mapping languages to their minted language counterpart.
  464. The key is a symbol, the major mode symbol without the \"-mode\".
  465. The value is the string that should be inserted as the language parameter
  466. for the minted package. If the mode name and the listings name are
  467. the same, the language does not need an entry in this list - but it does not
  468. hurt if it is present.
  469. Note that minted uses all lower case for language identifiers,
  470. and that the full list of language identifiers can be obtained
  471. with:
  472. pygmentize -L lexers
  473. "
  474. :group 'org-export-latex
  475. :version "24.1"
  476. :type '(repeat
  477. (list
  478. (symbol :tag "Major mode ")
  479. (string :tag "Listings language"))))
  480. (defcustom org-export-latex-listings-options nil
  481. "Association list of options for the latex listings package.
  482. These options are supplied as a comma-separated list to the
  483. \\lstset command. Each element of the association list should be
  484. a list containing two strings: the name of the option, and the
  485. value. For example,
  486. (setq org-export-latex-listings-options
  487. '((\"basicstyle\" \"\\small\")
  488. (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
  489. will typeset the code in a small size font with underlined, bold
  490. black keywords.
  491. Note that the same options will be applied to blocks of all
  492. languages."
  493. :group 'org-export-latex
  494. :version "24.1"
  495. :type '(repeat
  496. (list
  497. (string :tag "Listings option name ")
  498. (string :tag "Listings option value"))))
  499. (defcustom org-export-latex-minted-options nil
  500. "Association list of options for the latex minted package.
  501. These options are supplied within square brackets in
  502. \\begin{minted} environments. Each element of the alist should be
  503. a list containing two strings: the name of the option, and the
  504. value. For example,
  505. (setq org-export-latex-minted-options
  506. '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
  507. will result in src blocks being exported with
  508. \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
  509. as the start of the minted environment. Note that the same
  510. options will be applied to blocks of all languages."
  511. :group 'org-export-latex
  512. :version "24.1"
  513. :type '(repeat
  514. (list
  515. (string :tag "Minted option name ")
  516. (string :tag "Minted option value"))))
  517. (defvar org-export-latex-custom-lang-environments nil
  518. "Association list mapping languages to language-specific latex
  519. environments used during export of src blocks by the listings
  520. and minted latex packages. For example,
  521. (setq org-export-latex-custom-lang-environments
  522. '((python \"pythoncode\")))
  523. would have the effect that if org encounters begin_src python
  524. during latex export it will output
  525. \\begin{pythoncode}
  526. <src block body>
  527. \\end{pythoncode}")
  528. (defcustom org-export-latex-remove-from-headlines
  529. '(:todo nil :priority nil :tags nil)
  530. "A plist of keywords to remove from headlines. OBSOLETE.
  531. Non-nil means remove this keyword type from the headline.
  532. Don't remove the keys, just change their values.
  533. Obsolete, this variable is no longer used. Use the separate
  534. variables `org-export-with-todo-keywords', `org-export-with-priority',
  535. and `org-export-with-tags' instead."
  536. :type 'plist
  537. :group 'org-export-latex)
  538. (defcustom org-export-latex-image-default-option "width=.9\\linewidth"
  539. "Default option for images."
  540. :group 'org-export-latex
  541. :type 'string)
  542. (defcustom org-latex-default-figure-position "htb"
  543. "Default position for latex figures."
  544. :group 'org-export-latex
  545. :version "24.1"
  546. :type 'string)
  547. (defcustom org-export-latex-tabular-environment "tabular"
  548. "Default environment used to build tables."
  549. :group 'org-export-latex
  550. :version "24.1"
  551. :type 'string)
  552. (defcustom org-export-latex-link-with-unknown-path-format "\\texttt{%s}"
  553. "Format string for links with unknown path type."
  554. :group 'org-export-latex
  555. :version "24.3"
  556. :type 'string)
  557. (defcustom org-export-latex-inline-images 'maybe
  558. "Non-nil means inline images into exported LaTeX pages.
  559. If this option is `maybe', then images in links with an empty
  560. description will be inlined, while images with a description will
  561. be linked only."
  562. :group 'org-export-html
  563. :version "24.3"
  564. :type '(choice (const :tag "Never" nil)
  565. (const :tag "Always" t)
  566. (const :tag "When there is no description" maybe)))
  567. (defcustom org-export-latex-inline-image-extensions
  568. '("pdf" "jpeg" "jpg" "png" "ps" "eps")
  569. "Extensions of image files that can be inlined into LaTeX.
  570. Note that the image extension *actually* allowed depend on the way the
  571. LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
  572. are OK. When processing through dvi to Postscript, only ps and eps are
  573. allowed. The default we use here encompasses both."
  574. :group 'org-export-latex
  575. :type '(repeat (string :tag "Extension")))
  576. (defcustom org-export-latex-coding-system nil
  577. "Coding system for the exported LaTeX file."
  578. :group 'org-export-latex
  579. :type 'coding-system)
  580. (defgroup org-export-pdf nil
  581. "Options for exporting Org-mode files to PDF, via LaTeX."
  582. :tag "Org Export PDF"
  583. :group 'org-export-latex
  584. :group 'org-export)
  585. (defcustom org-latex-to-pdf-process
  586. '("pdflatex -interaction nonstopmode -output-directory %o %f"
  587. "pdflatex -interaction nonstopmode -output-directory %o %f"
  588. "pdflatex -interaction nonstopmode -output-directory %o %f")
  589. "Commands to process a LaTeX file to a PDF file and process latex
  590. fragments to pdf files.By default,this is a list of strings,and each of
  591. strings will be given to the shell as a command. %f in the command will
  592. be replaced by the full file name, %b by the file base name (i.e. without
  593. extension) and %o by the base directory of the file.
  594. If you set `org-create-formula-image-program'
  595. `org-export-with-LaTeX-fragments' to 'imagemagick, you can add a
  596. sublist which contains your own command(s) for LaTeX fragments
  597. previewing, like this:
  598. '(\"xelatex -interaction nonstopmode -output-directory %o %f\"
  599. \"xelatex -interaction nonstopmode -output-directory %o %f\"
  600. ;; use below command(s) to convert latex fragments
  601. (\"xelatex %f\"))
  602. With no such sublist, the default command used to convert LaTeX
  603. fragments will be the first string in the list.
  604. The reason why this is a list is that it usually takes several runs of
  605. `pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever
  606. mechanism to detect which of these commands have to be run to get to a stable
  607. result, and it also does not do any error checking.
  608. By default, Org uses 3 runs of `pdflatex' to do the processing. If you
  609. have texi2dvi on your system and if that does not cause the infamous
  610. egrep/locale bug:
  611. http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
  612. then `texi2dvi' is the superior choice. Org does offer it as one
  613. of the customize options.
  614. Alternatively, this may be a Lisp function that does the processing, so you
  615. could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
  616. This function should accept the file name as its single argument."
  617. :group 'org-export-pdf
  618. :type '(choice
  619. (repeat :tag "Shell command sequence"
  620. (string :tag "Shell command"))
  621. (const :tag "2 runs of pdflatex"
  622. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  623. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  624. (const :tag "3 runs of pdflatex"
  625. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  626. "pdflatex -interaction nonstopmode -output-directory %o %f"
  627. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  628. (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
  629. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  630. "bibtex %b"
  631. "pdflatex -interaction nonstopmode -output-directory %o %f"
  632. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  633. (const :tag "2 runs of xelatex"
  634. ("xelatex -interaction nonstopmode -output-directory %o %f"
  635. "xelatex -interaction nonstopmode -output-directory %o %f"))
  636. (const :tag "3 runs of xelatex"
  637. ("xelatex -interaction nonstopmode -output-directory %o %f"
  638. "xelatex -interaction nonstopmode -output-directory %o %f"
  639. "xelatex -interaction nonstopmode -output-directory %o %f"))
  640. (const :tag "xelatex,bibtex,xelatex,xelatex"
  641. ("xelatex -interaction nonstopmode -output-directory %o %f"
  642. "bibtex %b"
  643. "xelatex -interaction nonstopmode -output-directory %o %f"
  644. "xelatex -interaction nonstopmode -output-directory %o %f"))
  645. (const :tag "texi2dvi"
  646. ("texi2dvi -p -b -c -V %f"))
  647. (const :tag "rubber"
  648. ("rubber -d --into %o %f"))
  649. (function)))
  650. (defcustom org-export-pdf-logfiles
  651. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  652. "The list of file extensions to consider as LaTeX logfiles."
  653. :group 'org-export-pdf
  654. :version "24.1"
  655. :type '(repeat (string :tag "Extension")))
  656. (defcustom org-export-pdf-remove-logfiles t
  657. "Non-nil means remove the logfiles produced by PDF production.
  658. These are the .aux, .log, .out, and .toc files."
  659. :group 'org-export-pdf
  660. :type 'boolean)
  661. ;;; Hooks
  662. (defvar org-export-latex-after-initial-vars-hook nil
  663. "Hook run before LaTeX export.
  664. The exact moment is after the initial variables like org-export-latex-class
  665. have been determined from the environment.")
  666. (defvar org-export-latex-after-blockquotes-hook nil
  667. "Hook run during LaTeX export, after blockquote, verse, center are done.")
  668. (defvar org-export-latex-final-hook nil
  669. "Hook run in the finalized LaTeX buffer.")
  670. (defvar org-export-latex-after-save-hook nil
  671. "Hook run in the finalized LaTeX buffer, after it has been saved.")
  672. ;;; Autoload functions:
  673. ;;;###autoload
  674. (defun org-export-as-latex-batch ()
  675. "Call `org-export-as-latex', may be used in batch processing.
  676. For example:
  677. emacs --batch
  678. --load=$HOME/lib/emacs/org.el
  679. --eval \"(setq org-export-headline-levels 2)\"
  680. --visit=MyFile --funcall org-export-as-latex-batch"
  681. (org-export-as-latex org-export-headline-levels))
  682. ;;;###autoload
  683. (defun org-export-as-latex-to-buffer (arg)
  684. "Call `org-export-as-latex` with output to a temporary buffer.
  685. No file is created. The prefix ARG is passed through to `org-export-as-latex'."
  686. (interactive "P")
  687. (org-export-as-latex arg nil "*Org LaTeX Export*")
  688. (when org-export-show-temporary-export-buffer
  689. (switch-to-buffer-other-window "*Org LaTeX Export*")))
  690. ;;;###autoload
  691. (defun org-replace-region-by-latex (beg end)
  692. "Replace the region from BEG to END with its LaTeX export.
  693. It assumes the region has `org-mode' syntax, and then convert it to
  694. LaTeX. This can be used in any buffer. For example, you could
  695. write an itemized list in `org-mode' syntax in an LaTeX buffer and
  696. then use this command to convert it."
  697. (interactive "r")
  698. (let (reg latex buf)
  699. (save-window-excursion
  700. (if (derived-mode-p 'org-mode)
  701. (setq latex (org-export-region-as-latex
  702. beg end t 'string))
  703. (setq reg (buffer-substring beg end)
  704. buf (get-buffer-create "*Org tmp*"))
  705. (with-current-buffer buf
  706. (erase-buffer)
  707. (insert reg)
  708. (org-mode)
  709. (setq latex (org-export-region-as-latex
  710. (point-min) (point-max) t 'string)))
  711. (kill-buffer buf)))
  712. (delete-region beg end)
  713. (insert latex)))
  714. ;;;###autoload
  715. (defun org-export-region-as-latex (beg end &optional body-only buffer)
  716. "Convert region from BEG to END in `org-mode' buffer to LaTeX.
  717. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  718. contents, and only produce the region of converted text, useful for
  719. cut-and-paste operations.
  720. If BUFFER is a buffer or a string, use/create that buffer as a target
  721. of the converted LaTeX. If BUFFER is the symbol `string', return the
  722. produced LaTeX as a string and leave no buffer behind. For example,
  723. a Lisp program could call this function in the following way:
  724. (setq latex (org-export-region-as-latex beg end t 'string))
  725. When called interactively, the output buffer is selected, and shown
  726. in a window. A non-interactive call will only return the buffer."
  727. (interactive "r\nP")
  728. (when (org-called-interactively-p 'any)
  729. (setq buffer "*Org LaTeX Export*"))
  730. (let ((transient-mark-mode t) (zmacs-regions t)
  731. ext-plist rtn)
  732. (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
  733. (goto-char end)
  734. (set-mark (point)) ;; to activate the region
  735. (goto-char beg)
  736. (setq rtn (org-export-as-latex
  737. nil ext-plist
  738. buffer body-only))
  739. (if (fboundp 'deactivate-mark) (deactivate-mark))
  740. (if (and (org-called-interactively-p 'any) (bufferp rtn))
  741. (switch-to-buffer-other-window rtn)
  742. rtn)))
  743. ;;;###autoload
  744. (defun org-export-as-latex (arg &optional ext-plist to-buffer body-only pub-dir)
  745. "Export current buffer to a LaTeX file.
  746. If there is an active region, export only the region. The prefix
  747. ARG specifies how many levels of the outline should become
  748. headlines. The default is 3. Lower levels will be exported
  749. depending on `org-export-latex-low-levels'. The default is to
  750. convert them as description lists.
  751. EXT-PLIST is a property list with external parameters overriding
  752. org-mode's default settings, but still inferior to file-local settings.
  753. When TO-BUFFER is non-nil, create a buffer with that name and export
  754. to that buffer. If TO-BUFFER is the symbol `string', don't leave any
  755. buffer behind and just return the resulting LaTeX as a string, with
  756. no LaTeX header.
  757. When BODY-ONLY is set, don't produce the file header and footer,
  758. simply return the content of \\begin{document}...\\end{document},
  759. without even the \\begin{document} and \\end{document} commands.
  760. When PUB-DIR is set, use this as the publishing directory."
  761. (interactive "P")
  762. (when (and (not body-only) arg (listp arg)) (setq body-only t))
  763. (run-hooks 'org-export-first-hook)
  764. ;; Make sure we have a file name when we need it.
  765. (when (and (not (or to-buffer body-only))
  766. (not buffer-file-name))
  767. (if (buffer-base-buffer)
  768. (org-set-local 'buffer-file-name
  769. (with-current-buffer (buffer-base-buffer)
  770. buffer-file-name))
  771. (error "Need a file name to be able to export")))
  772. (message "Exporting to LaTeX...")
  773. (org-unmodified
  774. (let ((inhibit-read-only t))
  775. (remove-text-properties (point-min) (point-max)
  776. '(:org-license-to-kill nil))))
  777. (org-update-radio-target-regexp)
  778. (org-export-latex-set-initial-vars ext-plist arg)
  779. (setq org-export-opt-plist org-export-latex-options-plist
  780. org-export-footnotes-data (org-footnote-all-labels 'with-defs)
  781. org-export-footnotes-seen nil
  782. org-export-latex-footmark-seen nil)
  783. (org-install-letbind)
  784. (run-hooks 'org-export-latex-after-initial-vars-hook)
  785. (let* ((wcf (current-window-configuration))
  786. (opt-plist
  787. (org-export-process-option-filters org-export-latex-options-plist))
  788. (region-p (org-region-active-p))
  789. (rbeg (and region-p (region-beginning)))
  790. (rend (and region-p (region-end)))
  791. (subtree-p
  792. (if (plist-get opt-plist :ignore-subtree-p)
  793. nil
  794. (when region-p
  795. (save-excursion
  796. (goto-char rbeg)
  797. (and (org-at-heading-p)
  798. (>= (org-end-of-subtree t t) rend))))))
  799. (opt-plist (setq org-export-opt-plist
  800. (if subtree-p
  801. (org-export-add-subtree-options opt-plist rbeg)
  802. opt-plist)))
  803. ;; Make sure the variable contains the updated values.
  804. (org-export-latex-options-plist (setq org-export-opt-plist opt-plist))
  805. ;; The following two are dynamically scoped into other
  806. ;; routines below.
  807. (org-current-export-dir
  808. (or pub-dir (org-export-directory :html opt-plist)))
  809. (org-current-export-file buffer-file-name)
  810. (title (or (and subtree-p (org-export-get-title-from-subtree))
  811. (plist-get opt-plist :title)
  812. (and (not
  813. (plist-get opt-plist :skip-before-1st-heading))
  814. (org-export-grab-title-from-buffer))
  815. (and buffer-file-name
  816. (file-name-sans-extension
  817. (file-name-nondirectory buffer-file-name)))
  818. "No Title"))
  819. (filename
  820. (and (not to-buffer)
  821. (concat
  822. (file-name-as-directory
  823. (or pub-dir
  824. (org-export-directory :LaTeX org-export-latex-options-plist)))
  825. (file-name-sans-extension
  826. (or (and subtree-p
  827. (org-entry-get rbeg "EXPORT_FILE_NAME" t))
  828. (file-name-nondirectory ;sans-extension
  829. (or buffer-file-name
  830. (error "Don't know which export file to use")))))
  831. ".tex")))
  832. (filename
  833. (and filename
  834. (if (equal (file-truename filename)
  835. (file-truename (or buffer-file-name "dummy.org")))
  836. (concat filename ".tex")
  837. filename)))
  838. (auto-insert nil); Avoid any auto-insert stuff for the new file
  839. (TeX-master (boundp 'TeX-master))
  840. (buffer (if to-buffer
  841. (if (eq to-buffer 'string)
  842. (get-buffer-create "*Org LaTeX Export*")
  843. (get-buffer-create to-buffer))
  844. (find-file-noselect filename)))
  845. (odd org-odd-levels-only)
  846. (header (org-export-latex-make-header title opt-plist))
  847. (skip (cond (subtree-p nil)
  848. (region-p nil)
  849. (t (plist-get opt-plist :skip-before-1st-heading))))
  850. (text (plist-get opt-plist :text))
  851. (org-export-preprocess-hook
  852. (cons
  853. `(lambda () (org-set-local 'org-complex-heading-regexp
  854. ,org-export-latex-complex-heading-re))
  855. org-export-preprocess-hook))
  856. (first-lines (if skip "" (org-export-latex-first-lines
  857. opt-plist
  858. (if subtree-p
  859. (save-excursion
  860. (goto-char rbeg)
  861. (point-at-bol 2))
  862. rbeg)
  863. (if region-p rend))))
  864. (coding-system (and (boundp 'buffer-file-coding-system)
  865. buffer-file-coding-system))
  866. (coding-system-for-write (or org-export-latex-coding-system
  867. coding-system))
  868. (save-buffer-coding-system (or org-export-latex-coding-system
  869. coding-system))
  870. (region (buffer-substring
  871. (if region-p (region-beginning) (point-min))
  872. (if region-p (region-end) (point-max))))
  873. (text
  874. (and text (string-match "\\S-" text)
  875. (org-export-preprocess-string
  876. text
  877. :emph-multiline t
  878. :for-backend 'latex
  879. :comments nil
  880. :tags (plist-get opt-plist :tags)
  881. :priority (plist-get opt-plist :priority)
  882. :footnotes (plist-get opt-plist :footnotes)
  883. :drawers (plist-get opt-plist :drawers)
  884. :timestamps (plist-get opt-plist :timestamps)
  885. :todo-keywords (plist-get opt-plist :todo-keywords)
  886. :tasks (plist-get opt-plist :tasks)
  887. :add-text nil
  888. :skip-before-1st-heading skip
  889. :select-tags nil
  890. :exclude-tags nil
  891. :LaTeX-fragments nil)))
  892. (string-for-export
  893. (org-export-preprocess-string
  894. region
  895. :emph-multiline t
  896. :for-backend 'latex
  897. :comments nil
  898. :tags (plist-get opt-plist :tags)
  899. :priority (plist-get opt-plist :priority)
  900. :footnotes (plist-get opt-plist :footnotes)
  901. :drawers (plist-get opt-plist :drawers)
  902. :timestamps (plist-get opt-plist :timestamps)
  903. :todo-keywords (plist-get opt-plist :todo-keywords)
  904. :tasks (plist-get opt-plist :tasks)
  905. :add-text (if (eq to-buffer 'string) nil text)
  906. :skip-before-1st-heading skip
  907. :select-tags (plist-get opt-plist :select-tags)
  908. :exclude-tags (plist-get opt-plist :exclude-tags)
  909. :LaTeX-fragments nil)))
  910. (set-buffer buffer)
  911. (erase-buffer)
  912. (org-install-letbind)
  913. (and (fboundp 'set-buffer-file-coding-system)
  914. (set-buffer-file-coding-system coding-system-for-write))
  915. ;; insert the header and initial document commands
  916. (unless (or (eq to-buffer 'string) body-only)
  917. (insert header))
  918. ;; insert text found in #+TEXT
  919. (when (and text (not (eq to-buffer 'string)))
  920. (insert (org-export-latex-content
  921. text '(lists tables fixed-width keywords))
  922. "\n\n"))
  923. ;; insert lines before the first headline
  924. (unless (or skip (string-match "^\\*" first-lines))
  925. (insert first-lines))
  926. ;; export the content of headlines
  927. (org-export-latex-global
  928. (with-temp-buffer
  929. (insert string-for-export)
  930. (goto-char (point-min))
  931. (when (re-search-forward "^\\(\\*+\\) " nil t)
  932. (let* ((asters (length (match-string 1)))
  933. (level (if odd (- asters 2) (- asters 1))))
  934. (setq org-export-latex-add-level
  935. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  936. (org-export-latex-parse-global level odd)))))
  937. ;; finalization
  938. (unless body-only (insert "\n\\end{document}"))
  939. ;; Attach description terms to the \item macro
  940. (goto-char (point-min))
  941. (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
  942. (delete-region (match-beginning 1) (match-end 1)))
  943. ;; Relocate the table of contents
  944. (goto-char (point-min))
  945. (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
  946. (goto-char (point-min))
  947. (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
  948. (replace-match ""))
  949. (goto-char (point-min))
  950. (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
  951. (replace-match "\\tableofcontents" t t)))
  952. ;; Cleanup forced line ends in items where they are not needed
  953. (goto-char (point-min))
  954. (while (re-search-forward
  955. "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
  956. nil t)
  957. (delete-region (match-beginning 1) (match-end 1)))
  958. (goto-char (point-min))
  959. (while (re-search-forward
  960. "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
  961. nil t)
  962. (if (looking-at "[\n \t]+")
  963. (replace-match "\n")))
  964. ;; Ensure we have a final newline
  965. (goto-char (point-max))
  966. (or (eq (char-before) ?\n)
  967. (insert ?\n))
  968. (run-hooks 'org-export-latex-final-hook)
  969. (if to-buffer
  970. (unless (eq major-mode 'latex-mode) (latex-mode))
  971. (save-buffer))
  972. (org-export-latex-fix-inputenc)
  973. (run-hooks 'org-export-latex-after-save-hook)
  974. (goto-char (point-min))
  975. (or (org-export-push-to-kill-ring "LaTeX")
  976. (message "Exporting to LaTeX...done"))
  977. (prog1
  978. (if (eq to-buffer 'string)
  979. (prog1 (buffer-substring (point-min) (point-max))
  980. (kill-buffer (current-buffer)))
  981. (current-buffer))
  982. (set-window-configuration wcf))))
  983. ;;;###autoload
  984. (defun org-export-as-pdf (arg &optional hidden ext-plist
  985. to-buffer body-only pub-dir)
  986. "Export as LaTeX, then process through to PDF."
  987. (interactive "P")
  988. (message "Exporting to PDF...")
  989. (let* ((wconfig (current-window-configuration))
  990. (lbuf (org-export-as-latex arg ext-plist to-buffer body-only pub-dir))
  991. (file (buffer-file-name lbuf))
  992. (base (file-name-sans-extension (buffer-file-name lbuf)))
  993. (pdffile (concat base ".pdf"))
  994. (cmds (if (eq org-export-latex-listings 'minted)
  995. ;; automatically add -shell-escape when needed
  996. (mapcar (lambda (cmd)
  997. (replace-regexp-in-string
  998. "pdflatex " "pdflatex -shell-escape " cmd))
  999. org-latex-to-pdf-process)
  1000. org-latex-to-pdf-process))
  1001. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  1002. (bibtex-p (with-current-buffer lbuf
  1003. (save-excursion
  1004. (goto-char (point-min))
  1005. (re-search-forward "\\\\bibliography{" nil t))))
  1006. cmd output-dir errors)
  1007. (with-current-buffer outbuf (erase-buffer))
  1008. (message (concat "Processing LaTeX file " file "..."))
  1009. (setq output-dir (file-name-directory file))
  1010. (with-current-buffer lbuf
  1011. (save-excursion
  1012. (if (and cmds (symbolp cmds))
  1013. (funcall cmds (shell-quote-argument file))
  1014. (while cmds
  1015. (setq cmd (pop cmds))
  1016. (cond
  1017. ((not (listp cmd))
  1018. (while (string-match "%b" cmd)
  1019. (setq cmd (replace-match
  1020. (save-match-data
  1021. (shell-quote-argument base))
  1022. t t cmd)))
  1023. (while (string-match "%f" cmd)
  1024. (setq cmd (replace-match
  1025. (save-match-data
  1026. (shell-quote-argument file))
  1027. t t cmd)))
  1028. (while (string-match "%o" cmd)
  1029. (setq cmd (replace-match
  1030. (save-match-data
  1031. (shell-quote-argument output-dir))
  1032. t t cmd)))
  1033. (shell-command cmd outbuf)))))))
  1034. (message (concat "Processing LaTeX file " file "...done"))
  1035. (setq errors (org-export-latex-get-error outbuf))
  1036. (if (not (file-exists-p pdffile))
  1037. (error (concat "PDF file " pdffile " was not produced"
  1038. (if errors (concat ":" errors "") "")))
  1039. (set-window-configuration wconfig)
  1040. (when org-export-pdf-remove-logfiles
  1041. (dolist (ext org-export-pdf-logfiles)
  1042. (setq file (concat base "." ext))
  1043. (and (file-exists-p file) (delete-file file))))
  1044. (message (concat
  1045. "Exporting to PDF...done"
  1046. (if errors
  1047. (concat ", with some errors:" errors)
  1048. "")))
  1049. pdffile)))
  1050. (defun org-export-latex-get-error (buf)
  1051. "Collect the kinds of errors that remain in pdflatex processing."
  1052. (with-current-buffer buf
  1053. (save-excursion
  1054. (goto-char (point-max))
  1055. (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
  1056. ;; OK, we are at the location of the final run
  1057. (let ((pos (point)) (errors "") (case-fold-search t))
  1058. (if (re-search-forward "Reference.*?undefined" nil t)
  1059. (setq errors (concat errors " [undefined reference]")))
  1060. (goto-char pos)
  1061. (if (re-search-forward "Citation.*?undefined" nil t)
  1062. (setq errors (concat errors " [undefined citation]")))
  1063. (goto-char pos)
  1064. (if (re-search-forward "Undefined control sequence" nil t)
  1065. (setq errors (concat errors " [undefined control sequence]")))
  1066. (and (org-string-nw-p errors) errors))))))
  1067. ;;;###autoload
  1068. (defun org-export-as-pdf-and-open (arg)
  1069. "Export as LaTeX, then process through to PDF, and open."
  1070. (interactive "P")
  1071. (let ((pdffile (org-export-as-pdf arg)))
  1072. (if pdffile
  1073. (progn
  1074. (org-open-file pdffile)
  1075. (when org-export-kill-product-buffer-when-displayed
  1076. (kill-buffer (find-buffer-visiting
  1077. (concat (file-name-sans-extension (buffer-file-name))
  1078. ".tex")))))
  1079. (error "PDF file was not produced"))))
  1080. ;;; Parsing functions:
  1081. (defun org-export-latex-parse-global (level odd)
  1082. "Parse the current buffer recursively, starting at LEVEL.
  1083. If ODD is non-nil, assume the buffer only contains odd sections.
  1084. Return a list reflecting the document structure."
  1085. (save-excursion
  1086. (goto-char (point-min))
  1087. (let* ((cnt 0) output
  1088. (depth org-export-latex-sectioning-depth))
  1089. (while (org-re-search-forward-unprotected
  1090. (concat "^\\(\\(?:\\*\\)\\{"
  1091. (number-to-string (+ (if odd 2 1) level))
  1092. "\\}\\) \\(.*\\)$")
  1093. ;; make sure that there is no upper heading
  1094. (when (> level 0)
  1095. (save-excursion
  1096. (save-match-data
  1097. (org-re-search-forward-unprotected
  1098. (concat "^\\(\\(?:\\*\\)\\{"
  1099. (number-to-string level)
  1100. "\\}\\) \\(.*\\)$") nil t)))) t)
  1101. (setq cnt (1+ cnt))
  1102. (let* ((pos (match-beginning 0))
  1103. (heading (match-string 2))
  1104. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  1105. (save-excursion
  1106. (narrow-to-region
  1107. (point)
  1108. (save-match-data
  1109. (if (org-re-search-forward-unprotected
  1110. (concat "^\\(\\(?:\\*\\)\\{"
  1111. (number-to-string (+ (if odd 2 1) level))
  1112. "\\}\\) \\(.*\\)$") nil t)
  1113. (match-beginning 0)
  1114. (point-max))))
  1115. (goto-char (point-min))
  1116. (setq output
  1117. (append output
  1118. (list
  1119. (list
  1120. `(pos . ,pos)
  1121. `(level . ,nlevel)
  1122. `(occur . ,cnt)
  1123. `(heading . ,heading)
  1124. `(content . ,(org-export-latex-parse-content))
  1125. `(subcontent . ,(org-export-latex-parse-subcontent
  1126. level odd)))))))
  1127. (widen)))
  1128. (list output))))
  1129. (defun org-export-latex-parse-content ()
  1130. "Extract the content of a section."
  1131. (let ((beg (point))
  1132. (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t)
  1133. (progn (beginning-of-line) (point))
  1134. (point-max))))
  1135. (buffer-substring beg end)))
  1136. (defun org-export-latex-parse-subcontent (level odd)
  1137. "Extract the subcontent of a section at LEVEL.
  1138. If ODD Is non-nil, assume subcontent only contains odd sections."
  1139. (if (not (org-re-search-forward-unprotected
  1140. (concat "^\\(\\(?:\\*\\)\\{"
  1141. (number-to-string (+ (if odd 4 2) level))
  1142. "\\}\\) \\(.*\\)$")
  1143. nil t))
  1144. nil ; subcontent is nil
  1145. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  1146. ;;; Rendering functions:
  1147. (defun org-export-latex-global (content)
  1148. "Export CONTENT to LaTeX.
  1149. CONTENT is an element of the list produced by
  1150. `org-export-latex-parse-global'."
  1151. (if (eq (car content) 'subcontent)
  1152. (mapc 'org-export-latex-sub (cdr content))
  1153. (org-export-latex-sub (car content))))
  1154. (defun org-export-latex-sub (subcontent)
  1155. "Export the list SUBCONTENT to LaTeX.
  1156. SUBCONTENT is an alist containing information about the headline
  1157. and its content."
  1158. (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
  1159. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  1160. (defun org-export-latex-subcontent (subcontent num)
  1161. "Export each cell of SUBCONTENT to LaTeX.
  1162. If NUM is non-nil export numbered sections, otherwise use unnumbered
  1163. sections. If NUM is an integer, export the highest NUM levels as
  1164. numbered sections and lower levels as unnumbered sections."
  1165. (let* ((heading (cdr (assoc 'heading subcontent)))
  1166. (level (- (cdr (assoc 'level subcontent))
  1167. org-export-latex-add-level))
  1168. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  1169. (content (cdr (assoc 'content subcontent)))
  1170. (subcontent (cadr (assoc 'subcontent subcontent)))
  1171. (label (org-get-text-property-any 0 'target heading))
  1172. (label-list (cons label (cdr (assoc label
  1173. org-export-target-aliases))))
  1174. (sectioning org-export-latex-sectioning)
  1175. (depth org-export-latex-sectioning-depth)
  1176. main-heading sub-heading ctnt)
  1177. (when (symbolp (car sectioning))
  1178. (setq sectioning (funcall (car sectioning) level heading))
  1179. (when sectioning
  1180. (setq heading (car sectioning)
  1181. sectioning (cdr sectioning)
  1182. ;; target property migh have changed...
  1183. label (org-get-text-property-any 0 'target heading)
  1184. label-list (cons label (cdr (assoc label
  1185. org-export-target-aliases)))))
  1186. (if sectioning (setq sectioning (make-list 10 sectioning)))
  1187. (setq depth (if sectioning 10000 0)))
  1188. (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading)
  1189. (setq main-heading (substring heading 0 (match-beginning 0))
  1190. sub-heading (substring heading (match-end 0))))
  1191. (setq heading (org-export-latex-fontify-headline heading)
  1192. sub-heading (and sub-heading
  1193. (org-export-latex-fontify-headline sub-heading))
  1194. main-heading (and main-heading
  1195. (org-export-latex-fontify-headline main-heading)))
  1196. (cond
  1197. ;; Normal conversion
  1198. ((<= level depth)
  1199. (let* ((sec (nth (1- level) sectioning))
  1200. (num (if (integerp num)
  1201. (>= num level)
  1202. num))
  1203. start end)
  1204. (if (consp (cdr sec))
  1205. (setq start (nth (if num 0 2) sec)
  1206. end (nth (if num 1 3) sec))
  1207. (setq start (if num (car sec) (cdr sec))))
  1208. (insert (format start (if main-heading main-heading heading)
  1209. (or sub-heading "")))
  1210. (insert "\n")
  1211. (when label
  1212. (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
  1213. label-list "\n") "\n"))
  1214. (insert (org-export-latex-content content))
  1215. (cond ((stringp subcontent) (insert subcontent))
  1216. ((listp subcontent)
  1217. (while (org-looking-back "\n\n") (backward-delete-char 1))
  1218. (org-export-latex-sub subcontent)))
  1219. (when (and end (string-match "[^ \t]" end))
  1220. (let ((hook (org-get-text-property-any 0 'org-insert-hook end)))
  1221. (and (functionp hook) (funcall hook)))
  1222. (insert end "\n"))))
  1223. ;; At a level under the hl option: we can drop this subsection
  1224. ((> level depth)
  1225. (cond ((eq org-export-latex-low-levels 'description)
  1226. (if (string-match "% ends low level$"
  1227. (buffer-substring (point-at-bol 0) (point)))
  1228. (delete-region (point-at-bol 0) (point))
  1229. (insert "\\begin{description}\n"))
  1230. (insert (format "\n\\item[%s]%s~\n"
  1231. heading
  1232. (if label (format "\\label{%s}" label) "")))
  1233. (insert (org-export-latex-content content))
  1234. (cond ((stringp subcontent) (insert subcontent))
  1235. ((listp subcontent) (org-export-latex-sub subcontent)))
  1236. (insert "\\end{description} % ends low level\n"))
  1237. ((memq org-export-latex-low-levels '(itemize enumerate))
  1238. (if (string-match "% ends low level$"
  1239. (buffer-substring (point-at-bol 0) (point)))
  1240. (delete-region (point-at-bol 0) (point))
  1241. (insert (format "\\begin{%s}\n"
  1242. (symbol-name org-export-latex-low-levels))))
  1243. (let ((ctnt (org-export-latex-content content)))
  1244. (insert (format (if (not (equal (replace-regexp-in-string "\n" "" ctnt) ""))
  1245. "\n\\item %s\\\\\n%s%%"
  1246. "\n\\item %s\n%s%%")
  1247. heading
  1248. (if label (format "\\label{%s}" label) "")))
  1249. (insert ctnt))
  1250. (cond ((stringp subcontent) (insert subcontent))
  1251. ((listp subcontent) (org-export-latex-sub subcontent)))
  1252. (insert (format "\\end{%s} %% ends low level\n"
  1253. (symbol-name org-export-latex-low-levels))))
  1254. ((and (listp org-export-latex-low-levels)
  1255. org-export-latex-low-levels)
  1256. (if (string-match "% ends low level$"
  1257. (buffer-substring (point-at-bol 0) (point)))
  1258. (delete-region (point-at-bol 0) (point))
  1259. (insert (car org-export-latex-low-levels) "\n"))
  1260. (insert (format (nth 2 org-export-latex-low-levels)
  1261. heading
  1262. (if label (format "\\label{%s}" label) "")))
  1263. (insert (org-export-latex-content content))
  1264. (cond ((stringp subcontent) (insert subcontent))
  1265. ((listp subcontent) (org-export-latex-sub subcontent)))
  1266. (insert (nth 1 org-export-latex-low-levels)
  1267. " %% ends low level\n"))
  1268. ((stringp org-export-latex-low-levels)
  1269. (insert (format org-export-latex-low-levels heading) "\n")
  1270. (when label (insert (format "\\label{%s}\n" label)))
  1271. (insert (org-export-latex-content content))
  1272. (cond ((stringp subcontent) (insert subcontent))
  1273. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  1274. ;;; Exporting internals:
  1275. (defun org-export-latex-set-initial-vars (ext-plist level)
  1276. "Store org local variables required for LaTeX export.
  1277. EXT-PLIST is an optional additional plist.
  1278. LEVEL indicates the default depth for export."
  1279. (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
  1280. org-export-latex-done-keywords org-done-keywords
  1281. org-export-latex-not-done-keywords org-not-done-keywords
  1282. org-export-latex-complex-heading-re org-complex-heading-regexp
  1283. org-export-latex-display-custom-times org-display-custom-times
  1284. org-export-latex-all-targets-re
  1285. (org-make-target-link-regexp (org-all-targets))
  1286. org-export-latex-options-plist
  1287. (org-combine-plists (org-default-export-plist) ext-plist
  1288. (org-infile-export-plist))
  1289. org-export-latex-class
  1290. (or (and (org-region-active-p)
  1291. (save-excursion
  1292. (goto-char (region-beginning))
  1293. (and (looking-at org-complex-heading-regexp)
  1294. (org-entry-get nil "LaTeX_CLASS" 'selective))))
  1295. (save-excursion
  1296. (save-restriction
  1297. (widen)
  1298. (goto-char (point-min))
  1299. (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([-/a-zA-Z]+\\)" nil t)
  1300. (match-string 1))))
  1301. (plist-get org-export-latex-options-plist :latex-class)
  1302. org-export-latex-default-class)
  1303. org-export-latex-class-options
  1304. (or (and (org-region-active-p)
  1305. (save-excursion
  1306. (goto-char (region-beginning))
  1307. (and (looking-at org-complex-heading-regexp)
  1308. (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective))))
  1309. (save-excursion
  1310. (save-restriction
  1311. (widen)
  1312. (goto-char (point-min))
  1313. (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t)
  1314. (match-string 1))))
  1315. (plist-get org-export-latex-options-plist :latex-class-options))
  1316. org-export-latex-class
  1317. (or (car (assoc org-export-latex-class org-export-latex-classes))
  1318. (error "No definition for class `%s' in `org-export-latex-classes'"
  1319. org-export-latex-class))
  1320. org-export-latex-header
  1321. (cadr (assoc org-export-latex-class org-export-latex-classes))
  1322. org-export-latex-sectioning
  1323. (cddr (assoc org-export-latex-class org-export-latex-classes))
  1324. org-export-latex-sectioning-depth
  1325. (or level
  1326. (let ((hl-levels
  1327. (plist-get org-export-latex-options-plist :headline-levels))
  1328. (sec-depth (length org-export-latex-sectioning)))
  1329. (if (> hl-levels sec-depth) sec-depth hl-levels))))
  1330. (when (and org-export-latex-class-options
  1331. (string-match "\\S-" org-export-latex-class-options)
  1332. (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
  1333. org-export-latex-header))
  1334. (setq org-export-latex-header
  1335. (concat (substring org-export-latex-header 0 (match-end 1))
  1336. org-export-latex-class-options
  1337. (substring org-export-latex-header (match-end 0))))))
  1338. (defvar org-export-latex-format-toc-function
  1339. 'org-export-latex-format-toc-default
  1340. "The function formatting returning the string to create the table of contents.
  1341. The function mus take one parameter, the depth of the table of contents.")
  1342. (defun org-export-latex-make-header (title opt-plist)
  1343. "Make the LaTeX header and return it as a string.
  1344. TITLE is the current title from the buffer or region.
  1345. OPT-PLIST is the options plist for current buffer."
  1346. (let ((toc (plist-get opt-plist :table-of-contents))
  1347. (author (org-export-apply-macros-in-string
  1348. (plist-get opt-plist :author)))
  1349. (email (replace-regexp-in-string
  1350. "_" "\\\\_"
  1351. (org-export-apply-macros-in-string
  1352. (plist-get opt-plist :email))))
  1353. (description (org-export-apply-macros-in-string
  1354. (plist-get opt-plist :description)))
  1355. (keywords (org-export-apply-macros-in-string
  1356. (plist-get opt-plist :keywords))))
  1357. (concat
  1358. (if (plist-get opt-plist :time-stamp-file)
  1359. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  1360. ;; insert LaTeX custom header and packages from the list
  1361. (org-splice-latex-header
  1362. (org-export-apply-macros-in-string org-export-latex-header)
  1363. org-export-latex-default-packages-alist
  1364. org-export-latex-packages-alist nil
  1365. (org-export-apply-macros-in-string
  1366. (plist-get opt-plist :latex-header-extra)))
  1367. ;; append another special variable
  1368. (org-export-apply-macros-in-string org-export-latex-append-header)
  1369. ;; define alert if not yet defined
  1370. "\n\\providecommand{\\alert}[1]{\\textbf{#1}}"
  1371. ;; insert the title
  1372. (format
  1373. "\n\n\\title{%s}\n"
  1374. (org-export-latex-fontify-headline title))
  1375. ;; insert author info
  1376. (if (plist-get opt-plist :author-info)
  1377. (format "\\author{%s%s}\n"
  1378. (org-export-latex-fontify-headline (or author user-full-name))
  1379. (if (and (plist-get opt-plist :email-info) email
  1380. (string-match "\\S-" email))
  1381. (format "\\thanks{%s}" email)
  1382. ""))
  1383. (format "%%\\author{%s}\n"
  1384. (org-export-latex-fontify-headline (or author user-full-name))))
  1385. ;; insert the date
  1386. (format "\\date{%s}\n"
  1387. (format-time-string
  1388. (or (plist-get opt-plist :date)
  1389. org-export-latex-date-format)))
  1390. ;; add some hyperref options
  1391. (format org-export-latex-hyperref-options-format
  1392. (org-export-latex-fontify-headline keywords)
  1393. (org-export-latex-fontify-headline description)
  1394. (org-version))
  1395. ;; beginning of the document
  1396. "\n\\begin{document}\n\n"
  1397. ;; insert the title command
  1398. (when (string-match "\\S-" title)
  1399. (if (string-match "%s" org-export-latex-title-command)
  1400. (format org-export-latex-title-command title)
  1401. org-export-latex-title-command))
  1402. "\n\n"
  1403. ;; table of contents
  1404. (when (and org-export-with-toc
  1405. (plist-get opt-plist :section-numbers))
  1406. (funcall org-export-latex-format-toc-function
  1407. (cond ((numberp toc)
  1408. (min toc (plist-get opt-plist :headline-levels)))
  1409. (toc (plist-get opt-plist :headline-levels))))))))
  1410. (defun org-export-latex-format-toc-default (depth)
  1411. (when depth
  1412. (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
  1413. depth)))
  1414. (defun org-export-latex-first-lines (opt-plist &optional beg end)
  1415. "Export the first lines before first headline.
  1416. If BEG is non-nil, it is the beginning of the region.
  1417. If END is non-nil, it is the end of the region."
  1418. (save-excursion
  1419. (goto-char (or beg (point-min)))
  1420. (let* ((pt (point))
  1421. (end (if (re-search-forward
  1422. (concat "^" (org-get-limited-outline-regexp)) end t)
  1423. (goto-char (match-beginning 0))
  1424. (goto-char (or end (point-max))))))
  1425. (prog1
  1426. (org-export-latex-content
  1427. (org-export-preprocess-string
  1428. (buffer-substring pt end)
  1429. :for-backend 'latex
  1430. :emph-multiline t
  1431. :add-text nil
  1432. :comments nil
  1433. :skip-before-1st-heading nil
  1434. :LaTeX-fragments nil
  1435. :timestamps (plist-get opt-plist :timestamps)
  1436. :footnotes (plist-get opt-plist :footnotes)))
  1437. (org-unmodified
  1438. (let ((inhibit-read-only t)
  1439. (limit (max pt (1- end))))
  1440. (add-text-properties pt limit
  1441. '(:org-license-to-kill t))
  1442. (save-excursion
  1443. (goto-char pt)
  1444. (while (re-search-forward "^[ \t]*#\\+.*\n?" limit t)
  1445. (let ((case-fold-search t))
  1446. (unless (org-string-match-p
  1447. "^[ \t]*#\\+\\(attr_\\|caption\\>\\|label\\>\\)"
  1448. (match-string 0))
  1449. (remove-text-properties (match-beginning 0) (match-end 0)
  1450. '(:org-license-to-kill t))))))))))))
  1451. (defvar org-export-latex-header-defs nil
  1452. "The header definitions that might be used in the LaTeX body.")
  1453. (defun org-export-latex-content (content &optional exclude-list)
  1454. "Convert CONTENT string to LaTeX.
  1455. Don't perform conversions that are in EXCLUDE-LIST. Recognized
  1456. conversion types are: quotation-marks, emphasis, sub-superscript,
  1457. links, keywords, lists, tables, fixed-width"
  1458. (with-temp-buffer
  1459. (org-install-letbind)
  1460. (insert content)
  1461. (unless (memq 'timestamps exclude-list)
  1462. (org-export-latex-time-stamps))
  1463. (unless (memq 'quotation-marks exclude-list)
  1464. (org-export-latex-quotation-marks))
  1465. (unless (memq 'emphasis exclude-list)
  1466. (when (plist-get org-export-latex-options-plist :emphasize)
  1467. (org-export-latex-fontify)))
  1468. (unless (memq 'sub-superscript exclude-list)
  1469. (org-export-latex-special-chars
  1470. (plist-get org-export-latex-options-plist :sub-superscript)))
  1471. (unless (memq 'links exclude-list)
  1472. (org-export-latex-links))
  1473. (unless (memq 'keywords exclude-list)
  1474. (org-export-latex-keywords))
  1475. (unless (memq 'lists exclude-list)
  1476. (org-export-latex-lists))
  1477. (unless (memq 'tables exclude-list)
  1478. (org-export-latex-tables
  1479. (plist-get org-export-latex-options-plist :tables)))
  1480. (unless (memq 'fixed-width exclude-list)
  1481. (org-export-latex-fixed-width
  1482. (plist-get org-export-latex-options-plist :fixed-width)))
  1483. ;; return string
  1484. (buffer-substring (point-min) (point-max))))
  1485. (defun org-export-latex-protect-string (s)
  1486. "Add the org-protected property to string S."
  1487. (add-text-properties 0 (length s) '(org-protected t) s) s)
  1488. (defun org-export-latex-protect-char-in-string (char-list string)
  1489. "Add org-protected text-property to char from CHAR-LIST in STRING."
  1490. (with-temp-buffer
  1491. (save-match-data
  1492. (insert string)
  1493. (goto-char (point-min))
  1494. (while (re-search-forward (regexp-opt char-list) nil t)
  1495. (add-text-properties (match-beginning 0)
  1496. (match-end 0) '(org-protected t)))
  1497. (buffer-string))))
  1498. (defun org-export-latex-keywords-maybe (&optional remove-list)
  1499. "Maybe remove keywords depending on rules in REMOVE-LIST."
  1500. (goto-char (point-min))
  1501. (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
  1502. (case-fold-search nil)
  1503. (todo-markup org-export-latex-todo-keyword-markup)
  1504. fmt)
  1505. ;; convert TODO keywords
  1506. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  1507. (if (plist-get remove-list :todo)
  1508. (replace-match "")
  1509. (setq fmt (cond
  1510. ((stringp todo-markup) todo-markup)
  1511. ((and (consp todo-markup) (stringp (car todo-markup)))
  1512. (if (member (match-string 1) org-export-latex-done-keywords)
  1513. (cdr todo-markup) (car todo-markup)))
  1514. (t (cdr (or (assoc (match-string 1) todo-markup)
  1515. (car todo-markup))))))
  1516. (replace-match (org-export-latex-protect-string
  1517. (format fmt (match-string 1))) t t)))
  1518. ;; convert priority string
  1519. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  1520. (if (plist-get remove-list :priority)
  1521. (replace-match "")
  1522. (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
  1523. ;; convert tags
  1524. (when (re-search-forward "\\(:[a-zA-Z0-9_@#%]+\\)+:" nil t)
  1525. (if (or (not org-export-with-tags)
  1526. (plist-get remove-list :tags))
  1527. (replace-match "")
  1528. (replace-match
  1529. (org-export-latex-protect-string
  1530. (format org-export-latex-tag-markup
  1531. (save-match-data
  1532. (replace-regexp-in-string
  1533. "\\([_#]\\)" "\\\\\\1" (match-string 0)))))
  1534. t t)))))
  1535. (defun org-export-latex-fontify-headline (string)
  1536. "Fontify special words in STRING."
  1537. (with-temp-buffer
  1538. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  1539. ;; the beginning of the buffer - inserting "\n" is safe here though.
  1540. (insert "\n" string)
  1541. ;; Preserve math snippets
  1542. (let* ((matchers (plist-get org-format-latex-options :matchers))
  1543. (re-list org-latex-regexps)
  1544. beg end re e m n block off)
  1545. ;; Check the different regular expressions
  1546. (while (setq e (pop re-list))
  1547. (setq m (car e) re (nth 1 e) n (nth 2 e)
  1548. block (if (nth 3 e) "\n\n" ""))
  1549. (setq off (if (member m '("$" "$1")) 1 0))
  1550. (when (and (member m matchers) (not (equal m "begin")))
  1551. (goto-char (point-min))
  1552. (while (re-search-forward re nil t)
  1553. (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
  1554. (add-text-properties beg end
  1555. '(org-protected t org-latex-math t))))))
  1556. ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
  1557. (goto-char (point-min))
  1558. (let ((case-fold-search nil))
  1559. (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
  1560. (unless (eq (char-before (match-beginning 1)) ?\\)
  1561. (org-if-unprotected-1
  1562. (replace-match (org-export-latex-protect-string
  1563. (concat "\\" (match-string 1)
  1564. "{}")) t t)))))
  1565. (goto-char (point-min))
  1566. (let ((re (concat "\\\\\\([a-zA-Z]+\\)"
  1567. "\\(?:<[^<>\n]*>\\)*"
  1568. "\\(?:\\[[^][\n]*?\\]\\)*"
  1569. "\\(?:<[^<>\n]*>\\)*"
  1570. "\\("
  1571. (org-create-multibrace-regexp "{" "}" 3)
  1572. "\\)\\{1,3\\}")))
  1573. (while (re-search-forward re nil t)
  1574. (unless (or
  1575. ;; check for comment line
  1576. (save-excursion (goto-char (match-beginning 0))
  1577. (org-in-indented-comment-line))
  1578. ;; Check if this is a defined entity, so that is may need conversion
  1579. (org-entity-get (match-string 1)))
  1580. (add-text-properties (match-beginning 0) (match-end 0)
  1581. '(org-protected t)))))
  1582. (when (plist-get org-export-latex-options-plist :emphasize)
  1583. (org-export-latex-fontify))
  1584. (org-export-latex-time-stamps)
  1585. (org-export-latex-quotation-marks)
  1586. (org-export-latex-keywords-maybe)
  1587. (org-export-latex-special-chars
  1588. (plist-get org-export-latex-options-plist :sub-superscript))
  1589. (org-export-latex-links)
  1590. (org-trim (buffer-string))))
  1591. (defun org-export-latex-time-stamps ()
  1592. "Format time stamps."
  1593. (goto-char (point-min))
  1594. (let ((org-display-custom-times org-export-latex-display-custom-times))
  1595. (while (re-search-forward org-ts-regexp-both nil t)
  1596. (org-if-unprotected-at (1- (point))
  1597. (replace-match
  1598. (org-export-latex-protect-string
  1599. (format (if (string= "<" (substring (match-string 0) 0 1))
  1600. org-export-latex-timestamp-markup
  1601. org-export-latex-timestamp-inactive-markup)
  1602. (substring (org-translate-time (match-string 0)) 1 -1)))
  1603. t t)))))
  1604. (defun org-export-latex-quotation-marks ()
  1605. "Export quotation marks depending on language conventions."
  1606. (mapc (lambda(l)
  1607. (goto-char (point-min))
  1608. (while (re-search-forward (car l) nil t)
  1609. (let ((rpl (concat (match-string 1)
  1610. (org-export-latex-protect-string
  1611. (copy-sequence (cdr l))))))
  1612. (org-if-unprotected-1
  1613. (replace-match rpl t t)))))
  1614. (cdr (or (assoc (plist-get org-export-latex-options-plist :language)
  1615. org-export-latex-quotes)
  1616. ;; falls back on english
  1617. (assoc "en" org-export-latex-quotes)))))
  1618. (defun org-export-latex-special-chars (sub-superscript)
  1619. "Export special characters to LaTeX.
  1620. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  1621. See the `org-export-latex.el' code for a complete conversion table."
  1622. (goto-char (point-min))
  1623. (mapc (lambda(c)
  1624. (goto-char (point-min))
  1625. (while (re-search-forward c nil t)
  1626. ;; Put the point where to check for org-protected
  1627. (unless (get-text-property (match-beginning 2) 'org-protected)
  1628. (cond ((member (match-string 2) '("\\$" "$"))
  1629. (if (equal (match-string 2) "\\$")
  1630. nil
  1631. (replace-match "\\$" t t)))
  1632. ((member (match-string 2) '("&" "%" "#"))
  1633. (if (equal (match-string 1) "\\")
  1634. (replace-match (match-string 2) t t)
  1635. (replace-match (concat (match-string 1) "\\"
  1636. (match-string 2)) t t)
  1637. (backward-char 1)))
  1638. ((equal (match-string 2) "...")
  1639. (replace-match
  1640. (concat (match-string 1)
  1641. (org-export-latex-protect-string "\\ldots{}")) t t))
  1642. ((equal (match-string 2) "~")
  1643. (cond ((equal (match-string 1) "\\") nil)
  1644. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  1645. (replace-match (concat (match-string 1) "\\~") t t))
  1646. (t (replace-match
  1647. (org-export-latex-protect-string
  1648. (concat (match-string 1) "\\~{}")) t t))))
  1649. ((member (match-string 2) '("{" "}"))
  1650. (unless (save-match-data (org-inside-latex-math-p))
  1651. (if (equal (match-string 1) "\\")
  1652. (replace-match (match-string 2) t t)
  1653. (replace-match (concat (match-string 1) "\\"
  1654. (match-string 2)) t t)))))
  1655. (unless (save-match-data (or (org-inside-latex-math-p) (org-at-table-p)))
  1656. (cond ((equal (match-string 2) "\\")
  1657. (replace-match (or (save-match-data
  1658. (org-export-latex-treat-backslash-char
  1659. (match-string 1)
  1660. (or (match-string 3) "")))
  1661. "") t t)
  1662. (when (and (get-text-property (1- (point)) 'org-entity)
  1663. (looking-at "{}"))
  1664. ;; OK, this was an entity replacement, and the user
  1665. ;; had terminated the entity with {}. Make sure
  1666. ;; {} is protected as well, and remove the extra {}
  1667. ;; inserted by the conversion.
  1668. (put-text-property (point) (+ 2 (point)) 'org-protected t)
  1669. (if (save-excursion (goto-char (max (- (point) 2) (point-min)))
  1670. (looking-at "{}"))
  1671. (replace-match ""))
  1672. (forward-char 2))
  1673. (backward-char 1))
  1674. ((member (match-string 2) '("_" "^"))
  1675. (replace-match (or (save-match-data
  1676. (org-export-latex-treat-sub-super-char
  1677. sub-superscript
  1678. (match-string 2)
  1679. (match-string 1)
  1680. (match-string 3))) "") t t)
  1681. (backward-char 1)))))))
  1682. '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  1683. "\\(\\(\\\\?\\$\\)\\)"
  1684. "\\([a-zA-Z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-zA-Z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-zA-Z0-9]+}\\|([a-zA-Z0-9]+)\\)"
  1685. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|\\([&#%{}\"]\\|[a-zA-Z][a-zA-Z0-9]*\\)\\)"
  1686. "\\(^\\|.\\)\\([&#%{}~]\\|\\.\\.\\.\\)"
  1687. ;; (?\< . "\\textless{}")
  1688. ;; (?\> . "\\textgreater{}")
  1689. )))
  1690. (defun org-inside-latex-math-p ()
  1691. (get-text-property (point) 'org-latex-math))
  1692. (defun org-export-latex-treat-sub-super-char
  1693. (subsup char string-before string-after)
  1694. "Convert the \"_\" and \"^\" characters to LaTeX.
  1695. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  1696. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  1697. (cond ((equal string-before "\\")
  1698. (concat string-before char string-after))
  1699. ((and (string-match "\\S-+" string-after))
  1700. ;; this is part of a math formula
  1701. (cond ((eq 'org-link (get-text-property 0 'face char))
  1702. (concat string-before "\\" char string-after))
  1703. ((save-match-data (org-inside-latex-math-p))
  1704. (if subsup
  1705. (cond ((eq 1 (length string-after))
  1706. (concat string-before char string-after))
  1707. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  1708. (format "%s%s{%s}" string-before char
  1709. (match-string 1 string-after))))))
  1710. ((and (> (length string-after) 1)
  1711. (or (eq subsup t)
  1712. (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
  1713. (or (string-match "[{]?\\([^}]+\\)[}]?" string-after)
  1714. (string-match "[(]?\\([^)]+\\)[)]?" string-after)))
  1715. (org-export-latex-protect-string
  1716. (format "%s$%s{%s}$" string-before char
  1717. (if (and (> (match-end 1) (1+ (match-beginning 1)))
  1718. (not (equal (substring string-after 0 2) "{\\")))
  1719. (concat "\\mathrm{" (match-string 1 string-after) "}")
  1720. (match-string 1 string-after)))))
  1721. ((eq subsup t) (concat string-before "$" char string-after "$"))
  1722. (t (org-export-latex-protect-string
  1723. (concat string-before "\\" char "{}" string-after)))))
  1724. (t (org-export-latex-protect-string
  1725. (concat string-before "\\" char "{}" string-after)))))
  1726. (defun org-export-latex-treat-backslash-char (string-before string-after)
  1727. "Convert the \"$\" special character to LaTeX.
  1728. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  1729. (let ((ass (org-entity-get string-after)))
  1730. (cond
  1731. (ass (org-add-props
  1732. (if (nth 2 ass)
  1733. (concat string-before
  1734. (org-export-latex-protect-string
  1735. (concat "$" (nth 1 ass) "$")))
  1736. (concat string-before (org-export-latex-protect-string
  1737. (nth 1 ass))))
  1738. nil 'org-entity t))
  1739. ((and (not (string-match "^[ \n\t]" string-after))
  1740. (not (string-match "[ \t]\\'\\|^" string-before)))
  1741. ;; backslash is inside a word
  1742. (concat string-before
  1743. (org-export-latex-protect-string
  1744. (concat "\\textbackslash{}" string-after))))
  1745. ((not (or (equal string-after "")
  1746. (string-match "^[ \t\n]" string-after)))
  1747. ;; backslash might escape a character (like \#) or a user TeX
  1748. ;; macro (like \setcounter)
  1749. (concat string-before
  1750. (org-export-latex-protect-string (concat "\\" string-after))))
  1751. ((and (string-match "^[ \t\n]" string-after)
  1752. (string-match "[ \t\n]\\'" string-before))
  1753. ;; backslash is alone, convert it to $\backslash$
  1754. (org-export-latex-protect-string
  1755. (concat string-before "\\textbackslash{}" string-after)))
  1756. (t (org-export-latex-protect-string
  1757. (concat string-before "\\textbackslash{}" string-after))))))
  1758. (defun org-export-latex-keywords ()
  1759. "Convert special keywords to LaTeX."
  1760. (goto-char (point-min))
  1761. (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
  1762. (replace-match (format org-export-latex-timestamp-keyword-markup
  1763. (match-string 0)) t t)
  1764. (save-excursion
  1765. (beginning-of-line 1)
  1766. (unless (looking-at ".*\n[ \t]*\n")
  1767. (end-of-line 1)
  1768. (insert "\n")))))
  1769. (defun org-export-latex-fixed-width (opt)
  1770. "When OPT is non-nil convert fixed-width sections to LaTeX."
  1771. (goto-char (point-min))
  1772. (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
  1773. (unless (get-text-property (point) 'org-example)
  1774. (if opt
  1775. (progn (goto-char (match-beginning 0))
  1776. (insert "\\begin{verbatim}\n")
  1777. (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
  1778. (replace-match (concat (match-string 1)
  1779. (match-string 2)) t t)
  1780. (forward-line))
  1781. (insert "\\end{verbatim}\n"))
  1782. (progn (goto-char (match-beginning 0))
  1783. (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
  1784. (replace-match (concat "%" (match-string 1)
  1785. (match-string 2)) t t)
  1786. (forward-line)))))))
  1787. (defvar org-table-last-alignment) ; defined in org-table.el
  1788. (defvar org-table-last-column-widths) ; defined in org-table.el
  1789. (declare-function orgtbl-to-latex "org-table" (table params) t)
  1790. (defun org-export-latex-tables (insert)
  1791. "Convert tables to LaTeX and INSERT it."
  1792. ;; First, get the table.el tables
  1793. (goto-char (point-min))
  1794. (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t)
  1795. (org-if-unprotected
  1796. (require 'table)
  1797. (org-export-latex-convert-table.el-table)))
  1798. ;; And now the Org-mode tables
  1799. (goto-char (point-min))
  1800. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  1801. (org-if-unprotected-at (1- (point))
  1802. (org-table-align)
  1803. (let* ((beg (org-table-begin))
  1804. (end (org-table-end))
  1805. (raw-table (buffer-substring beg end))
  1806. (org-table-last-alignment (copy-sequence org-table-last-alignment))
  1807. (org-table-last-column-widths (copy-sequence
  1808. org-table-last-column-widths))
  1809. fnum fields line lines olines gr colgropen line-fmt align
  1810. caption width shortn label attr hfmt floatp placement
  1811. longtblp tblenv tabular-env)
  1812. (if org-export-latex-tables-verbatim
  1813. (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
  1814. "\\end{verbatim}\n")))
  1815. (apply 'delete-region (list beg end))
  1816. (insert (org-export-latex-protect-string tbl)))
  1817. (progn
  1818. (setq caption (org-find-text-property-in-string
  1819. 'org-caption raw-table)
  1820. shortn (org-find-text-property-in-string
  1821. 'org-caption-shortn raw-table)
  1822. attr (org-find-text-property-in-string
  1823. 'org-attributes raw-table)
  1824. label (org-find-text-property-in-string
  1825. 'org-label raw-table)
  1826. longtblp (and attr (stringp attr)
  1827. (string-match "\\<longtable\\>" attr))
  1828. tblenv (if (and attr (stringp attr))
  1829. (cond ((string-match "\\<sidewaystable\\>" attr)
  1830. "sidewaystable")
  1831. ((or (string-match (regexp-quote "table*") attr)
  1832. (string-match "\\<multicolumn\\>" attr))
  1833. "table*")
  1834. (t "table"))
  1835. "table")
  1836. tabular-env
  1837. (if (and attr (stringp attr)
  1838. (string-match "\\(tabular.\\)" attr))
  1839. (match-string 1 attr)
  1840. org-export-latex-tabular-environment)
  1841. width (and attr (stringp attr)
  1842. (string-match "\\<width=\\([^ \t\n\r]+\\)" attr)
  1843. (match-string 1 attr))
  1844. align (and attr (stringp attr)
  1845. (string-match "\\<align=\\([^ \t\n\r]+\\)" attr)
  1846. (match-string 1 attr))
  1847. hfmt (and attr (stringp attr)
  1848. (string-match "\\<hfmt=\\(\\S-+\\)" attr)
  1849. (match-string 1 attr))
  1850. floatp (or caption label (string= "table*" tblenv))
  1851. placement (if (and attr
  1852. (stringp attr)
  1853. (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
  1854. (match-string 1 attr)
  1855. (concat
  1856. "[" org-latex-default-figure-position "]")))
  1857. (setq caption (and caption (org-export-latex-fontify-headline caption)))
  1858. (setq lines (org-split-string raw-table "\n"))
  1859. (apply 'delete-region (list beg end))
  1860. (when org-export-table-remove-special-lines
  1861. (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
  1862. (when org-table-clean-did-remove-column
  1863. (pop org-table-last-alignment)
  1864. (pop org-table-last-column-widths))
  1865. ;; make a format string to reflect alignment
  1866. (setq olines lines)
  1867. (while (and (not line-fmt) (setq line (pop olines)))
  1868. (unless (string-match "^[ \t]*|-" line)
  1869. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1870. (setq fnum (make-vector (length fields) 0))
  1871. (setq line-fmt
  1872. (mapconcat
  1873. (lambda (x)
  1874. (setq gr (pop org-table-colgroup-info))
  1875. (format "%s%%s%s"
  1876. (cond ((eq gr :start)
  1877. (prog1 (if colgropen "|" "|")
  1878. (setq colgropen t)))
  1879. ((eq gr :startend)
  1880. (prog1 (if colgropen "|" "|")
  1881. (setq colgropen nil)))
  1882. (t ""))
  1883. (if (memq gr '(:end :startend))
  1884. (progn (setq colgropen nil) "|")
  1885. "")))
  1886. fnum ""))))
  1887. ;; fix double || in line-fmt
  1888. (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
  1889. ;; maybe remove the first and last "|"
  1890. (when (and (not org-export-latex-tables-column-borders)
  1891. (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
  1892. (setq line-fmt (match-string 2 line-fmt)))
  1893. ;; format alignment
  1894. (unless align
  1895. (setq align (apply 'format
  1896. (cons line-fmt
  1897. (mapcar (lambda (x) (if x "r" "l"))
  1898. org-table-last-alignment)))))
  1899. ;; prepare the table to send to orgtbl-to-latex
  1900. (setq lines
  1901. (mapcar
  1902. (lambda(elem)
  1903. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  1904. (org-split-string
  1905. (progn (set-text-properties 0 (length elem) nil elem)
  1906. (org-trim elem)) "|")))
  1907. lines))
  1908. (when insert
  1909. (insert (org-export-latex-protect-string
  1910. (concat
  1911. (if longtblp
  1912. (concat "\\begin{longtable}{" align "}\n")
  1913. (if floatp
  1914. (format "\\begin{%s}%s\n" tblenv placement)))
  1915. (if (and floatp org-export-latex-table-caption-above)
  1916. (format
  1917. "\\caption%s{%s} %s"
  1918. (if shortn (concat "[" shortn "]") "")
  1919. (or caption "")
  1920. (if label (format "\\label{%s}" label) "")))
  1921. (if (and longtblp caption org-export-latex-table-caption-above)
  1922. "\\\\\n" "\n")
  1923. (if (and org-export-latex-tables-centered (not longtblp))
  1924. "\\begin{center}\n")
  1925. (if (not longtblp)
  1926. (format "\\begin{%s}%s{%s}\n"
  1927. tabular-env
  1928. (if width (format "{%s}" width) "")
  1929. align))
  1930. (orgtbl-to-latex
  1931. lines
  1932. `(:tstart ,org-export-latex-tables-tstart
  1933. :tend ,org-export-latex-tables-tend
  1934. :hline ,org-export-latex-tables-hline
  1935. :skipheadrule ,longtblp
  1936. :hfmt ,hfmt
  1937. :hlend ,(if longtblp
  1938. (format "\\\\
  1939. %s
  1940. \\endhead
  1941. %s\\multicolumn{%d}{r}{Continued on next page}\\
  1942. \\endfoot
  1943. \\endlastfoot"
  1944. org-export-latex-tables-hline
  1945. org-export-latex-tables-hline
  1946. (length org-table-last-alignment))
  1947. nil)))
  1948. (if (not longtblp) (format "\n\\end{%s}" tabular-env))
  1949. (if longtblp "\n" (if org-export-latex-tables-centered
  1950. "\n\\end{center}\n" "\n"))
  1951. (if (and floatp (not org-export-latex-table-caption-above))
  1952. (format
  1953. "\\caption%s{%s} %s"
  1954. (if shortn (concat "[" shortn "]") "")
  1955. (or caption "")
  1956. (if label (format "\\label{%s}" label) "")))
  1957. (if longtblp
  1958. "\\end{longtable}"
  1959. (if floatp (format "\\end{%s}" tblenv)))))
  1960. "\n\n"))))))))
  1961. (defun org-export-latex-convert-table.el-table ()
  1962. "Replace table.el table at point with LaTeX code."
  1963. (let (tbl caption shortn label line floatp attr align rmlines)
  1964. (setq line (buffer-substring (point-at-bol) (point-at-eol))
  1965. label (org-get-text-property-any 0 'org-label line)
  1966. caption (org-get-text-property-any 0 'org-caption line)
  1967. shortn (org-get-text-property-any 0 'org-caption-shortn line)
  1968. attr (org-get-text-property-any 0 'org-attributes line)
  1969. align (and attr (stringp attr)
  1970. (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
  1971. (match-string 1 attr))
  1972. rmlines (and attr (stringp attr)
  1973. (string-match "\\<rmlines\\>" attr))
  1974. floatp (or label caption))
  1975. (and (get-buffer "*org-export-table*")
  1976. (kill-buffer (get-buffer "*org-export-table*")))
  1977. (table-generate-source 'latex "*org-export-table*" "caption")
  1978. (setq tbl (with-current-buffer "*org-export-table*"
  1979. (buffer-string)))
  1980. (while (string-match "^%.*\n" tbl)
  1981. (setq tbl (replace-match "" t t tbl)))
  1982. ;; fix the hlines
  1983. (when rmlines
  1984. (let ((n 0) lines)
  1985. (setq lines (mapcar (lambda (x)
  1986. (if (string-match "^\\\\hline$" x)
  1987. (progn
  1988. (setq n (1+ n))
  1989. (if (= n 2) x nil))
  1990. x))
  1991. (org-split-string tbl "\n")))
  1992. (setq tbl (mapconcat 'identity (delq nil lines) "\n"))))
  1993. (when (and align (string-match "\\\\begin{tabular}{.*}" tbl))
  1994. (setq tbl (replace-match (concat "\\begin{tabular}{" align "}")
  1995. t t tbl)))
  1996. (and (get-buffer "*org-export-table*")
  1997. (kill-buffer (get-buffer "*org-export-table*")))
  1998. (beginning-of-line 0)
  1999. (while (looking-at "[ \t]*\\(|\\|\\+-\\)")
  2000. (delete-region (point) (1+ (point-at-eol))))
  2001. (when org-export-latex-tables-centered
  2002. (setq tbl (concat "\\begin{center}\n" tbl "\\end{center}")))
  2003. (when floatp
  2004. (setq tbl (concat "\\begin{table}\n"
  2005. (if (not org-export-latex-table-caption-above) tbl)
  2006. (format "\\caption%s{%s%s}\n"
  2007. (if shortn (format "[%s]" shortn) "")
  2008. (if label (format "\\label{%s}" label) "")
  2009. (or caption ""))
  2010. (if org-export-latex-table-caption-above tbl)
  2011. "\n\\end{table}\n")))
  2012. (insert (org-export-latex-protect-string tbl))))
  2013. (defun org-export-latex-fontify ()
  2014. "Convert fontification to LaTeX."
  2015. (goto-char (point-min))
  2016. (while (re-search-forward org-emph-re nil t)
  2017. ;; The match goes one char after the *string*, except at the end of a line
  2018. (let ((emph (assoc (match-string 3)
  2019. org-export-latex-emphasis-alist))
  2020. (beg (match-beginning 0))
  2021. (end (match-end 0))
  2022. rpl s)
  2023. (unless emph
  2024. (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
  2025. (match-string 3)))
  2026. (unless (or (and (get-text-property (- (point) 2) 'org-protected)
  2027. (not (get-text-property
  2028. (- (point) 2) 'org-verbatim-emph)))
  2029. (equal (char-after (match-beginning 3))
  2030. (char-after (1+ (match-beginning 3))))
  2031. (save-excursion
  2032. (goto-char (match-beginning 1))
  2033. (save-match-data
  2034. (and (org-at-table-p)
  2035. (string-match
  2036. "[|\n]" (buffer-substring beg end)))))
  2037. (and (equal (match-string 3) "+")
  2038. (save-match-data
  2039. (string-match "\\`-+\\'" (match-string 4)))))
  2040. (setq s (match-string 4))
  2041. (setq rpl (concat (match-string 1)
  2042. (org-export-latex-emph-format (cadr emph)
  2043. (match-string 4))
  2044. (match-string 5)))
  2045. (if (caddr emph)
  2046. (setq rpl (org-export-latex-protect-string rpl))
  2047. (save-match-data
  2048. (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
  2049. (progn
  2050. (add-text-properties (match-beginning 1) (match-end 1)
  2051. '(org-protected t) rpl)
  2052. (add-text-properties (match-beginning 3) (match-end 3)
  2053. '(org-protected t) rpl)))))
  2054. (replace-match rpl t t)))
  2055. (backward-char)))
  2056. (defun org-export-latex-emph-format (format string)
  2057. "Format an emphasis string and handle the \\verb special case."
  2058. (when (member format '("\\verb" "\\protectedtexttt"))
  2059. (save-match-data
  2060. (if (equal format "\\verb")
  2061. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  2062. (catch 'exit
  2063. (loop for i from 0 to (1- (length ll)) do
  2064. (if (not (string-match (regexp-quote (substring ll i (1+ i)))
  2065. string))
  2066. (progn
  2067. (setq format (concat "\\verb" (substring ll i (1+ i))
  2068. "%s" (substring ll i (1+ i))))
  2069. (throw 'exit nil))))))
  2070. (let ((start 0)
  2071. (trans '(("\\" . "\\textbackslash{}")
  2072. ("~" . "\\textasciitilde{}")
  2073. ("^" . "\\textasciicircum{}")))
  2074. (rtn "") char)
  2075. (while (string-match "[\\{}$%&_#~^]" string)
  2076. (setq char (match-string 0 string))
  2077. (if (> (match-beginning 0) 0)
  2078. (setq rtn (concat rtn (substring string
  2079. 0 (match-beginning 0)))))
  2080. (setq string (substring string (1+ (match-beginning 0))))
  2081. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  2082. rtn (concat rtn char)))
  2083. (setq string (concat rtn string) format "\\texttt{%s}")
  2084. (while (string-match "--" string)
  2085. (setq string (replace-match "-{}-" t t string)))))))
  2086. (format format string))
  2087. (defun org-export-latex-links ()
  2088. ;; Make sure to use the LaTeX hyperref and graphicx package
  2089. ;; or send some warnings.
  2090. "Convert links to LaTeX."
  2091. (goto-char (point-min))
  2092. (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
  2093. (org-if-unprotected-1
  2094. (goto-char (match-beginning 0))
  2095. (let* ((re-radio org-export-latex-all-targets-re)
  2096. (remove (list (match-beginning 0) (match-end 0)))
  2097. (raw-path (org-extract-attributes (match-string 3)))
  2098. (full-raw-path (concat (match-string 1) raw-path))
  2099. (desc (match-string 5))
  2100. (type (or (match-string 2)
  2101. (if (or (file-name-absolute-p raw-path)
  2102. (string-match "^\\.\\.?/" raw-path))
  2103. "file")))
  2104. (coderefp (equal type "coderef"))
  2105. (caption (org-find-text-property-in-string 'org-caption raw-path))
  2106. (shortn (org-find-text-property-in-string 'org-caption-shortn raw-path))
  2107. (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
  2108. (plist-get org-export-latex-options-plist :latex-image-options)))
  2109. (label (org-find-text-property-in-string 'org-label raw-path))
  2110. imgp radiop fnc
  2111. ;; define the path of the link
  2112. (path (cond
  2113. ((member type '("coderef"))
  2114. raw-path)
  2115. ((member type '("http" "https" "ftp"))
  2116. (concat type ":" raw-path))
  2117. ((and re-radio (string-match re-radio raw-path))
  2118. (setq radiop t))
  2119. ((equal type "mailto")
  2120. (concat type ":" raw-path))
  2121. ((equal type "file")
  2122. (if (and (org-file-image-p
  2123. (expand-file-name (org-link-unescape raw-path))
  2124. org-export-latex-inline-image-extensions)
  2125. (or (get-text-property 0 'org-no-description raw-path)
  2126. (equal desc full-raw-path)))
  2127. (setq imgp t)
  2128. (progn (setq raw-path (org-link-unescape raw-path))
  2129. (when (string-match "\\(.+\\)::.+" raw-path)
  2130. (setq raw-path (match-string 1 raw-path)))
  2131. (if (file-exists-p raw-path)
  2132. (concat type "://" (expand-file-name raw-path))
  2133. (concat type "://" (org-export-directory
  2134. :LaTeX org-export-latex-options-plist)
  2135. raw-path))))))))
  2136. ;; process with link inserting
  2137. (apply 'delete-region remove)
  2138. (setq caption (and caption (org-export-latex-fontify-headline caption)))
  2139. (cond ((and imgp (plist-get org-export-latex-options-plist :latex-inline-images))
  2140. ;; OK, we need to inline an image
  2141. (insert
  2142. (org-export-latex-format-image raw-path caption label attr shortn)))
  2143. (coderefp
  2144. (insert (format
  2145. (org-export-get-coderef-format path desc)
  2146. (cdr (assoc path org-export-code-refs)))))
  2147. (radiop (insert (format org-export-latex-hyperref-format
  2148. (org-solidify-link-text raw-path) desc)))
  2149. ((not type)
  2150. (insert (format org-export-latex-hyperref-format
  2151. (org-remove-initial-hash
  2152. (org-solidify-link-text raw-path))
  2153. desc)))
  2154. (path
  2155. (when (org-at-table-p)
  2156. ;; There is a strange problem when we have a link in a table,
  2157. ;; ampersands then cause a problem. I think this must be
  2158. ;; a LaTeX issue, but we here implement a work-around anyway.
  2159. (setq path (org-export-latex-protect-amp path)
  2160. desc (org-export-latex-protect-amp desc)))
  2161. (insert
  2162. (if (string-match "%s.*%s" org-export-latex-href-format)
  2163. (format org-export-latex-href-format path desc)
  2164. (format org-export-latex-href-format path))))
  2165. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  2166. ;; The link protocol has a function for formatting the link
  2167. (insert
  2168. (save-match-data
  2169. (funcall fnc (org-link-unescape raw-path) desc 'latex))))
  2170. ;; Unrecognized path type
  2171. (t (insert (format org-export-latex-link-with-unknown-path-format desc))))))))
  2172. (defun org-export-latex-format-image (path caption label attr &optional shortn)
  2173. "Format the image element, depending on user settings."
  2174. (let (ind floatp wrapp multicolumnp placement figenv)
  2175. (setq floatp (or caption label))
  2176. (setq ind (org-get-text-property-any 0 'original-indentation path))
  2177. (when (and attr (stringp attr))
  2178. (if (string-match "[ \t]*\\<wrap\\>" attr)
  2179. (setq wrapp t floatp nil attr (replace-match "" t t attr)))
  2180. (if (string-match "[ \t]*\\<float\\>" attr)
  2181. (setq wrapp nil floatp t attr (replace-match "" t t attr)))
  2182. (if (string-match "[ \t]*\\<multicolumn\\>" attr)
  2183. (setq multicolumnp t attr (replace-match "" t t attr))))
  2184. (setq placement
  2185. (cond
  2186. (wrapp "{l}{0.5\\textwidth}")
  2187. (floatp (concat "[" org-latex-default-figure-position "]"))
  2188. (t "")))
  2189. (when (and attr (stringp attr)
  2190. (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
  2191. (setq placement (match-string 1 attr)
  2192. attr (replace-match "" t t attr)))
  2193. (setq attr (and attr (org-trim attr)))
  2194. (when (or (not attr) (= (length attr) 0))
  2195. (setq attr (cond (floatp "width=0.7\\textwidth")
  2196. (wrapp "width=0.48\\textwidth")
  2197. (t attr))))
  2198. (setq figenv
  2199. (cond
  2200. (wrapp "\\begin{wrapfigure}%placement
  2201. \\centering
  2202. \\includegraphics[%attr]{%path}
  2203. \\caption%shortn{%labelcmd%caption}
  2204. \\end{wrapfigure}")
  2205. (multicolumnp "\\begin{figure*}%placement
  2206. \\centering
  2207. \\includegraphics[%attr]{%path}
  2208. \\caption%shortn{%labelcmd%caption}
  2209. \\end{figure*}")
  2210. (floatp "\\begin{figure}%placement
  2211. \\centering
  2212. \\includegraphics[%attr]{%path}
  2213. \\caption%shortn{%labelcmd%caption}
  2214. \\end{figure}")
  2215. (t "\\includegraphics[%attr]{%path}")))
  2216. (setq figenv (mapconcat 'identity (split-string figenv "\n")
  2217. (save-excursion (beginning-of-line 1)
  2218. (looking-at "[ \t]*")
  2219. (concat "\n" (match-string 0)))))
  2220. (if (and (not label) (not caption)
  2221. (string-match "^\\\\caption{.*\n" figenv))
  2222. (setq figenv (replace-match "" t t figenv)))
  2223. (org-add-props
  2224. (org-fill-template
  2225. figenv
  2226. (list (cons "path"
  2227. (if (file-name-absolute-p path)
  2228. (expand-file-name path)
  2229. path))
  2230. (cons "attr" attr)
  2231. (cons "shortn" (if shortn (format "[%s]" shortn) ""))
  2232. (cons "labelcmd" (if label (format "\\label{%s}"
  2233. label)""))
  2234. (cons "caption" (or caption ""))
  2235. (cons "placement" (or placement ""))))
  2236. nil 'original-indentation ind)))
  2237. (defun org-export-latex-protect-amp (s)
  2238. (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
  2239. (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
  2240. t t s)))
  2241. s)
  2242. (defun org-remove-initial-hash (s)
  2243. (if (string-match "\\`#" s)
  2244. (substring s 1)
  2245. s))
  2246. (defvar org-latex-entities) ; defined below
  2247. (defvar org-latex-entities-regexp) ; defined below
  2248. (defun org-export-latex-preprocess (parameters)
  2249. "Clean stuff in the LaTeX export."
  2250. ;; Replace footnotes.
  2251. (when (plist-get parameters :footnotes)
  2252. (goto-char (point-min))
  2253. (let (ref)
  2254. (while (setq ref (org-footnote-get-next-reference))
  2255. (let* ((beg (nth 1 ref))
  2256. (lbl (car ref))
  2257. (def (nth 1 (assoc (string-to-number lbl)
  2258. (mapcar (lambda (e) (cdr e))
  2259. org-export-footnotes-seen)))))
  2260. ;; Fix body for footnotes ending on a link or a list and
  2261. ;; remove definition from buffer.
  2262. (setq def
  2263. (concat def
  2264. (if (string-match "ORG-LIST-END-MARKER\\'" def)
  2265. "\n" " ")))
  2266. (org-footnote-delete-definitions lbl)
  2267. ;; Compute string to insert (FNOTE), and protect the outside
  2268. ;; macro from further transformation. When footnote at
  2269. ;; point is referring to a previously defined footnote, use
  2270. ;; \footnotemark. Otherwise, use \footnote.
  2271. (let ((fnote (if (member lbl org-export-latex-footmark-seen)
  2272. (org-export-latex-protect-string
  2273. (format "\\footnotemark[%s]" lbl))
  2274. (push lbl org-export-latex-footmark-seen)
  2275. (concat (org-export-latex-protect-string "\\footnote{")
  2276. def
  2277. (org-export-latex-protect-string "}"))))
  2278. ;; Check if another footnote is immediately following.
  2279. ;; If so, add a separator in-between.
  2280. (sep (org-export-latex-protect-string
  2281. (if (save-excursion (goto-char (1- (nth 2 ref)))
  2282. (let ((next (org-footnote-get-next-reference)))
  2283. (and next (= (nth 1 next) (nth 2 ref)))))
  2284. org-export-latex-footnote-separator ""))))
  2285. (when (org-at-heading-p)
  2286. (setq fnote (concat (org-export-latex-protect-string "\\protect")
  2287. fnote)))
  2288. ;; Ensure a footnote at column 0 cannot end a list
  2289. ;; containing it.
  2290. (put-text-property 0 (length fnote) 'original-indentation 1000 fnote)
  2291. ;; Replace footnote reference with FNOTE and, maybe, SEP.
  2292. ;; `save-excursion' is required if there are two footnotes
  2293. ;; in a row. In that case, point would be left at the
  2294. ;; beginning of the second one, and
  2295. ;; `org-footnote-get-next-reference' would then skip it.
  2296. (goto-char beg)
  2297. (delete-region beg (nth 2 ref))
  2298. (save-excursion (insert fnote sep)))))))
  2299. ;; Remove footnote section tag for LaTeX
  2300. (goto-char (point-min))
  2301. (while (re-search-forward
  2302. (concat "^" footnote-section-tag-regexp) nil t)
  2303. (org-if-unprotected
  2304. (replace-match "")))
  2305. ;; Remove any left-over footnote definition.
  2306. (mapc (lambda (fn) (org-footnote-delete-definitions (car fn)))
  2307. org-export-footnotes-data)
  2308. (mapc (lambda (fn) (org-footnote-delete-definitions fn))
  2309. org-export-latex-footmark-seen)
  2310. ;; Preserve line breaks
  2311. (goto-char (point-min))
  2312. (while (re-search-forward "\\\\\\\\" nil t)
  2313. (add-text-properties (match-beginning 0) (match-end 0)
  2314. '(org-protected t)))
  2315. ;; Preserve latex environments
  2316. (goto-char (point-min))
  2317. (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
  2318. (org-if-unprotected
  2319. (let* ((start (progn (beginning-of-line) (point)))
  2320. (end (and (re-search-forward
  2321. (concat "^[ \t]*\\\\end{"
  2322. (regexp-quote (match-string 1))
  2323. "}") nil t)
  2324. (point-at-eol))))
  2325. (if end
  2326. (add-text-properties start end '(org-protected t))
  2327. (goto-char (point-at-eol))))))
  2328. ;; Preserve math snippets
  2329. (let* ((matchers (plist-get org-format-latex-options :matchers))
  2330. (re-list org-latex-regexps)
  2331. beg end re e m n block off)
  2332. ;; Check the different regular expressions
  2333. (while (setq e (pop re-list))
  2334. (setq m (car e) re (nth 1 e) n (nth 2 e)
  2335. block (if (nth 3 e) "\n\n" ""))
  2336. (setq off (if (member m '("$" "$1")) 1 0))
  2337. (when (and (member m matchers) (not (equal m "begin")))
  2338. (goto-char (point-min))
  2339. (while (re-search-forward re nil t)
  2340. (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
  2341. (add-text-properties beg end '(org-protected t org-latex-math t))))))
  2342. ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
  2343. (goto-char (point-min))
  2344. (let ((case-fold-search nil))
  2345. (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
  2346. (unless (eq (char-before (match-beginning 1)) ?\\)
  2347. (org-if-unprotected-1
  2348. (replace-match (org-export-latex-protect-string
  2349. (concat "\\" (match-string 1)
  2350. "{}")) t t)))))
  2351. ;; Convert blockquotes
  2352. (goto-char (point-min))
  2353. (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
  2354. (org-replace-match-keep-properties "\\begin{quote}" t t))
  2355. (goto-char (point-min))
  2356. (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
  2357. (org-replace-match-keep-properties "\\end{quote}" t t))
  2358. ;; Convert verse
  2359. (goto-char (point-min))
  2360. (while (search-forward "ORG-VERSE-START" nil t)
  2361. (org-replace-match-keep-properties "\\begin{verse}" t t)
  2362. (beginning-of-line 2)
  2363. (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
  2364. (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
  2365. (goto-char (match-end 1))
  2366. (org-replace-match-keep-properties
  2367. (org-export-latex-protect-string
  2368. (concat "\\hspace*{1cm}" (match-string 2))) t t)
  2369. (beginning-of-line 1))
  2370. (if (looking-at "[ \t]*$")
  2371. (insert (org-export-latex-protect-string "\\vspace*{1em}"))
  2372. (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
  2373. (end-of-line 1)
  2374. (insert "\\\\")))
  2375. (beginning-of-line 2))
  2376. (and (looking-at "[ \t]*ORG-VERSE-END.*")
  2377. (org-replace-match-keep-properties "\\end{verse}" t t)))
  2378. ;; Convert #+INDEX to LaTeX \\index.
  2379. (goto-char (point-min))
  2380. (let ((case-fold-search t) entry)
  2381. (while (re-search-forward
  2382. "^[ \t]*#\\+index:[ \t]*\\([^ \t\r\n].*?\\)[ \t]*$"
  2383. nil t)
  2384. (setq entry
  2385. (save-match-data
  2386. (org-export-latex-protect-string
  2387. (org-export-latex-fontify-headline (match-string 1)))))
  2388. (replace-match (format "\\index{%s}" entry) t t)))
  2389. ;; Convert center
  2390. (goto-char (point-min))
  2391. (while (search-forward "ORG-CENTER-START" nil t)
  2392. (org-replace-match-keep-properties "\\begin{center}" t t))
  2393. (goto-char (point-min))
  2394. (while (search-forward "ORG-CENTER-END" nil t)
  2395. (org-replace-match-keep-properties "\\end{center}" t t))
  2396. (run-hooks 'org-export-latex-after-blockquotes-hook)
  2397. ;; Convert horizontal rules
  2398. (goto-char (point-min))
  2399. (while (re-search-forward "^[ \t]*-\\{5,\\}[ \t]*$" nil t)
  2400. (org-if-unprotected
  2401. (replace-match (org-export-latex-protect-string "\\hrule") t t)))
  2402. ;; Protect LaTeX commands like \command[...]{...} or \command{...}
  2403. (goto-char (point-min))
  2404. (let ((re (concat
  2405. "\\\\\\([a-zA-Z]+\\*?\\)"
  2406. "\\(?:<[^<>\n]*>\\)*"
  2407. "\\(?:\\[[^][\n]*?\\]\\)*"
  2408. "\\(?:<[^<>\n]*>\\)*"
  2409. "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
  2410. (while (re-search-forward re nil t)
  2411. (unless (or
  2412. ;; Check for comment line.
  2413. (save-excursion (goto-char (match-beginning 0))
  2414. (org-in-indented-comment-line))
  2415. ;; Check if this is a defined entity, so that is may
  2416. ;; need conversion.
  2417. (org-entity-get (match-string 1))
  2418. ;; Do not protect interior of footnotes. Those have
  2419. ;; already been taken care of earlier in the function.
  2420. ;; Yet, keep looking inside them for more commands.
  2421. (and (equal (match-string 1) "footnote")
  2422. (goto-char (match-end 1))))
  2423. (add-text-properties (match-beginning 0) (match-end 0)
  2424. '(org-protected t)))))
  2425. ;; Special case for \nbsp
  2426. (goto-char (point-min))
  2427. (while (re-search-forward "\\\\nbsp\\({}\\|\\>\\)" nil t)
  2428. (org-if-unprotected
  2429. (replace-match (org-export-latex-protect-string "~"))))
  2430. ;; Protect LaTeX entities
  2431. (goto-char (point-min))
  2432. (while (re-search-forward org-latex-entities-regexp nil t)
  2433. (org-if-unprotected
  2434. (add-text-properties (match-beginning 0) (match-end 0)
  2435. '(org-protected t))))
  2436. ;; Replace radio links
  2437. (goto-char (point-min))
  2438. (while (re-search-forward
  2439. (concat "<<<?" org-export-latex-all-targets-re
  2440. ">>>?\\((INVISIBLE)\\)?") nil t)
  2441. (org-if-unprotected-at (+ (match-beginning 0) 2)
  2442. (replace-match
  2443. (concat
  2444. (org-export-latex-protect-string
  2445. (format "\\label{%s}" (save-match-data (org-solidify-link-text
  2446. (match-string 1)))))
  2447. (if (match-string 2) "" (match-string 1)))
  2448. t t)))
  2449. ;; Delete @<...> constructs
  2450. ;; Thanks to Daniel Clemente for this regexp
  2451. (goto-char (point-min))
  2452. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  2453. (org-if-unprotected
  2454. (replace-match ""))))
  2455. (defun org-export-latex-fix-inputenc ()
  2456. "Set the coding system in inputenc to what the buffer is."
  2457. (let* ((cs buffer-file-coding-system)
  2458. (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs))
  2459. "utf8")))
  2460. (when opt
  2461. ;; Translate if that is requested
  2462. (setq opt (or (cdr (assoc opt org-export-latex-inputenc-alist)) opt))
  2463. ;; find the \usepackage statement and replace the option
  2464. (goto-char (point-min))
  2465. (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  2466. nil t)
  2467. (goto-char (match-beginning 1))
  2468. (delete-region (match-beginning 1) (match-end 1))
  2469. (insert opt))
  2470. (and buffer-file-name
  2471. (save-buffer)))))
  2472. ;;; List handling:
  2473. (defun org-export-latex-lists ()
  2474. "Convert plain text lists in current buffer into LaTeX lists."
  2475. ;; `org-list-end-re' output has changed since preprocess from
  2476. ;; org-exp.el. Make sure it is taken into account.
  2477. (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
  2478. (mapc
  2479. (lambda (e)
  2480. ;; For each type of context allowed for list export (E), find
  2481. ;; every list, parse it, delete it and insert resulting
  2482. ;; conversion to latex (RES), while keeping the same
  2483. ;; `original-indentation' property.
  2484. (let (res)
  2485. (goto-char (point-min))
  2486. (while (re-search-forward (org-item-beginning-re) nil t)
  2487. (when (and (eq (get-text-property (point) 'list-context) e)
  2488. (not (get-text-property (point) 'org-example)))
  2489. (beginning-of-line)
  2490. (setq res
  2491. (org-list-to-latex
  2492. ;; Narrowing is needed because we're converting
  2493. ;; from inner functions to outer ones.
  2494. (save-restriction
  2495. (narrow-to-region (point) (point-max))
  2496. (org-list-parse-list t))
  2497. org-export-latex-list-parameters))
  2498. ;; Extend previous value of original-indentation to the
  2499. ;; whole string
  2500. (insert (org-add-props res nil 'original-indentation
  2501. (org-find-text-property-in-string
  2502. 'original-indentation res)))))))
  2503. ;; List of allowed contexts for export, and the default one.
  2504. (append org-list-export-context '(nil)))))
  2505. (defconst org-latex-entities
  2506. '("\\!"
  2507. "\\'"
  2508. "\\+"
  2509. "\\,"
  2510. "\\-"
  2511. "\\:"
  2512. "\\;"
  2513. "\\<"
  2514. "\\="
  2515. "\\>"
  2516. "\\Huge"
  2517. "\\LARGE"
  2518. "\\Large"
  2519. "\\Styles"
  2520. "\\\\"
  2521. "\\`"
  2522. "\\\""
  2523. "\\addcontentsline"
  2524. "\\address"
  2525. "\\addtocontents"
  2526. "\\addtocounter"
  2527. "\\addtolength"
  2528. "\\addvspace"
  2529. "\\alph"
  2530. "\\appendix"
  2531. "\\arabic"
  2532. "\\author"
  2533. "\\begin{array}"
  2534. "\\begin{center}"
  2535. "\\begin{description}"
  2536. "\\begin{enumerate}"
  2537. "\\begin{eqnarray}"
  2538. "\\begin{equation}"
  2539. "\\begin{figure}"
  2540. "\\begin{flushleft}"
  2541. "\\begin{flushright}"
  2542. "\\begin{itemize}"
  2543. "\\begin{list}"
  2544. "\\begin{minipage}"
  2545. "\\begin{picture}"
  2546. "\\begin{quotation}"
  2547. "\\begin{quote}"
  2548. "\\begin{tabbing}"
  2549. "\\begin{table}"
  2550. "\\begin{tabular}"
  2551. "\\begin{thebibliography}"
  2552. "\\begin{theorem}"
  2553. "\\begin{titlepage}"
  2554. "\\begin{verbatim}"
  2555. "\\begin{verse}"
  2556. "\\bf"
  2557. "\\bf"
  2558. "\\bibitem"
  2559. "\\bigskip"
  2560. "\\cdots"
  2561. "\\centering"
  2562. "\\circle"
  2563. "\\cite"
  2564. "\\cleardoublepage"
  2565. "\\clearpage"
  2566. "\\cline"
  2567. "\\closing"
  2568. "\\dashbox"
  2569. "\\date"
  2570. "\\ddots"
  2571. "\\dotfill"
  2572. "\\em"
  2573. "\\fbox"
  2574. "\\flushbottom"
  2575. "\\fnsymbol"
  2576. "\\footnote"
  2577. "\\footnotemark"
  2578. "\\footnotesize"
  2579. "\\footnotetext"
  2580. "\\frac"
  2581. "\\frame"
  2582. "\\framebox"
  2583. "\\hfill"
  2584. "\\hline"
  2585. "\\hrulespace"
  2586. "\\hspace"
  2587. "\\huge"
  2588. "\\hyphenation"
  2589. "\\include"
  2590. "\\includeonly"
  2591. "\\indent"
  2592. "\\input"
  2593. "\\it"
  2594. "\\kill"
  2595. "\\label"
  2596. "\\large"
  2597. "\\ldots"
  2598. "\\line"
  2599. "\\linebreak"
  2600. "\\linethickness"
  2601. "\\listoffigures"
  2602. "\\listoftables"
  2603. "\\location"
  2604. "\\makebox"
  2605. "\\maketitle"
  2606. "\\mark"
  2607. "\\mbox"
  2608. "\\medskip"
  2609. "\\multicolumn"
  2610. "\\multiput"
  2611. "\\newcommand"
  2612. "\\newcounter"
  2613. "\\newenvironment"
  2614. "\\newfont"
  2615. "\\newlength"
  2616. "\\newline"
  2617. "\\newpage"
  2618. "\\newsavebox"
  2619. "\\newtheorem"
  2620. "\\nocite"
  2621. "\\nofiles"
  2622. "\\noindent"
  2623. "\\nolinebreak"
  2624. "\\nopagebreak"
  2625. "\\normalsize"
  2626. "\\onecolumn"
  2627. "\\opening"
  2628. "\\oval"
  2629. "\\overbrace"
  2630. "\\overline"
  2631. "\\pagebreak"
  2632. "\\pagenumbering"
  2633. "\\pageref"
  2634. "\\pagestyle"
  2635. "\\par"
  2636. "\\parbox"
  2637. "\\put"
  2638. "\\raggedbottom"
  2639. "\\raggedleft"
  2640. "\\raggedright"
  2641. "\\raisebox"
  2642. "\\ref"
  2643. "\\rm"
  2644. "\\roman"
  2645. "\\rule"
  2646. "\\savebox"
  2647. "\\sc"
  2648. "\\scriptsize"
  2649. "\\setcounter"
  2650. "\\setlength"
  2651. "\\settowidth"
  2652. "\\sf"
  2653. "\\shortstack"
  2654. "\\signature"
  2655. "\\sl"
  2656. "\\small"
  2657. "\\smallskip"
  2658. "\\sqrt"
  2659. "\\tableofcontents"
  2660. "\\telephone"
  2661. "\\thanks"
  2662. "\\thispagestyle"
  2663. "\\tiny"
  2664. "\\title"
  2665. "\\tt"
  2666. "\\twocolumn"
  2667. "\\typein"
  2668. "\\typeout"
  2669. "\\underbrace"
  2670. "\\underline"
  2671. "\\usebox"
  2672. "\\usecounter"
  2673. "\\value"
  2674. "\\vdots"
  2675. "\\vector"
  2676. "\\verb"
  2677. "\\vfill"
  2678. "\\vline"
  2679. "\\vspace")
  2680. "A list of LaTeX commands to be protected when performing conversion.")
  2681. (defconst org-latex-entities-regexp
  2682. (let (names rest)
  2683. (dolist (x org-latex-entities)
  2684. (if (string-match "[a-zA-Z]$" x)
  2685. (push x names)
  2686. (push x rest)))
  2687. (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
  2688. "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
  2689. (provide 'org-export-latex)
  2690. (provide 'org-latex)
  2691. ;; Local variables:
  2692. ;; generated-autoload-file: "org-loaddefs.el"
  2693. ;; End:
  2694. ;;; org-latex.el ends here