ox-latex.el 126 KB

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