org-e-latex.el 90 KB

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