ox-latex.el 126 KB

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