org-latex.el 101 KB

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