ox-latex.el 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647
  1. ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; See Org manual for details.
  19. ;;; Code:
  20. (eval-when-compile (require 'cl))
  21. (require 'ox)
  22. (require 'ox-publish)
  23. (defvar org-latex-default-packages-alist)
  24. (defvar org-latex-packages-alist)
  25. (defvar orgtbl-exp-regexp)
  26. ;;; Define Back-End
  27. (org-export-define-backend 'latex
  28. '((bold . org-latex-bold)
  29. (center-block . org-latex-center-block)
  30. (clock . org-latex-clock)
  31. (code . org-latex-code)
  32. (drawer . org-latex-drawer)
  33. (dynamic-block . org-latex-dynamic-block)
  34. (entity . org-latex-entity)
  35. (example-block . org-latex-example-block)
  36. (export-block . org-latex-export-block)
  37. (export-snippet . org-latex-export-snippet)
  38. (fixed-width . org-latex-fixed-width)
  39. (footnote-definition . org-latex-footnote-definition)
  40. (footnote-reference . org-latex-footnote-reference)
  41. (headline . org-latex-headline)
  42. (horizontal-rule . org-latex-horizontal-rule)
  43. (inline-src-block . org-latex-inline-src-block)
  44. (inlinetask . org-latex-inlinetask)
  45. (italic . org-latex-italic)
  46. (item . org-latex-item)
  47. (keyword . org-latex-keyword)
  48. (latex-environment . org-latex-latex-environment)
  49. (latex-fragment . org-latex-latex-fragment)
  50. (line-break . org-latex-line-break)
  51. (link . org-latex-link)
  52. (node-property . org-latex-node-property)
  53. (paragraph . org-latex-paragraph)
  54. (plain-list . org-latex-plain-list)
  55. (plain-text . org-latex-plain-text)
  56. (planning . org-latex-planning)
  57. (property-drawer . org-latex-property-drawer)
  58. (quote-block . org-latex-quote-block)
  59. (radio-target . org-latex-radio-target)
  60. (section . org-latex-section)
  61. (special-block . org-latex-special-block)
  62. (src-block . org-latex-src-block)
  63. (statistics-cookie . org-latex-statistics-cookie)
  64. (strike-through . org-latex-strike-through)
  65. (subscript . org-latex-subscript)
  66. (superscript . org-latex-superscript)
  67. (table . org-latex-table)
  68. (table-cell . org-latex-table-cell)
  69. (table-row . org-latex-table-row)
  70. (target . org-latex-target)
  71. (template . org-latex-template)
  72. (timestamp . org-latex-timestamp)
  73. (underline . org-latex-underline)
  74. (verbatim . org-latex-verbatim)
  75. (verse-block . org-latex-verse-block)
  76. ;; Pseudo objects and elements.
  77. (latex-math-block . org-latex-math-block)
  78. (latex-matrices . org-latex-matrices))
  79. :export-block '("LATEX" "TEX")
  80. :menu-entry
  81. '(?l "Export to LaTeX"
  82. ((?L "As LaTeX buffer" org-latex-export-as-latex)
  83. (?l "As LaTeX file" org-latex-export-to-latex)
  84. (?p "As PDF file" org-latex-export-to-pdf)
  85. (?o "As PDF file and open"
  86. (lambda (a s v b)
  87. (if a (org-latex-export-to-pdf t s v b)
  88. (org-open-file (org-latex-export-to-pdf nil s v b)))))))
  89. :filters-alist '((:filter-options . org-latex-math-block-options-filter)
  90. (:filter-parse-tree org-latex-math-block-tree-filter
  91. org-latex-matrices-tree-filter))
  92. :options-alist
  93. '((:latex-class "LATEX_CLASS" nil org-latex-default-class t)
  94. (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
  95. (:latex-header "LATEX_HEADER" nil nil newline)
  96. (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
  97. (:description "DESCRIPTION" nil nil parse)
  98. (:keywords "KEYWORDS" nil nil parse)
  99. (:subtitle "SUBTITLE" nil nil parse)
  100. ;; Other variables.
  101. (:latex-active-timestamp-format nil nil org-latex-active-timestamp-format)
  102. (:latex-caption-above nil nil org-latex-caption-above)
  103. (:latex-classes nil nil org-latex-classes)
  104. (:latex-default-figure-position nil nil org-latex-default-figure-position)
  105. (:latex-default-table-environment nil nil org-latex-default-table-environment)
  106. (:latex-default-table-mode nil nil org-latex-default-table-mode)
  107. (:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
  108. (:latex-footnote-separator nil nil org-latex-footnote-separator)
  109. (:latex-format-drawer-function nil nil org-latex-format-drawer-function)
  110. (:latex-format-headline-function nil nil org-latex-format-headline-function)
  111. (:latex-format-inlinetask-function nil nil org-latex-format-inlinetask-function)
  112. (:latex-hyperref-template nil nil org-latex-hyperref-template t)
  113. (:latex-image-default-height nil nil org-latex-image-default-height)
  114. (:latex-image-default-option nil nil org-latex-image-default-option)
  115. (:latex-image-default-width nil nil org-latex-image-default-width)
  116. (:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format)
  117. (:latex-inline-image-rules nil nil org-latex-inline-image-rules)
  118. (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format)
  119. (:latex-listings nil nil org-latex-listings)
  120. (:latex-listings-langs nil nil org-latex-listings-langs)
  121. (:latex-listings-options nil nil org-latex-listings-options)
  122. (:latex-minted-langs nil nil org-latex-minted-langs)
  123. (:latex-minted-options nil nil org-latex-minted-options)
  124. (:latex-prefer-user-labels nil nil org-latex-prefer-user-labels)
  125. (:latex-subtitle-format nil nil org-latex-subtitle-format)
  126. (:latex-subtitle-separate nil nil org-latex-subtitle-separate)
  127. (:latex-table-scientific-notation nil nil org-latex-table-scientific-notation)
  128. (:latex-tables-booktabs nil nil org-latex-tables-booktabs)
  129. (:latex-tables-centered nil nil org-latex-tables-centered)
  130. (:latex-text-markup-alist nil nil org-latex-text-markup-alist)
  131. (:latex-title-command nil nil org-latex-title-command)
  132. (:latex-toc-command nil nil org-latex-toc-command)
  133. (:latex-compiler "LATEX_COMPILER" nil org-latex-compiler)
  134. ;; Redefine regular options.
  135. (:date "DATE" nil "\\today" parse)))
  136. ;;; Internal Variables
  137. (defconst org-latex-babel-language-alist
  138. '(("af" . "afrikaans")
  139. ("bg" . "bulgarian")
  140. ("bt-br" . "brazilian")
  141. ("ca" . "catalan")
  142. ("cs" . "czech")
  143. ("cy" . "welsh")
  144. ("da" . "danish")
  145. ("de" . "germanb")
  146. ("de-at" . "naustrian")
  147. ("de-de" . "ngerman")
  148. ("el" . "greek")
  149. ("en" . "english")
  150. ("en-au" . "australian")
  151. ("en-ca" . "canadian")
  152. ("en-gb" . "british")
  153. ("en-ie" . "irish")
  154. ("en-nz" . "newzealand")
  155. ("en-us" . "american")
  156. ("es" . "spanish")
  157. ("et" . "estonian")
  158. ("eu" . "basque")
  159. ("fi" . "finnish")
  160. ("fr" . "frenchb")
  161. ("fr-ca" . "canadien")
  162. ("gl" . "galician")
  163. ("hr" . "croatian")
  164. ("hu" . "hungarian")
  165. ("id" . "indonesian")
  166. ("is" . "icelandic")
  167. ("it" . "italian")
  168. ("la" . "latin")
  169. ("ms" . "malay")
  170. ("nl" . "dutch")
  171. ("nb" . "norsk")
  172. ("nn" . "nynorsk")
  173. ("no" . "norsk")
  174. ("pl" . "polish")
  175. ("pt" . "portuguese")
  176. ("ro" . "romanian")
  177. ("ru" . "russian")
  178. ("sa" . "sanskrit")
  179. ("sb" . "uppersorbian")
  180. ("sk" . "slovak")
  181. ("sl" . "slovene")
  182. ("sq" . "albanian")
  183. ("sr" . "serbian")
  184. ("sv" . "swedish")
  185. ("ta" . "tamil")
  186. ("tr" . "turkish")
  187. ("uk" . "ukrainian"))
  188. "Alist between language code and corresponding Babel option.")
  189. (defconst org-latex-polyglossia-language-alist
  190. '(("am" "amharic")
  191. ("ast" "asturian")
  192. ("ar" "arabic")
  193. ("bo" "tibetan")
  194. ("bn" "bengali")
  195. ("bg" "bulgarian")
  196. ("br" "breton")
  197. ("bt-br" "brazilian")
  198. ("ca" "catalan")
  199. ("cop" "coptic")
  200. ("cs" "czech")
  201. ("cy" "welsh")
  202. ("da" "danish")
  203. ("de" "german" "german")
  204. ("de-at" "german" "austrian")
  205. ("de-de" "german" "german")
  206. ("dv" "divehi")
  207. ("el" "greek")
  208. ("en" "english" "usmax")
  209. ("en-au" "english" "australian")
  210. ("en-gb" "english" "uk")
  211. ("en-nz" "english" "newzealand")
  212. ("en-us" "english" "usmax")
  213. ("eo" "esperanto")
  214. ("es" "spanish")
  215. ("et" "estonian")
  216. ("eu" "basque")
  217. ("fa" "farsi")
  218. ("fi" "finnish")
  219. ("fr" "french")
  220. ("fu" "friulan")
  221. ("ga" "irish")
  222. ("gd" "scottish")
  223. ("gl" "galician")
  224. ("he" "hebrew")
  225. ("hi" "hindi")
  226. ("hr" "croatian")
  227. ("hu" "magyar")
  228. ("hy" "armenian")
  229. ("id" "bahasai")
  230. ("ia" "interlingua")
  231. ("is" "icelandic")
  232. ("it" "italian")
  233. ("kn" "kannada")
  234. ("la" "latin" "modern")
  235. ("la-modern" "latin" "modern")
  236. ("la-classic" "latin" "classic")
  237. ("la-medieval" "latin" "medieval")
  238. ("lo" "lao")
  239. ("lt" "lithuanian")
  240. ("lv" "latvian")
  241. ("mr" "maranthi")
  242. ("ml" "malayalam")
  243. ("nl" "dutch")
  244. ("nb" "norsk")
  245. ("nn" "nynorsk")
  246. ("nko" "nko")
  247. ("no" "norsk")
  248. ("oc" "occitan")
  249. ("pl" "polish")
  250. ("pms" "piedmontese")
  251. ("pt" "portuges")
  252. ("rm" "romansh")
  253. ("ro" "romanian")
  254. ("ru" "russian")
  255. ("sa" "sanskrit")
  256. ("hsb" "usorbian")
  257. ("dsb" "lsorbian")
  258. ("sk" "slovak")
  259. ("sl" "slovenian")
  260. ("se" "samin")
  261. ("sq" "albanian")
  262. ("sr" "serbian")
  263. ("sv" "swedish")
  264. ("syr" "syriac")
  265. ("ta" "tamil")
  266. ("te" "telugu")
  267. ("th" "thai")
  268. ("tk" "turkmen")
  269. ("tr" "turkish")
  270. ("uk" "ukrainian")
  271. ("ur" "urdu")
  272. ("vi" "vietnamese"))
  273. "Alist between language code and corresponding Polyglossia option")
  274. (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
  275. ("qbordermatrix" . "\\cr")
  276. ("kbordermatrix" . "\\\\"))
  277. "Alist between matrix macros and their row ending.")
  278. (defconst org-latex-math-environments-re
  279. (format
  280. "\\`[ \t]*\\\\begin{%s\\*?}"
  281. (regexp-opt
  282. '("equation" "eqnarray" "math" "displaymath"
  283. "align" "gather" "multline" "flalign" "alignat"
  284. "xalignat" "xxalignat"
  285. "subequations"
  286. ;; breqn
  287. "dmath" "dseries" "dgroup" "darray"
  288. ;; empheq
  289. "empheq")))
  290. "Regexp of LaTeX math environments.")
  291. ;;; User Configurable Variables
  292. (defgroup org-export-latex nil
  293. "Options for exporting Org mode files to LaTeX."
  294. :tag "Org Export LaTeX"
  295. :group 'org-export)
  296. ;;;; Generic
  297. (defcustom org-latex-caption-above '(table)
  298. "When non-nil, place caption string at the beginning of elements.
  299. Otherwise, place it near the end. When value is a list of
  300. symbols, put caption above selected elements only. Allowed
  301. symbols are: `image', `table', `src-block' and `special-block'."
  302. :group 'org-export-latex
  303. :version "25.1"
  304. :package-version '(Org . "8.3")
  305. :type '(choice
  306. (const :tag "For all elements" t)
  307. (const :tag "For no element" nil)
  308. (set :tag "For the following elements only" :greedy t
  309. (const :tag "Images" image)
  310. (const :tag "Tables" table)
  311. (const :tag "Source code" src-block)
  312. (const :tag "Special blocks" special-block))))
  313. (defcustom org-latex-prefer-user-labels nil
  314. "Use user-provided labels instead of internal ones when non-nil.
  315. When this variable is non-nil, Org will use the value of
  316. CUSTOM_ID property, NAME keyword or Org target as the key for the
  317. \\label commands generated.
  318. By default, Org generates its own internal labels during LaTeX
  319. export. This process ensures that the \\label keys are unique
  320. and valid, but it means the keys are not available in advance of
  321. the export process.
  322. Setting this variable gives you control over how Org generates
  323. labels during LaTeX export, so that you may know their keys in
  324. advance. One reason to do this is that it allows you to refer to
  325. various elements using a single label both in Org's link syntax
  326. and in embedded LaTeX code.
  327. For example, when this variable is non-nil, a headline like this:
  328. ** Some section
  329. :PROPERTIES:
  330. :CUSTOM_ID: sec:foo
  331. :END:
  332. This is section [[#sec:foo]].
  333. #+BEGIN_LATEX
  334. And this is still section \\ref{sec:foo}.
  335. #+END_LATEX
  336. will be exported to LaTeX as:
  337. \\subsection{Some section}
  338. \\label{sec:foo}
  339. This is section \\ref{sec:foo}.
  340. And this is still section \\ref{sec:foo}.
  341. Note, however, that setting this variable introduces a limitation
  342. on the possible values for CUSTOM_ID and NAME. When this
  343. variable is non-nil, Org passes their value to \\label unchanged.
  344. You are responsible for ensuring that the value is a valid LaTeX
  345. \\label key, and that no other \\label commands with the same key
  346. appear elsewhere in your document. (Keys may contain letters,
  347. numbers, and the following punctuation: '_' '.' '-' ':'.) There
  348. are no such limitations on CUSTOM_ID and NAME when this variable
  349. is nil.
  350. For headlines that do not define the CUSTOM_ID property or
  351. elements without a NAME, Org will continue to use its default
  352. labeling scheme to generate labels and resolve links into proper
  353. references."
  354. :group 'org-export-latex
  355. :type 'boolean
  356. :version "25.1"
  357. :package-version '(Org . "8.3"))
  358. ;;;; Preamble
  359. (defcustom org-latex-default-class "article"
  360. "The default LaTeX class."
  361. :group 'org-export-latex
  362. :type '(string :tag "LaTeX class"))
  363. (defcustom org-latex-classes
  364. '(("article"
  365. "\\documentclass[11pt]{article}"
  366. ("\\section{%s}" . "\\section*{%s}")
  367. ("\\subsection{%s}" . "\\subsection*{%s}")
  368. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  369. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  370. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  371. ("report"
  372. "\\documentclass[11pt]{report}"
  373. ("\\part{%s}" . "\\part*{%s}")
  374. ("\\chapter{%s}" . "\\chapter*{%s}")
  375. ("\\section{%s}" . "\\section*{%s}")
  376. ("\\subsection{%s}" . "\\subsection*{%s}")
  377. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  378. ("book"
  379. "\\documentclass[11pt]{book}"
  380. ("\\part{%s}" . "\\part*{%s}")
  381. ("\\chapter{%s}" . "\\chapter*{%s}")
  382. ("\\section{%s}" . "\\section*{%s}")
  383. ("\\subsection{%s}" . "\\subsection*{%s}")
  384. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  385. "Alist of LaTeX classes and associated header and structure.
  386. If #+LATEX_CLASS is set in the buffer, use its value and the
  387. associated information. Here is the structure of each cell:
  388. (class-name
  389. header-string
  390. (numbered-section . unnumbered-section)
  391. ...)
  392. The header string
  393. -----------------
  394. The HEADER-STRING is the header that will be inserted into the
  395. LaTeX file. It should contain the \\documentclass macro, and
  396. anything else that is needed for this setup. To this header, the
  397. following commands will be added:
  398. - Calls to \\usepackage for all packages mentioned in the
  399. variables `org-latex-default-packages-alist' and
  400. `org-latex-packages-alist'. Thus, your header definitions
  401. should avoid to also request these packages.
  402. - Lines specified via \"#+LATEX_HEADER:\" and
  403. \"#+LATEX_HEADER_EXTRA:\" keywords.
  404. If you need more control about the sequence in which the header
  405. is built up, or if you want to exclude one of these building
  406. blocks for a particular class, you can use the following
  407. macro-like placeholders.
  408. [DEFAULT-PACKAGES] \\usepackage statements for default packages
  409. [NO-DEFAULT-PACKAGES] do not include any of the default packages
  410. [PACKAGES] \\usepackage statements for packages
  411. [NO-PACKAGES] do not include the packages
  412. [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
  413. [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
  414. So a header like
  415. \\documentclass{article}
  416. [NO-DEFAULT-PACKAGES]
  417. [EXTRA]
  418. \\providecommand{\\alert}[1]{\\textbf{#1}}
  419. [PACKAGES]
  420. will omit the default packages, and will include the
  421. #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
  422. to \\providecommand, and then place \\usepackage commands based
  423. on the content of `org-latex-packages-alist'.
  424. If your header, `org-latex-default-packages-alist' or
  425. `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
  426. AUTO will automatically be replaced with a coding system derived
  427. from `buffer-file-coding-system'. See also the variable
  428. `org-latex-inputenc-alist' for a way to influence this mechanism.
  429. Likewise, if your header contains \"\\usepackage[AUTO]{babel}\"
  430. or \"\\usepackage[AUTO]{polyglossia}\", AUTO will be replaced
  431. with the language related to the language code specified by
  432. `org-export-default-language'. Note that constructions such as
  433. \"\\usepackage[french,AUTO,english]{babel}\" are permitted. For
  434. Polyglossia the language will be set via the macros
  435. \"\\setmainlanguage\" and \"\\setotherlanguage\". See also
  436. `org-latex-guess-babel-language' and
  437. `org-latex-guess-polyglossia-language'.
  438. The sectioning structure
  439. ------------------------
  440. The sectioning structure of the class is given by the elements
  441. following the header string. For each sectioning level, a number
  442. of strings is specified. A %s formatter is mandatory in each
  443. section string and will be replaced by the title of the section.
  444. Instead of a cons cell (numbered . unnumbered), you can also
  445. provide a list of 2 or 4 elements,
  446. (numbered-open numbered-close)
  447. or
  448. (numbered-open numbered-close unnumbered-open unnumbered-close)
  449. providing opening and closing strings for a LaTeX environment
  450. that should represent the document section. The opening clause
  451. should have a %s to represent the section title.
  452. Instead of a list of sectioning commands, you can also specify
  453. a function name. That function will be called with two
  454. parameters, the (reduced) level of the headline, and a predicate
  455. non-nil when the headline should be numbered. It must return
  456. a format string in which the section title will be added."
  457. :group 'org-export-latex
  458. :type '(repeat
  459. (list (string :tag "LaTeX class")
  460. (string :tag "LaTeX header")
  461. (repeat :tag "Levels" :inline t
  462. (choice
  463. (cons :tag "Heading"
  464. (string :tag " numbered")
  465. (string :tag "unnumbered"))
  466. (list :tag "Environment"
  467. (string :tag "Opening (numbered)")
  468. (string :tag "Closing (numbered)")
  469. (string :tag "Opening (unnumbered)")
  470. (string :tag "Closing (unnumbered)"))
  471. (function :tag "Hook computing sectioning"))))))
  472. (defcustom org-latex-inputenc-alist nil
  473. "Alist of inputenc coding system names, and what should really be used.
  474. For example, adding an entry
  475. (\"utf8\" . \"utf8x\")
  476. will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
  477. are written as utf8 files."
  478. :group 'org-export-latex
  479. :type '(repeat
  480. (cons
  481. (string :tag "Derived from buffer")
  482. (string :tag "Use this instead"))))
  483. (defcustom org-latex-title-command "\\maketitle"
  484. "The command used to insert the title just after \\begin{document}.
  485. This format string may contain these elements:
  486. %a for AUTHOR keyword
  487. %t for TITLE keyword
  488. %s for SUBTITLE keyword
  489. %k for KEYWORDS line
  490. %d for DESCRIPTION line
  491. %c for CREATOR line
  492. %l for Language keyword
  493. %L for capitalized language keyword
  494. %D for DATE keyword
  495. If you need to use a \"%\" character, you need to escape it
  496. like that: \"%%\".
  497. Setting :latex-title-command in publishing projects will take
  498. precedence over this variable."
  499. :group 'org-export-latex
  500. :type '(string :tag "Format string"))
  501. (defcustom org-latex-subtitle-format "\\\\\\medskip\n\\large %s"
  502. "Format string used for transcoded subtitle.
  503. The format string should have at most one \"%s\"-expression,
  504. which is replaced with the subtitle."
  505. :group 'org-export-latex
  506. :version "25.1"
  507. :package-version '(Org . "8.3")
  508. :type '(string :tag "Format string"))
  509. (defcustom org-latex-subtitle-separate nil
  510. "Non-nil means the subtitle is not typeset as part of title."
  511. :group 'org-export-latex
  512. :version "25.1"
  513. :package-version '(Org . "8.3")
  514. :type 'boolean)
  515. (defcustom org-latex-toc-command "\\tableofcontents\n\n"
  516. "LaTeX command to set the table of contents, list of figures, etc.
  517. This command only applies to the table of contents generated with
  518. the toc:nil option, not to those generated with #+TOC keyword."
  519. :group 'org-export-latex
  520. :type 'string)
  521. (defcustom org-latex-hyperref-template
  522. "\\hypersetup{\n pdfauthor={%a},\n pdftitle={%t},\n pdfkeywords={%k},
  523. pdfsubject={%d},\n pdfcreator={%c}, \n pdflang={%L}}\n"
  524. "Template for hyperref package options.
  525. This format string may contain these elements:
  526. %a for AUTHOR keyword
  527. %t for TITLE keyword
  528. %s for SUBTITLE keyword
  529. %k for KEYWORDS line
  530. %d for DESCRIPTION line
  531. %c for CREATOR line
  532. %l for Language keyword
  533. %L for capitalized language keyword
  534. %D for DATE keyword
  535. If you need to use a \"%\" character, you need to escape it
  536. like that: \"%%\".
  537. As a special case, a nil value prevents template from being
  538. inserted.
  539. Setting :latex-hyperref-template in publishing projects will take
  540. precedence over this variable."
  541. :group 'org-export-latex
  542. :version "25.1"
  543. :package-version '(Org . "8.3")
  544. :type '(choice (const :tag "No template" nil)
  545. (string :tag "Format string")))
  546. (define-obsolete-variable-alias
  547. 'org-latex-with-hyperref 'org-latex-hyperref-template "25.1")
  548. ;;;; Headline
  549. (defcustom org-latex-format-headline-function
  550. 'org-latex-format-headline-default-function
  551. "Function for formatting the headline's text.
  552. This function will be called with six arguments:
  553. TODO the todo keyword (string or nil)
  554. TODO-TYPE the type of todo (symbol: `todo', `done', nil)
  555. PRIORITY the priority of the headline (integer or nil)
  556. TEXT the main headline text (string)
  557. TAGS the tags (list of strings or nil)
  558. INFO the export options (plist)
  559. The function result will be used in the section format string."
  560. :group 'org-export-latex
  561. :version "24.4"
  562. :package-version '(Org . "8.0")
  563. :type 'function)
  564. ;;;; Footnotes
  565. (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
  566. "Text used to separate footnotes."
  567. :group 'org-export-latex
  568. :type 'string)
  569. ;;;; Timestamps
  570. (defcustom org-latex-active-timestamp-format "\\textit{%s}"
  571. "A printf format string to be applied to active timestamps."
  572. :group 'org-export-latex
  573. :type 'string)
  574. (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
  575. "A printf format string to be applied to inactive timestamps."
  576. :group 'org-export-latex
  577. :type 'string)
  578. (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
  579. "A printf format string to be applied to diary timestamps."
  580. :group 'org-export-latex
  581. :type 'string)
  582. ;;;; Links
  583. (defcustom org-latex-image-default-option ""
  584. "Default option for images."
  585. :group 'org-export-latex
  586. :version "24.4"
  587. :package-version '(Org . "8.0")
  588. :type 'string)
  589. (defcustom org-latex-image-default-width ".9\\linewidth"
  590. "Default width for images.
  591. This value will not be used if a height is provided."
  592. :group 'org-export-latex
  593. :version "24.4"
  594. :package-version '(Org . "8.0")
  595. :type 'string)
  596. (defcustom org-latex-image-default-height ""
  597. "Default height for images.
  598. This value will not be used if a width is provided, or if the
  599. image is wrapped within a \"figure\" or \"wrapfigure\"
  600. environment."
  601. :group 'org-export-latex
  602. :version "24.4"
  603. :package-version '(Org . "8.0")
  604. :type 'string)
  605. (defcustom org-latex-default-figure-position "htb"
  606. "Default position for latex figures."
  607. :group 'org-export-latex
  608. :type 'string)
  609. (defcustom org-latex-inline-image-rules
  610. '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
  611. "Rules characterizing image files that can be inlined into LaTeX.
  612. A rule consists in an association whose key is the type of link
  613. to consider, and value is a regexp that will be matched against
  614. link's path.
  615. Note that, by default, the image extension *actually* allowed
  616. depend on the way the LaTeX file is processed. When used with
  617. pdflatex, pdf, jpg and png images are OK. When processing
  618. through dvi to Postscript, only ps and eps are allowed. The
  619. default we use here encompasses both."
  620. :group 'org-export-latex
  621. :version "24.4"
  622. :package-version '(Org . "8.0")
  623. :type '(alist :key-type (string :tag "Type")
  624. :value-type (regexp :tag "Path")))
  625. (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
  626. "Format string for links with unknown path type."
  627. :group 'org-export-latex
  628. :type 'string)
  629. ;;;; Tables
  630. (defcustom org-latex-default-table-environment "tabular"
  631. "Default environment used to build tables."
  632. :group 'org-export-latex
  633. :version "24.4"
  634. :package-version '(Org . "8.0")
  635. :type 'string)
  636. (defcustom org-latex-default-table-mode 'table
  637. "Default mode for tables.
  638. Value can be a symbol among:
  639. `table' Regular LaTeX table.
  640. `math' In this mode, every cell is considered as being in math
  641. mode and the complete table will be wrapped within a math
  642. environment. It is particularly useful to write matrices.
  643. `inline-math' This mode is almost the same as `math', but the
  644. math environment will be inlined.
  645. `verbatim' The table is exported as it appears in the Org
  646. buffer, within a verbatim environment.
  647. This value can be overridden locally with, i.e. \":mode math\" in
  648. LaTeX attributes.
  649. When modifying this variable, it may be useful to change
  650. `org-latex-default-table-environment' accordingly."
  651. :group 'org-export-latex
  652. :version "24.4"
  653. :package-version '(Org . "8.0")
  654. :type '(choice (const :tag "Table" table)
  655. (const :tag "Matrix" math)
  656. (const :tag "Inline matrix" inline-math)
  657. (const :tag "Verbatim" verbatim))
  658. :safe (lambda (s) (memq s '(table math inline-math verbatim))))
  659. (defcustom org-latex-tables-centered t
  660. "When non-nil, tables are exported in a center environment."
  661. :group 'org-export-latex
  662. :type 'boolean
  663. :safe #'booleanp)
  664. (defcustom org-latex-tables-booktabs nil
  665. "When non-nil, display tables in a formal \"booktabs\" style.
  666. This option assumes that the \"booktabs\" package is properly
  667. loaded in the header of the document. This value can be ignored
  668. locally with \":booktabs t\" and \":booktabs nil\" LaTeX
  669. attributes."
  670. :group 'org-export-latex
  671. :version "24.4"
  672. :package-version '(Org . "8.0")
  673. :type 'boolean
  674. :safe #'booleanp)
  675. (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
  676. "Format string to display numbers in scientific notation.
  677. The format should have \"%s\" twice, for mantissa and exponent
  678. \(i.e., \"%s\\\\times10^{%s}\").
  679. When nil, no transformation is made."
  680. :group 'org-export-latex
  681. :version "24.4"
  682. :package-version '(Org . "8.0")
  683. :type '(choice
  684. (string :tag "Format string")
  685. (const :tag "No formatting" nil)))
  686. ;;;; Text markup
  687. (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
  688. (code . protectedtexttt)
  689. (italic . "\\emph{%s}")
  690. (strike-through . "\\sout{%s}")
  691. (underline . "\\uline{%s}")
  692. (verbatim . protectedtexttt))
  693. "Alist of LaTeX expressions to convert text markup.
  694. The key must be a symbol among `bold', `code', `italic',
  695. `strike-through', `underline' and `verbatim'. The value is
  696. a formatting string to wrap fontified text with.
  697. Value can also be set to the following symbols: `verb' and
  698. `protectedtexttt'. For the former, Org will use \"\\verb\" to
  699. create a format string and select a delimiter character that
  700. isn't in the string. For the latter, Org will use \"\\texttt\"
  701. to typeset and try to protect special characters.
  702. If no association can be found for a given markup, text will be
  703. returned as-is."
  704. :group 'org-export-latex
  705. :version "25.1"
  706. :package-version '(Org . "8.3")
  707. :type 'alist
  708. :options '(bold code italic strike-through underline verbatim))
  709. ;;;; Drawers
  710. (defcustom org-latex-format-drawer-function (lambda (_ contents) contents)
  711. "Function called to format a drawer in LaTeX code.
  712. The function must accept two parameters:
  713. NAME the drawer name, like \"LOGBOOK\"
  714. CONTENTS the contents of the drawer.
  715. The function should return the string to be exported.
  716. The default function simply returns the value of CONTENTS."
  717. :group 'org-export-latex
  718. :version "24.4"
  719. :package-version '(Org . "8.3")
  720. :type 'function)
  721. ;;;; Inlinetasks
  722. (defcustom org-latex-format-inlinetask-function
  723. 'org-latex-format-inlinetask-default-function
  724. "Function called to format an inlinetask in LaTeX code.
  725. The function must accept seven parameters:
  726. TODO the todo keyword (string or nil)
  727. TODO-TYPE the todo type (symbol: `todo', `done', nil)
  728. PRIORITY the inlinetask priority (integer or nil)
  729. NAME the inlinetask name (string)
  730. TAGS the inlinetask tags (list of strings or nil)
  731. CONTENTS the contents of the inlinetask (string or nil)
  732. INFO the export options (plist)
  733. The function should return the string to be exported."
  734. :group 'org-export-latex
  735. :type 'function
  736. :version "25.1"
  737. :package-version '(Org . "8.3"))
  738. ;; Src blocks
  739. (defcustom org-latex-listings nil
  740. "Non-nil means export source code using the listings package.
  741. This package will fontify source code, possibly even with color.
  742. If you want to use this, you also need to make LaTeX use the
  743. listings package, and if you want to have color, the color
  744. package. Just add these to `org-latex-packages-alist', for
  745. example using customize, or with something like:
  746. (require \\='ox-latex)
  747. (add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
  748. (add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
  749. Alternatively,
  750. (setq org-latex-listings \\='minted)
  751. causes source code to be exported using the minted package as
  752. opposed to listings. If you want to use minted, you need to add
  753. the minted package to `org-latex-packages-alist', for example
  754. using customize, or with
  755. (require \\='ox-latex)
  756. (add-to-list \\='org-latex-packages-alist \\='(\"newfloat\" \"minted\"))
  757. In addition, it is necessary to install pygments
  758. \(http://pygments.org), and to configure the variable
  759. `org-latex-pdf-process' so that the -shell-escape option is
  760. passed to pdflatex.
  761. The minted choice has possible repercussions on the preview of
  762. latex fragments (see `org-preview-latex-fragment'). If you run
  763. into previewing problems, please consult
  764. http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
  765. :group 'org-export-latex
  766. :type '(choice
  767. (const :tag "Use listings" t)
  768. (const :tag "Use minted" minted)
  769. (const :tag "Export verbatim" nil))
  770. :safe (lambda (s) (memq s '(t nil minted))))
  771. (defcustom org-latex-listings-langs
  772. '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
  773. (c "C") (cc "C++")
  774. (fortran "fortran")
  775. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  776. (html "HTML") (xml "XML")
  777. (tex "TeX") (latex "[LaTeX]TeX")
  778. (shell-script "bash")
  779. (gnuplot "Gnuplot")
  780. (ocaml "Caml") (caml "Caml")
  781. (sql "SQL") (sqlite "sql")
  782. (makefile "make")
  783. (R "r"))
  784. "Alist mapping languages to their listing language counterpart.
  785. The key is a symbol, the major mode symbol without the \"-mode\".
  786. The value is the string that should be inserted as the language
  787. parameter for the listings package. If the mode name and the
  788. listings name are the same, the language does not need an entry
  789. in this list - but it does not hurt if it is present."
  790. :group 'org-export-latex
  791. :version "24.4"
  792. :package-version '(Org . "8.3")
  793. :type '(repeat
  794. (list
  795. (symbol :tag "Major mode ")
  796. (string :tag "Listings language"))))
  797. (defcustom org-latex-listings-options nil
  798. "Association list of options for the latex listings package.
  799. These options are supplied as a comma-separated list to the
  800. \\lstset command. Each element of the association list should be
  801. a list containing two strings: the name of the option, and the
  802. value. For example,
  803. (setq org-latex-listings-options
  804. \\='((\"basicstyle\" \"\\\\small\")
  805. (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
  806. will typeset the code in a small size font with underlined, bold
  807. black keywords.
  808. Note that the same options will be applied to blocks of all
  809. languages. If you need block-specific options, you may use the
  810. following syntax:
  811. #+ATTR_LATEX: :options key1=value1,key2=value2
  812. #+BEGIN_SRC <LANG>
  813. ...
  814. #+END_SRC"
  815. :group 'org-export-latex
  816. :type '(repeat
  817. (list
  818. (string :tag "Listings option name ")
  819. (string :tag "Listings option value"))))
  820. (defcustom org-latex-minted-langs
  821. '((emacs-lisp "common-lisp")
  822. (cc "c++")
  823. (cperl "perl")
  824. (shell-script "bash")
  825. (caml "ocaml"))
  826. "Alist mapping languages to their minted language counterpart.
  827. The key is a symbol, the major mode symbol without the \"-mode\".
  828. The value is the string that should be inserted as the language
  829. parameter for the minted package. If the mode name and the
  830. listings name are the same, the language does not need an entry
  831. in this list - but it does not hurt if it is present.
  832. Note that minted uses all lower case for language identifiers,
  833. and that the full list of language identifiers can be obtained
  834. with:
  835. pygmentize -L lexers"
  836. :group 'org-export-latex
  837. :type '(repeat
  838. (list
  839. (symbol :tag "Major mode ")
  840. (string :tag "Minted language"))))
  841. (defcustom org-latex-minted-options nil
  842. "Association list of options for the latex minted package.
  843. These options are supplied within square brackets in
  844. \\begin{minted} environments. Each element of the alist should
  845. be a list containing two strings: the name of the option, and the
  846. value. For example,
  847. (setq org-latex-minted-options
  848. '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
  849. will result in src blocks being exported with
  850. \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
  851. as the start of the minted environment. Note that the same
  852. options will be applied to blocks of all languages. If you need
  853. block-specific options, you may use the following syntax:
  854. #+ATTR_LATEX: :options key1=value1,key2=value2
  855. #+BEGIN_SRC <LANG>
  856. ...
  857. #+END_SRC"
  858. :group 'org-export-latex
  859. :type '(repeat
  860. (list
  861. (string :tag "Minted option name ")
  862. (string :tag "Minted option value"))))
  863. (defvar org-latex-custom-lang-environments nil
  864. "Alist mapping languages to language-specific LaTeX environments.
  865. It is used during export of src blocks by the listings and minted
  866. latex packages. The environment may be a simple string, composed of
  867. only letters and numbers. In this case, the string is directly the
  868. name of the latex environment to use. The environment may also be
  869. a format string. In this case the format string will be directly
  870. exported. This format string may contain these elements:
  871. %s for the formatted source
  872. %c for the caption
  873. %f for the float attribute
  874. %l for an appropriate label
  875. For example,
  876. (setq org-latex-custom-lang-environments
  877. '((python \"pythoncode\")
  878. (ocaml \"\\\\begin{listing}
  879. \\\\begin{minted}{ocaml}
  880. %s\\\\end{minted}
  881. \\\\caption{%c}
  882. \\\\label{%l}\")))
  883. would have the effect that if Org encounters a Python source block
  884. during LaTeX export it will produce
  885. \\begin{pythoncode}
  886. <src block body>
  887. \\end{pythoncode}
  888. and if Org encounters an Ocaml source block during LaTeX export it
  889. will produce
  890. \\begin{listing}
  891. \\begin{minted}{ocaml}
  892. <src block body>
  893. \\end{minted}
  894. \\caption{<caption>}
  895. \\label{<label>}
  896. \\end{listing}")
  897. ;;;; Compilation
  898. (defcustom org-latex-compiler-file-string "%% Indented LaTeX compiler: %s\n"
  899. "LaTeX compiler format-string.
  900. See also `org-latex-compiler'."
  901. :group 'org-export-latex
  902. :type '(choice
  903. (const :tag "Comment" "%% Indented LaTeX compiler: %s\n")
  904. (const :tag "latex-mode file variable" "%% -*- latex-run-command: %s -*-\n")
  905. (const :tag "AUCTeX file variable" "%% -*- LaTeX-command: %s -*-\n")
  906. (string :tag "custom format" "%% %s"))
  907. :version "25.1"
  908. :package-version '(Org . "9.0"))
  909. (defcustom org-latex-compiler "pdflatex"
  910. "LaTeX compiler to use.
  911. Must be an element in `org-latex-compilers' or the empty quote.
  912. Can also be set in buffers via #+LATEX_COMPILER. See also
  913. `org-latex-compiler-file-string'."
  914. :group 'org-export-latex
  915. :type '(choice
  916. (const :tag "pdfLaTeX" "pdflatex")
  917. (const :tag "XeLaTeX" "xelatex")
  918. (const :tag "LuaLaTeX" "lualatex")
  919. (const :tag "Unset" ""))
  920. :version "25.1"
  921. :package-version '(Org . "9.0"))
  922. (defconst org-latex-compilers '("pdflatex" "xelatex" "lualatex")
  923. "Known LaTeX compilers.
  924. See also `org-latex-compiler'.")
  925. (defcustom org-latex-bib-compiler "bibtex"
  926. "Command to process a LaTeX file's bibliography.
  927. The shorthand %bib in `org-latex-pdf-process' is replaced with
  928. this value.
  929. A better approach is to use a compiler suit such as `latexmk'."
  930. :group 'org-export-latex
  931. :type '(choice (const :tag "BibTeX" "bibtex")
  932. (const :tag "Biber" "biber")
  933. (string :tag "Other process"))
  934. :version "25.1"
  935. :package-version '(Org . "9.0"))
  936. (defcustom org-latex-pdf-process
  937. '("%latex -interaction nonstopmode -output-directory %o %f"
  938. "%latex -interaction nonstopmode -output-directory %o %f"
  939. "%latex -interaction nonstopmode -output-directory %o %f")
  940. "Commands to process a LaTeX file to a PDF file.
  941. This is a list of strings, each of them will be given to the
  942. shell as a command. %f in the command will be replaced by the
  943. full file name, %b by the file base name (i.e. without directory
  944. and extension parts), %o by the base directory of the file,
  945. %latex is the LaTeX compiler (see `org-latex-compiler'), and %bib
  946. is the BibTeX-like compiler (see `org-latex-bib-compiler').
  947. The reason why this is a list is that it usually takes several
  948. runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
  949. does not have a clever mechanism to detect which of these
  950. commands have to be run to get to a stable result, and it also
  951. does not do any error checking.
  952. Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
  953. which calls the \"correct\" combinations of auxiliary programs.
  954. Alternatively, this may be a Lisp function that does the
  955. processing, so you could use this to apply the machinery of
  956. AUCTeX or the Emacs LaTeX mode. This function should accept the
  957. file name as its single argument."
  958. :group 'org-export-pdf
  959. :type '(choice
  960. (repeat :tag "Shell command sequence"
  961. (string :tag "Shell command"))
  962. (const :tag "2 runs of latex"
  963. ("%latex -interaction nonstopmode -output-directory %o %f"
  964. "%latex -interaction nonstopmode -output-directory %o %f"))
  965. (const :tag "3 runs of latex"
  966. ("%latex -interaction nonstopmode -output-directory %o %f"
  967. "%latex -interaction nonstopmode -output-directory %o %f"
  968. "%latex -interaction nonstopmode -output-directory %o %f"))
  969. (const :tag "latex,bibtex,latex,latex"
  970. ("%latex -interaction nonstopmode -output-directory %o %f"
  971. "%bib %b"
  972. "%latex -interaction nonstopmode -output-directory %o %f"
  973. "%latex -interaction nonstopmode -output-directory %o %f"))
  974. (const :tag "texi2dvi"
  975. ("LATEX=\"%latex\" texi2dvi -p -b -V %f"))
  976. (const :tag "latexmk"
  977. ("latexmk -g -pdflatex=\"%latex\" %f"))
  978. (function)))
  979. (defcustom org-latex-logfiles-extensions
  980. '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
  981. "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
  982. "The list of file extensions to consider as LaTeX logfiles.
  983. The logfiles will be removed if `org-latex-remove-logfiles' is
  984. non-nil."
  985. :group 'org-export-latex
  986. :version "25.1"
  987. :package-version '(Org . "8.3")
  988. :type '(repeat (string :tag "Extension")))
  989. (defcustom org-latex-remove-logfiles t
  990. "Non-nil means remove the logfiles produced by PDF production.
  991. By default, logfiles are files with these extensions: .aux, .idx,
  992. .log, .out, .toc, .nav, .snm and .vrb. To define the set of
  993. logfiles to remove, set `org-latex-logfiles-extensions'."
  994. :group 'org-export-latex
  995. :type 'boolean)
  996. (defcustom org-latex-known-warnings
  997. '(("Reference.*?undefined" . "[undefined reference]")
  998. ("Runaway argument" . "[runaway argument]")
  999. ("Underfull \\hbox" . "[underfull hbox]")
  1000. ("Overfull \\hbox" . "[overfull hbox]")
  1001. ("Citation.*?undefined" . "[undefined citation]")
  1002. ("Undefined control sequence" . "[undefined control sequence]"))
  1003. "Alist of regular expressions and associated messages for the user.
  1004. The regular expressions are used to find possible warnings in the
  1005. log of a latex-run. These warnings will be reported after
  1006. calling `org-latex-compile'."
  1007. :group 'org-export-latex
  1008. :version "25.1"
  1009. :package-version '(Org . "8.3")
  1010. :type '(repeat
  1011. (cons
  1012. (string :tag "Regexp")
  1013. (string :tag "Message"))))
  1014. ;;; Internal Functions
  1015. (defun org-latex--caption-above-p (element info)
  1016. "Non nil when caption is expected to be located above ELEMENT.
  1017. INFO is a plist holding contextual information."
  1018. (let ((above (plist-get info :latex-caption-above)))
  1019. (if (symbolp above) above
  1020. (let ((type (org-element-type element)))
  1021. (memq (if (eq type 'link) 'image type) above)))))
  1022. (defun org-latex--label (datum info &optional force full)
  1023. "Return an appropriate label for DATUM.
  1024. DATUM is an element or a `target' type object. INFO is the
  1025. current export state, as a plist.
  1026. Return nil if element DATUM has no NAME or VALUE affiliated
  1027. keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
  1028. this case always return a unique label.
  1029. Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
  1030. (let* ((type (org-element-type datum))
  1031. (user-label
  1032. (org-element-property
  1033. (case type
  1034. ((headline inlinetask) :CUSTOM_ID)
  1035. (target :value)
  1036. (otherwise :name))
  1037. datum))
  1038. (label
  1039. (and (or user-label force)
  1040. (if (and user-label (plist-get info :latex-prefer-user-labels))
  1041. user-label
  1042. (concat (case type
  1043. (headline "sec:")
  1044. (table "tab:")
  1045. (latex-environment
  1046. (and (org-string-match-p
  1047. org-latex-math-environments-re
  1048. (org-element-property :value datum))
  1049. "eq:"))
  1050. (paragraph
  1051. (and (org-element-property :caption datum)
  1052. "fig:")))
  1053. (org-export-get-reference datum info))))))
  1054. (cond ((not full) label)
  1055. (label (format "\\label{%s}%s"
  1056. label
  1057. (if (eq type 'target) "" "\n")))
  1058. (t ""))))
  1059. (defun org-latex--caption/label-string (element info)
  1060. "Return caption and label LaTeX string for ELEMENT.
  1061. INFO is a plist holding contextual information. If there's no
  1062. caption nor label, return the empty string.
  1063. For non-floats, see `org-latex--wrap-label'."
  1064. (let* ((label (org-latex--label element info nil t))
  1065. (main (org-export-get-caption element))
  1066. (attr (org-export-read-attribute :attr_latex element))
  1067. (type (org-element-type element))
  1068. (nonfloat (or (and (plist-member attr :float)
  1069. (not (plist-get attr :float))
  1070. main)
  1071. (and (eq type 'src-block)
  1072. (not (plist-get attr :float))
  1073. (memq (plist-get info :latex-listings)
  1074. '(nil minted)))))
  1075. (short (org-export-get-caption element t))
  1076. (caption-from-attr-latex (plist-get attr :caption)))
  1077. (cond
  1078. ((org-string-nw-p caption-from-attr-latex)
  1079. (concat caption-from-attr-latex "\n"))
  1080. ((and (not main) (equal label "")) "")
  1081. ((not main) (concat label "\n"))
  1082. ;; Option caption format with short name.
  1083. (t
  1084. (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
  1085. "\\caption%s%s{%s%s}\n")
  1086. (if nonfloat
  1087. (case type
  1088. (paragraph "figure")
  1089. (src-block (if (plist-get info :latex-listings)
  1090. "listing"
  1091. "figure"))
  1092. (t (symbol-name type)))
  1093. "")
  1094. (if short (format "[%s]" (org-export-data short info)) "")
  1095. label
  1096. (org-export-data main info))))))
  1097. (defun org-latex-guess-inputenc (header)
  1098. "Set the coding system in inputenc to what the buffer is.
  1099. HEADER is the LaTeX header string. This function only applies
  1100. when specified inputenc option is \"AUTO\".
  1101. Return the new header, as a string."
  1102. (let* ((cs (or (ignore-errors
  1103. (latexenc-coding-system-to-inputenc
  1104. (or org-export-coding-system buffer-file-coding-system)))
  1105. "utf8")))
  1106. (if (not cs) header
  1107. ;; First translate if that is requested.
  1108. (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
  1109. ;; Then find the \usepackage statement and replace the option.
  1110. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  1111. cs header t nil 1))))
  1112. (defun org-latex-guess-babel-language (header info)
  1113. "Set Babel's language according to LANGUAGE keyword.
  1114. HEADER is the LaTeX header string. INFO is the plist used as
  1115. a communication channel.
  1116. Insertion of guessed language only happens when Babel package has
  1117. explicitly been loaded. Then it is added to the rest of
  1118. package's options.
  1119. The argument to Babel may be \"AUTO\" which is then replaced with
  1120. the language of the document or `org-export-default-language'
  1121. unless language in question is already loaded.
  1122. Return the new header."
  1123. (let ((language-code (plist-get info :language)))
  1124. ;; If no language is set or Babel package is not loaded, return
  1125. ;; HEADER as-is.
  1126. (if (or (not (stringp language-code))
  1127. (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
  1128. header
  1129. (let ((options (save-match-data
  1130. (org-split-string (match-string 1 header) ",[ \t]*")))
  1131. (language (cdr (assoc language-code
  1132. org-latex-babel-language-alist))))
  1133. ;; If LANGUAGE is already loaded, return header without AUTO.
  1134. ;; Otherwise, replace AUTO with language or append language if
  1135. ;; AUTO is not present.
  1136. (replace-match
  1137. (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
  1138. (cond ((member language options) (delete "AUTO" options))
  1139. ((member "AUTO" options) options)
  1140. (t (append options (list language))))
  1141. ", ")
  1142. t nil header 1)))))
  1143. (defun org-latex-guess-polyglossia-language (header info)
  1144. "Set the Polyglossia language according to the LANGUAGE keyword.
  1145. HEADER is the LaTeX header string. INFO is the plist used as
  1146. a communication channel.
  1147. Insertion of guessed language only happens when the Polyglossia
  1148. package has been explicitly loaded.
  1149. The argument to Polyglossia may be \"AUTO\" which is then
  1150. replaced with the language of the document or
  1151. `org-export-default-language'. Note, the language is really set
  1152. using \setdefaultlanguage and not as an option to the package.
  1153. Return the new header."
  1154. (let ((language (plist-get info :language)))
  1155. ;; If no language is set or Polyglossia is not loaded, return
  1156. ;; HEADER as-is.
  1157. (if (or (not (stringp language))
  1158. (not (string-match
  1159. "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
  1160. header)))
  1161. header
  1162. (let* ((options (org-string-nw-p (match-string 1 header)))
  1163. (languages (and options
  1164. ;; Reverse as the last loaded language is
  1165. ;; the main language.
  1166. (nreverse
  1167. (delete-dups
  1168. (save-match-data
  1169. (org-split-string
  1170. (replace-regexp-in-string
  1171. "AUTO" language options t)
  1172. ",[ \t]*"))))))
  1173. (main-language-set
  1174. (string-match-p "\\\\setmainlanguage{.*?}" header)))
  1175. (replace-match
  1176. (concat "\\usepackage{polyglossia}\n"
  1177. (mapconcat
  1178. (lambda (l)
  1179. (let ((l (or (assoc l org-latex-polyglossia-language-alist)
  1180. l)))
  1181. (format (if main-language-set "\\setotherlanguage%s{%s}\n"
  1182. (setq main-language-set t)
  1183. "\\setmainlanguage%s{%s}\n")
  1184. (if (and (consp l) (= (length l) 3))
  1185. (format "[variant=%s]" (nth 2 l))
  1186. "")
  1187. (nth 1 l))))
  1188. languages
  1189. ""))
  1190. t t header 0)))))
  1191. (defun org-latex--remove-packages (pkg-alist info)
  1192. "Remove packages based on the current LaTeX compiler.
  1193. If the fourth argument of an element is set in pkg-alist, and it
  1194. is not a member of the LaTeX compiler of the document, the packages
  1195. is removed. See also `org-latex-compiler'.
  1196. Return modified pkg-alist."
  1197. (let ((compiler (or (plist-get info :latex-compiler) "")))
  1198. (if (member-ignore-case compiler org-latex-compilers)
  1199. (delq nil
  1200. (mapcar
  1201. (lambda (pkg)
  1202. (unless (and
  1203. (listp pkg)
  1204. (let ((third (nth 3 pkg)))
  1205. (and third
  1206. (not (member-ignore-case
  1207. compiler
  1208. (if (listp third) third (list third)))))))
  1209. pkg))
  1210. pkg-alist))
  1211. pkg-alist)))
  1212. (defun org-latex--find-verb-separator (s)
  1213. "Return a character not used in string S.
  1214. This is used to choose a separator for constructs like \\verb."
  1215. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1216. (loop for c across ll
  1217. when (not (string-match (regexp-quote (char-to-string c)) s))
  1218. return (char-to-string c))))
  1219. (defun org-latex--make-option-string (options)
  1220. "Return a comma separated string of keywords and values.
  1221. OPTIONS is an alist where the key is the options keyword as
  1222. a string, and the value a list containing the keyword value, or
  1223. nil."
  1224. (mapconcat (lambda (pair)
  1225. (concat (first pair)
  1226. (when (> (length (second pair)) 0)
  1227. (concat "=" (second pair)))))
  1228. options
  1229. ","))
  1230. (defun org-latex--wrap-label (element output info)
  1231. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  1232. INFO is the current export state, as a plist. This function
  1233. should not be used for floats. See
  1234. `org-latex--caption/label-string'."
  1235. (if (not (and (org-string-nw-p output) (org-element-property :name element)))
  1236. output
  1237. (concat (format "\\phantomsection\n\\label{%s}\n"
  1238. (org-latex--label element info))
  1239. output)))
  1240. (defun org-latex--protect-text (text)
  1241. "Protect special characters in string TEXT and return it."
  1242. (replace-regexp-in-string
  1243. "--\\|[\\{}$%&_#~^]"
  1244. (lambda (m)
  1245. (cond ((equal m "--") "-{}-")
  1246. ((equal m "\\") "\\textbackslash{}")
  1247. ((equal m "~") "\\textasciitilde{}")
  1248. ((equal m "^") "\\textasciicircum{}")
  1249. (t (concat "\\" m))))
  1250. text nil t))
  1251. (defun org-latex--text-markup (text markup info)
  1252. "Format TEXT depending on MARKUP text markup.
  1253. INFO is a plist used as a communication channel. See
  1254. `org-latex-text-markup-alist' for details."
  1255. (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
  1256. (case fmt
  1257. ;; No format string: Return raw text.
  1258. ((nil) text)
  1259. ;; Handle the `verb' special case: Find an appropriate separator
  1260. ;; and use "\\verb" command.
  1261. (verb
  1262. (let ((separator (org-latex--find-verb-separator text)))
  1263. (concat "\\verb" separator
  1264. (replace-regexp-in-string "\n" " " text)
  1265. separator)))
  1266. ;; Handle the `protectedtexttt' special case: Protect some
  1267. ;; special chars and use "\texttt{%s}" format string.
  1268. (protectedtexttt
  1269. (format "\\texttt{%s}" (org-latex--protect-text text)))
  1270. ;; Else use format string.
  1271. (t (format fmt text)))))
  1272. (defun org-latex--delayed-footnotes-definitions (element info)
  1273. "Return footnotes definitions in ELEMENT as a string.
  1274. INFO is a plist used as a communication channel.
  1275. Footnotes definitions are returned within \"\\footnotetxt{}\"
  1276. commands.
  1277. This function is used within constructs that don't support
  1278. \"\\footnote{}\" command (i.e. an item's tag). In that case,
  1279. \"\\footnotemark\" is used within the construct and the function
  1280. just outside of it."
  1281. (mapconcat
  1282. (lambda (ref)
  1283. (format
  1284. "\\footnotetext[%s]{%s}"
  1285. (org-export-get-footnote-number ref info)
  1286. (org-trim
  1287. (org-export-data
  1288. (org-export-get-footnote-definition ref info) info))))
  1289. ;; Find every footnote reference in ELEMENT.
  1290. (letrec ((all-refs)
  1291. (search-refs
  1292. (lambda (data)
  1293. ;; Return a list of all footnote references never seen
  1294. ;; before in DATA.
  1295. (org-element-map data 'footnote-reference
  1296. (lambda (ref)
  1297. (when (org-export-footnote-first-reference-p ref info)
  1298. (push ref all-refs)
  1299. (when (eq (org-element-property :type ref) 'standard)
  1300. (funcall search-refs
  1301. (org-export-get-footnote-definition ref info)))))
  1302. info)
  1303. (reverse all-refs))))
  1304. (funcall search-refs element))
  1305. ""))
  1306. (defun org-latex--translate (s info)
  1307. "Translate string S according to specified language.
  1308. INFO is a plist used as a communication channel."
  1309. (org-export-translate s :latex info))
  1310. (defun org-latex--format-spec (info)
  1311. "Create a format-spec for document meta-data.
  1312. INFO is a plist used as a communication channel."
  1313. (let ((language (let ((lang (plist-get info :language)))
  1314. (or (cdr (assoc lang org-latex-babel-language-alist))
  1315. lang))))
  1316. `((?a . ,(org-export-data (plist-get info :author) info))
  1317. (?t . ,(org-export-data (plist-get info :title) info))
  1318. (?k . ,(org-export-data (org-latex--wrap-latex-math-block
  1319. (plist-get info :keywords) info)
  1320. info))
  1321. (?d . ,(org-export-data (org-latex--wrap-latex-math-block
  1322. (plist-get info :description) info)
  1323. info))
  1324. (?c . ,(plist-get info :creator))
  1325. (?l . ,language)
  1326. (?L . ,(capitalize language))
  1327. (?D . ,(org-export-get-date info)))))
  1328. (defun org-latex--make-preamble (info)
  1329. "Return a formatted LaTeX preamble.
  1330. INFO is a plist used as a communication channel."
  1331. (let* ((class (plist-get info :latex-class))
  1332. (class-options (plist-get info :latex-class-options))
  1333. (header (nth 1 (assoc class (plist-get info :latex-classes))))
  1334. (document-class-string
  1335. (and (stringp header)
  1336. (if (not class-options) header
  1337. (replace-regexp-in-string
  1338. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  1339. class-options header t nil 1)))))
  1340. (if (not document-class-string)
  1341. (user-error "Unknown LaTeX class `%s'" class)
  1342. (org-latex-guess-polyglossia-language
  1343. (org-latex-guess-babel-language
  1344. (org-latex-guess-inputenc
  1345. (org-element-normalize-string
  1346. (org-splice-latex-header
  1347. document-class-string
  1348. (org-latex--remove-packages
  1349. org-latex-default-packages-alist info)
  1350. (org-latex--remove-packages
  1351. org-latex-packages-alist info)
  1352. nil
  1353. (mapconcat 'org-element-normalize-string
  1354. (list (plist-get info :latex-header)
  1355. (plist-get info :latex-header-extra)) ""))))
  1356. info)
  1357. info))))
  1358. (defun org-latex--insert-compiler (info)
  1359. "Insert LaTeX_compiler info into the document.
  1360. INFO is a plist used as a communication channel."
  1361. (let ((compiler (plist-get info :latex-compiler)))
  1362. (and (org-string-nw-p org-latex-compiler-file-string)
  1363. (string-match-p (regexp-opt org-latex-compilers) (or compiler ""))
  1364. (format org-latex-compiler-file-string compiler))))
  1365. ;;; Template
  1366. (defun org-latex-template (contents info)
  1367. "Return complete document string after LaTeX conversion.
  1368. CONTENTS is the transcoded contents string. INFO is a plist
  1369. holding export options."
  1370. (let ((title (org-export-data (plist-get info :title) info))
  1371. (spec (org-latex--format-spec info)))
  1372. (concat
  1373. ;; Time-stamp.
  1374. (and (plist-get info :time-stamp-file)
  1375. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  1376. ;; LaTeX compiler.
  1377. (org-latex--insert-compiler info)
  1378. ;; Document class and packages.
  1379. (org-latex--make-preamble info)
  1380. ;; Possibly limit depth for headline numbering.
  1381. (let ((sec-num (plist-get info :section-numbers)))
  1382. (when (integerp sec-num)
  1383. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  1384. ;; Author.
  1385. (let ((author (and (plist-get info :with-author)
  1386. (let ((auth (plist-get info :author)))
  1387. (and auth (org-export-data auth info)))))
  1388. (email (and (plist-get info :with-email)
  1389. (org-export-data (plist-get info :email) info))))
  1390. (cond ((and author email (not (string= "" email)))
  1391. (format "\\author{%s\\thanks{%s}}\n" author email))
  1392. ((or author email) (format "\\author{%s}\n" (or author email)))))
  1393. ;; Date.
  1394. (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
  1395. (format "\\date{%s}\n" (org-export-data date info)))
  1396. ;; Title and subtitle.
  1397. (let* ((subtitle (plist-get info :subtitle))
  1398. (formatted-subtitle
  1399. (when subtitle
  1400. (format (plist-get info :latex-subtitle-format)
  1401. (org-export-data subtitle info))))
  1402. (separate (plist-get info :latex-subtitle-separate)))
  1403. (concat
  1404. (format "\\title{%s%s}\n" title
  1405. (if separate "" (or formatted-subtitle "")))
  1406. (when (and separate subtitle)
  1407. (concat formatted-subtitle "\n"))))
  1408. ;; Hyperref options.
  1409. (let ((template (plist-get info :latex-hyperref-template)))
  1410. (and (stringp template)
  1411. (format-spec template spec)))
  1412. ;; Document start.
  1413. "\\begin{document}\n\n"
  1414. ;; Title command.
  1415. (let* ((title-command (plist-get info :latex-title-command))
  1416. (command (and (stringp title-command)
  1417. (format-spec title-command spec))))
  1418. (org-element-normalize-string
  1419. (cond ((not (plist-get info :with-title)) nil)
  1420. ((string= "" title) nil)
  1421. ((not (stringp command)) nil)
  1422. ((string-match "\\(?:[^%]\\|^\\)%s" command)
  1423. (format command title))
  1424. (t command))))
  1425. ;; Table of contents.
  1426. (let ((depth (plist-get info :with-toc)))
  1427. (when depth
  1428. (concat (when (wholenump depth)
  1429. (format "\\setcounter{tocdepth}{%d}\n" depth))
  1430. (plist-get info :latex-toc-command))))
  1431. ;; Document's body.
  1432. contents
  1433. ;; Creator.
  1434. (and (plist-get info :with-creator)
  1435. (concat (plist-get info :creator) "\n"))
  1436. ;; Document end.
  1437. "\\end{document}")))
  1438. ;;; Transcode Functions
  1439. ;;;; Bold
  1440. (defun org-latex-bold (_bold contents info)
  1441. "Transcode BOLD from Org to LaTeX.
  1442. CONTENTS is the text with bold markup. INFO is a plist holding
  1443. contextual information."
  1444. (org-latex--text-markup contents 'bold info))
  1445. ;;;; Center Block
  1446. (defun org-latex-center-block (center-block contents info)
  1447. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  1448. CONTENTS holds the contents of the center block. INFO is a plist
  1449. holding contextual information."
  1450. (org-latex--wrap-label
  1451. center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
  1452. ;;;; Clock
  1453. (defun org-latex-clock (clock _contents info)
  1454. "Transcode a CLOCK element from Org to LaTeX.
  1455. CONTENTS is nil. INFO is a plist holding contextual
  1456. information."
  1457. (concat
  1458. "\\noindent"
  1459. (format "\\textbf{%s} " org-clock-string)
  1460. (format (plist-get info :latex-inactive-timestamp-format)
  1461. (concat (org-timestamp-translate (org-element-property :value clock))
  1462. (let ((time (org-element-property :duration clock)))
  1463. (and time (format " (%s)" time)))))
  1464. "\\\\"))
  1465. ;;;; Code
  1466. (defun org-latex-code (code _contents info)
  1467. "Transcode a CODE object from Org to LaTeX.
  1468. CONTENTS is nil. INFO is a plist used as a communication
  1469. channel."
  1470. (org-latex--text-markup (org-element-property :value code) 'code info))
  1471. ;;;; Drawer
  1472. (defun org-latex-drawer (drawer contents info)
  1473. "Transcode a DRAWER element from Org to LaTeX.
  1474. CONTENTS holds the contents of the block. INFO is a plist
  1475. holding contextual information."
  1476. (let* ((name (org-element-property :drawer-name drawer))
  1477. (output (funcall (plist-get info :latex-format-drawer-function)
  1478. name contents)))
  1479. (org-latex--wrap-label drawer output info)))
  1480. ;;;; Dynamic Block
  1481. (defun org-latex-dynamic-block (dynamic-block contents info)
  1482. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1483. CONTENTS holds the contents of the block. INFO is a plist
  1484. holding contextual information. See `org-export-data'."
  1485. (org-latex--wrap-label dynamic-block contents info))
  1486. ;;;; Entity
  1487. (defun org-latex-entity (entity _contents _info)
  1488. "Transcode an ENTITY object from Org to LaTeX.
  1489. CONTENTS are the definition itself. INFO is a plist holding
  1490. contextual information."
  1491. (org-element-property :latex entity))
  1492. ;;;; Example Block
  1493. (defun org-latex-example-block (example-block _contents info)
  1494. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1495. CONTENTS is nil. INFO is a plist holding contextual
  1496. information."
  1497. (when (org-string-nw-p (org-element-property :value example-block))
  1498. (let ((environment (or (org-export-read-attribute
  1499. :attr_latex example-block :environment)
  1500. "verbatim")))
  1501. (org-latex--wrap-label
  1502. example-block
  1503. (format "\\begin{%s}\n%s\\end{%s}"
  1504. environment
  1505. (org-export-format-code-default example-block info)
  1506. environment)
  1507. info))))
  1508. ;;;; Export Block
  1509. (defun org-latex-export-block (export-block _contents _info)
  1510. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1511. CONTENTS is nil. INFO is a plist holding contextual information."
  1512. (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
  1513. (org-remove-indentation (org-element-property :value export-block))))
  1514. ;;;; Export Snippet
  1515. (defun org-latex-export-snippet (export-snippet _contents _info)
  1516. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1517. CONTENTS is nil. INFO is a plist holding contextual information."
  1518. (when (eq (org-export-snippet-backend export-snippet) 'latex)
  1519. (org-element-property :value export-snippet)))
  1520. ;;;; Fixed Width
  1521. (defun org-latex-fixed-width (fixed-width _contents info)
  1522. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1523. CONTENTS is nil. INFO is a plist holding contextual information."
  1524. (org-latex--wrap-label
  1525. fixed-width
  1526. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1527. (org-remove-indentation
  1528. (org-element-property :value fixed-width)))
  1529. info))
  1530. ;;;; Footnote Reference
  1531. (defun org-latex-footnote-reference (footnote-reference _contents info)
  1532. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1533. CONTENTS is nil. INFO is a plist holding contextual information."
  1534. (concat
  1535. ;; Insert separator between two footnotes in a row.
  1536. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1537. (when (eq (org-element-type prev) 'footnote-reference)
  1538. (plist-get info :latex-footnote-separator)))
  1539. (cond
  1540. ;; Use \footnotemark if the footnote has already been defined.
  1541. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1542. (format "\\footnotemark[%s]{}"
  1543. (org-export-get-footnote-number footnote-reference info)))
  1544. ;; Use \footnotemark if reference is within another footnote
  1545. ;; reference, footnote definition or table cell.
  1546. ((org-element-lineage footnote-reference
  1547. '(footnote-reference footnote-definition table-cell))
  1548. "\\footnotemark")
  1549. ;; Otherwise, define it with \footnote command.
  1550. (t
  1551. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1552. (concat
  1553. (format "\\footnote{%s}" (org-trim (org-export-data def info)))
  1554. ;; Retrieve all footnote references within the footnote and
  1555. ;; add their definition after it, since LaTeX doesn't support
  1556. ;; them inside.
  1557. (org-latex--delayed-footnotes-definitions def info)))))))
  1558. ;;;; Headline
  1559. (defun org-latex-headline (headline contents info)
  1560. "Transcode a HEADLINE element from Org to LaTeX.
  1561. CONTENTS holds the contents of the headline. INFO is a plist
  1562. holding contextual information."
  1563. (unless (org-element-property :footnote-section-p headline)
  1564. (let* ((class (plist-get info :latex-class))
  1565. (level (org-export-get-relative-level headline info))
  1566. (numberedp (org-export-numbered-headline-p headline info))
  1567. (class-sectioning (assoc class (plist-get info :latex-classes)))
  1568. ;; Section formatting will set two placeholders: one for
  1569. ;; the title and the other for the contents.
  1570. (section-fmt
  1571. (let ((sec (if (functionp (nth 2 class-sectioning))
  1572. (funcall (nth 2 class-sectioning) level numberedp)
  1573. (nth (1+ level) class-sectioning))))
  1574. (cond
  1575. ;; No section available for that LEVEL.
  1576. ((not sec) nil)
  1577. ;; Section format directly returned by a function. Add
  1578. ;; placeholder for contents.
  1579. ((stringp sec) (concat sec "\n%s"))
  1580. ;; (numbered-section . unnumbered-section)
  1581. ((not (consp (cdr sec)))
  1582. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1583. ;; (numbered-open numbered-close)
  1584. ((= (length sec) 2)
  1585. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1586. ;; (num-in num-out no-num-in no-num-out)
  1587. ((= (length sec) 4)
  1588. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1589. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1590. ;; Create a temporary export back-end that hard-codes
  1591. ;; "\underline" within "\section" and alike.
  1592. (section-back-end
  1593. (org-export-create-backend
  1594. :parent 'latex
  1595. :transcoders
  1596. '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
  1597. (text
  1598. (org-export-data-with-backend
  1599. (org-element-property :title headline) section-back-end info))
  1600. (todo
  1601. (and (plist-get info :with-todo-keywords)
  1602. (let ((todo (org-element-property :todo-keyword headline)))
  1603. (and todo (org-export-data todo info)))))
  1604. (todo-type (and todo (org-element-property :todo-type headline)))
  1605. (tags (and (plist-get info :with-tags)
  1606. (org-export-get-tags headline info)))
  1607. (priority (and (plist-get info :with-priority)
  1608. (org-element-property :priority headline)))
  1609. ;; Create the headline text along with a no-tag version.
  1610. ;; The latter is required to remove tags from toc.
  1611. (full-text (funcall (plist-get info :latex-format-headline-function)
  1612. todo todo-type priority text tags info))
  1613. ;; Associate \label to the headline for internal links.
  1614. (headline-label (org-latex--label headline info t t))
  1615. (pre-blanks
  1616. (make-string (org-element-property :pre-blank headline) ?\n)))
  1617. (if (or (not section-fmt) (org-export-low-level-p headline info))
  1618. ;; This is a deep sub-tree: export it as a list item. Also
  1619. ;; export as items headlines for which no section format has
  1620. ;; been found.
  1621. (let ((low-level-body
  1622. (concat
  1623. ;; If headline is the first sibling, start a list.
  1624. (when (org-export-first-sibling-p headline info)
  1625. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1626. ;; Itemize headline
  1627. "\\item"
  1628. (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
  1629. "\\relax")
  1630. " " full-text "\n"
  1631. headline-label
  1632. pre-blanks
  1633. contents)))
  1634. ;; If headline is not the last sibling simply return
  1635. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
  1636. ;; any blank line.
  1637. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1638. (replace-regexp-in-string
  1639. "[ \t\n]*\\'"
  1640. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1641. low-level-body)))
  1642. ;; This is a standard headline. Export it as a section. Add
  1643. ;; an alternative heading when possible, and when this is not
  1644. ;; identical to the usual heading.
  1645. (let ((opt-title
  1646. (funcall (plist-get info :latex-format-headline-function)
  1647. todo todo-type priority
  1648. (org-export-data-with-backend
  1649. (org-export-get-alt-title headline info)
  1650. section-back-end info)
  1651. (and (eq (plist-get info :with-tags) t) tags)
  1652. info))
  1653. ;; Maybe end local TOC (see `org-latex-keyword').
  1654. (contents
  1655. (concat
  1656. contents
  1657. (let ((case-fold-search t)
  1658. (section
  1659. (let ((first (car (org-element-contents headline))))
  1660. (and (eq (org-element-type first) 'section) first))))
  1661. (org-element-map section 'keyword
  1662. (lambda (k)
  1663. (and (equal (org-element-property :key k) "TOC")
  1664. (let ((v (org-element-property :value k)))
  1665. (and (org-string-match-p "\\<headlines\\>" v)
  1666. (org-string-match-p "\\<local\\>" v)
  1667. (format "\\stopcontents[level-%d]" level)))))
  1668. info t)))))
  1669. (if (and opt-title
  1670. (not (equal opt-title full-text))
  1671. (string-match "\\`\\\\\\(.+?\\){" section-fmt))
  1672. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  1673. ;; Replace square brackets with parenthesis
  1674. ;; since square brackets are not supported in
  1675. ;; optional arguments.
  1676. (replace-regexp-in-string
  1677. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  1678. full-text
  1679. (concat headline-label pre-blanks contents))
  1680. ;; Impossible to add an alternative heading. Fallback to
  1681. ;; regular sectioning format string.
  1682. (format section-fmt full-text
  1683. (concat headline-label pre-blanks contents))))))))
  1684. (defun org-latex-format-headline-default-function
  1685. (todo _todo-type priority text tags info)
  1686. "Default format function for a headline.
  1687. See `org-latex-format-headline-function' for details."
  1688. (concat
  1689. (and todo (format "{\\bfseries\\sffamily %s} " todo))
  1690. (and priority (format "\\framebox{\\#%c} " priority))
  1691. text
  1692. (and tags
  1693. (format "\\hfill{}\\textsc{%s}"
  1694. (mapconcat (lambda (tag) (org-latex-plain-text tag info))
  1695. tags ":")))))
  1696. ;;;; Horizontal Rule
  1697. (defun org-latex-horizontal-rule (horizontal-rule _contents info)
  1698. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1699. CONTENTS is nil. INFO is a plist holding contextual information."
  1700. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1701. (prev (org-export-get-previous-element horizontal-rule info)))
  1702. (concat
  1703. ;; Make sure the rule doesn't start at the end of the current
  1704. ;; line by separating it with a blank line from previous element.
  1705. (when (and prev
  1706. (let ((prev-blank (org-element-property :post-blank prev)))
  1707. (or (not prev-blank) (zerop prev-blank))))
  1708. "\n")
  1709. (org-latex--wrap-label
  1710. horizontal-rule
  1711. (format "\\rule{%s}{%s}"
  1712. (or (plist-get attr :width) "\\linewidth")
  1713. (or (plist-get attr :thickness) "0.5pt"))
  1714. info))))
  1715. ;;;; Inline Src Block
  1716. (defun org-latex-inline-src-block (inline-src-block _contents info)
  1717. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1718. CONTENTS holds the contents of the item. INFO is a plist holding
  1719. contextual information."
  1720. (let* ((code (org-element-property :value inline-src-block))
  1721. (separator (org-latex--find-verb-separator code)))
  1722. (case (plist-get info :latex-listings)
  1723. ;; Do not use a special package: transcode it verbatim.
  1724. ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
  1725. ;; Use minted package.
  1726. (minted
  1727. (let* ((org-lang (org-element-property :language inline-src-block))
  1728. (mint-lang (or (cadr (assq (intern org-lang)
  1729. (plist-get info :latex-minted-langs)))
  1730. (downcase org-lang)))
  1731. (options (org-latex--make-option-string
  1732. (plist-get info :latex-minted-options))))
  1733. (concat (format "\\mint%s{%s}"
  1734. (if (string= options "") "" (format "[%s]" options))
  1735. mint-lang)
  1736. separator code separator)))
  1737. ;; Use listings package.
  1738. (otherwise
  1739. ;; Maybe translate language's name.
  1740. (let* ((org-lang (org-element-property :language inline-src-block))
  1741. (lst-lang (or (cadr (assq (intern org-lang)
  1742. (plist-get info :latex-listings-langs)))
  1743. org-lang))
  1744. (options (org-latex--make-option-string
  1745. (append (plist-get info :latex-listings-options)
  1746. `(("language" ,lst-lang))))))
  1747. (concat (format "\\lstinline[%s]" options)
  1748. separator code separator))))))
  1749. ;;;; Inlinetask
  1750. (defun org-latex-inlinetask (inlinetask contents info)
  1751. "Transcode an INLINETASK element from Org to LaTeX.
  1752. CONTENTS holds the contents of the block. INFO is a plist
  1753. holding contextual information."
  1754. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1755. (todo (and (plist-get info :with-todo-keywords)
  1756. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1757. (and todo (org-export-data todo info)))))
  1758. (todo-type (org-element-property :todo-type inlinetask))
  1759. (tags (and (plist-get info :with-tags)
  1760. (org-export-get-tags inlinetask info)))
  1761. (priority (and (plist-get info :with-priority)
  1762. (org-element-property :priority inlinetask)))
  1763. (contents (concat (org-latex--label inlinetask info) contents)))
  1764. (funcall (plist-get info :latex-format-inlinetask-function)
  1765. todo todo-type priority title tags contents info)))
  1766. (defun org-latex-format-inlinetask-default-function
  1767. (todo _todo-type priority title tags contents info)
  1768. "Default format function for a inlinetasks.
  1769. See `org-latex-format-inlinetask-function' for details."
  1770. (let ((full-title
  1771. (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1772. (when priority (format "\\framebox{\\#%c} " priority))
  1773. title
  1774. (when tags
  1775. (format "\\hfill{}\\textsc{:%s:}"
  1776. (mapconcat
  1777. (lambda (tag) (org-latex-plain-text tag info))
  1778. tags ":"))))))
  1779. (concat "\\begin{center}\n"
  1780. "\\fbox{\n"
  1781. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1782. full-title "\n\n"
  1783. (and (org-string-nw-p contents)
  1784. (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
  1785. "\\end{minipage}\n"
  1786. "}\n"
  1787. "\\end{center}")))
  1788. ;;;; Italic
  1789. (defun org-latex-italic (_italic contents info)
  1790. "Transcode ITALIC from Org to LaTeX.
  1791. CONTENTS is the text with italic markup. INFO is a plist holding
  1792. contextual information."
  1793. (org-latex--text-markup contents 'italic info))
  1794. ;;;; Item
  1795. (defun org-latex-item (item contents info)
  1796. "Transcode an ITEM element from Org to LaTeX.
  1797. CONTENTS holds the contents of the item. INFO is a plist holding
  1798. contextual information."
  1799. (let* ((counter
  1800. (let ((count (org-element-property :counter item))
  1801. (level
  1802. ;; Determine level of current item to determine the
  1803. ;; correct LaTeX counter to use (enumi, enumii...).
  1804. (let ((parent item) (level 0))
  1805. (while (memq (org-element-type
  1806. (setq parent (org-export-get-parent parent)))
  1807. '(plain-list item))
  1808. (when (and (eq (org-element-type parent) 'plain-list)
  1809. (eq (org-element-property :type parent)
  1810. 'ordered))
  1811. (incf level)))
  1812. level)))
  1813. (and count
  1814. (< level 5)
  1815. (format "\\setcounter{enum%s}{%s}\n"
  1816. (nth (1- level) '("i" "ii" "iii" "iv"))
  1817. (1- count)))))
  1818. (checkbox (case (org-element-property :checkbox item)
  1819. (on "$\\boxtimes$ ")
  1820. (off "$\\square$ ")
  1821. (trans "$\\boxminus$ ")))
  1822. (tag (let ((tag (org-element-property :tag item)))
  1823. ;; Check-boxes must belong to the tag.
  1824. (and tag (format "[{%s}] "
  1825. (concat checkbox
  1826. (org-export-data tag info)))))))
  1827. (concat counter
  1828. "\\item"
  1829. (cond
  1830. (tag)
  1831. (checkbox (concat " " checkbox))
  1832. ;; Without a tag or a check-box, if CONTENTS starts with
  1833. ;; an opening square bracket, add "\relax" to "\item",
  1834. ;; unless the brackets comes from an initial export
  1835. ;; snippet (i.e. it is inserted willingly by the user).
  1836. ((and contents
  1837. (org-string-match-p "\\`[ \t]*\\[" contents)
  1838. (not (let ((e (car (org-element-contents item))))
  1839. (and (eq (org-element-type e) 'paragraph)
  1840. (let ((o (car (org-element-contents e))))
  1841. (and (eq (org-element-type o) 'export-snippet)
  1842. (eq (org-export-snippet-backend o)
  1843. 'latex)))))))
  1844. "\\relax ")
  1845. (t " "))
  1846. (and contents (org-trim contents))
  1847. ;; If there are footnotes references in tag, be sure to
  1848. ;; add their definition at the end of the item. This
  1849. ;; workaround is necessary since "\footnote{}" command is
  1850. ;; not supported in tags.
  1851. (and tag
  1852. (org-latex--delayed-footnotes-definitions
  1853. (org-element-property :tag item) info)))))
  1854. ;;;; Keyword
  1855. (defun org-latex-keyword (keyword _contents info)
  1856. "Transcode a KEYWORD element from Org to LaTeX.
  1857. CONTENTS is nil. INFO is a plist holding contextual information."
  1858. (let ((key (org-element-property :key keyword))
  1859. (value (org-element-property :value keyword)))
  1860. (cond
  1861. ((string= key "LATEX") value)
  1862. ((string= key "INDEX") (format "\\index{%s}" value))
  1863. ((string= key "TOC")
  1864. (let ((case-fold-search t))
  1865. (cond
  1866. ((org-string-match-p "\\<headlines\\>" value)
  1867. (let* ((localp (org-string-match-p "\\<local\\>" value))
  1868. (parent (org-element-lineage keyword '(headline)))
  1869. (level (if (not (and localp parent)) 0
  1870. (org-export-get-relative-level parent info)))
  1871. (depth
  1872. (and (string-match "\\<[0-9]+\\>" value)
  1873. (format
  1874. "\\setcounter{tocdepth}{%d}"
  1875. (+ (string-to-number (match-string 0 value)) level)))))
  1876. (if (and localp parent)
  1877. ;; Start local TOC, assuming package "titletoc" is
  1878. ;; required.
  1879. (format "\\startcontents[level-%d]
  1880. \\printcontents[level-%d]{}{0}{%s}"
  1881. level level (or depth ""))
  1882. (concat depth (and depth "\n") "\\tableofcontents"))))
  1883. ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
  1884. ((org-string-match-p "\\<listings\\>" value)
  1885. (case (plist-get info :latex-listings)
  1886. ((nil) "\\listoffigures")
  1887. (minted "\\listoflistings")
  1888. (otherwise "\\lstlistoflistings")))))))))
  1889. ;;;; Latex Environment
  1890. (defun org-latex-latex-environment (latex-environment _contents info)
  1891. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1892. CONTENTS is nil. INFO is a plist holding contextual information."
  1893. (when (plist-get info :with-latex)
  1894. (let ((value (org-remove-indentation
  1895. (org-element-property :value latex-environment))))
  1896. (if (not (org-element-property :name latex-environment)) value
  1897. ;; Environment is labeled: label must be within the environment
  1898. ;; (otherwise, a reference pointing to that element will count
  1899. ;; the section instead).
  1900. (with-temp-buffer
  1901. (insert value)
  1902. (goto-char (point-min))
  1903. (forward-line)
  1904. (insert (org-latex--label latex-environment info nil t))
  1905. (buffer-string))))))
  1906. ;;;; Latex Fragment
  1907. (defun org-latex-latex-fragment (latex-fragment _contents _info)
  1908. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1909. CONTENTS is nil. INFO is a plist holding contextual information."
  1910. (let ((value (org-element-property :value latex-fragment)))
  1911. ;; Trim math markers since the fragment is enclosed within
  1912. ;; a latex-math-block object anyway.
  1913. (cond ((string-match-p "\\`\\$[^$]" value) (substring value 1 -1))
  1914. ((string-prefix-p "\\(" value) (substring value 2 -2))
  1915. (t value))))
  1916. ;;;; Line Break
  1917. (defun org-latex-line-break (_line-break _contents _info)
  1918. "Transcode a LINE-BREAK object from Org to LaTeX.
  1919. CONTENTS is nil. INFO is a plist holding contextual information."
  1920. "\\\\\n")
  1921. ;;;; Link
  1922. (defun org-latex--inline-image (link info)
  1923. "Return LaTeX code for an inline image.
  1924. LINK is the link pointing to the inline image. INFO is a plist
  1925. used as a communication channel."
  1926. (let* ((parent (org-export-get-parent-element link))
  1927. (path (let ((raw-path (org-element-property :path link)))
  1928. (if (not (file-name-absolute-p raw-path)) raw-path
  1929. (expand-file-name raw-path))))
  1930. (filetype (file-name-extension path))
  1931. (caption (org-latex--caption/label-string parent info))
  1932. (caption-above-p (org-latex--caption-above-p link info))
  1933. ;; Retrieve latex attributes from the element around.
  1934. (attr (org-export-read-attribute :attr_latex parent))
  1935. (float (let ((float (plist-get attr :float)))
  1936. (cond ((string= float "wrap") 'wrap)
  1937. ((string= float "sideways") 'sideways)
  1938. ((string= float "multicolumn") 'multicolumn)
  1939. ((or float
  1940. (org-element-property :caption parent)
  1941. (org-string-nw-p (plist-get attr :caption)))
  1942. (if (and (plist-member attr :float) (not float))
  1943. 'nonfloat
  1944. 'figure))
  1945. ((and (not float) (plist-member attr :float)) nil))))
  1946. (placement
  1947. (let ((place (plist-get attr :placement)))
  1948. (cond
  1949. (place (format "%s" place))
  1950. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  1951. ((eq float 'figure)
  1952. (format "[%s]" (plist-get info :latex-default-figure-position)))
  1953. (t ""))))
  1954. (comment-include (if (plist-get attr :comment-include) "%" ""))
  1955. ;; It is possible to specify width and height in the
  1956. ;; ATTR_LATEX line, and also via default variables.
  1957. (width (cond ((plist-get attr :width))
  1958. ((plist-get attr :height) "")
  1959. ((eq float 'wrap) "0.48\\textwidth")
  1960. (t (plist-get info :latex-image-default-width))))
  1961. (height (cond ((plist-get attr :height))
  1962. ((or (plist-get attr :width)
  1963. (memq float '(figure wrap))) "")
  1964. (t (plist-get info :latex-image-default-height))))
  1965. (options (let ((opt (or (plist-get attr :options)
  1966. (plist-get info :latex-image-default-option))))
  1967. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  1968. (match-string 1 opt))))
  1969. image-code)
  1970. (if (member filetype '("tikz" "pgf"))
  1971. ;; For tikz images:
  1972. ;; - use \input to read in image file.
  1973. ;; - if options are present, wrap in a tikzpicture environment.
  1974. ;; - if width or height are present, use \resizebox to change
  1975. ;; the image size.
  1976. (progn
  1977. (setq image-code (format "\\input{%s}" path))
  1978. (when (org-string-nw-p options)
  1979. (setq image-code
  1980. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  1981. options
  1982. image-code)))
  1983. (when (or (org-string-nw-p width) (org-string-nw-p height))
  1984. (setq image-code (format "\\resizebox{%s}{%s}{%s}"
  1985. (if (org-string-nw-p width) width "!")
  1986. (if (org-string-nw-p height) height "!")
  1987. image-code))))
  1988. ;; For other images:
  1989. ;; - add width and height to options.
  1990. ;; - include the image with \includegraphics.
  1991. (when (org-string-nw-p width)
  1992. (setq options (concat options ",width=" width)))
  1993. (when (org-string-nw-p height)
  1994. (setq options (concat options ",height=" height)))
  1995. (let ((search-option (org-element-property :search-option link)))
  1996. (when (and search-option
  1997. (equal filetype "pdf")
  1998. (org-string-match-p "\\`[0-9]+\\'" search-option)
  1999. (not (org-string-match-p "page=" options)))
  2000. (setq options (concat options ",page=" search-option))))
  2001. (setq image-code
  2002. (format "\\includegraphics%s{%s}"
  2003. (cond ((not (org-string-nw-p options)) "")
  2004. ((= (aref options 0) ?,)
  2005. (format "[%s]"(substring options 1)))
  2006. (t (format "[%s]" options)))
  2007. path))
  2008. (when (equal filetype "svg")
  2009. (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
  2010. "\\includesvg"
  2011. image-code
  2012. nil t))
  2013. (setq image-code (replace-regexp-in-string "\\.svg}"
  2014. "}"
  2015. image-code
  2016. nil t))))
  2017. ;; Return proper string, depending on FLOAT.
  2018. (case float
  2019. (wrap (format "\\begin{wrapfigure}%s
  2020. %s\\centering
  2021. %s%s
  2022. %s\\end{wrapfigure}"
  2023. placement
  2024. (if caption-above-p caption "")
  2025. comment-include image-code
  2026. (if caption-above-p "" caption)))
  2027. (sideways (format "\\begin{sidewaysfigure}
  2028. %s\\centering
  2029. %s%s
  2030. %s\\end{sidewaysfigure}"
  2031. (if caption-above-p caption "")
  2032. comment-include image-code
  2033. (if caption-above-p "" caption)))
  2034. (multicolumn (format "\\begin{figure*}%s
  2035. %s\\centering
  2036. %s%s
  2037. %s\\end{figure*}"
  2038. placement
  2039. (if caption-above-p caption "")
  2040. comment-include image-code
  2041. (if caption-above-p "" caption)))
  2042. (figure (format "\\begin{figure}%s
  2043. %s\\centering
  2044. %s%s
  2045. %s\\end{figure}"
  2046. placement
  2047. (if caption-above-p caption "")
  2048. comment-include image-code
  2049. (if caption-above-p "" caption)))
  2050. (nonfloat
  2051. (format "\\begin{center}
  2052. %s%s
  2053. %s\\end{center}"
  2054. (if caption-above-p caption "")
  2055. image-code
  2056. (if caption-above-p "" caption)))
  2057. (otherwise image-code))))
  2058. (defun org-latex-link (link desc info)
  2059. "Transcode a LINK object from Org to LaTeX.
  2060. DESC is the description part of the link, or the empty string.
  2061. INFO is a plist holding contextual information. See
  2062. `org-export-data'."
  2063. (let* ((type (org-element-property :type link))
  2064. (raw-path (replace-regexp-in-string
  2065. "%" "\\%" (org-element-property :path link) nil t))
  2066. ;; Ensure DESC really exists, or set it to nil.
  2067. (desc (and (not (string= desc "")) desc))
  2068. (imagep (org-export-inline-image-p
  2069. link (plist-get info :latex-inline-image-rules)))
  2070. (path (cond
  2071. ((member type '("http" "https" "ftp" "mailto" "doi"))
  2072. (concat type ":" raw-path))
  2073. ((string= type "file") (org-export-file-uri raw-path))
  2074. (t raw-path))))
  2075. (cond
  2076. ;; Link type is handled by a special function.
  2077. ((org-export-custom-protocol-maybe link desc 'latex))
  2078. ;; Image file.
  2079. (imagep (org-latex--inline-image link info))
  2080. ;; Radio link: Transcode target's contents and use them as link's
  2081. ;; description.
  2082. ((string= type "radio")
  2083. (let ((destination (org-export-resolve-radio-link link info)))
  2084. (if (not destination) desc
  2085. (format "\\hyperref[%s]{%s}"
  2086. (org-export-get-reference destination info)
  2087. desc))))
  2088. ;; Links pointing to a headline: Find destination and build
  2089. ;; appropriate referencing command.
  2090. ((member type '("custom-id" "fuzzy" "id"))
  2091. (let ((destination (if (string= type "fuzzy")
  2092. (org-export-resolve-fuzzy-link link info)
  2093. (org-export-resolve-id-link link info))))
  2094. (case (org-element-type destination)
  2095. ;; Id link points to an external file.
  2096. (plain-text
  2097. (if desc (format "\\href{%s}{%s}" destination desc)
  2098. (format "\\url{%s}" destination)))
  2099. ;; Fuzzy link points nowhere.
  2100. ((nil)
  2101. (format (plist-get info :latex-link-with-unknown-path-format)
  2102. (or desc
  2103. (org-export-data
  2104. (org-element-property :raw-link link) info))))
  2105. ;; LINK points to a headline. If headlines are numbered
  2106. ;; and the link has no description, display headline's
  2107. ;; number. Otherwise, display description or headline's
  2108. ;; title.
  2109. (headline
  2110. (let ((label (org-latex--label destination info t)))
  2111. (if (and (not desc)
  2112. (org-export-numbered-headline-p destination info))
  2113. (format "\\ref{%s}" label)
  2114. (format "\\hyperref[%s]{%s}" label
  2115. (or desc
  2116. (org-export-data
  2117. (org-element-property :title destination) info))))))
  2118. ;; Fuzzy link points to a target. Do as above.
  2119. (otherwise
  2120. (let ((ref (org-latex--label destination info t)))
  2121. (if (not desc) (format "\\ref{%s}" ref)
  2122. (format "\\hyperref[%s]{%s}" ref desc)))))))
  2123. ;; Coderef: replace link with the reference name or the
  2124. ;; equivalent line number.
  2125. ((string= type "coderef")
  2126. (format (org-export-get-coderef-format path desc)
  2127. (org-export-resolve-coderef path info)))
  2128. ;; External link with a description part.
  2129. ((and path desc) (format "\\href{%s}{%s}" path desc))
  2130. ;; External link without a description part.
  2131. (path (format "\\url{%s}" path))
  2132. ;; No path, only description. Try to do something useful.
  2133. (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
  2134. ;;;; Node Property
  2135. (defun org-latex-node-property (node-property _contents _info)
  2136. "Transcode a NODE-PROPERTY element from Org to LaTeX.
  2137. CONTENTS is nil. INFO is a plist holding contextual
  2138. information."
  2139. (format "%s:%s"
  2140. (org-element-property :key node-property)
  2141. (let ((value (org-element-property :value node-property)))
  2142. (if value (concat " " value) ""))))
  2143. ;;;; Paragraph
  2144. (defun org-latex-paragraph (_paragraph contents _info)
  2145. "Transcode a PARAGRAPH element from Org to LaTeX.
  2146. CONTENTS is the contents of the paragraph, as a string. INFO is
  2147. the plist used as a communication channel."
  2148. contents)
  2149. ;;;; Plain List
  2150. (defun org-latex-plain-list (plain-list contents info)
  2151. "Transcode a PLAIN-LIST element from Org to LaTeX.
  2152. CONTENTS is the contents of the list. INFO is a plist holding
  2153. contextual information."
  2154. (let* ((type (org-element-property :type plain-list))
  2155. (attr (org-export-read-attribute :attr_latex plain-list))
  2156. (latex-type (let ((env (plist-get attr :environment)))
  2157. (cond (env (format "%s" env))
  2158. ((eq type 'ordered) "enumerate")
  2159. ((eq type 'descriptive) "description")
  2160. (t "itemize")))))
  2161. (org-latex--wrap-label
  2162. plain-list
  2163. (format "\\begin{%s}%s\n%s\\end{%s}"
  2164. latex-type
  2165. (or (plist-get attr :options) "")
  2166. contents
  2167. latex-type)
  2168. info)))
  2169. ;;;; Plain Text
  2170. (defun org-latex-plain-text (text info)
  2171. "Transcode a TEXT string from Org to LaTeX.
  2172. TEXT is the string to transcode. INFO is a plist holding
  2173. contextual information."
  2174. (let* ((specialp (plist-get info :with-special-strings))
  2175. (output
  2176. ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
  2177. (let ((case-fold-search nil))
  2178. (replace-regexp-in-string
  2179. "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
  2180. ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
  2181. ;; However, if special strings are used, be careful not
  2182. ;; to protect "\" in "\-" constructs.
  2183. (replace-regexp-in-string
  2184. (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
  2185. (lambda (m)
  2186. (case (string-to-char m)
  2187. (?\\ "$\\\\backslash$\\1")
  2188. (?~ "\\\\textasciitilde{}")
  2189. (?^ "\\\\^{}")
  2190. (t "\\\\\\&")))
  2191. text)))))
  2192. ;; Activate smart quotes. Be sure to provide original TEXT string
  2193. ;; since OUTPUT may have been modified.
  2194. (when (plist-get info :with-smart-quotes)
  2195. (setq output (org-export-activate-smart-quotes output :latex info text)))
  2196. ;; Convert special strings.
  2197. (when specialp
  2198. (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
  2199. ;; Handle break preservation if required.
  2200. (when (plist-get info :preserve-breaks)
  2201. (setq output (replace-regexp-in-string
  2202. "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
  2203. ;; Return value.
  2204. output))
  2205. ;;;; Planning
  2206. (defun org-latex-planning (planning _contents info)
  2207. "Transcode a PLANNING element from Org to LaTeX.
  2208. CONTENTS is nil. INFO is a plist holding contextual
  2209. information."
  2210. (concat
  2211. "\\noindent"
  2212. (mapconcat
  2213. 'identity
  2214. (delq nil
  2215. (list
  2216. (let ((closed (org-element-property :closed planning)))
  2217. (when closed
  2218. (concat
  2219. (format "\\textbf{%s} " org-closed-string)
  2220. (format (plist-get info :latex-inactive-timestamp-format)
  2221. (org-timestamp-translate closed)))))
  2222. (let ((deadline (org-element-property :deadline planning)))
  2223. (when deadline
  2224. (concat
  2225. (format "\\textbf{%s} " org-deadline-string)
  2226. (format (plist-get info :latex-active-timestamp-format)
  2227. (org-timestamp-translate deadline)))))
  2228. (let ((scheduled (org-element-property :scheduled planning)))
  2229. (when scheduled
  2230. (concat
  2231. (format "\\textbf{%s} " org-scheduled-string)
  2232. (format (plist-get info :latex-active-timestamp-format)
  2233. (org-timestamp-translate scheduled)))))))
  2234. " ")
  2235. "\\\\"))
  2236. ;;;; Property Drawer
  2237. (defun org-latex-property-drawer (_property-drawer contents _info)
  2238. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  2239. CONTENTS holds the contents of the drawer. INFO is a plist
  2240. holding contextual information."
  2241. (and (org-string-nw-p contents)
  2242. (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
  2243. ;;;; Pseudo Element: LaTeX Matrices
  2244. ;; `latex-matrices' elements have the following properties:
  2245. ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
  2246. ;; `math').
  2247. (defun org-latex--wrap-latex-matrices (data info)
  2248. "Merge contiguous tables with the same mode within a pseudo-element.
  2249. DATA is a parse tree or a secondary string. INFO is a plist
  2250. containing export options. Modify DATA by side-effect and return
  2251. it."
  2252. (org-element-map data 'table
  2253. (lambda (table)
  2254. (when (eq (org-element-property :type table) 'org)
  2255. (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
  2256. (plist-get info :latex-default-table-mode))))
  2257. (when (and (member mode '("inline-math" "math"))
  2258. ;; Do not wrap twice the same table.
  2259. (not (eq (org-element-type
  2260. (org-element-property :parent table))
  2261. 'latex-matrices)))
  2262. (let* ((caption (and (not (string= mode "inline-math"))
  2263. (org-element-property :caption table)))
  2264. (matrices
  2265. (list 'latex-matrices
  2266. (list :caption caption
  2267. :markup
  2268. (cond ((string= mode "inline-math") 'inline)
  2269. (caption 'equation)
  2270. (t 'math)))))
  2271. (previous table)
  2272. (next (org-export-get-next-element table info)))
  2273. (org-element-insert-before matrices table)
  2274. ;; Swallow all contiguous tables sharing the same mode.
  2275. (while (and
  2276. (zerop (or (org-element-property :post-blank previous) 0))
  2277. (setq next (org-export-get-next-element previous info))
  2278. (eq (org-element-type next) 'table)
  2279. (eq (org-element-property :type next) 'org)
  2280. (string= (or (org-export-read-attribute
  2281. :attr_latex next :mode)
  2282. (plist-get info :latex-default-table-mode))
  2283. mode))
  2284. (org-element-extract-element previous)
  2285. (org-element-adopt-elements matrices previous)
  2286. (setq previous next))
  2287. (org-element-put-property
  2288. matrices :post-blank (org-element-property :post-blank previous))
  2289. (org-element-extract-element previous)
  2290. (org-element-adopt-elements matrices previous))))))
  2291. info)
  2292. data)
  2293. (defun org-latex-matrices (matrices contents _info)
  2294. "Transcode a MATRICES element from Org to LaTeX.
  2295. CONTENTS is a string. INFO is a plist used as a communication
  2296. channel."
  2297. (format (case (org-element-property :markup matrices)
  2298. (inline "\\(%s\\)")
  2299. (equation "\\begin{equation}\n%s\\end{equation}")
  2300. (t "\\[\n%s\\]"))
  2301. contents))
  2302. (defun org-latex-matrices-tree-filter (tree _backend info)
  2303. (org-latex--wrap-latex-matrices tree info))
  2304. ;;;; Pseudo Object: LaTeX Math Block
  2305. ;; `latex-math-block' objects have the following property:
  2306. ;; `:post-blank'.
  2307. (defun org-latex--wrap-latex-math-block (data info)
  2308. "Merge contiguous math objects in a pseudo-object container.
  2309. DATA is a parse tree or a secondary string. INFO is a plist
  2310. containing export options. Modify DATA by side-effect and return it."
  2311. (let ((valid-object-p
  2312. ;; Non-nil when OBJ can be added to the latex math block.
  2313. (lambda (obj)
  2314. (pcase (org-element-type obj)
  2315. (`entity (org-element-property :latex-math-p obj))
  2316. (`latex-fragment
  2317. (let ((value (org-element-property :value obj)))
  2318. (or (string-prefix-p "\\(" value)
  2319. (string-match-p "\\`\\$[^$]" value))))
  2320. ((or `subscript `superscript) t)))))
  2321. (org-element-map data '(entity latex-fragment subscript superscript)
  2322. (lambda (object)
  2323. ;; Skip objects already wrapped.
  2324. (when (and (not (eq (org-element-type
  2325. (org-element-property :parent object))
  2326. 'latex-math-block))
  2327. (funcall valid-object-p object))
  2328. (let ((math-block (list 'latex-math-block nil))
  2329. (next-elements (org-export-get-next-element object info t))
  2330. (last object))
  2331. ;; Wrap MATH-BLOCK around OBJECT in DATA.
  2332. (org-element-insert-before math-block object)
  2333. (org-element-extract-element object)
  2334. (org-element-adopt-elements math-block object)
  2335. (when (zerop (or (org-element-property :post-blank object) 0))
  2336. ;; MATH-BLOCK swallows consecutive math objects.
  2337. (catch 'exit
  2338. (dolist (next next-elements)
  2339. (if (not (funcall valid-object-p next)) (throw 'exit nil)
  2340. (org-element-extract-element next)
  2341. (org-element-adopt-elements math-block next)
  2342. ;; Eschew the case: \beta$x$ -> \(\betax\).
  2343. (unless (memq (org-element-type next)
  2344. '(subscript superscript))
  2345. (org-element-put-property last :post-blank 1))
  2346. (setq last next)
  2347. (when (> (or (org-element-property :post-blank next) 0) 0)
  2348. (throw 'exit nil))))))
  2349. (org-element-put-property
  2350. math-block :post-blank (org-element-property :post-blank last)))))
  2351. info nil '(subscript superscript latex-math-block) t)
  2352. ;; Return updated DATA.
  2353. data))
  2354. (defun org-latex-math-block-tree-filter (tree _backend info)
  2355. (org-latex--wrap-latex-math-block tree info))
  2356. (defun org-latex-math-block-options-filter (info _backend)
  2357. (dolist (prop '(:author :date :title) info)
  2358. (plist-put info prop
  2359. (org-latex--wrap-latex-math-block (plist-get info prop) info))))
  2360. (defun org-latex-math-block (_math-block contents _info)
  2361. "Transcode a MATH-BLOCK object from Org to LaTeX.
  2362. CONTENTS is a string. INFO is a plist used as a communication
  2363. channel."
  2364. (when (org-string-nw-p contents)
  2365. (format "\\(%s\\)" (org-trim contents))))
  2366. ;;;; Quote Block
  2367. (defun org-latex-quote-block (quote-block contents info)
  2368. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  2369. CONTENTS holds the contents of the block. INFO is a plist
  2370. holding contextual information."
  2371. (org-latex--wrap-label
  2372. quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
  2373. ;;;; Radio Target
  2374. (defun org-latex-radio-target (radio-target text info)
  2375. "Transcode a RADIO-TARGET object from Org to LaTeX.
  2376. TEXT is the text of the target. INFO is a plist holding
  2377. contextual information."
  2378. (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
  2379. ;;;; Section
  2380. (defun org-latex-section (_section contents _info)
  2381. "Transcode a SECTION element from Org to LaTeX.
  2382. CONTENTS holds the contents of the section. INFO is a plist
  2383. holding contextual information."
  2384. contents)
  2385. ;;;; Special Block
  2386. (defun org-latex-special-block (special-block contents info)
  2387. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  2388. CONTENTS holds the contents of the block. INFO is a plist
  2389. holding contextual information."
  2390. (let ((type (org-element-property :type special-block))
  2391. (opt (org-export-read-attribute :attr_latex special-block :options))
  2392. (caption (org-latex--caption/label-string special-block info))
  2393. (caption-above-p (org-latex--caption-above-p special-block info)))
  2394. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  2395. (and caption-above-p caption)
  2396. contents
  2397. (and (not caption-above-p) caption)
  2398. (format "\\end{%s}" type))))
  2399. ;;;; Src Block
  2400. (defun org-latex-src-block (src-block _contents info)
  2401. "Transcode a SRC-BLOCK element from Org to LaTeX.
  2402. CONTENTS holds the contents of the item. INFO is a plist holding
  2403. contextual information."
  2404. (when (org-string-nw-p (org-element-property :value src-block))
  2405. (let* ((lang (org-element-property :language src-block))
  2406. (caption (org-element-property :caption src-block))
  2407. (caption-above-p (org-latex--caption-above-p src-block info))
  2408. (label (org-element-property :name src-block))
  2409. (custom-env (and lang
  2410. (cadr (assq (intern lang)
  2411. org-latex-custom-lang-environments))))
  2412. (num-start (case (org-element-property :number-lines src-block)
  2413. (continued (org-export-get-loc src-block info))
  2414. (new 0)))
  2415. (retain-labels (org-element-property :retain-labels src-block))
  2416. (attributes (org-export-read-attribute :attr_latex src-block))
  2417. (float (plist-get attributes :float))
  2418. (listings (plist-get info :latex-listings)))
  2419. (cond
  2420. ;; Case 1. No source fontification.
  2421. ((not listings)
  2422. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2423. (float-env
  2424. (cond ((string= "multicolumn" float)
  2425. (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
  2426. (plist-get info :latex-default-figure-position)
  2427. (if caption-above-p caption-str "")
  2428. (if caption-above-p "" caption-str)))
  2429. (caption (concat
  2430. (if caption-above-p caption-str "")
  2431. "%s"
  2432. (if caption-above-p "" (concat "\n" caption-str))))
  2433. (t "%s"))))
  2434. (format
  2435. float-env
  2436. (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
  2437. (org-export-format-code-default src-block info))))))
  2438. ;; Case 2. Custom environment.
  2439. (custom-env
  2440. (let ((caption-str (org-latex--caption/label-string src-block info))
  2441. (formatted-src (org-export-format-code-default src-block info)))
  2442. (if (org-string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
  2443. (format "\\begin{%s}\n%s\\end{%s}\n"
  2444. custom-env
  2445. (concat (and caption-above-p caption-str)
  2446. formatted-src
  2447. (and (not caption-above-p) caption-str))
  2448. custom-env)
  2449. (format-spec custom-env
  2450. `((?s . ,formatted-src)
  2451. (?c . ,caption)
  2452. (?f . ,float)
  2453. (?l . ,(org-latex--label src-block info)))))))
  2454. ;; Case 3. Use minted package.
  2455. ((eq listings 'minted)
  2456. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2457. (float-env
  2458. (cond
  2459. ((string= "multicolumn" float)
  2460. (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
  2461. (if caption-above-p caption-str "")
  2462. (if caption-above-p "" caption-str)))
  2463. (caption
  2464. (concat (if caption-above-p caption-str "")
  2465. "%s"
  2466. (if caption-above-p "" (concat "\n" caption-str))))
  2467. (t "%s")))
  2468. (options (plist-get info :latex-minted-options))
  2469. (body
  2470. (format
  2471. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  2472. ;; Options.
  2473. (concat
  2474. (org-latex--make-option-string
  2475. (if (or (not num-start) (assoc "linenos" options))
  2476. options
  2477. (append
  2478. `(("linenos")
  2479. ("firstnumber" ,(number-to-string (1+ num-start))))
  2480. options)))
  2481. (let ((local-options (plist-get attributes :options)))
  2482. (and local-options (concat "," local-options))))
  2483. ;; Language.
  2484. (or (cadr (assq (intern lang)
  2485. (plist-get info :latex-minted-langs)))
  2486. (downcase lang))
  2487. ;; Source code.
  2488. (let* ((code-info (org-export-unravel-code src-block))
  2489. (max-width
  2490. (apply 'max
  2491. (mapcar 'length
  2492. (org-split-string (car code-info)
  2493. "\n")))))
  2494. (org-export-format-code
  2495. (car code-info)
  2496. (lambda (loc _num ref)
  2497. (concat
  2498. loc
  2499. (when ref
  2500. ;; Ensure references are flushed to the right,
  2501. ;; separated with 6 spaces from the widest line
  2502. ;; of code.
  2503. (concat (make-string (+ (- max-width (length loc)) 6)
  2504. ?\s)
  2505. (format "(%s)" ref)))))
  2506. nil (and retain-labels (cdr code-info)))))))
  2507. ;; Return value.
  2508. (format float-env body)))
  2509. ;; Case 4. Use listings package.
  2510. (t
  2511. (let ((lst-lang
  2512. (or (cadr (assq (intern lang)
  2513. (plist-get info :latex-listings-langs)))
  2514. lang))
  2515. (caption-str
  2516. (when caption
  2517. (let ((main (org-export-get-caption src-block))
  2518. (secondary (org-export-get-caption src-block t)))
  2519. (if (not secondary)
  2520. (format "{%s}" (org-export-data main info))
  2521. (format "{[%s]%s}"
  2522. (org-export-data secondary info)
  2523. (org-export-data main info))))))
  2524. (lst-opt (plist-get info :latex-listings-options)))
  2525. (concat
  2526. ;; Options.
  2527. (format
  2528. "\\lstset{%s}\n"
  2529. (concat
  2530. (org-latex--make-option-string
  2531. (append
  2532. lst-opt
  2533. (cond
  2534. ((and (not float) (plist-member attributes :float)) nil)
  2535. ((string= "multicolumn" float) '(("float" "*")))
  2536. ((and float (not (assoc "float" lst-opt)))
  2537. `(("float" ,(plist-get info :latex-default-figure-position)))))
  2538. `(("language" ,lst-lang))
  2539. (if label `(("label" ,label)) '(("label" " ")))
  2540. (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
  2541. `(("captionpos" ,(if caption-above-p "t" "b")))
  2542. (cond ((assoc "numbers" lst-opt) nil)
  2543. ((not num-start) '(("numbers" "none")))
  2544. ((zerop num-start) '(("numbers" "left")))
  2545. (t `(("firstnumber" ,(number-to-string (1+ num-start)))
  2546. ("numbers" "left"))))))
  2547. (let ((local-options (plist-get attributes :options)))
  2548. (and local-options (concat "," local-options)))))
  2549. ;; Source code.
  2550. (format
  2551. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  2552. (let* ((code-info (org-export-unravel-code src-block))
  2553. (max-width
  2554. (apply 'max
  2555. (mapcar 'length
  2556. (org-split-string (car code-info) "\n")))))
  2557. (org-export-format-code
  2558. (car code-info)
  2559. (lambda (loc _num ref)
  2560. (concat
  2561. loc
  2562. (when ref
  2563. ;; Ensure references are flushed to the right,
  2564. ;; separated with 6 spaces from the widest line of
  2565. ;; code
  2566. (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
  2567. (format "(%s)" ref)))))
  2568. nil (and retain-labels (cdr code-info))))))))))))
  2569. ;;;; Statistics Cookie
  2570. (defun org-latex-statistics-cookie (statistics-cookie _contents _info)
  2571. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  2572. CONTENTS is nil. INFO is a plist holding contextual information."
  2573. (replace-regexp-in-string
  2574. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  2575. ;;;; Strike-Through
  2576. (defun org-latex-strike-through (_strike-through contents info)
  2577. "Transcode STRIKE-THROUGH from Org to LaTeX.
  2578. CONTENTS is the text with strike-through markup. INFO is a plist
  2579. holding contextual information."
  2580. (org-latex--text-markup contents 'strike-through info))
  2581. ;;;; Subscript
  2582. (defun org-latex--script-size (object info)
  2583. "Transcode a subscript or superscript object.
  2584. OBJECT is an Org object. INFO is a plist used as a communication
  2585. channel."
  2586. (let ((output ""))
  2587. (org-element-map (org-element-contents object)
  2588. (cons 'plain-text org-element-all-objects)
  2589. (lambda (obj)
  2590. (case (org-element-type obj)
  2591. ((entity latex-fragment)
  2592. (let ((data (org-trim (org-export-data obj info))))
  2593. (string-match
  2594. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  2595. data)
  2596. (setq output
  2597. (concat output
  2598. (match-string 1 data)
  2599. (let ((blank (org-element-property :post-blank obj)))
  2600. (and blank (> blank 0) "\\ "))))))
  2601. (plain-text
  2602. (setq output
  2603. (format "%s\\text{%s}" output (org-export-data obj info))))
  2604. (otherwise
  2605. (setq output
  2606. (concat output
  2607. (org-export-data obj info)
  2608. (let ((blank (org-element-property :post-blank obj)))
  2609. (and blank (> blank 0) "\\ ")))))))
  2610. info nil org-element-recursive-objects)
  2611. ;; Result. Do not wrap into curly brackets if OUTPUT is a single
  2612. ;; character.
  2613. (concat (if (eq (org-element-type object) 'subscript) "_" "^")
  2614. (and (> (length output) 1) "{")
  2615. output
  2616. (and (> (length output) 1) "}"))))
  2617. (defun org-latex-subscript (subscript _contents info)
  2618. "Transcode a SUBSCRIPT object from Org to LaTeX.
  2619. CONTENTS is the contents of the object. INFO is a plist holding
  2620. contextual information."
  2621. (org-latex--script-size subscript info))
  2622. ;;;; Superscript
  2623. (defun org-latex-superscript (superscript _contents info)
  2624. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  2625. CONTENTS is the contents of the object. INFO is a plist holding
  2626. contextual information."
  2627. (org-latex--script-size superscript info))
  2628. ;;;; Table
  2629. ;;
  2630. ;; `org-latex-table' is the entry point for table transcoding. It
  2631. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  2632. ;; delegates the job to either `org-latex--table.el-table',
  2633. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  2634. ;; depending of the type of the table and the mode requested.
  2635. ;;
  2636. ;; `org-latex--align-string' is a subroutine used to build alignment
  2637. ;; string for Org tables.
  2638. (defun org-latex-table (table contents info)
  2639. "Transcode a TABLE element from Org to LaTeX.
  2640. CONTENTS is the contents of the table. INFO is a plist holding
  2641. contextual information."
  2642. (if (eq (org-element-property :type table) 'table.el)
  2643. ;; "table.el" table. Convert it using appropriate tools.
  2644. (org-latex--table.el-table table info)
  2645. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  2646. (plist-get info :latex-default-table-mode))))
  2647. (cond
  2648. ;; Case 1: Verbatim table.
  2649. ((string= type "verbatim")
  2650. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  2651. ;; Re-create table, without affiliated keywords.
  2652. (org-trim (org-element-interpret-data
  2653. `(table nil ,@(org-element-contents table))))))
  2654. ;; Case 2: Matrix.
  2655. ((or (string= type "math") (string= type "inline-math"))
  2656. (org-latex--math-table table info))
  2657. ;; Case 3: Standard table.
  2658. (t (concat (org-latex--org-table table contents info)
  2659. ;; When there are footnote references within the
  2660. ;; table, insert their definition just after it.
  2661. (org-latex--delayed-footnotes-definitions table info)))))))
  2662. (defun org-latex--align-string (table info)
  2663. "Return an appropriate LaTeX alignment string.
  2664. TABLE is the considered table. INFO is a plist used as
  2665. a communication channel."
  2666. (or (org-export-read-attribute :attr_latex table :align)
  2667. (let (align)
  2668. ;; Extract column groups and alignment from first (non-rule)
  2669. ;; row.
  2670. (org-element-map
  2671. (org-element-map table 'table-row
  2672. (lambda (row)
  2673. (and (eq (org-element-property :type row) 'standard) row))
  2674. info 'first-match)
  2675. 'table-cell
  2676. (lambda (cell)
  2677. (let ((borders (org-export-table-cell-borders cell info)))
  2678. ;; Check left border for the first cell only.
  2679. (when (and (memq 'left borders) (not align))
  2680. (push "|" align))
  2681. (push (case (org-export-table-cell-alignment cell info)
  2682. (left "l")
  2683. (right "r")
  2684. (center "c"))
  2685. align)
  2686. (when (memq 'right borders) (push "|" align))))
  2687. info)
  2688. (apply 'concat (nreverse align)))))
  2689. (defun org-latex--org-table (table contents info)
  2690. "Return appropriate LaTeX code for an Org table.
  2691. TABLE is the table type element to transcode. CONTENTS is its
  2692. contents, as a string. INFO is a plist used as a communication
  2693. channel.
  2694. This function assumes TABLE has `org' as its `:type' property and
  2695. `table' as its `:mode' attribute."
  2696. (let* ((caption (org-latex--caption/label-string table info))
  2697. (attr (org-export-read-attribute :attr_latex table))
  2698. ;; Determine alignment string.
  2699. (alignment (org-latex--align-string table info))
  2700. ;; Determine environment for the table: longtable, tabular...
  2701. (table-env (or (plist-get attr :environment)
  2702. (plist-get info :latex-default-table-environment)))
  2703. ;; If table is a float, determine environment: table, table*
  2704. ;; or sidewaystable.
  2705. (float-env (unless (member table-env '("longtable" "longtabu"))
  2706. (let ((float (plist-get attr :float)))
  2707. (cond
  2708. ((and (not float) (plist-member attr :float)) nil)
  2709. ((or (string= float "sidewaystable")
  2710. (string= float "sideways")) "sidewaystable")
  2711. ((string= float "multicolumn") "table*")
  2712. ((or float
  2713. (org-element-property :caption table)
  2714. (org-string-nw-p (plist-get attr :caption)))
  2715. "table")))))
  2716. ;; Extract others display options.
  2717. (fontsize (let ((font (plist-get attr :font)))
  2718. (and font (concat font "\n"))))
  2719. ;; "tabular" environment doesn't allow to define a width.
  2720. (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
  2721. (spreadp (plist-get attr :spread))
  2722. (placement
  2723. (or (plist-get attr :placement)
  2724. (format "[%s]" (plist-get info :latex-default-figure-position))))
  2725. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2726. (plist-get info :latex-tables-centered)))
  2727. (caption-above-p (org-latex--caption-above-p table info)))
  2728. ;; Prepare the final format string for the table.
  2729. (cond
  2730. ;; Longtable.
  2731. ((equal "longtable" table-env)
  2732. (concat (and fontsize (concat "{" fontsize))
  2733. (format "\\begin{longtable}{%s}\n" alignment)
  2734. (and caption-above-p
  2735. (org-string-nw-p caption)
  2736. (concat caption "\\\\\n"))
  2737. contents
  2738. (and (not caption-above-p)
  2739. (org-string-nw-p caption)
  2740. (concat caption "\\\\\n"))
  2741. "\\end{longtable}\n"
  2742. (and fontsize "}")))
  2743. ;; Longtabu
  2744. ((equal "longtabu" table-env)
  2745. (concat (and fontsize (concat "{" fontsize))
  2746. (format "\\begin{longtabu}%s{%s}\n"
  2747. (if width
  2748. (format " %s %s "
  2749. (if spreadp "spread" "to") width) "")
  2750. alignment)
  2751. (and caption-above-p
  2752. (org-string-nw-p caption)
  2753. (concat caption "\\\\\n"))
  2754. contents
  2755. (and (not caption-above-p)
  2756. (org-string-nw-p caption)
  2757. (concat caption "\\\\\n"))
  2758. "\\end{longtabu}\n"
  2759. (and fontsize "}")))
  2760. ;; Others.
  2761. (t (concat (cond
  2762. (float-env
  2763. (concat (format "\\begin{%s}%s\n" float-env placement)
  2764. (if caption-above-p caption "")
  2765. (when centerp "\\centering\n")
  2766. fontsize))
  2767. ((and (not float-env) caption)
  2768. (concat
  2769. (and centerp "\\begin{center}\n" )
  2770. (if caption-above-p caption "")
  2771. (cond ((and fontsize centerp) fontsize)
  2772. (fontsize (concat "{" fontsize)))))
  2773. (centerp (concat "\\begin{center}\n" fontsize))
  2774. (fontsize (concat "{" fontsize)))
  2775. (cond ((equal "tabu" table-env)
  2776. (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
  2777. (if width (format
  2778. (if spreadp " spread %s " " to %s ")
  2779. width) "")
  2780. alignment
  2781. contents))
  2782. (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2783. table-env
  2784. (if width (format "{%s}" width) "")
  2785. alignment
  2786. contents
  2787. table-env)))
  2788. (cond
  2789. (float-env
  2790. (concat (if caption-above-p "" (concat "\n" caption))
  2791. (format "\n\\end{%s}" float-env)))
  2792. ((and (not float-env) caption)
  2793. (concat
  2794. (if caption-above-p "" (concat "\n" caption))
  2795. (and centerp "\n\\end{center}")
  2796. (and fontsize (not centerp) "}")))
  2797. (centerp "\n\\end{center}")
  2798. (fontsize "}")))))))
  2799. (defun org-latex--table.el-table (table info)
  2800. "Return appropriate LaTeX code for a table.el table.
  2801. TABLE is the table type element to transcode. INFO is a plist
  2802. used as a communication channel.
  2803. This function assumes TABLE has `table.el' as its `:type'
  2804. property."
  2805. (require 'table)
  2806. ;; Ensure "*org-export-table*" buffer is empty.
  2807. (with-current-buffer (get-buffer-create "*org-export-table*")
  2808. (erase-buffer))
  2809. (let ((output (with-temp-buffer
  2810. (insert (org-element-property :value table))
  2811. (goto-char 1)
  2812. (re-search-forward "^[ \t]*|[^|]" nil t)
  2813. (table-generate-source 'latex "*org-export-table*")
  2814. (with-current-buffer "*org-export-table*"
  2815. (org-trim (buffer-string))))))
  2816. (kill-buffer (get-buffer "*org-export-table*"))
  2817. ;; Remove left out comments.
  2818. (while (string-match "^%.*\n" output)
  2819. (setq output (replace-match "" t t output)))
  2820. (let ((attr (org-export-read-attribute :attr_latex table)))
  2821. (when (plist-get attr :rmlines)
  2822. ;; When the "rmlines" attribute is provided, remove all hlines
  2823. ;; but the the one separating heading from the table body.
  2824. (let ((n 0) (pos 0))
  2825. (while (and (< (length output) pos)
  2826. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2827. (incf n)
  2828. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2829. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2830. (plist-get info :latex-tables-centered))))
  2831. (if (not centerp) output
  2832. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2833. (defun org-latex--math-table (table info)
  2834. "Return appropriate LaTeX code for a matrix.
  2835. TABLE is the table type element to transcode. INFO is a plist
  2836. used as a communication channel.
  2837. This function assumes TABLE has `org' as its `:type' property and
  2838. `inline-math' or `math' as its `:mode' attribute."
  2839. (let* ((attr (org-export-read-attribute :attr_latex table))
  2840. (env (or (plist-get attr :environment)
  2841. (plist-get info :latex-default-table-environment)))
  2842. (contents
  2843. (mapconcat
  2844. (lambda (row)
  2845. ;; Ignore horizontal rules.
  2846. (when (eq (org-element-property :type row) 'standard)
  2847. ;; Return each cell unmodified.
  2848. (concat
  2849. (mapconcat
  2850. (lambda (cell)
  2851. (substring (org-element-interpret-data cell) 0 -1))
  2852. (org-element-map row 'table-cell #'identity info) "&")
  2853. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2854. "\n")))
  2855. (org-element-map table 'table-row #'identity info) "")))
  2856. (concat
  2857. ;; Prefix.
  2858. (plist-get attr :math-prefix)
  2859. ;; Environment. Also treat special cases.
  2860. (cond ((member env '("array" "tabular"))
  2861. (let ((align (make-string
  2862. (cdr (org-export-table-dimensions table info)) ?c)))
  2863. (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
  2864. ((assoc env org-latex-table-matrix-macros)
  2865. (format "\\%s%s{\n%s}"
  2866. env
  2867. (or (plist-get attr :math-arguments) "")
  2868. contents))
  2869. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2870. ;; Suffix.
  2871. (plist-get attr :math-suffix))))
  2872. ;;;; Table Cell
  2873. (defun org-latex-table-cell (table-cell contents info)
  2874. "Transcode a TABLE-CELL element from Org to LaTeX.
  2875. CONTENTS is the cell contents. INFO is a plist used as
  2876. a communication channel."
  2877. (concat
  2878. (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
  2879. (if (and contents
  2880. scientific-format
  2881. (string-match orgtbl-exp-regexp contents))
  2882. ;; Use appropriate format string for scientific
  2883. ;; notation.
  2884. (format scientific-format
  2885. (match-string 1 contents)
  2886. (match-string 2 contents))
  2887. contents))
  2888. (when (org-export-get-next-element table-cell info) " & ")))
  2889. ;;;; Table Row
  2890. (defun org-latex-table-row (table-row contents info)
  2891. "Transcode a TABLE-ROW element from Org to LaTeX.
  2892. CONTENTS is the contents of the row. INFO is a plist used as
  2893. a communication channel."
  2894. (let* ((attr (org-export-read-attribute :attr_latex
  2895. (org-export-get-parent table-row)))
  2896. (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
  2897. (plist-get info :latex-tables-booktabs)))
  2898. (longtablep
  2899. (member (or (plist-get attr :environment)
  2900. (plist-get info :latex-default-table-environment))
  2901. '("longtable" "longtabu"))))
  2902. (if (eq (org-element-property :type table-row) 'rule)
  2903. (cond
  2904. ((not booktabsp) "\\hline")
  2905. ((not (org-export-get-previous-element table-row info)) "\\toprule")
  2906. ((not (org-export-get-next-element table-row info)) "\\bottomrule")
  2907. ((and longtablep
  2908. (org-export-table-row-ends-header-p
  2909. (org-export-get-previous-element table-row info) info))
  2910. "")
  2911. (t "\\midrule"))
  2912. (concat
  2913. ;; When BOOKTABS are activated enforce top-rule even when no
  2914. ;; hline was specifically marked.
  2915. (and booktabsp (not (org-export-get-previous-element table-row info))
  2916. "\\toprule\n")
  2917. contents "\\\\\n"
  2918. (cond
  2919. ;; Special case for long tables. Define header and footers.
  2920. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2921. (let ((columns (cdr (org-export-table-dimensions
  2922. (org-export-get-parent-table table-row) info))))
  2923. (format "%s
  2924. \\endfirsthead
  2925. \\multicolumn{%d}{l}{%s} \\\\
  2926. %s
  2927. %s \\\\\n
  2928. %s
  2929. \\endhead
  2930. %s\\multicolumn{%d}{r}{%s} \\\\
  2931. \\endfoot
  2932. \\endlastfoot"
  2933. (if booktabsp "\\midrule" "\\hline")
  2934. columns
  2935. (org-latex--translate "Continued from previous page" info)
  2936. (cond
  2937. ((not (org-export-table-row-starts-header-p table-row info))
  2938. "")
  2939. (booktabsp "\\toprule\n")
  2940. (t "\\hline\n"))
  2941. contents
  2942. (if booktabsp "\\midrule" "\\hline")
  2943. (if booktabsp "\\midrule" "\\hline")
  2944. columns
  2945. (org-latex--translate "Continued on next page" info))))
  2946. ;; When BOOKTABS are activated enforce bottom rule even when
  2947. ;; no hline was specifically marked.
  2948. ((and booktabsp (not (org-export-get-next-element table-row info)))
  2949. "\\bottomrule"))))))
  2950. ;;;; Target
  2951. (defun org-latex-target (target _contents info)
  2952. "Transcode a TARGET object from Org to LaTeX.
  2953. CONTENTS is nil. INFO is a plist holding contextual
  2954. information."
  2955. (format "\\label{%s}" (org-latex--label target info)))
  2956. ;;;; Timestamp
  2957. (defun org-latex-timestamp (timestamp _contents info)
  2958. "Transcode a TIMESTAMP object from Org to LaTeX.
  2959. CONTENTS is nil. INFO is a plist holding contextual
  2960. information."
  2961. (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
  2962. (format
  2963. (plist-get info
  2964. (case (org-element-property :type timestamp)
  2965. ((active active-range) :latex-active-timestamp-format)
  2966. ((inactive inactive-range) :latex-inactive-timestamp-format)
  2967. (otherwise :latex-diary-timestamp-format)))
  2968. value)))
  2969. ;;;; Underline
  2970. (defun org-latex-underline (_underline contents info)
  2971. "Transcode UNDERLINE from Org to LaTeX.
  2972. CONTENTS is the text with underline markup. INFO is a plist
  2973. holding contextual information."
  2974. (org-latex--text-markup contents 'underline info))
  2975. ;;;; Verbatim
  2976. (defun org-latex-verbatim (verbatim _contents info)
  2977. "Transcode a VERBATIM object from Org to LaTeX.
  2978. CONTENTS is nil. INFO is a plist used as a communication
  2979. channel."
  2980. (org-latex--text-markup
  2981. (org-element-property :value verbatim) 'verbatim info))
  2982. ;;;; Verse Block
  2983. (defun org-latex-verse-block (verse-block contents info)
  2984. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2985. CONTENTS is verse block contents. INFO is a plist holding
  2986. contextual information."
  2987. (org-latex--wrap-label
  2988. verse-block
  2989. ;; In a verse environment, add a line break to each newline
  2990. ;; character and change each white space at beginning of a line
  2991. ;; into a space of 1 em. Also change each blank line with
  2992. ;; a vertical space of 1 em.
  2993. (format "\\begin{verse}\n%s\\end{verse}"
  2994. (replace-regexp-in-string
  2995. "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
  2996. (replace-regexp-in-string
  2997. "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
  2998. (replace-regexp-in-string
  2999. "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
  3000. contents nil t) nil t) nil t))
  3001. info))
  3002. ;;; End-user functions
  3003. ;;;###autoload
  3004. (defun org-latex-export-as-latex
  3005. (&optional async subtreep visible-only body-only ext-plist)
  3006. "Export current buffer as a LaTeX buffer.
  3007. If narrowing is active in the current buffer, only export its
  3008. narrowed part.
  3009. If a region is active, export that region.
  3010. A non-nil optional argument ASYNC means the process should happen
  3011. asynchronously. The resulting buffer should be accessible
  3012. through the `org-export-stack' interface.
  3013. When optional argument SUBTREEP is non-nil, export the sub-tree
  3014. at point, extracting information from the headline properties
  3015. first.
  3016. When optional argument VISIBLE-ONLY is non-nil, don't export
  3017. contents of hidden elements.
  3018. When optional argument BODY-ONLY is non-nil, only write code
  3019. between \"\\begin{document}\" and \"\\end{document}\".
  3020. EXT-PLIST, when provided, is a property list with external
  3021. parameters overriding Org default settings, but still inferior to
  3022. file-local settings.
  3023. Export is done in a buffer named \"*Org LATEX Export*\", which
  3024. will be displayed when `org-export-show-temporary-export-buffer'
  3025. is non-nil."
  3026. (interactive)
  3027. (org-export-to-buffer 'latex "*Org LATEX Export*"
  3028. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  3029. ;;;###autoload
  3030. (defun org-latex-convert-region-to-latex ()
  3031. "Assume the current region has org-mode syntax, and convert it to LaTeX.
  3032. This can be used in any buffer. For example, you can write an
  3033. itemized list in org-mode syntax in an LaTeX buffer and use this
  3034. command to convert it."
  3035. (interactive)
  3036. (org-export-replace-region-by 'latex))
  3037. ;;;###autoload
  3038. (defun org-latex-export-to-latex
  3039. (&optional async subtreep visible-only body-only ext-plist)
  3040. "Export current buffer to a LaTeX file.
  3041. If narrowing is active in the current buffer, only export its
  3042. narrowed part.
  3043. If a region is active, export that region.
  3044. A non-nil optional argument ASYNC means the process should happen
  3045. asynchronously. The resulting file should be accessible through
  3046. the `org-export-stack' interface.
  3047. When optional argument SUBTREEP is non-nil, export the sub-tree
  3048. at point, extracting information from the headline properties
  3049. first.
  3050. When optional argument VISIBLE-ONLY is non-nil, don't export
  3051. contents of hidden elements.
  3052. When optional argument BODY-ONLY is non-nil, only write code
  3053. between \"\\begin{document}\" and \"\\end{document}\".
  3054. EXT-PLIST, when provided, is a property list with external
  3055. parameters overriding Org default settings, but still inferior to
  3056. file-local settings."
  3057. (interactive)
  3058. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3059. (org-export-to-file 'latex outfile
  3060. async subtreep visible-only body-only ext-plist)))
  3061. ;;;###autoload
  3062. (defun org-latex-export-to-pdf
  3063. (&optional async subtreep visible-only body-only ext-plist)
  3064. "Export current buffer to LaTeX then process through to PDF.
  3065. If narrowing is active in the current buffer, only export its
  3066. narrowed part.
  3067. If a region is active, export that region.
  3068. A non-nil optional argument ASYNC means the process should happen
  3069. asynchronously. The resulting file should be accessible through
  3070. the `org-export-stack' interface.
  3071. When optional argument SUBTREEP is non-nil, export the sub-tree
  3072. at point, extracting information from the headline properties
  3073. first.
  3074. When optional argument VISIBLE-ONLY is non-nil, don't export
  3075. contents of hidden elements.
  3076. When optional argument BODY-ONLY is non-nil, only write code
  3077. between \"\\begin{document}\" and \"\\end{document}\".
  3078. EXT-PLIST, when provided, is a property list with external
  3079. parameters overriding Org default settings, but still inferior to
  3080. file-local settings.
  3081. Return PDF file's name."
  3082. (interactive)
  3083. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3084. (org-export-to-file 'latex outfile
  3085. async subtreep visible-only body-only ext-plist
  3086. (lambda (file) (org-latex-compile file)))))
  3087. (defun org-latex-compile (texfile &optional snippet)
  3088. "Compile a TeX file.
  3089. TEXFILE is the name of the file being compiled. Processing is
  3090. done through the command specified in `org-latex-pdf-process'.
  3091. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  3092. file used to preview a LaTeX snippet. In this case, do not
  3093. create a log buffer and do not bother removing log files.
  3094. Return PDF file name or an error if it couldn't be produced."
  3095. (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
  3096. (full-name (file-truename texfile))
  3097. (compiler (or (with-temp-buffer
  3098. (save-excursion (insert-file-contents full-name))
  3099. (when (and (search-forward-regexp
  3100. (regexp-opt org-latex-compilers) (line-end-position 2) t)
  3101. (progn (beginning-of-line)
  3102. (looking-at-p "%")))
  3103. (match-string 0)))
  3104. "pdflatex"))
  3105. (out-dir (file-name-directory texfile))
  3106. ;; Properly set working directory for compilation.
  3107. (default-directory (if (file-name-absolute-p texfile)
  3108. (file-name-directory full-name)
  3109. default-directory))
  3110. (time (current-time))
  3111. warnings)
  3112. (unless snippet (message "Processing LaTeX file %s..." texfile))
  3113. (save-window-excursion
  3114. (cond
  3115. ;; A function is provided: Apply it.
  3116. ((functionp org-latex-pdf-process)
  3117. (funcall org-latex-pdf-process (shell-quote-argument texfile)))
  3118. ;; A list is provided: Replace %b, %f and %o with appropriate
  3119. ;; values in each command before applying it. Note that while
  3120. ;; "%latex" and "%bibtex" is used in `org-latex-pdf-process',
  3121. ;; they are replaced with "%L" and "%B" to adhere to
  3122. ;; format-spec. Output is redirected to "*Org PDF LaTeX
  3123. ;; Output*" buffer.
  3124. ((consp org-latex-pdf-process)
  3125. (let ((outbuf (and (not snippet)
  3126. (get-buffer-create "*Org PDF LaTeX Output*")))
  3127. (spec (list (cons ?B (shell-quote-argument org-latex-bib-compiler))
  3128. (cons ?L (shell-quote-argument compiler))
  3129. (cons ?b (shell-quote-argument base-name))
  3130. (cons ?f (shell-quote-argument full-name))
  3131. (cons ?o (shell-quote-argument out-dir)))))
  3132. (dolist (command org-latex-pdf-process)
  3133. (let ((c (replace-regexp-in-string
  3134. "%\\(latex\\|bibtex\\)\\>"
  3135. (lambda (str) (upcase (substring str 0 2)))
  3136. command)))
  3137. (shell-command (format-spec c spec) outbuf)))
  3138. ;; Collect standard errors from output buffer.
  3139. (setq warnings (and (not snippet)
  3140. (org-latex--collect-warnings outbuf)))))
  3141. (t (error "No valid command to process to PDF")))
  3142. (let ((pdffile (concat out-dir base-name ".pdf")))
  3143. ;; Check for process failure. Provide collected errors if
  3144. ;; possible.
  3145. (if (or (not (file-exists-p pdffile))
  3146. (time-less-p (nth 5 (file-attributes pdffile)) time))
  3147. (error (format "PDF file %s wasn't produced" pdffile))
  3148. ;; Else remove log files, when specified, and signal end of
  3149. ;; process to user, along with any error encountered.
  3150. (unless snippet
  3151. (when org-latex-remove-logfiles
  3152. (dolist (file (directory-files
  3153. out-dir t
  3154. (concat (regexp-quote base-name)
  3155. "\\(?:\\.[0-9]+\\)?"
  3156. "\\."
  3157. (regexp-opt org-latex-logfiles-extensions))))
  3158. (delete-file file)))
  3159. (message (concat "PDF file produced"
  3160. (cond
  3161. ((eq warnings 'error) " with errors.")
  3162. (warnings (concat " with warnings: " warnings))
  3163. (t "."))))))
  3164. ;; Return output file name.
  3165. pdffile))))
  3166. (defun org-latex--collect-warnings (buffer)
  3167. "Collect some warnings from \"pdflatex\" command output.
  3168. BUFFER is the buffer containing output. Return collected
  3169. warnings types as a string, `error' if a LaTeX error was
  3170. encountered or nil if there was none."
  3171. (with-current-buffer buffer
  3172. (save-excursion
  3173. (goto-char (point-max))
  3174. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  3175. (if (re-search-forward "^!" nil t) 'error
  3176. (let ((case-fold-search t)
  3177. (warnings ""))
  3178. (dolist (warning org-latex-known-warnings)
  3179. (when (save-excursion (re-search-forward (car warning) nil t))
  3180. (setq warnings (concat warnings " " (cdr warning)))))
  3181. (org-string-nw-p (org-trim warnings))))))))
  3182. ;;;###autoload
  3183. (defun org-latex-publish-to-latex (plist filename pub-dir)
  3184. "Publish an Org file to LaTeX.
  3185. FILENAME is the filename of the Org file to be published. PLIST
  3186. is the property list for the given project. PUB-DIR is the
  3187. publishing directory.
  3188. Return output file name."
  3189. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  3190. ;;;###autoload
  3191. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  3192. "Publish an Org file to PDF (via LaTeX).
  3193. FILENAME is the filename of the Org file to be published. PLIST
  3194. is the property list for the given project. PUB-DIR is the
  3195. publishing directory.
  3196. Return output file name."
  3197. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  3198. ;; in working directory and then moved to publishing directory.
  3199. (org-publish-attachment
  3200. plist
  3201. (org-latex-compile
  3202. (org-publish-org-to
  3203. 'latex filename ".tex" plist (file-name-directory filename)))
  3204. pub-dir))
  3205. (provide 'ox-latex)
  3206. ;; Local variables:
  3207. ;; generated-autoload-file: "org-loaddefs.el"
  3208. ;; End:
  3209. ;;; ox-latex.el ends here