org-latex.el 89 KB

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