ox-latex.el 150 KB

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