org-e-latex.el 86 KB

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