ox-latex.el 163 KB

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