ox-latex.el 129 KB

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