ox-latex.el 154 KB

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