org-e-latex.el 88 KB

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