ox-latex.el 130 KB

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