ox-latex.el 130 KB

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