org-e-latex.el 91 KB

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