ox-latex.el 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694
  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 (lambda (tag) (org-latex-plain-text tag info))
  1744. tags ":")))))
  1745. ;;;; Horizontal Rule
  1746. (defun org-latex-horizontal-rule (horizontal-rule _contents info)
  1747. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1748. CONTENTS is nil. INFO is a plist holding contextual information."
  1749. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1750. (prev (org-export-get-previous-element horizontal-rule info)))
  1751. (concat
  1752. ;; Make sure the rule doesn't start at the end of the current
  1753. ;; line by separating it with a blank line from previous element.
  1754. (when (and prev
  1755. (let ((prev-blank (org-element-property :post-blank prev)))
  1756. (or (not prev-blank) (zerop prev-blank))))
  1757. "\n")
  1758. (org-latex--wrap-label
  1759. horizontal-rule
  1760. (format "\\rule{%s}{%s}"
  1761. (or (plist-get attr :width) "\\linewidth")
  1762. (or (plist-get attr :thickness) "0.5pt"))
  1763. info))))
  1764. ;;;; Inline Src Block
  1765. (defun org-latex-inline-src-block (inline-src-block _contents info)
  1766. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1767. CONTENTS holds the contents of the item. INFO is a plist holding
  1768. contextual information."
  1769. (let* ((code (org-element-property :value inline-src-block))
  1770. (separator (org-latex--find-verb-separator code)))
  1771. (cl-case (plist-get info :latex-listings)
  1772. ;; Do not use a special package: transcode it verbatim.
  1773. ((nil) (format "\\texttt{%s}" (org-latex--text-markup code 'code info)))
  1774. ;; Use minted package.
  1775. (minted
  1776. (let* ((org-lang (org-element-property :language inline-src-block))
  1777. (mint-lang (or (cadr (assq (intern org-lang)
  1778. (plist-get info :latex-minted-langs)))
  1779. (downcase org-lang)))
  1780. (options (org-latex--make-option-string
  1781. (plist-get info :latex-minted-options))))
  1782. (format "\\mintinline%s{%s}{%s}"
  1783. (if (string= options "") "" (format "[%s]" options))
  1784. mint-lang
  1785. code)))
  1786. ;; Use listings package.
  1787. (otherwise
  1788. ;; Maybe translate language's name.
  1789. (let* ((org-lang (org-element-property :language inline-src-block))
  1790. (lst-lang (or (cadr (assq (intern org-lang)
  1791. (plist-get info :latex-listings-langs)))
  1792. org-lang))
  1793. (options (org-latex--make-option-string
  1794. (append (plist-get info :latex-listings-options)
  1795. `(("language" ,lst-lang))))))
  1796. (concat (format "\\lstinline[%s]" options)
  1797. separator code separator))))))
  1798. ;;;; Inlinetask
  1799. (defun org-latex-inlinetask (inlinetask contents info)
  1800. "Transcode an INLINETASK element from Org to LaTeX.
  1801. CONTENTS holds the contents of the block. INFO is a plist
  1802. holding contextual information."
  1803. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1804. (todo (and (plist-get info :with-todo-keywords)
  1805. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1806. (and todo (org-export-data todo info)))))
  1807. (todo-type (org-element-property :todo-type inlinetask))
  1808. (tags (and (plist-get info :with-tags)
  1809. (org-export-get-tags inlinetask info)))
  1810. (priority (and (plist-get info :with-priority)
  1811. (org-element-property :priority inlinetask)))
  1812. (contents (concat (org-latex--label inlinetask info) contents)))
  1813. (funcall (plist-get info :latex-format-inlinetask-function)
  1814. todo todo-type priority title tags contents info)))
  1815. (defun org-latex-format-inlinetask-default-function
  1816. (todo _todo-type priority title tags contents info)
  1817. "Default format function for a inlinetasks.
  1818. See `org-latex-format-inlinetask-function' for details."
  1819. (let ((full-title
  1820. (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1821. (when priority (format "\\framebox{\\#%c} " priority))
  1822. title
  1823. (when tags
  1824. (format "\\hfill{}\\textsc{:%s:}"
  1825. (mapconcat
  1826. (lambda (tag) (org-latex-plain-text tag info))
  1827. tags ":"))))))
  1828. (concat "\\begin{center}\n"
  1829. "\\fbox{\n"
  1830. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1831. full-title "\n\n"
  1832. (and (org-string-nw-p contents)
  1833. (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
  1834. "\\end{minipage}\n"
  1835. "}\n"
  1836. "\\end{center}")))
  1837. ;;;; Italic
  1838. (defun org-latex-italic (_italic contents info)
  1839. "Transcode ITALIC from Org to LaTeX.
  1840. CONTENTS is the text with italic markup. INFO is a plist holding
  1841. contextual information."
  1842. (org-latex--text-markup contents 'italic info))
  1843. ;;;; Item
  1844. (defun org-latex-item (item contents info)
  1845. "Transcode an ITEM element from Org to LaTeX.
  1846. CONTENTS holds the contents of the item. INFO is a plist holding
  1847. contextual information."
  1848. (let* ((counter
  1849. (let ((count (org-element-property :counter item))
  1850. (level
  1851. ;; Determine level of current item to determine the
  1852. ;; correct LaTeX counter to use (enumi, enumii...).
  1853. (let ((parent item) (level 0))
  1854. (while (memq (org-element-type
  1855. (setq parent (org-export-get-parent parent)))
  1856. '(plain-list item))
  1857. (when (and (eq (org-element-type parent) 'plain-list)
  1858. (eq (org-element-property :type parent)
  1859. 'ordered))
  1860. (cl-incf level)))
  1861. level)))
  1862. (and count
  1863. (< level 5)
  1864. (format "\\setcounter{enum%s}{%s}\n"
  1865. (nth (1- level) '("i" "ii" "iii" "iv"))
  1866. (1- count)))))
  1867. (checkbox (cl-case (org-element-property :checkbox item)
  1868. (on "$\\boxtimes$ ")
  1869. (off "$\\square$ ")
  1870. (trans "$\\boxminus$ ")))
  1871. (tag (let ((tag (org-element-property :tag item)))
  1872. ;; Check-boxes must belong to the tag.
  1873. (and tag (format "[{%s}] "
  1874. (concat checkbox
  1875. (org-export-data tag info)))))))
  1876. (concat counter
  1877. "\\item"
  1878. (cond
  1879. (tag)
  1880. (checkbox (concat " " checkbox))
  1881. ;; Without a tag or a check-box, if CONTENTS starts with
  1882. ;; an opening square bracket, add "\relax" to "\item",
  1883. ;; unless the brackets comes from an initial export
  1884. ;; snippet (i.e. it is inserted willingly by the user).
  1885. ((and contents
  1886. (string-match-p "\\`[ \t]*\\[" contents)
  1887. (not (let ((e (car (org-element-contents item))))
  1888. (and (eq (org-element-type e) 'paragraph)
  1889. (let ((o (car (org-element-contents e))))
  1890. (and (eq (org-element-type o) 'export-snippet)
  1891. (eq (org-export-snippet-backend o)
  1892. 'latex)))))))
  1893. "\\relax ")
  1894. (t " "))
  1895. (and contents (org-trim contents))
  1896. ;; If there are footnotes references in tag, be sure to
  1897. ;; add their definition at the end of the item. This
  1898. ;; workaround is necessary since "\footnote{}" command is
  1899. ;; not supported in tags.
  1900. (and tag
  1901. (org-latex--delayed-footnotes-definitions
  1902. (org-element-property :tag item) info)))))
  1903. ;;;; Keyword
  1904. (defun org-latex-keyword (keyword _contents info)
  1905. "Transcode a KEYWORD element from Org to LaTeX.
  1906. CONTENTS is nil. INFO is a plist holding contextual information."
  1907. (let ((key (org-element-property :key keyword))
  1908. (value (org-element-property :value keyword)))
  1909. (cond
  1910. ((string= key "LATEX") value)
  1911. ((string= key "INDEX") (format "\\index{%s}" value))
  1912. ((string= key "TOC")
  1913. (let ((case-fold-search t))
  1914. (cond
  1915. ((string-match-p "\\<headlines\\>" value)
  1916. (let* ((localp (string-match-p "\\<local\\>" value))
  1917. (parent (org-element-lineage keyword '(headline)))
  1918. (level (if (not (and localp parent)) 0
  1919. (org-export-get-relative-level parent info)))
  1920. (depth
  1921. (and (string-match "\\<[0-9]+\\>" value)
  1922. (format
  1923. "\\setcounter{tocdepth}{%d}"
  1924. (+ (string-to-number (match-string 0 value)) level)))))
  1925. (if (and localp parent)
  1926. ;; Start local TOC, assuming package "titletoc" is
  1927. ;; required.
  1928. (format "\\startcontents[level-%d]
  1929. \\printcontents[level-%d]{}{0}{%s}"
  1930. level level (or depth ""))
  1931. (concat depth (and depth "\n") "\\tableofcontents"))))
  1932. ((string-match-p "\\<tables\\>" value) "\\listoftables")
  1933. ((string-match-p "\\<listings\\>" value)
  1934. (cl-case (plist-get info :latex-listings)
  1935. ((nil) "\\listoffigures")
  1936. (minted "\\listoflistings")
  1937. (otherwise "\\lstlistoflistings")))))))))
  1938. ;;;; Latex Environment
  1939. (defun org-latex-latex-environment (latex-environment _contents info)
  1940. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1941. CONTENTS is nil. INFO is a plist holding contextual information."
  1942. (when (plist-get info :with-latex)
  1943. (let ((value (org-remove-indentation
  1944. (org-element-property :value latex-environment))))
  1945. (if (not (org-element-property :name latex-environment)) value
  1946. ;; Environment is labeled: label must be within the environment
  1947. ;; (otherwise, a reference pointing to that element will count
  1948. ;; the section instead).
  1949. (with-temp-buffer
  1950. (insert value)
  1951. (goto-char (point-min))
  1952. (forward-line)
  1953. (insert (org-latex--label latex-environment info nil t))
  1954. (buffer-string))))))
  1955. ;;;; Latex Fragment
  1956. (defun org-latex-latex-fragment (latex-fragment _contents _info)
  1957. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1958. CONTENTS is nil. INFO is a plist holding contextual information."
  1959. (let ((value (org-element-property :value latex-fragment)))
  1960. ;; Trim math markers since the fragment is enclosed within
  1961. ;; a latex-math-block object anyway.
  1962. (cond ((string-match-p "\\`\\$[^$]" value) (substring value 1 -1))
  1963. ((string-prefix-p "\\(" value) (substring value 2 -2))
  1964. (t value))))
  1965. ;;;; Line Break
  1966. (defun org-latex-line-break (_line-break _contents _info)
  1967. "Transcode a LINE-BREAK object from Org to LaTeX.
  1968. CONTENTS is nil. INFO is a plist holding contextual information."
  1969. "\\\\\n")
  1970. ;;;; Link
  1971. (defun org-latex-image-link-filter (data _backend info)
  1972. (org-export-insert-image-links data info org-latex-inline-image-rules))
  1973. (defun org-latex--inline-image (link info)
  1974. "Return LaTeX code for an inline image.
  1975. LINK is the link pointing to the inline image. INFO is a plist
  1976. used as a communication channel."
  1977. (let* ((parent (org-export-get-parent-element link))
  1978. (path (let ((raw-path (org-element-property :path link)))
  1979. (if (not (file-name-absolute-p raw-path)) raw-path
  1980. (expand-file-name raw-path))))
  1981. (filetype (file-name-extension path))
  1982. (caption (org-latex--caption/label-string parent info))
  1983. (caption-above-p (org-latex--caption-above-p link info))
  1984. ;; Retrieve latex attributes from the element around.
  1985. (attr (org-export-read-attribute :attr_latex parent))
  1986. (float (let ((float (plist-get attr :float)))
  1987. (cond ((string= float "wrap") 'wrap)
  1988. ((string= float "sideways") 'sideways)
  1989. ((string= float "multicolumn") 'multicolumn)
  1990. ((and (plist-member attr :float) (not float)) 'nonfloat)
  1991. ((or float
  1992. (org-element-property :caption parent)
  1993. (org-string-nw-p (plist-get attr :caption)))
  1994. 'figure)
  1995. (t 'nonfloat))))
  1996. (placement
  1997. (let ((place (plist-get attr :placement)))
  1998. (cond
  1999. (place (format "%s" place))
  2000. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  2001. ((eq float 'figure)
  2002. (format "[%s]" (plist-get info :latex-default-figure-position)))
  2003. (t ""))))
  2004. (center
  2005. (if (plist-member attr :center) (plist-get attr :center)
  2006. (plist-get info :latex-images-centered)))
  2007. (comment-include (if (plist-get attr :comment-include) "%" ""))
  2008. ;; It is possible to specify width and height in the
  2009. ;; ATTR_LATEX line, and also via default variables.
  2010. (width (cond ((plist-get attr :width))
  2011. ((plist-get attr :height) "")
  2012. ((eq float 'wrap) "0.48\\textwidth")
  2013. (t (plist-get info :latex-image-default-width))))
  2014. (height (cond ((plist-get attr :height))
  2015. ((or (plist-get attr :width)
  2016. (memq float '(figure wrap))) "")
  2017. (t (plist-get info :latex-image-default-height))))
  2018. (options (let ((opt (or (plist-get attr :options)
  2019. (plist-get info :latex-image-default-option))))
  2020. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  2021. (match-string 1 opt))))
  2022. image-code)
  2023. (if (member filetype '("tikz" "pgf"))
  2024. ;; For tikz images:
  2025. ;; - use \input to read in image file.
  2026. ;; - if options are present, wrap in a tikzpicture environment.
  2027. ;; - if width or height are present, use \resizebox to change
  2028. ;; the image size.
  2029. (progn
  2030. (setq image-code (format "\\input{%s}" path))
  2031. (when (org-string-nw-p options)
  2032. (setq image-code
  2033. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  2034. options
  2035. image-code)))
  2036. (when (or (org-string-nw-p width) (org-string-nw-p height))
  2037. (setq image-code (format "\\resizebox{%s}{%s}{%s}"
  2038. (if (org-string-nw-p width) width "!")
  2039. (if (org-string-nw-p height) height "!")
  2040. image-code))))
  2041. ;; For other images:
  2042. ;; - add width and height to options.
  2043. ;; - include the image with \includegraphics.
  2044. (when (org-string-nw-p width)
  2045. (setq options (concat options ",width=" width)))
  2046. (when (org-string-nw-p height)
  2047. (setq options (concat options ",height=" height)))
  2048. (let ((search-option (org-element-property :search-option link)))
  2049. (when (and search-option
  2050. (equal filetype "pdf")
  2051. (string-match-p "\\`[0-9]+\\'" search-option)
  2052. (not (string-match-p "page=" options)))
  2053. (setq options (concat options ",page=" search-option))))
  2054. (setq image-code
  2055. (format "\\includegraphics%s{%s}"
  2056. (cond ((not (org-string-nw-p options)) "")
  2057. ((string-prefix-p "," options)
  2058. (format "[%s]" (substring options 1)))
  2059. (t (format "[%s]" options)))
  2060. path))
  2061. (when (equal filetype "svg")
  2062. (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
  2063. "\\includesvg"
  2064. image-code
  2065. nil t))
  2066. (setq image-code (replace-regexp-in-string "\\.svg}"
  2067. "}"
  2068. image-code
  2069. nil t))))
  2070. ;; Return proper string, depending on FLOAT.
  2071. (pcase float
  2072. (`wrap (format "\\begin{wrapfigure}%s
  2073. %s%s
  2074. %s%s
  2075. %s\\end{wrapfigure}"
  2076. placement
  2077. (if caption-above-p caption "")
  2078. (if center "\\centering" "")
  2079. comment-include image-code
  2080. (if caption-above-p "" caption)))
  2081. (`sideways (format "\\begin{sidewaysfigure}
  2082. %s%s
  2083. %s%s
  2084. %s\\end{sidewaysfigure}"
  2085. (if caption-above-p caption "")
  2086. (if center "\\centering" "")
  2087. comment-include image-code
  2088. (if caption-above-p "" caption)))
  2089. (`multicolumn (format "\\begin{figure*}%s
  2090. %s%s
  2091. %s%s
  2092. %s\\end{figure*}"
  2093. placement
  2094. (if caption-above-p caption "")
  2095. (if center "\\centering" "")
  2096. comment-include image-code
  2097. (if caption-above-p "" caption)))
  2098. (`figure (format "\\begin{figure}%s
  2099. %s%s
  2100. %s%s
  2101. %s\\end{figure}"
  2102. placement
  2103. (if caption-above-p caption "")
  2104. (if center "\\centering" "")
  2105. comment-include image-code
  2106. (if caption-above-p "" caption)))
  2107. ((guard center)
  2108. (format "\\begin{center}
  2109. %s%s
  2110. %s\\end{center}"
  2111. (if caption-above-p caption "")
  2112. image-code
  2113. (if caption-above-p "" caption)))
  2114. (_
  2115. (concat (if caption-above-p caption "")
  2116. image-code
  2117. (if caption-above-p caption ""))))))
  2118. (defun org-latex-link (link desc info)
  2119. "Transcode a LINK object from Org to LaTeX.
  2120. DESC is the description part of the link, or the empty string.
  2121. INFO is a plist holding contextual information. See
  2122. `org-export-data'."
  2123. (let* ((type (org-element-property :type link))
  2124. (raw-path (org-element-property :path link))
  2125. ;; Ensure DESC really exists, or set it to nil.
  2126. (desc (and (not (string= desc "")) desc))
  2127. (imagep (org-export-inline-image-p
  2128. link (plist-get info :latex-inline-image-rules)))
  2129. (path (org-latex--protect-text
  2130. (cond ((member type '("http" "https" "ftp" "mailto" "doi"))
  2131. (concat type ":" raw-path))
  2132. ((string= type "file") (org-export-file-uri raw-path))
  2133. (t raw-path)))))
  2134. (cond
  2135. ;; Link type is handled by a special function.
  2136. ((org-export-custom-protocol-maybe link desc 'latex))
  2137. ;; Image file.
  2138. (imagep (org-latex--inline-image link info))
  2139. ;; Radio link: Transcode target's contents and use them as link's
  2140. ;; description.
  2141. ((string= type "radio")
  2142. (let ((destination (org-export-resolve-radio-link link info)))
  2143. (if (not destination) desc
  2144. (format "\\hyperref[%s]{%s}"
  2145. (org-export-get-reference destination info)
  2146. desc))))
  2147. ;; Links pointing to a headline: Find destination and build
  2148. ;; appropriate referencing command.
  2149. ((member type '("custom-id" "fuzzy" "id"))
  2150. (let ((destination (if (string= type "fuzzy")
  2151. (org-export-resolve-fuzzy-link link info)
  2152. (org-export-resolve-id-link link info))))
  2153. (cl-case (org-element-type destination)
  2154. ;; Id link points to an external file.
  2155. (plain-text
  2156. (if desc (format "\\href{%s}{%s}" destination desc)
  2157. (format "\\url{%s}" destination)))
  2158. ;; Fuzzy link points nowhere.
  2159. ((nil)
  2160. (format (plist-get info :latex-link-with-unknown-path-format)
  2161. (or desc
  2162. (org-export-data
  2163. (org-element-property :raw-link link) info))))
  2164. ;; LINK points to a headline. If headlines are numbered
  2165. ;; and the link has no description, display headline's
  2166. ;; number. Otherwise, display description or headline's
  2167. ;; title.
  2168. (headline
  2169. (let ((label (org-latex--label destination info t)))
  2170. (if (and (not desc)
  2171. (org-export-numbered-headline-p destination info))
  2172. (format "\\ref{%s}" label)
  2173. (format "\\hyperref[%s]{%s}" label
  2174. (or desc
  2175. (org-export-data
  2176. (org-element-property :title destination) info))))))
  2177. ;; Fuzzy link points to a target. Do as above.
  2178. (otherwise
  2179. (let ((ref (org-latex--label destination info t)))
  2180. (if (not desc) (format "\\ref{%s}" ref)
  2181. (format "\\hyperref[%s]{%s}" ref desc)))))))
  2182. ;; Coderef: replace link with the reference name or the
  2183. ;; equivalent line number.
  2184. ((string= type "coderef")
  2185. (format (org-export-get-coderef-format path desc)
  2186. (org-export-resolve-coderef path info)))
  2187. ;; External link with a description part.
  2188. ((and path desc) (format "\\href{%s}{%s}" path desc))
  2189. ;; External link without a description part.
  2190. (path (format "\\url{%s}" path))
  2191. ;; No path, only description. Try to do something useful.
  2192. (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
  2193. ;;;; Node Property
  2194. (defun org-latex-node-property (node-property _contents _info)
  2195. "Transcode a NODE-PROPERTY element from Org to LaTeX.
  2196. CONTENTS is nil. INFO is a plist holding contextual
  2197. information."
  2198. (format "%s:%s"
  2199. (org-element-property :key node-property)
  2200. (let ((value (org-element-property :value node-property)))
  2201. (if value (concat " " value) ""))))
  2202. ;;;; Paragraph
  2203. (defun org-latex-paragraph (_paragraph contents _info)
  2204. "Transcode a PARAGRAPH element from Org to LaTeX.
  2205. CONTENTS is the contents of the paragraph, as a string. INFO is
  2206. the plist used as a communication channel."
  2207. contents)
  2208. ;;;; Plain List
  2209. (defun org-latex-plain-list (plain-list contents info)
  2210. "Transcode a PLAIN-LIST element from Org to LaTeX.
  2211. CONTENTS is the contents of the list. INFO is a plist holding
  2212. contextual information."
  2213. (let* ((type (org-element-property :type plain-list))
  2214. (attr (org-export-read-attribute :attr_latex plain-list))
  2215. (latex-type (let ((env (plist-get attr :environment)))
  2216. (cond (env (format "%s" env))
  2217. ((eq type 'ordered) "enumerate")
  2218. ((eq type 'descriptive) "description")
  2219. (t "itemize")))))
  2220. (org-latex--wrap-label
  2221. plain-list
  2222. (format "\\begin{%s}%s\n%s\\end{%s}"
  2223. latex-type
  2224. (or (plist-get attr :options) "")
  2225. contents
  2226. latex-type)
  2227. info)))
  2228. ;;;; Plain Text
  2229. (defun org-latex-plain-text (text info)
  2230. "Transcode a TEXT string from Org to LaTeX.
  2231. TEXT is the string to transcode. INFO is a plist holding
  2232. contextual information."
  2233. (let* ((specialp (plist-get info :with-special-strings))
  2234. (output
  2235. ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
  2236. (let ((case-fold-search nil))
  2237. (replace-regexp-in-string
  2238. "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
  2239. ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
  2240. ;; However, if special strings are used, be careful not
  2241. ;; to protect "\" in "\-" constructs.
  2242. (replace-regexp-in-string
  2243. (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
  2244. (lambda (m)
  2245. (cl-case (string-to-char m)
  2246. (?\\ "$\\\\backslash$\\1")
  2247. (?~ "\\\\textasciitilde{}")
  2248. (?^ "\\\\^{}")
  2249. (t "\\\\\\&")))
  2250. text)))))
  2251. ;; Activate smart quotes. Be sure to provide original TEXT string
  2252. ;; since OUTPUT may have been modified.
  2253. (when (plist-get info :with-smart-quotes)
  2254. (setq output (org-export-activate-smart-quotes output :latex info text)))
  2255. ;; Convert special strings.
  2256. (when specialp
  2257. (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
  2258. ;; Handle break preservation if required.
  2259. (when (plist-get info :preserve-breaks)
  2260. (setq output (replace-regexp-in-string
  2261. "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
  2262. ;; Return value.
  2263. output))
  2264. ;;;; Planning
  2265. (defun org-latex-planning (planning _contents info)
  2266. "Transcode a PLANNING element from Org to LaTeX.
  2267. CONTENTS is nil. INFO is a plist holding contextual
  2268. information."
  2269. (concat
  2270. "\\noindent"
  2271. (mapconcat
  2272. 'identity
  2273. (delq nil
  2274. (list
  2275. (let ((closed (org-element-property :closed planning)))
  2276. (when closed
  2277. (concat
  2278. (format "\\textbf{%s} " org-closed-string)
  2279. (format (plist-get info :latex-inactive-timestamp-format)
  2280. (org-timestamp-translate closed)))))
  2281. (let ((deadline (org-element-property :deadline planning)))
  2282. (when deadline
  2283. (concat
  2284. (format "\\textbf{%s} " org-deadline-string)
  2285. (format (plist-get info :latex-active-timestamp-format)
  2286. (org-timestamp-translate deadline)))))
  2287. (let ((scheduled (org-element-property :scheduled planning)))
  2288. (when scheduled
  2289. (concat
  2290. (format "\\textbf{%s} " org-scheduled-string)
  2291. (format (plist-get info :latex-active-timestamp-format)
  2292. (org-timestamp-translate scheduled)))))))
  2293. " ")
  2294. "\\\\"))
  2295. ;;;; Property Drawer
  2296. (defun org-latex-property-drawer (_property-drawer contents _info)
  2297. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  2298. CONTENTS holds the contents of the drawer. INFO is a plist
  2299. holding contextual information."
  2300. (and (org-string-nw-p contents)
  2301. (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
  2302. ;;;; Pseudo Element: LaTeX Matrices
  2303. ;; `latex-matrices' elements have the following properties:
  2304. ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
  2305. ;; `math').
  2306. (defun org-latex--wrap-latex-matrices (data info)
  2307. "Merge contiguous tables with the same mode within a pseudo-element.
  2308. DATA is a parse tree or a secondary string. INFO is a plist
  2309. containing export options. Modify DATA by side-effect and return
  2310. it."
  2311. (org-element-map data 'table
  2312. (lambda (table)
  2313. (when (eq (org-element-property :type table) 'org)
  2314. (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
  2315. (plist-get info :latex-default-table-mode))))
  2316. (when (and (member mode '("inline-math" "math"))
  2317. ;; Do not wrap twice the same table.
  2318. (not (eq (org-element-type
  2319. (org-element-property :parent table))
  2320. 'latex-matrices)))
  2321. (let* ((caption (and (not (string= mode "inline-math"))
  2322. (org-element-property :caption table)))
  2323. (matrices
  2324. (list 'latex-matrices
  2325. (list :caption caption
  2326. :markup
  2327. (cond ((string= mode "inline-math") 'inline)
  2328. (caption 'equation)
  2329. (t 'math)))))
  2330. (previous table)
  2331. (next (org-export-get-next-element table info)))
  2332. (org-element-insert-before matrices table)
  2333. ;; Swallow all contiguous tables sharing the same mode.
  2334. (while (and
  2335. (zerop (or (org-element-property :post-blank previous) 0))
  2336. (setq next (org-export-get-next-element previous info))
  2337. (eq (org-element-type next) 'table)
  2338. (eq (org-element-property :type next) 'org)
  2339. (string= (or (org-export-read-attribute
  2340. :attr_latex next :mode)
  2341. (plist-get info :latex-default-table-mode))
  2342. mode))
  2343. (org-element-extract-element previous)
  2344. (org-element-adopt-elements matrices previous)
  2345. (setq previous next))
  2346. ;; Inherit `:post-blank' from the value of the last
  2347. ;; swallowed table. Set the latter's `:post-blank'
  2348. ;; value to 0 so as to not duplicate empty lines.
  2349. (org-element-put-property
  2350. matrices :post-blank (org-element-property :post-blank previous))
  2351. (org-element-put-property previous :post-blank 0)
  2352. (org-element-extract-element previous)
  2353. (org-element-adopt-elements matrices previous))))))
  2354. info)
  2355. data)
  2356. (defun org-latex-matrices (matrices contents _info)
  2357. "Transcode a MATRICES element from Org to LaTeX.
  2358. CONTENTS is a string. INFO is a plist used as a communication
  2359. channel."
  2360. (format (cl-case (org-element-property :markup matrices)
  2361. (inline "\\(%s\\)")
  2362. (equation "\\begin{equation}\n%s\\end{equation}")
  2363. (t "\\[\n%s\\]"))
  2364. contents))
  2365. (defun org-latex-matrices-tree-filter (tree _backend info)
  2366. (org-latex--wrap-latex-matrices tree info))
  2367. ;;;; Pseudo Object: LaTeX Math Block
  2368. ;; `latex-math-block' objects have the following property:
  2369. ;; `:post-blank'.
  2370. (defun org-latex--wrap-latex-math-block (data info)
  2371. "Merge contiguous math objects in a pseudo-object container.
  2372. DATA is a parse tree or a secondary string. INFO is a plist
  2373. containing export options. Modify DATA by side-effect and return it."
  2374. (let ((valid-object-p
  2375. ;; Non-nil when OBJ can be added to the latex math block B.
  2376. (lambda (obj b)
  2377. (pcase (org-element-type obj)
  2378. (`entity (org-element-property :latex-math-p obj))
  2379. (`latex-fragment
  2380. (let ((value (org-element-property :value obj)))
  2381. (or (string-prefix-p "\\(" value)
  2382. (string-match-p "\\`\\$[^$]" value))))
  2383. ((and type (or `subscript `superscript))
  2384. (not (memq type (mapcar #'org-element-type
  2385. (org-element-contents b)))))))))
  2386. (org-element-map data '(entity latex-fragment subscript superscript)
  2387. (lambda (object)
  2388. ;; Skip objects already wrapped.
  2389. (when (and (not (eq (org-element-type
  2390. (org-element-property :parent object))
  2391. 'latex-math-block))
  2392. (funcall valid-object-p object nil))
  2393. (let ((math-block (list 'latex-math-block nil))
  2394. (next-elements (org-export-get-next-element object info t))
  2395. (last object))
  2396. ;; Wrap MATH-BLOCK around OBJECT in DATA.
  2397. (org-element-insert-before math-block object)
  2398. (org-element-extract-element object)
  2399. (org-element-adopt-elements math-block object)
  2400. (when (zerop (or (org-element-property :post-blank object) 0))
  2401. ;; MATH-BLOCK swallows consecutive math objects.
  2402. (catch 'exit
  2403. (dolist (next next-elements)
  2404. (unless (funcall valid-object-p next math-block)
  2405. (throw 'exit nil))
  2406. (org-element-extract-element next)
  2407. (org-element-adopt-elements math-block next)
  2408. ;; Eschew the case: \beta$x$ -> \(\betax\).
  2409. (unless (memq (org-element-type next)
  2410. '(subscript superscript))
  2411. (org-element-put-property last :post-blank 1))
  2412. (setq last next)
  2413. (when (> (or (org-element-property :post-blank next) 0) 0)
  2414. (throw 'exit nil)))))
  2415. (org-element-put-property
  2416. math-block :post-blank (org-element-property :post-blank last)))))
  2417. info nil '(subscript superscript latex-math-block) t)
  2418. ;; Return updated DATA.
  2419. data))
  2420. (defun org-latex-math-block-tree-filter (tree _backend info)
  2421. (org-latex--wrap-latex-math-block tree info))
  2422. (defun org-latex-math-block-options-filter (info _backend)
  2423. (dolist (prop '(:author :date :title) info)
  2424. (plist-put info prop
  2425. (org-latex--wrap-latex-math-block (plist-get info prop) info))))
  2426. (defun org-latex-math-block (_math-block contents _info)
  2427. "Transcode a MATH-BLOCK object from Org to LaTeX.
  2428. CONTENTS is a string. INFO is a plist used as a communication
  2429. channel."
  2430. (when (org-string-nw-p contents)
  2431. (format "\\(%s\\)" (org-trim contents))))
  2432. ;;;; Quote Block
  2433. (defun org-latex-quote-block (quote-block contents info)
  2434. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  2435. CONTENTS holds the contents of the block. INFO is a plist
  2436. holding contextual information."
  2437. (org-latex--wrap-label
  2438. quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
  2439. ;;;; Radio Target
  2440. (defun org-latex-radio-target (radio-target text info)
  2441. "Transcode a RADIO-TARGET object from Org to LaTeX.
  2442. TEXT is the text of the target. INFO is a plist holding
  2443. contextual information."
  2444. (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
  2445. ;;;; Section
  2446. (defun org-latex-section (_section contents _info)
  2447. "Transcode a SECTION element from Org to LaTeX.
  2448. CONTENTS holds the contents of the section. INFO is a plist
  2449. holding contextual information."
  2450. contents)
  2451. ;;;; Special Block
  2452. (defun org-latex-special-block (special-block contents info)
  2453. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  2454. CONTENTS holds the contents of the block. INFO is a plist
  2455. holding contextual information."
  2456. (let ((type (org-element-property :type special-block))
  2457. (opt (org-export-read-attribute :attr_latex special-block :options))
  2458. (caption (org-latex--caption/label-string special-block info))
  2459. (caption-above-p (org-latex--caption-above-p special-block info)))
  2460. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  2461. (and caption-above-p caption)
  2462. contents
  2463. (and (not caption-above-p) caption)
  2464. (format "\\end{%s}" type))))
  2465. ;;;; Src Block
  2466. (defun org-latex-src-block (src-block _contents info)
  2467. "Transcode a SRC-BLOCK element from Org to LaTeX.
  2468. CONTENTS holds the contents of the item. INFO is a plist holding
  2469. contextual information."
  2470. (when (org-string-nw-p (org-element-property :value src-block))
  2471. (let* ((lang (org-element-property :language src-block))
  2472. (caption (org-element-property :caption src-block))
  2473. (caption-above-p (org-latex--caption-above-p src-block info))
  2474. (label (org-element-property :name src-block))
  2475. (custom-env (and lang
  2476. (cadr (assq (intern lang)
  2477. org-latex-custom-lang-environments))))
  2478. (num-start (org-export-get-loc src-block info))
  2479. (retain-labels (org-element-property :retain-labels src-block))
  2480. (attributes (org-export-read-attribute :attr_latex src-block))
  2481. (float (plist-get attributes :float))
  2482. (listings (plist-get info :latex-listings)))
  2483. (cond
  2484. ;; Case 1. No source fontification.
  2485. ((not listings)
  2486. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2487. (float-env
  2488. (cond ((string= "multicolumn" float)
  2489. (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
  2490. (plist-get info :latex-default-figure-position)
  2491. (if caption-above-p caption-str "")
  2492. (if caption-above-p "" caption-str)))
  2493. (caption (concat
  2494. (if caption-above-p caption-str "")
  2495. "%s"
  2496. (if caption-above-p "" (concat "\n" caption-str))))
  2497. (t "%s"))))
  2498. (format
  2499. float-env
  2500. (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
  2501. (org-export-format-code-default src-block info))))))
  2502. ;; Case 2. Custom environment.
  2503. (custom-env
  2504. (let ((caption-str (org-latex--caption/label-string src-block info))
  2505. (formatted-src (org-export-format-code-default src-block info)))
  2506. (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
  2507. (format "\\begin{%s}\n%s\\end{%s}\n"
  2508. custom-env
  2509. (concat (and caption-above-p caption-str)
  2510. formatted-src
  2511. (and (not caption-above-p) caption-str))
  2512. custom-env)
  2513. (format-spec custom-env
  2514. `((?s . ,formatted-src)
  2515. (?c . ,caption)
  2516. (?f . ,float)
  2517. (?l . ,(org-latex--label src-block info))
  2518. (?o . ,(or (plist-get attributes :options) "")))))))
  2519. ;; Case 3. Use minted package.
  2520. ((eq listings 'minted)
  2521. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2522. (float-env
  2523. (cond
  2524. ((string= "multicolumn" float)
  2525. (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
  2526. (if caption-above-p caption-str "")
  2527. (if caption-above-p "" caption-str)))
  2528. (caption
  2529. (concat (if caption-above-p caption-str "")
  2530. "%s"
  2531. (if caption-above-p "" (concat "\n" caption-str))))
  2532. (t "%s")))
  2533. (options (plist-get info :latex-minted-options))
  2534. (body
  2535. (format
  2536. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  2537. ;; Options.
  2538. (concat
  2539. (org-latex--make-option-string
  2540. (if (or (not num-start) (assoc "linenos" options))
  2541. options
  2542. (append
  2543. `(("linenos")
  2544. ("firstnumber" ,(number-to-string (1+ num-start))))
  2545. options)))
  2546. (let ((local-options (plist-get attributes :options)))
  2547. (and local-options (concat "," local-options))))
  2548. ;; Language.
  2549. (or (cadr (assq (intern lang)
  2550. (plist-get info :latex-minted-langs)))
  2551. (downcase lang))
  2552. ;; Source code.
  2553. (let* ((code-info (org-export-unravel-code src-block))
  2554. (max-width
  2555. (apply 'max
  2556. (mapcar 'length
  2557. (org-split-string (car code-info)
  2558. "\n")))))
  2559. (org-export-format-code
  2560. (car code-info)
  2561. (lambda (loc _num ref)
  2562. (concat
  2563. loc
  2564. (when ref
  2565. ;; Ensure references are flushed to the right,
  2566. ;; separated with 6 spaces from the widest line
  2567. ;; of code.
  2568. (concat (make-string (+ (- max-width (length loc)) 6)
  2569. ?\s)
  2570. (format "(%s)" ref)))))
  2571. nil (and retain-labels (cdr code-info)))))))
  2572. ;; Return value.
  2573. (format float-env body)))
  2574. ;; Case 4. Use listings package.
  2575. (t
  2576. (let ((lst-lang
  2577. (or (cadr (assq (intern lang)
  2578. (plist-get info :latex-listings-langs)))
  2579. lang))
  2580. (caption-str
  2581. (when caption
  2582. (let ((main (org-export-get-caption src-block))
  2583. (secondary (org-export-get-caption src-block t)))
  2584. (if (not secondary)
  2585. (format "{%s}" (org-export-data main info))
  2586. (format "{[%s]%s}"
  2587. (org-export-data secondary info)
  2588. (org-export-data main info))))))
  2589. (lst-opt (plist-get info :latex-listings-options)))
  2590. (concat
  2591. ;; Options.
  2592. (format
  2593. "\\lstset{%s}\n"
  2594. (concat
  2595. (org-latex--make-option-string
  2596. (append
  2597. lst-opt
  2598. (cond
  2599. ((and (not float) (plist-member attributes :float)) nil)
  2600. ((string= "multicolumn" float) '(("float" "*")))
  2601. ((and float (not (assoc "float" lst-opt)))
  2602. `(("float" ,(plist-get info :latex-default-figure-position)))))
  2603. `(("language" ,lst-lang))
  2604. (if label
  2605. `(("label" ,(org-latex--label src-block info)))
  2606. '(("label" " ")))
  2607. (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
  2608. `(("captionpos" ,(if caption-above-p "t" "b")))
  2609. (cond ((assoc "numbers" lst-opt) nil)
  2610. ((not num-start) '(("numbers" "none")))
  2611. (t `(("firstnumber" ,(number-to-string (1+ num-start)))
  2612. ("numbers" "left"))))))
  2613. (let ((local-options (plist-get attributes :options)))
  2614. (and local-options (concat "," local-options)))))
  2615. ;; Source code.
  2616. (format
  2617. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  2618. (let* ((code-info (org-export-unravel-code src-block))
  2619. (max-width
  2620. (apply 'max
  2621. (mapcar 'length
  2622. (org-split-string (car code-info) "\n")))))
  2623. (org-export-format-code
  2624. (car code-info)
  2625. (lambda (loc _num ref)
  2626. (concat
  2627. loc
  2628. (when ref
  2629. ;; Ensure references are flushed to the right,
  2630. ;; separated with 6 spaces from the widest line of
  2631. ;; code
  2632. (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
  2633. (format "(%s)" ref)))))
  2634. nil (and retain-labels (cdr code-info))))))))))))
  2635. ;;;; Statistics Cookie
  2636. (defun org-latex-statistics-cookie (statistics-cookie _contents _info)
  2637. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  2638. CONTENTS is nil. INFO is a plist holding contextual information."
  2639. (replace-regexp-in-string
  2640. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  2641. ;;;; Strike-Through
  2642. (defun org-latex-strike-through (_strike-through contents info)
  2643. "Transcode STRIKE-THROUGH from Org to LaTeX.
  2644. CONTENTS is the text with strike-through markup. INFO is a plist
  2645. holding contextual information."
  2646. (org-latex--text-markup contents 'strike-through info))
  2647. ;;;; Subscript
  2648. (defun org-latex--script-size (object info)
  2649. "Transcode a subscript or superscript object.
  2650. OBJECT is an Org object. INFO is a plist used as a communication
  2651. channel."
  2652. (let ((output ""))
  2653. (org-element-map (org-element-contents object)
  2654. (cons 'plain-text org-element-all-objects)
  2655. (lambda (obj)
  2656. (cl-case (org-element-type obj)
  2657. ((entity latex-fragment)
  2658. (let ((data (org-trim (org-export-data obj info))))
  2659. (string-match
  2660. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  2661. data)
  2662. (setq output
  2663. (concat output
  2664. (match-string 1 data)
  2665. (let ((blank (org-element-property :post-blank obj)))
  2666. (and blank (> blank 0) "\\ "))))))
  2667. (plain-text
  2668. (setq output
  2669. (format "%s\\text{%s}" output (org-export-data obj info))))
  2670. (otherwise
  2671. (setq output
  2672. (concat output
  2673. (org-export-data obj info)
  2674. (let ((blank (org-element-property :post-blank obj)))
  2675. (and blank (> blank 0) "\\ ")))))))
  2676. info nil org-element-recursive-objects)
  2677. ;; Result. Do not wrap into curly brackets if OUTPUT is a single
  2678. ;; character.
  2679. (concat (if (eq (org-element-type object) 'subscript) "_" "^")
  2680. (and (> (length output) 1) "{")
  2681. output
  2682. (and (> (length output) 1) "}"))))
  2683. (defun org-latex-subscript (subscript _contents info)
  2684. "Transcode a SUBSCRIPT object from Org to LaTeX.
  2685. CONTENTS is the contents of the object. INFO is a plist holding
  2686. contextual information."
  2687. (org-latex--script-size subscript info))
  2688. ;;;; Superscript
  2689. (defun org-latex-superscript (superscript _contents info)
  2690. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  2691. CONTENTS is the contents of the object. INFO is a plist holding
  2692. contextual information."
  2693. (org-latex--script-size superscript info))
  2694. ;;;; Table
  2695. ;;
  2696. ;; `org-latex-table' is the entry point for table transcoding. It
  2697. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  2698. ;; delegates the job to either `org-latex--table.el-table',
  2699. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  2700. ;; depending of the type of the table and the mode requested.
  2701. ;;
  2702. ;; `org-latex--align-string' is a subroutine used to build alignment
  2703. ;; string for Org tables.
  2704. (defun org-latex-table (table contents info)
  2705. "Transcode a TABLE element from Org to LaTeX.
  2706. CONTENTS is the contents of the table. INFO is a plist holding
  2707. contextual information."
  2708. (if (eq (org-element-property :type table) 'table.el)
  2709. ;; "table.el" table. Convert it using appropriate tools.
  2710. (org-latex--table.el-table table info)
  2711. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  2712. (plist-get info :latex-default-table-mode))))
  2713. (cond
  2714. ;; Case 1: Verbatim table.
  2715. ((string= type "verbatim")
  2716. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  2717. ;; Re-create table, without affiliated keywords.
  2718. (org-trim (org-element-interpret-data
  2719. `(table nil ,@(org-element-contents table))))))
  2720. ;; Case 2: Matrix.
  2721. ((or (string= type "math") (string= type "inline-math"))
  2722. (org-latex--math-table table info))
  2723. ;; Case 3: Standard table.
  2724. (t (concat (org-latex--org-table table contents info)
  2725. ;; When there are footnote references within the
  2726. ;; table, insert their definition just after it.
  2727. (org-latex--delayed-footnotes-definitions table info)))))))
  2728. (defun org-latex--align-string (table info)
  2729. "Return an appropriate LaTeX alignment string.
  2730. TABLE is the considered table. INFO is a plist used as
  2731. a communication channel."
  2732. (or (org-export-read-attribute :attr_latex table :align)
  2733. (let (align)
  2734. ;; Extract column groups and alignment from first (non-rule)
  2735. ;; row.
  2736. (org-element-map
  2737. (org-element-map table 'table-row
  2738. (lambda (row)
  2739. (and (eq (org-element-property :type row) 'standard) row))
  2740. info 'first-match)
  2741. 'table-cell
  2742. (lambda (cell)
  2743. (let ((borders (org-export-table-cell-borders cell info)))
  2744. ;; Check left border for the first cell only.
  2745. (when (and (memq 'left borders) (not align))
  2746. (push "|" align))
  2747. (push (cl-case (org-export-table-cell-alignment cell info)
  2748. (left "l")
  2749. (right "r")
  2750. (center "c"))
  2751. align)
  2752. (when (memq 'right borders) (push "|" align))))
  2753. info)
  2754. (apply 'concat (nreverse align)))))
  2755. (defun org-latex--org-table (table contents info)
  2756. "Return appropriate LaTeX code for an Org table.
  2757. TABLE is the table type element to transcode. CONTENTS is its
  2758. contents, as a string. INFO is a plist used as a communication
  2759. channel.
  2760. This function assumes TABLE has `org' as its `:type' property and
  2761. `table' as its `:mode' attribute."
  2762. (let* ((caption (org-latex--caption/label-string table info))
  2763. (attr (org-export-read-attribute :attr_latex table))
  2764. ;; Determine alignment string.
  2765. (alignment (org-latex--align-string table info))
  2766. ;; Determine environment for the table: longtable, tabular...
  2767. (table-env (or (plist-get attr :environment)
  2768. (plist-get info :latex-default-table-environment)))
  2769. ;; If table is a float, determine environment: table, table*
  2770. ;; or sidewaystable.
  2771. (float-env (unless (member table-env '("longtable" "longtabu"))
  2772. (let ((float (plist-get attr :float)))
  2773. (cond
  2774. ((and (not float) (plist-member attr :float)) nil)
  2775. ((or (string= float "sidewaystable")
  2776. (string= float "sideways")) "sidewaystable")
  2777. ((string= float "multicolumn") "table*")
  2778. ((or float
  2779. (org-element-property :caption table)
  2780. (org-string-nw-p (plist-get attr :caption)))
  2781. "table")))))
  2782. ;; Extract others display options.
  2783. (fontsize (let ((font (plist-get attr :font)))
  2784. (and font (concat font "\n"))))
  2785. ;; "tabular" environment doesn't allow to define a width.
  2786. (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
  2787. (spreadp (plist-get attr :spread))
  2788. (placement
  2789. (or (plist-get attr :placement)
  2790. (format "[%s]" (plist-get info :latex-default-figure-position))))
  2791. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2792. (plist-get info :latex-tables-centered)))
  2793. (caption-above-p (org-latex--caption-above-p table info)))
  2794. ;; Prepare the final format string for the table.
  2795. (cond
  2796. ;; Longtable.
  2797. ((equal "longtable" table-env)
  2798. (concat (and fontsize (concat "{" fontsize))
  2799. (format "\\begin{longtable}{%s}\n" alignment)
  2800. (and caption-above-p
  2801. (org-string-nw-p caption)
  2802. (concat caption "\\\\\n"))
  2803. contents
  2804. (and (not caption-above-p)
  2805. (org-string-nw-p caption)
  2806. (concat caption "\\\\\n"))
  2807. "\\end{longtable}\n"
  2808. (and fontsize "}")))
  2809. ;; Longtabu
  2810. ((equal "longtabu" table-env)
  2811. (concat (and fontsize (concat "{" fontsize))
  2812. (format "\\begin{longtabu}%s{%s}\n"
  2813. (if width
  2814. (format " %s %s "
  2815. (if spreadp "spread" "to") width) "")
  2816. alignment)
  2817. (and caption-above-p
  2818. (org-string-nw-p caption)
  2819. (concat caption "\\\\\n"))
  2820. contents
  2821. (and (not caption-above-p)
  2822. (org-string-nw-p caption)
  2823. (concat caption "\\\\\n"))
  2824. "\\end{longtabu}\n"
  2825. (and fontsize "}")))
  2826. ;; Others.
  2827. (t (concat (cond
  2828. (float-env
  2829. (concat (format "\\begin{%s}%s\n" float-env placement)
  2830. (if caption-above-p caption "")
  2831. (when centerp "\\centering\n")
  2832. fontsize))
  2833. ((and (not float-env) caption)
  2834. (concat
  2835. (and centerp "\\begin{center}\n" )
  2836. (if caption-above-p caption "")
  2837. (cond ((and fontsize centerp) fontsize)
  2838. (fontsize (concat "{" fontsize)))))
  2839. (centerp (concat "\\begin{center}\n" fontsize))
  2840. (fontsize (concat "{" fontsize)))
  2841. (cond ((equal "tabu" table-env)
  2842. (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
  2843. (if width (format
  2844. (if spreadp " spread %s " " to %s ")
  2845. width) "")
  2846. alignment
  2847. contents))
  2848. (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2849. table-env
  2850. (if width (format "{%s}" width) "")
  2851. alignment
  2852. contents
  2853. table-env)))
  2854. (cond
  2855. (float-env
  2856. (concat (if caption-above-p "" (concat "\n" caption))
  2857. (format "\n\\end{%s}" float-env)))
  2858. ((and (not float-env) caption)
  2859. (concat
  2860. (if caption-above-p "" (concat "\n" caption))
  2861. (and centerp "\n\\end{center}")
  2862. (and fontsize (not centerp) "}")))
  2863. (centerp "\n\\end{center}")
  2864. (fontsize "}")))))))
  2865. (defun org-latex--table.el-table (table info)
  2866. "Return appropriate LaTeX code for a table.el table.
  2867. TABLE is the table type element to transcode. INFO is a plist
  2868. used as a communication channel.
  2869. This function assumes TABLE has `table.el' as its `:type'
  2870. property."
  2871. (require 'table)
  2872. ;; Ensure "*org-export-table*" buffer is empty.
  2873. (with-current-buffer (get-buffer-create "*org-export-table*")
  2874. (erase-buffer))
  2875. (let ((output (with-temp-buffer
  2876. (insert (org-element-property :value table))
  2877. (goto-char 1)
  2878. (re-search-forward "^[ \t]*|[^|]" nil t)
  2879. (table-generate-source 'latex "*org-export-table*")
  2880. (with-current-buffer "*org-export-table*"
  2881. (org-trim (buffer-string))))))
  2882. (kill-buffer (get-buffer "*org-export-table*"))
  2883. ;; Remove left out comments.
  2884. (while (string-match "^%.*\n" output)
  2885. (setq output (replace-match "" t t output)))
  2886. (let ((attr (org-export-read-attribute :attr_latex table)))
  2887. (when (plist-get attr :rmlines)
  2888. ;; When the "rmlines" attribute is provided, remove all hlines
  2889. ;; but the the one separating heading from the table body.
  2890. (let ((n 0) (pos 0))
  2891. (while (and (< (length output) pos)
  2892. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2893. (cl-incf n)
  2894. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2895. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2896. (plist-get info :latex-tables-centered))))
  2897. (if (not centerp) output
  2898. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2899. (defun org-latex--math-table (table info)
  2900. "Return appropriate LaTeX code for a matrix.
  2901. TABLE is the table type element to transcode. INFO is a plist
  2902. used as a communication channel.
  2903. This function assumes TABLE has `org' as its `:type' property and
  2904. `inline-math' or `math' as its `:mode' attribute."
  2905. (let* ((attr (org-export-read-attribute :attr_latex table))
  2906. (env (or (plist-get attr :environment)
  2907. (plist-get info :latex-default-table-environment)))
  2908. (contents
  2909. (mapconcat
  2910. (lambda (row)
  2911. (if (eq (org-element-property :type row) 'rule) "\\hline"
  2912. ;; Return each cell unmodified.
  2913. (concat
  2914. (mapconcat
  2915. (lambda (cell)
  2916. (substring (org-element-interpret-data cell) 0 -1))
  2917. (org-element-map row 'table-cell #'identity info) "&")
  2918. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2919. "\n")))
  2920. (org-element-map table 'table-row #'identity info) "")))
  2921. (concat
  2922. ;; Prefix.
  2923. (plist-get attr :math-prefix)
  2924. ;; Environment. Also treat special cases.
  2925. (cond ((member env '("array" "tabular"))
  2926. ;; Make sure cells are always centered while preserving
  2927. ;; vertical separators.
  2928. (let ((align (replace-regexp-in-string
  2929. "[lr]" "c" (org-latex--align-string table info))))
  2930. (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
  2931. ((assoc env org-latex-table-matrix-macros)
  2932. (format "\\%s%s{\n%s}"
  2933. env
  2934. (or (plist-get attr :math-arguments) "")
  2935. contents))
  2936. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2937. ;; Suffix.
  2938. (plist-get attr :math-suffix))))
  2939. ;;;; Table Cell
  2940. (defun org-latex-table-cell (table-cell contents info)
  2941. "Transcode a TABLE-CELL element from Org to LaTeX.
  2942. CONTENTS is the cell contents. INFO is a plist used as
  2943. a communication channel."
  2944. (concat
  2945. (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
  2946. (if (and contents
  2947. scientific-format
  2948. (string-match orgtbl-exp-regexp contents))
  2949. ;; Use appropriate format string for scientific
  2950. ;; notation.
  2951. (format scientific-format
  2952. (match-string 1 contents)
  2953. (match-string 2 contents))
  2954. contents))
  2955. (when (org-export-get-next-element table-cell info) " & ")))
  2956. ;;;; Table Row
  2957. (defun org-latex-table-row (table-row contents info)
  2958. "Transcode a TABLE-ROW element from Org to LaTeX.
  2959. CONTENTS is the contents of the row. INFO is a plist used as
  2960. a communication channel."
  2961. (let* ((attr (org-export-read-attribute :attr_latex
  2962. (org-export-get-parent table-row)))
  2963. (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
  2964. (plist-get info :latex-tables-booktabs)))
  2965. (longtablep
  2966. (member (or (plist-get attr :environment)
  2967. (plist-get info :latex-default-table-environment))
  2968. '("longtable" "longtabu"))))
  2969. (if (eq (org-element-property :type table-row) 'rule)
  2970. (cond
  2971. ((not booktabsp) "\\hline")
  2972. ((not (org-export-get-previous-element table-row info)) "\\toprule")
  2973. ((not (org-export-get-next-element table-row info)) "\\bottomrule")
  2974. ((and longtablep
  2975. (org-export-table-row-ends-header-p
  2976. (org-export-get-previous-element table-row info) info))
  2977. "")
  2978. (t "\\midrule"))
  2979. (concat
  2980. ;; When BOOKTABS are activated enforce top-rule even when no
  2981. ;; hline was specifically marked.
  2982. (and booktabsp (not (org-export-get-previous-element table-row info))
  2983. "\\toprule\n")
  2984. contents "\\\\\n"
  2985. (cond
  2986. ;; Special case for long tables. Define header and footers.
  2987. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2988. (let ((columns (cdr (org-export-table-dimensions
  2989. (org-export-get-parent-table table-row) info))))
  2990. (format "%s
  2991. \\endfirsthead
  2992. \\multicolumn{%d}{l}{%s} \\\\
  2993. %s
  2994. %s \\\\\n
  2995. %s
  2996. \\endhead
  2997. %s\\multicolumn{%d}{r}{%s} \\\\
  2998. \\endfoot
  2999. \\endlastfoot"
  3000. (if booktabsp "\\midrule" "\\hline")
  3001. columns
  3002. (org-latex--translate "Continued from previous page" info)
  3003. (cond
  3004. ((not (org-export-table-row-starts-header-p table-row info))
  3005. "")
  3006. (booktabsp "\\toprule\n")
  3007. (t "\\hline\n"))
  3008. contents
  3009. (if booktabsp "\\midrule" "\\hline")
  3010. (if booktabsp "\\midrule" "\\hline")
  3011. columns
  3012. (org-latex--translate "Continued on next page" info))))
  3013. ;; When BOOKTABS are activated enforce bottom rule even when
  3014. ;; no hline was specifically marked.
  3015. ((and booktabsp (not (org-export-get-next-element table-row info)))
  3016. "\\bottomrule"))))))
  3017. ;;;; Target
  3018. (defun org-latex-target (target _contents info)
  3019. "Transcode a TARGET object from Org to LaTeX.
  3020. CONTENTS is nil. INFO is a plist holding contextual
  3021. information."
  3022. (format "\\label{%s}" (org-latex--label target info)))
  3023. ;;;; Timestamp
  3024. (defun org-latex-timestamp (timestamp _contents info)
  3025. "Transcode a TIMESTAMP object from Org to LaTeX.
  3026. CONTENTS is nil. INFO is a plist holding contextual
  3027. information."
  3028. (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
  3029. (format
  3030. (plist-get info
  3031. (cl-case (org-element-property :type timestamp)
  3032. ((active active-range) :latex-active-timestamp-format)
  3033. ((inactive inactive-range) :latex-inactive-timestamp-format)
  3034. (otherwise :latex-diary-timestamp-format)))
  3035. value)))
  3036. ;;;; Underline
  3037. (defun org-latex-underline (_underline contents info)
  3038. "Transcode UNDERLINE from Org to LaTeX.
  3039. CONTENTS is the text with underline markup. INFO is a plist
  3040. holding contextual information."
  3041. (org-latex--text-markup contents 'underline info))
  3042. ;;;; Verbatim
  3043. (defun org-latex-verbatim (verbatim _contents info)
  3044. "Transcode a VERBATIM object from Org to LaTeX.
  3045. CONTENTS is nil. INFO is a plist used as a communication
  3046. channel."
  3047. (org-latex--text-markup
  3048. (org-element-property :value verbatim) 'verbatim info))
  3049. ;;;; Verse Block
  3050. (defun org-latex-verse-block (verse-block contents info)
  3051. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  3052. CONTENTS is verse block contents. INFO is a plist holding
  3053. contextual information."
  3054. (org-latex--wrap-label
  3055. verse-block
  3056. ;; In a verse environment, add a line break to each newline
  3057. ;; character and change each white space at beginning of a line
  3058. ;; into a space of 1 em. Also change each blank line with
  3059. ;; a vertical space of 1 em.
  3060. (format "\\begin{verse}\n%s\\end{verse}"
  3061. (replace-regexp-in-string
  3062. "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
  3063. (replace-regexp-in-string
  3064. "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
  3065. (replace-regexp-in-string
  3066. "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
  3067. contents nil t) nil t) nil t))
  3068. info))
  3069. ;;; End-user functions
  3070. ;;;###autoload
  3071. (defun org-latex-export-as-latex
  3072. (&optional async subtreep visible-only body-only ext-plist)
  3073. "Export current buffer as a LaTeX buffer.
  3074. If narrowing is active in the current buffer, only export its
  3075. narrowed part.
  3076. If a region is active, export that region.
  3077. A non-nil optional argument ASYNC means the process should happen
  3078. asynchronously. The resulting buffer should be accessible
  3079. through the `org-export-stack' interface.
  3080. When optional argument SUBTREEP is non-nil, export the sub-tree
  3081. at point, extracting information from the headline properties
  3082. first.
  3083. When optional argument VISIBLE-ONLY is non-nil, don't export
  3084. contents of hidden elements.
  3085. When optional argument BODY-ONLY is non-nil, only write code
  3086. between \"\\begin{document}\" and \"\\end{document}\".
  3087. EXT-PLIST, when provided, is a property list with external
  3088. parameters overriding Org default settings, but still inferior to
  3089. file-local settings.
  3090. Export is done in a buffer named \"*Org LATEX Export*\", which
  3091. will be displayed when `org-export-show-temporary-export-buffer'
  3092. is non-nil."
  3093. (interactive)
  3094. (org-export-to-buffer 'latex "*Org LATEX Export*"
  3095. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  3096. ;;;###autoload
  3097. (defun org-latex-convert-region-to-latex ()
  3098. "Assume the current region has Org syntax, and convert it to LaTeX.
  3099. This can be used in any buffer. For example, you can write an
  3100. itemized list in Org syntax in an LaTeX buffer and use this
  3101. command to convert it."
  3102. (interactive)
  3103. (org-export-replace-region-by 'latex))
  3104. ;;;###autoload
  3105. (defun org-latex-export-to-latex
  3106. (&optional async subtreep visible-only body-only ext-plist)
  3107. "Export current buffer to a LaTeX file.
  3108. If narrowing is active in the current buffer, only export its
  3109. narrowed part.
  3110. If a region is active, export that region.
  3111. A non-nil optional argument ASYNC means the process should happen
  3112. asynchronously. The resulting file should be accessible through
  3113. the `org-export-stack' interface.
  3114. When optional argument SUBTREEP is non-nil, export the sub-tree
  3115. at point, extracting information from the headline properties
  3116. first.
  3117. When optional argument VISIBLE-ONLY is non-nil, don't export
  3118. contents of hidden elements.
  3119. When optional argument BODY-ONLY is non-nil, only write code
  3120. between \"\\begin{document}\" and \"\\end{document}\".
  3121. EXT-PLIST, when provided, is a property list with external
  3122. parameters overriding Org default settings, but still inferior to
  3123. file-local settings."
  3124. (interactive)
  3125. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3126. (org-export-to-file 'latex outfile
  3127. async subtreep visible-only body-only ext-plist)))
  3128. ;;;###autoload
  3129. (defun org-latex-export-to-pdf
  3130. (&optional async subtreep visible-only body-only ext-plist)
  3131. "Export current buffer to LaTeX then process through to PDF.
  3132. If narrowing is active in the current buffer, only export its
  3133. narrowed part.
  3134. If a region is active, export that region.
  3135. A non-nil optional argument ASYNC means the process should happen
  3136. asynchronously. The resulting file should be accessible through
  3137. the `org-export-stack' interface.
  3138. When optional argument SUBTREEP is non-nil, export the sub-tree
  3139. at point, extracting information from the headline properties
  3140. first.
  3141. When optional argument VISIBLE-ONLY is non-nil, don't export
  3142. contents of hidden elements.
  3143. When optional argument BODY-ONLY is non-nil, only write code
  3144. between \"\\begin{document}\" and \"\\end{document}\".
  3145. EXT-PLIST, when provided, is a property list with external
  3146. parameters overriding Org default settings, but still inferior to
  3147. file-local settings.
  3148. Return PDF file's name."
  3149. (interactive)
  3150. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3151. (org-export-to-file 'latex outfile
  3152. async subtreep visible-only body-only ext-plist
  3153. (lambda (file) (org-latex-compile file)))))
  3154. (defun org-latex-compile (texfile &optional snippet)
  3155. "Compile a TeX file.
  3156. TEXFILE is the name of the file being compiled. Processing is
  3157. done through the command specified in `org-latex-pdf-process',
  3158. which see. Output is redirected to \"*Org PDF LaTeX Output*\"
  3159. buffer.
  3160. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  3161. file used to preview a LaTeX snippet. In this case, do not
  3162. create a log buffer and do not remove log files.
  3163. Return PDF file name or raise an error if it couldn't be
  3164. produced."
  3165. (unless snippet (message "Processing LaTeX file %s..." texfile))
  3166. (let* ((compiler
  3167. (or (with-temp-buffer
  3168. (save-excursion (insert-file-contents texfile))
  3169. (and (search-forward-regexp (regexp-opt org-latex-compilers)
  3170. (line-end-position 2)
  3171. t)
  3172. (progn (beginning-of-line) (looking-at-p "%"))
  3173. (match-string 0)))
  3174. "pdflatex"))
  3175. (process (if (functionp org-latex-pdf-process) org-latex-pdf-process
  3176. ;; Replace "%latex" and "%bibtex" with,
  3177. ;; respectively, "%L" and "%B" so as to adhere to
  3178. ;; `format-spec' specifications.
  3179. (mapcar (lambda (command)
  3180. (replace-regexp-in-string
  3181. "%\\(?:bib\\|la\\)tex\\>"
  3182. (lambda (m) (upcase (substring m 0 2)))
  3183. command))
  3184. org-latex-pdf-process)))
  3185. (spec `((?B . ,(shell-quote-argument org-latex-bib-compiler))
  3186. (?L . ,(shell-quote-argument compiler))))
  3187. (log-buf-name "*Org PDF LaTeX Output*")
  3188. (log-buf (and (not snippet) (get-buffer-create log-buf-name)))
  3189. (outfile (org-compile-file texfile process "pdf"
  3190. (format "See %S for details" log-buf-name)
  3191. log-buf spec)))
  3192. (unless snippet
  3193. (when org-latex-remove-logfiles
  3194. (mapc #'delete-file
  3195. (directory-files
  3196. (file-name-directory outfile)
  3197. t
  3198. (concat (regexp-quote (file-name-base outfile))
  3199. "\\(?:\\.[0-9]+\\)?\\."
  3200. (regexp-opt org-latex-logfiles-extensions))
  3201. t)))
  3202. (let ((warnings (org-latex--collect-warnings log-buf)))
  3203. (message (concat "PDF file produced"
  3204. (cond
  3205. ((eq warnings 'error) " with errors.")
  3206. (warnings (concat " with warnings: " warnings))
  3207. (t "."))))))
  3208. ;; Return output file name.
  3209. outfile))
  3210. (defun org-latex--collect-warnings (buffer)
  3211. "Collect some warnings from \"pdflatex\" command output.
  3212. BUFFER is the buffer containing output. Return collected
  3213. warnings types as a string, `error' if a LaTeX error was
  3214. encountered or nil if there was none."
  3215. (with-current-buffer buffer
  3216. (save-excursion
  3217. (goto-char (point-max))
  3218. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  3219. (if (re-search-forward "^!" nil t) 'error
  3220. (let ((case-fold-search t)
  3221. (warnings ""))
  3222. (dolist (warning org-latex-known-warnings)
  3223. (when (save-excursion (re-search-forward (car warning) nil t))
  3224. (setq warnings (concat warnings " " (cdr warning)))))
  3225. (org-string-nw-p (org-trim warnings))))))))
  3226. ;;;###autoload
  3227. (defun org-latex-publish-to-latex (plist filename pub-dir)
  3228. "Publish an Org file to LaTeX.
  3229. FILENAME is the filename of the Org file to be published. PLIST
  3230. is the property list for the given project. PUB-DIR is the
  3231. publishing directory.
  3232. Return output file name."
  3233. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  3234. ;;;###autoload
  3235. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  3236. "Publish an Org file to PDF (via LaTeX).
  3237. FILENAME is the filename of the Org file to be published. PLIST
  3238. is the property list for the given project. PUB-DIR is the
  3239. publishing directory.
  3240. Return output file name."
  3241. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  3242. ;; in working directory and then moved to publishing directory.
  3243. (org-publish-attachment
  3244. plist
  3245. (org-latex-compile
  3246. (org-publish-org-to
  3247. 'latex filename ".tex" plist (file-name-directory filename)))
  3248. pub-dir))
  3249. (provide 'ox-latex)
  3250. ;; Local variables:
  3251. ;; generated-autoload-file: "org-loaddefs.el"
  3252. ;; End:
  3253. ;;; ox-latex.el ends here