ox-latex.el 159 KB

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