org-e-latex.el 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363
  1. ;;; org-e-latex.el --- LaTeX Back-End For Org Export Engine
  2. ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; This library implements a LaTeX back-end for Org generic exporter.
  18. ;;
  19. ;; To test it, run
  20. ;;
  21. ;; M-: (org-export-to-buffer 'e-latex "*Test e-LaTeX*") RET
  22. ;;
  23. ;; in an org-mode buffer then switch to the buffer to see the LaTeX
  24. ;; export. See contrib/lisp/org-export.el for more details on how
  25. ;; this exporter works.
  26. ;;
  27. ;; It introduces three new buffer keywords: "LATEX_CLASS",
  28. ;; "LATEX_CLASS_OPTIONS" and "LATEX_HEADER".
  29. ;;; Code:
  30. (eval-when-compile (require 'cl))
  31. (defvar org-export-latex-default-packages-alist)
  32. (defvar org-export-latex-packages-alist)
  33. (declare-function org-element-property "org-element" (property element))
  34. (declare-function org-element-normalize-string "org-element" (s))
  35. (declare-function org-element-parse-secondary-string
  36. "org-element" (string restriction &optional buffer))
  37. (defvar org-element-string-restrictions)
  38. (defvar org-element-object-restrictions)
  39. (declare-function org-export-data "org-export" (data info))
  40. (declare-function org-export-directory "org-export" (type plist))
  41. (declare-function org-export-expand-macro "org-export" (macro info))
  42. (declare-function org-export-first-sibling-p "org-export" (headline info))
  43. (declare-function org-export-footnote-first-reference-p "org-export"
  44. (footnote-reference info))
  45. (declare-function org-export-format-code "org-export"
  46. (code fun &optional num-lines ref-alist))
  47. (declare-function org-export-format-code-default "org-export" (element info))
  48. (declare-function org-export-get-coderef-format "org-export" (path desc))
  49. (declare-function org-export-get-footnote-definition "org-export"
  50. (footnote-reference info))
  51. (declare-function org-export-get-footnote-number "org-export" (footnote info))
  52. (declare-function org-export-get-previous-element "org-export" (blob info))
  53. (declare-function org-export-get-relative-level "org-export" (headline info))
  54. (declare-function org-export-unravel-code "org-export" (element))
  55. (declare-function org-export-inline-image-p "org-export"
  56. (link &optional extensions))
  57. (declare-function org-export-last-sibling-p "org-export" (headline info))
  58. (declare-function org-export-low-level-p "org-export" (headline info))
  59. (declare-function org-export-output-file-name
  60. "org-export" (extension &optional subtreep pub-dir))
  61. (declare-function org-export-resolve-coderef "org-export" (ref info))
  62. (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
  63. (declare-function org-export-solidify-link-text "org-export" (s))
  64. (declare-function
  65. org-export-to-buffer "org-export"
  66. (backend buffer &optional subtreep visible-only body-only ext-plist))
  67. (declare-function
  68. org-export-to-file "org-export"
  69. (backend file &optional subtreep visible-only body-only ext-plist))
  70. ;;; Internal Variables
  71. (defconst org-e-latex-option-alist
  72. '((:date "DATE" nil org-e-latex-date-format t)
  73. (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
  74. (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
  75. (:latex-header-extra "LATEX_HEADER" nil nil newline))
  76. "Alist between LaTeX export properties and ways to set them.
  77. See `org-export-option-alist' for more information on the
  78. structure of the value.")
  79. ;;; User Configurable Variables
  80. (defgroup org-export-e-latex nil
  81. "Options for exporting Org mode files to LaTeX."
  82. :tag "Org Export LaTeX"
  83. :group 'org-export)
  84. ;;;; Preamble
  85. (defcustom org-e-latex-default-class "article"
  86. "The default LaTeX class."
  87. :group 'org-export-e-latex
  88. :type '(string :tag "LaTeX class"))
  89. (defcustom org-e-latex-classes
  90. '(("article"
  91. "\\documentclass[11pt]{article}"
  92. ("\\section{%s}" . "\\section*{%s}")
  93. ("\\subsection{%s}" . "\\subsection*{%s}")
  94. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  95. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  96. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  97. ("report"
  98. "\\documentclass[11pt]{report}"
  99. ("\\part{%s}" . "\\part*{%s}")
  100. ("\\chapter{%s}" . "\\chapter*{%s}")
  101. ("\\section{%s}" . "\\section*{%s}")
  102. ("\\subsection{%s}" . "\\subsection*{%s}")
  103. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  104. ("book"
  105. "\\documentclass[11pt]{book}"
  106. ("\\part{%s}" . "\\part*{%s}")
  107. ("\\chapter{%s}" . "\\chapter*{%s}")
  108. ("\\section{%s}" . "\\section*{%s}")
  109. ("\\subsection{%s}" . "\\subsection*{%s}")
  110. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  111. "Alist of LaTeX classes and associated header and structure.
  112. If #+LaTeX_CLASS is set in the buffer, use its value and the
  113. associated information. Here is the structure of each cell:
  114. \(class-name
  115. header-string
  116. \(numbered-section . unnumbered-section\)
  117. ...\)
  118. The header string
  119. -----------------
  120. The HEADER-STRING is the header that will be inserted into the
  121. LaTeX file. It should contain the \\documentclass macro, and
  122. anything else that is needed for this setup. To this header, the
  123. following commands will be added:
  124. - Calls to \\usepackage for all packages mentioned in the
  125. variables `org-export-latex-default-packages-alist' and
  126. `org-export-latex-packages-alist'. Thus, your header
  127. definitions should avoid to also request these packages.
  128. - Lines specified via \"#+LaTeX_HEADER:\"
  129. If you need more control about the sequence in which the header
  130. is built up, or if you want to exclude one of these building
  131. blocks for a particular class, you can use the following
  132. macro-like placeholders.
  133. [DEFAULT-PACKAGES] \\usepackage statements for default packages
  134. [NO-DEFAULT-PACKAGES] do not include any of the default packages
  135. [PACKAGES] \\usepackage statements for packages
  136. [NO-PACKAGES] do not include the packages
  137. [EXTRA] the stuff from #+LaTeX_HEADER
  138. [NO-EXTRA] do not include #+LaTeX_HEADER stuff
  139. [BEAMER-HEADER-EXTRA] the beamer extra headers
  140. So a header like
  141. \\documentclass{article}
  142. [NO-DEFAULT-PACKAGES]
  143. [EXTRA]
  144. \\providecommand{\\alert}[1]{\\textbf{#1}}
  145. [PACKAGES]
  146. will omit the default packages, and will include the
  147. #+LaTeX_HEADER lines, then have a call to \\providecommand, and
  148. then place \\usepackage commands based on the content of
  149. `org-export-latex-packages-alist'.
  150. If your header, `org-export-latex-default-packages-alist' or
  151. `org-export-latex-packages-alist' inserts
  152. \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
  153. replaced with a coding system derived from
  154. `buffer-file-coding-system'. See also the variable
  155. `org-e-latex-inputenc-alist' for a way to influence this
  156. mechanism.
  157. The sectioning structure
  158. ------------------------
  159. The sectioning structure of the class is given by the elements
  160. following the header string. For each sectioning level, a number
  161. of strings is specified. A %s formatter is mandatory in each
  162. section string and will be replaced by the title of the section.
  163. Instead of a cons cell \(numbered . unnumbered\), you can also
  164. provide a list of 2 or 4 elements,
  165. \(numbered-open numbered-close\)
  166. or
  167. \(numbered-open numbered-close unnumbered-open unnumbered-close\)
  168. providing opening and closing strings for a LaTeX environment
  169. that should represent the document section. The opening clause
  170. should have a %s to represent the section title.
  171. Instead of a list of sectioning commands, you can also specify
  172. a function name. That function will be called with two
  173. parameters, the \(reduced) level of the headline, and a predicate
  174. non-nil when the headline should be numbered. It must return
  175. a format string in which the section title will be added."
  176. :group 'org-export-e-latex
  177. :type '(repeat
  178. (list (string :tag "LaTeX class")
  179. (string :tag "LaTeX header")
  180. (repeat :tag "Levels" :inline t
  181. (choice
  182. (cons :tag "Heading"
  183. (string :tag " numbered")
  184. (string :tag "unnumbered"))
  185. (list :tag "Environment"
  186. (string :tag "Opening (numbered)")
  187. (string :tag "Closing (numbered)")
  188. (string :tag "Opening (unnumbered)")
  189. (string :tag "Closing (unnumbered)"))
  190. (function :tag "Hook computing sectioning"))))))
  191. (defcustom org-e-latex-inputenc-alist nil
  192. "Alist of inputenc coding system names, and what should really be used.
  193. For example, adding an entry
  194. (\"utf8\" . \"utf8x\")
  195. will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
  196. are written as utf8 files."
  197. :group 'org-export-e-latex
  198. :type '(repeat
  199. (cons
  200. (string :tag "Derived from buffer")
  201. (string :tag "Use this instead"))))
  202. (defcustom org-e-latex-date-format
  203. "\\today"
  204. "Format string for \\date{...}."
  205. :group 'org-export-e-latex
  206. :type 'boolean)
  207. (defcustom org-e-latex-title-command "\\maketitle"
  208. "The command used to insert the title just after \\begin{document}.
  209. If this string contains the formatting specification \"%s\" then
  210. it will be used as a formatting string, passing the title as an
  211. argument."
  212. :group 'org-export-e-latex
  213. :type 'string)
  214. ;;;; Headline
  215. (defcustom org-e-latex-format-headline-function nil
  216. "Function to format headline text.
  217. This function will be called with 5 arguments:
  218. TODO the todo keyword \(string or nil\).
  219. TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
  220. PRIORITY the priority of the headline \(integer or nil\)
  221. TEXT the main headline text \(string\).
  222. TAGS the tags string, separated with colons \(string or nil\).
  223. The function result will be used in the section format string.
  224. As an example, one could set the variable to the following, in
  225. order to reproduce the default set-up:
  226. \(defun org-e-latex-format-headline \(todo todo-type priority text tags\)
  227. \"Default format function for an headline.\"
  228. \(concat \(when todo
  229. \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
  230. \(when priority
  231. \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  232. text
  233. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
  234. :group 'org-export-e-latex
  235. :type 'function)
  236. ;;;; Footnotes
  237. (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
  238. "Text used to separate footnotes."
  239. :group 'org-export-e-latex
  240. :type 'string)
  241. ;;;; Timestamps
  242. (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
  243. "A printf format string to be applied to active timestamps."
  244. :group 'org-export-e-latex
  245. :type 'string)
  246. (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
  247. "A printf format string to be applied to inactive timestamps."
  248. :group 'org-export-e-latex
  249. :type 'string)
  250. (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
  251. "A printf format string to be applied to diary timestamps."
  252. :group 'org-export-e-latex
  253. :type 'string)
  254. ;;;; Links
  255. (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
  256. "Default option for images."
  257. :group 'org-export-e-latex
  258. :type 'string)
  259. (defcustom org-e-latex-default-figure-position "htb"
  260. "Default position for latex figures."
  261. :group 'org-export-e-latex
  262. :type 'string)
  263. (defcustom org-e-latex-inline-image-rules
  264. '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
  265. "Rules characterizing image files that can be inlined into LaTeX.
  266. A rule consists in an association whose key is the type of link
  267. to consider, and value is a regexp that will be matched against
  268. link's path.
  269. Note that, by default, the image extension *actually* allowed
  270. depend on the way the LaTeX file is processed. When used with
  271. pdflatex, pdf, jpg and png images are OK. When processing
  272. through dvi to Postscript, only ps and eps are allowed. The
  273. default we use here encompasses both."
  274. :group 'org-export-e-latex
  275. :type '(alist :key-type (string :tag "Type")
  276. :value-type (regexp :tag "Path")))
  277. ;;;; Tables
  278. (defcustom org-e-latex-default-table-environment "tabular"
  279. "Default environment used to build tables."
  280. :group 'org-export-e-latex
  281. :type 'string)
  282. (defcustom org-e-latex-tables-centered t
  283. "When non-nil, tables are exported in a center environment."
  284. :group 'org-export-e-latex
  285. :type 'boolean)
  286. (defcustom org-e-latex-tables-verbatim nil
  287. "When non-nil, tables are exported verbatim."
  288. :group 'org-export-e-latex
  289. :type 'boolean)
  290. (defcustom org-e-latex-tables-booktabs nil
  291. "When non-nil, display tables in a formal \"booktabs\" style.
  292. This option assumes that the \"booktabs\" package is properly
  293. loaded in the header of the document. This value can be ignored
  294. locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
  295. attributes."
  296. :group 'org-export-e-latex
  297. :type 'boolean)
  298. (defcustom org-e-latex-table-caption-above t
  299. "When non-nil, place caption string at the beginning of the table.
  300. Otherwise, place it near the end."
  301. :group 'org-export-e-latex
  302. :type 'boolean)
  303. (defcustom org-e-latex-table-scientific-notation "%s\\,(%s)"
  304. "Format string to display numbers in scientific notation.
  305. The format should have \"%s\" twice, for mantissa and exponent
  306. \(i.e. \"%s\\\\times10^{%s}\").
  307. When nil, no transformation is made."
  308. :group 'org-export-e-latex
  309. :type '(choice
  310. (string :tag "Format string")
  311. (const :tag "No formatting")))
  312. ;;;; Text markup
  313. (defcustom org-e-latex-text-markup-alist '((bold . "\\textbf{%s}")
  314. (code . verb)
  315. (italic . "\\emph{%s}")
  316. (strike-through . "\\st{%s}")
  317. (underline . "\\underline{%s}")
  318. (verbatim . protectedtexttt))
  319. "Alist of LaTeX expressions to convert text markup.
  320. The key must be a symbol among `bold', `code', `italic',
  321. `strike-through', `underline' and `verbatim'. The value is
  322. a formatting string to wrap fontified text with.
  323. Value can also be set to the following symbols: `verb' and
  324. `protectedtexttt'. For the former, Org will use \"\\verb\" to
  325. create a format string and select a delimiter character that
  326. isn't in the string. For the latter, Org will use \"\\texttt\"
  327. to typeset and try to protect special characters.
  328. If no association can be found for a given markup, text will be
  329. returned as-is."
  330. :group 'org-export-e-latex
  331. :type 'alist
  332. :options '(bold code italic strike-through underline verbatim))
  333. ;;;; Drawers
  334. (defcustom org-e-latex-format-drawer-function nil
  335. "Function called to format a drawer in LaTeX code.
  336. The function must accept two parameters:
  337. NAME the drawer name, like \"LOGBOOK\"
  338. CONTENTS the contents of the drawer.
  339. The function should return the string to be exported.
  340. For example, the variable could be set to the following function
  341. in order to mimic default behaviour:
  342. \(defun org-e-latex-format-drawer-default \(name contents\)
  343. \"Format a drawer element for LaTeX export.\"
  344. contents\)"
  345. :group 'org-export-e-latex
  346. :type 'function)
  347. ;;;; Inlinetasks
  348. (defcustom org-e-latex-format-inlinetask-function nil
  349. "Function called to format an inlinetask in LaTeX code.
  350. The function must accept six parameters:
  351. TODO the todo keyword, as a string
  352. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  353. PRIORITY the inlinetask priority, as a string
  354. NAME the inlinetask name, as a string.
  355. TAGS the inlinetask tags, as a string.
  356. CONTENTS the contents of the inlinetask, as a string.
  357. The function should return the string to be exported.
  358. For example, the variable could be set to the following function
  359. in order to mimic default behaviour:
  360. \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
  361. \"Format an inline task element for LaTeX export.\"
  362. \(let \(\(full-title
  363. \(concat
  364. \(when todo
  365. \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
  366. \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  367. title
  368. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
  369. \(format \(concat \"\\\\begin{center}\\n\"
  370. \"\\\\fbox{\\n\"
  371. \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
  372. \"%s\\n\\n\"
  373. \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
  374. \"%s\"
  375. \"\\\\end{minipage}}\"
  376. \"\\\\end{center}\"\)
  377. full-title contents\)\)"
  378. :group 'org-export-e-latex
  379. :type 'function)
  380. ;; Src blocks
  381. (defcustom org-e-latex-listings nil
  382. "Non-nil means export source code using the listings package.
  383. This package will fontify source code, possibly even with color.
  384. If you want to use this, you also need to make LaTeX use the
  385. listings package, and if you want to have color, the color
  386. package. Just add these to `org-export-latex-packages-alist',
  387. for example using customize, or with something like:
  388. \(require 'org-e-latex)
  389. \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
  390. \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
  391. Alternatively,
  392. \(setq org-e-latex-listings 'minted)
  393. causes source code to be exported using the minted package as
  394. opposed to listings. If you want to use minted, you need to add
  395. the minted package to `org-export-latex-packages-alist', for
  396. example using customize, or with
  397. \(require 'org-e-latex)
  398. \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
  399. In addition, it is necessary to install pygments
  400. \(http://pygments.org), and to configure the variable
  401. `org-e-latex-pdf-process' so that the -shell-escape option is
  402. passed to pdflatex."
  403. :group 'org-export-e-latex
  404. :type '(choice
  405. (const :tag "Use listings" t)
  406. (const :tag "Use minted" 'minted)
  407. (const :tag "Export verbatim" nil)))
  408. (defcustom org-e-latex-listings-langs
  409. '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
  410. (c "C") (cc "C++")
  411. (fortran "fortran")
  412. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  413. (html "HTML") (xml "XML")
  414. (tex "TeX") (latex "TeX")
  415. (shell-script "bash")
  416. (gnuplot "Gnuplot")
  417. (ocaml "Caml") (caml "Caml")
  418. (sql "SQL") (sqlite "sql"))
  419. "Alist mapping languages to their listing language counterpart.
  420. The key is a symbol, the major mode symbol without the \"-mode\".
  421. The value is the string that should be inserted as the language
  422. parameter for the listings package. If the mode name and the
  423. listings name are the same, the language does not need an entry
  424. in this list - but it does not hurt if it is present."
  425. :group 'org-export-e-latex
  426. :type '(repeat
  427. (list
  428. (symbol :tag "Major mode ")
  429. (string :tag "Listings language"))))
  430. (defcustom org-e-latex-listings-options nil
  431. "Association list of options for the latex listings package.
  432. These options are supplied as a comma-separated list to the
  433. \\lstset command. Each element of the association list should be
  434. a list containing two strings: the name of the option, and the
  435. value. For example,
  436. (setq org-e-latex-listings-options
  437. '((\"basicstyle\" \"\\small\")
  438. (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
  439. will typeset the code in a small size font with underlined, bold
  440. black keywords.
  441. Note that the same options will be applied to blocks of all
  442. languages."
  443. :group 'org-export-e-latex
  444. :type '(repeat
  445. (list
  446. (string :tag "Listings option name ")
  447. (string :tag "Listings option value"))))
  448. (defcustom org-e-latex-minted-langs
  449. '((emacs-lisp "common-lisp")
  450. (cc "c++")
  451. (cperl "perl")
  452. (shell-script "bash")
  453. (caml "ocaml"))
  454. "Alist mapping languages to their minted language counterpart.
  455. The key is a symbol, the major mode symbol without the \"-mode\".
  456. The value is the string that should be inserted as the language
  457. parameter for the minted package. If the mode name and the
  458. listings name are the same, the language does not need an entry
  459. in this list - but it does not hurt if it is present.
  460. Note that minted uses all lower case for language identifiers,
  461. and that the full list of language identifiers can be obtained
  462. with:
  463. pygmentize -L lexers"
  464. :group 'org-export-e-latex
  465. :type '(repeat
  466. (list
  467. (symbol :tag "Major mode ")
  468. (string :tag "Minted language"))))
  469. (defcustom org-e-latex-minted-options nil
  470. "Association list of options for the latex minted package.
  471. These options are supplied within square brackets in
  472. \\begin{minted} environments. Each element of the alist should
  473. be a list containing two strings: the name of the option, and the
  474. value. For example,
  475. \(setq org-e-latex-minted-options
  476. '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
  477. will result in src blocks being exported with
  478. \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
  479. as the start of the minted environment. Note that the same
  480. options will be applied to blocks of all languages."
  481. :group 'org-export-e-latex
  482. :type '(repeat
  483. (list
  484. (string :tag "Minted option name ")
  485. (string :tag "Minted option value"))))
  486. (defvar org-e-latex-custom-lang-environments nil
  487. "Alist mapping languages to language-specific LaTeX environments.
  488. It is used during export of src blocks by the listings and minted
  489. latex packages. For example,
  490. \(setq org-e-latex-custom-lang-environments
  491. '\(\(python \"pythoncode\"\)\)\)
  492. would have the effect that if org encounters begin_src python
  493. during latex export it will output
  494. \\begin{pythoncode}
  495. <src block body>
  496. \\end{pythoncode}")
  497. ;;;; Plain text
  498. (defcustom org-e-latex-quotes
  499. '(("fr"
  500. ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
  501. ("\\(\\S-\\)\"" . "~»")
  502. ("\\(\\s-\\|(\\|^\\)'" . "'"))
  503. ("en"
  504. ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
  505. ("\\(\\S-\\)\"" . "''")
  506. ("\\(\\s-\\|(\\|^\\)'" . "`")))
  507. "Alist for quotes to use when converting english double-quotes.
  508. The CAR of each item in this alist is the language code.
  509. The CDR of each item in this alist is a list of three CONS:
  510. - the first CONS defines the opening quote;
  511. - the second CONS defines the closing quote;
  512. - the last CONS defines single quotes.
  513. For each item in a CONS, the first string is a regexp
  514. for allowed characters before/after the quote, the second
  515. string defines the replacement string for this quote."
  516. :group 'org-export-e-latex
  517. :type '(list
  518. (cons :tag "Opening quote"
  519. (string :tag "Regexp for char before")
  520. (string :tag "Replacement quote "))
  521. (cons :tag "Closing quote"
  522. (string :tag "Regexp for char after ")
  523. (string :tag "Replacement quote "))
  524. (cons :tag "Single quote"
  525. (string :tag "Regexp for char before")
  526. (string :tag "Replacement quote "))))
  527. ;;;; Compilation
  528. (defcustom org-e-latex-pdf-process
  529. '("pdflatex -interaction nonstopmode -output-directory %o %f"
  530. "pdflatex -interaction nonstopmode -output-directory %o %f"
  531. "pdflatex -interaction nonstopmode -output-directory %o %f")
  532. "Commands to process a LaTeX file to a PDF file.
  533. This is a list of strings, each of them will be given to the
  534. shell as a command. %f in the command will be replaced by the
  535. full file name, %b by the file base name \(i.e. without
  536. extension) and %o by the base directory of the file.
  537. The reason why this is a list is that it usually takes several
  538. runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
  539. does not have a clever mechanism to detect which of these
  540. commands have to be run to get to a stable result, and it also
  541. does not do any error checking.
  542. By default, Org uses 3 runs of `pdflatex' to do the processing.
  543. If you have texi2dvi on your system and if that does not cause
  544. the infamous egrep/locale bug:
  545. http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
  546. then `texi2dvi' is the superior choice. Org does offer it as one
  547. of the customize options.
  548. Alternatively, this may be a Lisp function that does the
  549. processing, so you could use this to apply the machinery of
  550. AUCTeX or the Emacs LaTeX mode. This function should accept the
  551. file name as its single argument."
  552. :group 'org-export-pdf
  553. :type '(choice
  554. (repeat :tag "Shell command sequence"
  555. (string :tag "Shell command"))
  556. (const :tag "2 runs of pdflatex"
  557. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  558. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  559. (const :tag "3 runs of pdflatex"
  560. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  561. "pdflatex -interaction nonstopmode -output-directory %o %f"
  562. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  563. (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
  564. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  565. "bibtex %b"
  566. "pdflatex -interaction nonstopmode -output-directory %o %f"
  567. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  568. (const :tag "texi2dvi"
  569. ("texi2dvi -p -b -c -V %f"))
  570. (const :tag "rubber"
  571. ("rubber -d --into %o %f"))
  572. (function)))
  573. (defcustom org-e-latex-logfiles-extensions
  574. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  575. "The list of file extensions to consider as LaTeX logfiles."
  576. :group 'org-export-e-latex
  577. :type '(repeat (string :tag "Extension")))
  578. (defcustom org-e-latex-remove-logfiles t
  579. "Non-nil means remove the logfiles produced by PDF production.
  580. These are the .aux, .log, .out, and .toc files."
  581. :group 'org-export-e-latex
  582. :type 'boolean)
  583. ;;; Internal Functions
  584. (defun org-e-latex--caption/label-string (caption label info)
  585. "Return caption and label LaTeX string for floats.
  586. CAPTION is a cons cell of secondary strings, the car being the
  587. standard caption and the cdr its short form. LABEL is a string
  588. representing the label. INFO is a plist holding contextual
  589. information.
  590. If there's no caption nor label, return the empty string.
  591. For non-floats, see `org-e-latex--wrap-label'."
  592. (let ((label-str (if label (format "\\label{%s}" label) "")))
  593. (cond
  594. ((and (not caption) (not label)) "")
  595. ((not caption) (format "\\label{%s}\n" label))
  596. ;; Option caption format with short name.
  597. ((cdr caption)
  598. (format "\\caption[%s]{%s%s}\n"
  599. (org-export-data (cdr caption) info)
  600. label-str
  601. (org-export-data (car caption) info)))
  602. ;; Standard caption format.
  603. (t (format "\\caption{%s%s}\n"
  604. label-str
  605. (org-export-data (car caption) info))))))
  606. (defun org-e-latex--guess-inputenc (header)
  607. "Set the coding system in inputenc to what the buffer is.
  608. HEADER is the LaTeX header string.
  609. Return the new header."
  610. (let* ((cs (or (ignore-errors
  611. (latexenc-coding-system-to-inputenc
  612. buffer-file-coding-system))
  613. "utf8")))
  614. (if (not cs) header
  615. ;; First translate if that is requested.
  616. (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
  617. ;; Then find the \usepackage statement and replace the option.
  618. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  619. cs header t nil 1))))
  620. (defun org-e-latex--find-verb-separator (s)
  621. "Return a character not used in string S.
  622. This is used to choose a separator for constructs like \\verb."
  623. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  624. (loop for c across ll
  625. when (not (string-match (regexp-quote (char-to-string c)) s))
  626. return (char-to-string c))))
  627. (defun org-e-latex--make-option-string (options)
  628. "Return a comma separated string of keywords and values.
  629. OPTIONS is an alist where the key is the options keyword as
  630. a string, and the value a list containing the keyword value, or
  631. nil."
  632. (mapconcat (lambda (pair)
  633. (concat (first pair)
  634. (when (> (length (second pair)) 0)
  635. (concat "=" (second pair)))))
  636. options
  637. ","))
  638. (defun org-e-latex--quotation-marks (text info)
  639. "Export quotation marks depending on language conventions.
  640. TEXT is a string containing quotation marks to be replaced. INFO
  641. is a plist used as a communication channel."
  642. (mapc (lambda(l)
  643. (let ((start 0))
  644. (while (setq start (string-match (car l) text start))
  645. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  646. (setq text (replace-match new-quote t t text))))))
  647. (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
  648. ;; Falls back on English.
  649. (assoc "en" org-e-latex-quotes))))
  650. text)
  651. (defun org-e-latex--wrap-label (element output)
  652. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  653. This function shouldn't be used for floats. See
  654. `org-e-latex--caption/label-string'."
  655. (let ((label (org-element-property :name element)))
  656. (if (or (not output) (not label) (string= output "") (string= label ""))
  657. output
  658. (concat (format "\\label{%s}\n" label) output))))
  659. (defun org-e-latex--text-markup (text markup)
  660. "Format text depending on MARKUP text markup.
  661. See `org-e-latex-text-markup-alist' for details."
  662. (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist))))
  663. (cond
  664. ;; No format string: Return raw text.
  665. ((not fmt) text)
  666. ;; Handle the `verb' special case: Find and appropriate separator
  667. ;; and use "\\verb" command.
  668. ((eq 'verb fmt)
  669. (let ((separator (org-e-latex--find-verb-separator text)))
  670. (concat "\\verb" separator text separator)))
  671. ;; Handle the `protectedtexttt' special case: Protect some
  672. ;; special chars and use "\texttt{%s}" format string.
  673. ((eq 'protectedtexttt fmt)
  674. (let ((start 0)
  675. (trans '(("\\" . "\\textbackslash{}")
  676. ("~" . "\\textasciitilde{}")
  677. ("^" . "\\textasciicircum{}")))
  678. (rtn "")
  679. char)
  680. (while (string-match "[\\{}$%&_#~^]" text)
  681. (setq char (match-string 0 text))
  682. (if (> (match-beginning 0) 0)
  683. (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
  684. (setq text (substring text (1+ (match-beginning 0))))
  685. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  686. rtn (concat rtn char)))
  687. (setq text (concat rtn text)
  688. fmt "\\texttt{%s}")
  689. (while (string-match "--" text)
  690. (setq text (replace-match "-{}-" t t text)))
  691. (format fmt text)))
  692. ;; Else use format string.
  693. (t (format fmt text)))))
  694. ;;; Template
  695. (defun org-e-latex-template (contents info)
  696. "Return complete document string after LaTeX conversion.
  697. CONTENTS is the transcoded contents string. INFO is a plist
  698. holding export options."
  699. (let ((title (org-export-data (plist-get info :title) info)))
  700. (concat
  701. ;; 1. Time-stamp.
  702. (and (plist-get info :time-stamp-file)
  703. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  704. ;; 2. Document class and packages.
  705. (let ((class (plist-get info :latex-class))
  706. (class-options (plist-get info :latex-class-options)))
  707. (org-element-normalize-string
  708. (let* ((header (nth 1 (assoc class org-e-latex-classes)))
  709. (document-class-string
  710. (and (stringp header)
  711. (if class-options
  712. (replace-regexp-in-string
  713. "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
  714. class-options header t nil 1)
  715. header))))
  716. (org-e-latex--guess-inputenc
  717. (org-splice-latex-header
  718. document-class-string
  719. org-export-latex-default-packages-alist ; defined in org.el
  720. org-export-latex-packages-alist nil ; defined in org.el
  721. (plist-get info :latex-header-extra))))))
  722. ;; 3. Define alert if not yet defined.
  723. "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
  724. ;; 4. Possibly limit depth for headline numbering.
  725. (let ((sec-num (plist-get info :section-numbers)))
  726. (when (integerp sec-num)
  727. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  728. ;; 5. Author.
  729. (let ((author (and (plist-get info :with-author)
  730. (let ((auth (plist-get info :author)))
  731. (and auth (org-export-data auth info)))))
  732. (email (and (plist-get info :with-email)
  733. (org-export-data (plist-get info :email) info))))
  734. (cond ((and author email (not (string= "" email)))
  735. (format "\\author{%s\\thanks{%s}}\n" author email))
  736. (author (format "\\author{%s}\n" author))
  737. (t "\\author{}\n")))
  738. ;; 6. Date.
  739. (let ((date (plist-get info :date)))
  740. (and date (format "\\date{%s}\n" date)))
  741. ;; 7. Title
  742. (format "\\title{%s}\n" title)
  743. ;; 8. Hyperref options.
  744. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  745. (or (plist-get info :keywords) "")
  746. (or (plist-get info :description) "")
  747. (if (not (plist-get info :with-creator)) ""
  748. (plist-get info :creator)))
  749. ;; 9. Document start.
  750. "\\begin{document}\n\n"
  751. ;; 10. Title command.
  752. (org-element-normalize-string
  753. (cond ((string= "" title) nil)
  754. ((not (stringp org-e-latex-title-command)) nil)
  755. ((string-match "\\(?:[^%]\\|^\\)%s"
  756. org-e-latex-title-command)
  757. (format org-e-latex-title-command title))
  758. (t org-e-latex-title-command)))
  759. ;; 11. Table of contents.
  760. (let ((depth (plist-get info :with-toc)))
  761. (when depth
  762. (concat (when (wholenump depth)
  763. (format "\\setcounter{tocdepth}{%d}\n" depth))
  764. "\\tableofcontents\n\\vspace*{1cm}\n\n")))
  765. ;; 12. Document's body.
  766. contents
  767. ;; 13. Creator.
  768. (let ((creator-info (plist-get info :with-creator)))
  769. (cond
  770. ((not creator-info) "")
  771. ((eq creator-info 'comment)
  772. (format "%% %s\n" (plist-get info :creator)))
  773. (t (concat (plist-get info :creator) "\n"))))
  774. ;; 14. Document end.
  775. "\\end{document}")))
  776. ;;; Transcode Functions
  777. ;;;; Babel Call
  778. ;;
  779. ;; Babel Calls are ignored.
  780. ;;;; Bold
  781. (defun org-e-latex-bold (bold contents info)
  782. "Transcode BOLD from Org to LaTeX.
  783. CONTENTS is the text with bold markup. INFO is a plist holding
  784. contextual information."
  785. (org-e-latex--text-markup contents 'bold))
  786. ;;;; Center Block
  787. (defun org-e-latex-center-block (center-block contents info)
  788. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  789. CONTENTS holds the contents of the center block. INFO is a plist
  790. holding contextual information."
  791. (org-e-latex--wrap-label
  792. center-block
  793. (format "\\begin{center}\n%s\\end{center}" contents)))
  794. ;;;; Clock
  795. (defun org-e-latex-clock (clock contents info)
  796. "Transcode a CLOCK element from Org to LaTeX.
  797. CONTENTS is nil. INFO is a plist holding contextual
  798. information."
  799. (concat
  800. "\\noindent"
  801. (format "\\textbf{%s} " org-clock-string)
  802. (format org-e-latex-inactive-timestamp-format
  803. (concat (org-translate-time (org-element-property :value clock))
  804. (let ((time (org-element-property :time clock)))
  805. (and time (format " (%s)" time)))))
  806. "\\\\"))
  807. ;;;; Code
  808. (defun org-e-latex-code (code contents info)
  809. "Transcode a CODE object from Org to LaTeX.
  810. CONTENTS is nil. INFO is a plist used as a communication
  811. channel."
  812. (org-e-latex--text-markup (org-element-property :value code) 'code))
  813. ;;;; Comment
  814. ;;
  815. ;; Comments are ignored.
  816. ;;;; Comment Block
  817. ;;
  818. ;; Comment Blocks are ignored.
  819. ;;;; Drawer
  820. (defun org-e-latex-drawer (drawer contents info)
  821. "Transcode a DRAWER element from Org to LaTeX.
  822. CONTENTS holds the contents of the block. INFO is a plist
  823. holding contextual information."
  824. (let* ((name (org-element-property :drawer-name drawer))
  825. (output (if (functionp org-e-latex-format-drawer-function)
  826. (funcall org-e-latex-format-drawer-function
  827. name contents)
  828. ;; If there's no user defined function: simply
  829. ;; display contents of the drawer.
  830. contents)))
  831. (org-e-latex--wrap-label drawer output)))
  832. ;;;; Dynamic Block
  833. (defun org-e-latex-dynamic-block (dynamic-block contents info)
  834. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  835. CONTENTS holds the contents of the block. INFO is a plist
  836. holding contextual information. See `org-export-data'."
  837. (org-e-latex--wrap-label dynamic-block contents))
  838. ;;;; Entity
  839. (defun org-e-latex-entity (entity contents info)
  840. "Transcode an ENTITY object from Org to LaTeX.
  841. CONTENTS are the definition itself. INFO is a plist holding
  842. contextual information."
  843. (let ((ent (org-element-property :latex entity)))
  844. (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
  845. ;;;; Example Block
  846. (defun org-e-latex-example-block (example-block contents info)
  847. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  848. CONTENTS is nil. INFO is a plist holding contextual
  849. information."
  850. (org-e-latex--wrap-label
  851. example-block
  852. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  853. (org-export-format-code-default example-block info))))
  854. ;;;; Export Snippet
  855. (defun org-e-latex-export-snippet (export-snippet contents info)
  856. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  857. CONTENTS is nil. INFO is a plist holding contextual information."
  858. (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
  859. (org-element-property :value export-snippet)))
  860. ;;;; Export Block
  861. (defun org-e-latex-export-block (export-block contents info)
  862. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  863. CONTENTS is nil. INFO is a plist holding contextual information."
  864. (when (string= (org-element-property :type export-block) "latex")
  865. (org-remove-indentation (org-element-property :value export-block))))
  866. ;;;; Fixed Width
  867. (defun org-e-latex-fixed-width (fixed-width contents info)
  868. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  869. CONTENTS is nil. INFO is a plist holding contextual information."
  870. (let* ((value (org-element-normalize-string
  871. (replace-regexp-in-string
  872. "^[ \t]*: ?" ""
  873. (org-element-property :value fixed-width)))))
  874. (org-e-latex--wrap-label
  875. fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  876. ;;;; Footnote Definition
  877. ;;
  878. ;; Footnote Definitions are ignored.
  879. ;;;; Footnote Reference
  880. (defun org-e-latex-footnote-reference (footnote-reference contents info)
  881. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  882. CONTENTS is nil. INFO is a plist holding contextual information."
  883. (concat
  884. ;; Insert separator between two footnotes in a row.
  885. (let ((prev (org-export-get-previous-element footnote-reference info)))
  886. (when (eq (org-element-type prev) 'footnote-reference)
  887. org-e-latex-footnote-separator))
  888. (cond
  889. ;; Use \footnotemark if the footnote has already been defined.
  890. ((not (org-export-footnote-first-reference-p footnote-reference info))
  891. (format "\\footnotemark[%s]{}"
  892. (org-export-get-footnote-number footnote-reference info)))
  893. ;; Use also \footnotemark if reference is within another footnote
  894. ;; reference or footnote definition.
  895. ((loop for parent in (org-export-get-genealogy footnote-reference info)
  896. thereis (memq (org-element-type parent)
  897. '(footnote-reference footnote-definition)))
  898. (let ((num (org-export-get-footnote-number footnote-reference info)))
  899. (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num)))
  900. ;; Otherwise, define it with \footnote command.
  901. (t
  902. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  903. (unless (eq (org-element-type def) 'org-data)
  904. (setq def (cons 'org-data (cons nil def))))
  905. (concat
  906. (format "\\footnote{%s}" (org-trim (org-export-data def info)))
  907. ;; Retrieve all footnote references within the footnote and
  908. ;; add their definition after it, since LaTeX doesn't support
  909. ;; them inside.
  910. (let* (all-refs
  911. search-refs ; for byte-compiler
  912. (search-refs
  913. (function
  914. (lambda (data)
  915. ;; Return a list of all footnote references in DATA.
  916. (org-element-map
  917. data 'footnote-reference
  918. (lambda (ref)
  919. (when (org-export-footnote-first-reference-p ref info)
  920. (push ref all-refs)
  921. (when (eq (org-element-property :type ref) 'standard)
  922. (funcall
  923. search-refs
  924. (org-export-get-footnote-definition ref info)))))
  925. info) (reverse all-refs)))))
  926. (mapconcat
  927. (lambda (ref)
  928. (format
  929. "\\footnotetext[%s]{%s}"
  930. (org-export-get-footnote-number ref info)
  931. (org-trim
  932. (org-export-data
  933. (org-export-get-footnote-definition ref info) info))))
  934. (funcall search-refs def) ""))))))))
  935. ;;;; Headline
  936. (defun org-e-latex-headline (headline contents info)
  937. "Transcode an HEADLINE element from Org to LaTeX.
  938. CONTENTS holds the contents of the headline. INFO is a plist
  939. holding contextual information."
  940. (let* ((class (plist-get info :latex-class))
  941. (level (org-export-get-relative-level headline info))
  942. (numberedp (org-export-numbered-headline-p headline info))
  943. (class-sectionning (assoc class org-e-latex-classes))
  944. ;; Section formatting will set two placeholders: one for the
  945. ;; title and the other for the contents.
  946. (section-fmt
  947. (let ((sec (if (and (symbolp (nth 2 class-sectionning))
  948. (fboundp (nth 2 class-sectionning)))
  949. (funcall (nth 2 class-sectionning) level numberedp)
  950. (nth (1+ level) class-sectionning))))
  951. (cond
  952. ;; No section available for that LEVEL.
  953. ((not sec) nil)
  954. ;; Section format directly returned by a function.
  955. ((stringp sec) sec)
  956. ;; (numbered-section . unnumbered-section)
  957. ((not (consp (cdr sec)))
  958. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  959. ;; (numbered-open numbered-close)
  960. ((= (length sec) 2)
  961. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  962. ;; (num-in num-out no-num-in no-num-out)
  963. ((= (length sec) 4)
  964. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  965. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  966. (text (org-export-data (org-element-property :title headline) info))
  967. (todo
  968. (and (plist-get info :with-todo-keywords)
  969. (let ((todo (org-element-property :todo-keyword headline)))
  970. (and todo (org-export-data todo info)))))
  971. (todo-type (and todo (org-element-property :todo-type headline)))
  972. (tags (and (plist-get info :with-tags)
  973. (org-element-property :tags headline)))
  974. (priority (and (plist-get info :with-priority)
  975. (org-element-property :priority headline)))
  976. ;; Create the headline text.
  977. (full-text (if (functionp org-e-latex-format-headline-function)
  978. ;; User-defined formatting function.
  979. (funcall org-e-latex-format-headline-function
  980. todo todo-type priority text tags)
  981. ;; Default formatting.
  982. (concat
  983. (when todo
  984. (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  985. (when priority (format "\\framebox{\\#%c} " priority))
  986. text
  987. (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
  988. ;; Associate some \label to the headline for internal links.
  989. (headline-label
  990. (format "\\label{sec-%s}\n"
  991. (mapconcat 'number-to-string
  992. (org-export-get-headline-number headline info)
  993. "-")))
  994. (pre-blanks
  995. (make-string (org-element-property :pre-blank headline) 10)))
  996. (cond
  997. ;; Case 1: This is a footnote section: ignore it.
  998. ((org-element-property :footnote-section-p headline) nil)
  999. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1000. ;; Also export as items headlines for which no section
  1001. ;; format has been found.
  1002. ((or (not section-fmt) (org-export-low-level-p headline info))
  1003. ;; Build the real contents of the sub-tree.
  1004. (let ((low-level-body
  1005. (concat
  1006. ;; If the headline is the first sibling, start a list.
  1007. (when (org-export-first-sibling-p headline info)
  1008. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1009. ;; Itemize headline
  1010. "\\item " full-text "\n" headline-label pre-blanks contents)))
  1011. ;; If headline is not the last sibling simply return
  1012. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  1013. ;; blank line.
  1014. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1015. (replace-regexp-in-string
  1016. "[ \t\n]*\\'"
  1017. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1018. low-level-body))))
  1019. ;; Case 3. Standard headline. Export it as a section.
  1020. (t (format section-fmt full-text
  1021. (concat headline-label pre-blanks contents))))))
  1022. ;;;; Horizontal Rule
  1023. (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
  1024. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1025. CONTENTS is nil. INFO is a plist holding contextual information."
  1026. (let ((attr (mapconcat #'identity
  1027. (org-element-property :attr_latex horizontal-rule)
  1028. " ")))
  1029. (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
  1030. ;;;; Inline Babel Call
  1031. ;;
  1032. ;; Inline Babel Calls are ignored.
  1033. ;;;; Inline Src Block
  1034. (defun org-e-latex-inline-src-block (inline-src-block contents info)
  1035. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1036. CONTENTS holds the contents of the item. INFO is a plist holding
  1037. contextual information."
  1038. (let* ((code (org-element-property :value inline-src-block))
  1039. (separator (org-e-latex--find-verb-separator code)))
  1040. (cond
  1041. ;; Do not use a special package: transcode it verbatim.
  1042. ((not org-e-latex-listings)
  1043. (concat "\\verb" separator code separator))
  1044. ;; Use minted package.
  1045. ((eq org-e-latex-listings 'minted)
  1046. (let* ((org-lang (org-element-property :language inline-src-block))
  1047. (mint-lang (or (cadr (assq (intern org-lang)
  1048. org-e-latex-minted-langs))
  1049. org-lang))
  1050. (options (org-e-latex--make-option-string
  1051. org-e-latex-minted-options)))
  1052. (concat (format "\\mint%s{%s}"
  1053. (if (string= options "") "" (format "[%s]" options))
  1054. mint-lang)
  1055. separator code separator)))
  1056. ;; Use listings package.
  1057. (t
  1058. ;; Maybe translate language's name.
  1059. (let* ((org-lang (org-element-property :language inline-src-block))
  1060. (lst-lang (or (cadr (assq (intern org-lang)
  1061. org-e-latex-listings-langs))
  1062. org-lang))
  1063. (options (org-e-latex--make-option-string
  1064. (append org-e-latex-listings-options
  1065. `(("language" ,lst-lang))))))
  1066. (concat (format "\\lstinline[%s]" options)
  1067. separator code separator))))))
  1068. ;;;; Inlinetask
  1069. (defun org-e-latex-inlinetask (inlinetask contents info)
  1070. "Transcode an INLINETASK element from Org to LaTeX.
  1071. CONTENTS holds the contents of the block. INFO is a plist
  1072. holding contextual information."
  1073. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1074. (todo (and (plist-get info :with-todo-keywords)
  1075. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1076. (and todo (org-export-data todo info)))))
  1077. (todo-type (org-element-property :todo-type inlinetask))
  1078. (tags (and (plist-get info :with-tags)
  1079. (org-element-property :tags inlinetask)))
  1080. (priority (and (plist-get info :with-priority)
  1081. (org-element-property :priority inlinetask))))
  1082. ;; If `org-e-latex-format-inlinetask-function' is provided, call it
  1083. ;; with appropriate arguments.
  1084. (if (functionp org-e-latex-format-inlinetask-function)
  1085. (funcall org-e-latex-format-inlinetask-function
  1086. todo todo-type priority title tags contents)
  1087. ;; Otherwise, use a default template.
  1088. (org-e-latex--wrap-label
  1089. inlinetask
  1090. (let ((full-title
  1091. (concat
  1092. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1093. (when priority (format "\\framebox{\\#%c} " priority))
  1094. title
  1095. (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
  1096. (format (concat "\\begin{center}\n"
  1097. "\\fbox{\n"
  1098. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1099. "%s\n\n"
  1100. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  1101. "%s"
  1102. "\\end{minipage}\n"
  1103. "}\n"
  1104. "\\end{center}")
  1105. full-title contents))))))
  1106. ;;;; Italic
  1107. (defun org-e-latex-italic (italic contents info)
  1108. "Transcode ITALIC from Org to LaTeX.
  1109. CONTENTS is the text with italic markup. INFO is a plist holding
  1110. contextual information."
  1111. (org-e-latex--text-markup contents 'italic))
  1112. ;;;; Item
  1113. (defun org-e-latex-item (item contents info)
  1114. "Transcode an ITEM element from Org to LaTeX.
  1115. CONTENTS holds the contents of the item. INFO is a plist holding
  1116. contextual information."
  1117. ;; Grab `:level' from plain-list properties, which is always the
  1118. ;; first element above current item.
  1119. (let* ((level (org-element-property :level (org-export-get-parent item info)))
  1120. (counter (let ((count (org-element-property :counter item)))
  1121. (and count
  1122. (< level 4)
  1123. (format "\\setcounter{enum%s}{%s}\n"
  1124. (nth level '("i" "ii" "iii" "iv"))
  1125. (1- count)))))
  1126. (checkbox (let ((checkbox (org-element-property :checkbox item)))
  1127. (cond ((eq checkbox 'on) "$\\boxtimes$ ")
  1128. ((eq checkbox 'off) "$\\Box$ ")
  1129. ((eq checkbox 'trans) "$\\boxminus$ "))))
  1130. (tag (let ((tag (org-element-property :tag item)))
  1131. (and tag (format "[%s]" (org-export-data tag info))))))
  1132. (concat counter "\\item" tag " " checkbox contents)))
  1133. ;;;; Keyword
  1134. (defun org-e-latex-keyword (keyword contents info)
  1135. "Transcode a KEYWORD element from Org to LaTeX.
  1136. CONTENTS is nil. INFO is a plist holding contextual information."
  1137. (let ((key (org-element-property :key keyword))
  1138. (value (org-element-property :value keyword)))
  1139. (cond
  1140. ((string= key "LATEX") value)
  1141. ((string= key "INDEX") (format "\\index{%s}" value))
  1142. ;; Invisible targets.
  1143. ((string= key "TARGET") nil)
  1144. ((string= key "TOC")
  1145. (let ((value (downcase value)))
  1146. (cond
  1147. ((string-match "\\<headlines\\>" value)
  1148. (let ((depth (or (and (string-match "[0-9]+" value)
  1149. (string-to-number (match-string 0 value)))
  1150. (plist-get info :with-toc))))
  1151. (concat
  1152. (when (wholenump depth)
  1153. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1154. "\\tableofcontents")))
  1155. ((string= "tables" value) "\\listoftables")
  1156. ((string= "figures" value) "\\listoffigures")
  1157. ((string= "listings" value)
  1158. (cond
  1159. ((eq org-e-latex-listings 'minted) "\\listoflistings")
  1160. (org-e-latex-listings "\\lstlistoflistings")
  1161. ;; At the moment, src blocks with a caption are wrapped
  1162. ;; into a figure environment.
  1163. (t "\\listoffigures")))))))))
  1164. ;;;; Latex Environment
  1165. (defun org-e-latex-latex-environment (latex-environment contents info)
  1166. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1167. CONTENTS is nil. INFO is a plist holding contextual information."
  1168. (let ((label (org-element-property :name latex-environment))
  1169. (value (org-remove-indentation
  1170. (org-element-property :value latex-environment))))
  1171. (if (not (org-string-nw-p label)) value
  1172. ;; Environment is labelled: label must be within the environment
  1173. ;; (otherwise, a reference pointing to that element will count
  1174. ;; the section instead).
  1175. (with-temp-buffer
  1176. (insert value)
  1177. (goto-char (point-min))
  1178. (forward-line)
  1179. (insert (format "\\label{%s}\n" label))
  1180. (buffer-string)))))
  1181. ;;;; Latex Fragment
  1182. (defun org-e-latex-latex-fragment (latex-fragment contents info)
  1183. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1184. CONTENTS is nil. INFO is a plist holding contextual information."
  1185. (org-element-property :value latex-fragment))
  1186. ;;;; Line Break
  1187. (defun org-e-latex-line-break (line-break contents info)
  1188. "Transcode a LINE-BREAK object from Org to LaTeX.
  1189. CONTENTS is nil. INFO is a plist holding contextual information."
  1190. "\\\\")
  1191. ;;;; Link
  1192. (defun org-e-latex-link--inline-image (link info)
  1193. "Return LaTeX code for an inline image.
  1194. LINK is the link pointing to the inline image. INFO is a plist
  1195. used as a communication channel."
  1196. (let* ((parent (org-export-get-parent-paragraph link info))
  1197. (path (let ((raw-path (org-element-property :path link)))
  1198. (if (not (file-name-absolute-p raw-path)) raw-path
  1199. (expand-file-name raw-path))))
  1200. (caption (org-e-latex--caption/label-string
  1201. (org-element-property :caption parent)
  1202. (org-element-property :name parent)
  1203. info))
  1204. ;; Retrieve latex attributes from the element around.
  1205. (attr (let ((raw-attr
  1206. (mapconcat #'identity
  1207. (org-element-property :attr_latex parent)
  1208. " ")))
  1209. (unless (string= raw-attr "") raw-attr)))
  1210. (disposition
  1211. (cond
  1212. ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
  1213. ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
  1214. ((or (and attr (string-match "\\<float\\>" attr))
  1215. (not (string= caption "")))
  1216. 'float)))
  1217. (placement
  1218. (cond
  1219. ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1220. (org-match-string-no-properties 1 attr))
  1221. ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
  1222. ((eq disposition 'float)
  1223. (concat "[" org-e-latex-default-figure-position "]"))
  1224. (t ""))))
  1225. ;; Now clear ATTR from any special keyword and set a default
  1226. ;; value if nothing is left.
  1227. (setq attr
  1228. (if (not attr) ""
  1229. (org-trim
  1230. (replace-regexp-in-string
  1231. "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
  1232. (setq attr (cond ((not (string= attr "")) attr)
  1233. ((eq disposition 'float) "width=0.7\\textwidth")
  1234. ((eq disposition 'wrap) "width=0.48\\textwidth")
  1235. (t (or org-e-latex-image-default-option ""))))
  1236. ;; Return proper string, depending on DISPOSITION.
  1237. (case disposition
  1238. (wrap (format "\\begin{wrapfigure}%s
  1239. \\centering
  1240. \\includegraphics[%s]{%s}
  1241. %s\\end{wrapfigure}" placement attr path caption))
  1242. (mulicolumn (format "\\begin{figure*}%s
  1243. \\centering
  1244. \\includegraphics[%s]{%s}
  1245. %s\\end{figure*}" placement attr path caption))
  1246. (float (format "\\begin{figure}%s
  1247. \\centering
  1248. \\includegraphics[%s]{%s}
  1249. %s\\end{figure}" placement attr path caption))
  1250. (t (format "\\includegraphics[%s]{%s}" attr path)))))
  1251. (defun org-e-latex-link (link desc info)
  1252. "Transcode a LINK object from Org to LaTeX.
  1253. DESC is the description part of the link, or the empty string.
  1254. INFO is a plist holding contextual information. See
  1255. `org-export-data'."
  1256. (let* ((type (org-element-property :type link))
  1257. (raw-path (org-element-property :path link))
  1258. ;; Ensure DESC really exists, or set it to nil.
  1259. (desc (and (not (string= desc "")) desc))
  1260. (imagep (org-export-inline-image-p
  1261. link org-e-latex-inline-image-rules))
  1262. (path (cond
  1263. ((member type '("http" "https" "ftp" "mailto"))
  1264. (concat type ":" raw-path))
  1265. ((string= type "file")
  1266. (when (string-match "\\(.+\\)::.+" raw-path)
  1267. (setq raw-path (match-string 1 raw-path)))
  1268. (if (file-name-absolute-p raw-path)
  1269. (concat "file://" (expand-file-name raw-path))
  1270. ;; TODO: Not implemented yet. Concat also:
  1271. ;; (org-export-directory :LaTeX info)
  1272. (concat "file://" raw-path)))
  1273. (t raw-path)))
  1274. protocol)
  1275. (cond
  1276. ;; Image file.
  1277. (imagep (org-e-latex-link--inline-image link info))
  1278. ;; Radioed target: Target's name is obtained from original raw
  1279. ;; link. Path is parsed and transcoded in order to have a proper
  1280. ;; display of the contents.
  1281. ((string= type "radio")
  1282. (format "\\hyperref[%s]{%s}"
  1283. (org-export-solidify-link-text path)
  1284. (org-export-data
  1285. (org-element-parse-secondary-string
  1286. path (cdr (assq 'radio-target org-element-object-restrictions)))
  1287. info)))
  1288. ;; Links pointing to an headline: Find destination and build
  1289. ;; appropriate referencing command.
  1290. ((member type '("custom-id" "fuzzy" "id"))
  1291. (let ((destination (if (string= type "fuzzy")
  1292. (org-export-resolve-fuzzy-link link info)
  1293. (org-export-resolve-id-link link info))))
  1294. (case (org-element-type destination)
  1295. ;; Fuzzy link points nowhere.
  1296. ('nil
  1297. (format "\\texttt{%s}"
  1298. (or desc
  1299. (org-export-data
  1300. (org-element-property :raw-link link) info))))
  1301. ;; Fuzzy link points to an invisible target.
  1302. (keyword nil)
  1303. ;; LINK points to an headline. If headlines are numbered
  1304. ;; and the link has no description, display headline's
  1305. ;; number. Otherwise, display description or headline's
  1306. ;; title.
  1307. (headline
  1308. (let ((label
  1309. (format "sec-%s"
  1310. (mapconcat
  1311. 'number-to-string
  1312. (org-export-get-headline-number destination info)
  1313. "-"))))
  1314. (if (and (plist-get info :section-numbers) (not desc))
  1315. (format "\\ref{%s}" label)
  1316. (format "\\hyperref[%s]{%s}" label
  1317. (or desc
  1318. (org-export-data
  1319. (org-element-property :title destination) info))))))
  1320. ;; Fuzzy link points to a target. Do as above.
  1321. (otherwise
  1322. (let ((path (org-export-solidify-link-text path)))
  1323. (if (not desc) (format "\\ref{%s}" path)
  1324. (format "\\hyperref[%s]{%s}" path desc)))))))
  1325. ;; Coderef: replace link with the reference name or the
  1326. ;; equivalent line number.
  1327. ((string= type "coderef")
  1328. (format (org-export-get-coderef-format path desc)
  1329. (org-export-resolve-coderef path info)))
  1330. ;; Link type is handled by a special function.
  1331. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1332. (funcall protocol (org-link-unescape path) desc 'latex))
  1333. ;; External link with a description part.
  1334. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1335. ;; External link without a description part.
  1336. (path (format "\\url{%s}" path))
  1337. ;; No path, only description. Try to do something useful.
  1338. (t (format "\\texttt{%s}" desc)))))
  1339. ;;;; Macro
  1340. (defun org-e-latex-macro (macro contents info)
  1341. "Transcode a MACRO element from Org to LaTeX.
  1342. CONTENTS is nil. INFO is a plist holding contextual information."
  1343. ;; Use available tools.
  1344. (org-export-expand-macro macro info))
  1345. ;;;; Paragraph
  1346. (defun org-e-latex-paragraph (paragraph contents info)
  1347. "Transcode a PARAGRAPH element from Org to LaTeX.
  1348. CONTENTS is the contents of the paragraph, as a string. INFO is
  1349. the plist used as a communication channel."
  1350. contents)
  1351. ;;;; Plain List
  1352. (defun org-e-latex-plain-list (plain-list contents info)
  1353. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1354. CONTENTS is the contents of the list. INFO is a plist holding
  1355. contextual information."
  1356. (let* ((type (org-element-property :type plain-list))
  1357. (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
  1358. "inparadesc" "asparadesc"))
  1359. (paralist-regexp (concat
  1360. "\\("
  1361. (mapconcat 'identity paralist-types "\\|")
  1362. "\\)"))
  1363. (attr (mapconcat #'identity
  1364. (org-element-property :attr_latex plain-list)
  1365. " "))
  1366. (latex-type (cond
  1367. ((and attr
  1368. (string-match
  1369. (format "\\<%s\\>" paralist-regexp) attr))
  1370. (match-string 1 attr))
  1371. ((eq type 'ordered) "enumerate")
  1372. ((eq type 'unordered) "itemize")
  1373. ((eq type 'descriptive) "description"))))
  1374. (org-e-latex--wrap-label
  1375. plain-list
  1376. (format "\\begin{%s}%s\n%s\\end{%s}"
  1377. latex-type
  1378. ;; Once special environment, if any, has been removed, the
  1379. ;; rest of the attributes will be optional arguments.
  1380. ;; They will be put inside square brackets if necessary.
  1381. (let ((opt (replace-regexp-in-string
  1382. (format " *%s *" paralist-regexp) "" attr)))
  1383. (cond ((string= opt "") "")
  1384. ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
  1385. (t (format "[%s]" opt))))
  1386. contents
  1387. latex-type))))
  1388. ;;;; Plain Text
  1389. (defun org-e-latex-plain-text (text info)
  1390. "Transcode a TEXT string from Org to LaTeX.
  1391. TEXT is the string to transcode. INFO is a plist holding
  1392. contextual information."
  1393. ;; Protect %, #, &, $, ~, ^, _, { and }.
  1394. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
  1395. (setq text
  1396. (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
  1397. ;; Protect \
  1398. (setq text (replace-regexp-in-string
  1399. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  1400. "$\\backslash$" text nil t 1))
  1401. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1402. (let ((case-fold-search nil)
  1403. (start 0))
  1404. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
  1405. (setq text (replace-match
  1406. (format "\\%s{}" (match-string 1 text)) nil t text)
  1407. start (match-end 0))))
  1408. ;; Handle quotation marks
  1409. (setq text (org-e-latex--quotation-marks text info))
  1410. ;; Convert special strings.
  1411. (when (plist-get info :with-special-strings)
  1412. (while (string-match (regexp-quote "...") text)
  1413. (setq text (replace-match "\\ldots{}" nil t text))))
  1414. ;; Handle break preservation if required.
  1415. (when (plist-get info :preserve-breaks)
  1416. (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1417. text)))
  1418. ;; Return value.
  1419. text)
  1420. ;;;; Planning
  1421. (defun org-e-latex-planning (planning contents info)
  1422. "Transcode a PLANNING element from Org to LaTeX.
  1423. CONTENTS is nil. INFO is a plist holding contextual
  1424. information."
  1425. (concat
  1426. "\\noindent"
  1427. (mapconcat
  1428. 'identity
  1429. (delq nil
  1430. (list
  1431. (let ((closed (org-element-property :closed planning)))
  1432. (when closed
  1433. (concat
  1434. (format "\\textbf{%s} " org-closed-string)
  1435. (format org-e-latex-inactive-timestamp-format
  1436. (org-translate-time closed)))))
  1437. (let ((deadline (org-element-property :deadline planning)))
  1438. (when deadline
  1439. (concat
  1440. (format "\\textbf{%s} " org-deadline-string)
  1441. (format org-e-latex-active-timestamp-format
  1442. (org-translate-time deadline)))))
  1443. (let ((scheduled (org-element-property :scheduled planning)))
  1444. (when scheduled
  1445. (concat
  1446. (format "\\textbf{%s} " org-scheduled-string)
  1447. (format org-e-latex-active-timestamp-format
  1448. (org-translate-time scheduled)))))))
  1449. " ")
  1450. "\\\\"))
  1451. ;;;; Property Drawer
  1452. (defun org-e-latex-property-drawer (property-drawer contents info)
  1453. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  1454. CONTENTS is nil. INFO is a plist holding contextual
  1455. information."
  1456. ;; The property drawer isn't exported but we want separating blank
  1457. ;; lines nonetheless.
  1458. "")
  1459. ;;;; Quote Block
  1460. (defun org-e-latex-quote-block (quote-block contents info)
  1461. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1462. CONTENTS holds the contents of the block. INFO is a plist
  1463. holding contextual information."
  1464. (org-e-latex--wrap-label
  1465. quote-block
  1466. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1467. ;;;; Quote Section
  1468. (defun org-e-latex-quote-section (quote-section contents info)
  1469. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1470. CONTENTS is nil. INFO is a plist holding contextual information."
  1471. (let ((value (org-remove-indentation
  1472. (org-element-property :value quote-section))))
  1473. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1474. ;;;; Section
  1475. (defun org-e-latex-section (section contents info)
  1476. "Transcode a SECTION element from Org to LaTeX.
  1477. CONTENTS holds the contents of the section. INFO is a plist
  1478. holding contextual information."
  1479. contents)
  1480. ;;;; Radio Target
  1481. (defun org-e-latex-radio-target (radio-target text info)
  1482. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1483. TEXT is the text of the target. INFO is a plist holding
  1484. contextual information."
  1485. (format "\\label{%s}%s"
  1486. (org-export-solidify-link-text
  1487. (org-element-property :value radio-target))
  1488. text))
  1489. ;;;; Special Block
  1490. (defun org-e-latex-special-block (special-block contents info)
  1491. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1492. CONTENTS holds the contents of the block. INFO is a plist
  1493. holding contextual information."
  1494. (let ((type (downcase (org-element-property :type special-block))))
  1495. (org-e-latex--wrap-label
  1496. special-block
  1497. (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
  1498. ;;;; Src Block
  1499. (defun org-e-latex-src-block (src-block contents info)
  1500. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1501. CONTENTS holds the contents of the item. INFO is a plist holding
  1502. contextual information."
  1503. (let* ((lang (org-element-property :language src-block))
  1504. (caption (org-element-property :caption src-block))
  1505. (label (org-element-property :name src-block))
  1506. (custom-env (and lang
  1507. (cadr (assq (intern lang)
  1508. org-e-latex-custom-lang-environments))))
  1509. (num-start (case (org-element-property :number-lines src-block)
  1510. (continued (org-export-get-loc src-block info))
  1511. (new 0)))
  1512. (retain-labels (org-element-property :retain-labels src-block)))
  1513. (cond
  1514. ;; Case 1. No source fontification.
  1515. ((not org-e-latex-listings)
  1516. (let ((caption-str (org-e-latex--caption/label-string caption label info))
  1517. (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
  1518. (format
  1519. (or float-env "%s")
  1520. (concat caption-str
  1521. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1522. (org-export-format-code-default src-block info))))))
  1523. ;; Case 2. Custom environment.
  1524. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1525. custom-env
  1526. (org-export-format-code-default src-block info)
  1527. custom-env))
  1528. ;; Case 3. Use minted package.
  1529. ((eq org-e-latex-listings 'minted)
  1530. (let ((float-env (when (or label caption)
  1531. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1532. (org-e-latex--caption/label-string
  1533. caption label info))))
  1534. (body
  1535. (format
  1536. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1537. ;; Options.
  1538. (org-e-latex--make-option-string
  1539. (if (not num-start) org-e-latex-minted-options
  1540. (append `(("linenos")
  1541. ("firstnumber" ,(number-to-string (1+ num-start))))
  1542. org-e-latex-minted-options)))
  1543. ;; Language.
  1544. (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
  1545. ;; Source code.
  1546. (let* ((code-info (org-export-unravel-code src-block))
  1547. (max-width
  1548. (apply 'max
  1549. (mapcar 'length
  1550. (org-split-string (car code-info) "\n")))))
  1551. (org-export-format-code
  1552. (car code-info)
  1553. (lambda (loc num ref)
  1554. (concat
  1555. loc
  1556. (when ref
  1557. ;; Ensure references are flushed to the right,
  1558. ;; separated with 6 spaces from the widest line
  1559. ;; of code.
  1560. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1561. (format "(%s)" ref)))))
  1562. nil (and retain-labels (cdr code-info)))))))
  1563. ;; Return value.
  1564. (if float-env (format float-env body) body)))
  1565. ;; Case 4. Use listings package.
  1566. (t
  1567. (let ((lst-lang
  1568. (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
  1569. (caption-str
  1570. (when caption
  1571. (let ((main (org-export-data (car caption) info)))
  1572. (if (not (cdr caption)) (format "{%s}" main)
  1573. (format "{[%s]%s}"
  1574. (org-export-data (cdr caption) info)
  1575. main))))))
  1576. (concat
  1577. ;; Options.
  1578. (format "\\lstset{%s}\n"
  1579. (org-e-latex--make-option-string
  1580. (append org-e-latex-listings-options
  1581. `(("language" ,lst-lang))
  1582. (when label `(("label" ,label)))
  1583. (when caption-str `(("caption" ,caption-str)))
  1584. (cond ((not num-start) '(("numbers" "none")))
  1585. ((zerop num-start) '(("numbers" "left")))
  1586. (t `(("numbers" "left")
  1587. ("firstnumber"
  1588. ,(number-to-string (1+ num-start)))))))))
  1589. ;; Source code.
  1590. (format
  1591. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1592. (let* ((code-info (org-export-unravel-code src-block))
  1593. (max-width
  1594. (apply 'max
  1595. (mapcar 'length
  1596. (org-split-string (car code-info) "\n")))))
  1597. (org-export-format-code
  1598. (car code-info)
  1599. (lambda (loc num ref)
  1600. (concat
  1601. loc
  1602. (when ref
  1603. ;; Ensure references are flushed to the right,
  1604. ;; separated with 6 spaces from the widest line of
  1605. ;; code
  1606. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1607. (format "(%s)" ref)))))
  1608. nil (and retain-labels (cdr code-info)))))))))))
  1609. ;;;; Statistics Cookie
  1610. (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
  1611. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1612. CONTENTS is nil. INFO is a plist holding contextual information."
  1613. (org-element-property :value statistics-cookie))
  1614. ;;;; Strike-Through
  1615. (defun org-e-latex-strike-through (strike-through contents info)
  1616. "Transcode STRIKE-THROUGH from Org to LaTeX.
  1617. CONTENTS is the text with strike-through markup. INFO is a plist
  1618. holding contextual information."
  1619. (org-e-latex--text-markup contents 'strike-through))
  1620. ;;;; Subscript
  1621. (defun org-e-latex-subscript (subscript contents info)
  1622. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1623. CONTENTS is the contents of the object. INFO is a plist holding
  1624. contextual information."
  1625. (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
  1626. ;;;; Superscript
  1627. (defun org-e-latex-superscript (superscript contents info)
  1628. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1629. CONTENTS is the contents of the object. INFO is a plist holding
  1630. contextual information."
  1631. (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
  1632. ;;;; Table
  1633. (defun org-e-latex-table--format-string (table info)
  1634. "Return an appropriate format string for TABLE.
  1635. TABLE-INFO is the plist containing format info about the table,
  1636. as returned by `org-export-table-format-info'. INFO is a plist
  1637. used as a communication channel.
  1638. The format string leaves one placeholder for the body of the
  1639. table."
  1640. (let* ((label (org-element-property :name table))
  1641. (caption (org-e-latex--caption/label-string
  1642. (org-element-property :caption table) label info))
  1643. (attr (mapconcat 'identity
  1644. (org-element-property :attr_latex table)
  1645. " "))
  1646. ;; Determine alignment string.
  1647. (alignment (org-e-latex-table--align-string table info))
  1648. ;; Determine environment for the table: longtable, tabular...
  1649. (table-env (cond
  1650. ((not attr) org-e-latex-default-table-environment)
  1651. ((string-match "\\<longtable\\>" attr) "longtable")
  1652. ((string-match "\\<tabular.?\\>" attr)
  1653. (org-match-string-no-properties 0 attr))
  1654. (t org-e-latex-default-table-environment)))
  1655. ;; If table is a float, determine environment: table, table*
  1656. ;; or sidewaystable.
  1657. (float-env (cond
  1658. ((string= "longtable" table-env) nil)
  1659. ((and attr (string-match "\\<sidewaystable\\>" attr))
  1660. "sidewaystables")
  1661. ((and attr
  1662. (or (string-match (regexp-quote "table*") attr)
  1663. (string-match "\\<multicolumn\\>" attr)))
  1664. "table*")
  1665. ((or (not (string= caption "")) label) "table")))
  1666. ;; Extract others display options.
  1667. (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
  1668. (org-match-string-no-properties 1 attr)))
  1669. (placement
  1670. (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1671. (org-match-string-no-properties 1 attr)
  1672. (format "[%s]" org-e-latex-default-figure-position))))
  1673. ;; Prepare the final format string for the table.
  1674. (cond
  1675. ;; Longtable.
  1676. ((string= "longtable" table-env)
  1677. (format
  1678. "\\begin{longtable}{%s}\n%s%%s%s\\end{longtable}"
  1679. alignment
  1680. (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
  1681. (concat (org-trim caption) "\\\\\n"))
  1682. (if (or org-e-latex-table-caption-above (string= "" caption)) ""
  1683. (concat (org-trim caption) "\\\\\n"))))
  1684. ;; Others.
  1685. (t (concat (when float-env
  1686. (concat
  1687. (format "\\begin{%s}%s\n" float-env placement)
  1688. (if org-e-latex-table-caption-above caption "")))
  1689. (when org-e-latex-tables-centered "\\begin{center}\n")
  1690. (format "\\begin{%s}%s{%s}\n%%s\\end{%s}"
  1691. table-env
  1692. (if width (format "{%s}" width) "") alignment table-env)
  1693. (when org-e-latex-tables-centered "\n\\end{center}")
  1694. (when float-env
  1695. (concat (if org-e-latex-table-caption-above "" caption)
  1696. (format "\n\\end{%s}" float-env))))))))
  1697. (defun org-e-latex-table--align-string (table info)
  1698. "Return an appropriate LaTeX alignment string.
  1699. TABLE is the considered table. INFO is a plist used as
  1700. a communication channel."
  1701. (let ((attr (mapconcat 'identity
  1702. (org-element-property :attr_latex table)
  1703. " ")))
  1704. (if (and attr (string-match "\\<align=\\(\\S-+\\)" attr))
  1705. (match-string 1 attr)
  1706. (let (alignment)
  1707. ;; Extract column groups and alignment from first (non-rule)
  1708. ;; row.
  1709. (org-element-map
  1710. (org-element-map table 'table-row 'identity info 'first-match)
  1711. 'table-cell
  1712. (lambda (cell)
  1713. (let ((borders (org-export-table-cell-borders cell info)))
  1714. ;; Check left border for the first cell only.
  1715. (when (and (memq 'left borders) (not alignment))
  1716. (push "|" alignment))
  1717. (push (case (org-export-table-cell-alignment cell info)
  1718. (left "l")
  1719. (right "r")
  1720. (center "c"))
  1721. alignment)
  1722. (when (memq 'right borders) (push "|" alignment))))
  1723. info)
  1724. (apply 'concat (reverse alignment))))))
  1725. (defun org-e-latex-table (table contents info)
  1726. "Transcode a TABLE element from Org to LaTeX.
  1727. CONTENTS is nil. INFO is a plist holding contextual information."
  1728. (cond
  1729. ;; Case 1: verbatim table.
  1730. ((or org-e-latex-tables-verbatim
  1731. (let ((attr (mapconcat 'identity
  1732. (org-element-property :attr_latex table)
  1733. " ")))
  1734. (and attr (string-match "\\<verbatim\\>" attr))))
  1735. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1736. ;; Re-create table, without affiliated keywords.
  1737. (org-trim
  1738. (org-element-interpret-data
  1739. `(org-data nil (table nil ,@(org-element-contents table)))))))
  1740. ;; Case 2: table.el table. Convert it using appropriate tools.
  1741. ((eq (org-element-property :type table) 'table.el)
  1742. (require 'table)
  1743. ;; Ensure "*org-export-table*" buffer is empty.
  1744. (with-current-buffer (get-buffer-create "*org-export-table*")
  1745. (erase-buffer))
  1746. (let ((output (with-temp-buffer
  1747. (insert (org-element-property :value table))
  1748. (goto-char 1)
  1749. (re-search-forward "^[ \t]*|[^|]" nil t)
  1750. (table-generate-source 'latex "*org-export-table*")
  1751. (with-current-buffer "*org-export-table*"
  1752. (org-trim (buffer-string))))))
  1753. (kill-buffer (get-buffer "*org-export-table*"))
  1754. ;; Remove left out comments.
  1755. (while (string-match "^%.*\n" output)
  1756. (setq output (replace-match "" t t output)))
  1757. ;; When the "rmlines" attribute is provided, remove all hlines
  1758. ;; but the the one separating heading from the table body.
  1759. (let ((attr (mapconcat 'identity
  1760. (org-element-property :attr_latex table)
  1761. " ")))
  1762. (when (and attr (string-match "\\<rmlines\\>" attr))
  1763. (let ((n 0) (pos 0))
  1764. (while (and (< (length output) pos)
  1765. (setq pos (string-match "^\\\\hline\n?" output pos)))
  1766. (incf n)
  1767. (unless (= n 2)
  1768. (setq output (replace-match "" nil nil output)))))))
  1769. (if (not org-e-latex-tables-centered) output
  1770. (format "\\begin{center}\n%s\n\\end{center}" output))))
  1771. ;; Case 3: Standard table.
  1772. (t (format (org-e-latex-table--format-string table info) contents))))
  1773. ;;;; Table Cell
  1774. (defun org-e-latex-table-cell (table-cell contents info)
  1775. "Transcode a TABLE-CELL element from Org to LaTeX.
  1776. CONTENTS is the cell contents. INFO is a plist used as
  1777. a communication channel."
  1778. (concat (if (and contents
  1779. org-e-latex-table-scientific-notation
  1780. (string-match orgtbl-exp-regexp contents))
  1781. ;; Use appropriate format string for scientific
  1782. ;; notation.
  1783. (format org-e-latex-table-scientific-notation
  1784. (match-string 1 contents)
  1785. (match-string 2 contents))
  1786. contents)
  1787. (when (org-export-get-next-element table-cell info) " & ")))
  1788. ;;;; Table Row
  1789. (defun org-e-latex-table-row (table-row contents info)
  1790. "Transcode a TABLE-ROW element from Org to LaTeX.
  1791. CONTENTS is the contents of the row. INFO is a plist used as
  1792. a communication channel."
  1793. ;; Rules are ignored since table separators are deduced from
  1794. ;; borders of the current row.
  1795. (when (eq (org-element-property :type table-row) 'standard)
  1796. (let* ((attr (mapconcat 'identity
  1797. (org-element-property
  1798. :attr_latex (org-export-get-parent table-row info))
  1799. " "))
  1800. (longtablep (and attr (string-match "\\<longtable\\>" attr)))
  1801. (booktabsp
  1802. (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
  1803. org-e-latex-tables-booktabs))
  1804. ;; TABLE-ROW's borders are extracted from its first cell.
  1805. (borders
  1806. (org-export-table-cell-borders
  1807. (car (org-element-contents table-row)) info)))
  1808. (concat
  1809. ;; When BOOKTABS are activated enforce top-rule even when no
  1810. ;; hline was specifically marked.
  1811. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  1812. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  1813. contents "\\\\\n"
  1814. (cond
  1815. ;; Special case for long tables. Define header and footers.
  1816. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  1817. (format "%s
  1818. \\endhead
  1819. %s\\multicolumn{%d}{r}{Continued on next page} \\\\
  1820. \\endfoot
  1821. \\endlastfoot"
  1822. (if booktabsp "\\midrule" "\\hline")
  1823. (if booktabsp "\\midrule" "\\hline")
  1824. ;; Number of columns.
  1825. (cdr (org-export-table-dimensions
  1826. (org-export-get-parent-table table-row info) info))))
  1827. ;; When BOOKTABS are activated enforce bottom rule even when
  1828. ;; no hline was specifically marked.
  1829. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  1830. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  1831. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  1832. ;;;; Target
  1833. (defun org-e-latex-target (target contents info)
  1834. "Transcode a TARGET object from Org to LaTeX.
  1835. CONTENTS is nil. INFO is a plist holding contextual
  1836. information."
  1837. (format "\\label{%s}"
  1838. (org-export-solidify-link-text (org-element-property :value target))))
  1839. ;;;; Timestamp
  1840. (defun org-e-latex-timestamp (timestamp contents info)
  1841. "Transcode a TIMESTAMP object from Org to LaTeX.
  1842. CONTENTS is nil. INFO is a plist holding contextual
  1843. information."
  1844. (let ((value (org-translate-time (org-element-property :value timestamp)))
  1845. (type (org-element-property :type timestamp)))
  1846. (cond ((memq type '(active active-range))
  1847. (format org-e-latex-active-timestamp-format value))
  1848. ((memq type '(inactive inactive-range))
  1849. (format org-e-latex-inactive-timestamp-format value))
  1850. (t (format org-e-latex-diary-timestamp-format value)))))
  1851. ;;;; Underline
  1852. (defun org-e-latex-underline (underline contents info)
  1853. "Transcode UNDERLINE from Org to LaTeX.
  1854. CONTENTS is the text with underline markup. INFO is a plist
  1855. holding contextual information."
  1856. (org-e-latex--text-markup contents 'underline))
  1857. ;;;; Verbatim
  1858. (defun org-e-latex-verbatim (verbatim contents info)
  1859. "Transcode a VERBATIM object from Org to LaTeX.
  1860. CONTENTS is nil. INFO is a plist used as a communication
  1861. channel."
  1862. (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  1863. ;;;; Verse Block
  1864. (defun org-e-latex-verse-block (verse-block contents info)
  1865. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  1866. CONTENTS is verse block contents. INFO is a plist holding
  1867. contextual information."
  1868. (org-e-latex--wrap-label
  1869. verse-block
  1870. ;; In a verse environment, add a line break to each newline
  1871. ;; character and change each white space at beginning of a line
  1872. ;; into a space of 1 em. Also change each blank line with
  1873. ;; a vertical space of 1 em.
  1874. (progn
  1875. (setq contents (replace-regexp-in-string
  1876. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  1877. (replace-regexp-in-string
  1878. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  1879. (while (string-match "^[ \t]+" contents)
  1880. (let ((new-str (format "\\hspace*{%dem}"
  1881. (length (match-string 0 contents)))))
  1882. (setq contents (replace-match new-str nil t contents))))
  1883. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  1884. ;;; Interactive functions
  1885. (defun org-e-latex-export-to-latex
  1886. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1887. "Export current buffer to a LaTeX file.
  1888. If narrowing is active in the current buffer, only export its
  1889. narrowed part.
  1890. If a region is active, export that region.
  1891. When optional argument SUBTREEP is non-nil, export the sub-tree
  1892. at point, extracting information from the headline properties
  1893. first.
  1894. When optional argument VISIBLE-ONLY is non-nil, don't export
  1895. contents of hidden elements.
  1896. When optional argument BODY-ONLY is non-nil, only write code
  1897. between \"\\begin{document}\" and \"\\end{document}\".
  1898. EXT-PLIST, when provided, is a property list with external
  1899. parameters overriding Org default settings, but still inferior to
  1900. file-local settings.
  1901. When optional argument PUB-DIR is set, use it as the publishing
  1902. directory.
  1903. Return output file's name."
  1904. (interactive)
  1905. (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
  1906. (org-export-to-file
  1907. 'e-latex outfile subtreep visible-only body-only ext-plist)))
  1908. (defun org-e-latex-export-to-pdf
  1909. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1910. "Export current buffer to LaTeX then process through to PDF.
  1911. If narrowing is active in the current buffer, only export its
  1912. narrowed part.
  1913. If a region is active, export that region.
  1914. When optional argument SUBTREEP is non-nil, export the sub-tree
  1915. at point, extracting information from the headline properties
  1916. first.
  1917. When optional argument VISIBLE-ONLY is non-nil, don't export
  1918. contents of hidden elements.
  1919. When optional argument BODY-ONLY is non-nil, only write code
  1920. between \"\\begin{document}\" and \"\\end{document}\".
  1921. EXT-PLIST, when provided, is a property list with external
  1922. parameters overriding Org default settings, but still inferior to
  1923. file-local settings.
  1924. When optional argument PUB-DIR is set, use it as the publishing
  1925. directory.
  1926. Return PDF file's name."
  1927. (interactive)
  1928. (org-e-latex-compile
  1929. (org-e-latex-export-to-latex
  1930. subtreep visible-only body-only ext-plist pub-dir)))
  1931. (defun org-e-latex-compile (texfile)
  1932. "Compile a TeX file.
  1933. TEXFILE is the name of the file being compiled. Processing is
  1934. done through the command specified in `org-e-latex-pdf-process'.
  1935. Return PDF file name or an error if it couldn't be produced."
  1936. (let* ((wconfig (current-window-configuration))
  1937. (texfile (file-truename texfile))
  1938. (base (file-name-sans-extension texfile))
  1939. errors)
  1940. (message (format "Processing LaTeX file %s ..." texfile))
  1941. (unwind-protect
  1942. (progn
  1943. (cond
  1944. ;; A function is provided: Apply it.
  1945. ((functionp org-e-latex-pdf-process)
  1946. (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
  1947. ;; A list is provided: Replace %b, %f and %o with appropriate
  1948. ;; values in each command before applying it. Output is
  1949. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  1950. ((consp org-e-latex-pdf-process)
  1951. (let* ((out-dir (or (file-name-directory texfile) "./"))
  1952. (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
  1953. (mapc
  1954. (lambda (command)
  1955. (shell-command
  1956. (replace-regexp-in-string
  1957. "%b" (shell-quote-argument base)
  1958. (replace-regexp-in-string
  1959. "%f" (shell-quote-argument texfile)
  1960. (replace-regexp-in-string
  1961. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  1962. outbuf))
  1963. org-e-latex-pdf-process)
  1964. ;; Collect standard errors from output buffer.
  1965. (setq errors (org-e-latex-collect-errors outbuf))))
  1966. (t (error "No valid command to process to PDF")))
  1967. (let ((pdffile (concat base ".pdf")))
  1968. ;; Check for process failure. Provide collected errors if
  1969. ;; possible.
  1970. (if (not (file-exists-p pdffile))
  1971. (error (concat (format "PDF file %s wasn't produced" pdffile)
  1972. (when errors (concat ": " errors))))
  1973. ;; Else remove log files, when specified, and signal end of
  1974. ;; process to user, along with any error encountered.
  1975. (when org-e-latex-remove-logfiles
  1976. (dolist (ext org-e-latex-logfiles-extensions)
  1977. (let ((file (concat base "." ext)))
  1978. (when (file-exists-p file) (delete-file file)))))
  1979. (message (concat "Process completed"
  1980. (if (not errors) "."
  1981. (concat " with errors: " errors)))))
  1982. ;; Return output file name.
  1983. pdffile))
  1984. (set-window-configuration wconfig))))
  1985. (defun org-e-latex-collect-errors (buffer)
  1986. "Collect some kind of errors from \"pdflatex\" command output.
  1987. BUFFER is the buffer containing output.
  1988. Return collected error types as a string, or nil if there was
  1989. none."
  1990. (with-current-buffer buffer
  1991. (save-excursion
  1992. (goto-char (point-max))
  1993. ;; Find final "pdflatex" run.
  1994. (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
  1995. (let ((case-fold-search t)
  1996. (errors ""))
  1997. (when (save-excursion
  1998. (re-search-forward "Reference.*?undefined" nil t))
  1999. (setq errors (concat errors " [undefined reference]")))
  2000. (when (save-excursion
  2001. (re-search-forward "Citation.*?undefined" nil t))
  2002. (setq errors (concat errors " [undefined citation]")))
  2003. (when (save-excursion
  2004. (re-search-forward "Undefined control sequence" nil t))
  2005. (setq errors (concat errors " [undefined control sequence]")))
  2006. (when (save-excursion
  2007. (re-search-forward "^! LaTeX.*?Error" nil t))
  2008. (setq errors (concat errors " [LaTeX error]")))
  2009. (when (save-excursion
  2010. (re-search-forward "^! Package.*?Error" nil t))
  2011. (setq errors (concat errors " [package error]")))
  2012. (and (org-string-nw-p errors) (org-trim errors)))))))
  2013. (provide 'org-e-latex)
  2014. ;;; org-e-latex.el ends here