org-latex.el 92 KB

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