ox-latex.el 163 KB

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