ox-latex.el 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607
  1. ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
  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
  711. (lambda (name contents) contents)
  712. "Function called to format a drawer in LaTeX code.
  713. The function must accept two parameters:
  714. NAME the drawer name, like \"LOGBOOK\"
  715. CONTENTS the contents of the drawer.
  716. The function should return the string to be exported.
  717. The default function simply returns the value of CONTENTS."
  718. :group 'org-export-latex
  719. :version "24.4"
  720. :package-version '(Org . "8.3")
  721. :type 'function)
  722. ;;;; Inlinetasks
  723. (defcustom org-latex-format-inlinetask-function
  724. 'org-latex-format-inlinetask-default-function
  725. "Function called to format an inlinetask in LaTeX code.
  726. The function must accept seven parameters:
  727. TODO the todo keyword (string or nil)
  728. TODO-TYPE the todo type (symbol: `todo', `done', nil)
  729. PRIORITY the inlinetask priority (integer or nil)
  730. NAME the inlinetask name (string)
  731. TAGS the inlinetask tags (list of strings or nil)
  732. CONTENTS the contents of the inlinetask (string or nil)
  733. INFO the export options (plist)
  734. The function should return the string to be exported."
  735. :group 'org-export-latex
  736. :type 'function
  737. :version "25.1"
  738. :package-version '(Org . "8.3"))
  739. ;; Src blocks
  740. (defcustom org-latex-listings nil
  741. "Non-nil means export source code using the listings package.
  742. This package will fontify source code, possibly even with color.
  743. If you want to use this, you also need to make LaTeX use the
  744. listings package, and if you want to have color, the color
  745. package. Just add these to `org-latex-packages-alist', for
  746. example using customize, or with something like:
  747. (require \\='ox-latex)
  748. (add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
  749. (add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
  750. Alternatively,
  751. (setq org-latex-listings \\='minted)
  752. causes source code to be exported using the minted package as
  753. opposed to listings. If you want to use minted, you need to add
  754. the minted package to `org-latex-packages-alist', for example
  755. using customize, or with
  756. (require \\='ox-latex)
  757. (add-to-list \\='org-latex-packages-alist \\='(\"newfloat\" \"minted\"))
  758. In addition, it is necessary to install pygments
  759. \(http://pygments.org), and to configure the variable
  760. `org-latex-pdf-process' so that the -shell-escape option is
  761. passed to pdflatex.
  762. The minted choice has possible repercussions on the preview of
  763. latex fragments (see `org-preview-latex-fragment'). If you run
  764. into previewing problems, please consult
  765. http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
  766. :group 'org-export-latex
  767. :type '(choice
  768. (const :tag "Use listings" t)
  769. (const :tag "Use minted" minted)
  770. (const :tag "Export verbatim" nil))
  771. :safe (lambda (s) (memq s '(t nil minted))))
  772. (defcustom org-latex-listings-langs
  773. '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
  774. (c "C") (cc "C++")
  775. (fortran "fortran")
  776. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  777. (html "HTML") (xml "XML")
  778. (tex "TeX") (latex "[LaTeX]TeX")
  779. (shell-script "bash")
  780. (gnuplot "Gnuplot")
  781. (ocaml "Caml") (caml "Caml")
  782. (sql "SQL") (sqlite "sql")
  783. (makefile "make"))
  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 program format-string."
  877. :group 'org-export-latex
  878. :type '(choice
  879. (const :tag "Comment" "%% Indented LaTeX compiler: %s\n")
  880. (const :tag "latex-mode file variable" "%% -*- latex-run-command: %s -*-\n")
  881. (const :tag "AUCTeX file variable" "%% -*- LaTeX-command: %s -*-\n")
  882. (string :tag "custom format" "%% %s"))
  883. :version "25.1"
  884. :package-version '(Org . "9.0"))
  885. (defcustom org-latex-compiler "pdflatex"
  886. "LaTeX program to use. Must be an element in `org-latex-compilers'."
  887. :group 'org-export-latex
  888. :type '(choice
  889. (const :tag "pdfLaTeX" "pdflatex")
  890. (const :tag "XeLaTeX" "xelatex")
  891. (const :tag "LuaLaTeX" "lualatex"))
  892. :version "25.1"
  893. :package-version '(Org . "9.0"))
  894. (defconst org-latex-compilers '("pdflatex" "xelatex" "lualatex")
  895. "Known LaTeX programs.")
  896. (defcustom org-latex-bib-compiler "bibtex"
  897. "Command used to process a LaTeX files bibliography.
  898. The shorthand %bib in `org-latex-pdf-process' is replaced with
  899. this value.
  900. A better approach is to use a compiler suit such as `latexmk'.
  901. "
  902. :group 'org-export-latex
  903. :type '(choice (const :tag "BibTeX" "bibtex")
  904. (const :tag "Biber" "biber")
  905. (string :tag "Other process"))
  906. :version "25.1"
  907. :package-version '(Org . "9.0"))
  908. (defcustom org-latex-pdf-process
  909. '("%latex -interaction nonstopmode -output-directory %o %f"
  910. "%latex -interaction nonstopmode -output-directory %o %f"
  911. "%latex -interaction nonstopmode -output-directory %o %f")
  912. "Commands to process a LaTeX file to a PDF file.
  913. This is a list of strings, each of them will be given to the
  914. shell as a command. %f in the command will be replaced by the
  915. full file name, %b by the file base name (i.e. without directory
  916. and extension parts), %o by the base directory of the file,
  917. %latex is the LaTeX compiler (see `org-latex-compiler'), and %bib
  918. is the BibTeX-like compiler (see `org-latex-bib-compiler').
  919. The reason why this is a list is that it usually takes several
  920. runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
  921. does not have a clever mechanism to detect which of these
  922. commands have to be run to get to a stable result, and it also
  923. does not do any error checking.
  924. Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
  925. which calls the \"correct\" combinations of auxiliary programs.
  926. Alternatively, this may be a Lisp function that does the
  927. processing, so you could use this to apply the machinery of
  928. AUCTeX or the Emacs LaTeX mode. This function should accept the
  929. file name as its single argument."
  930. :group 'org-export-pdf
  931. :type '(choice
  932. (repeat :tag "Shell command sequence"
  933. (string :tag "Shell command"))
  934. (const :tag "2 runs of latex"
  935. ("%latex -interaction nonstopmode -output-directory %o %f"
  936. "%latex -interaction nonstopmode -output-directory %o %f"))
  937. (const :tag "3 runs of latex"
  938. ("%latex -interaction nonstopmode -output-directory %o %f"
  939. "%latex -interaction nonstopmode -output-directory %o %f"
  940. "%latex -interaction nonstopmode -output-directory %o %f"))
  941. (const :tag "latex,bibtex,latex,latex"
  942. ("%latex -interaction nonstopmode -output-directory %o %f"
  943. "%bib %b"
  944. "%latex -interaction nonstopmode -output-directory %o %f"
  945. "%latex -interaction nonstopmode -output-directory %o %f"))
  946. (const :tag "texi2dvi"
  947. ("LATEX=\"%latex\" texi2dvi -p -b -V %f"))
  948. (const :tag "latexmk"
  949. ("latexmk -g -pdflatex=\"%latex\" %f"))
  950. (function)))
  951. (defcustom org-latex-logfiles-extensions
  952. '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
  953. "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
  954. "The list of file extensions to consider as LaTeX logfiles.
  955. The logfiles will be removed if `org-latex-remove-logfiles' is
  956. non-nil."
  957. :group 'org-export-latex
  958. :version "25.1"
  959. :package-version '(Org . "8.3")
  960. :type '(repeat (string :tag "Extension")))
  961. (defcustom org-latex-remove-logfiles t
  962. "Non-nil means remove the logfiles produced by PDF production.
  963. By default, logfiles are files with these extensions: .aux, .idx,
  964. .log, .out, .toc, .nav, .snm and .vrb. To define the set of
  965. logfiles to remove, set `org-latex-logfiles-extensions'."
  966. :group 'org-export-latex
  967. :type 'boolean)
  968. (defcustom org-latex-known-warnings
  969. '(("Reference.*?undefined" . "[undefined reference]")
  970. ("Runaway argument" . "[runaway argument]")
  971. ("Underfull \\hbox" . "[underfull hbox]")
  972. ("Overfull \\hbox" . "[overfull hbox]")
  973. ("Citation.*?undefined" . "[undefined citation]")
  974. ("Undefined control sequence" . "[undefined control sequence]"))
  975. "Alist of regular expressions and associated messages for the user.
  976. The regular expressions are used to find possible warnings in the
  977. log of a latex-run. These warnings will be reported after
  978. calling `org-latex-compile'."
  979. :group 'org-export-latex
  980. :version "25.1"
  981. :package-version '(Org . "8.3")
  982. :type '(repeat
  983. (cons
  984. (string :tag "Regexp")
  985. (string :tag "Message"))))
  986. ;;; Internal Functions
  987. (defun org-latex--caption-above-p (element info)
  988. "Non nil when caption is expected to be located above ELEMENT.
  989. INFO is a plist holding contextual information."
  990. (let ((above (plist-get info :latex-caption-above)))
  991. (if (symbolp above) above
  992. (let ((type (org-element-type element)))
  993. (memq (if (eq type 'link) 'image type) above)))))
  994. (defun org-latex--label (datum info &optional force full)
  995. "Return an appropriate label for DATUM.
  996. DATUM is an element or a `target' type object. INFO is the
  997. current export state, as a plist.
  998. Return nil if element DATUM has no NAME or VALUE affiliated
  999. keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
  1000. this case always return a unique label.
  1001. Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
  1002. (let* ((type (org-element-type datum))
  1003. (user-label
  1004. (org-element-property
  1005. (case type
  1006. ((headline inlinetask) :CUSTOM_ID)
  1007. (target :value)
  1008. (otherwise :name))
  1009. datum))
  1010. (label
  1011. (and (or user-label force)
  1012. (if (and user-label (plist-get info :latex-prefer-user-labels))
  1013. user-label
  1014. (concat (case type
  1015. (headline "sec:")
  1016. (table "tab:")
  1017. (latex-environment
  1018. (and (org-string-match-p
  1019. org-latex-math-environments-re
  1020. (org-element-property :value datum))
  1021. "eq:"))
  1022. (paragraph
  1023. (and (org-element-property :caption datum)
  1024. "fig:")))
  1025. (org-export-get-reference datum info))))))
  1026. (cond ((not full) label)
  1027. (label (format "\\label{%s}%s"
  1028. label
  1029. (if (eq type 'target) "" "\n")))
  1030. (t ""))))
  1031. (defun org-latex--caption/label-string (element info)
  1032. "Return caption and label LaTeX string for ELEMENT.
  1033. INFO is a plist holding contextual information. If there's no
  1034. caption nor label, return the empty string.
  1035. For non-floats, see `org-latex--wrap-label'."
  1036. (let* ((label (org-latex--label element info nil t))
  1037. (main (org-export-get-caption element))
  1038. (attr (org-export-read-attribute :attr_latex element))
  1039. (type (org-element-type element))
  1040. (nonfloat (or (and (plist-member attr :float)
  1041. (not (plist-get attr :float))
  1042. main)
  1043. (and (eq type 'src-block)
  1044. (not (plist-get attr :float))
  1045. (memq (plist-get info :latex-listings)
  1046. '(nil minted)))))
  1047. (short (org-export-get-caption element t))
  1048. (caption-from-attr-latex (plist-get attr :caption)))
  1049. (cond
  1050. ((org-string-nw-p caption-from-attr-latex)
  1051. (concat caption-from-attr-latex "\n"))
  1052. ((and (not main) (equal label "")) "")
  1053. ((not main) (concat label "\n"))
  1054. ;; Option caption format with short name.
  1055. (t
  1056. (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
  1057. "\\caption%s%s{%s%s}\n")
  1058. (if nonfloat
  1059. (case type
  1060. (paragraph "figure")
  1061. (src-block (if (plist-get info :latex-listings)
  1062. "listing"
  1063. "figure"))
  1064. (t (symbol-name type)))
  1065. "")
  1066. (if short (format "[%s]" (org-export-data short info)) "")
  1067. label
  1068. (org-export-data main info))))))
  1069. (defun org-latex-guess-inputenc (header)
  1070. "Set the coding system in inputenc to what the buffer is.
  1071. HEADER is the LaTeX header string. This function only applies
  1072. when specified inputenc option is \"AUTO\".
  1073. Return the new header, as a string."
  1074. (let* ((cs (or (ignore-errors
  1075. (latexenc-coding-system-to-inputenc
  1076. (or org-export-coding-system buffer-file-coding-system)))
  1077. "utf8")))
  1078. (if (not cs) header
  1079. ;; First translate if that is requested.
  1080. (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
  1081. ;; Then find the \usepackage statement and replace the option.
  1082. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  1083. cs header t nil 1))))
  1084. (defun org-latex-guess-babel-language (header info)
  1085. "Set Babel's language according to LANGUAGE keyword.
  1086. HEADER is the LaTeX header string. INFO is the plist used as
  1087. a communication channel.
  1088. Insertion of guessed language only happens when Babel package has
  1089. explicitly been loaded. Then it is added to the rest of
  1090. package's options.
  1091. The argument to Babel may be \"AUTO\" which is then replaced with
  1092. the language of the document or `org-export-default-language'
  1093. unless language in question is already loaded.
  1094. Return the new header."
  1095. (let ((language-code (plist-get info :language)))
  1096. ;; If no language is set or Babel package is not loaded, return
  1097. ;; HEADER as-is.
  1098. (if (or (not (stringp language-code))
  1099. (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
  1100. header
  1101. (let ((options (save-match-data
  1102. (org-split-string (match-string 1 header) ",[ \t]*")))
  1103. (language (cdr (assoc language-code
  1104. org-latex-babel-language-alist))))
  1105. ;; If LANGUAGE is already loaded, return header without AUTO.
  1106. ;; Otherwise, replace AUTO with language or append language if
  1107. ;; AUTO is not present.
  1108. (replace-match
  1109. (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
  1110. (cond ((member language options) (delete "AUTO" options))
  1111. ((member "AUTO" options) options)
  1112. (t (append options (list language))))
  1113. ", ")
  1114. t nil header 1)))))
  1115. (defun org-latex-guess-polyglossia-language (header info)
  1116. "Set the Polyglossia language according to the LANGUAGE keyword.
  1117. HEADER is the LaTeX header string. INFO is the plist used as
  1118. a communication channel.
  1119. Insertion of guessed language only happens when the Polyglossia
  1120. package has been explicitly loaded.
  1121. The argument to Polyglossia may be \"AUTO\" which is then
  1122. replaced with the language of the document or
  1123. `org-export-default-language'. Note, the language is really set
  1124. using \setdefaultlanguage and not as an option to the package.
  1125. Return the new header."
  1126. (let ((language (plist-get info :language)))
  1127. ;; If no language is set or Polyglossia is not loaded, return
  1128. ;; HEADER as-is.
  1129. (if (or (not (stringp language))
  1130. (not (string-match
  1131. "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
  1132. header)))
  1133. header
  1134. (let* ((options (org-string-nw-p (match-string 1 header)))
  1135. (languages (and options
  1136. ;; Reverse as the last loaded language is
  1137. ;; the main language.
  1138. (nreverse
  1139. (delete-dups
  1140. (save-match-data
  1141. (org-split-string
  1142. (replace-regexp-in-string
  1143. "AUTO" language options t)
  1144. ",[ \t]*"))))))
  1145. (main-language-set
  1146. (string-match-p "\\\\setmainlanguage{.*?}" header)))
  1147. (replace-match
  1148. (concat "\\usepackage{polyglossia}\n"
  1149. (mapconcat
  1150. (lambda (l)
  1151. (let ((l (or (assoc l org-latex-polyglossia-language-alist)
  1152. l)))
  1153. (format (if main-language-set "\\setotherlanguage%s{%s}\n"
  1154. (setq main-language-set t)
  1155. "\\setmainlanguage%s{%s}\n")
  1156. (if (and (consp l) (= (length l) 3))
  1157. (format "[variant=%s]" (nth 2 l))
  1158. "")
  1159. (nth 1 l))))
  1160. languages
  1161. ""))
  1162. t t header 0)))))
  1163. (defun org-latex--remove-packages (pkg-alist info)
  1164. "Remove packages based on the current LaTeX program.
  1165. If the fourth argument of an element is set in pkg-alist, and it
  1166. is not a member of the LaTeX program of the document, the packages
  1167. is removed. See also `org-latex-compiler'.
  1168. Return modified pkg-alist."
  1169. (let ((compiler (or (plist-get info :latex-compiler) "")))
  1170. (if (member-ignore-case compiler org-latex-compilers)
  1171. (delq nil
  1172. (mapcar
  1173. (lambda (pkg)
  1174. (unless (and
  1175. (listp pkg)
  1176. (let ((third (nth 3 pkg)))
  1177. (and third
  1178. (not (member-ignore-case
  1179. compiler
  1180. (if (listp third) third (list third)))))))
  1181. pkg))
  1182. pkg-alist))
  1183. pkg-alist)))
  1184. (defun org-latex--find-verb-separator (s)
  1185. "Return a character not used in string S.
  1186. This is used to choose a separator for constructs like \\verb."
  1187. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1188. (loop for c across ll
  1189. when (not (string-match (regexp-quote (char-to-string c)) s))
  1190. return (char-to-string c))))
  1191. (defun org-latex--make-option-string (options)
  1192. "Return a comma separated string of keywords and values.
  1193. OPTIONS is an alist where the key is the options keyword as
  1194. a string, and the value a list containing the keyword value, or
  1195. nil."
  1196. (mapconcat (lambda (pair)
  1197. (concat (first pair)
  1198. (when (> (length (second pair)) 0)
  1199. (concat "=" (second pair)))))
  1200. options
  1201. ","))
  1202. (defun org-latex--wrap-label (element output info)
  1203. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  1204. INFO is the current export state, as a plist. This function
  1205. should not be used for floats. See
  1206. `org-latex--caption/label-string'."
  1207. (if (not (and (org-string-nw-p output) (org-element-property :name element)))
  1208. output
  1209. (concat (format "\\phantomsection\n\\label{%s}\n"
  1210. (org-latex--label element info))
  1211. output)))
  1212. (defun org-latex--protect-text (text)
  1213. "Protect special characters in string TEXT and return it."
  1214. (replace-regexp-in-string
  1215. "--\\|[\\{}$%&_#~^]"
  1216. (lambda (m)
  1217. (cond ((equal m "--") "-{}-")
  1218. ((equal m "\\") "\\textbackslash{}")
  1219. ((equal m "~") "\\textasciitilde{}")
  1220. ((equal m "^") "\\textasciicircum{}")
  1221. (t (concat "\\" m))))
  1222. text nil t))
  1223. (defun org-latex--text-markup (text markup info)
  1224. "Format TEXT depending on MARKUP text markup.
  1225. INFO is a plist used as a communication channel. See
  1226. `org-latex-text-markup-alist' for details."
  1227. (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
  1228. (case fmt
  1229. ;; No format string: Return raw text.
  1230. ((nil) text)
  1231. ;; Handle the `verb' special case: Find an appropriate separator
  1232. ;; and use "\\verb" command.
  1233. (verb
  1234. (let ((separator (org-latex--find-verb-separator text)))
  1235. (concat "\\verb" separator
  1236. (replace-regexp-in-string "\n" " " text)
  1237. separator)))
  1238. ;; Handle the `protectedtexttt' special case: Protect some
  1239. ;; special chars and use "\texttt{%s}" format string.
  1240. (protectedtexttt
  1241. (format "\\texttt{%s}" (org-latex--protect-text text)))
  1242. ;; Else use format string.
  1243. (t (format fmt text)))))
  1244. (defun org-latex--delayed-footnotes-definitions (element info)
  1245. "Return footnotes definitions in ELEMENT as a string.
  1246. INFO is a plist used as a communication channel.
  1247. Footnotes definitions are returned within \"\\footnotetxt{}\"
  1248. commands.
  1249. This function is used within constructs that don't support
  1250. \"\\footnote{}\" command (i.e. an item's tag). In that case,
  1251. \"\\footnotemark\" is used within the construct and the function
  1252. just outside of it."
  1253. (mapconcat
  1254. (lambda (ref)
  1255. (format
  1256. "\\footnotetext[%s]{%s}"
  1257. (org-export-get-footnote-number ref info)
  1258. (org-trim
  1259. (org-export-data
  1260. (org-export-get-footnote-definition ref info) info))))
  1261. ;; Find every footnote reference in ELEMENT.
  1262. (let* (all-refs
  1263. search-refs ; For byte-compiler.
  1264. (search-refs
  1265. (function
  1266. (lambda (data)
  1267. ;; Return a list of all footnote references never seen
  1268. ;; before in DATA.
  1269. (org-element-map data 'footnote-reference
  1270. (lambda (ref)
  1271. (when (org-export-footnote-first-reference-p ref info)
  1272. (push ref all-refs)
  1273. (when (eq (org-element-property :type ref) 'standard)
  1274. (funcall search-refs
  1275. (org-export-get-footnote-definition ref info)))))
  1276. info)
  1277. (reverse all-refs)))))
  1278. (funcall search-refs element))
  1279. ""))
  1280. (defun org-latex--translate (s info)
  1281. "Translate string S according to specified language.
  1282. INFO is a plist used as a communication channel."
  1283. (org-export-translate s :latex info))
  1284. (defun org-latex--format-spec (info)
  1285. "Create a format-spec for document meta-data.
  1286. INFO is a plist used as a communication channel."
  1287. (let ((language (let ((lang (plist-get info :language)))
  1288. (or (cdr (assoc lang org-latex-babel-language-alist))
  1289. lang))))
  1290. `((?a . ,(org-export-data (plist-get info :author) info))
  1291. (?t . ,(org-export-data (plist-get info :title) info))
  1292. (?k . ,(org-export-data (org-latex--wrap-latex-math-block
  1293. (plist-get info :keywords) info)
  1294. info))
  1295. (?d . ,(org-export-data (org-latex--wrap-latex-math-block
  1296. (plist-get info :description) info)
  1297. info))
  1298. (?c . ,(plist-get info :creator))
  1299. (?l . ,language)
  1300. (?L . ,(capitalize language))
  1301. (?D . ,(org-export-get-date info)))))
  1302. (defun org-latex--make-header (info)
  1303. "Return a formatted LaTeX header.
  1304. INFO is a plist used as a communication channel."
  1305. (let* ((class (plist-get info :latex-class))
  1306. (class-options (plist-get info :latex-class-options))
  1307. (header (nth 1 (assoc class (plist-get info :latex-classes))))
  1308. (document-class-string
  1309. (and (stringp header)
  1310. (if (not class-options) header
  1311. (replace-regexp-in-string
  1312. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  1313. class-options header t nil 1)))))
  1314. (if (not document-class-string)
  1315. (user-error "Unknown LaTeX class `%s'" class)
  1316. (org-latex-guess-polyglossia-language
  1317. (org-latex-guess-babel-language
  1318. (org-latex-guess-inputenc
  1319. (org-element-normalize-string
  1320. (org-splice-latex-header
  1321. document-class-string
  1322. (org-latex--remove-packages org-latex-default-packages-alist info)
  1323. (org-latex--remove-packages org-latex-packages-alist info)
  1324. nil
  1325. (concat (org-element-normalize-string
  1326. (plist-get info :latex-header))
  1327. (plist-get info :latex-header-extra)))))
  1328. info)
  1329. info))))
  1330. ;;; Template
  1331. (defun org-latex-template (contents info)
  1332. "Return complete document string after LaTeX conversion.
  1333. CONTENTS is the transcoded contents string. INFO is a plist
  1334. holding export options."
  1335. (let ((title (org-export-data (plist-get info :title) info))
  1336. (spec (org-latex--format-spec info)))
  1337. (concat
  1338. ;; Time-stamp.
  1339. (and (plist-get info :time-stamp-file)
  1340. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  1341. ;; LaTeX program.
  1342. (let ((compiler (plist-get info :latex-compiler)))
  1343. (and (org-string-nw-p org-latex-compiler-file-string)
  1344. (string-match-p (regexp-opt org-latex-compilers) (or compiler ""))
  1345. (format org-latex-compiler-file-string compiler)))
  1346. ;; Document class and packages.
  1347. (org-latex--make-header info)
  1348. ;; Possibly limit depth for headline numbering.
  1349. (let ((sec-num (plist-get info :section-numbers)))
  1350. (when (integerp sec-num)
  1351. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  1352. ;; Author.
  1353. (let ((author (and (plist-get info :with-author)
  1354. (let ((auth (plist-get info :author)))
  1355. (and auth (org-export-data auth info)))))
  1356. (email (and (plist-get info :with-email)
  1357. (org-export-data (plist-get info :email) info))))
  1358. (cond ((and author email (not (string= "" email)))
  1359. (format "\\author{%s\\thanks{%s}}\n" author email))
  1360. ((or author email) (format "\\author{%s}\n" (or author email)))))
  1361. ;; Date.
  1362. (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
  1363. (format "\\date{%s}\n" (org-export-data date info)))
  1364. ;; Title and subtitle.
  1365. (let* ((subtitle (plist-get info :subtitle))
  1366. (formatted-subtitle
  1367. (when subtitle
  1368. (format (plist-get info :latex-subtitle-format)
  1369. (org-export-data subtitle info))))
  1370. (separate (plist-get info :latex-subtitle-separate)))
  1371. (concat
  1372. (format "\\title{%s%s}\n" title
  1373. (if separate "" (or formatted-subtitle "")))
  1374. (when (and separate subtitle)
  1375. (concat formatted-subtitle "\n"))))
  1376. ;; Hyperref options.
  1377. (let ((template (plist-get info :latex-hyperref-template)))
  1378. (and (stringp template)
  1379. (format-spec template spec)))
  1380. ;; Document start.
  1381. "\\begin{document}\n\n"
  1382. ;; Title command.
  1383. (let* ((title-command (plist-get info :latex-title-command))
  1384. (command (and (stringp title-command)
  1385. (format-spec title-command spec))))
  1386. (org-element-normalize-string
  1387. (cond ((not (plist-get info :with-title)) nil)
  1388. ((string= "" title) nil)
  1389. ((not (stringp command)) nil)
  1390. ((string-match "\\(?:[^%]\\|^\\)%s" command)
  1391. (format command title))
  1392. (t command))))
  1393. ;; Table of contents.
  1394. (let ((depth (plist-get info :with-toc)))
  1395. (when depth
  1396. (concat (when (wholenump depth)
  1397. (format "\\setcounter{tocdepth}{%d}\n" depth))
  1398. (plist-get info :latex-toc-command))))
  1399. ;; Document's body.
  1400. contents
  1401. ;; Creator.
  1402. (and (plist-get info :with-creator)
  1403. (concat (plist-get info :creator) "\n"))
  1404. ;; Document end.
  1405. "\\end{document}")))
  1406. ;;; Transcode Functions
  1407. ;;;; Bold
  1408. (defun org-latex-bold (bold contents info)
  1409. "Transcode BOLD from Org to LaTeX.
  1410. CONTENTS is the text with bold markup. INFO is a plist holding
  1411. contextual information."
  1412. (org-latex--text-markup contents 'bold info))
  1413. ;;;; Center Block
  1414. (defun org-latex-center-block (center-block contents info)
  1415. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  1416. CONTENTS holds the contents of the center block. INFO is a plist
  1417. holding contextual information."
  1418. (org-latex--wrap-label
  1419. center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
  1420. ;;;; Clock
  1421. (defun org-latex-clock (clock contents info)
  1422. "Transcode a CLOCK element from Org to LaTeX.
  1423. CONTENTS is nil. INFO is a plist holding contextual
  1424. information."
  1425. (concat
  1426. "\\noindent"
  1427. (format "\\textbf{%s} " org-clock-string)
  1428. (format (plist-get info :latex-inactive-timestamp-format)
  1429. (concat (org-timestamp-translate (org-element-property :value clock))
  1430. (let ((time (org-element-property :duration clock)))
  1431. (and time (format " (%s)" time)))))
  1432. "\\\\"))
  1433. ;;;; Code
  1434. (defun org-latex-code (code contents info)
  1435. "Transcode a CODE object from Org to LaTeX.
  1436. CONTENTS is nil. INFO is a plist used as a communication
  1437. channel."
  1438. (org-latex--text-markup (org-element-property :value code) 'code info))
  1439. ;;;; Drawer
  1440. (defun org-latex-drawer (drawer contents info)
  1441. "Transcode a DRAWER element from Org to LaTeX.
  1442. CONTENTS holds the contents of the block. INFO is a plist
  1443. holding contextual information."
  1444. (let* ((name (org-element-property :drawer-name drawer))
  1445. (output (funcall (plist-get info :latex-format-drawer-function)
  1446. name contents)))
  1447. (org-latex--wrap-label drawer output info)))
  1448. ;;;; Dynamic Block
  1449. (defun org-latex-dynamic-block (dynamic-block contents info)
  1450. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1451. CONTENTS holds the contents of the block. INFO is a plist
  1452. holding contextual information. See `org-export-data'."
  1453. (org-latex--wrap-label dynamic-block contents info))
  1454. ;;;; Entity
  1455. (defun org-latex-entity (entity contents info)
  1456. "Transcode an ENTITY object from Org to LaTeX.
  1457. CONTENTS are the definition itself. INFO is a plist holding
  1458. contextual information."
  1459. (org-element-property :latex entity))
  1460. ;;;; Example Block
  1461. (defun org-latex-example-block (example-block contents info)
  1462. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1463. CONTENTS is nil. INFO is a plist holding contextual
  1464. information."
  1465. (when (org-string-nw-p (org-element-property :value example-block))
  1466. (let ((environment (or (org-export-read-attribute
  1467. :attr_latex example-block :environment)
  1468. "verbatim")))
  1469. (org-latex--wrap-label
  1470. example-block
  1471. (format "\\begin{%s}\n%s\\end{%s}"
  1472. environment
  1473. (org-export-format-code-default example-block info)
  1474. environment)
  1475. info))))
  1476. ;;;; Export Block
  1477. (defun org-latex-export-block (export-block contents info)
  1478. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1479. CONTENTS is nil. INFO is a plist holding contextual information."
  1480. (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
  1481. (org-remove-indentation (org-element-property :value export-block))))
  1482. ;;;; Export Snippet
  1483. (defun org-latex-export-snippet (export-snippet contents info)
  1484. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1485. CONTENTS is nil. INFO is a plist holding contextual information."
  1486. (when (eq (org-export-snippet-backend export-snippet) 'latex)
  1487. (org-element-property :value export-snippet)))
  1488. ;;;; Fixed Width
  1489. (defun org-latex-fixed-width (fixed-width contents info)
  1490. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1491. CONTENTS is nil. INFO is a plist holding contextual information."
  1492. (org-latex--wrap-label
  1493. fixed-width
  1494. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1495. (org-remove-indentation
  1496. (org-element-property :value fixed-width)))
  1497. info))
  1498. ;;;; Footnote Reference
  1499. (defun org-latex-footnote-reference (footnote-reference contents info)
  1500. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1501. CONTENTS is nil. INFO is a plist holding contextual information."
  1502. (concat
  1503. ;; Insert separator between two footnotes in a row.
  1504. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1505. (when (eq (org-element-type prev) 'footnote-reference)
  1506. (plist-get info :latex-footnote-separator)))
  1507. (cond
  1508. ;; Use \footnotemark if the footnote has already been defined.
  1509. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1510. (format "\\footnotemark[%s]{}"
  1511. (org-export-get-footnote-number footnote-reference info)))
  1512. ;; Use \footnotemark if reference is within another footnote
  1513. ;; reference, footnote definition or table cell.
  1514. ((org-element-lineage footnote-reference
  1515. '(footnote-reference footnote-definition table-cell))
  1516. "\\footnotemark")
  1517. ;; Otherwise, define it with \footnote command.
  1518. (t
  1519. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1520. (concat
  1521. (format "\\footnote{%s}" (org-trim (org-export-data def info)))
  1522. ;; Retrieve all footnote references within the footnote and
  1523. ;; add their definition after it, since LaTeX doesn't support
  1524. ;; them inside.
  1525. (org-latex--delayed-footnotes-definitions def info)))))))
  1526. ;;;; Headline
  1527. (defun org-latex-headline (headline contents info)
  1528. "Transcode a HEADLINE element from Org to LaTeX.
  1529. CONTENTS holds the contents of the headline. INFO is a plist
  1530. holding contextual information."
  1531. (unless (org-element-property :footnote-section-p headline)
  1532. (let* ((class (plist-get info :latex-class))
  1533. (level (org-export-get-relative-level headline info))
  1534. (numberedp (org-export-numbered-headline-p headline info))
  1535. (class-sectioning (assoc class (plist-get info :latex-classes)))
  1536. ;; Section formatting will set two placeholders: one for
  1537. ;; the title and the other for the contents.
  1538. (section-fmt
  1539. (let ((sec (if (functionp (nth 2 class-sectioning))
  1540. (funcall (nth 2 class-sectioning) level numberedp)
  1541. (nth (1+ level) class-sectioning))))
  1542. (cond
  1543. ;; No section available for that LEVEL.
  1544. ((not sec) nil)
  1545. ;; Section format directly returned by a function. Add
  1546. ;; placeholder for contents.
  1547. ((stringp sec) (concat sec "\n%s"))
  1548. ;; (numbered-section . unnumbered-section)
  1549. ((not (consp (cdr sec)))
  1550. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1551. ;; (numbered-open numbered-close)
  1552. ((= (length sec) 2)
  1553. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1554. ;; (num-in num-out no-num-in no-num-out)
  1555. ((= (length sec) 4)
  1556. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1557. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1558. ;; Create a temporary export back-end that hard-codes
  1559. ;; "\underline" within "\section" and alike.
  1560. (section-back-end
  1561. (org-export-create-backend
  1562. :parent 'latex
  1563. :transcoders
  1564. '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
  1565. (text
  1566. (org-export-data-with-backend
  1567. (org-element-property :title headline) section-back-end info))
  1568. (todo
  1569. (and (plist-get info :with-todo-keywords)
  1570. (let ((todo (org-element-property :todo-keyword headline)))
  1571. (and todo (org-export-data todo info)))))
  1572. (todo-type (and todo (org-element-property :todo-type headline)))
  1573. (tags (and (plist-get info :with-tags)
  1574. (org-export-get-tags headline info)))
  1575. (priority (and (plist-get info :with-priority)
  1576. (org-element-property :priority headline)))
  1577. ;; Create the headline text along with a no-tag version.
  1578. ;; The latter is required to remove tags from toc.
  1579. (full-text (funcall (plist-get info :latex-format-headline-function)
  1580. todo todo-type priority text tags info))
  1581. ;; Associate \label to the headline for internal links.
  1582. (headline-label (org-latex--label headline info t t))
  1583. (pre-blanks
  1584. (make-string (org-element-property :pre-blank headline) ?\n)))
  1585. (if (or (not section-fmt) (org-export-low-level-p headline info))
  1586. ;; This is a deep sub-tree: export it as a list item. Also
  1587. ;; export as items headlines for which no section format has
  1588. ;; been found.
  1589. (let ((low-level-body
  1590. (concat
  1591. ;; If headline is the first sibling, start a list.
  1592. (when (org-export-first-sibling-p headline info)
  1593. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1594. ;; Itemize headline
  1595. "\\item"
  1596. (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
  1597. "\\relax")
  1598. " " full-text "\n"
  1599. headline-label
  1600. pre-blanks
  1601. contents)))
  1602. ;; If headline is not the last sibling simply return
  1603. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
  1604. ;; any blank line.
  1605. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1606. (replace-regexp-in-string
  1607. "[ \t\n]*\\'"
  1608. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1609. low-level-body)))
  1610. ;; This is a standard headline. Export it as a section. Add
  1611. ;; an alternative heading when possible, and when this is not
  1612. ;; identical to the usual heading.
  1613. (let ((opt-title
  1614. (funcall (plist-get info :latex-format-headline-function)
  1615. todo todo-type priority
  1616. (org-export-data-with-backend
  1617. (org-export-get-alt-title headline info)
  1618. section-back-end info)
  1619. (and (eq (plist-get info :with-tags) t) tags)
  1620. info))
  1621. ;; Maybe end local TOC (see `org-latex-keyword').
  1622. (contents
  1623. (concat
  1624. contents
  1625. (let ((case-fold-search t)
  1626. (section
  1627. (let ((first (car (org-element-contents headline))))
  1628. (and (eq (org-element-type first) 'section) first))))
  1629. (org-element-map section 'keyword
  1630. (lambda (k)
  1631. (and (equal (org-element-property :key k) "TOC")
  1632. (let ((v (org-element-property :value k)))
  1633. (and (org-string-match-p "\\<headlines\\>" v)
  1634. (org-string-match-p "\\<local\\>" v)
  1635. (format "\\stopcontents[level-%d]" level)))))
  1636. info t)))))
  1637. (if (and opt-title
  1638. (not (equal opt-title full-text))
  1639. (string-match "\\`\\\\\\(.+?\\){" section-fmt))
  1640. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  1641. ;; Replace square brackets with parenthesis
  1642. ;; since square brackets are not supported in
  1643. ;; optional arguments.
  1644. (replace-regexp-in-string
  1645. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  1646. full-text
  1647. (concat headline-label pre-blanks contents))
  1648. ;; Impossible to add an alternative heading. Fallback to
  1649. ;; regular sectioning format string.
  1650. (format section-fmt full-text
  1651. (concat headline-label pre-blanks contents))))))))
  1652. (defun org-latex-format-headline-default-function
  1653. (todo todo-type priority text tags info)
  1654. "Default format function for a headline.
  1655. See `org-latex-format-headline-function' for details."
  1656. (concat
  1657. (and todo (format "{\\bfseries\\sffamily %s} " todo))
  1658. (and priority (format "\\framebox{\\#%c} " priority))
  1659. text
  1660. (and tags
  1661. (format "\\hfill{}\\textsc{%s}"
  1662. (mapconcat (lambda (tag) (org-latex-plain-text tag info))
  1663. tags ":")))))
  1664. ;;;; Horizontal Rule
  1665. (defun org-latex-horizontal-rule (horizontal-rule contents info)
  1666. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1667. CONTENTS is nil. INFO is a plist holding contextual information."
  1668. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1669. (prev (org-export-get-previous-element horizontal-rule info)))
  1670. (concat
  1671. ;; Make sure the rule doesn't start at the end of the current
  1672. ;; line by separating it with a blank line from previous element.
  1673. (when (and prev
  1674. (let ((prev-blank (org-element-property :post-blank prev)))
  1675. (or (not prev-blank) (zerop prev-blank))))
  1676. "\n")
  1677. (org-latex--wrap-label
  1678. horizontal-rule
  1679. (format "\\rule{%s}{%s}"
  1680. (or (plist-get attr :width) "\\linewidth")
  1681. (or (plist-get attr :thickness) "0.5pt"))
  1682. info))))
  1683. ;;;; Inline Src Block
  1684. (defun org-latex-inline-src-block (inline-src-block contents info)
  1685. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1686. CONTENTS holds the contents of the item. INFO is a plist holding
  1687. contextual information."
  1688. (let* ((code (org-element-property :value inline-src-block))
  1689. (separator (org-latex--find-verb-separator code)))
  1690. (case (plist-get info :latex-listings)
  1691. ;; Do not use a special package: transcode it verbatim.
  1692. ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
  1693. ;; Use minted package.
  1694. (minted
  1695. (let* ((org-lang (org-element-property :language inline-src-block))
  1696. (mint-lang (or (cadr (assq (intern org-lang)
  1697. (plist-get info :latex-minted-langs)))
  1698. (downcase org-lang)))
  1699. (options (org-latex--make-option-string
  1700. (plist-get info :latex-minted-options))))
  1701. (concat (format "\\mint%s{%s}"
  1702. (if (string= options "") "" (format "[%s]" options))
  1703. mint-lang)
  1704. separator code separator)))
  1705. ;; Use listings package.
  1706. (otherwise
  1707. ;; Maybe translate language's name.
  1708. (let* ((org-lang (org-element-property :language inline-src-block))
  1709. (lst-lang (or (cadr (assq (intern org-lang)
  1710. (plist-get info :latex-listings-langs)))
  1711. org-lang))
  1712. (options (org-latex--make-option-string
  1713. (append (plist-get info :latex-listings-options)
  1714. `(("language" ,lst-lang))))))
  1715. (concat (format "\\lstinline[%s]" options)
  1716. separator code separator))))))
  1717. ;;;; Inlinetask
  1718. (defun org-latex-inlinetask (inlinetask contents info)
  1719. "Transcode an INLINETASK element from Org to LaTeX.
  1720. CONTENTS holds the contents of the block. INFO is a plist
  1721. holding contextual information."
  1722. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1723. (todo (and (plist-get info :with-todo-keywords)
  1724. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1725. (and todo (org-export-data todo info)))))
  1726. (todo-type (org-element-property :todo-type inlinetask))
  1727. (tags (and (plist-get info :with-tags)
  1728. (org-export-get-tags inlinetask info)))
  1729. (priority (and (plist-get info :with-priority)
  1730. (org-element-property :priority inlinetask)))
  1731. (contents (concat (org-latex--label inlinetask info) contents)))
  1732. (funcall (plist-get info :latex-format-inlinetask-function)
  1733. todo todo-type priority title tags contents info)))
  1734. (defun org-latex-format-inlinetask-default-function
  1735. (todo todo-type priority title tags contents info)
  1736. "Default format function for a inlinetasks.
  1737. See `org-latex-format-inlinetask-function' for details."
  1738. (let ((full-title
  1739. (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1740. (when priority (format "\\framebox{\\#%c} " priority))
  1741. title
  1742. (when tags
  1743. (format "\\hfill{}\\textsc{:%s:}"
  1744. (mapconcat
  1745. (lambda (tag) (org-latex-plain-text tag info))
  1746. tags ":"))))))
  1747. (concat "\\begin{center}\n"
  1748. "\\fbox{\n"
  1749. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1750. full-title "\n\n"
  1751. (and (org-string-nw-p contents)
  1752. (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
  1753. "\\end{minipage}\n"
  1754. "}\n"
  1755. "\\end{center}")))
  1756. ;;;; Italic
  1757. (defun org-latex-italic (italic contents info)
  1758. "Transcode ITALIC from Org to LaTeX.
  1759. CONTENTS is the text with italic markup. INFO is a plist holding
  1760. contextual information."
  1761. (org-latex--text-markup contents 'italic info))
  1762. ;;;; Item
  1763. (defun org-latex-item (item contents info)
  1764. "Transcode an ITEM element from Org to LaTeX.
  1765. CONTENTS holds the contents of the item. INFO is a plist holding
  1766. contextual information."
  1767. (let* ((counter
  1768. (let ((count (org-element-property :counter item))
  1769. (level
  1770. ;; Determine level of current item to determine the
  1771. ;; correct LaTeX counter to use (enumi, enumii...).
  1772. (let ((parent item) (level 0))
  1773. (while (memq (org-element-type
  1774. (setq parent (org-export-get-parent parent)))
  1775. '(plain-list item))
  1776. (when (and (eq (org-element-type parent) 'plain-list)
  1777. (eq (org-element-property :type parent)
  1778. 'ordered))
  1779. (incf level)))
  1780. level)))
  1781. (and count
  1782. (< level 5)
  1783. (format "\\setcounter{enum%s}{%s}\n"
  1784. (nth (1- level) '("i" "ii" "iii" "iv"))
  1785. (1- count)))))
  1786. (checkbox (case (org-element-property :checkbox item)
  1787. (on "$\\boxtimes$ ")
  1788. (off "$\\square$ ")
  1789. (trans "$\\boxminus$ ")))
  1790. (tag (let ((tag (org-element-property :tag item)))
  1791. ;; Check-boxes must belong to the tag.
  1792. (and tag (format "[{%s}] "
  1793. (concat checkbox
  1794. (org-export-data tag info)))))))
  1795. (concat counter
  1796. "\\item"
  1797. (cond
  1798. (tag)
  1799. (checkbox (concat " " checkbox))
  1800. ;; Without a tag or a check-box, if CONTENTS starts with
  1801. ;; an opening square bracket, add "\relax" to "\item",
  1802. ;; unless the brackets comes from an initial export
  1803. ;; snippet (i.e. it is inserted willingly by the user).
  1804. ((and contents
  1805. (org-string-match-p "\\`[ \t]*\\[" contents)
  1806. (not (let ((e (car (org-element-contents item))))
  1807. (and (eq (org-element-type e) 'paragraph)
  1808. (let ((o (car (org-element-contents e))))
  1809. (and (eq (org-element-type o) 'export-snippet)
  1810. (eq (org-export-snippet-backend o)
  1811. 'latex)))))))
  1812. "\\relax ")
  1813. (t " "))
  1814. (and contents (org-trim contents))
  1815. ;; If there are footnotes references in tag, be sure to
  1816. ;; add their definition at the end of the item. This
  1817. ;; workaround is necessary since "\footnote{}" command is
  1818. ;; not supported in tags.
  1819. (and tag
  1820. (org-latex--delayed-footnotes-definitions
  1821. (org-element-property :tag item) info)))))
  1822. ;;;; Keyword
  1823. (defun org-latex-keyword (keyword contents info)
  1824. "Transcode a KEYWORD element from Org to LaTeX.
  1825. CONTENTS is nil. INFO is a plist holding contextual information."
  1826. (let ((key (org-element-property :key keyword))
  1827. (value (org-element-property :value keyword)))
  1828. (cond
  1829. ((string= key "LATEX") value)
  1830. ((string= key "INDEX") (format "\\index{%s}" value))
  1831. ((string= key "TOC")
  1832. (let ((case-fold-search t))
  1833. (cond
  1834. ((org-string-match-p "\\<headlines\\>" value)
  1835. (let* ((localp (org-string-match-p "\\<local\\>" value))
  1836. (parent (org-element-lineage keyword '(headline)))
  1837. (level (if (not (and localp parent)) 0
  1838. (org-export-get-relative-level parent info)))
  1839. (depth
  1840. (and (string-match "\\<[0-9]+\\>" value)
  1841. (format
  1842. "\\setcounter{tocdepth}{%d}"
  1843. (+ (string-to-number (match-string 0 value)) level)))))
  1844. (if (and localp parent)
  1845. ;; Start local TOC, assuming package "titletoc" is
  1846. ;; required.
  1847. (format "\\startcontents[level-%d]
  1848. \\printcontents[level-%d]{}{0}{%s}"
  1849. level level (or depth ""))
  1850. (concat depth (and depth "\n") "\\tableofcontents"))))
  1851. ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
  1852. ((org-string-match-p "\\<listings\\>" value)
  1853. (case (plist-get info :latex-listings)
  1854. ((nil) "\\listoffigures")
  1855. (minted "\\listoflistings")
  1856. (otherwise "\\lstlistoflistings")))))))))
  1857. ;;;; Latex Environment
  1858. (defun org-latex-latex-environment (latex-environment contents info)
  1859. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1860. CONTENTS is nil. INFO is a plist holding contextual information."
  1861. (when (plist-get info :with-latex)
  1862. (let ((value (org-remove-indentation
  1863. (org-element-property :value latex-environment))))
  1864. (if (not (org-element-property :name latex-environment)) value
  1865. ;; Environment is labeled: label must be within the environment
  1866. ;; (otherwise, a reference pointing to that element will count
  1867. ;; the section instead).
  1868. (with-temp-buffer
  1869. (insert value)
  1870. (goto-char (point-min))
  1871. (forward-line)
  1872. (insert (org-latex--label latex-environment info nil t))
  1873. (buffer-string))))))
  1874. ;;;; Latex Fragment
  1875. (defun org-latex-latex-fragment (latex-fragment contents info)
  1876. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1877. CONTENTS is nil. INFO is a plist holding contextual information."
  1878. (let ((value (org-element-property :value latex-fragment)))
  1879. ;; Trim math markers since the fragment is enclosed within
  1880. ;; a latex-math-block object anyway.
  1881. (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
  1882. (match-string 2 value))
  1883. ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
  1884. (match-string 1 value))
  1885. (t value))))
  1886. ;;;; Line Break
  1887. (defun org-latex-line-break (line-break contents info)
  1888. "Transcode a LINE-BREAK object from Org to LaTeX.
  1889. CONTENTS is nil. INFO is a plist holding contextual information."
  1890. "\\\\\n")
  1891. ;;;; Link
  1892. (defun org-latex--inline-image (link info)
  1893. "Return LaTeX code for an inline image.
  1894. LINK is the link pointing to the inline image. INFO is a plist
  1895. used as a communication channel."
  1896. (let* ((parent (org-export-get-parent-element link))
  1897. (path (let ((raw-path (org-element-property :path link)))
  1898. (if (not (file-name-absolute-p raw-path)) raw-path
  1899. (expand-file-name raw-path))))
  1900. (filetype (file-name-extension path))
  1901. (caption (org-latex--caption/label-string parent info))
  1902. (caption-above-p (org-latex--caption-above-p link info))
  1903. ;; Retrieve latex attributes from the element around.
  1904. (attr (org-export-read-attribute :attr_latex parent))
  1905. (float (let ((float (plist-get attr :float)))
  1906. (cond ((string= float "wrap") 'wrap)
  1907. ((string= float "sideways") 'sideways)
  1908. ((string= float "multicolumn") 'multicolumn)
  1909. ((or float
  1910. (org-element-property :caption parent)
  1911. (org-string-nw-p (plist-get attr :caption)))
  1912. (if (and (plist-member attr :float) (not float))
  1913. 'nonfloat
  1914. 'figure))
  1915. ((and (not float) (plist-member attr :float)) nil))))
  1916. (placement
  1917. (let ((place (plist-get attr :placement)))
  1918. (cond
  1919. (place (format "%s" place))
  1920. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  1921. ((eq float 'figure)
  1922. (format "[%s]" (plist-get info :latex-default-figure-position)))
  1923. (t ""))))
  1924. (comment-include (if (plist-get attr :comment-include) "%" ""))
  1925. ;; It is possible to specify width and height in the
  1926. ;; ATTR_LATEX line, and also via default variables.
  1927. (width (cond ((plist-get attr :width))
  1928. ((plist-get attr :height) "")
  1929. ((eq float 'wrap) "0.48\\textwidth")
  1930. (t (plist-get info :latex-image-default-width))))
  1931. (height (cond ((plist-get attr :height))
  1932. ((or (plist-get attr :width)
  1933. (memq float '(figure wrap))) "")
  1934. (t (plist-get info :latex-image-default-height))))
  1935. (options (let ((opt (or (plist-get attr :options)
  1936. (plist-get info :latex-image-default-option))))
  1937. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  1938. (match-string 1 opt))))
  1939. image-code)
  1940. (if (member filetype '("tikz" "pgf"))
  1941. ;; For tikz images:
  1942. ;; - use \input to read in image file.
  1943. ;; - if options are present, wrap in a tikzpicture environment.
  1944. ;; - if width or height are present, use \resizebox to change
  1945. ;; the image size.
  1946. (progn
  1947. (setq image-code (format "\\input{%s}" path))
  1948. (when (org-string-nw-p options)
  1949. (setq image-code
  1950. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  1951. options
  1952. image-code)))
  1953. (when (or (org-string-nw-p width) (org-string-nw-p height))
  1954. (setq image-code (format "\\resizebox{%s}{%s}{%s}"
  1955. (if (org-string-nw-p width) width "!")
  1956. (if (org-string-nw-p height) height "!")
  1957. image-code))))
  1958. ;; For other images:
  1959. ;; - add width and height to options.
  1960. ;; - include the image with \includegraphics.
  1961. (when (org-string-nw-p width)
  1962. (setq options (concat options ",width=" width)))
  1963. (when (org-string-nw-p height)
  1964. (setq options (concat options ",height=" height)))
  1965. (let ((search-option (org-element-property :search-option link)))
  1966. (when (and search-option
  1967. (equal filetype "pdf")
  1968. (org-string-match-p "\\`[0-9]+\\'" search-option)
  1969. (not (org-string-match-p "page=" options)))
  1970. (setq options (concat options ",page=" search-option))))
  1971. (setq image-code
  1972. (format "\\includegraphics%s{%s}"
  1973. (cond ((not (org-string-nw-p options)) "")
  1974. ((= (aref options 0) ?,)
  1975. (format "[%s]"(substring options 1)))
  1976. (t (format "[%s]" options)))
  1977. path))
  1978. (when (equal filetype "svg")
  1979. (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
  1980. "\\includesvg"
  1981. image-code
  1982. nil t))
  1983. (setq image-code (replace-regexp-in-string "\\.svg}"
  1984. "}"
  1985. image-code
  1986. nil t))))
  1987. ;; Return proper string, depending on FLOAT.
  1988. (case float
  1989. (wrap (format "\\begin{wrapfigure}%s
  1990. %s\\centering
  1991. %s%s
  1992. %s\\end{wrapfigure}"
  1993. placement
  1994. (if caption-above-p caption "")
  1995. comment-include image-code
  1996. (if caption-above-p "" caption)))
  1997. (sideways (format "\\begin{sidewaysfigure}
  1998. %s\\centering
  1999. %s%s
  2000. %s\\end{sidewaysfigure}"
  2001. (if caption-above-p caption "")
  2002. comment-include image-code
  2003. (if caption-above-p "" caption)))
  2004. (multicolumn (format "\\begin{figure*}%s
  2005. %s\\centering
  2006. %s%s
  2007. %s\\end{figure*}"
  2008. placement
  2009. (if caption-above-p caption "")
  2010. comment-include image-code
  2011. (if caption-above-p "" caption)))
  2012. (figure (format "\\begin{figure}%s
  2013. %s\\centering
  2014. %s%s
  2015. %s\\end{figure}"
  2016. placement
  2017. (if caption-above-p caption "")
  2018. comment-include image-code
  2019. (if caption-above-p "" caption)))
  2020. (nonfloat
  2021. (format "\\begin{center}
  2022. %s%s
  2023. %s\\end{center}"
  2024. (if caption-above-p caption "")
  2025. image-code
  2026. (if caption-above-p "" caption)))
  2027. (otherwise image-code))))
  2028. (defun org-latex-link (link desc info)
  2029. "Transcode a LINK object from Org to LaTeX.
  2030. DESC is the description part of the link, or the empty string.
  2031. INFO is a plist holding contextual information. See
  2032. `org-export-data'."
  2033. (let* ((type (org-element-property :type link))
  2034. (raw-path (replace-regexp-in-string
  2035. "%" "\\%" (org-element-property :path link) nil t))
  2036. ;; Ensure DESC really exists, or set it to nil.
  2037. (desc (and (not (string= desc "")) desc))
  2038. (imagep (org-export-inline-image-p
  2039. link (plist-get info :latex-inline-image-rules)))
  2040. (path (cond
  2041. ((member type '("http" "https" "ftp" "mailto" "doi"))
  2042. (concat type ":" raw-path))
  2043. ((string= type "file") (org-export-file-uri raw-path))
  2044. (t raw-path))))
  2045. (cond
  2046. ;; Link type is handled by a special function.
  2047. ((org-export-custom-protocol-maybe link desc 'latex))
  2048. ;; Image file.
  2049. (imagep (org-latex--inline-image link info))
  2050. ;; Radio link: Transcode target's contents and use them as link's
  2051. ;; description.
  2052. ((string= type "radio")
  2053. (let ((destination (org-export-resolve-radio-link link info)))
  2054. (if (not destination) desc
  2055. (format "\\hyperref[%s]{%s}"
  2056. (org-export-get-reference destination info)
  2057. desc))))
  2058. ;; Links pointing to a headline: Find destination and build
  2059. ;; appropriate referencing command.
  2060. ((member type '("custom-id" "fuzzy" "id"))
  2061. (let ((destination (if (string= type "fuzzy")
  2062. (org-export-resolve-fuzzy-link link info)
  2063. (org-export-resolve-id-link link info))))
  2064. (case (org-element-type destination)
  2065. ;; Id link points to an external file.
  2066. (plain-text
  2067. (if desc (format "\\href{%s}{%s}" destination desc)
  2068. (format "\\url{%s}" destination)))
  2069. ;; Fuzzy link points nowhere.
  2070. ((nil)
  2071. (format (plist-get info :latex-link-with-unknown-path-format)
  2072. (or desc
  2073. (org-export-data
  2074. (org-element-property :raw-link link) info))))
  2075. ;; LINK points to a headline. If headlines are numbered
  2076. ;; and the link has no description, display headline's
  2077. ;; number. Otherwise, display description or headline's
  2078. ;; title.
  2079. (headline
  2080. (let ((label (org-latex--label destination info t)))
  2081. (if (and (not desc)
  2082. (org-export-numbered-headline-p destination info))
  2083. (format "\\ref{%s}" label)
  2084. (format "\\hyperref[%s]{%s}" label
  2085. (or desc
  2086. (org-export-data
  2087. (org-element-property :title destination) info))))))
  2088. ;; Fuzzy link points to a target. Do as above.
  2089. (otherwise
  2090. (let ((ref (org-latex--label destination info t)))
  2091. (if (not desc) (format "\\ref{%s}" ref)
  2092. (format "\\hyperref[%s]{%s}" ref desc)))))))
  2093. ;; Coderef: replace link with the reference name or the
  2094. ;; equivalent line number.
  2095. ((string= type "coderef")
  2096. (format (org-export-get-coderef-format path desc)
  2097. (org-export-resolve-coderef path info)))
  2098. ;; External link with a description part.
  2099. ((and path desc) (format "\\href{%s}{%s}" path desc))
  2100. ;; External link without a description part.
  2101. (path (format "\\url{%s}" path))
  2102. ;; No path, only description. Try to do something useful.
  2103. (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
  2104. ;;;; Node Property
  2105. (defun org-latex-node-property (node-property contents info)
  2106. "Transcode a NODE-PROPERTY element from Org to LaTeX.
  2107. CONTENTS is nil. INFO is a plist holding contextual
  2108. information."
  2109. (format "%s:%s"
  2110. (org-element-property :key node-property)
  2111. (let ((value (org-element-property :value node-property)))
  2112. (if value (concat " " value) ""))))
  2113. ;;;; Paragraph
  2114. (defun org-latex-paragraph (paragraph contents info)
  2115. "Transcode a PARAGRAPH element from Org to LaTeX.
  2116. CONTENTS is the contents of the paragraph, as a string. INFO is
  2117. the plist used as a communication channel."
  2118. contents)
  2119. ;;;; Plain List
  2120. (defun org-latex-plain-list (plain-list contents info)
  2121. "Transcode a PLAIN-LIST element from Org to LaTeX.
  2122. CONTENTS is the contents of the list. INFO is a plist holding
  2123. contextual information."
  2124. (let* ((type (org-element-property :type plain-list))
  2125. (attr (org-export-read-attribute :attr_latex plain-list))
  2126. (latex-type (let ((env (plist-get attr :environment)))
  2127. (cond (env (format "%s" env))
  2128. ((eq type 'ordered) "enumerate")
  2129. ((eq type 'descriptive) "description")
  2130. (t "itemize")))))
  2131. (org-latex--wrap-label
  2132. plain-list
  2133. (format "\\begin{%s}%s\n%s\\end{%s}"
  2134. latex-type
  2135. (or (plist-get attr :options) "")
  2136. contents
  2137. latex-type)
  2138. info)))
  2139. ;;;; Plain Text
  2140. (defun org-latex-plain-text (text info)
  2141. "Transcode a TEXT string from Org to LaTeX.
  2142. TEXT is the string to transcode. INFO is a plist holding
  2143. contextual information."
  2144. (let* ((specialp (plist-get info :with-special-strings))
  2145. (output
  2146. ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
  2147. (let ((case-fold-search nil))
  2148. (replace-regexp-in-string
  2149. "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
  2150. ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
  2151. ;; However, if special strings are used, be careful not
  2152. ;; to protect "\" in "\-" constructs.
  2153. (replace-regexp-in-string
  2154. (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
  2155. (lambda (m)
  2156. (case (string-to-char m)
  2157. (?\\ "$\\\\backslash$\\1")
  2158. (?~ "\\\\textasciitilde{}")
  2159. (?^ "\\\\^{}")
  2160. (t "\\\\\\&")))
  2161. text)))))
  2162. ;; Activate smart quotes. Be sure to provide original TEXT string
  2163. ;; since OUTPUT may have been modified.
  2164. (when (plist-get info :with-smart-quotes)
  2165. (setq output (org-export-activate-smart-quotes output :latex info text)))
  2166. ;; Convert special strings.
  2167. (when specialp
  2168. (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
  2169. ;; Handle break preservation if required.
  2170. (when (plist-get info :preserve-breaks)
  2171. (setq output (replace-regexp-in-string
  2172. "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
  2173. ;; Return value.
  2174. output))
  2175. ;;;; Planning
  2176. (defun org-latex-planning (planning contents info)
  2177. "Transcode a PLANNING element from Org to LaTeX.
  2178. CONTENTS is nil. INFO is a plist holding contextual
  2179. information."
  2180. (concat
  2181. "\\noindent"
  2182. (mapconcat
  2183. 'identity
  2184. (delq nil
  2185. (list
  2186. (let ((closed (org-element-property :closed planning)))
  2187. (when closed
  2188. (concat
  2189. (format "\\textbf{%s} " org-closed-string)
  2190. (format (plist-get info :latex-inactive-timestamp-format)
  2191. (org-timestamp-translate closed)))))
  2192. (let ((deadline (org-element-property :deadline planning)))
  2193. (when deadline
  2194. (concat
  2195. (format "\\textbf{%s} " org-deadline-string)
  2196. (format (plist-get info :latex-active-timestamp-format)
  2197. (org-timestamp-translate deadline)))))
  2198. (let ((scheduled (org-element-property :scheduled planning)))
  2199. (when scheduled
  2200. (concat
  2201. (format "\\textbf{%s} " org-scheduled-string)
  2202. (format (plist-get info :latex-active-timestamp-format)
  2203. (org-timestamp-translate scheduled)))))))
  2204. " ")
  2205. "\\\\"))
  2206. ;;;; Property Drawer
  2207. (defun org-latex-property-drawer (property-drawer contents info)
  2208. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  2209. CONTENTS holds the contents of the drawer. INFO is a plist
  2210. holding contextual information."
  2211. (and (org-string-nw-p contents)
  2212. (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
  2213. ;;;; Pseudo Element: LaTeX Matrices
  2214. ;; `latex-matrices' elements have the following properties:
  2215. ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
  2216. ;; `math').
  2217. (defun org-latex--wrap-latex-matrices (data info)
  2218. "Merge contiguous tables with the same mode within a pseudo-element.
  2219. DATA is a parse tree or a secondary string. INFO is a plist
  2220. containing export options. Modify DATA by side-effect and return
  2221. it."
  2222. (org-element-map data 'table
  2223. (lambda (table)
  2224. (when (eq (org-element-property :type table) 'org)
  2225. (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
  2226. (plist-get info :latex-default-table-mode))))
  2227. (when (and (member mode '("inline-math" "math"))
  2228. ;; Do not wrap twice the same table.
  2229. (not (eq (org-element-type
  2230. (org-element-property :parent table))
  2231. 'latex-matrices)))
  2232. (let* ((caption (and (not (string= mode "inline-math"))
  2233. (org-element-property :caption table)))
  2234. (matrices
  2235. (list 'latex-matrices
  2236. (list :caption caption
  2237. :markup
  2238. (cond ((string= mode "inline-math") 'inline)
  2239. (caption 'equation)
  2240. (t 'math)))))
  2241. (previous table)
  2242. (next (org-export-get-next-element table info)))
  2243. (org-element-insert-before matrices table)
  2244. ;; Swallow all contiguous tables sharing the same mode.
  2245. (while (and
  2246. (zerop (or (org-element-property :post-blank previous) 0))
  2247. (setq next (org-export-get-next-element previous info))
  2248. (eq (org-element-type next) 'table)
  2249. (eq (org-element-property :type next) 'org)
  2250. (string= (or (org-export-read-attribute
  2251. :attr_latex next :mode)
  2252. (plist-get info :latex-default-table-mode))
  2253. mode))
  2254. (org-element-extract-element previous)
  2255. (org-element-adopt-elements matrices previous)
  2256. (setq previous next))
  2257. (org-element-put-property
  2258. matrices :post-blank (org-element-property :post-blank previous))
  2259. (org-element-extract-element previous)
  2260. (org-element-adopt-elements matrices previous))))))
  2261. info)
  2262. data)
  2263. (defun org-latex-matrices (matrices contents info)
  2264. "Transcode a MATRICES element from Org to LaTeX.
  2265. CONTENTS is a string. INFO is a plist used as a communication
  2266. channel."
  2267. (format (case (org-element-property :markup matrices)
  2268. (inline "\\(%s\\)")
  2269. (equation "\\begin{equation}\n%s\\end{equation}")
  2270. (t "\\[\n%s\\]"))
  2271. contents))
  2272. (defun org-latex-matrices-tree-filter (tree backend info)
  2273. (org-latex--wrap-latex-matrices tree info))
  2274. ;;;; Pseudo Object: LaTeX Math Block
  2275. ;; `latex-math-block' objects have the following property:
  2276. ;; `:post-blank'.
  2277. (defun org-latex--wrap-latex-math-block (data info)
  2278. "Merge contiguous math objects in a pseudo-object container.
  2279. DATA is a parse tree or a secondary string. INFO is a plist
  2280. containing export options. Modify DATA by side-effect and return it."
  2281. (let ((valid-object-p
  2282. (function
  2283. ;; Non-nil when OBJ can be added to the latex math block.
  2284. (lambda (obj)
  2285. (case (org-element-type obj)
  2286. (entity (org-element-property :latex-math-p obj))
  2287. (latex-fragment
  2288. (let ((value (org-element-property :value obj)))
  2289. (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
  2290. (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
  2291. ((subscript superscript) t))))))
  2292. (org-element-map data '(entity latex-fragment subscript superscript)
  2293. (lambda (object)
  2294. ;; Skip objects already wrapped.
  2295. (when (and (not (eq (org-element-type
  2296. (org-element-property :parent object))
  2297. 'latex-math-block))
  2298. (funcall valid-object-p object))
  2299. (let ((math-block (list 'latex-math-block nil))
  2300. (next-elements (org-export-get-next-element object info t))
  2301. (last object))
  2302. ;; Wrap MATH-BLOCK around OBJECT in DATA.
  2303. (org-element-insert-before math-block object)
  2304. (org-element-extract-element object)
  2305. (org-element-adopt-elements math-block object)
  2306. (when (zerop (or (org-element-property :post-blank object) 0))
  2307. ;; MATH-BLOCK swallows consecutive math objects.
  2308. (catch 'exit
  2309. (dolist (next next-elements)
  2310. (if (not (funcall valid-object-p next)) (throw 'exit nil)
  2311. (org-element-extract-element next)
  2312. (org-element-adopt-elements math-block next)
  2313. ;; Eschew the case: \beta$x$ -> \(\betax\).
  2314. (unless (memq (org-element-type next)
  2315. '(subscript superscript))
  2316. (org-element-put-property last :post-blank 1))
  2317. (setq last next)
  2318. (when (> (or (org-element-property :post-blank next) 0) 0)
  2319. (throw 'exit nil))))))
  2320. (org-element-put-property
  2321. math-block :post-blank (org-element-property :post-blank last)))))
  2322. info nil '(subscript superscript latex-math-block) t)
  2323. ;; Return updated DATA.
  2324. data))
  2325. (defun org-latex-math-block-tree-filter (tree backend info)
  2326. (org-latex--wrap-latex-math-block tree info))
  2327. (defun org-latex-math-block-options-filter (info backend)
  2328. (dolist (prop '(:author :date :title) info)
  2329. (plist-put info prop
  2330. (org-latex--wrap-latex-math-block (plist-get info prop) info))))
  2331. (defun org-latex-math-block (math-block contents info)
  2332. "Transcode a MATH-BLOCK object from Org to LaTeX.
  2333. CONTENTS is a string. INFO is a plist used as a communication
  2334. channel."
  2335. (when (org-string-nw-p contents)
  2336. (format "\\(%s\\)" (org-trim contents))))
  2337. ;;;; Quote Block
  2338. (defun org-latex-quote-block (quote-block contents info)
  2339. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  2340. CONTENTS holds the contents of the block. INFO is a plist
  2341. holding contextual information."
  2342. (org-latex--wrap-label
  2343. quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
  2344. ;;;; Radio Target
  2345. (defun org-latex-radio-target (radio-target text info)
  2346. "Transcode a RADIO-TARGET object from Org to LaTeX.
  2347. TEXT is the text of the target. INFO is a plist holding
  2348. contextual information."
  2349. (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
  2350. ;;;; Section
  2351. (defun org-latex-section (section contents info)
  2352. "Transcode a SECTION element from Org to LaTeX.
  2353. CONTENTS holds the contents of the section. INFO is a plist
  2354. holding contextual information."
  2355. contents)
  2356. ;;;; Special Block
  2357. (defun org-latex-special-block (special-block contents info)
  2358. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  2359. CONTENTS holds the contents of the block. INFO is a plist
  2360. holding contextual information."
  2361. (let ((type (org-element-property :type special-block))
  2362. (opt (org-export-read-attribute :attr_latex special-block :options))
  2363. (caption (org-latex--caption/label-string special-block info))
  2364. (caption-above-p (org-latex--caption-above-p special-block info)))
  2365. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  2366. (and caption-above-p caption)
  2367. contents
  2368. (and (not caption-above-p) caption)
  2369. (format "\\end{%s}" type))))
  2370. ;;;; Src Block
  2371. (defun org-latex-src-block (src-block contents info)
  2372. "Transcode a SRC-BLOCK element from Org to LaTeX.
  2373. CONTENTS holds the contents of the item. INFO is a plist holding
  2374. contextual information."
  2375. (when (org-string-nw-p (org-element-property :value src-block))
  2376. (let* ((lang (org-element-property :language src-block))
  2377. (caption (org-element-property :caption src-block))
  2378. (caption-above-p (org-latex--caption-above-p src-block info))
  2379. (label (org-element-property :name src-block))
  2380. (custom-env (and lang
  2381. (cadr (assq (intern lang)
  2382. org-latex-custom-lang-environments))))
  2383. (num-start (case (org-element-property :number-lines src-block)
  2384. (continued (org-export-get-loc src-block info))
  2385. (new 0)))
  2386. (retain-labels (org-element-property :retain-labels src-block))
  2387. (attributes (org-export-read-attribute :attr_latex src-block))
  2388. (float (plist-get attributes :float))
  2389. (listings (plist-get info :latex-listings)))
  2390. (cond
  2391. ;; Case 1. No source fontification.
  2392. ((not listings)
  2393. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2394. (float-env
  2395. (cond ((string= "multicolumn" float)
  2396. (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
  2397. (plist-get info :latex-default-figure-position)
  2398. (if caption-above-p caption-str "")
  2399. (if caption-above-p "" caption-str)))
  2400. (caption (concat
  2401. (if caption-above-p caption-str "")
  2402. "%s"
  2403. (if caption-above-p "" (concat "\n" caption-str))))
  2404. (t "%s"))))
  2405. (format
  2406. float-env
  2407. (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
  2408. (org-export-format-code-default src-block info))))))
  2409. ;; Case 2. Custom environment.
  2410. (custom-env
  2411. (let ((caption-str (org-latex--caption/label-string src-block info)))
  2412. (format "\\begin{%s}\n%s\\end{%s}\n"
  2413. custom-env
  2414. (concat (and caption-above-p caption-str)
  2415. (org-export-format-code-default src-block info)
  2416. (and (not caption-above-p) caption-str))
  2417. custom-env)))
  2418. ;; Case 3. Use minted package.
  2419. ((eq listings 'minted)
  2420. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2421. (float-env
  2422. (cond
  2423. ((string= "multicolumn" float)
  2424. (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
  2425. (if caption-above-p caption-str "")
  2426. (if caption-above-p "" caption-str)))
  2427. (caption
  2428. (concat (if caption-above-p caption-str "")
  2429. "%s"
  2430. (if caption-above-p "" (concat "\n" caption-str))))
  2431. (t "%s")))
  2432. (options (plist-get info :latex-minted-options))
  2433. (body
  2434. (format
  2435. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  2436. ;; Options.
  2437. (concat
  2438. (org-latex--make-option-string
  2439. (if (or (not num-start) (assoc "linenos" options))
  2440. options
  2441. (append
  2442. `(("linenos")
  2443. ("firstnumber" ,(number-to-string (1+ num-start))))
  2444. options)))
  2445. (let ((local-options (plist-get attributes :options)))
  2446. (and local-options (concat "," local-options))))
  2447. ;; Language.
  2448. (or (cadr (assq (intern lang)
  2449. (plist-get info :latex-minted-langs)))
  2450. (downcase lang))
  2451. ;; Source code.
  2452. (let* ((code-info (org-export-unravel-code src-block))
  2453. (max-width
  2454. (apply 'max
  2455. (mapcar 'length
  2456. (org-split-string (car code-info)
  2457. "\n")))))
  2458. (org-export-format-code
  2459. (car code-info)
  2460. (lambda (loc num ref)
  2461. (concat
  2462. loc
  2463. (when ref
  2464. ;; Ensure references are flushed to the right,
  2465. ;; separated with 6 spaces from the widest line
  2466. ;; of code.
  2467. (concat (make-string (+ (- max-width (length loc)) 6)
  2468. ?\s)
  2469. (format "(%s)" ref)))))
  2470. nil (and retain-labels (cdr code-info)))))))
  2471. ;; Return value.
  2472. (format float-env body)))
  2473. ;; Case 4. Use listings package.
  2474. (t
  2475. (let ((lst-lang
  2476. (or (cadr (assq (intern lang)
  2477. (plist-get info :latex-listings-langs)))
  2478. lang))
  2479. (caption-str
  2480. (when caption
  2481. (let ((main (org-export-get-caption src-block))
  2482. (secondary (org-export-get-caption src-block t)))
  2483. (if (not secondary)
  2484. (format "{%s}" (org-export-data main info))
  2485. (format "{[%s]%s}"
  2486. (org-export-data secondary info)
  2487. (org-export-data main info))))))
  2488. (lst-opt (plist-get info :latex-listings-options)))
  2489. (concat
  2490. ;; Options.
  2491. (format
  2492. "\\lstset{%s}\n"
  2493. (concat
  2494. (org-latex--make-option-string
  2495. (append
  2496. lst-opt
  2497. (cond
  2498. ((and (not float) (plist-member attributes :float)) nil)
  2499. ((string= "multicolumn" float) '(("float" "*")))
  2500. ((and float (not (assoc "float" lst-opt)))
  2501. `(("float" ,(plist-get info :latex-default-figure-position)))))
  2502. `(("language" ,lst-lang))
  2503. (if label `(("label" ,label)) '(("label" " ")))
  2504. (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
  2505. `(("captionpos" ,(if caption-above-p "t" "b")))
  2506. (cond ((assoc "numbers" lst-opt) nil)
  2507. ((not num-start) '(("numbers" "none")))
  2508. ((zerop num-start) '(("numbers" "left")))
  2509. (t `(("firstnumber" ,(number-to-string (1+ num-start)))
  2510. ("numbers" "left"))))))
  2511. (let ((local-options (plist-get attributes :options)))
  2512. (and local-options (concat "," local-options)))))
  2513. ;; Source code.
  2514. (format
  2515. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  2516. (let* ((code-info (org-export-unravel-code src-block))
  2517. (max-width
  2518. (apply 'max
  2519. (mapcar 'length
  2520. (org-split-string (car code-info) "\n")))))
  2521. (org-export-format-code
  2522. (car code-info)
  2523. (lambda (loc num ref)
  2524. (concat
  2525. loc
  2526. (when ref
  2527. ;; Ensure references are flushed to the right,
  2528. ;; separated with 6 spaces from the widest line of
  2529. ;; code
  2530. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  2531. (format "(%s)" ref)))))
  2532. nil (and retain-labels (cdr code-info))))))))))))
  2533. ;;;; Statistics Cookie
  2534. (defun org-latex-statistics-cookie (statistics-cookie contents info)
  2535. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  2536. CONTENTS is nil. INFO is a plist holding contextual information."
  2537. (replace-regexp-in-string
  2538. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  2539. ;;;; Strike-Through
  2540. (defun org-latex-strike-through (strike-through contents info)
  2541. "Transcode STRIKE-THROUGH from Org to LaTeX.
  2542. CONTENTS is the text with strike-through markup. INFO is a plist
  2543. holding contextual information."
  2544. (org-latex--text-markup contents 'strike-through info))
  2545. ;;;; Subscript
  2546. (defun org-latex--script-size (object info)
  2547. "Transcode a subscript or superscript object.
  2548. OBJECT is an Org object. INFO is a plist used as a communication
  2549. channel."
  2550. (let ((type (org-element-type object))
  2551. (output ""))
  2552. (org-element-map (org-element-contents object)
  2553. (cons 'plain-text org-element-all-objects)
  2554. (lambda (obj)
  2555. (case (org-element-type obj)
  2556. ((entity latex-fragment)
  2557. (let ((data (org-trim (org-export-data obj info))))
  2558. (string-match
  2559. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  2560. data)
  2561. (setq output
  2562. (concat output
  2563. (match-string 1 data)
  2564. (let ((blank (org-element-property :post-blank obj)))
  2565. (and blank (> blank 0) "\\ "))))))
  2566. (plain-text
  2567. (setq output
  2568. (format "%s\\text{%s}" output (org-export-data obj info))))
  2569. (otherwise
  2570. (setq output
  2571. (concat output
  2572. (org-export-data obj info)
  2573. (let ((blank (org-element-property :post-blank obj)))
  2574. (and blank (> blank 0) "\\ ")))))))
  2575. info nil org-element-recursive-objects)
  2576. ;; Result. Do not wrap into curly brackets if OUTPUT is a single
  2577. ;; character.
  2578. (concat (if (eq (org-element-type object) 'subscript) "_" "^")
  2579. (and (> (length output) 1) "{")
  2580. output
  2581. (and (> (length output) 1) "}"))))
  2582. (defun org-latex-subscript (subscript contents info)
  2583. "Transcode a SUBSCRIPT object from Org to LaTeX.
  2584. CONTENTS is the contents of the object. INFO is a plist holding
  2585. contextual information."
  2586. (org-latex--script-size subscript info))
  2587. ;;;; Superscript
  2588. (defun org-latex-superscript (superscript contents info)
  2589. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  2590. CONTENTS is the contents of the object. INFO is a plist holding
  2591. contextual information."
  2592. (org-latex--script-size superscript info))
  2593. ;;;; Table
  2594. ;;
  2595. ;; `org-latex-table' is the entry point for table transcoding. It
  2596. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  2597. ;; delegates the job to either `org-latex--table.el-table',
  2598. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  2599. ;; depending of the type of the table and the mode requested.
  2600. ;;
  2601. ;; `org-latex--align-string' is a subroutine used to build alignment
  2602. ;; string for Org tables.
  2603. (defun org-latex-table (table contents info)
  2604. "Transcode a TABLE element from Org to LaTeX.
  2605. CONTENTS is the contents of the table. INFO is a plist holding
  2606. contextual information."
  2607. (if (eq (org-element-property :type table) 'table.el)
  2608. ;; "table.el" table. Convert it using appropriate tools.
  2609. (org-latex--table.el-table table info)
  2610. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  2611. (plist-get info :latex-default-table-mode))))
  2612. (cond
  2613. ;; Case 1: Verbatim table.
  2614. ((string= type "verbatim")
  2615. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  2616. ;; Re-create table, without affiliated keywords.
  2617. (org-trim (org-element-interpret-data
  2618. `(table nil ,@(org-element-contents table))))))
  2619. ;; Case 2: Matrix.
  2620. ((or (string= type "math") (string= type "inline-math"))
  2621. (org-latex--math-table table info))
  2622. ;; Case 3: Standard table.
  2623. (t (concat (org-latex--org-table table contents info)
  2624. ;; When there are footnote references within the
  2625. ;; table, insert their definition just after it.
  2626. (org-latex--delayed-footnotes-definitions table info)))))))
  2627. (defun org-latex--align-string (table info)
  2628. "Return an appropriate LaTeX alignment string.
  2629. TABLE is the considered table. INFO is a plist used as
  2630. a communication channel."
  2631. (or (org-export-read-attribute :attr_latex table :align)
  2632. (let (align)
  2633. ;; Extract column groups and alignment from first (non-rule)
  2634. ;; row.
  2635. (org-element-map
  2636. (org-element-map table 'table-row
  2637. (lambda (row)
  2638. (and (eq (org-element-property :type row) 'standard) row))
  2639. info 'first-match)
  2640. 'table-cell
  2641. (lambda (cell)
  2642. (let ((borders (org-export-table-cell-borders cell info)))
  2643. ;; Check left border for the first cell only.
  2644. (when (and (memq 'left borders) (not align))
  2645. (push "|" align))
  2646. (push (case (org-export-table-cell-alignment cell info)
  2647. (left "l")
  2648. (right "r")
  2649. (center "c"))
  2650. align)
  2651. (when (memq 'right borders) (push "|" align))))
  2652. info)
  2653. (apply 'concat (nreverse align)))))
  2654. (defun org-latex--org-table (table contents info)
  2655. "Return appropriate LaTeX code for an Org table.
  2656. TABLE is the table type element to transcode. CONTENTS is its
  2657. contents, as a string. INFO is a plist used as a communication
  2658. channel.
  2659. This function assumes TABLE has `org' as its `:type' property and
  2660. `table' as its `:mode' attribute."
  2661. (let* ((caption (org-latex--caption/label-string table info))
  2662. (attr (org-export-read-attribute :attr_latex table))
  2663. ;; Determine alignment string.
  2664. (alignment (org-latex--align-string table info))
  2665. ;; Determine environment for the table: longtable, tabular...
  2666. (table-env (or (plist-get attr :environment)
  2667. (plist-get info :latex-default-table-environment)))
  2668. ;; If table is a float, determine environment: table, table*
  2669. ;; or sidewaystable.
  2670. (float-env (unless (member table-env '("longtable" "longtabu"))
  2671. (let ((float (plist-get attr :float)))
  2672. (cond
  2673. ((and (not float) (plist-member attr :float)) nil)
  2674. ((or (string= float "sidewaystable")
  2675. (string= float "sideways")) "sidewaystable")
  2676. ((string= float "multicolumn") "table*")
  2677. ((or float
  2678. (org-element-property :caption table)
  2679. (org-string-nw-p (plist-get attr :caption)))
  2680. "table")))))
  2681. ;; Extract others display options.
  2682. (fontsize (let ((font (plist-get attr :font)))
  2683. (and font (concat font "\n"))))
  2684. ;; "tabular" environment doesn't allow to define a width.
  2685. (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
  2686. (spreadp (plist-get attr :spread))
  2687. (placement
  2688. (or (plist-get attr :placement)
  2689. (format "[%s]" (plist-get info :latex-default-figure-position))))
  2690. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2691. (plist-get info :latex-tables-centered)))
  2692. (caption-above-p (org-latex--caption-above-p table info)))
  2693. ;; Prepare the final format string for the table.
  2694. (cond
  2695. ;; Longtable.
  2696. ((equal "longtable" table-env)
  2697. (concat (and fontsize (concat "{" fontsize))
  2698. (format "\\begin{longtable}{%s}\n" alignment)
  2699. (and caption-above-p
  2700. (org-string-nw-p caption)
  2701. (concat caption "\\\\\n"))
  2702. contents
  2703. (and (not caption-above-p)
  2704. (org-string-nw-p caption)
  2705. (concat caption "\\\\\n"))
  2706. "\\end{longtable}\n"
  2707. (and fontsize "}")))
  2708. ;; Longtabu
  2709. ((equal "longtabu" table-env)
  2710. (concat (and fontsize (concat "{" fontsize))
  2711. (format "\\begin{longtabu}%s{%s}\n"
  2712. (if width
  2713. (format " %s %s "
  2714. (if spreadp "spread" "to") width) "")
  2715. alignment)
  2716. (and caption-above-p
  2717. (org-string-nw-p caption)
  2718. (concat caption "\\\\\n"))
  2719. contents
  2720. (and (not caption-above-p)
  2721. (org-string-nw-p caption)
  2722. (concat caption "\\\\\n"))
  2723. "\\end{longtabu}\n"
  2724. (and fontsize "}")))
  2725. ;; Others.
  2726. (t (concat (cond
  2727. (float-env
  2728. (concat (format "\\begin{%s}%s\n" float-env placement)
  2729. (if caption-above-p caption "")
  2730. (when centerp "\\centering\n")
  2731. fontsize))
  2732. ((and (not float-env) caption)
  2733. (concat
  2734. (and centerp "\\begin{center}\n" )
  2735. (if caption-above-p caption "")
  2736. (cond ((and fontsize centerp) fontsize)
  2737. (fontsize (concat "{" fontsize)))))
  2738. (centerp (concat "\\begin{center}\n" fontsize))
  2739. (fontsize (concat "{" fontsize)))
  2740. (cond ((equal "tabu" table-env)
  2741. (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
  2742. (if width (format
  2743. (if spreadp " spread %s " " to %s ")
  2744. width) "")
  2745. alignment
  2746. contents))
  2747. (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2748. table-env
  2749. (if width (format "{%s}" width) "")
  2750. alignment
  2751. contents
  2752. table-env)))
  2753. (cond
  2754. (float-env
  2755. (concat (if caption-above-p "" (concat "\n" caption))
  2756. (format "\n\\end{%s}" float-env)))
  2757. ((and (not float-env) caption)
  2758. (concat
  2759. (if caption-above-p "" (concat "\n" caption))
  2760. (and centerp "\n\\end{center}")
  2761. (and fontsize (not centerp) "}")))
  2762. (centerp "\n\\end{center}")
  2763. (fontsize "}")))))))
  2764. (defun org-latex--table.el-table (table info)
  2765. "Return appropriate LaTeX code for a table.el table.
  2766. TABLE is the table type element to transcode. INFO is a plist
  2767. used as a communication channel.
  2768. This function assumes TABLE has `table.el' as its `:type'
  2769. property."
  2770. (require 'table)
  2771. ;; Ensure "*org-export-table*" buffer is empty.
  2772. (with-current-buffer (get-buffer-create "*org-export-table*")
  2773. (erase-buffer))
  2774. (let ((output (with-temp-buffer
  2775. (insert (org-element-property :value table))
  2776. (goto-char 1)
  2777. (re-search-forward "^[ \t]*|[^|]" nil t)
  2778. (table-generate-source 'latex "*org-export-table*")
  2779. (with-current-buffer "*org-export-table*"
  2780. (org-trim (buffer-string))))))
  2781. (kill-buffer (get-buffer "*org-export-table*"))
  2782. ;; Remove left out comments.
  2783. (while (string-match "^%.*\n" output)
  2784. (setq output (replace-match "" t t output)))
  2785. (let ((attr (org-export-read-attribute :attr_latex table)))
  2786. (when (plist-get attr :rmlines)
  2787. ;; When the "rmlines" attribute is provided, remove all hlines
  2788. ;; but the the one separating heading from the table body.
  2789. (let ((n 0) (pos 0))
  2790. (while (and (< (length output) pos)
  2791. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2792. (incf n)
  2793. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2794. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2795. (plist-get info :latex-tables-centered))))
  2796. (if (not centerp) output
  2797. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2798. (defun org-latex--math-table (table info)
  2799. "Return appropriate LaTeX code for a matrix.
  2800. TABLE is the table type element to transcode. INFO is a plist
  2801. used as a communication channel.
  2802. This function assumes TABLE has `org' as its `:type' property and
  2803. `inline-math' or `math' as its `:mode' attribute."
  2804. (let* ((attr (org-export-read-attribute :attr_latex table))
  2805. (env (or (plist-get attr :environment)
  2806. (plist-get info :latex-default-table-environment)))
  2807. (contents
  2808. (mapconcat
  2809. (lambda (row)
  2810. ;; Ignore horizontal rules.
  2811. (when (eq (org-element-property :type row) 'standard)
  2812. ;; Return each cell unmodified.
  2813. (concat
  2814. (mapconcat
  2815. (lambda (cell)
  2816. (substring (org-element-interpret-data cell) 0 -1))
  2817. (org-element-map row 'table-cell #'identity info) "&")
  2818. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2819. "\n")))
  2820. (org-element-map table 'table-row #'identity info) "")))
  2821. (concat
  2822. ;; Prefix.
  2823. (plist-get attr :math-prefix)
  2824. ;; Environment. Also treat special cases.
  2825. (cond ((member env '("array" "tabular"))
  2826. (let ((align (make-string
  2827. (cdr (org-export-table-dimensions table info)) ?c)))
  2828. (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
  2829. ((assoc env org-latex-table-matrix-macros)
  2830. (format "\\%s%s{\n%s}"
  2831. env
  2832. (or (plist-get attr :math-arguments) "")
  2833. contents))
  2834. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2835. ;; Suffix.
  2836. (plist-get attr :math-suffix))))
  2837. ;;;; Table Cell
  2838. (defun org-latex-table-cell (table-cell contents info)
  2839. "Transcode a TABLE-CELL element from Org to LaTeX.
  2840. CONTENTS is the cell contents. INFO is a plist used as
  2841. a communication channel."
  2842. (concat
  2843. (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
  2844. (if (and contents
  2845. scientific-format
  2846. (string-match orgtbl-exp-regexp contents))
  2847. ;; Use appropriate format string for scientific
  2848. ;; notation.
  2849. (format scientific-format
  2850. (match-string 1 contents)
  2851. (match-string 2 contents))
  2852. contents))
  2853. (when (org-export-get-next-element table-cell info) " & ")))
  2854. ;;;; Table Row
  2855. (defun org-latex-table-row (table-row contents info)
  2856. "Transcode a TABLE-ROW element from Org to LaTeX.
  2857. CONTENTS is the contents of the row. INFO is a plist used as
  2858. a communication channel."
  2859. (let* ((attr (org-export-read-attribute :attr_latex
  2860. (org-export-get-parent table-row)))
  2861. (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
  2862. (plist-get info :latex-tables-booktabs)))
  2863. (longtablep
  2864. (member (or (plist-get attr :environment)
  2865. (plist-get info :latex-default-table-environment))
  2866. '("longtable" "longtabu"))))
  2867. (if (eq (org-element-property :type table-row) 'rule)
  2868. (cond
  2869. ((not booktabsp) "\\hline")
  2870. ((not (org-export-get-previous-element table-row info)) "\\toprule")
  2871. ((not (org-export-get-next-element table-row info)) "\\bottomrule")
  2872. ((and longtablep
  2873. (org-export-table-row-ends-header-p
  2874. (org-export-get-previous-element table-row info) info))
  2875. "")
  2876. (t "\\midrule"))
  2877. (concat
  2878. ;; When BOOKTABS are activated enforce top-rule even when no
  2879. ;; hline was specifically marked.
  2880. (and booktabsp (not (org-export-get-previous-element table-row info))
  2881. "\\toprule\n")
  2882. contents "\\\\\n"
  2883. (cond
  2884. ;; Special case for long tables. Define header and footers.
  2885. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2886. (let ((columns (cdr (org-export-table-dimensions
  2887. (org-export-get-parent-table table-row) info))))
  2888. (format "%s
  2889. \\endfirsthead
  2890. \\multicolumn{%d}{l}{%s} \\\\
  2891. %s
  2892. %s \\\\\n
  2893. %s
  2894. \\endhead
  2895. %s\\multicolumn{%d}{r}{%s} \\\\
  2896. \\endfoot
  2897. \\endlastfoot"
  2898. (if booktabsp "\\midrule" "\\hline")
  2899. columns
  2900. (org-latex--translate "Continued from previous page" info)
  2901. (cond
  2902. ((not (org-export-table-row-starts-header-p table-row info))
  2903. "")
  2904. (booktabsp "\\toprule\n")
  2905. (t "\\hline\n"))
  2906. contents
  2907. (if booktabsp "\\midrule" "\\hline")
  2908. (if booktabsp "\\midrule" "\\hline")
  2909. columns
  2910. (org-latex--translate "Continued on next page" info))))
  2911. ;; When BOOKTABS are activated enforce bottom rule even when
  2912. ;; no hline was specifically marked.
  2913. ((and booktabsp (not (org-export-get-next-element table-row info)))
  2914. "\\bottomrule"))))))
  2915. ;;;; Target
  2916. (defun org-latex-target (target contents info)
  2917. "Transcode a TARGET object from Org to LaTeX.
  2918. CONTENTS is nil. INFO is a plist holding contextual
  2919. information."
  2920. (format "\\label{%s}" (org-latex--label target info)))
  2921. ;;;; Timestamp
  2922. (defun org-latex-timestamp (timestamp contents info)
  2923. "Transcode a TIMESTAMP object from Org to LaTeX.
  2924. CONTENTS is nil. INFO is a plist holding contextual
  2925. information."
  2926. (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
  2927. (format
  2928. (plist-get info
  2929. (case (org-element-property :type timestamp)
  2930. ((active active-range) :latex-active-timestamp-format)
  2931. ((inactive inactive-range) :latex-inactive-timestamp-format)
  2932. (otherwise :latex-diary-timestamp-format)))
  2933. value)))
  2934. ;;;; Underline
  2935. (defun org-latex-underline (underline contents info)
  2936. "Transcode UNDERLINE from Org to LaTeX.
  2937. CONTENTS is the text with underline markup. INFO is a plist
  2938. holding contextual information."
  2939. (org-latex--text-markup contents 'underline info))
  2940. ;;;; Verbatim
  2941. (defun org-latex-verbatim (verbatim contents info)
  2942. "Transcode a VERBATIM object from Org to LaTeX.
  2943. CONTENTS is nil. INFO is a plist used as a communication
  2944. channel."
  2945. (org-latex--text-markup
  2946. (org-element-property :value verbatim) 'verbatim info))
  2947. ;;;; Verse Block
  2948. (defun org-latex-verse-block (verse-block contents info)
  2949. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2950. CONTENTS is verse block contents. INFO is a plist holding
  2951. contextual information."
  2952. (org-latex--wrap-label
  2953. verse-block
  2954. ;; In a verse environment, add a line break to each newline
  2955. ;; character and change each white space at beginning of a line
  2956. ;; into a space of 1 em. Also change each blank line with
  2957. ;; a vertical space of 1 em.
  2958. (format "\\begin{verse}\n%s\\end{verse}"
  2959. (replace-regexp-in-string
  2960. "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
  2961. (replace-regexp-in-string
  2962. "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
  2963. (replace-regexp-in-string
  2964. "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
  2965. contents nil t) nil t) nil t))
  2966. info))
  2967. ;;; End-user functions
  2968. ;;;###autoload
  2969. (defun org-latex-export-as-latex
  2970. (&optional async subtreep visible-only body-only ext-plist)
  2971. "Export current buffer as a LaTeX buffer.
  2972. If narrowing is active in the current buffer, only export its
  2973. narrowed part.
  2974. If a region is active, export that region.
  2975. A non-nil optional argument ASYNC means the process should happen
  2976. asynchronously. The resulting buffer should be accessible
  2977. through the `org-export-stack' interface.
  2978. When optional argument SUBTREEP is non-nil, export the sub-tree
  2979. at point, extracting information from the headline properties
  2980. first.
  2981. When optional argument VISIBLE-ONLY is non-nil, don't export
  2982. contents of hidden elements.
  2983. When optional argument BODY-ONLY is non-nil, only write code
  2984. between \"\\begin{document}\" and \"\\end{document}\".
  2985. EXT-PLIST, when provided, is a property list with external
  2986. parameters overriding Org default settings, but still inferior to
  2987. file-local settings.
  2988. Export is done in a buffer named \"*Org LATEX Export*\", which
  2989. will be displayed when `org-export-show-temporary-export-buffer'
  2990. is non-nil."
  2991. (interactive)
  2992. (org-export-to-buffer 'latex "*Org LATEX Export*"
  2993. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  2994. ;;;###autoload
  2995. (defun org-latex-convert-region-to-latex ()
  2996. "Assume the current region has org-mode syntax, and convert it to LaTeX.
  2997. This can be used in any buffer. For example, you can write an
  2998. itemized list in org-mode syntax in an LaTeX buffer and use this
  2999. command to convert it."
  3000. (interactive)
  3001. (org-export-replace-region-by 'latex))
  3002. ;;;###autoload
  3003. (defun org-latex-export-to-latex
  3004. (&optional async subtreep visible-only body-only ext-plist)
  3005. "Export current buffer to a LaTeX file.
  3006. If narrowing is active in the current buffer, only export its
  3007. narrowed part.
  3008. If a region is active, export that region.
  3009. A non-nil optional argument ASYNC means the process should happen
  3010. asynchronously. The resulting file should be accessible through
  3011. the `org-export-stack' interface.
  3012. When optional argument SUBTREEP is non-nil, export the sub-tree
  3013. at point, extracting information from the headline properties
  3014. first.
  3015. When optional argument VISIBLE-ONLY is non-nil, don't export
  3016. contents of hidden elements.
  3017. When optional argument BODY-ONLY is non-nil, only write code
  3018. between \"\\begin{document}\" and \"\\end{document}\".
  3019. EXT-PLIST, when provided, is a property list with external
  3020. parameters overriding Org default settings, but still inferior to
  3021. file-local settings."
  3022. (interactive)
  3023. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3024. (org-export-to-file 'latex outfile
  3025. async subtreep visible-only body-only ext-plist)))
  3026. ;;;###autoload
  3027. (defun org-latex-export-to-pdf
  3028. (&optional async subtreep visible-only body-only ext-plist)
  3029. "Export current buffer to LaTeX then process through to PDF.
  3030. If narrowing is active in the current buffer, only export its
  3031. narrowed part.
  3032. If a region is active, export that region.
  3033. A non-nil optional argument ASYNC means the process should happen
  3034. asynchronously. The resulting file should be accessible through
  3035. the `org-export-stack' interface.
  3036. When optional argument SUBTREEP is non-nil, export the sub-tree
  3037. at point, extracting information from the headline properties
  3038. first.
  3039. When optional argument VISIBLE-ONLY is non-nil, don't export
  3040. contents of hidden elements.
  3041. When optional argument BODY-ONLY is non-nil, only write code
  3042. between \"\\begin{document}\" and \"\\end{document}\".
  3043. EXT-PLIST, when provided, is a property list with external
  3044. parameters overriding Org default settings, but still inferior to
  3045. file-local settings.
  3046. Return PDF file's name."
  3047. (interactive)
  3048. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3049. (org-export-to-file 'latex outfile
  3050. async subtreep visible-only body-only ext-plist
  3051. (lambda (file) (org-latex-compile file)))))
  3052. (defun org-latex-compile (texfile &optional snippet)
  3053. "Compile a TeX file.
  3054. TEXFILE is the name of the file being compiled. Processing is
  3055. done through the command specified in `org-latex-pdf-process'.
  3056. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  3057. file used to preview a LaTeX snippet. In this case, do not
  3058. create a log buffer and do not bother removing log files.
  3059. Return PDF file name or an error if it couldn't be produced."
  3060. (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
  3061. (full-name (file-truename texfile))
  3062. (compiler (or (with-temp-buffer
  3063. (save-excursion (insert-file-contents full-name))
  3064. (when (and (search-forward-regexp
  3065. (regexp-opt org-latex-compilers) (line-end-position 2) t)
  3066. (progn (beginning-of-line)
  3067. (looking-at-p "%")))
  3068. (match-string 0)))
  3069. "pdflatex"))
  3070. (out-dir (file-name-directory texfile))
  3071. ;; Properly set working directory for compilation.
  3072. (default-directory (if (file-name-absolute-p texfile)
  3073. (file-name-directory full-name)
  3074. default-directory))
  3075. (time (current-time))
  3076. warnings)
  3077. (unless snippet (message "Processing LaTeX file %s..." texfile))
  3078. (save-window-excursion
  3079. (cond
  3080. ;; A function is provided: Apply it.
  3081. ((functionp org-latex-pdf-process)
  3082. (funcall org-latex-pdf-process (shell-quote-argument texfile)))
  3083. ;; A list is provided: Replace %b, %f and %o with appropriate
  3084. ;; values in each command before applying it. Note that while
  3085. ;; "%latex" and "%bibtex" is used in `org-latex-pdf-process',
  3086. ;; they are replaced with "%L" and "%B" to adhere to
  3087. ;; format-spec. Output is redirected to "*Org PDF LaTeX
  3088. ;; Output*" buffer.
  3089. ((consp org-latex-pdf-process)
  3090. (let ((outbuf (and (not snippet)
  3091. (get-buffer-create "*Org PDF LaTeX Output*")))
  3092. (spec (list (cons ?B (shell-quote-argument org-latex-bib-compiler))
  3093. (cons ?L (shell-quote-argument compiler))
  3094. (cons ?b (shell-quote-argument base-name))
  3095. (cons ?f (shell-quote-argument full-name))
  3096. (cons ?o (shell-quote-argument out-dir)))))
  3097. (mapc (lambda (command)
  3098. (shell-command (format-spec command spec) outbuf))
  3099. (mapcar (lambda (command)
  3100. (replace-regexp-in-string "%\\(latex\\|bibtex\\)\\>"
  3101. (lambda (str) (upcase (substring str 0 2)))
  3102. command))
  3103. org-latex-pdf-process))
  3104. ;; Collect standard errors from output buffer.
  3105. (setq warnings (and (not snippet)
  3106. (org-latex--collect-warnings outbuf)))))
  3107. (t (error "No valid command to process to PDF")))
  3108. (let ((pdffile (concat out-dir base-name ".pdf")))
  3109. ;; Check for process failure. Provide collected errors if
  3110. ;; possible.
  3111. (if (or (not (file-exists-p pdffile))
  3112. (time-less-p (nth 5 (file-attributes pdffile)) time))
  3113. (error (format "PDF file %s wasn't produced" pdffile))
  3114. ;; Else remove log files, when specified, and signal end of
  3115. ;; process to user, along with any error encountered.
  3116. (unless snippet
  3117. (when org-latex-remove-logfiles
  3118. (dolist (file (directory-files
  3119. out-dir t
  3120. (concat (regexp-quote base-name)
  3121. "\\(?:\\.[0-9]+\\)?"
  3122. "\\."
  3123. (regexp-opt org-latex-logfiles-extensions))))
  3124. (delete-file file)))
  3125. (message (concat "PDF file produced"
  3126. (cond
  3127. ((eq warnings 'error) " with errors.")
  3128. (warnings (concat " with warnings: " warnings))
  3129. (t "."))))))
  3130. ;; Return output file name.
  3131. pdffile))))
  3132. (defun org-latex--collect-warnings (buffer)
  3133. "Collect some warnings from \"pdflatex\" command output.
  3134. BUFFER is the buffer containing output. Return collected
  3135. warnings types as a string, `error' if a LaTeX error was
  3136. encountered or nil if there was none."
  3137. (with-current-buffer buffer
  3138. (save-excursion
  3139. (goto-char (point-max))
  3140. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  3141. (if (re-search-forward "^!" nil t) 'error
  3142. (let ((case-fold-search t)
  3143. (warnings ""))
  3144. (dolist (warning org-latex-known-warnings)
  3145. (when (save-excursion (re-search-forward (car warning) nil t))
  3146. (setq warnings (concat warnings " " (cdr warning)))))
  3147. (org-string-nw-p (org-trim warnings))))))))
  3148. ;;;###autoload
  3149. (defun org-latex-publish-to-latex (plist filename pub-dir)
  3150. "Publish an Org file to LaTeX.
  3151. FILENAME is the filename of the Org file to be published. PLIST
  3152. is the property list for the given project. PUB-DIR is the
  3153. publishing directory.
  3154. Return output file name."
  3155. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  3156. ;;;###autoload
  3157. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  3158. "Publish an Org file to PDF (via LaTeX).
  3159. FILENAME is the filename of the Org file to be published. PLIST
  3160. is the property list for the given project. PUB-DIR is the
  3161. publishing directory.
  3162. Return output file name."
  3163. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  3164. ;; in working directory and then moved to publishing directory.
  3165. (org-publish-attachment
  3166. plist
  3167. (org-latex-compile
  3168. (org-publish-org-to
  3169. 'latex filename ".tex" plist (file-name-directory filename)))
  3170. pub-dir))
  3171. (provide 'ox-latex)
  3172. ;; Local variables:
  3173. ;; generated-autoload-file: "org-loaddefs.el"
  3174. ;; End:
  3175. ;;; ox-latex.el ends here