org-latex.el 90 KB

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