org-e-latex.el 86 KB

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