org-e-latex.el 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459
  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 (case (org-element-property :checkbox item)
  1191. (on "$\\boxtimes$ ")
  1192. (off "$\\Box$ ")
  1193. (trans "$\\boxminus$ ")))
  1194. (tag (let ((tag (org-element-property :tag item)))
  1195. ;; Check-boxes must belong to the tag.
  1196. (and tag (format "[%s] "
  1197. (concat checkbox
  1198. (org-export-data tag info)))))))
  1199. (concat counter "\\item" (or tag (concat " " checkbox)) contents)))
  1200. ;;;; Keyword
  1201. (defun org-e-latex-keyword (keyword contents info)
  1202. "Transcode a KEYWORD element from Org to LaTeX.
  1203. CONTENTS is nil. INFO is a plist holding contextual information."
  1204. (let ((key (org-element-property :key keyword))
  1205. (value (org-element-property :value keyword)))
  1206. (cond
  1207. ((string= key "LATEX") value)
  1208. ((string= key "INDEX") (format "\\index{%s}" value))
  1209. ;; Invisible targets.
  1210. ((string= key "TARGET") nil)
  1211. ((string= key "TOC")
  1212. (let ((value (downcase value)))
  1213. (cond
  1214. ((string-match "\\<headlines\\>" value)
  1215. (let ((depth (or (and (string-match "[0-9]+" value)
  1216. (string-to-number (match-string 0 value)))
  1217. (plist-get info :with-toc))))
  1218. (concat
  1219. (when (wholenump depth)
  1220. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1221. "\\tableofcontents")))
  1222. ((string= "tables" value) "\\listoftables")
  1223. ((string= "figures" value) "\\listoffigures")
  1224. ((string= "listings" value)
  1225. (cond
  1226. ((eq org-e-latex-listings 'minted) "\\listoflistings")
  1227. (org-e-latex-listings "\\lstlistoflistings")
  1228. ;; At the moment, src blocks with a caption are wrapped
  1229. ;; into a figure environment.
  1230. (t "\\listoffigures")))))))))
  1231. ;;;; Latex Environment
  1232. (defun org-e-latex-latex-environment (latex-environment contents info)
  1233. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1234. CONTENTS is nil. INFO is a plist holding contextual information."
  1235. (let ((label (org-element-property :name latex-environment))
  1236. (value (org-remove-indentation
  1237. (org-element-property :value latex-environment))))
  1238. (if (not (org-string-nw-p label)) value
  1239. ;; Environment is labelled: label must be within the environment
  1240. ;; (otherwise, a reference pointing to that element will count
  1241. ;; the section instead).
  1242. (with-temp-buffer
  1243. (insert value)
  1244. (goto-char (point-min))
  1245. (forward-line)
  1246. (insert (format "\\label{%s}\n" label))
  1247. (buffer-string)))))
  1248. ;;;; Latex Fragment
  1249. (defun org-e-latex-latex-fragment (latex-fragment contents info)
  1250. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1251. CONTENTS is nil. INFO is a plist holding contextual information."
  1252. (org-element-property :value latex-fragment))
  1253. ;;;; Line Break
  1254. (defun org-e-latex-line-break (line-break contents info)
  1255. "Transcode a LINE-BREAK object from Org to LaTeX.
  1256. CONTENTS is nil. INFO is a plist holding contextual information."
  1257. "\\\\")
  1258. ;;;; Link
  1259. (defun org-e-latex-link--inline-image (link info)
  1260. "Return LaTeX code for an inline image.
  1261. LINK is the link pointing to the inline image. INFO is a plist
  1262. used as a communication channel."
  1263. (let* ((parent (org-export-get-parent-element link))
  1264. (path (let ((raw-path (org-element-property :path link)))
  1265. (if (not (file-name-absolute-p raw-path)) raw-path
  1266. (expand-file-name raw-path))))
  1267. (caption (org-e-latex--caption/label-string
  1268. (org-element-property :caption parent)
  1269. (org-element-property :name parent)
  1270. info))
  1271. ;; Retrieve latex attributes from the element around.
  1272. (attr (let ((raw-attr
  1273. (mapconcat #'identity
  1274. (org-element-property :attr_latex parent)
  1275. " ")))
  1276. (unless (string= raw-attr "") raw-attr)))
  1277. (disposition
  1278. (cond
  1279. ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
  1280. ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
  1281. ((or (and attr (string-match "\\<float\\>" attr))
  1282. (not (string= caption "")))
  1283. 'float)))
  1284. (placement
  1285. (cond
  1286. ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1287. (org-match-string-no-properties 1 attr))
  1288. ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
  1289. ((eq disposition 'float)
  1290. (concat "[" org-e-latex-default-figure-position "]"))
  1291. (t ""))))
  1292. ;; Now clear ATTR from any special keyword and set a default
  1293. ;; value if nothing is left.
  1294. (setq attr
  1295. (if (not attr) ""
  1296. (org-trim
  1297. (replace-regexp-in-string
  1298. "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
  1299. (setq attr (cond ((not (string= attr "")) attr)
  1300. ((eq disposition 'float) "width=0.7\\textwidth")
  1301. ((eq disposition 'wrap) "width=0.48\\textwidth")
  1302. (t (or org-e-latex-image-default-option ""))))
  1303. ;; Return proper string, depending on DISPOSITION.
  1304. (case disposition
  1305. (wrap (format "\\begin{wrapfigure}%s
  1306. \\centering
  1307. \\includegraphics[%s]{%s}
  1308. %s\\end{wrapfigure}" placement attr path caption))
  1309. (multicolumn (format "\\begin{figure*}%s
  1310. \\centering
  1311. \\includegraphics[%s]{%s}
  1312. %s\\end{figure*}" placement attr path caption))
  1313. (float (format "\\begin{figure}%s
  1314. \\centering
  1315. \\includegraphics[%s]{%s}
  1316. %s\\end{figure}" placement attr path caption))
  1317. (t (format "\\includegraphics[%s]{%s}" attr path)))))
  1318. (defun org-e-latex-link (link desc info)
  1319. "Transcode a LINK object from Org to LaTeX.
  1320. DESC is the description part of the link, or the empty string.
  1321. INFO is a plist holding contextual information. See
  1322. `org-export-data'."
  1323. (let* ((type (org-element-property :type link))
  1324. (raw-path (org-element-property :path link))
  1325. ;; Ensure DESC really exists, or set it to nil.
  1326. (desc (and (not (string= desc "")) desc))
  1327. (imagep (org-export-inline-image-p
  1328. link org-e-latex-inline-image-rules))
  1329. (path (cond
  1330. ((member type '("http" "https" "ftp" "mailto"))
  1331. (concat type ":" raw-path))
  1332. ((string= type "file")
  1333. (when (string-match "\\(.+\\)::.+" raw-path)
  1334. (setq raw-path (match-string 1 raw-path)))
  1335. (if (file-name-absolute-p raw-path)
  1336. (concat "file://" (expand-file-name raw-path))
  1337. (concat "file://" raw-path)))
  1338. (t raw-path)))
  1339. protocol)
  1340. (cond
  1341. ;; Image file.
  1342. (imagep (org-e-latex-link--inline-image link info))
  1343. ;; Radio link: Transcode target's contents and use them as link's
  1344. ;; description.
  1345. ((string= type "radio")
  1346. (let ((destination (org-export-resolve-radio-link link info)))
  1347. (when destination
  1348. (format "\\hyperref[%s]{%s}"
  1349. (org-export-solidify-link-text path)
  1350. (org-export-data (org-element-contents destination) info)))))
  1351. ;; Links pointing to an headline: Find destination and build
  1352. ;; appropriate referencing command.
  1353. ((member type '("custom-id" "fuzzy" "id"))
  1354. (let ((destination (if (string= type "fuzzy")
  1355. (org-export-resolve-fuzzy-link link info)
  1356. (org-export-resolve-id-link link info))))
  1357. (case (org-element-type destination)
  1358. ;; Id link points to an external file.
  1359. (plain-text
  1360. (if desc (format "\\href{file://%s}{%s}" destination desc)
  1361. (format "\\url{file://%s}" destination)))
  1362. ;; Fuzzy link points nowhere.
  1363. ('nil
  1364. (format org-e-latex-link-with-unknown-path-format
  1365. (or desc
  1366. (org-export-data
  1367. (org-element-property :raw-link link) info))))
  1368. ;; Fuzzy link points to an invisible target.
  1369. (keyword nil)
  1370. ;; LINK points to an headline. If headlines are numbered
  1371. ;; and the link has no description, display headline's
  1372. ;; number. Otherwise, display description or headline's
  1373. ;; title.
  1374. (headline
  1375. (let ((label
  1376. (format "sec-%s"
  1377. (mapconcat
  1378. 'number-to-string
  1379. (org-export-get-headline-number destination info)
  1380. "-"))))
  1381. (if (and (plist-get info :section-numbers) (not desc))
  1382. (format "\\ref{%s}" label)
  1383. (format "\\hyperref[%s]{%s}" label
  1384. (or desc
  1385. (org-export-data
  1386. (org-element-property :title destination) info))))))
  1387. ;; Fuzzy link points to a target. Do as above.
  1388. (otherwise
  1389. (let ((path (org-export-solidify-link-text path)))
  1390. (if (not desc) (format "\\ref{%s}" path)
  1391. (format "\\hyperref[%s]{%s}" path desc)))))))
  1392. ;; Coderef: replace link with the reference name or the
  1393. ;; equivalent line number.
  1394. ((string= type "coderef")
  1395. (format (org-export-get-coderef-format path desc)
  1396. (org-export-resolve-coderef path info)))
  1397. ;; Link type is handled by a special function.
  1398. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1399. (funcall protocol (org-link-unescape path) desc 'latex))
  1400. ;; External link with a description part.
  1401. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1402. ;; External link without a description part.
  1403. (path (format "\\url{%s}" path))
  1404. ;; No path, only description. Try to do something useful.
  1405. (t (format org-e-latex-link-with-unknown-path-format desc)))))
  1406. ;;;; Macro
  1407. (defun org-e-latex-macro (macro contents info)
  1408. "Transcode a MACRO element from Org to LaTeX.
  1409. CONTENTS is nil. INFO is a plist holding contextual information."
  1410. ;; Use available tools.
  1411. (org-export-expand-macro macro info))
  1412. ;;;; Paragraph
  1413. (defun org-e-latex-paragraph (paragraph contents info)
  1414. "Transcode a PARAGRAPH element from Org to LaTeX.
  1415. CONTENTS is the contents of the paragraph, as a string. INFO is
  1416. the plist used as a communication channel."
  1417. contents)
  1418. ;;;; Plain List
  1419. (defun org-e-latex-plain-list (plain-list contents info)
  1420. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1421. CONTENTS is the contents of the list. INFO is a plist holding
  1422. contextual information."
  1423. (let* ((type (org-element-property :type plain-list))
  1424. (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
  1425. "inparadesc" "asparadesc"))
  1426. (paralist-regexp (concat
  1427. "\\("
  1428. (mapconcat 'identity paralist-types "\\|")
  1429. "\\)"))
  1430. (attr (mapconcat #'identity
  1431. (org-element-property :attr_latex plain-list)
  1432. " "))
  1433. (latex-type (cond
  1434. ((and attr
  1435. (string-match
  1436. (format "\\<%s\\>" paralist-regexp) attr))
  1437. (match-string 1 attr))
  1438. ((eq type 'ordered) "enumerate")
  1439. ((eq type 'unordered) "itemize")
  1440. ((eq type 'descriptive) "description"))))
  1441. (org-e-latex--wrap-label
  1442. plain-list
  1443. (format "\\begin{%s}%s\n%s\\end{%s}"
  1444. latex-type
  1445. ;; Once special environment, if any, has been removed, the
  1446. ;; rest of the attributes will be optional arguments.
  1447. ;; They will be put inside square brackets if necessary.
  1448. (let ((opt (replace-regexp-in-string
  1449. (format " *%s *" paralist-regexp) "" attr)))
  1450. (cond ((string= opt "") "")
  1451. ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
  1452. (t (format "[%s]" opt))))
  1453. contents
  1454. latex-type))))
  1455. ;;;; Plain Text
  1456. (defun org-e-latex-plain-text (text info)
  1457. "Transcode a TEXT string from Org to LaTeX.
  1458. TEXT is the string to transcode. INFO is a plist holding
  1459. contextual information."
  1460. ;; Protect %, #, &, $, ~, ^, _, { and }.
  1461. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
  1462. (setq text
  1463. (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
  1464. ;; Protect \
  1465. (setq text (replace-regexp-in-string
  1466. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  1467. "$\\backslash$" text nil t 1))
  1468. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1469. (let ((case-fold-search nil)
  1470. (start 0))
  1471. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
  1472. (setq text (replace-match
  1473. (format "\\%s{}" (match-string 1 text)) nil t text)
  1474. start (match-end 0))))
  1475. ;; Handle quotation marks
  1476. (setq text (org-e-latex--quotation-marks text info))
  1477. ;; Convert special strings.
  1478. (when (plist-get info :with-special-strings)
  1479. (while (string-match (regexp-quote "...") text)
  1480. (setq text (replace-match "\\ldots{}" nil t text))))
  1481. ;; Handle break preservation if required.
  1482. (when (plist-get info :preserve-breaks)
  1483. (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1484. text)))
  1485. ;; Return value.
  1486. text)
  1487. ;;;; Planning
  1488. (defun org-e-latex-planning (planning contents info)
  1489. "Transcode a PLANNING element from Org to LaTeX.
  1490. CONTENTS is nil. INFO is a plist holding contextual
  1491. information."
  1492. (concat
  1493. "\\noindent"
  1494. (mapconcat
  1495. 'identity
  1496. (delq nil
  1497. (list
  1498. (let ((closed (org-element-property :closed planning)))
  1499. (when closed
  1500. (concat
  1501. (format "\\textbf{%s} " org-closed-string)
  1502. (format org-e-latex-inactive-timestamp-format
  1503. (org-translate-time closed)))))
  1504. (let ((deadline (org-element-property :deadline planning)))
  1505. (when deadline
  1506. (concat
  1507. (format "\\textbf{%s} " org-deadline-string)
  1508. (format org-e-latex-active-timestamp-format
  1509. (org-translate-time deadline)))))
  1510. (let ((scheduled (org-element-property :scheduled planning)))
  1511. (when scheduled
  1512. (concat
  1513. (format "\\textbf{%s} " org-scheduled-string)
  1514. (format org-e-latex-active-timestamp-format
  1515. (org-translate-time scheduled)))))))
  1516. " ")
  1517. "\\\\"))
  1518. ;;;; Property Drawer
  1519. (defun org-e-latex-property-drawer (property-drawer contents info)
  1520. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  1521. CONTENTS is nil. INFO is a plist holding contextual
  1522. information."
  1523. ;; The property drawer isn't exported but we want separating blank
  1524. ;; lines nonetheless.
  1525. "")
  1526. ;;;; Quote Block
  1527. (defun org-e-latex-quote-block (quote-block contents info)
  1528. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1529. CONTENTS holds the contents of the block. INFO is a plist
  1530. holding contextual information."
  1531. (org-e-latex--wrap-label
  1532. quote-block
  1533. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1534. ;;;; Quote Section
  1535. (defun org-e-latex-quote-section (quote-section contents info)
  1536. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1537. CONTENTS is nil. INFO is a plist holding contextual information."
  1538. (let ((value (org-remove-indentation
  1539. (org-element-property :value quote-section))))
  1540. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1541. ;;;; Radio Target
  1542. (defun org-e-latex-radio-target (radio-target text info)
  1543. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1544. TEXT is the text of the target. INFO is a plist holding
  1545. contextual information."
  1546. (format "\\label{%s}%s"
  1547. (org-export-solidify-link-text
  1548. (org-element-property :value radio-target))
  1549. text))
  1550. ;;;; Section
  1551. (defun org-e-latex-section (section contents info)
  1552. "Transcode a SECTION element from Org to LaTeX.
  1553. CONTENTS holds the contents of the section. INFO is a plist
  1554. holding contextual information."
  1555. contents)
  1556. ;;;; Special Block
  1557. (defun org-e-latex-special-block (special-block contents info)
  1558. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1559. CONTENTS holds the contents of the block. INFO is a plist
  1560. holding contextual information."
  1561. (let ((type (downcase (org-element-property :type special-block))))
  1562. (org-e-latex--wrap-label
  1563. special-block
  1564. (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
  1565. ;;;; Src Block
  1566. (defun org-e-latex-src-block (src-block contents info)
  1567. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1568. CONTENTS holds the contents of the item. INFO is a plist holding
  1569. contextual information."
  1570. (let* ((lang (org-element-property :language src-block))
  1571. (caption (org-element-property :caption src-block))
  1572. (label (org-element-property :name src-block))
  1573. (custom-env (and lang
  1574. (cadr (assq (intern lang)
  1575. org-e-latex-custom-lang-environments))))
  1576. (num-start (case (org-element-property :number-lines src-block)
  1577. (continued (org-export-get-loc src-block info))
  1578. (new 0)))
  1579. (retain-labels (org-element-property :retain-labels src-block)))
  1580. (cond
  1581. ;; Case 1. No source fontification.
  1582. ((not org-e-latex-listings)
  1583. (let ((caption-str (org-e-latex--caption/label-string caption label info))
  1584. (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
  1585. (format
  1586. (or float-env "%s")
  1587. (concat caption-str
  1588. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1589. (org-export-format-code-default src-block info))))))
  1590. ;; Case 2. Custom environment.
  1591. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1592. custom-env
  1593. (org-export-format-code-default src-block info)
  1594. custom-env))
  1595. ;; Case 3. Use minted package.
  1596. ((eq org-e-latex-listings 'minted)
  1597. (let ((float-env (when (or label caption)
  1598. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1599. (org-e-latex--caption/label-string
  1600. caption label info))))
  1601. (body
  1602. (format
  1603. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1604. ;; Options.
  1605. (org-e-latex--make-option-string
  1606. (if (not num-start) org-e-latex-minted-options
  1607. (append `(("linenos")
  1608. ("firstnumber" ,(number-to-string (1+ num-start))))
  1609. org-e-latex-minted-options)))
  1610. ;; Language.
  1611. (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
  1612. ;; Source code.
  1613. (let* ((code-info (org-export-unravel-code src-block))
  1614. (max-width
  1615. (apply 'max
  1616. (mapcar 'length
  1617. (org-split-string (car code-info) "\n")))))
  1618. (org-export-format-code
  1619. (car code-info)
  1620. (lambda (loc num ref)
  1621. (concat
  1622. loc
  1623. (when ref
  1624. ;; Ensure references are flushed to the right,
  1625. ;; separated with 6 spaces from the widest line
  1626. ;; of code.
  1627. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1628. (format "(%s)" ref)))))
  1629. nil (and retain-labels (cdr code-info)))))))
  1630. ;; Return value.
  1631. (if float-env (format float-env body) body)))
  1632. ;; Case 4. Use listings package.
  1633. (t
  1634. (let ((lst-lang
  1635. (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
  1636. (caption-str
  1637. (when caption
  1638. (let ((main (org-export-data (car caption) info)))
  1639. (if (not (cdr caption)) (format "{%s}" main)
  1640. (format "{[%s]%s}"
  1641. (org-export-data (cdr caption) info)
  1642. main))))))
  1643. (concat
  1644. ;; Options.
  1645. (format "\\lstset{%s}\n"
  1646. (org-e-latex--make-option-string
  1647. (append org-e-latex-listings-options
  1648. `(("language" ,lst-lang))
  1649. (when label `(("label" ,label)))
  1650. (when caption-str `(("caption" ,caption-str)))
  1651. (cond ((not num-start) '(("numbers" "none")))
  1652. ((zerop num-start) '(("numbers" "left")))
  1653. (t `(("numbers" "left")
  1654. ("firstnumber"
  1655. ,(number-to-string (1+ num-start)))))))))
  1656. ;; Source code.
  1657. (format
  1658. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1659. (let* ((code-info (org-export-unravel-code src-block))
  1660. (max-width
  1661. (apply 'max
  1662. (mapcar 'length
  1663. (org-split-string (car code-info) "\n")))))
  1664. (org-export-format-code
  1665. (car code-info)
  1666. (lambda (loc num ref)
  1667. (concat
  1668. loc
  1669. (when ref
  1670. ;; Ensure references are flushed to the right,
  1671. ;; separated with 6 spaces from the widest line of
  1672. ;; code
  1673. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1674. (format "(%s)" ref)))))
  1675. nil (and retain-labels (cdr code-info)))))))))))
  1676. ;;;; Statistics Cookie
  1677. (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
  1678. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1679. CONTENTS is nil. INFO is a plist holding contextual information."
  1680. (org-element-property :value statistics-cookie))
  1681. ;;;; Strike-Through
  1682. (defun org-e-latex-strike-through (strike-through contents info)
  1683. "Transcode STRIKE-THROUGH from Org to LaTeX.
  1684. CONTENTS is the text with strike-through markup. INFO is a plist
  1685. holding contextual information."
  1686. (org-e-latex--text-markup contents 'strike-through))
  1687. ;;;; Subscript
  1688. (defun org-e-latex-subscript (subscript contents info)
  1689. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1690. CONTENTS is the contents of the object. INFO is a plist holding
  1691. contextual information."
  1692. (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
  1693. ;;;; Superscript
  1694. (defun org-e-latex-superscript (superscript contents info)
  1695. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1696. CONTENTS is the contents of the object. INFO is a plist holding
  1697. contextual information."
  1698. (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
  1699. ;;;; Table
  1700. ;;
  1701. ;; `org-e-latex-table' is the entry point for table transcoding. It
  1702. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  1703. ;; delegates the job to either `org-e-latex-table--table.el-table' or
  1704. ;; `org-e-latex-table--org-table' functions, depending of the type of
  1705. ;; the table.
  1706. ;;
  1707. ;; `org-e-latex-table--align-string' is a subroutine used to build
  1708. ;; alignment string for Org tables.
  1709. (defun org-e-latex-table (table contents info)
  1710. "Transcode a TABLE element from Org to LaTeX.
  1711. CONTENTS is the contents of the table. INFO is a plist holding
  1712. contextual information."
  1713. (cond
  1714. ;; Case 1: verbatim table.
  1715. ((or org-e-latex-tables-verbatim
  1716. (let ((attr (mapconcat 'identity
  1717. (org-element-property :attr_latex table)
  1718. " ")))
  1719. (and attr (string-match "\\<verbatim\\>" attr))))
  1720. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1721. ;; Re-create table, without affiliated keywords.
  1722. (org-trim
  1723. (org-element-interpret-data
  1724. `(table nil ,@(org-element-contents table))))))
  1725. ;; Case 2: table.el table. Convert it using appropriate tools.
  1726. ((eq (org-element-property :type table) 'table.el)
  1727. (org-e-latex-table--table.el-table table contents info))
  1728. ;; Case 3: Standard table.
  1729. (t (org-e-latex-table--org-table table contents info))))
  1730. (defun org-e-latex-table--align-string (table info)
  1731. "Return an appropriate LaTeX alignment string.
  1732. TABLE is the considered table. INFO is a plist used as
  1733. a communication channel."
  1734. (let ((attr (mapconcat 'identity
  1735. (org-element-property :attr_latex table)
  1736. " ")))
  1737. (if (string-match "\\<align=\\(\\S-+\\)" attr) (match-string 1 attr)
  1738. (let (alignment)
  1739. ;; Extract column groups and alignment from first (non-rule)
  1740. ;; row.
  1741. (org-element-map
  1742. (org-element-map
  1743. table 'table-row
  1744. (lambda (row)
  1745. (and (eq (org-element-property :type row) 'standard) row))
  1746. info 'first-match)
  1747. 'table-cell
  1748. (lambda (cell)
  1749. (let ((borders (org-export-table-cell-borders cell info)))
  1750. ;; Check left border for the first cell only.
  1751. (when (and (memq 'left borders) (not alignment))
  1752. (push "|" alignment))
  1753. (push (case (org-export-table-cell-alignment cell info)
  1754. (left "l")
  1755. (right "r")
  1756. (center "c"))
  1757. alignment)
  1758. (when (memq 'right borders) (push "|" alignment))))
  1759. info)
  1760. (apply 'concat (reverse alignment))))))
  1761. (defun org-e-latex-table--org-table (table contents info)
  1762. "Return appropriate LaTeX code for an Org table.
  1763. TABLE is the table type element to transcode. CONTENTS is its
  1764. contents, as a string. INFO is a plist used as a communication
  1765. channel.
  1766. This function assumes TABLE has `org' as its `:type' attribute."
  1767. (let* ((label (org-element-property :name table))
  1768. (caption (org-e-latex--caption/label-string
  1769. (org-element-property :caption table) label info))
  1770. (attr (mapconcat 'identity
  1771. (org-element-property :attr_latex table)
  1772. " "))
  1773. ;; Determine alignment string.
  1774. (alignment (org-e-latex-table--align-string table info))
  1775. ;; Determine environment for the table: longtable, tabular...
  1776. (table-env (cond
  1777. ((not attr) org-e-latex-default-table-environment)
  1778. ((string-match "\\<longtable\\>" attr) "longtable")
  1779. ((string-match "\\<tabular.?\\>" attr)
  1780. (org-match-string-no-properties 0 attr))
  1781. (t org-e-latex-default-table-environment)))
  1782. ;; If table is a float, determine environment: table, table*
  1783. ;; or sidewaystable.
  1784. (float-env (cond
  1785. ((string= "longtable" table-env) nil)
  1786. ((and attr (string-match "\\<sidewaystable\\>" attr))
  1787. "sidewaystables")
  1788. ((and attr
  1789. (or (string-match (regexp-quote "table*") attr)
  1790. (string-match "\\<multicolumn\\>" attr)))
  1791. "table*")
  1792. ((or (not (string= caption "")) label) "table")))
  1793. ;; Extract others display options.
  1794. (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
  1795. (org-match-string-no-properties 1 attr)))
  1796. (placement
  1797. (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1798. (org-match-string-no-properties 1 attr)
  1799. (format "[%s]" org-e-latex-default-figure-position))))
  1800. ;; Prepare the final format string for the table.
  1801. (cond
  1802. ;; Longtable.
  1803. ((string= "longtable" table-env)
  1804. (format
  1805. "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
  1806. alignment
  1807. (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
  1808. (concat (org-trim caption) "\\\\\n"))
  1809. contents
  1810. (if (or org-e-latex-table-caption-above (string= "" caption)) ""
  1811. (concat (org-trim caption) "\\\\\n"))))
  1812. ;; Others.
  1813. (t (concat (when float-env
  1814. (concat
  1815. (format "\\begin{%s}%s\n" float-env placement)
  1816. (if org-e-latex-table-caption-above caption "")))
  1817. (when org-e-latex-tables-centered "\\begin{center}\n")
  1818. (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  1819. table-env
  1820. (if width (format "{%s}" width) "")
  1821. alignment
  1822. contents
  1823. table-env)
  1824. (when org-e-latex-tables-centered "\n\\end{center}")
  1825. (when float-env
  1826. (concat (if org-e-latex-table-caption-above "" caption)
  1827. (format "\n\\end{%s}" float-env))))))))
  1828. (defun org-e-latex-table--table.el-table (table contents info)
  1829. "Return appropriate LaTeX code for a table.el table.
  1830. TABLE is the table type element to transcode. CONTENTS is its
  1831. contents, as a string. INFO is a plist used as a communication
  1832. channel.
  1833. This function assumes TABLE has `table.el' as its `:type'
  1834. attribute."
  1835. (require 'table)
  1836. ;; Ensure "*org-export-table*" buffer is empty.
  1837. (with-current-buffer (get-buffer-create "*org-export-table*")
  1838. (erase-buffer))
  1839. (let ((output (with-temp-buffer
  1840. (insert (org-element-property :value table))
  1841. (goto-char 1)
  1842. (re-search-forward "^[ \t]*|[^|]" nil t)
  1843. (table-generate-source 'latex "*org-export-table*")
  1844. (with-current-buffer "*org-export-table*"
  1845. (org-trim (buffer-string))))))
  1846. (kill-buffer (get-buffer "*org-export-table*"))
  1847. ;; Remove left out comments.
  1848. (while (string-match "^%.*\n" output)
  1849. (setq output (replace-match "" t t output)))
  1850. ;; When the "rmlines" attribute is provided, remove all hlines but
  1851. ;; the the one separating heading from the table body.
  1852. (let ((attr (mapconcat 'identity
  1853. (org-element-property :attr_latex table)
  1854. " ")))
  1855. (when (and attr (string-match "\\<rmlines\\>" attr))
  1856. (let ((n 0) (pos 0))
  1857. (while (and (< (length output) pos)
  1858. (setq pos (string-match "^\\\\hline\n?" output pos)))
  1859. (incf n)
  1860. (unless (= n 2)
  1861. (setq output (replace-match "" nil nil output)))))))
  1862. (if (not org-e-latex-tables-centered) output
  1863. (format "\\begin{center}\n%s\n\\end{center}" output))))
  1864. ;;;; Table Cell
  1865. (defun org-e-latex-table-cell (table-cell contents info)
  1866. "Transcode a TABLE-CELL element from Org to LaTeX.
  1867. CONTENTS is the cell contents. INFO is a plist used as
  1868. a communication channel."
  1869. (concat (if (and contents
  1870. org-e-latex-table-scientific-notation
  1871. (string-match orgtbl-exp-regexp contents))
  1872. ;; Use appropriate format string for scientific
  1873. ;; notation.
  1874. (format org-e-latex-table-scientific-notation
  1875. (match-string 1 contents)
  1876. (match-string 2 contents))
  1877. contents)
  1878. (when (org-export-get-next-element table-cell) " & ")))
  1879. ;;;; Table Row
  1880. (defun org-e-latex-table-row (table-row contents info)
  1881. "Transcode a TABLE-ROW element from Org to LaTeX.
  1882. CONTENTS is the contents of the row. INFO is a plist used as
  1883. a communication channel."
  1884. ;; Rules are ignored since table separators are deduced from
  1885. ;; borders of the current row.
  1886. (when (eq (org-element-property :type table-row) 'standard)
  1887. (let* ((attr (mapconcat 'identity
  1888. (org-element-property
  1889. :attr_latex (org-export-get-parent table-row))
  1890. " "))
  1891. (longtablep (and attr (string-match "\\<longtable\\>" attr)))
  1892. (booktabsp
  1893. (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
  1894. org-e-latex-tables-booktabs))
  1895. ;; TABLE-ROW's borders are extracted from its first cell.
  1896. (borders
  1897. (org-export-table-cell-borders
  1898. (car (org-element-contents table-row)) info)))
  1899. (concat
  1900. ;; When BOOKTABS are activated enforce top-rule even when no
  1901. ;; hline was specifically marked.
  1902. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  1903. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  1904. contents "\\\\\n"
  1905. (cond
  1906. ;; Special case for long tables. Define header and footers.
  1907. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  1908. (format "%s
  1909. \\endhead
  1910. %s\\multicolumn{%d}{r}{Continued on next page} \\\\
  1911. \\endfoot
  1912. \\endlastfoot"
  1913. (if booktabsp "\\midrule" "\\hline")
  1914. (if booktabsp "\\midrule" "\\hline")
  1915. ;; Number of columns.
  1916. (cdr (org-export-table-dimensions
  1917. (org-export-get-parent-table table-row) info))))
  1918. ;; When BOOKTABS are activated enforce bottom rule even when
  1919. ;; no hline was specifically marked.
  1920. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  1921. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  1922. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  1923. ;;;; Target
  1924. (defun org-e-latex-target (target contents info)
  1925. "Transcode a TARGET object from Org to LaTeX.
  1926. CONTENTS is nil. INFO is a plist holding contextual
  1927. information."
  1928. (format "\\label{%s}"
  1929. (org-export-solidify-link-text (org-element-property :value target))))
  1930. ;;;; Timestamp
  1931. (defun org-e-latex-timestamp (timestamp contents info)
  1932. "Transcode a TIMESTAMP object from Org to LaTeX.
  1933. CONTENTS is nil. INFO is a plist holding contextual
  1934. information."
  1935. (let ((value (org-translate-time (org-element-property :value timestamp)))
  1936. (type (org-element-property :type timestamp)))
  1937. (cond ((memq type '(active active-range))
  1938. (format org-e-latex-active-timestamp-format value))
  1939. ((memq type '(inactive inactive-range))
  1940. (format org-e-latex-inactive-timestamp-format value))
  1941. (t (format org-e-latex-diary-timestamp-format value)))))
  1942. ;;;; Underline
  1943. (defun org-e-latex-underline (underline contents info)
  1944. "Transcode UNDERLINE from Org to LaTeX.
  1945. CONTENTS is the text with underline markup. INFO is a plist
  1946. holding contextual information."
  1947. (org-e-latex--text-markup contents 'underline))
  1948. ;;;; Verbatim
  1949. (defun org-e-latex-verbatim (verbatim contents info)
  1950. "Transcode a VERBATIM object from Org to LaTeX.
  1951. CONTENTS is nil. INFO is a plist used as a communication
  1952. channel."
  1953. (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  1954. ;;;; Verse Block
  1955. (defun org-e-latex-verse-block (verse-block contents info)
  1956. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  1957. CONTENTS is verse block contents. INFO is a plist holding
  1958. contextual information."
  1959. (org-e-latex--wrap-label
  1960. verse-block
  1961. ;; In a verse environment, add a line break to each newline
  1962. ;; character and change each white space at beginning of a line
  1963. ;; into a space of 1 em. Also change each blank line with
  1964. ;; a vertical space of 1 em.
  1965. (progn
  1966. (setq contents (replace-regexp-in-string
  1967. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  1968. (replace-regexp-in-string
  1969. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  1970. (while (string-match "^[ \t]+" contents)
  1971. (let ((new-str (format "\\hspace*{%dem}"
  1972. (length (match-string 0 contents)))))
  1973. (setq contents (replace-match new-str nil t contents))))
  1974. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  1975. ;;; Interactive functions
  1976. (defun org-e-latex-export-to-latex
  1977. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1978. "Export current buffer to a LaTeX file.
  1979. If narrowing is active in the current buffer, only export its
  1980. narrowed part.
  1981. If a region is active, export that region.
  1982. When optional argument SUBTREEP is non-nil, export the sub-tree
  1983. at point, extracting information from the headline properties
  1984. first.
  1985. When optional argument VISIBLE-ONLY is non-nil, don't export
  1986. contents of hidden elements.
  1987. When optional argument BODY-ONLY is non-nil, only write code
  1988. between \"\\begin{document}\" and \"\\end{document}\".
  1989. EXT-PLIST, when provided, is a property list with external
  1990. parameters overriding Org default settings, but still inferior to
  1991. file-local settings.
  1992. When optional argument PUB-DIR is set, use it as the publishing
  1993. directory.
  1994. Return output file's name."
  1995. (interactive)
  1996. (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
  1997. (org-export-to-file
  1998. 'e-latex outfile subtreep visible-only body-only ext-plist)))
  1999. (defun org-e-latex-export-to-pdf
  2000. (&optional subtreep visible-only body-only ext-plist pub-dir)
  2001. "Export current buffer to LaTeX then process through to PDF.
  2002. If narrowing is active in the current buffer, only export its
  2003. narrowed part.
  2004. If a region is active, export that region.
  2005. When optional argument SUBTREEP is non-nil, export the sub-tree
  2006. at point, extracting information from the headline properties
  2007. first.
  2008. When optional argument VISIBLE-ONLY is non-nil, don't export
  2009. contents of hidden elements.
  2010. When optional argument BODY-ONLY is non-nil, only write code
  2011. between \"\\begin{document}\" and \"\\end{document}\".
  2012. EXT-PLIST, when provided, is a property list with external
  2013. parameters overriding Org default settings, but still inferior to
  2014. file-local settings.
  2015. When optional argument PUB-DIR is set, use it as the publishing
  2016. directory.
  2017. Return PDF file's name."
  2018. (interactive)
  2019. (org-e-latex-compile
  2020. (org-e-latex-export-to-latex
  2021. subtreep visible-only body-only ext-plist pub-dir)))
  2022. (defun org-e-latex-compile (texfile)
  2023. "Compile a TeX file.
  2024. TEXFILE is the name of the file being compiled. Processing is
  2025. done through the command specified in `org-e-latex-pdf-process'.
  2026. Return PDF file name or an error if it couldn't be produced."
  2027. (let* ((wconfig (current-window-configuration))
  2028. (texfile (file-truename texfile))
  2029. (base (file-name-sans-extension texfile))
  2030. errors)
  2031. (message (format "Processing LaTeX file %s ..." texfile))
  2032. (unwind-protect
  2033. (progn
  2034. (cond
  2035. ;; A function is provided: Apply it.
  2036. ((functionp org-e-latex-pdf-process)
  2037. (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
  2038. ;; A list is provided: Replace %b, %f and %o with appropriate
  2039. ;; values in each command before applying it. Output is
  2040. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2041. ((consp org-e-latex-pdf-process)
  2042. (let* ((out-dir (or (file-name-directory texfile) "./"))
  2043. (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
  2044. (mapc
  2045. (lambda (command)
  2046. (shell-command
  2047. (replace-regexp-in-string
  2048. "%b" (shell-quote-argument base)
  2049. (replace-regexp-in-string
  2050. "%f" (shell-quote-argument texfile)
  2051. (replace-regexp-in-string
  2052. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2053. outbuf))
  2054. org-e-latex-pdf-process)
  2055. ;; Collect standard errors from output buffer.
  2056. (setq errors (org-e-latex-collect-errors outbuf))))
  2057. (t (error "No valid command to process to PDF")))
  2058. (let ((pdffile (concat base ".pdf")))
  2059. ;; Check for process failure. Provide collected errors if
  2060. ;; possible.
  2061. (if (not (file-exists-p pdffile))
  2062. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2063. (when errors (concat ": " errors))))
  2064. ;; Else remove log files, when specified, and signal end of
  2065. ;; process to user, along with any error encountered.
  2066. (when org-e-latex-remove-logfiles
  2067. (dolist (ext org-e-latex-logfiles-extensions)
  2068. (let ((file (concat base "." ext)))
  2069. (when (file-exists-p file) (delete-file file)))))
  2070. (message (concat "Process completed"
  2071. (if (not errors) "."
  2072. (concat " with errors: " errors)))))
  2073. ;; Return output file name.
  2074. pdffile))
  2075. (set-window-configuration wconfig))))
  2076. (defun org-e-latex-collect-errors (buffer)
  2077. "Collect some kind of errors from \"pdflatex\" command output.
  2078. BUFFER is the buffer containing output.
  2079. Return collected error types as a string, or nil if there was
  2080. none."
  2081. (with-current-buffer buffer
  2082. (save-excursion
  2083. (goto-char (point-max))
  2084. ;; Find final "pdflatex" run.
  2085. (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
  2086. (let ((case-fold-search t)
  2087. (errors ""))
  2088. (when (save-excursion
  2089. (re-search-forward "Reference.*?undefined" nil t))
  2090. (setq errors (concat errors " [undefined reference]")))
  2091. (when (save-excursion
  2092. (re-search-forward "Citation.*?undefined" nil t))
  2093. (setq errors (concat errors " [undefined citation]")))
  2094. (when (save-excursion
  2095. (re-search-forward "Undefined control sequence" nil t))
  2096. (setq errors (concat errors " [undefined control sequence]")))
  2097. (when (save-excursion
  2098. (re-search-forward "^! LaTeX.*?Error" nil t))
  2099. (setq errors (concat errors " [LaTeX error]")))
  2100. (when (save-excursion
  2101. (re-search-forward "^! Package.*?Error" nil t))
  2102. (setq errors (concat errors " [package error]")))
  2103. (and (org-string-nw-p errors) (org-trim errors)))))))
  2104. (provide 'org-e-latex)
  2105. ;;; org-e-latex.el ends here