org-e-latex.el 91 KB

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