ox-latex.el 139 KB

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