ox-latex.el 131 KB

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