org-latex.el 105 KB

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