org-latex.el 102 KB

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