ox-latex.el 128 KB

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