ox-latex.el 163 KB

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