ox-latex.el 149 KB

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