ox-latex.el 131 KB

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