ox-latex.el 129 KB

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