org-latex.el 101 KB

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