ox-latex.el 164 KB

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