ox-latex.el 135 KB

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