org-e-latex.el 95 KB

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