ox-latex.el 128 KB

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