ox-latex.el 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278
  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. (`src-block "lst:")
  1332. (_ nil))
  1333. (org-export-get-reference datum info))))))
  1334. (cond ((not full) label)
  1335. (label (format "\\label{%s}%s"
  1336. label
  1337. (if (eq type 'target) "" "\n")))
  1338. (t ""))))
  1339. (defun org-latex--caption/label-string (element info)
  1340. "Return caption and label LaTeX string for ELEMENT.
  1341. INFO is a plist holding contextual information. If there's no
  1342. caption nor label, return the empty string.
  1343. For non-floats, see `org-latex--wrap-label'."
  1344. (let* ((label (org-latex--label element info nil t))
  1345. (main (org-export-get-caption element))
  1346. (attr (org-export-read-attribute :attr_latex element))
  1347. (type (org-element-type element))
  1348. (nonfloat (or (and (plist-member attr :float)
  1349. (not (plist-get attr :float))
  1350. main)
  1351. (and (eq type 'src-block)
  1352. (not (plist-get attr :float))
  1353. (memq (plist-get info :latex-src-block-backend)
  1354. '(verbatim nil)))))
  1355. (short (org-export-get-caption element t))
  1356. (caption-from-attr-latex (plist-get attr :caption)))
  1357. (cond
  1358. ((org-string-nw-p caption-from-attr-latex)
  1359. (concat caption-from-attr-latex "\n"))
  1360. ((and (not main) (equal label "")) "")
  1361. ((not main) label)
  1362. ;; Option caption format with short name.
  1363. (t
  1364. (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
  1365. "\\caption%s%s{%s%s}\n")
  1366. (let ((type* (if (eq type 'latex-environment)
  1367. (org-latex--environment-type element)
  1368. type)))
  1369. (if nonfloat
  1370. (cl-case type*
  1371. (paragraph "figure")
  1372. (image "figure")
  1373. (special-block "figure")
  1374. (src-block (if (not (memq (plist-get info :latex-src-block-backend)
  1375. '(verbatim nil)))
  1376. "listing"
  1377. "figure"))
  1378. (t (symbol-name type*)))
  1379. ""))
  1380. (if short (format "[%s]" (org-export-data short info)) "")
  1381. (org-trim label)
  1382. (org-export-data main info))))))
  1383. (defun org-latex-guess-inputenc (header)
  1384. "Set the coding system in inputenc to what the buffer is.
  1385. HEADER is the LaTeX header string. This function only applies
  1386. when specified inputenc option is \"AUTO\".
  1387. Return the new header, as a string."
  1388. (let* ((cs (or (ignore-errors
  1389. (latexenc-coding-system-to-inputenc
  1390. (or org-export-coding-system buffer-file-coding-system)))
  1391. "utf8")))
  1392. (if (not cs) header
  1393. ;; First translate if that is requested.
  1394. (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
  1395. ;; Then find the \usepackage statement and replace the option.
  1396. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  1397. cs header t nil 1))))
  1398. (defun org-latex-guess-babel-language (header info)
  1399. "Set Babel's language according to LANGUAGE keyword.
  1400. HEADER is the LaTeX header string. INFO is the plist used as
  1401. a communication channel.
  1402. Insertion of guessed language only happens when Babel package has
  1403. explicitly been loaded. Then it is added to the rest of
  1404. package's options.
  1405. The argument to Babel may be \"AUTO\" which is then replaced with
  1406. the language of the document or `org-export-default-language'
  1407. unless language in question is already loaded.
  1408. Return the new header."
  1409. (let ((language-code (plist-get info :language)))
  1410. ;; If no language is set or Babel package is not loaded, return
  1411. ;; HEADER as-is.
  1412. (if (or (not (stringp language-code))
  1413. (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
  1414. header
  1415. (let ((options (save-match-data
  1416. (org-split-string (match-string 1 header) ",[ \t]*")))
  1417. (language (cdr (assoc-string language-code
  1418. org-latex-babel-language-alist t))))
  1419. ;; If LANGUAGE is already loaded, return header without AUTO.
  1420. ;; Otherwise, replace AUTO with language or append language if
  1421. ;; AUTO is not present.
  1422. (replace-match
  1423. (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
  1424. (cond ((member language options) (delete "AUTO" options))
  1425. ((member "AUTO" options) options)
  1426. (t (append options (list language))))
  1427. ", ")
  1428. t nil header 1)))))
  1429. (defun org-latex-guess-polyglossia-language (header info)
  1430. "Set the Polyglossia language according to the LANGUAGE keyword.
  1431. HEADER is the LaTeX header string. INFO is the plist used as
  1432. a communication channel.
  1433. Insertion of guessed language only happens when the Polyglossia
  1434. package has been explicitly loaded.
  1435. The argument to Polyglossia may be \"AUTO\" which is then
  1436. replaced with the language of the document or
  1437. `org-export-default-language'. Note, the language is really set
  1438. using \setdefaultlanguage and not as an option to the package.
  1439. Return the new header."
  1440. (let ((language (plist-get info :language)))
  1441. ;; If no language is set or Polyglossia is not loaded, return
  1442. ;; HEADER as-is.
  1443. (if (or (not (stringp language))
  1444. (not (string-match
  1445. "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
  1446. header)))
  1447. header
  1448. (let* ((options (org-string-nw-p (match-string 1 header)))
  1449. (languages (and options
  1450. ;; Reverse as the last loaded language is
  1451. ;; the main language.
  1452. (nreverse
  1453. (delete-dups
  1454. (save-match-data
  1455. (org-split-string
  1456. (replace-regexp-in-string
  1457. "AUTO" language options t)
  1458. ",[ \t]*"))))))
  1459. (main-language-set
  1460. (string-match-p "\\\\setmainlanguage{.*?}" header)))
  1461. (replace-match
  1462. (concat "\\usepackage{polyglossia}\n"
  1463. (mapconcat
  1464. (lambda (l)
  1465. (let ((l (or (assoc l org-latex-polyglossia-language-alist)
  1466. l)))
  1467. (format (if main-language-set "\\setotherlanguage%s{%s}\n"
  1468. (setq main-language-set t)
  1469. "\\setmainlanguage%s{%s}\n")
  1470. (if (and (consp l) (= (length l) 3))
  1471. (format "[variant=%s]" (nth 2 l))
  1472. "")
  1473. (nth 1 l))))
  1474. languages
  1475. ""))
  1476. t t header 0)))))
  1477. (defun org-latex--remove-packages (pkg-alist info)
  1478. "Remove packages based on the current LaTeX compiler.
  1479. PKG-ALIST is a list of packages, as in `org-latex-packages-alist'
  1480. and `org-latex-default-packages-alist'. If the fourth argument
  1481. of a package is neither nil nor a member of the LaTeX compiler
  1482. associated to the document, the package is removed.
  1483. Return new list of packages."
  1484. (let ((compiler (or (plist-get info :latex-compiler) "")))
  1485. (if (not (member-ignore-case compiler org-latex-compilers)) pkg-alist
  1486. (cl-remove-if-not
  1487. (lambda (package)
  1488. (pcase package
  1489. (`(,_ ,_ ,_ nil) t)
  1490. (`(,_ ,_ ,_ ,compilers) (member-ignore-case compiler compilers))
  1491. (_ t)))
  1492. pkg-alist))))
  1493. (defun org-latex--find-verb-separator (s)
  1494. "Return a character not used in string S.
  1495. This is used to choose a separator for constructs like \\verb."
  1496. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1497. (cl-loop for c across ll
  1498. when (not (string-match (regexp-quote (char-to-string c)) s))
  1499. return (char-to-string c))))
  1500. (defun org-latex--make-option-string (options &optional seperator)
  1501. "Return a comma separated string of keywords and values.
  1502. OPTIONS is an alist where the key is the options keyword as
  1503. a string, and the value a list containing the keyword value, or
  1504. nil."
  1505. (mapconcat (lambda (pair)
  1506. (let ((keyword (car pair))
  1507. (value (pcase (cdr pair)
  1508. ((pred stringp) (cdr pair))
  1509. ((pred consp) (cadr pair)))))
  1510. (concat keyword
  1511. (when value
  1512. (concat "="
  1513. (if (string-match-p (rx (any "[]")) value)
  1514. (format "{%s}" value)
  1515. value))))))
  1516. options
  1517. (or seperator ",")))
  1518. (defun org-latex--wrap-label (element output info)
  1519. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  1520. INFO is the current export state, as a plist. This function
  1521. should not be used for floats. See
  1522. `org-latex--caption/label-string'."
  1523. (if (not (and (org-string-nw-p output) (org-element-property :name element)))
  1524. output
  1525. (concat (format "\\phantomsection\n\\label{%s}\n"
  1526. (org-latex--label element info))
  1527. output)))
  1528. (defun org-latex--protect-text (text)
  1529. "Protect special characters in string TEXT and return it."
  1530. (replace-regexp-in-string "[\\{}$%&_#~^]" "\\\\\\&" text))
  1531. (defun org-latex--text-markup (text markup info)
  1532. "Format TEXT depending on MARKUP text markup.
  1533. INFO is a plist used as a communication channel. See
  1534. `org-latex-text-markup-alist' for details."
  1535. (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
  1536. (cl-case fmt
  1537. ;; No format string: Return raw text.
  1538. ((nil) text)
  1539. ;; Handle the `verb' special case: Find an appropriate separator
  1540. ;; and use "\\verb" command.
  1541. (verb
  1542. (let ((separator (org-latex--find-verb-separator text)))
  1543. (concat "\\verb"
  1544. separator
  1545. (replace-regexp-in-string "\n" " " text)
  1546. separator)))
  1547. (protectedtexttt (org-latex--protect-texttt text))
  1548. ;; Else use format string.
  1549. (t (format fmt text)))))
  1550. (defun org-latex--protect-texttt (text)
  1551. "Protect special chars, then wrap TEXT in \"\\texttt{}\"."
  1552. (format "\\texttt{%s}"
  1553. (replace-regexp-in-string
  1554. "--\\|[\\{}$%&_#~^]"
  1555. (lambda (m)
  1556. (cond ((equal m "--") "-{}-")
  1557. ((equal m "\\") "\\textbackslash{}")
  1558. ((equal m "~") "\\textasciitilde{}")
  1559. ((equal m "^") "\\textasciicircum{}")
  1560. (t (org-latex--protect-text m))))
  1561. text nil t)))
  1562. (defun org-latex--delayed-footnotes-definitions (element info)
  1563. "Return footnotes definitions in ELEMENT as a string.
  1564. INFO is a plist used as a communication channel.
  1565. Footnotes definitions are returned within \"\\footnotetext{}\"
  1566. commands.
  1567. This function is used within constructs that don't support
  1568. \"\\footnote{}\" command (e.g., an item tag). In that case,
  1569. \"\\footnotemark\" is used within the construct and the function
  1570. just outside of it."
  1571. (mapconcat
  1572. (lambda (ref)
  1573. (let ((def (org-export-get-footnote-definition ref info)))
  1574. (format "\\footnotetext[%d]{%s%s}"
  1575. (org-export-get-footnote-number ref info)
  1576. (org-trim (org-latex--label def info t t))
  1577. (org-trim (org-export-data def info)))))
  1578. ;; Find every footnote reference in ELEMENT.
  1579. (letrec ((all-refs nil)
  1580. (search-refs
  1581. (lambda (data)
  1582. ;; Return a list of all footnote references never seen
  1583. ;; before in DATA.
  1584. (org-element-map data 'footnote-reference
  1585. (lambda (ref)
  1586. (when (org-export-footnote-first-reference-p ref info)
  1587. (push ref all-refs)
  1588. (when (eq (org-element-property :type ref) 'standard)
  1589. (funcall search-refs
  1590. (org-export-get-footnote-definition ref info)))))
  1591. info)
  1592. (reverse all-refs))))
  1593. (funcall search-refs element))
  1594. ""))
  1595. (defun org-latex--translate (s info)
  1596. "Translate string S according to specified language.
  1597. INFO is a plist used as a communication channel."
  1598. (org-export-translate s :latex info))
  1599. (defun org-latex--format-spec (info)
  1600. "Create a format-spec for document meta-data.
  1601. INFO is a plist used as a communication channel."
  1602. (let ((language (let ((lang (plist-get info :language)))
  1603. (or (cdr (assoc-string lang org-latex-babel-language-alist t))
  1604. (nth 1 (assoc-string lang org-latex-polyglossia-language-alist t))
  1605. lang))))
  1606. `((?a . ,(org-export-data (plist-get info :author) info))
  1607. (?t . ,(org-export-data (plist-get info :title) info))
  1608. (?s . ,(org-export-data (plist-get info :subtitle) info))
  1609. (?k . ,(org-export-data (org-latex--wrap-latex-math-block
  1610. (plist-get info :keywords) info)
  1611. info))
  1612. (?d . ,(org-export-data (org-latex--wrap-latex-math-block
  1613. (plist-get info :description) info)
  1614. info))
  1615. (?c . ,(plist-get info :creator))
  1616. (?l . ,language)
  1617. (?L . ,(capitalize language))
  1618. (?D . ,(org-export-get-date info)))))
  1619. (defun org-latex--insert-compiler (info)
  1620. "Insert LaTeX_compiler info into the document.
  1621. INFO is a plist used as a communication channel."
  1622. (let ((compiler (plist-get info :latex-compiler)))
  1623. (and (org-string-nw-p org-latex-compiler-file-string)
  1624. (member (or compiler "") org-latex-compilers)
  1625. (format org-latex-compiler-file-string compiler))))
  1626. ;;; Filters
  1627. (defun org-latex-matrices-tree-filter (tree _backend info)
  1628. (org-latex--wrap-latex-matrices tree info))
  1629. (defun org-latex-math-block-tree-filter (tree _backend info)
  1630. (org-latex--wrap-latex-math-block tree info))
  1631. (defun org-latex-math-block-options-filter (info _backend)
  1632. (dolist (prop '(:author :date :title) info)
  1633. (plist-put info prop
  1634. (org-latex--wrap-latex-math-block (plist-get info prop) info))))
  1635. (defun org-latex-clean-invalid-line-breaks (data _backend _info)
  1636. (replace-regexp-in-string
  1637. "\\(\\\\end{[A-Za-z0-9*]+}\\|^\\)[ \t]*\\\\\\\\[ \t]*$" "\\1"
  1638. data))
  1639. ;;; Template
  1640. ;;;###autoload
  1641. (defun org-latex-make-preamble (info &optional template snippet?)
  1642. "Return a formatted LaTeX preamble.
  1643. INFO is a plist used as a communication channel. Optional
  1644. argument TEMPLATE, when non-nil, is the header template string,
  1645. as expected by `org-splice-latex-header'. When SNIPPET? is
  1646. non-nil, only includes packages relevant to image generation, as
  1647. specified in `org-latex-default-packages-alist' or
  1648. `org-latex-packages-alist'."
  1649. (let* ((class (plist-get info :latex-class))
  1650. (class-template
  1651. (or template
  1652. (let* ((class-options (plist-get info :latex-class-options))
  1653. (header (nth 1 (assoc class (plist-get info :latex-classes)))))
  1654. (and (stringp header)
  1655. (if (not class-options) header
  1656. (replace-regexp-in-string
  1657. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  1658. class-options header t nil 1))))
  1659. (user-error "Unknown LaTeX class `%s'" class))))
  1660. (org-latex-guess-polyglossia-language
  1661. (org-latex-guess-babel-language
  1662. (org-latex-guess-inputenc
  1663. (org-element-normalize-string
  1664. (org-splice-latex-header
  1665. class-template
  1666. (org-latex--remove-packages org-latex-default-packages-alist info)
  1667. (org-latex--remove-packages org-latex-packages-alist info)
  1668. snippet?
  1669. (mapconcat #'org-element-normalize-string
  1670. (list (plist-get info :latex-header)
  1671. (and (not snippet?)
  1672. (plist-get info :latex-header-extra)))
  1673. ""))))
  1674. info)
  1675. info)))
  1676. (defun org-latex-template (contents info)
  1677. "Return complete document string after LaTeX conversion.
  1678. CONTENTS is the transcoded contents string. INFO is a plist
  1679. holding export options."
  1680. (let ((title (org-export-data (plist-get info :title) info))
  1681. (spec (org-latex--format-spec info)))
  1682. (concat
  1683. ;; Time-stamp.
  1684. (and (plist-get info :time-stamp-file)
  1685. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  1686. ;; LaTeX compiler.
  1687. (org-latex--insert-compiler info)
  1688. ;; Document class and packages.
  1689. (org-latex-make-preamble info)
  1690. ;; Possibly limit depth for headline numbering.
  1691. (let ((sec-num (plist-get info :section-numbers)))
  1692. (when (integerp sec-num)
  1693. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  1694. ;; Author.
  1695. (let ((author (and (plist-get info :with-author)
  1696. (let ((auth (plist-get info :author)))
  1697. (and auth (org-export-data auth info)))))
  1698. (email (and (plist-get info :with-email)
  1699. (org-export-data (plist-get info :email) info))))
  1700. (cond ((and author email (not (string= "" email)))
  1701. (format "\\author{%s\\thanks{%s}}\n" author email))
  1702. ((or author email) (format "\\author{%s}\n" (or author email)))))
  1703. ;; Date.
  1704. (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
  1705. (format "\\date{%s}\n" (org-export-data date info)))
  1706. ;; Title and subtitle.
  1707. (let* ((subtitle (plist-get info :subtitle))
  1708. (formatted-subtitle
  1709. (when subtitle
  1710. (format (plist-get info :latex-subtitle-format)
  1711. (org-export-data subtitle info))))
  1712. (separate (plist-get info :latex-subtitle-separate)))
  1713. (concat
  1714. (format "\\title{%s%s}\n" title
  1715. (if separate "" (or formatted-subtitle "")))
  1716. (when (and separate subtitle)
  1717. (concat formatted-subtitle "\n"))))
  1718. ;; Hyperref options.
  1719. (let ((template (plist-get info :latex-hyperref-template)))
  1720. (and (stringp template)
  1721. (format-spec template spec)))
  1722. ;; engrave-faces-latex preamble
  1723. (when (and (eq org-latex-src-block-backend 'engraved)
  1724. (org-element-map (plist-get info :parse-tree)
  1725. '(src-block inline-src-block) #'identity
  1726. info t))
  1727. (org-latex-generate-engraved-preamble info t))
  1728. ;; Document start.
  1729. "\\begin{document}\n\n"
  1730. ;; Title command.
  1731. (let* ((title-command (plist-get info :latex-title-command))
  1732. (command (and (stringp title-command)
  1733. (format-spec title-command spec))))
  1734. (org-element-normalize-string
  1735. (cond ((not (plist-get info :with-title)) nil)
  1736. ((string= "" title) nil)
  1737. ((not (stringp command)) nil)
  1738. ((string-match "\\(?:[^%]\\|^\\)%s" command)
  1739. (format command title))
  1740. (t command))))
  1741. ;; Table of contents.
  1742. (let ((depth (plist-get info :with-toc)))
  1743. (when depth
  1744. (concat (when (integerp depth)
  1745. (format "\\setcounter{tocdepth}{%d}\n" depth))
  1746. (plist-get info :latex-toc-command))))
  1747. ;; Document's body.
  1748. contents
  1749. ;; Creator.
  1750. (and (plist-get info :with-creator)
  1751. (concat (plist-get info :creator) "\n"))
  1752. ;; Document end.
  1753. "\\end{document}")))
  1754. ;;; Transcode Functions
  1755. ;;;; Bold
  1756. (defun org-latex-bold (_bold contents info)
  1757. "Transcode BOLD from Org to LaTeX.
  1758. CONTENTS is the text with bold markup. INFO is a plist holding
  1759. contextual information."
  1760. (org-latex--text-markup contents 'bold info))
  1761. ;;;; Center Block
  1762. (defun org-latex-center-block (center-block contents info)
  1763. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  1764. CONTENTS holds the contents of the center block. INFO is a plist
  1765. holding contextual information."
  1766. (org-latex--wrap-label
  1767. center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
  1768. ;;;; Clock
  1769. (defun org-latex-clock (clock _contents info)
  1770. "Transcode a CLOCK element from Org to LaTeX.
  1771. CONTENTS is nil. INFO is a plist holding contextual
  1772. information."
  1773. (concat
  1774. "\\noindent"
  1775. (format "\\textbf{%s} " org-clock-string)
  1776. (format (plist-get info :latex-inactive-timestamp-format)
  1777. (concat (org-timestamp-translate (org-element-property :value clock))
  1778. (let ((time (org-element-property :duration clock)))
  1779. (and time (format " (%s)" time)))))
  1780. "\\\\"))
  1781. ;;;; Code
  1782. (defun org-latex-code (code _contents info)
  1783. "Transcode a CODE object from Org to LaTeX.
  1784. CONTENTS is nil. INFO is a plist used as a communication
  1785. channel."
  1786. (org-latex--text-markup (org-element-property :value code) 'code info))
  1787. ;;;; Drawer
  1788. (defun org-latex-drawer (drawer contents info)
  1789. "Transcode a DRAWER element from Org to LaTeX.
  1790. CONTENTS holds the contents of the block. INFO is a plist
  1791. holding contextual information."
  1792. (let* ((name (org-element-property :drawer-name drawer))
  1793. (output (funcall (plist-get info :latex-format-drawer-function)
  1794. name contents)))
  1795. (org-latex--wrap-label drawer output info)))
  1796. ;;;; Dynamic Block
  1797. (defun org-latex-dynamic-block (dynamic-block contents info)
  1798. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1799. CONTENTS holds the contents of the block. INFO is a plist
  1800. holding contextual information. See `org-export-data'."
  1801. (org-latex--wrap-label dynamic-block contents info))
  1802. ;;;; Entity
  1803. (defun org-latex-entity (entity _contents _info)
  1804. "Transcode an ENTITY object from Org to LaTeX.
  1805. CONTENTS are the definition itself. INFO is a plist holding
  1806. contextual information."
  1807. (org-element-property :latex entity))
  1808. ;;;; Example Block
  1809. (defun org-latex-example-block (example-block _contents info)
  1810. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1811. CONTENTS is nil. INFO is a plist holding contextual
  1812. information."
  1813. (when (org-string-nw-p (org-element-property :value example-block))
  1814. (let ((environment (or (org-export-read-attribute
  1815. :attr_latex example-block :environment)
  1816. "verbatim")))
  1817. (org-latex--wrap-label
  1818. example-block
  1819. (format "\\begin{%s}\n%s\\end{%s}"
  1820. environment
  1821. (org-export-format-code-default example-block info)
  1822. environment)
  1823. info))))
  1824. ;;;; Export Block
  1825. (defun org-latex-export-block (export-block _contents _info)
  1826. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1827. CONTENTS is nil. INFO is a plist holding contextual information."
  1828. (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
  1829. (org-remove-indentation (org-element-property :value export-block))))
  1830. ;;;; Export Snippet
  1831. (defun org-latex-export-snippet (export-snippet _contents _info)
  1832. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1833. CONTENTS is nil. INFO is a plist holding contextual information."
  1834. (when (eq (org-export-snippet-backend export-snippet) 'latex)
  1835. (org-element-property :value export-snippet)))
  1836. ;;;; Fixed Width
  1837. (defun org-latex-fixed-width (fixed-width _contents info)
  1838. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1839. CONTENTS is nil. INFO is a plist holding contextual information."
  1840. (org-latex--wrap-label
  1841. fixed-width
  1842. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1843. (org-remove-indentation
  1844. (org-element-property :value fixed-width)))
  1845. info))
  1846. ;;;; Footnote Reference
  1847. (defun org-latex-footnote-reference (footnote-reference _contents info)
  1848. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1849. CONTENTS is nil. INFO is a plist holding contextual information."
  1850. (let ((label (org-element-property :label footnote-reference)))
  1851. (concat
  1852. ;; Insert separator between two footnotes in a row.
  1853. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1854. (when (eq (org-element-type prev) 'footnote-reference)
  1855. (plist-get info :latex-footnote-separator)))
  1856. (cond
  1857. ;; Use `:latex-footnote-defined-format' if the footnote has
  1858. ;; already been defined.
  1859. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1860. (format (plist-get info :latex-footnote-defined-format)
  1861. (org-latex--label
  1862. (org-export-get-footnote-definition footnote-reference info)
  1863. info t)))
  1864. ;; Use \footnotemark if reference is within another footnote
  1865. ;; reference, footnote definition, table cell, verse block, or
  1866. ;; item's tag.
  1867. ((or (org-element-lineage footnote-reference
  1868. '(footnote-reference footnote-definition
  1869. table-cell verse-block))
  1870. (eq 'item (org-element-type
  1871. (org-export-get-parent-element footnote-reference))))
  1872. "\\footnotemark")
  1873. ;; Otherwise, define it with \footnote command.
  1874. (t
  1875. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1876. (concat
  1877. (format "\\footnote{%s%s}" (org-trim (org-export-data def info))
  1878. ;; Only insert a \label if there exist another
  1879. ;; reference to def.
  1880. (cond ((not label) "")
  1881. ((org-element-map (plist-get info :parse-tree)
  1882. 'footnote-reference
  1883. (lambda (f)
  1884. (and (not (eq f footnote-reference))
  1885. (equal (org-element-property :label f) label)
  1886. (org-trim (org-latex--label def info t t))))
  1887. info t))
  1888. (t "")))
  1889. ;; Retrieve all footnote references within the footnote and
  1890. ;; add their definition after it, since LaTeX doesn't support
  1891. ;; them inside.
  1892. (org-latex--delayed-footnotes-definitions def info))))))))
  1893. ;;;; Headline
  1894. (defun org-latex-headline (headline contents info)
  1895. "Transcode a HEADLINE element from Org to LaTeX.
  1896. CONTENTS holds the contents of the headline. INFO is a plist
  1897. holding contextual information."
  1898. (unless (org-element-property :footnote-section-p headline)
  1899. (let* ((class (plist-get info :latex-class))
  1900. (level (org-export-get-relative-level headline info))
  1901. (numberedp (org-export-numbered-headline-p headline info))
  1902. (class-sectioning (assoc class (plist-get info :latex-classes)))
  1903. ;; Section formatting will set two placeholders: one for
  1904. ;; the title and the other for the contents.
  1905. (section-fmt
  1906. (let ((sec (if (functionp (nth 2 class-sectioning))
  1907. (funcall (nth 2 class-sectioning) level numberedp)
  1908. (nth (1+ level) class-sectioning))))
  1909. (cond
  1910. ;; No section available for that LEVEL.
  1911. ((not sec) nil)
  1912. ;; Section format directly returned by a function. Add
  1913. ;; placeholder for contents.
  1914. ((stringp sec) (concat sec "\n%s"))
  1915. ;; (numbered-section . unnumbered-section)
  1916. ((not (consp (cdr sec)))
  1917. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1918. ;; (numbered-open numbered-close)
  1919. ((= (length sec) 2)
  1920. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1921. ;; (num-in num-out no-num-in no-num-out)
  1922. ((= (length sec) 4)
  1923. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1924. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1925. ;; Create a temporary export back-end that hard-codes
  1926. ;; "\underline" within "\section" and alike.
  1927. (section-back-end
  1928. (org-export-create-backend
  1929. :parent 'latex
  1930. :transcoders
  1931. '((underline . (lambda (o c i) (format "\\underline{%s}" c)))
  1932. ;; LaTeX isn't happy when you try to use \verb inside the argument of other
  1933. ;; commands (like \section, etc.), and this causes compilation to fail.
  1934. ;; So, within headings it's a good idea to replace any instances of \verb
  1935. ;; with \texttt.
  1936. (code . (lambda (o _ _) (org-latex--protect-texttt (org-element-property :value o))))
  1937. (verbatim . (lambda (o _ _) (org-latex--protect-texttt (org-element-property :value o)))))))
  1938. (text
  1939. (org-export-data-with-backend
  1940. (org-element-property :title headline) section-back-end info))
  1941. (todo
  1942. (and (plist-get info :with-todo-keywords)
  1943. (let ((todo (org-element-property :todo-keyword headline)))
  1944. (and todo (org-export-data todo info)))))
  1945. (todo-type (and todo (org-element-property :todo-type headline)))
  1946. (tags (and (plist-get info :with-tags)
  1947. (org-export-get-tags headline info)))
  1948. (priority (and (plist-get info :with-priority)
  1949. (org-element-property :priority headline)))
  1950. ;; Create the headline text along with a no-tag version.
  1951. ;; The latter is required to remove tags from toc.
  1952. (full-text (funcall (plist-get info :latex-format-headline-function)
  1953. todo todo-type priority text tags info))
  1954. ;; Associate \label to the headline for internal links.
  1955. (headline-label (org-latex--label headline info t t))
  1956. (pre-blanks
  1957. (make-string (org-element-property :pre-blank headline) ?\n)))
  1958. (if (or (not section-fmt) (org-export-low-level-p headline info))
  1959. ;; This is a deep sub-tree: export it as a list item. Also
  1960. ;; export as items headlines for which no section format has
  1961. ;; been found.
  1962. (let ((low-level-body
  1963. (concat
  1964. ;; If headline is the first sibling, start a list.
  1965. (when (org-export-first-sibling-p headline info)
  1966. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1967. ;; Itemize headline
  1968. "\\item"
  1969. (and full-text
  1970. (string-match-p "\\`[ \t]*\\[" full-text)
  1971. "\\relax")
  1972. " " full-text "\n"
  1973. headline-label
  1974. pre-blanks
  1975. contents)))
  1976. ;; If headline is not the last sibling simply return
  1977. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
  1978. ;; any blank line.
  1979. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1980. (replace-regexp-in-string
  1981. "[ \t\n]*\\'"
  1982. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1983. low-level-body)))
  1984. ;; This is a standard headline. Export it as a section. Add
  1985. ;; an alternative heading when possible, and when this is not
  1986. ;; identical to the usual heading.
  1987. (let ((opt-title
  1988. (funcall (plist-get info :latex-format-headline-function)
  1989. todo todo-type priority
  1990. (org-export-data-with-backend
  1991. (org-export-get-alt-title headline info)
  1992. section-back-end info)
  1993. (and (eq (plist-get info :with-tags) t) tags)
  1994. info))
  1995. ;; Maybe end local TOC (see `org-latex-keyword').
  1996. (contents
  1997. (concat
  1998. contents
  1999. (let ((case-fold-search t)
  2000. (section
  2001. (let ((first (car (org-element-contents headline))))
  2002. (and (eq (org-element-type first) 'section) first))))
  2003. (org-element-map section 'keyword
  2004. (lambda (k)
  2005. (and (equal (org-element-property :key k) "TOC")
  2006. (let ((v (org-element-property :value k)))
  2007. (and (string-match-p "\\<headlines\\>" v)
  2008. (string-match-p "\\<local\\>" v)
  2009. (format "\\stopcontents[level-%d]" level)))))
  2010. info t)))))
  2011. (if (and opt-title
  2012. (not (equal opt-title full-text))
  2013. (string-match "\\`\\\\\\(.+?\\){" section-fmt))
  2014. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  2015. ;; Replace square brackets with parenthesis
  2016. ;; since square brackets are not supported in
  2017. ;; optional arguments.
  2018. (replace-regexp-in-string
  2019. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  2020. full-text
  2021. (concat headline-label pre-blanks contents))
  2022. ;; Impossible to add an alternative heading. Fallback to
  2023. ;; regular sectioning format string.
  2024. (format section-fmt full-text
  2025. (concat headline-label pre-blanks contents))))))))
  2026. (defun org-latex-format-headline-default-function
  2027. (todo _todo-type priority text tags _info)
  2028. "Default format function for a headline.
  2029. See `org-latex-format-headline-function' for details."
  2030. (concat
  2031. (and todo (format "{\\bfseries\\sffamily %s} " todo))
  2032. (and priority (format "\\framebox{\\#%c} " priority))
  2033. text
  2034. (and tags
  2035. (format "\\hfill{}\\textsc{%s}"
  2036. (mapconcat #'org-latex--protect-text tags ":")))))
  2037. ;;;; Horizontal Rule
  2038. (defun org-latex-horizontal-rule (horizontal-rule _contents info)
  2039. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  2040. CONTENTS is nil. INFO is a plist holding contextual information."
  2041. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  2042. (prev (org-export-get-previous-element horizontal-rule info)))
  2043. (concat
  2044. ;; Make sure the rule doesn't start at the end of the current
  2045. ;; line by separating it with a blank line from previous element.
  2046. (when (and prev
  2047. (let ((prev-blank (org-element-property :post-blank prev)))
  2048. (or (not prev-blank) (zerop prev-blank))))
  2049. "\n")
  2050. (org-latex--wrap-label
  2051. horizontal-rule
  2052. (format "\\noindent\\rule{%s}{%s}"
  2053. (or (plist-get attr :width) "\\textwidth")
  2054. (or (plist-get attr :thickness) "0.5pt"))
  2055. info))))
  2056. ;;;; Inline Src Block
  2057. (defun org-latex-inline-src-block (inline-src-block _contents info)
  2058. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  2059. CONTENTS holds the contents of the item. INFO is a plist holding
  2060. contextual information."
  2061. (let ((code (org-element-property :value inline-src-block))
  2062. (lang (org-element-property :language inline-src-block)))
  2063. (pcase (plist-get info :latex-src-block-backend)
  2064. (`verbatim (org-latex--text-markup code 'code info))
  2065. (`minted (org-latex-inline-src-block--minted info code lang))
  2066. (`engraved (org-latex-inline-src-block--engraved info code lang))
  2067. (`listings (org-latex-inline-src-block--listings info code lang))
  2068. (oldval
  2069. (message "Please update the LaTeX src-block-backend to %s"
  2070. (if oldval "listings" "verbatim"))
  2071. (if oldval
  2072. (org-latex-inline-src-block--listings info code lang)
  2073. (org-latex--text-markup code 'code info))))))
  2074. (defun org-latex-inline-src-block--minted (info code lang)
  2075. "Transcode an inline src block's content from Org to LaTeX, using minted.
  2076. INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
  2077. (let ((mint-lang (or (cadr (assq (intern lang)
  2078. (plist-get info :latex-minted-langs)))
  2079. (downcase lang)))
  2080. (options (org-latex--make-option-string
  2081. (plist-get info :latex-minted-options))))
  2082. (format "\\mintinline%s{%s}{%s}"
  2083. (if (string= options "") "" (format "[%s]" options))
  2084. mint-lang
  2085. code)))
  2086. (defun org-latex-inline-src-block--engraved (info code lang)
  2087. "Transcode an inline src block's content from Org to LaTeX, using engrave-faces.
  2088. INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
  2089. (org-latex-src--engrave-code
  2090. code lang nil (plist-get info :latex-engraved-options) t))
  2091. (defun org-latex-inline-src-block--listings (info code lang)
  2092. "Transcode an inline src block's content from Org to LaTeX, using lstlistings.
  2093. INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
  2094. (let* ((lst-lang (or (cadr (assq (intern lang)
  2095. (plist-get info :latex-listings-langs)))
  2096. lang))
  2097. (separator (org-latex--find-verb-separator code))
  2098. (options (org-latex--make-option-string
  2099. (append (plist-get info :latex-listings-options)
  2100. `(("language" ,lst-lang))))))
  2101. (concat (format "\\lstinline[%s]" options)
  2102. separator code separator)))
  2103. ;;;; Inlinetask
  2104. (defun org-latex-inlinetask (inlinetask contents info)
  2105. "Transcode an INLINETASK element from Org to LaTeX.
  2106. CONTENTS holds the contents of the block. INFO is a plist
  2107. holding contextual information."
  2108. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  2109. (todo (and (plist-get info :with-todo-keywords)
  2110. (let ((todo (org-element-property :todo-keyword inlinetask)))
  2111. (and todo (org-export-data todo info)))))
  2112. (todo-type (org-element-property :todo-type inlinetask))
  2113. (tags (and (plist-get info :with-tags)
  2114. (org-export-get-tags inlinetask info)))
  2115. (priority (and (plist-get info :with-priority)
  2116. (org-element-property :priority inlinetask)))
  2117. (contents (concat (org-latex--label inlinetask info) contents)))
  2118. (funcall (plist-get info :latex-format-inlinetask-function)
  2119. todo todo-type priority title tags contents info)))
  2120. (defun org-latex-format-inlinetask-default-function
  2121. (todo _todo-type priority title tags contents _info)
  2122. "Default format function for inlinetasks.
  2123. See `org-latex-format-inlinetask-function' for details."
  2124. (let ((full-title
  2125. (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  2126. (when priority (format "\\framebox{\\#%c} " priority))
  2127. title
  2128. (when tags
  2129. (format "\\hfill{}\\textsc{%s}"
  2130. (org-make-tag-string
  2131. (mapcar #'org-latex--protect-text tags)))))))
  2132. (concat "\\begin{center}\n"
  2133. "\\fbox{\n"
  2134. "\\begin{minipage}[c]{.6\\linewidth}\n"
  2135. full-title "\n\n"
  2136. (and (org-string-nw-p contents)
  2137. (concat "\\rule[.8em]{\\linewidth}{2pt}\n\n" contents))
  2138. "\\end{minipage}\n"
  2139. "}\n"
  2140. "\\end{center}")))
  2141. ;;;; Italic
  2142. (defun org-latex-italic (_italic contents info)
  2143. "Transcode ITALIC from Org to LaTeX.
  2144. CONTENTS is the text with italic markup. INFO is a plist holding
  2145. contextual information."
  2146. (org-latex--text-markup contents 'italic info))
  2147. ;;;; Item
  2148. (defun org-latex-item (item contents info)
  2149. "Transcode an ITEM element from Org to LaTeX.
  2150. CONTENTS holds the contents of the item. INFO is a plist holding
  2151. contextual information."
  2152. (let* ((orderedp (eq (org-element-property
  2153. :type (org-export-get-parent item))
  2154. 'ordered))
  2155. (level
  2156. ;; Determine level of current item to determine the
  2157. ;; correct LaTeX counter to use (enumi, enumii...).
  2158. (let ((parent item) (level 0))
  2159. (while (memq (org-element-type
  2160. (setq parent (org-export-get-parent parent)))
  2161. '(plain-list item))
  2162. (when (and (eq (org-element-type parent) 'plain-list)
  2163. (eq (org-element-property :type parent)
  2164. 'ordered))
  2165. (cl-incf level)))
  2166. level))
  2167. (count (org-element-property :counter item))
  2168. (counter (and count
  2169. (< level 5)
  2170. (format "\\setcounter{enum%s}{%s}\n"
  2171. (nth (1- level) '("i" "ii" "iii" "iv"))
  2172. (1- count))))
  2173. (checkbox (cl-case (org-element-property :checkbox item)
  2174. (on "$\\boxtimes$")
  2175. (off "$\\square$")
  2176. (trans "$\\boxminus$")))
  2177. (tag (let ((tag (org-element-property :tag item)))
  2178. (and tag (org-export-data tag info))))
  2179. ;; If there are footnotes references in tag, be sure to add
  2180. ;; their definition at the end of the item. This workaround
  2181. ;; is necessary since "\footnote{}" command is not supported
  2182. ;; in tags.
  2183. (tag-footnotes
  2184. (or (and tag (org-latex--delayed-footnotes-definitions
  2185. (org-element-property :tag item) info))
  2186. "")))
  2187. (concat counter
  2188. "\\item"
  2189. (cond
  2190. ((and checkbox tag)
  2191. (format (if orderedp "{%s %s} %s" "[{%s %s}] %s")
  2192. checkbox tag tag-footnotes))
  2193. ((or checkbox tag)
  2194. (format (if orderedp "{%s} %s" "[{%s}] %s")
  2195. (or checkbox tag) tag-footnotes))
  2196. ;; Without a tag or a check-box, if CONTENTS starts with
  2197. ;; an opening square bracket, add "\relax" to "\item",
  2198. ;; unless the brackets comes from an initial export
  2199. ;; snippet (i.e. it is inserted willingly by the user).
  2200. ((and contents
  2201. (string-match-p "\\`[ \t]*\\[" contents)
  2202. (not (let ((e (car (org-element-contents item))))
  2203. (and (eq (org-element-type e) 'paragraph)
  2204. (let ((o (car (org-element-contents e))))
  2205. (and (eq (org-element-type o) 'export-snippet)
  2206. (eq (org-export-snippet-backend o)
  2207. 'latex)))))))
  2208. "\\relax ")
  2209. (t " "))
  2210. (and contents (org-trim contents)))))
  2211. ;;;; Keyword
  2212. (defun org-latex-keyword (keyword _contents info)
  2213. "Transcode a KEYWORD element from Org to LaTeX.
  2214. CONTENTS is nil. INFO is a plist holding contextual information."
  2215. (let ((key (org-element-property :key keyword))
  2216. (value (org-element-property :value keyword)))
  2217. (cond
  2218. ((string= key "LATEX") value)
  2219. ((string= key "INDEX") (format "\\index{%s}" value))
  2220. ((string= key "TOC")
  2221. (let ((case-fold-search t))
  2222. (cond
  2223. ((string-match-p "\\<headlines\\>" value)
  2224. (let* ((localp (string-match-p "\\<local\\>" value))
  2225. (parent (org-element-lineage keyword '(headline)))
  2226. (level (if (not (and localp parent)) 0
  2227. (org-export-get-relative-level parent info)))
  2228. (depth
  2229. (and (string-match "\\<[0-9]+\\>" value)
  2230. (format
  2231. "\\setcounter{tocdepth}{%d}"
  2232. (+ (string-to-number (match-string 0 value)) level)))))
  2233. (if (and localp parent)
  2234. ;; Start local TOC, assuming package "titletoc" is
  2235. ;; required.
  2236. (format "\\startcontents[level-%d]
  2237. \\printcontents[level-%d]{}{0}{%s}"
  2238. level level (or depth ""))
  2239. (concat depth (and depth "\n") "\\tableofcontents"))))
  2240. ((string-match-p "\\<tables\\>" value) "\\listoftables")
  2241. ((string-match-p "\\<listings\\>" value)
  2242. (cl-case (plist-get info :latex-src-block-backend)
  2243. ((nil) "\\listoffigures")
  2244. (minted "\\listoflistings")
  2245. (engraved "\\listoflistings")
  2246. (otherwise "\\lstlistoflistings")))))))))
  2247. ;;;; Latex Environment
  2248. (defun org-latex--environment-type (latex-environment)
  2249. "Return the TYPE of LATEX-ENVIRONMENT.
  2250. The TYPE is determined from the actual latex environment, and
  2251. could be a member of `org-latex-caption-above' or `math'."
  2252. (let* ((latex-begin-re "\\\\begin{\\([A-Za-z0-9*]+\\)}")
  2253. (value (org-remove-indentation
  2254. (org-element-property :value latex-environment)))
  2255. (env (or (and (string-match latex-begin-re value)
  2256. (match-string 1 value))
  2257. "")))
  2258. (cond
  2259. ((string-match-p org-latex-math-environments-re value) 'math)
  2260. ((string-match-p
  2261. (eval-when-compile
  2262. (regexp-opt '("table" "longtable" "tabular" "tabu" "longtabu")))
  2263. env)
  2264. 'table)
  2265. ((string-match-p "figure" env) 'image)
  2266. ((string-match-p
  2267. (eval-when-compile
  2268. (regexp-opt '("lstlisting" "listing" "verbatim" "minted")))
  2269. env)
  2270. 'src-block)
  2271. (t 'special-block))))
  2272. (defun org-latex-latex-environment (latex-environment _contents info)
  2273. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  2274. CONTENTS is nil. INFO is a plist holding contextual information."
  2275. (when (plist-get info :with-latex)
  2276. (let* ((value (org-remove-indentation
  2277. (org-element-property :value latex-environment)))
  2278. (type (org-latex--environment-type latex-environment))
  2279. (caption (if (eq type 'math)
  2280. (org-latex--label latex-environment info nil t)
  2281. (org-latex--caption/label-string latex-environment info)))
  2282. (caption-above-p
  2283. (memq type (append (plist-get info :latex-caption-above) '(math)))))
  2284. (if (not (or (org-element-property :name latex-environment)
  2285. (org-element-property :caption latex-environment)))
  2286. value
  2287. ;; Environment is labeled: label must be within the environment
  2288. ;; (otherwise, a reference pointing to that element will count
  2289. ;; the section instead). Also insert caption if `latex-environment'
  2290. ;; is not a math environment.
  2291. (with-temp-buffer
  2292. (insert value)
  2293. (if caption-above-p
  2294. (progn
  2295. (goto-char (point-min))
  2296. (forward-line))
  2297. (goto-char (point-max))
  2298. (forward-line -1))
  2299. (insert caption)
  2300. (buffer-string))))))
  2301. ;;;; Latex Fragment
  2302. (defun org-latex-latex-fragment (latex-fragment _contents _info)
  2303. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  2304. CONTENTS is nil. INFO is a plist holding contextual information."
  2305. (let ((value (org-element-property :value latex-fragment)))
  2306. ;; Trim math markers since the fragment is enclosed within
  2307. ;; a latex-math-block object anyway.
  2308. (cond ((string-match-p "\\`\\$[^$]" value) (substring value 1 -1))
  2309. ((string-prefix-p "\\(" value) (substring value 2 -2))
  2310. (t value))))
  2311. ;;;; Line Break
  2312. (defun org-latex-line-break (_line-break _contents _info)
  2313. "Transcode a LINE-BREAK object from Org to LaTeX.
  2314. CONTENTS is nil. INFO is a plist holding contextual information."
  2315. "\\\\\n")
  2316. ;;;; Link
  2317. (defun org-latex-image-link-filter (data _backend info)
  2318. (org-export-insert-image-links data info org-latex-inline-image-rules))
  2319. (defun org-latex--inline-image (link info)
  2320. "Return LaTeX code for an inline image.
  2321. LINK is the link pointing to the inline image. INFO is a plist
  2322. used as a communication channel."
  2323. (let* ((parent (org-export-get-parent-element link))
  2324. (path (let ((raw-path (org-element-property :path link)))
  2325. (if (not (file-name-absolute-p raw-path)) raw-path
  2326. (expand-file-name raw-path))))
  2327. (filetype (file-name-extension path))
  2328. (caption (org-latex--caption/label-string parent info))
  2329. (caption-above-p (org-latex--caption-above-p link info))
  2330. ;; Retrieve latex attributes from the element around.
  2331. (attr (org-export-read-attribute :attr_latex parent))
  2332. (float (let ((float (plist-get attr :float)))
  2333. (cond ((string= float "wrap") 'wrap)
  2334. ((string= float "sideways") 'sideways)
  2335. ((string= float "multicolumn") 'multicolumn)
  2336. ((string= float "t") 'figure)
  2337. ((and (plist-member attr :float) (not float)) 'nonfloat)
  2338. (float float)
  2339. ((or (org-element-property :caption parent)
  2340. (org-string-nw-p (plist-get attr :caption)))
  2341. 'figure)
  2342. (t 'nonfloat))))
  2343. (placement
  2344. (let ((place (plist-get attr :placement)))
  2345. (cond
  2346. (place (format "%s" place))
  2347. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  2348. ((eq float 'figure)
  2349. (format "[%s]" (plist-get info :latex-default-figure-position)))
  2350. (t ""))))
  2351. (center
  2352. (cond
  2353. ;; If link is an image link, do not center.
  2354. ((eq 'link (org-element-type (org-export-get-parent link))) nil)
  2355. ((plist-member attr :center) (plist-get attr :center))
  2356. (t (plist-get info :latex-images-centered))))
  2357. (comment-include (if (plist-get attr :comment-include) "%" ""))
  2358. ;; It is possible to specify scale or width and height in
  2359. ;; the ATTR_LATEX line, and also via default variables.
  2360. (scale (cond ((eq float 'wrap) "")
  2361. ((plist-get attr :scale))
  2362. (t (plist-get info :latex-image-default-scale))))
  2363. (width (cond ((org-string-nw-p scale) "")
  2364. ((plist-get attr :width))
  2365. ((plist-get attr :height) "")
  2366. ((eq float 'wrap) "0.48\\textwidth")
  2367. (t (plist-get info :latex-image-default-width))))
  2368. (height (cond ((org-string-nw-p scale) "")
  2369. ((plist-get attr :height))
  2370. ((or (plist-get attr :width)
  2371. (memq float '(figure wrap))) "")
  2372. (t (plist-get info :latex-image-default-height))))
  2373. (options (let ((opt (or (plist-get attr :options)
  2374. (plist-get info :latex-image-default-option))))
  2375. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  2376. (match-string 1 opt))))
  2377. image-code)
  2378. (if (member filetype '("tikz" "pgf"))
  2379. ;; For tikz images:
  2380. ;; - use \input to read in image file.
  2381. ;; - if options are present, wrap in a tikzpicture environment.
  2382. ;; - if width or height are present, use \resizebox to change
  2383. ;; the image size.
  2384. (progn
  2385. (setq image-code (format "\\input{%s}" path))
  2386. (when (org-string-nw-p options)
  2387. (setq image-code
  2388. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  2389. options
  2390. image-code)))
  2391. (setq image-code
  2392. (cond ((org-string-nw-p scale)
  2393. (format "\\scalebox{%s}{%s}" scale image-code))
  2394. ((or (org-string-nw-p width) (org-string-nw-p height))
  2395. (format "\\resizebox{%s}{%s}{%s}"
  2396. (if (org-string-nw-p width) width "!")
  2397. (if (org-string-nw-p height) height "!")
  2398. image-code))
  2399. (t image-code))))
  2400. ;; For other images:
  2401. ;; - add scale, or width and height to options.
  2402. ;; - include the image with \includegraphics.
  2403. (if (org-string-nw-p scale)
  2404. (setq options (concat options ",scale=" scale))
  2405. (when (org-string-nw-p width) (setq options (concat options ",width=" width)))
  2406. (when (org-string-nw-p height) (setq options (concat options ",height=" height))))
  2407. (let ((search-option (org-element-property :search-option link)))
  2408. (when (and search-option
  2409. (equal filetype "pdf")
  2410. (string-match-p "\\`[0-9]+\\'" search-option)
  2411. (not (string-match-p "page=" options)))
  2412. (setq options (concat options ",page=" search-option))))
  2413. (setq image-code
  2414. (format "\\includegraphics%s{%s}"
  2415. (cond ((not (org-string-nw-p options)) "")
  2416. ((string-prefix-p "," options)
  2417. (format "[%s]" (substring options 1)))
  2418. (t (format "[%s]" options)))
  2419. path))
  2420. (when (equal filetype "svg")
  2421. (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
  2422. "\\includesvg"
  2423. image-code
  2424. nil t))
  2425. (setq image-code (replace-regexp-in-string "\\.svg}"
  2426. "}"
  2427. image-code
  2428. nil t))))
  2429. ;; Return proper string, depending on FLOAT.
  2430. (pcase float
  2431. ((and (pred stringp) env-string)
  2432. (format "\\begin{%s}%s
  2433. %s%s
  2434. %s%s
  2435. %s\\end{%s}"
  2436. env-string
  2437. placement
  2438. (if caption-above-p caption "")
  2439. (if center "\\centering" "")
  2440. comment-include image-code
  2441. (if caption-above-p "" caption)
  2442. env-string))
  2443. (`wrap (format "\\begin{wrapfigure}%s
  2444. %s%s
  2445. %s%s
  2446. %s\\end{wrapfigure}"
  2447. placement
  2448. (if caption-above-p caption "")
  2449. (if center "\\centering" "")
  2450. comment-include image-code
  2451. (if caption-above-p "" caption)))
  2452. (`sideways (format "\\begin{sidewaysfigure}
  2453. %s%s
  2454. %s%s
  2455. %s\\end{sidewaysfigure}"
  2456. (if caption-above-p caption "")
  2457. (if center "\\centering" "")
  2458. comment-include image-code
  2459. (if caption-above-p "" caption)))
  2460. (`multicolumn (format "\\begin{figure*}%s
  2461. %s%s
  2462. %s%s
  2463. %s\\end{figure*}"
  2464. placement
  2465. (if caption-above-p caption "")
  2466. (if center "\\centering" "")
  2467. comment-include image-code
  2468. (if caption-above-p "" caption)))
  2469. (`figure (format "\\begin{figure}%s
  2470. %s%s
  2471. %s%s
  2472. %s\\end{figure}"
  2473. placement
  2474. (if caption-above-p caption "")
  2475. (if center "\\centering" "")
  2476. comment-include image-code
  2477. (if caption-above-p "" caption)))
  2478. ((guard center)
  2479. (format "\\begin{center}
  2480. %s%s
  2481. %s\\end{center}"
  2482. (if caption-above-p caption "")
  2483. image-code
  2484. (if caption-above-p "" caption)))
  2485. (_
  2486. (concat (if caption-above-p caption "")
  2487. image-code
  2488. (if caption-above-p caption ""))))))
  2489. (defun org-latex-link (link desc info)
  2490. "Transcode a LINK object from Org to LaTeX.
  2491. DESC is the description part of the link, or the empty string.
  2492. INFO is a plist holding contextual information. See
  2493. `org-export-data'."
  2494. (let* ((type (org-element-property :type link))
  2495. (raw-path (org-element-property :path link))
  2496. ;; Ensure DESC really exists, or set it to nil.
  2497. (desc (and (not (string= desc "")) desc))
  2498. (imagep (org-export-inline-image-p
  2499. link (plist-get info :latex-inline-image-rules)))
  2500. (path (org-latex--protect-text
  2501. (pcase type
  2502. ((or "http" "https" "ftp" "mailto" "doi")
  2503. (concat type ":" raw-path))
  2504. ("file"
  2505. (org-export-file-uri raw-path))
  2506. (_
  2507. raw-path)))))
  2508. (cond
  2509. ;; Link type is handled by a special function.
  2510. ((org-export-custom-protocol-maybe link desc 'latex info))
  2511. ;; Image file.
  2512. (imagep (org-latex--inline-image (org-export-link-localise link) info))
  2513. ;; Radio link: Transcode target's contents and use them as link's
  2514. ;; description.
  2515. ((string= type "radio")
  2516. (let ((destination (org-export-resolve-radio-link link info)))
  2517. (if (not destination) desc
  2518. (format "\\hyperref[%s]{%s}"
  2519. (org-export-get-reference destination info)
  2520. desc))))
  2521. ;; Links pointing to a headline: Find destination and build
  2522. ;; appropriate referencing command.
  2523. ((member type '("custom-id" "fuzzy" "id"))
  2524. (let ((destination
  2525. (if (string= type "fuzzy")
  2526. (org-export-resolve-fuzzy-link link info 'latex-matrices)
  2527. (org-export-resolve-id-link link info))))
  2528. (cl-case (org-element-type destination)
  2529. ;; Id link points to an external file.
  2530. (plain-text
  2531. (if desc (format "\\href{%s}{%s}" destination desc)
  2532. (format "\\url{%s}" destination)))
  2533. ;; Fuzzy link points nowhere.
  2534. ((nil)
  2535. (format (plist-get info :latex-link-with-unknown-path-format)
  2536. (or desc
  2537. (org-export-data
  2538. (org-element-property :raw-link link) info))))
  2539. ;; LINK points to a headline. If headlines are numbered
  2540. ;; and the link has no description, display headline's
  2541. ;; number. Otherwise, display description or headline's
  2542. ;; title.
  2543. (headline
  2544. (let ((label (org-latex--label destination info t)))
  2545. (if (and (not desc)
  2546. (org-export-numbered-headline-p destination info))
  2547. (format org-latex-reference-command label)
  2548. (format "\\hyperref[%s]{%s}" label
  2549. (or desc
  2550. (org-export-data
  2551. (org-element-property :title destination) info))))))
  2552. ;; Fuzzy link points to a target. Do as above.
  2553. (otherwise
  2554. (let ((ref (org-latex--label destination info t)))
  2555. (if (not desc) (format org-latex-reference-command ref)
  2556. (format "\\hyperref[%s]{%s}" ref desc)))))))
  2557. ;; Coderef: replace link with the reference name or the
  2558. ;; equivalent line number.
  2559. ((string= type "coderef")
  2560. (format (org-export-get-coderef-format path desc)
  2561. ;; Resolve with RAW-PATH since PATH could be tainted
  2562. ;; with `org-latex--protect-text' call above.
  2563. (org-export-resolve-coderef raw-path info)))
  2564. ;; External link with a description part.
  2565. ((and path desc) (format "\\href{%s}{%s}" path desc))
  2566. ;; External link without a description part.
  2567. (path (format "\\url{%s}" path))
  2568. ;; No path, only description. Try to do something useful.
  2569. (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
  2570. ;;;; Node Property
  2571. (defun org-latex-node-property (node-property _contents _info)
  2572. "Transcode a NODE-PROPERTY element from Org to LaTeX.
  2573. CONTENTS is nil. INFO is a plist holding contextual
  2574. information."
  2575. (format "%s:%s"
  2576. (org-element-property :key node-property)
  2577. (let ((value (org-element-property :value node-property)))
  2578. (if value (concat " " value) ""))))
  2579. ;;;; Paragraph
  2580. (defun org-latex-paragraph (_paragraph contents _info)
  2581. "Transcode a PARAGRAPH element from Org to LaTeX.
  2582. CONTENTS is the contents of the paragraph, as a string. INFO is
  2583. the plist used as a communication channel."
  2584. contents)
  2585. ;;;; Plain List
  2586. (defun org-latex-plain-list (plain-list contents info)
  2587. "Transcode a PLAIN-LIST element from Org to LaTeX.
  2588. CONTENTS is the contents of the list. INFO is a plist holding
  2589. contextual information."
  2590. (let* ((type (org-element-property :type plain-list))
  2591. (attr (org-export-read-attribute :attr_latex plain-list))
  2592. (latex-type (let ((env (plist-get attr :environment)))
  2593. (cond (env (format "%s" env))
  2594. ((eq type 'ordered) "enumerate")
  2595. ((eq type 'descriptive) "description")
  2596. (t "itemize")))))
  2597. (org-latex--wrap-label
  2598. plain-list
  2599. (format "\\begin{%s}%s\n%s\\end{%s}"
  2600. latex-type
  2601. (or (plist-get attr :options) "")
  2602. contents
  2603. latex-type)
  2604. info)))
  2605. ;;;; Plain Text
  2606. (defun org-latex-plain-text (text info)
  2607. "Transcode a TEXT string from Org to LaTeX.
  2608. TEXT is the string to transcode. INFO is a plist holding
  2609. contextual information."
  2610. (let* ((specialp (plist-get info :with-special-strings))
  2611. (output
  2612. ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
  2613. (let ((case-fold-search nil))
  2614. (replace-regexp-in-string
  2615. "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
  2616. ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
  2617. ;; However, if special strings are used, be careful not
  2618. ;; to protect "\" in "\-" constructs.
  2619. (replace-regexp-in-string
  2620. (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
  2621. (lambda (m)
  2622. (cl-case (string-to-char m)
  2623. (?\\ "$\\\\backslash$\\1")
  2624. (?~ "\\\\textasciitilde{}")
  2625. (?^ "\\\\^{}")
  2626. (t "\\\\\\&")))
  2627. text)))))
  2628. ;; Activate smart quotes. Be sure to provide original TEXT string
  2629. ;; since OUTPUT may have been modified.
  2630. (when (plist-get info :with-smart-quotes)
  2631. (setq output (org-export-activate-smart-quotes output :latex info text)))
  2632. ;; Convert special strings.
  2633. (when specialp
  2634. (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
  2635. ;; Handle break preservation if required.
  2636. (when (plist-get info :preserve-breaks)
  2637. (setq output (replace-regexp-in-string
  2638. "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
  2639. ;; Return value.
  2640. output))
  2641. ;;;; Planning
  2642. (defun org-latex-planning (planning _contents info)
  2643. "Transcode a PLANNING element from Org to LaTeX.
  2644. CONTENTS is nil. INFO is a plist holding contextual
  2645. information."
  2646. (concat
  2647. "\\noindent"
  2648. (mapconcat
  2649. 'identity
  2650. (delq nil
  2651. (list
  2652. (let ((closed (org-element-property :closed planning)))
  2653. (when closed
  2654. (concat
  2655. (format "\\textbf{%s} " org-closed-string)
  2656. (format (plist-get info :latex-inactive-timestamp-format)
  2657. (org-timestamp-translate closed)))))
  2658. (let ((deadline (org-element-property :deadline planning)))
  2659. (when deadline
  2660. (concat
  2661. (format "\\textbf{%s} " org-deadline-string)
  2662. (format (plist-get info :latex-active-timestamp-format)
  2663. (org-timestamp-translate deadline)))))
  2664. (let ((scheduled (org-element-property :scheduled planning)))
  2665. (when scheduled
  2666. (concat
  2667. (format "\\textbf{%s} " org-scheduled-string)
  2668. (format (plist-get info :latex-active-timestamp-format)
  2669. (org-timestamp-translate scheduled)))))))
  2670. " ")
  2671. "\\\\"))
  2672. ;;;; Property Drawer
  2673. (defun org-latex-property-drawer (_property-drawer contents _info)
  2674. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  2675. CONTENTS holds the contents of the drawer. INFO is a plist
  2676. holding contextual information."
  2677. (and (org-string-nw-p contents)
  2678. (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
  2679. ;;;; Pseudo Element: LaTeX Matrices
  2680. ;; `latex-matrices' elements have the following properties:
  2681. ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
  2682. ;; `math').
  2683. (defun org-latex--wrap-latex-matrices (data info)
  2684. "Merge contiguous tables with the same mode within a pseudo-element.
  2685. DATA is a parse tree or a secondary string. INFO is a plist
  2686. containing export options. Modify DATA by side-effect and return
  2687. it."
  2688. (org-element-map data 'table
  2689. (lambda (table)
  2690. (when (eq (org-element-property :type table) 'org)
  2691. (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
  2692. (plist-get info :latex-default-table-mode))))
  2693. (when (and (member mode '("inline-math" "math"))
  2694. ;; Do not wrap twice the same table.
  2695. (not (eq (org-element-type
  2696. (org-element-property :parent table))
  2697. 'latex-matrices)))
  2698. (let* ((caption (and (not (string= mode "inline-math"))
  2699. (org-element-property :caption table)))
  2700. (name (and (not (string= mode "inline-math"))
  2701. (org-element-property :name table)))
  2702. (matrices
  2703. (list 'latex-matrices
  2704. ;; Inherit name from the first table.
  2705. (list :name name
  2706. ;; FIXME: what syntax for captions?
  2707. ;;
  2708. ;; :caption caption
  2709. :markup
  2710. (cond ((string= mode "inline-math") 'inline)
  2711. ((or caption name) 'equation)
  2712. (t 'math)))))
  2713. (previous table)
  2714. (next (org-export-get-next-element table info)))
  2715. (org-element-insert-before matrices table)
  2716. ;; Swallow all contiguous tables sharing the same mode.
  2717. (while (and
  2718. (zerop (or (org-element-property :post-blank previous) 0))
  2719. (setq next (org-export-get-next-element previous info))
  2720. (eq (org-element-type next) 'table)
  2721. (eq (org-element-property :type next) 'org)
  2722. (string= (or (org-export-read-attribute
  2723. :attr_latex next :mode)
  2724. (plist-get info :latex-default-table-mode))
  2725. mode))
  2726. (org-element-put-property table :name nil)
  2727. (org-element-put-property table :caption nil)
  2728. (org-element-extract-element previous)
  2729. (org-element-adopt-elements matrices previous)
  2730. (setq previous next))
  2731. ;; Inherit `:post-blank' from the value of the last
  2732. ;; swallowed table. Set the latter's `:post-blank'
  2733. ;; value to 0 so as to not duplicate empty lines.
  2734. (org-element-put-property
  2735. matrices :post-blank (org-element-property :post-blank previous))
  2736. (org-element-put-property previous :post-blank 0)
  2737. (org-element-put-property table :name nil)
  2738. (org-element-put-property table :caption nil)
  2739. (org-element-extract-element previous)
  2740. (org-element-adopt-elements matrices previous))))))
  2741. info)
  2742. data)
  2743. (defun org-latex-matrices (matrices contents info)
  2744. "Transcode a MATRICES element from Org to LaTeX.
  2745. CONTENTS is a string. INFO is a plist used as a communication
  2746. channel."
  2747. (pcase (org-element-property :markup matrices)
  2748. (`inline (format "\\(%s\\)" contents))
  2749. (`equation
  2750. (let ((caption (org-latex--caption/label-string matrices info))
  2751. (caption-above? (org-latex--caption-above-p matrices info)))
  2752. (concat "\\begin{equation}\n"
  2753. (and caption-above? caption)
  2754. contents
  2755. (and (not caption-above?) caption)
  2756. "\\end{equation}")))
  2757. (_
  2758. (format "\\[\n%s\\]" contents))))
  2759. ;;;; Pseudo Object: LaTeX Math Block
  2760. ;; `latex-math-block' objects have the following property:
  2761. ;; `:post-blank'.
  2762. (defun org-latex--wrap-latex-math-block (data info)
  2763. "Merge contiguous math objects in a pseudo-object container.
  2764. DATA is a parse tree or a secondary string. INFO is a plist
  2765. containing export options. Modify DATA by side-effect and return it."
  2766. (let ((valid-object-p
  2767. ;; Non-nil when OBJECT can be added to a latex math block.
  2768. (lambda (object)
  2769. (pcase (org-element-type object)
  2770. (`entity (org-element-property :latex-math-p object))
  2771. (`latex-fragment
  2772. (let ((value (org-element-property :value object)))
  2773. (or (string-prefix-p "\\(" value)
  2774. (string-match-p "\\`\\$[^$]" value))))))))
  2775. (org-element-map data '(entity latex-fragment)
  2776. (lambda (object)
  2777. ;; Skip objects already wrapped.
  2778. (when (and (not (eq (org-element-type
  2779. (org-element-property :parent object))
  2780. 'latex-math-block))
  2781. (funcall valid-object-p object))
  2782. (let ((math-block (list 'latex-math-block nil))
  2783. (next-elements (org-export-get-next-element object info t))
  2784. (last object))
  2785. ;; Wrap MATH-BLOCK around OBJECT in DATA.
  2786. (org-element-insert-before math-block object)
  2787. (org-element-extract-element object)
  2788. (org-element-adopt-elements math-block object)
  2789. (when (zerop (or (org-element-property :post-blank object) 0))
  2790. ;; MATH-BLOCK swallows consecutive math objects.
  2791. (catch 'exit
  2792. (dolist (next next-elements)
  2793. (unless (funcall valid-object-p next) (throw 'exit nil))
  2794. (org-element-extract-element next)
  2795. (org-element-adopt-elements math-block next)
  2796. ;; Eschew the case: \beta$x$ -> \(\betax\).
  2797. (org-element-put-property last :post-blank 1)
  2798. (setq last next)
  2799. (when (> (or (org-element-property :post-blank next) 0) 0)
  2800. (throw 'exit nil)))))
  2801. (org-element-put-property
  2802. math-block :post-blank (org-element-property :post-blank last)))))
  2803. info nil '(latex-math-block) t)
  2804. ;; Return updated DATA.
  2805. data))
  2806. (defun org-latex-math-block (_math-block contents _info)
  2807. "Transcode a MATH-BLOCK object from Org to LaTeX.
  2808. CONTENTS is a string. INFO is a plist used as a communication
  2809. channel."
  2810. (when (org-string-nw-p contents)
  2811. (format "\\(%s\\)" (org-trim contents))))
  2812. ;;;; Quote Block
  2813. (defun org-latex-quote-block (quote-block contents info)
  2814. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  2815. CONTENTS holds the contents of the block. INFO is a plist
  2816. holding contextual information."
  2817. (let ((environment
  2818. (or (org-export-read-attribute :attr_latex quote-block :environment)
  2819. (plist-get info :latex-default-quote-environment)))
  2820. (options
  2821. (or (org-export-read-attribute :attr_latex quote-block :options)
  2822. "")))
  2823. (org-latex--wrap-label
  2824. quote-block (format "\\begin{%s}%s\n%s\\end{%s}"
  2825. environment
  2826. options
  2827. contents
  2828. environment)
  2829. info)))
  2830. ;;;; Radio Target
  2831. (defun org-latex-radio-target (radio-target text info)
  2832. "Transcode a RADIO-TARGET object from Org to LaTeX.
  2833. TEXT is the text of the target. INFO is a plist holding
  2834. contextual information."
  2835. (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
  2836. ;;;; Section
  2837. (defun org-latex-section (_section contents _info)
  2838. "Transcode a SECTION element from Org to LaTeX.
  2839. CONTENTS holds the contents of the section. INFO is a plist
  2840. holding contextual information."
  2841. contents)
  2842. ;;;; Special Block
  2843. (defun org-latex-special-block (special-block contents info)
  2844. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  2845. CONTENTS holds the contents of the block. INFO is a plist
  2846. holding contextual information."
  2847. (let ((type (org-element-property :type special-block))
  2848. (opt (org-export-read-attribute :attr_latex special-block :options))
  2849. (caption (org-latex--caption/label-string special-block info))
  2850. (caption-above-p (org-latex--caption-above-p special-block info)))
  2851. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  2852. (and caption-above-p caption)
  2853. contents
  2854. (and (not caption-above-p) caption)
  2855. (format "\\end{%s}" type))))
  2856. ;;;; Src Block
  2857. (defun org-latex-src-block (src-block _contents info)
  2858. "Transcode a SRC-BLOCK element from Org to LaTeX.
  2859. CONTENTS holds the contents of the item. INFO is a plist holding
  2860. contextual information."
  2861. (when (org-string-nw-p (org-element-property :value src-block))
  2862. (let* ((lang (org-element-property :language src-block))
  2863. (caption (org-element-property :caption src-block))
  2864. (caption-above-p (org-latex--caption-above-p src-block info))
  2865. (label (org-element-property :name src-block))
  2866. (custom-env (and lang
  2867. (cadr (assq (intern lang)
  2868. org-latex-custom-lang-environments))))
  2869. (num-start (org-export-get-loc src-block info))
  2870. (retain-labels (org-element-property :retain-labels src-block))
  2871. (attributes (org-export-read-attribute :attr_latex src-block))
  2872. (float (plist-get attributes :float)))
  2873. (funcall
  2874. (pcase (plist-get info :latex-src-block-backend)
  2875. ((or `verbatim (guard (not lang))) #'org-latex-src-block--verbatim)
  2876. (`minted #'org-latex-src-block--minted)
  2877. (`engraved #'org-latex-src-block--engraved)
  2878. (`listings #'org-latex-src-block--listings)
  2879. ((guard custom-env) #'org-latex-src-block--custom)
  2880. (oldval
  2881. (message "Please update the LaTeX src-block-backend to %s"
  2882. (if oldval "listings" "verbatim"))
  2883. (if oldval
  2884. #'org-latex-src-block--listings
  2885. #'org-latex-src-block--verbatim)))
  2886. :src-block src-block
  2887. :info info
  2888. :lang lang
  2889. :caption caption
  2890. :caption-above-p caption-above-p
  2891. :label label
  2892. :num-start num-start
  2893. :retain-labels retain-labels
  2894. :attributes attributes
  2895. :float float))))
  2896. (cl-defun org-latex-src-block--verbatim
  2897. (&key src-block info caption caption-above-p float &allow-other-keys)
  2898. "Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
  2899. LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
  2900. and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
  2901. (let ((caption-str (org-latex--caption/label-string src-block info))
  2902. (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
  2903. (org-export-format-code-default src-block info))))
  2904. (cond ((string= "multicolumn" float)
  2905. (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
  2906. (plist-get info :latex-default-figure-position)
  2907. (if caption-above-p caption-str "")
  2908. verbatim
  2909. (if caption-above-p "" caption-str)))
  2910. (caption (concat
  2911. (if caption-above-p caption-str "")
  2912. verbatim
  2913. (if caption-above-p "" (concat "\n" caption-str))))
  2914. (t verbatim))))
  2915. (cl-defun org-latex-src-block--custom
  2916. (&key src-block info caption caption-above-p attributes float custom-env &allow-other-keys)
  2917. "Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
  2918. LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
  2919. and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
  2920. (let ((caption-str (org-latex--caption/label-string src-block info))
  2921. (formatted-src (org-export-format-code-default src-block info)))
  2922. (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
  2923. (format "\\begin{%s}\n%s\\end{%s}\n"
  2924. custom-env
  2925. (concat (and caption-above-p caption-str)
  2926. formatted-src
  2927. (and (not caption-above-p) caption-str))
  2928. custom-env)
  2929. (format-spec custom-env
  2930. `((?s . ,formatted-src)
  2931. (?c . ,caption)
  2932. (?f . ,float)
  2933. (?l . ,(org-latex--label src-block info))
  2934. (?o . ,(or (plist-get attributes :options) "")))))))
  2935. (cl-defun org-latex-src-block--minted
  2936. (&key src-block info lang caption caption-above-p num-start retain-labels attributes float &allow-other-keys)
  2937. "Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
  2938. LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
  2939. and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
  2940. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2941. (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
  2942. (plist-get info :latex-default-figure-position)))
  2943. (multicolumn-p (string= "multicolumn" float))
  2944. (float-env
  2945. (cond
  2946. ((or caption multicolumn-p)
  2947. (cons
  2948. (concat "\\begin{listing" (when multicolumn-p "*")
  2949. "}[" placement "]\n"
  2950. (if caption-above-p caption-str ""))
  2951. (concat "\n" (if caption-above-p "" caption-str)
  2952. "\\end{listing" (when multicolumn-p "*") "}")))
  2953. ((string= "t" float)
  2954. (cons
  2955. (concat "\\begin{listing}[" placement "]\n")
  2956. "\n\\end{listing}"))))
  2957. (options (plist-get info :latex-minted-options))
  2958. (body
  2959. (format
  2960. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  2961. ;; Options.
  2962. (concat
  2963. (org-latex--make-option-string
  2964. (if (or (not num-start) (assoc "linenos" options))
  2965. options
  2966. (append
  2967. `(("linenos")
  2968. ("firstnumber" ,(number-to-string (1+ num-start))))
  2969. options)))
  2970. (let ((local-options (plist-get attributes :options)))
  2971. (and local-options (concat "," local-options))))
  2972. ;; Language.
  2973. (or (cadr (assq (intern lang)
  2974. (plist-get info :latex-minted-langs)))
  2975. (downcase lang))
  2976. ;; Source code.
  2977. (let* ((code-info (org-export-unravel-code src-block))
  2978. (max-width
  2979. (apply 'max
  2980. (mapcar 'string-width
  2981. (org-split-string (car code-info)
  2982. "\n")))))
  2983. (org-export-format-code
  2984. (car code-info)
  2985. (lambda (loc _num ref)
  2986. (concat
  2987. loc
  2988. (when ref
  2989. ;; Ensure references are flushed to the right,
  2990. ;; separated with 6 spaces from the widest line
  2991. ;; of code.
  2992. (concat (make-string (+ (- max-width (length loc)) 6)
  2993. ?\s)
  2994. (format "(%s)" ref)))))
  2995. nil (and retain-labels (cdr code-info)))))))
  2996. (concat (car float-env) body (cdr float-env))))
  2997. (defun org-latex-src--engrave-mathescape-p (info options)
  2998. "From the export INFO plist, and the per-block OPTIONS, determine mathescape."
  2999. (let ((default-options (plist-get info :latex-engraved-options))
  3000. (mathescape-status
  3001. (lambda (opts)
  3002. (cl-some
  3003. (lambda (opt)
  3004. (or (and
  3005. (null (cdr opt))
  3006. (cond
  3007. ((string-match-p
  3008. "\\(?:^\\|,\\)mathescape=false\\(?:,\\|$\\)"
  3009. (car opt))
  3010. 'no)
  3011. ((or (string-match-p
  3012. "\\(?:^\\|,\\)mathescape\\(?:=true\\)?\\(?:,\\|$\\)"
  3013. (car opt))
  3014. (string= "mathescape" (car opt)))
  3015. 'yes)))
  3016. (and
  3017. (string= (car opt) "mathescape")
  3018. (cond
  3019. ((or (and (stringp (cdr opt)) (string= (cdr opt) "true"))
  3020. (equal '("true") (cdr opt)))
  3021. 'yes)
  3022. ((or (and (stringp (cdr opt)) (string= "false" (cdr opt)))
  3023. (equal '("false") (cdr opt)))
  3024. 'no)))))
  3025. opts))))
  3026. (if-let ((mathescape (or (funcall mathescape-status default-options)
  3027. (funcall mathescape-status options))))
  3028. (when (eq mathescape 'yes)
  3029. (or engrave-faces-latex-mathescape t)))))
  3030. (defun org-latex-src--engrave-code (content lang &optional theme options inline)
  3031. "Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result.
  3032. When the THEME symbol is non-nil, that theme will be used.
  3033. When INLINE is nil, a Verbatim environment wrapped in a Code
  3034. environment will be used. When t, a Verb command will be used.
  3035. When OPTIONS is provided, as either a string or list of key-value
  3036. pairs accepted by `org-latex--make-option-string', it is passed
  3037. to the Verbatim environment or Verb command."
  3038. (if (require 'engrave-faces-latex nil t)
  3039. (let* ((lang-mode (and lang (org-src-get-lang-mode lang)))
  3040. (engrave-faces-current-preset-style
  3041. (if theme
  3042. (engrave-faces-get-theme theme)
  3043. engrave-faces-current-preset-style))
  3044. (engraved-buffer
  3045. (with-temp-buffer
  3046. (insert (string-trim-right content "\n"))
  3047. (when lang-mode
  3048. (if (functionp lang-mode)
  3049. (funcall lang-mode)
  3050. (message "Cannot engrave code as %s. %s is undefined."
  3051. lang lang-mode)))
  3052. (engrave-faces-latex-buffer)))
  3053. (engraved-code
  3054. (with-current-buffer engraved-buffer
  3055. (buffer-string)))
  3056. (engraved-options
  3057. (when options
  3058. (concat "["
  3059. (if (listp options)
  3060. (org-latex--make-option-string options)
  3061. options)
  3062. "]")))
  3063. (engraved-wrapped
  3064. (if inline
  3065. (concat "\\Verb" engraved-options "{" engraved-code "}")
  3066. (concat "\\begin{Code}\n\\begin{Verbatim}" engraved-options "\n"
  3067. engraved-code "\n\\end{Verbatim}\n\\end{Code}"))))
  3068. (kill-buffer engraved-buffer)
  3069. (if theme
  3070. (concat "{\\engravedtheme"
  3071. (replace-regexp-in-string "[^A-Za-z]" ""
  3072. (symbol-name theme))
  3073. engraved-wrapped
  3074. "}")
  3075. engraved-wrapped))
  3076. (user-error "Cannot engrave code as `engrave-faces-latex' is unavailible.")))
  3077. (cl-defun org-latex-src-block--engraved
  3078. (&key src-block info lang caption caption-above-p num-start retain-labels attributes float &allow-other-keys)
  3079. "Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
  3080. LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
  3081. and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
  3082. (let* ((caption-str (org-latex--caption/label-string src-block info))
  3083. (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
  3084. (plist-get info :latex-default-figure-position)))
  3085. (multicolumn-p (string= "multicolumn" float))
  3086. (float-env
  3087. (cond
  3088. ((or caption multicolumn-p)
  3089. (cons
  3090. (concat "\\begin{listing" (when multicolumn-p "*")
  3091. "}[" placement "]\n"
  3092. (if caption-above-p caption-str ""))
  3093. (concat "\n" (if caption-above-p "" caption-str)
  3094. "\\end{listing" (when multicolumn-p "*") "}")))
  3095. ((string= "t" float)
  3096. (cons
  3097. (concat "\\begin{listing}[" placement "]\n")
  3098. "\n\\end{listing}"))))
  3099. (options
  3100. (let ((engraved-options (plist-get info :latex-engraved-options))
  3101. (local-options (plist-get attributes :options)))
  3102. (append
  3103. (when (and num-start (not (assoc "linenos" engraved-options)))
  3104. `(("linenos")
  3105. ("firstnumber" ,(number-to-string (1+ num-start)))))
  3106. (and local-options `((,local-options))))))
  3107. (engraved-theme (plist-get attributes :engraved-theme))
  3108. (content
  3109. (let* ((code-info (org-export-unravel-code src-block))
  3110. (max-width
  3111. (apply 'max
  3112. (mapcar 'string-width
  3113. (org-split-string (car code-info)
  3114. "\n")))))
  3115. (org-export-format-code
  3116. (car code-info)
  3117. (lambda (loc _num ref)
  3118. (concat
  3119. loc
  3120. (when ref
  3121. ;; Ensure references are flushed to the right,
  3122. ;; separated with 6 spaces from the widest line
  3123. ;; of code.
  3124. (concat (make-string (+ (- max-width (length loc)) 6)
  3125. ?\s)
  3126. (format "(%s)" ref)))))
  3127. nil (and retain-labels (cdr code-info)))))
  3128. (body
  3129. (let ((engrave-faces-latex-mathescape
  3130. (org-latex-src--engrave-mathescape-p info options)))
  3131. (org-latex-src--engrave-code
  3132. content lang
  3133. (when engraved-theme (intern engraved-theme))
  3134. options))))
  3135. (concat (car float-env) body (cdr float-env))))
  3136. (cl-defun org-latex-src-block--listings
  3137. (&key src-block info lang caption caption-above-p label num-start retain-labels attributes float &allow-other-keys)
  3138. "Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
  3139. LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
  3140. and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
  3141. (let ((lst-lang
  3142. (or (cadr (assq (intern lang)
  3143. (plist-get info :latex-listings-langs)))
  3144. lang))
  3145. (caption-str
  3146. (when caption
  3147. (let ((main (org-export-get-caption src-block))
  3148. (secondary (org-export-get-caption src-block t)))
  3149. (if (not secondary)
  3150. (format "{%s}" (org-export-data main info))
  3151. (format "{[%s]%s}"
  3152. (org-export-data secondary info)
  3153. (org-export-data main info))))))
  3154. (lst-opt (plist-get info :latex-listings-options)))
  3155. (concat
  3156. ;; Options.
  3157. (format
  3158. "\\lstset{%s}\n"
  3159. (concat
  3160. (org-latex--make-option-string
  3161. (append
  3162. lst-opt
  3163. (cond
  3164. ((and (not float) (plist-member attributes :float)) nil)
  3165. ((string= "multicolumn" float) '(("float" "*")))
  3166. ((and float (not (assoc "float" lst-opt)))
  3167. `(("float" ,(plist-get info :latex-default-figure-position)))))
  3168. `(("language" ,lst-lang))
  3169. (if label
  3170. `(("label" ,(org-latex--label src-block info)))
  3171. '(("label" " ")))
  3172. (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
  3173. `(("captionpos" ,(if caption-above-p "t" "b")))
  3174. (cond ((assoc "numbers" lst-opt) nil)
  3175. ((not num-start) '(("numbers" "none")))
  3176. (t `(("firstnumber" ,(number-to-string (1+ num-start)))
  3177. ("numbers" "left"))))))
  3178. (let ((local-options (plist-get attributes :options)))
  3179. (and local-options (concat "," local-options)))))
  3180. ;; Source code.
  3181. (format
  3182. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  3183. (let* ((code-info (org-export-unravel-code src-block))
  3184. (max-width
  3185. (apply 'max
  3186. (mapcar 'string-width
  3187. (org-split-string (car code-info) "\n")))))
  3188. (org-export-format-code
  3189. (car code-info)
  3190. (lambda (loc _num ref)
  3191. (concat
  3192. loc
  3193. (when ref
  3194. ;; Ensure references are flushed to the right,
  3195. ;; separated with 6 spaces from the widest line of
  3196. ;; code
  3197. (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
  3198. (format "(%s)" ref)))))
  3199. nil (and retain-labels (cdr code-info))))))))
  3200. ;;;; Statistics Cookie
  3201. (defun org-latex-statistics-cookie (statistics-cookie _contents _info)
  3202. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  3203. CONTENTS is nil. INFO is a plist holding contextual information."
  3204. (replace-regexp-in-string
  3205. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  3206. ;;;; Strike-Through
  3207. (defun org-latex-strike-through (_strike-through contents info)
  3208. "Transcode STRIKE-THROUGH from Org to LaTeX.
  3209. CONTENTS is the text with strike-through markup. INFO is a plist
  3210. holding contextual information."
  3211. (org-latex--text-markup contents 'strike-through info))
  3212. ;;;; Subscript
  3213. (defun org-latex-subscript (_subscript contents _info)
  3214. "Transcode a SUBSCRIPT object from Org to LaTeX.
  3215. CONTENTS is the contents of the object."
  3216. (format "\\textsubscript{%s}" contents))
  3217. ;;;; Superscript
  3218. (defun org-latex-superscript (_superscript contents _info)
  3219. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  3220. CONTENTS is the contents of the object."
  3221. (format "\\textsuperscript{%s}" contents))
  3222. ;;;; Table
  3223. ;;
  3224. ;; `org-latex-table' is the entry point for table transcoding. It
  3225. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  3226. ;; delegates the job to either `org-latex--table.el-table',
  3227. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  3228. ;; depending of the type of the table and the mode requested.
  3229. ;;
  3230. ;; `org-latex--align-string' is a subroutine used to build alignment
  3231. ;; string for Org tables.
  3232. (defun org-latex-table (table contents info)
  3233. "Transcode a TABLE element from Org to LaTeX.
  3234. CONTENTS is the contents of the table. INFO is a plist holding
  3235. contextual information."
  3236. (if (eq (org-element-property :type table) 'table.el)
  3237. ;; "table.el" table. Convert it using appropriate tools.
  3238. (org-latex--table.el-table table info)
  3239. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  3240. (plist-get info :latex-default-table-mode))))
  3241. (cond
  3242. ;; Case 1: Verbatim table.
  3243. ((string= type "verbatim")
  3244. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  3245. ;; Re-create table, without affiliated keywords.
  3246. (org-trim (org-element-interpret-data
  3247. `(table nil ,@(org-element-contents table))))))
  3248. ;; Case 2: Matrix.
  3249. ((or (string= type "math") (string= type "inline-math"))
  3250. (org-latex--math-table table info))
  3251. ;; Case 3: Standard table.
  3252. (t (concat (org-latex--org-table table contents info)
  3253. ;; When there are footnote references within the
  3254. ;; table, insert their definition just after it.
  3255. (org-latex--delayed-footnotes-definitions table info)))))))
  3256. (defun org-latex--align-string (table info &optional math?)
  3257. "Return an appropriate LaTeX alignment string.
  3258. TABLE is the considered table. INFO is a plist used as
  3259. a communication channel. When optional argument MATH? is
  3260. non-nil, TABLE is meant to be a matrix, where all cells are
  3261. centered."
  3262. (or (org-export-read-attribute :attr_latex table :align)
  3263. (let (align)
  3264. ;; Extract column groups and alignment from first (non-rule)
  3265. ;; row.
  3266. (org-element-map
  3267. (org-element-map table 'table-row
  3268. (lambda (row)
  3269. (and (eq (org-element-property :type row) 'standard) row))
  3270. info 'first-match)
  3271. 'table-cell
  3272. (lambda (cell)
  3273. (let ((borders (org-export-table-cell-borders cell info)))
  3274. ;; Check left border for the first cell only.
  3275. (when (and (memq 'left borders) (not align))
  3276. (push "|" align))
  3277. (push (if math? "c" ;center cells in matrices
  3278. (cl-case (org-export-table-cell-alignment cell info)
  3279. (left "l")
  3280. (right "r")
  3281. (center "c")))
  3282. align)
  3283. (when (memq 'right borders) (push "|" align))))
  3284. info)
  3285. (apply 'concat (nreverse align)))))
  3286. (defun org-latex--decorate-table (table attributes caption above? info)
  3287. "Decorate TABLE string with caption and float environment.
  3288. ATTRIBUTES is the plist containing LaTeX attributes. CAPTION is
  3289. its caption, as a string or nil. It is located above the table
  3290. if ABOVE? is non-nil. INFO is the plist containing current
  3291. export parameters.
  3292. Return new environment, as a string."
  3293. (let* ((float-environment
  3294. (let ((float (plist-get attributes :float)))
  3295. (cond ((and (not float) (plist-member attributes :float)) nil)
  3296. ((member float '("sidewaystable" "sideways")) "sidewaystable")
  3297. ((equal float "multicolumn") "table*")
  3298. ((string= float "t") "table")
  3299. (float float)
  3300. ((org-string-nw-p caption) "table")
  3301. (t nil))))
  3302. (placement
  3303. (or (plist-get attributes :placement)
  3304. (format "[%s]" (plist-get info :latex-default-figure-position))))
  3305. (center? (if (plist-member attributes :center)
  3306. (plist-get attributes :center)
  3307. (plist-get info :latex-tables-centered)))
  3308. (fontsize (let ((font (plist-get attributes :font)))
  3309. (and font (concat font "\n")))))
  3310. (concat (cond
  3311. (float-environment
  3312. (concat (format "\\begin{%s}%s\n" float-environment placement)
  3313. (if above? caption "")
  3314. (when center? "\\centering\n")
  3315. fontsize))
  3316. (caption
  3317. (concat (and center? "\\begin{center}\n" )
  3318. (if above? caption "")
  3319. (cond ((and fontsize center?) fontsize)
  3320. (fontsize (concat "{" fontsize))
  3321. (t nil))))
  3322. (center? (concat "\\begin{center}\n" fontsize))
  3323. (fontsize (concat "{" fontsize)))
  3324. table
  3325. (cond
  3326. (float-environment
  3327. (concat (if above? "" (concat "\n" caption))
  3328. (format "\n\\end{%s}" float-environment)))
  3329. (caption
  3330. (concat (if above? "" (concat "\n" caption))
  3331. (and center? "\n\\end{center}")
  3332. (and fontsize (not center?) "}")))
  3333. (center? "\n\\end{center}")
  3334. (fontsize "}")))))
  3335. (defun org-latex--org-table (table contents info)
  3336. "Return appropriate LaTeX code for an Org table.
  3337. TABLE is the table type element to transcode. CONTENTS is its
  3338. contents, as a string. INFO is a plist used as a communication
  3339. channel.
  3340. This function assumes TABLE has `org' as its `:type' property and
  3341. `table' as its `:mode' attribute."
  3342. (let* ((attr (org-export-read-attribute :attr_latex table))
  3343. (alignment (org-latex--align-string table info))
  3344. (opt (org-export-read-attribute :attr_latex table :options))
  3345. (table-env (or (plist-get attr :environment)
  3346. (plist-get info :latex-default-table-environment)))
  3347. (width
  3348. (let ((w (plist-get attr :width)))
  3349. (cond ((not w) "")
  3350. ((member table-env '("tabular" "longtable")) "")
  3351. ((member table-env '("tabu" "longtabu"))
  3352. (format (if (plist-get attr :spread) " spread %s "
  3353. " to %s ")
  3354. w))
  3355. (t (format "{%s}" w)))))
  3356. (caption (org-latex--caption/label-string table info))
  3357. (above? (org-latex--caption-above-p table info)))
  3358. (cond
  3359. ((member table-env '("longtable" "longtabu"))
  3360. (let ((fontsize (let ((font (plist-get attr :font)))
  3361. (and font (concat font "\n")))))
  3362. (concat (and fontsize (concat "{" fontsize))
  3363. (format "\\begin{%s}%s{%s}\n" table-env width alignment)
  3364. (and above?
  3365. (org-string-nw-p caption)
  3366. (concat caption "\\\\\n"))
  3367. contents
  3368. (and (not above?)
  3369. (org-string-nw-p caption)
  3370. (concat caption "\\\\\n"))
  3371. (format "\\end{%s}" table-env)
  3372. (and fontsize "}"))))
  3373. (t
  3374. (let ((output (format "\\begin{%s}%s%s{%s}\n%s\\end{%s}"
  3375. table-env
  3376. (if opt (format "[%s]" opt) "")
  3377. width
  3378. alignment
  3379. contents
  3380. table-env)))
  3381. (org-latex--decorate-table output attr caption above? info))))))
  3382. (defun org-latex--table.el-table (table info)
  3383. "Return appropriate LaTeX code for a table.el table.
  3384. TABLE is the table type element to transcode. INFO is a plist
  3385. used as a communication channel.
  3386. This function assumes TABLE has `table.el' as its `:type'
  3387. property."
  3388. (require 'table)
  3389. ;; Ensure "*org-export-table*" buffer is empty.
  3390. (with-current-buffer (get-buffer-create "*org-export-table*")
  3391. (erase-buffer))
  3392. (let ((output
  3393. (replace-regexp-in-string
  3394. "^%.*\n" "" ;remove comments
  3395. (with-temp-buffer
  3396. (save-excursion (insert (org-element-property :value table)))
  3397. (re-search-forward "^[ \t]*|[^|]" nil t)
  3398. (table-generate-source 'latex "*org-export-table*")
  3399. (with-current-buffer "*org-export-table*"
  3400. (org-trim (buffer-string))))
  3401. t t)))
  3402. (kill-buffer (get-buffer "*org-export-table*"))
  3403. (let ((attr (org-export-read-attribute :attr_latex table))
  3404. (caption (org-latex--caption/label-string table info))
  3405. (above? (org-latex--caption-above-p table info)))
  3406. (when (plist-get attr :rmlines)
  3407. ;; When the "rmlines" attribute is provided, remove all hlines
  3408. ;; but the one separating heading from the table body.
  3409. (let ((n 0) (pos 0))
  3410. (while (and (< (length output) pos)
  3411. (setq pos (string-match "^\\\\hline\n?" output pos)))
  3412. (cl-incf n)
  3413. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  3414. (org-latex--decorate-table output attr caption above? info))))
  3415. (defun org-latex--math-table (table info)
  3416. "Return appropriate LaTeX code for a matrix.
  3417. TABLE is the table type element to transcode. INFO is a plist
  3418. used as a communication channel.
  3419. This function assumes TABLE has `org' as its `:type' property and
  3420. `inline-math' or `math' as its `:mode' attribute."
  3421. (let* ((attr (org-export-read-attribute :attr_latex table))
  3422. (env (or (plist-get attr :environment)
  3423. (plist-get info :latex-default-table-environment)))
  3424. (contents
  3425. (mapconcat
  3426. (lambda (row)
  3427. (if (eq (org-element-property :type row) 'rule) "\\hline"
  3428. ;; Return each cell unmodified.
  3429. (concat
  3430. (mapconcat
  3431. (lambda (cell)
  3432. (substring (org-element-interpret-data cell) 0 -1))
  3433. (org-element-map row 'table-cell #'identity info) "&")
  3434. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  3435. "\n")))
  3436. (org-element-map table 'table-row #'identity info) "")))
  3437. (concat
  3438. ;; Prefix.
  3439. (plist-get attr :math-prefix)
  3440. ;; Environment. Also treat special cases.
  3441. (cond ((member env '("array" "tabular"))
  3442. (format "\\begin{%s}{%s}\n%s\\end{%s}"
  3443. env (org-latex--align-string table info t) contents env))
  3444. ((assoc env org-latex-table-matrix-macros)
  3445. (format "\\%s%s{\n%s}"
  3446. env
  3447. (or (plist-get attr :math-arguments) "")
  3448. contents))
  3449. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  3450. ;; Suffix.
  3451. (plist-get attr :math-suffix))))
  3452. ;;;; Table Cell
  3453. (defun org-latex-table-cell (table-cell contents info)
  3454. "Transcode a TABLE-CELL element from Org to LaTeX.
  3455. CONTENTS is the cell contents. INFO is a plist used as
  3456. a communication channel."
  3457. (concat
  3458. (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
  3459. (if (and contents
  3460. scientific-format
  3461. (string-match orgtbl-exp-regexp contents))
  3462. ;; Use appropriate format string for scientific
  3463. ;; notation.
  3464. (format scientific-format
  3465. (match-string 1 contents)
  3466. (match-string 2 contents))
  3467. contents))
  3468. (when (org-export-get-next-element table-cell info) " & ")))
  3469. ;;;; Table Row
  3470. (defun org-latex-table-row (table-row contents info)
  3471. "Transcode a TABLE-ROW element from Org to LaTeX.
  3472. CONTENTS is the contents of the row. INFO is a plist used as
  3473. a communication channel."
  3474. (let* ((attr (org-export-read-attribute :attr_latex
  3475. (org-export-get-parent table-row)))
  3476. (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
  3477. (plist-get info :latex-tables-booktabs)))
  3478. (longtablep
  3479. (member (or (plist-get attr :environment)
  3480. (plist-get info :latex-default-table-environment))
  3481. '("longtable" "longtabu"))))
  3482. (if (eq (org-element-property :type table-row) 'rule)
  3483. (cond
  3484. ((not booktabsp) "\\hline")
  3485. ((not (org-export-get-previous-element table-row info)) "\\toprule")
  3486. ((not (org-export-get-next-element table-row info)) "\\bottomrule")
  3487. ((and longtablep
  3488. (org-export-table-row-ends-header-p
  3489. (org-export-get-previous-element table-row info) info))
  3490. "")
  3491. (t "\\midrule"))
  3492. (concat
  3493. ;; When BOOKTABS are activated enforce top-rule even when no
  3494. ;; hline was specifically marked.
  3495. (and booktabsp (not (org-export-get-previous-element table-row info))
  3496. "\\toprule\n")
  3497. contents "\\\\\n"
  3498. (cond
  3499. ;; Special case for long tables. Define header and footers.
  3500. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  3501. (let ((columns (cdr (org-export-table-dimensions
  3502. (org-export-get-parent-table table-row) info))))
  3503. (format "%s
  3504. \\endfirsthead
  3505. \\multicolumn{%d}{l}{%s} \\\\
  3506. %s
  3507. %s \\\\\n
  3508. %s
  3509. \\endhead
  3510. %s\\multicolumn{%d}{r}{%s} \\\\
  3511. \\endfoot
  3512. \\endlastfoot"
  3513. (if booktabsp "\\midrule" "\\hline")
  3514. columns
  3515. (org-latex--translate "Continued from previous page" info)
  3516. (cond
  3517. ((not (org-export-table-row-starts-header-p table-row info))
  3518. "")
  3519. (booktabsp "\\toprule\n")
  3520. (t "\\hline\n"))
  3521. contents
  3522. (if booktabsp "\\midrule" "\\hline")
  3523. (if booktabsp "\\midrule" "\\hline")
  3524. columns
  3525. (org-latex--translate "Continued on next page" info))))
  3526. ;; When BOOKTABS are activated enforce bottom rule even when
  3527. ;; no hline was specifically marked.
  3528. ((and booktabsp (not (org-export-get-next-element table-row info)))
  3529. "\\bottomrule"))))))
  3530. ;;;; Target
  3531. (defun org-latex-target (target _contents info)
  3532. "Transcode a TARGET object from Org to LaTeX.
  3533. CONTENTS is nil. INFO is a plist holding contextual
  3534. information."
  3535. (format "\\label{%s}" (org-latex--label target info)))
  3536. ;;;; Timestamp
  3537. (defun org-latex-timestamp (timestamp _contents info)
  3538. "Transcode a TIMESTAMP object from Org to LaTeX.
  3539. CONTENTS is nil. INFO is a plist holding contextual
  3540. information."
  3541. (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
  3542. (format
  3543. (plist-get info
  3544. (cl-case (org-element-property :type timestamp)
  3545. ((active active-range) :latex-active-timestamp-format)
  3546. ((inactive inactive-range) :latex-inactive-timestamp-format)
  3547. (otherwise :latex-diary-timestamp-format)))
  3548. value)))
  3549. ;;;; Underline
  3550. (defun org-latex-underline (_underline contents info)
  3551. "Transcode UNDERLINE from Org to LaTeX.
  3552. CONTENTS is the text with underline markup. INFO is a plist
  3553. holding contextual information."
  3554. (org-latex--text-markup contents 'underline info))
  3555. ;;;; Verbatim
  3556. (defun org-latex-verbatim (verbatim _contents info)
  3557. "Transcode a VERBATIM object from Org to LaTeX.
  3558. CONTENTS is nil. INFO is a plist used as a communication
  3559. channel."
  3560. (org-latex--text-markup
  3561. (org-element-property :value verbatim) 'verbatim info))
  3562. ;;;; Verse Block
  3563. (defun org-latex-verse-block (verse-block contents info)
  3564. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  3565. CONTENTS is verse block contents. INFO is a plist holding
  3566. contextual information."
  3567. (let* ((lin (org-export-read-attribute :attr_latex verse-block :lines))
  3568. (latcode (org-export-read-attribute :attr_latex verse-block :latexcode))
  3569. (cent (org-export-read-attribute :attr_latex verse-block :center))
  3570. (attr (concat
  3571. (if cent "[\\versewidth]" "")
  3572. (if lin (format "\n\\poemlines{%s}" lin) "")
  3573. (if latcode (format "\n%s" latcode) "")))
  3574. (versewidth (org-export-read-attribute :attr_latex verse-block :versewidth))
  3575. (vwidth (if versewidth (format "\\settowidth{\\versewidth}{%s}\n" versewidth) ""))
  3576. (linreset (if lin "\n\\poemlines{0}" "")))
  3577. (concat
  3578. (org-latex--wrap-label
  3579. verse-block
  3580. ;; In a verse environment, add a line break to each newline
  3581. ;; character and change each white space at beginning of a line
  3582. ;; into a space of 1 em. Also change each blank line with
  3583. ;; a vertical space of 1 em.
  3584. (format "%s\\begin{verse}%s\n%s\\end{verse}%s"
  3585. vwidth
  3586. attr
  3587. (replace-regexp-in-string
  3588. "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
  3589. (replace-regexp-in-string
  3590. "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
  3591. (replace-regexp-in-string
  3592. "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
  3593. contents nil t) nil t) nil t) linreset)
  3594. info)
  3595. ;; Insert footnote definitions, if any, after the environment, so
  3596. ;; the special formatting above is not applied to them.
  3597. (org-latex--delayed-footnotes-definitions verse-block info))))
  3598. ;;; End-user functions
  3599. ;;;###autoload
  3600. (defun org-latex-export-as-latex
  3601. (&optional async subtreep visible-only body-only ext-plist)
  3602. "Export current buffer as a LaTeX buffer.
  3603. If narrowing is active in the current buffer, only export its
  3604. narrowed part.
  3605. If a region is active, export that region.
  3606. A non-nil optional argument ASYNC means the process should happen
  3607. asynchronously. The resulting buffer should be accessible
  3608. through the `org-export-stack' interface.
  3609. When optional argument SUBTREEP is non-nil, export the sub-tree
  3610. at point, extracting information from the headline properties
  3611. first.
  3612. When optional argument VISIBLE-ONLY is non-nil, don't export
  3613. contents of hidden elements.
  3614. When optional argument BODY-ONLY is non-nil, only write code
  3615. between \"\\begin{document}\" and \"\\end{document}\".
  3616. EXT-PLIST, when provided, is a property list with external
  3617. parameters overriding Org default settings, but still inferior to
  3618. file-local settings.
  3619. Export is done in a buffer named \"*Org LATEX Export*\", which
  3620. will be displayed when `org-export-show-temporary-export-buffer'
  3621. is non-nil."
  3622. (interactive)
  3623. (org-export-to-buffer 'latex "*Org LATEX Export*"
  3624. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  3625. ;;;###autoload
  3626. (defun org-latex-convert-region-to-latex ()
  3627. "Assume the current region has Org syntax, and convert it to LaTeX.
  3628. This can be used in any buffer. For example, you can write an
  3629. itemized list in Org syntax in an LaTeX buffer and use this
  3630. command to convert it."
  3631. (interactive)
  3632. (org-export-replace-region-by 'latex))
  3633. ;;;###autoload
  3634. (defun org-latex-export-to-latex
  3635. (&optional async subtreep visible-only body-only ext-plist)
  3636. "Export current buffer to a LaTeX file.
  3637. If narrowing is active in the current buffer, only export its
  3638. narrowed part.
  3639. If a region is active, export that region.
  3640. A non-nil optional argument ASYNC means the process should happen
  3641. asynchronously. The resulting file should be accessible through
  3642. the `org-export-stack' interface.
  3643. When optional argument SUBTREEP is non-nil, export the sub-tree
  3644. at point, extracting information from the headline properties
  3645. first.
  3646. When optional argument VISIBLE-ONLY is non-nil, don't export
  3647. contents of hidden elements.
  3648. When optional argument BODY-ONLY is non-nil, only write code
  3649. between \"\\begin{document}\" and \"\\end{document}\".
  3650. EXT-PLIST, when provided, is a property list with external
  3651. parameters overriding Org default settings, but still inferior to
  3652. file-local settings."
  3653. (interactive)
  3654. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3655. (org-export-to-file 'latex outfile
  3656. async subtreep visible-only body-only ext-plist)))
  3657. ;;;###autoload
  3658. (defun org-latex-export-to-pdf
  3659. (&optional async subtreep visible-only body-only ext-plist)
  3660. "Export current buffer to LaTeX then process through to PDF.
  3661. If narrowing is active in the current buffer, only export its
  3662. narrowed part.
  3663. If a region is active, export that region.
  3664. A non-nil optional argument ASYNC means the process should happen
  3665. asynchronously. The resulting file should be accessible through
  3666. the `org-export-stack' interface.
  3667. When optional argument SUBTREEP is non-nil, export the sub-tree
  3668. at point, extracting information from the headline properties
  3669. first.
  3670. When optional argument VISIBLE-ONLY is non-nil, don't export
  3671. contents of hidden elements.
  3672. When optional argument BODY-ONLY is non-nil, only write code
  3673. between \"\\begin{document}\" and \"\\end{document}\".
  3674. EXT-PLIST, when provided, is a property list with external
  3675. parameters overriding Org default settings, but still inferior to
  3676. file-local settings.
  3677. Return PDF file's name."
  3678. (interactive)
  3679. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3680. (org-export-to-file 'latex outfile
  3681. async subtreep visible-only body-only ext-plist
  3682. #'org-latex-compile)))
  3683. (defun org-latex-compile (texfile &optional snippet)
  3684. "Compile a TeX file.
  3685. TEXFILE is the name of the file being compiled. Processing is
  3686. done through the command specified in `org-latex-pdf-process',
  3687. which see. Output is redirected to \"*Org PDF LaTeX Output*\"
  3688. buffer.
  3689. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  3690. file used to preview a LaTeX snippet. In this case, do not
  3691. create a log buffer and do not remove log files.
  3692. Return PDF file name or raise an error if it couldn't be
  3693. produced."
  3694. (unless snippet (message "Processing LaTeX file %s..." texfile))
  3695. (let* ((compiler
  3696. (or (with-temp-buffer
  3697. (save-excursion (insert-file-contents texfile))
  3698. (and (search-forward-regexp (regexp-opt org-latex-compilers)
  3699. (line-end-position 2)
  3700. t)
  3701. (progn (beginning-of-line) (looking-at-p "%"))
  3702. (match-string 0)))
  3703. ;; Cannot find the compiler inserted by
  3704. ;; `org-latex-template' -> `org-latex--insert-compiler'.
  3705. ;; Use a fallback.
  3706. "pdflatex"))
  3707. (process (if (functionp org-latex-pdf-process) org-latex-pdf-process
  3708. ;; Replace "%latex" with "%L" and "%bib" and
  3709. ;; "%bibtex" with "%B" to adhere to `format-spec'
  3710. ;; specifications.
  3711. (mapcar (lambda (command)
  3712. (replace-regexp-in-string
  3713. "%\\(?:\\(?:bib\\|la\\)tex\\|bib\\)\\>"
  3714. (lambda (m) (upcase (substring m 0 2)))
  3715. command))
  3716. org-latex-pdf-process)))
  3717. (spec `((?B . ,(shell-quote-argument org-latex-bib-compiler))
  3718. (?L . ,(shell-quote-argument compiler))))
  3719. (log-buf-name "*Org PDF LaTeX Output*")
  3720. (log-buf (and (not snippet) (get-buffer-create log-buf-name)))
  3721. (outfile (org-compile-file texfile process "pdf"
  3722. (format "See %S for details" log-buf-name)
  3723. log-buf spec)))
  3724. (unless snippet
  3725. (when org-latex-remove-logfiles
  3726. (mapc #'delete-file
  3727. (directory-files
  3728. (file-name-directory outfile)
  3729. t
  3730. (concat (regexp-quote (file-name-base outfile))
  3731. "\\(?:\\.[0-9]+\\)?\\."
  3732. (regexp-opt org-latex-logfiles-extensions))
  3733. t)))
  3734. (let ((warnings (org-latex--collect-warnings log-buf)))
  3735. (message (concat "PDF file produced"
  3736. (cond
  3737. ((eq warnings 'error) " with errors.")
  3738. (warnings (concat " with warnings: " warnings))
  3739. (t "."))))))
  3740. ;; Return output file name.
  3741. outfile))
  3742. (defun org-latex--collect-warnings (buffer)
  3743. "Collect some warnings from \"pdflatex\" command output.
  3744. BUFFER is the buffer containing output. Return collected
  3745. warnings types as a string, `error' if a LaTeX error was
  3746. encountered or nil if there was none."
  3747. (with-current-buffer buffer
  3748. (save-excursion
  3749. (goto-char (point-max))
  3750. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  3751. (if (re-search-forward "^!" nil t) 'error
  3752. (let ((case-fold-search t)
  3753. (warnings ""))
  3754. (dolist (warning org-latex-known-warnings)
  3755. (when (save-excursion (re-search-forward (car warning) nil t))
  3756. (setq warnings (concat warnings " " (cdr warning)))))
  3757. (org-string-nw-p (org-trim warnings))))))))
  3758. ;;;###autoload
  3759. (defun org-latex-publish-to-latex (plist filename pub-dir)
  3760. "Publish an Org file to LaTeX.
  3761. FILENAME is the filename of the Org file to be published. PLIST
  3762. is the property list for the given project. PUB-DIR is the
  3763. publishing directory.
  3764. Return output file name."
  3765. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  3766. ;;;###autoload
  3767. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  3768. "Publish an Org file to PDF (via LaTeX).
  3769. FILENAME is the filename of the Org file to be published. PLIST
  3770. is the property list for the given project. PUB-DIR is the
  3771. publishing directory.
  3772. Return output file name."
  3773. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  3774. ;; in working directory and then moved to publishing directory.
  3775. (org-publish-attachment
  3776. plist
  3777. ;; Default directory could be anywhere when this function is
  3778. ;; called. We ensure it is set to source file directory during
  3779. ;; compilation so as to not break links to external documents.
  3780. (let ((default-directory (file-name-directory filename)))
  3781. (org-latex-compile
  3782. (org-publish-org-to
  3783. 'latex filename ".tex" plist (file-name-directory filename))))
  3784. pub-dir))
  3785. (provide 'ox-latex)
  3786. ;; Local variables:
  3787. ;; generated-autoload-file: "org-loaddefs.el"
  3788. ;; End:
  3789. ;;; ox-latex.el ends here