ox-latex.el 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674
  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. full file name, %b by the file base name (i.e. without directory
  971. and extension parts), %o by the base directory of the file,
  972. %latex is the LaTeX compiler (see `org-latex-compiler'), and %bib
  973. is the BibTeX-like compiler (see `org-latex-bib-compiler').
  974. The reason why this is a list is that it usually takes several
  975. runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
  976. does not have a clever mechanism to detect which of these
  977. commands have to be run to get to a stable result, and it also
  978. does not do any error checking.
  979. Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
  980. which calls the \"correct\" combinations of auxiliary programs.
  981. Alternatively, this may be a Lisp function that does the
  982. processing, so you could use this to apply the machinery of
  983. AUCTeX or the Emacs LaTeX mode. This function should accept the
  984. file name as its single argument."
  985. :group 'org-export-pdf
  986. :type '(choice
  987. (repeat :tag "Shell command sequence"
  988. (string :tag "Shell command"))
  989. (const :tag "2 runs of latex"
  990. ("%latex -interaction nonstopmode -output-directory %o %f"
  991. "%latex -interaction nonstopmode -output-directory %o %f"))
  992. (const :tag "3 runs of latex"
  993. ("%latex -interaction nonstopmode -output-directory %o %f"
  994. "%latex -interaction nonstopmode -output-directory %o %f"
  995. "%latex -interaction nonstopmode -output-directory %o %f"))
  996. (const :tag "latex,bibtex,latex,latex"
  997. ("%latex -interaction nonstopmode -output-directory %o %f"
  998. "%bib %b"
  999. "%latex -interaction nonstopmode -output-directory %o %f"
  1000. "%latex -interaction nonstopmode -output-directory %o %f"))
  1001. (const :tag "texi2dvi"
  1002. ("LATEX=\"%latex\" texi2dvi -p -b -V %f"))
  1003. (const :tag "latexmk"
  1004. ("latexmk -g -pdflatex=\"%latex\" %f"))
  1005. (function)))
  1006. (defcustom org-latex-logfiles-extensions
  1007. '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
  1008. "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
  1009. "The list of file extensions to consider as LaTeX logfiles.
  1010. The logfiles will be removed if `org-latex-remove-logfiles' is
  1011. non-nil."
  1012. :group 'org-export-latex
  1013. :version "25.2"
  1014. :package-version '(Org . "8.3")
  1015. :type '(repeat (string :tag "Extension")))
  1016. (defcustom org-latex-remove-logfiles t
  1017. "Non-nil means remove the logfiles produced by PDF production.
  1018. By default, logfiles are files with these extensions: .aux, .idx,
  1019. .log, .out, .toc, .nav, .snm and .vrb. To define the set of
  1020. logfiles to remove, set `org-latex-logfiles-extensions'."
  1021. :group 'org-export-latex
  1022. :type 'boolean)
  1023. (defcustom org-latex-known-warnings
  1024. '(("Reference.*?undefined" . "[undefined reference]")
  1025. ("Runaway argument" . "[runaway argument]")
  1026. ("Underfull \\hbox" . "[underfull hbox]")
  1027. ("Overfull \\hbox" . "[overfull hbox]")
  1028. ("Citation.*?undefined" . "[undefined citation]")
  1029. ("Undefined control sequence" . "[undefined control sequence]"))
  1030. "Alist of regular expressions and associated messages for the user.
  1031. The regular expressions are used to find possible warnings in the
  1032. log of a latex-run. These warnings will be reported after
  1033. calling `org-latex-compile'."
  1034. :group 'org-export-latex
  1035. :version "25.2"
  1036. :package-version '(Org . "8.3")
  1037. :type '(repeat
  1038. (cons
  1039. (string :tag "Regexp")
  1040. (string :tag "Message"))))
  1041. ;;; Internal Functions
  1042. (defun org-latex--caption-above-p (element info)
  1043. "Non nil when caption is expected to be located above ELEMENT.
  1044. INFO is a plist holding contextual information."
  1045. (let ((above (plist-get info :latex-caption-above)))
  1046. (if (symbolp above) above
  1047. (let ((type (org-element-type element)))
  1048. (memq (if (eq type 'link) 'image type) above)))))
  1049. (defun org-latex--label (datum info &optional force full)
  1050. "Return an appropriate label for DATUM.
  1051. DATUM is an element or a `target' type object. INFO is the
  1052. current export state, as a plist.
  1053. Return nil if element DATUM has no NAME or VALUE affiliated
  1054. keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
  1055. this case always return a unique label.
  1056. Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
  1057. (let* ((type (org-element-type datum))
  1058. (user-label
  1059. (org-element-property
  1060. (cl-case type
  1061. ((headline inlinetask) :CUSTOM_ID)
  1062. (target :value)
  1063. (otherwise :name))
  1064. datum))
  1065. (label
  1066. (and (or user-label force)
  1067. (if (and user-label (plist-get info :latex-prefer-user-labels))
  1068. user-label
  1069. (concat (cl-case type
  1070. (headline "sec:")
  1071. (table "tab:")
  1072. (latex-environment
  1073. (and (string-match-p
  1074. org-latex-math-environments-re
  1075. (org-element-property :value datum))
  1076. "eq:"))
  1077. (paragraph
  1078. (and (org-element-property :caption datum)
  1079. "fig:")))
  1080. (org-export-get-reference datum info))))))
  1081. (cond ((not full) label)
  1082. (label (format "\\label{%s}%s"
  1083. label
  1084. (if (eq type 'target) "" "\n")))
  1085. (t ""))))
  1086. (defun org-latex--caption/label-string (element info)
  1087. "Return caption and label LaTeX string for ELEMENT.
  1088. INFO is a plist holding contextual information. If there's no
  1089. caption nor label, return the empty string.
  1090. For non-floats, see `org-latex--wrap-label'."
  1091. (let* ((label (org-latex--label element info nil t))
  1092. (main (org-export-get-caption element))
  1093. (attr (org-export-read-attribute :attr_latex element))
  1094. (type (org-element-type element))
  1095. (nonfloat (or (and (plist-member attr :float)
  1096. (not (plist-get attr :float))
  1097. main)
  1098. (and (eq type 'src-block)
  1099. (not (plist-get attr :float))
  1100. (memq (plist-get info :latex-listings)
  1101. '(nil minted)))))
  1102. (short (org-export-get-caption element t))
  1103. (caption-from-attr-latex (plist-get attr :caption)))
  1104. (cond
  1105. ((org-string-nw-p caption-from-attr-latex)
  1106. (concat caption-from-attr-latex "\n"))
  1107. ((and (not main) (equal label "")) "")
  1108. ((not main) label)
  1109. ;; Option caption format with short name.
  1110. (t
  1111. (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
  1112. "\\caption%s%s{%s%s}\n")
  1113. (if nonfloat
  1114. (cl-case type
  1115. (paragraph "figure")
  1116. (src-block (if (plist-get info :latex-listings)
  1117. "listing"
  1118. "figure"))
  1119. (t (symbol-name type)))
  1120. "")
  1121. (if short (format "[%s]" (org-export-data short info)) "")
  1122. label
  1123. (org-export-data main info))))))
  1124. (defun org-latex-guess-inputenc (header)
  1125. "Set the coding system in inputenc to what the buffer is.
  1126. HEADER is the LaTeX header string. This function only applies
  1127. when specified inputenc option is \"AUTO\".
  1128. Return the new header, as a string."
  1129. (let* ((cs (or (ignore-errors
  1130. (latexenc-coding-system-to-inputenc
  1131. (or org-export-coding-system buffer-file-coding-system)))
  1132. "utf8")))
  1133. (if (not cs) header
  1134. ;; First translate if that is requested.
  1135. (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
  1136. ;; Then find the \usepackage statement and replace the option.
  1137. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  1138. cs header t nil 1))))
  1139. (defun org-latex-guess-babel-language (header info)
  1140. "Set Babel's language according to LANGUAGE keyword.
  1141. HEADER is the LaTeX header string. INFO is the plist used as
  1142. a communication channel.
  1143. Insertion of guessed language only happens when Babel package has
  1144. explicitly been loaded. Then it is added to the rest of
  1145. package's options.
  1146. The argument to Babel may be \"AUTO\" which is then replaced with
  1147. the language of the document or `org-export-default-language'
  1148. unless language in question is already loaded.
  1149. Return the new header."
  1150. (let ((language-code (plist-get info :language)))
  1151. ;; If no language is set or Babel package is not loaded, return
  1152. ;; HEADER as-is.
  1153. (if (or (not (stringp language-code))
  1154. (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
  1155. header
  1156. (let ((options (save-match-data
  1157. (org-split-string (match-string 1 header) ",[ \t]*")))
  1158. (language (cdr (assoc-string language-code
  1159. org-latex-babel-language-alist t))))
  1160. ;; If LANGUAGE is already loaded, return header without AUTO.
  1161. ;; Otherwise, replace AUTO with language or append language if
  1162. ;; AUTO is not present.
  1163. (replace-match
  1164. (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
  1165. (cond ((member language options) (delete "AUTO" options))
  1166. ((member "AUTO" options) options)
  1167. (t (append options (list language))))
  1168. ", ")
  1169. t nil header 1)))))
  1170. (defun org-latex-guess-polyglossia-language (header info)
  1171. "Set the Polyglossia language according to the LANGUAGE keyword.
  1172. HEADER is the LaTeX header string. INFO is the plist used as
  1173. a communication channel.
  1174. Insertion of guessed language only happens when the Polyglossia
  1175. package has been explicitly loaded.
  1176. The argument to Polyglossia may be \"AUTO\" which is then
  1177. replaced with the language of the document or
  1178. `org-export-default-language'. Note, the language is really set
  1179. using \setdefaultlanguage and not as an option to the package.
  1180. Return the new header."
  1181. (let ((language (plist-get info :language)))
  1182. ;; If no language is set or Polyglossia is not loaded, return
  1183. ;; HEADER as-is.
  1184. (if (or (not (stringp language))
  1185. (not (string-match
  1186. "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
  1187. header)))
  1188. header
  1189. (let* ((options (org-string-nw-p (match-string 1 header)))
  1190. (languages (and options
  1191. ;; Reverse as the last loaded language is
  1192. ;; the main language.
  1193. (nreverse
  1194. (delete-dups
  1195. (save-match-data
  1196. (org-split-string
  1197. (replace-regexp-in-string
  1198. "AUTO" language options t)
  1199. ",[ \t]*"))))))
  1200. (main-language-set
  1201. (string-match-p "\\\\setmainlanguage{.*?}" header)))
  1202. (replace-match
  1203. (concat "\\usepackage{polyglossia}\n"
  1204. (mapconcat
  1205. (lambda (l)
  1206. (let ((l (or (assoc l org-latex-polyglossia-language-alist)
  1207. l)))
  1208. (format (if main-language-set "\\setotherlanguage%s{%s}\n"
  1209. (setq main-language-set t)
  1210. "\\setmainlanguage%s{%s}\n")
  1211. (if (and (consp l) (= (length l) 3))
  1212. (format "[variant=%s]" (nth 2 l))
  1213. "")
  1214. (nth 1 l))))
  1215. languages
  1216. ""))
  1217. t t header 0)))))
  1218. (defun org-latex--remove-packages (pkg-alist info)
  1219. "Remove packages based on the current LaTeX compiler.
  1220. If the fourth argument of an element is set in pkg-alist, and it
  1221. is not a member of the LaTeX compiler of the document, the packages
  1222. is removed. See also `org-latex-compiler'.
  1223. Return modified pkg-alist."
  1224. (let ((compiler (or (plist-get info :latex-compiler) "")))
  1225. (if (member-ignore-case compiler org-latex-compilers)
  1226. (delq nil
  1227. (mapcar
  1228. (lambda (pkg)
  1229. (unless (and
  1230. (listp pkg)
  1231. (let ((third (nth 3 pkg)))
  1232. (and third
  1233. (not (member-ignore-case
  1234. compiler
  1235. (if (listp third) third (list third)))))))
  1236. pkg))
  1237. pkg-alist))
  1238. pkg-alist)))
  1239. (defun org-latex--find-verb-separator (s)
  1240. "Return a character not used in string S.
  1241. This is used to choose a separator for constructs like \\verb."
  1242. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1243. (cl-loop for c across ll
  1244. when (not (string-match (regexp-quote (char-to-string c)) s))
  1245. return (char-to-string c))))
  1246. (defun org-latex--make-option-string (options)
  1247. "Return a comma separated string of keywords and values.
  1248. OPTIONS is an alist where the key is the options keyword as
  1249. a string, and the value a list containing the keyword value, or
  1250. nil."
  1251. (mapconcat (lambda (pair)
  1252. (pcase-let ((`(,keyword ,value) pair))
  1253. (concat keyword
  1254. (and (> (length value) 0)
  1255. (concat "=" value)))))
  1256. options
  1257. ","))
  1258. (defun org-latex--wrap-label (element output info)
  1259. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  1260. INFO is the current export state, as a plist. This function
  1261. should not be used for floats. See
  1262. `org-latex--caption/label-string'."
  1263. (if (not (and (org-string-nw-p output) (org-element-property :name element)))
  1264. output
  1265. (concat (format "\\phantomsection\n\\label{%s}\n"
  1266. (org-latex--label element info))
  1267. output)))
  1268. (defun org-latex--protect-text (text)
  1269. "Protect special characters in string TEXT and return it."
  1270. (replace-regexp-in-string
  1271. "--\\|[\\{}$%&_#~^]"
  1272. (lambda (m)
  1273. (cond ((equal m "--") "-{}-")
  1274. ((equal m "\\") "\\textbackslash{}")
  1275. ((equal m "~") "\\textasciitilde{}")
  1276. ((equal m "^") "\\textasciicircum{}")
  1277. (t (concat "\\" m))))
  1278. text nil t))
  1279. (defun org-latex--text-markup (text markup info)
  1280. "Format TEXT depending on MARKUP text markup.
  1281. INFO is a plist used as a communication channel. See
  1282. `org-latex-text-markup-alist' for details."
  1283. (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
  1284. (cl-case fmt
  1285. ;; No format string: Return raw text.
  1286. ((nil) text)
  1287. ;; Handle the `verb' special case: Find an appropriate separator
  1288. ;; and use "\\verb" command.
  1289. (verb
  1290. (let ((separator (org-latex--find-verb-separator text)))
  1291. (concat "\\verb" separator
  1292. (replace-regexp-in-string "\n" " " text)
  1293. separator)))
  1294. ;; Handle the `protectedtexttt' special case: Protect some
  1295. ;; special chars and use "\texttt{%s}" format string.
  1296. (protectedtexttt
  1297. (format "\\texttt{%s}" (org-latex--protect-text text)))
  1298. ;; Else use format string.
  1299. (t (format fmt text)))))
  1300. (defun org-latex--delayed-footnotes-definitions (element info)
  1301. "Return footnotes definitions in ELEMENT as a string.
  1302. INFO is a plist used as a communication channel.
  1303. Footnotes definitions are returned within \"\\footnotetext{}\"
  1304. commands.
  1305. This function is used within constructs that don't support
  1306. \"\\footnote{}\" command (e.g., an item tag). In that case,
  1307. \"\\footnotemark\" is used within the construct and the function
  1308. just outside of it."
  1309. (mapconcat
  1310. (lambda (ref)
  1311. (let ((def (org-export-get-footnote-definition ref info)))
  1312. (format "\\footnotetext[%d]{%s%s}"
  1313. (org-export-get-footnote-number ref info)
  1314. (org-trim (org-latex--label def info t t))
  1315. (org-trim (org-export-data def info)))))
  1316. ;; Find every footnote reference in ELEMENT.
  1317. (letrec ((all-refs nil)
  1318. (search-refs
  1319. (lambda (data)
  1320. ;; Return a list of all footnote references never seen
  1321. ;; before in DATA.
  1322. (org-element-map data 'footnote-reference
  1323. (lambda (ref)
  1324. (when (org-export-footnote-first-reference-p ref info)
  1325. (push ref all-refs)
  1326. (when (eq (org-element-property :type ref) 'standard)
  1327. (funcall search-refs
  1328. (org-export-get-footnote-definition ref info)))))
  1329. info)
  1330. (reverse all-refs))))
  1331. (funcall search-refs element))
  1332. ""))
  1333. (defun org-latex--translate (s info)
  1334. "Translate string S according to specified language.
  1335. INFO is a plist used as a communication channel."
  1336. (org-export-translate s :latex info))
  1337. (defun org-latex--format-spec (info)
  1338. "Create a format-spec for document meta-data.
  1339. INFO is a plist used as a communication channel."
  1340. (let ((language (let ((lang (plist-get info :language)))
  1341. (or (cdr (assoc-string lang org-latex-babel-language-alist t))
  1342. (nth 1 (assoc-string lang org-latex-polyglossia-language-alist t))
  1343. lang))))
  1344. `((?a . ,(org-export-data (plist-get info :author) info))
  1345. (?t . ,(org-export-data (plist-get info :title) info))
  1346. (?k . ,(org-export-data (org-latex--wrap-latex-math-block
  1347. (plist-get info :keywords) info)
  1348. info))
  1349. (?d . ,(org-export-data (org-latex--wrap-latex-math-block
  1350. (plist-get info :description) info)
  1351. info))
  1352. (?c . ,(plist-get info :creator))
  1353. (?l . ,language)
  1354. (?L . ,(capitalize language))
  1355. (?D . ,(org-export-get-date info)))))
  1356. (defun org-latex--make-preamble (info)
  1357. "Return a formatted LaTeX preamble.
  1358. INFO is a plist used as a communication channel."
  1359. (let* ((class (plist-get info :latex-class))
  1360. (class-options (plist-get info :latex-class-options))
  1361. (header (nth 1 (assoc class (plist-get info :latex-classes))))
  1362. (document-class-string
  1363. (and (stringp header)
  1364. (if (not class-options) header
  1365. (replace-regexp-in-string
  1366. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  1367. class-options header t nil 1)))))
  1368. (if (not document-class-string)
  1369. (user-error "Unknown LaTeX class `%s'" class)
  1370. (org-latex-guess-polyglossia-language
  1371. (org-latex-guess-babel-language
  1372. (org-latex-guess-inputenc
  1373. (org-element-normalize-string
  1374. (org-splice-latex-header
  1375. document-class-string
  1376. (org-latex--remove-packages
  1377. org-latex-default-packages-alist info)
  1378. (org-latex--remove-packages
  1379. org-latex-packages-alist info)
  1380. nil
  1381. (mapconcat 'org-element-normalize-string
  1382. (list (plist-get info :latex-header)
  1383. (plist-get info :latex-header-extra)) ""))))
  1384. info)
  1385. info))))
  1386. (defun org-latex--insert-compiler (info)
  1387. "Insert LaTeX_compiler info into the document.
  1388. INFO is a plist used as a communication channel."
  1389. (let ((compiler (plist-get info :latex-compiler)))
  1390. (and (org-string-nw-p org-latex-compiler-file-string)
  1391. (member (or compiler "") org-latex-compilers)
  1392. (format org-latex-compiler-file-string compiler))))
  1393. ;;; Template
  1394. (defun org-latex-template (contents info)
  1395. "Return complete document string after LaTeX conversion.
  1396. CONTENTS is the transcoded contents string. INFO is a plist
  1397. holding export options."
  1398. (let ((title (org-export-data (plist-get info :title) info))
  1399. (spec (org-latex--format-spec info)))
  1400. (concat
  1401. ;; Time-stamp.
  1402. (and (plist-get info :time-stamp-file)
  1403. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  1404. ;; LaTeX compiler.
  1405. (org-latex--insert-compiler info)
  1406. ;; Document class and packages.
  1407. (org-latex--make-preamble info)
  1408. ;; Possibly limit depth for headline numbering.
  1409. (let ((sec-num (plist-get info :section-numbers)))
  1410. (when (integerp sec-num)
  1411. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  1412. ;; Author.
  1413. (let ((author (and (plist-get info :with-author)
  1414. (let ((auth (plist-get info :author)))
  1415. (and auth (org-export-data auth info)))))
  1416. (email (and (plist-get info :with-email)
  1417. (org-export-data (plist-get info :email) info))))
  1418. (cond ((and author email (not (string= "" email)))
  1419. (format "\\author{%s\\thanks{%s}}\n" author email))
  1420. ((or author email) (format "\\author{%s}\n" (or author email)))))
  1421. ;; Date.
  1422. (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
  1423. (format "\\date{%s}\n" (org-export-data date info)))
  1424. ;; Title and subtitle.
  1425. (let* ((subtitle (plist-get info :subtitle))
  1426. (formatted-subtitle
  1427. (when subtitle
  1428. (format (plist-get info :latex-subtitle-format)
  1429. (org-export-data subtitle info))))
  1430. (separate (plist-get info :latex-subtitle-separate)))
  1431. (concat
  1432. (format "\\title{%s%s}\n" title
  1433. (if separate "" (or formatted-subtitle "")))
  1434. (when (and separate subtitle)
  1435. (concat formatted-subtitle "\n"))))
  1436. ;; Hyperref options.
  1437. (let ((template (plist-get info :latex-hyperref-template)))
  1438. (and (stringp template)
  1439. (format-spec template spec)))
  1440. ;; Document start.
  1441. "\\begin{document}\n\n"
  1442. ;; Title command.
  1443. (let* ((title-command (plist-get info :latex-title-command))
  1444. (command (and (stringp title-command)
  1445. (format-spec title-command spec))))
  1446. (org-element-normalize-string
  1447. (cond ((not (plist-get info :with-title)) nil)
  1448. ((string= "" title) nil)
  1449. ((not (stringp command)) nil)
  1450. ((string-match "\\(?:[^%]\\|^\\)%s" command)
  1451. (format command title))
  1452. (t command))))
  1453. ;; Table of contents.
  1454. (let ((depth (plist-get info :with-toc)))
  1455. (when depth
  1456. (concat (when (wholenump depth)
  1457. (format "\\setcounter{tocdepth}{%d}\n" depth))
  1458. (plist-get info :latex-toc-command))))
  1459. ;; Document's body.
  1460. contents
  1461. ;; Creator.
  1462. (and (plist-get info :with-creator)
  1463. (concat (plist-get info :creator) "\n"))
  1464. ;; Document end.
  1465. "\\end{document}")))
  1466. ;;; Transcode Functions
  1467. ;;;; Bold
  1468. (defun org-latex-bold (_bold contents info)
  1469. "Transcode BOLD from Org to LaTeX.
  1470. CONTENTS is the text with bold markup. INFO is a plist holding
  1471. contextual information."
  1472. (org-latex--text-markup contents 'bold info))
  1473. ;;;; Center Block
  1474. (defun org-latex-center-block (center-block contents info)
  1475. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  1476. CONTENTS holds the contents of the center block. INFO is a plist
  1477. holding contextual information."
  1478. (org-latex--wrap-label
  1479. center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
  1480. ;;;; Clock
  1481. (defun org-latex-clock (clock _contents info)
  1482. "Transcode a CLOCK element from Org to LaTeX.
  1483. CONTENTS is nil. INFO is a plist holding contextual
  1484. information."
  1485. (concat
  1486. "\\noindent"
  1487. (format "\\textbf{%s} " org-clock-string)
  1488. (format (plist-get info :latex-inactive-timestamp-format)
  1489. (concat (org-timestamp-translate (org-element-property :value clock))
  1490. (let ((time (org-element-property :duration clock)))
  1491. (and time (format " (%s)" time)))))
  1492. "\\\\"))
  1493. ;;;; Code
  1494. (defun org-latex-code (code _contents info)
  1495. "Transcode a CODE object from Org to LaTeX.
  1496. CONTENTS is nil. INFO is a plist used as a communication
  1497. channel."
  1498. (org-latex--text-markup (org-element-property :value code) 'code info))
  1499. ;;;; Drawer
  1500. (defun org-latex-drawer (drawer contents info)
  1501. "Transcode a DRAWER element from Org to LaTeX.
  1502. CONTENTS holds the contents of the block. INFO is a plist
  1503. holding contextual information."
  1504. (let* ((name (org-element-property :drawer-name drawer))
  1505. (output (funcall (plist-get info :latex-format-drawer-function)
  1506. name contents)))
  1507. (org-latex--wrap-label drawer output info)))
  1508. ;;;; Dynamic Block
  1509. (defun org-latex-dynamic-block (dynamic-block contents info)
  1510. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1511. CONTENTS holds the contents of the block. INFO is a plist
  1512. holding contextual information. See `org-export-data'."
  1513. (org-latex--wrap-label dynamic-block contents info))
  1514. ;;;; Entity
  1515. (defun org-latex-entity (entity _contents _info)
  1516. "Transcode an ENTITY object from Org to LaTeX.
  1517. CONTENTS are the definition itself. INFO is a plist holding
  1518. contextual information."
  1519. (org-element-property :latex entity))
  1520. ;;;; Example Block
  1521. (defun org-latex-example-block (example-block _contents info)
  1522. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1523. CONTENTS is nil. INFO is a plist holding contextual
  1524. information."
  1525. (when (org-string-nw-p (org-element-property :value example-block))
  1526. (let ((environment (or (org-export-read-attribute
  1527. :attr_latex example-block :environment)
  1528. "verbatim")))
  1529. (org-latex--wrap-label
  1530. example-block
  1531. (format "\\begin{%s}\n%s\\end{%s}"
  1532. environment
  1533. (org-export-format-code-default example-block info)
  1534. environment)
  1535. info))))
  1536. ;;;; Export Block
  1537. (defun org-latex-export-block (export-block _contents _info)
  1538. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1539. CONTENTS is nil. INFO is a plist holding contextual information."
  1540. (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
  1541. (org-remove-indentation (org-element-property :value export-block))))
  1542. ;;;; Export Snippet
  1543. (defun org-latex-export-snippet (export-snippet _contents _info)
  1544. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1545. CONTENTS is nil. INFO is a plist holding contextual information."
  1546. (when (eq (org-export-snippet-backend export-snippet) 'latex)
  1547. (org-element-property :value export-snippet)))
  1548. ;;;; Fixed Width
  1549. (defun org-latex-fixed-width (fixed-width _contents info)
  1550. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1551. CONTENTS is nil. INFO is a plist holding contextual information."
  1552. (org-latex--wrap-label
  1553. fixed-width
  1554. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1555. (org-remove-indentation
  1556. (org-element-property :value fixed-width)))
  1557. info))
  1558. ;;;; Footnote Reference
  1559. (defun org-latex-footnote-reference (footnote-reference _contents info)
  1560. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1561. CONTENTS is nil. INFO is a plist holding contextual information."
  1562. (let ((label (org-element-property :label footnote-reference)))
  1563. (concat
  1564. ;; Insert separator between two footnotes in a row.
  1565. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1566. (when (eq (org-element-type prev) 'footnote-reference)
  1567. (plist-get info :latex-footnote-separator)))
  1568. (cond
  1569. ;; Use `:latex-footnote-defined-format' if the footnote has
  1570. ;; already been defined.
  1571. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1572. (format (plist-get info :latex-footnote-defined-format)
  1573. (org-latex--label
  1574. (org-export-get-footnote-definition footnote-reference info)
  1575. info t)))
  1576. ;; Use \footnotemark if reference is within another footnote
  1577. ;; reference, footnote definition or table cell.
  1578. ((org-element-lineage footnote-reference
  1579. '(footnote-reference footnote-definition table-cell))
  1580. "\\footnotemark")
  1581. ;; Otherwise, define it with \footnote command.
  1582. (t
  1583. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1584. (concat
  1585. (format "\\footnote{%s%s}" (org-trim (org-export-data def info))
  1586. ;; Only insert a \label if there exist another
  1587. ;; reference to def.
  1588. (cond ((not label) "")
  1589. ((org-element-map (plist-get info :parse-tree) 'footnote-reference
  1590. (lambda (f)
  1591. (and (not (eq f footnote-reference))
  1592. (equal (org-element-property :label f) label)
  1593. (org-trim (org-latex--label def info t t))))
  1594. info t))
  1595. (t "")))
  1596. ;; Retrieve all footnote references within the footnote and
  1597. ;; add their definition after it, since LaTeX doesn't support
  1598. ;; them inside.
  1599. (org-latex--delayed-footnotes-definitions def info))))))))
  1600. ;;;; Headline
  1601. (defun org-latex-headline (headline contents info)
  1602. "Transcode a HEADLINE element from Org to LaTeX.
  1603. CONTENTS holds the contents of the headline. INFO is a plist
  1604. holding contextual information."
  1605. (unless (org-element-property :footnote-section-p headline)
  1606. (let* ((class (plist-get info :latex-class))
  1607. (level (org-export-get-relative-level headline info))
  1608. (numberedp (org-export-numbered-headline-p headline info))
  1609. (class-sectioning (assoc class (plist-get info :latex-classes)))
  1610. ;; Section formatting will set two placeholders: one for
  1611. ;; the title and the other for the contents.
  1612. (section-fmt
  1613. (let ((sec (if (functionp (nth 2 class-sectioning))
  1614. (funcall (nth 2 class-sectioning) level numberedp)
  1615. (nth (1+ level) class-sectioning))))
  1616. (cond
  1617. ;; No section available for that LEVEL.
  1618. ((not sec) nil)
  1619. ;; Section format directly returned by a function. Add
  1620. ;; placeholder for contents.
  1621. ((stringp sec) (concat sec "\n%s"))
  1622. ;; (numbered-section . unnumbered-section)
  1623. ((not (consp (cdr sec)))
  1624. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1625. ;; (numbered-open numbered-close)
  1626. ((= (length sec) 2)
  1627. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1628. ;; (num-in num-out no-num-in no-num-out)
  1629. ((= (length sec) 4)
  1630. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1631. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1632. ;; Create a temporary export back-end that hard-codes
  1633. ;; "\underline" within "\section" and alike.
  1634. (section-back-end
  1635. (org-export-create-backend
  1636. :parent 'latex
  1637. :transcoders
  1638. '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
  1639. (text
  1640. (org-export-data-with-backend
  1641. (org-element-property :title headline) section-back-end info))
  1642. (todo
  1643. (and (plist-get info :with-todo-keywords)
  1644. (let ((todo (org-element-property :todo-keyword headline)))
  1645. (and todo (org-export-data todo info)))))
  1646. (todo-type (and todo (org-element-property :todo-type headline)))
  1647. (tags (and (plist-get info :with-tags)
  1648. (org-export-get-tags headline info)))
  1649. (priority (and (plist-get info :with-priority)
  1650. (org-element-property :priority headline)))
  1651. ;; Create the headline text along with a no-tag version.
  1652. ;; The latter is required to remove tags from toc.
  1653. (full-text (funcall (plist-get info :latex-format-headline-function)
  1654. todo todo-type priority text tags info))
  1655. ;; Associate \label to the headline for internal links.
  1656. (headline-label (org-latex--label headline info t t))
  1657. (pre-blanks
  1658. (make-string (org-element-property :pre-blank headline) ?\n)))
  1659. (if (or (not section-fmt) (org-export-low-level-p headline info))
  1660. ;; This is a deep sub-tree: export it as a list item. Also
  1661. ;; export as items headlines for which no section format has
  1662. ;; been found.
  1663. (let ((low-level-body
  1664. (concat
  1665. ;; If headline is the first sibling, start a list.
  1666. (when (org-export-first-sibling-p headline info)
  1667. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1668. ;; Itemize headline
  1669. "\\item"
  1670. (and full-text
  1671. (string-match-p "\\`[ \t]*\\[" full-text)
  1672. "\\relax")
  1673. " " full-text "\n"
  1674. headline-label
  1675. pre-blanks
  1676. contents)))
  1677. ;; If headline is not the last sibling simply return
  1678. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
  1679. ;; any blank line.
  1680. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1681. (replace-regexp-in-string
  1682. "[ \t\n]*\\'"
  1683. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1684. low-level-body)))
  1685. ;; This is a standard headline. Export it as a section. Add
  1686. ;; an alternative heading when possible, and when this is not
  1687. ;; identical to the usual heading.
  1688. (let ((opt-title
  1689. (funcall (plist-get info :latex-format-headline-function)
  1690. todo todo-type priority
  1691. (org-export-data-with-backend
  1692. (org-export-get-alt-title headline info)
  1693. section-back-end info)
  1694. (and (eq (plist-get info :with-tags) t) tags)
  1695. info))
  1696. ;; Maybe end local TOC (see `org-latex-keyword').
  1697. (contents
  1698. (concat
  1699. contents
  1700. (let ((case-fold-search t)
  1701. (section
  1702. (let ((first (car (org-element-contents headline))))
  1703. (and (eq (org-element-type first) 'section) first))))
  1704. (org-element-map section 'keyword
  1705. (lambda (k)
  1706. (and (equal (org-element-property :key k) "TOC")
  1707. (let ((v (org-element-property :value k)))
  1708. (and (string-match-p "\\<headlines\\>" v)
  1709. (string-match-p "\\<local\\>" v)
  1710. (format "\\stopcontents[level-%d]" level)))))
  1711. info t)))))
  1712. (if (and opt-title
  1713. (not (equal opt-title full-text))
  1714. (string-match "\\`\\\\\\(.+?\\){" section-fmt))
  1715. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  1716. ;; Replace square brackets with parenthesis
  1717. ;; since square brackets are not supported in
  1718. ;; optional arguments.
  1719. (replace-regexp-in-string
  1720. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  1721. full-text
  1722. (concat headline-label pre-blanks contents))
  1723. ;; Impossible to add an alternative heading. Fallback to
  1724. ;; regular sectioning format string.
  1725. (format section-fmt full-text
  1726. (concat headline-label pre-blanks contents))))))))
  1727. (defun org-latex-format-headline-default-function
  1728. (todo _todo-type priority text tags info)
  1729. "Default format function for a headline.
  1730. See `org-latex-format-headline-function' for details."
  1731. (concat
  1732. (and todo (format "{\\bfseries\\sffamily %s} " todo))
  1733. (and priority (format "\\framebox{\\#%c} " priority))
  1734. text
  1735. (and tags
  1736. (format "\\hfill{}\\textsc{%s}"
  1737. (mapconcat (lambda (tag) (org-latex-plain-text tag info))
  1738. tags ":")))))
  1739. ;;;; Horizontal Rule
  1740. (defun org-latex-horizontal-rule (horizontal-rule _contents info)
  1741. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1742. CONTENTS is nil. INFO is a plist holding contextual information."
  1743. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1744. (prev (org-export-get-previous-element horizontal-rule info)))
  1745. (concat
  1746. ;; Make sure the rule doesn't start at the end of the current
  1747. ;; line by separating it with a blank line from previous element.
  1748. (when (and prev
  1749. (let ((prev-blank (org-element-property :post-blank prev)))
  1750. (or (not prev-blank) (zerop prev-blank))))
  1751. "\n")
  1752. (org-latex--wrap-label
  1753. horizontal-rule
  1754. (format "\\rule{%s}{%s}"
  1755. (or (plist-get attr :width) "\\linewidth")
  1756. (or (plist-get attr :thickness) "0.5pt"))
  1757. info))))
  1758. ;;;; Inline Src Block
  1759. (defun org-latex-inline-src-block (inline-src-block _contents info)
  1760. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1761. CONTENTS holds the contents of the item. INFO is a plist holding
  1762. contextual information."
  1763. (let* ((code (org-element-property :value inline-src-block))
  1764. (separator (org-latex--find-verb-separator code)))
  1765. (cl-case (plist-get info :latex-listings)
  1766. ;; Do not use a special package: transcode it verbatim.
  1767. ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
  1768. ;; Use minted package.
  1769. (minted
  1770. (let* ((org-lang (org-element-property :language inline-src-block))
  1771. (mint-lang (or (cadr (assq (intern org-lang)
  1772. (plist-get info :latex-minted-langs)))
  1773. (downcase org-lang)))
  1774. (options (org-latex--make-option-string
  1775. (plist-get info :latex-minted-options))))
  1776. (format "\\mintinline%s{%s}{%s}"
  1777. (if (string= options "") "" (format "[%s]" options))
  1778. mint-lang
  1779. code)))
  1780. ;; Use listings package.
  1781. (otherwise
  1782. ;; Maybe translate language's name.
  1783. (let* ((org-lang (org-element-property :language inline-src-block))
  1784. (lst-lang (or (cadr (assq (intern org-lang)
  1785. (plist-get info :latex-listings-langs)))
  1786. org-lang))
  1787. (options (org-latex--make-option-string
  1788. (append (plist-get info :latex-listings-options)
  1789. `(("language" ,lst-lang))))))
  1790. (concat (format "\\lstinline[%s]" options)
  1791. separator code separator))))))
  1792. ;;;; Inlinetask
  1793. (defun org-latex-inlinetask (inlinetask contents info)
  1794. "Transcode an INLINETASK element from Org to LaTeX.
  1795. CONTENTS holds the contents of the block. INFO is a plist
  1796. holding contextual information."
  1797. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1798. (todo (and (plist-get info :with-todo-keywords)
  1799. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1800. (and todo (org-export-data todo info)))))
  1801. (todo-type (org-element-property :todo-type inlinetask))
  1802. (tags (and (plist-get info :with-tags)
  1803. (org-export-get-tags inlinetask info)))
  1804. (priority (and (plist-get info :with-priority)
  1805. (org-element-property :priority inlinetask)))
  1806. (contents (concat (org-latex--label inlinetask info) contents)))
  1807. (funcall (plist-get info :latex-format-inlinetask-function)
  1808. todo todo-type priority title tags contents info)))
  1809. (defun org-latex-format-inlinetask-default-function
  1810. (todo _todo-type priority title tags contents info)
  1811. "Default format function for a inlinetasks.
  1812. See `org-latex-format-inlinetask-function' for details."
  1813. (let ((full-title
  1814. (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1815. (when priority (format "\\framebox{\\#%c} " priority))
  1816. title
  1817. (when tags
  1818. (format "\\hfill{}\\textsc{:%s:}"
  1819. (mapconcat
  1820. (lambda (tag) (org-latex-plain-text tag info))
  1821. tags ":"))))))
  1822. (concat "\\begin{center}\n"
  1823. "\\fbox{\n"
  1824. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1825. full-title "\n\n"
  1826. (and (org-string-nw-p contents)
  1827. (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
  1828. "\\end{minipage}\n"
  1829. "}\n"
  1830. "\\end{center}")))
  1831. ;;;; Italic
  1832. (defun org-latex-italic (_italic contents info)
  1833. "Transcode ITALIC from Org to LaTeX.
  1834. CONTENTS is the text with italic markup. INFO is a plist holding
  1835. contextual information."
  1836. (org-latex--text-markup contents 'italic info))
  1837. ;;;; Item
  1838. (defun org-latex-item (item contents info)
  1839. "Transcode an ITEM element from Org to LaTeX.
  1840. CONTENTS holds the contents of the item. INFO is a plist holding
  1841. contextual information."
  1842. (let* ((counter
  1843. (let ((count (org-element-property :counter item))
  1844. (level
  1845. ;; Determine level of current item to determine the
  1846. ;; correct LaTeX counter to use (enumi, enumii...).
  1847. (let ((parent item) (level 0))
  1848. (while (memq (org-element-type
  1849. (setq parent (org-export-get-parent parent)))
  1850. '(plain-list item))
  1851. (when (and (eq (org-element-type parent) 'plain-list)
  1852. (eq (org-element-property :type parent)
  1853. 'ordered))
  1854. (cl-incf level)))
  1855. level)))
  1856. (and count
  1857. (< level 5)
  1858. (format "\\setcounter{enum%s}{%s}\n"
  1859. (nth (1- level) '("i" "ii" "iii" "iv"))
  1860. (1- count)))))
  1861. (checkbox (cl-case (org-element-property :checkbox item)
  1862. (on "$\\boxtimes$ ")
  1863. (off "$\\square$ ")
  1864. (trans "$\\boxminus$ ")))
  1865. (tag (let ((tag (org-element-property :tag item)))
  1866. ;; Check-boxes must belong to the tag.
  1867. (and tag (format "[{%s}] "
  1868. (concat checkbox
  1869. (org-export-data tag info)))))))
  1870. (concat counter
  1871. "\\item"
  1872. (cond
  1873. (tag)
  1874. (checkbox (concat " " checkbox))
  1875. ;; Without a tag or a check-box, if CONTENTS starts with
  1876. ;; an opening square bracket, add "\relax" to "\item",
  1877. ;; unless the brackets comes from an initial export
  1878. ;; snippet (i.e. it is inserted willingly by the user).
  1879. ((and contents
  1880. (string-match-p "\\`[ \t]*\\[" contents)
  1881. (not (let ((e (car (org-element-contents item))))
  1882. (and (eq (org-element-type e) 'paragraph)
  1883. (let ((o (car (org-element-contents e))))
  1884. (and (eq (org-element-type o) 'export-snippet)
  1885. (eq (org-export-snippet-backend o)
  1886. 'latex)))))))
  1887. "\\relax ")
  1888. (t " "))
  1889. (and contents (org-trim contents))
  1890. ;; If there are footnotes references in tag, be sure to
  1891. ;; add their definition at the end of the item. This
  1892. ;; workaround is necessary since "\footnote{}" command is
  1893. ;; not supported in tags.
  1894. (and tag
  1895. (org-latex--delayed-footnotes-definitions
  1896. (org-element-property :tag item) info)))))
  1897. ;;;; Keyword
  1898. (defun org-latex-keyword (keyword _contents info)
  1899. "Transcode a KEYWORD element from Org to LaTeX.
  1900. CONTENTS is nil. INFO is a plist holding contextual information."
  1901. (let ((key (org-element-property :key keyword))
  1902. (value (org-element-property :value keyword)))
  1903. (cond
  1904. ((string= key "LATEX") value)
  1905. ((string= key "INDEX") (format "\\index{%s}" value))
  1906. ((string= key "TOC")
  1907. (let ((case-fold-search t))
  1908. (cond
  1909. ((string-match-p "\\<headlines\\>" value)
  1910. (let* ((localp (string-match-p "\\<local\\>" value))
  1911. (parent (org-element-lineage keyword '(headline)))
  1912. (level (if (not (and localp parent)) 0
  1913. (org-export-get-relative-level parent info)))
  1914. (depth
  1915. (and (string-match "\\<[0-9]+\\>" value)
  1916. (format
  1917. "\\setcounter{tocdepth}{%d}"
  1918. (+ (string-to-number (match-string 0 value)) level)))))
  1919. (if (and localp parent)
  1920. ;; Start local TOC, assuming package "titletoc" is
  1921. ;; required.
  1922. (format "\\startcontents[level-%d]
  1923. \\printcontents[level-%d]{}{0}{%s}"
  1924. level level (or depth ""))
  1925. (concat depth (and depth "\n") "\\tableofcontents"))))
  1926. ((string-match-p "\\<tables\\>" value) "\\listoftables")
  1927. ((string-match-p "\\<listings\\>" value)
  1928. (cl-case (plist-get info :latex-listings)
  1929. ((nil) "\\listoffigures")
  1930. (minted "\\listoflistings")
  1931. (otherwise "\\lstlistoflistings")))))))))
  1932. ;;;; Latex Environment
  1933. (defun org-latex-latex-environment (latex-environment _contents info)
  1934. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1935. CONTENTS is nil. INFO is a plist holding contextual information."
  1936. (when (plist-get info :with-latex)
  1937. (let ((value (org-remove-indentation
  1938. (org-element-property :value latex-environment))))
  1939. (if (not (org-element-property :name latex-environment)) value
  1940. ;; Environment is labeled: label must be within the environment
  1941. ;; (otherwise, a reference pointing to that element will count
  1942. ;; the section instead).
  1943. (with-temp-buffer
  1944. (insert value)
  1945. (goto-char (point-min))
  1946. (forward-line)
  1947. (insert (org-latex--label latex-environment info nil t))
  1948. (buffer-string))))))
  1949. ;;;; Latex Fragment
  1950. (defun org-latex-latex-fragment (latex-fragment _contents _info)
  1951. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1952. CONTENTS is nil. INFO is a plist holding contextual information."
  1953. (let ((value (org-element-property :value latex-fragment)))
  1954. ;; Trim math markers since the fragment is enclosed within
  1955. ;; a latex-math-block object anyway.
  1956. (cond ((string-match-p "\\`\\$[^$]" value) (substring value 1 -1))
  1957. ((string-prefix-p "\\(" value) (substring value 2 -2))
  1958. (t value))))
  1959. ;;;; Line Break
  1960. (defun org-latex-line-break (_line-break _contents _info)
  1961. "Transcode a LINE-BREAK object from Org to LaTeX.
  1962. CONTENTS is nil. INFO is a plist holding contextual information."
  1963. "\\\\\n")
  1964. ;;;; Link
  1965. (defun org-latex--inline-image (link info)
  1966. "Return LaTeX code for an inline image.
  1967. LINK is the link pointing to the inline image. INFO is a plist
  1968. used as a communication channel."
  1969. (let* ((parent (org-export-get-parent-element link))
  1970. (path (let ((raw-path (org-element-property :path link)))
  1971. (if (not (file-name-absolute-p raw-path)) raw-path
  1972. (expand-file-name raw-path))))
  1973. (filetype (file-name-extension path))
  1974. (caption (org-latex--caption/label-string parent info))
  1975. (caption-above-p (org-latex--caption-above-p link info))
  1976. ;; Retrieve latex attributes from the element around.
  1977. (attr (org-export-read-attribute :attr_latex parent))
  1978. (float (let ((float (plist-get attr :float)))
  1979. (cond ((string= float "wrap") 'wrap)
  1980. ((string= float "sideways") 'sideways)
  1981. ((string= float "multicolumn") 'multicolumn)
  1982. ((and (plist-member attr :float) (not float)) 'nonfloat)
  1983. ((or float
  1984. (org-element-property :caption parent)
  1985. (org-string-nw-p (plist-get attr :caption)))
  1986. 'figure)
  1987. (t 'nonfloat))))
  1988. (placement
  1989. (let ((place (plist-get attr :placement)))
  1990. (cond
  1991. (place (format "%s" place))
  1992. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  1993. ((eq float 'figure)
  1994. (format "[%s]" (plist-get info :latex-default-figure-position)))
  1995. (t ""))))
  1996. (center
  1997. (if (plist-member attr :center) (plist-get attr :center)
  1998. (plist-get info :latex-images-centered)))
  1999. (comment-include (if (plist-get attr :comment-include) "%" ""))
  2000. ;; It is possible to specify width and height in the
  2001. ;; ATTR_LATEX line, and also via default variables.
  2002. (width (cond ((plist-get attr :width))
  2003. ((plist-get attr :height) "")
  2004. ((eq float 'wrap) "0.48\\textwidth")
  2005. (t (plist-get info :latex-image-default-width))))
  2006. (height (cond ((plist-get attr :height))
  2007. ((or (plist-get attr :width)
  2008. (memq float '(figure wrap))) "")
  2009. (t (plist-get info :latex-image-default-height))))
  2010. (options (let ((opt (or (plist-get attr :options)
  2011. (plist-get info :latex-image-default-option))))
  2012. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  2013. (match-string 1 opt))))
  2014. image-code)
  2015. (if (member filetype '("tikz" "pgf"))
  2016. ;; For tikz images:
  2017. ;; - use \input to read in image file.
  2018. ;; - if options are present, wrap in a tikzpicture environment.
  2019. ;; - if width or height are present, use \resizebox to change
  2020. ;; the image size.
  2021. (progn
  2022. (setq image-code (format "\\input{%s}" path))
  2023. (when (org-string-nw-p options)
  2024. (setq image-code
  2025. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  2026. options
  2027. image-code)))
  2028. (when (or (org-string-nw-p width) (org-string-nw-p height))
  2029. (setq image-code (format "\\resizebox{%s}{%s}{%s}"
  2030. (if (org-string-nw-p width) width "!")
  2031. (if (org-string-nw-p height) height "!")
  2032. image-code))))
  2033. ;; For other images:
  2034. ;; - add width and height to options.
  2035. ;; - include the image with \includegraphics.
  2036. (when (org-string-nw-p width)
  2037. (setq options (concat options ",width=" width)))
  2038. (when (org-string-nw-p height)
  2039. (setq options (concat options ",height=" height)))
  2040. (let ((search-option (org-element-property :search-option link)))
  2041. (when (and search-option
  2042. (equal filetype "pdf")
  2043. (string-match-p "\\`[0-9]+\\'" search-option)
  2044. (not (string-match-p "page=" options)))
  2045. (setq options (concat options ",page=" search-option))))
  2046. (setq image-code
  2047. (format "\\includegraphics%s{%s}"
  2048. (cond ((not (org-string-nw-p options)) "")
  2049. ((string-prefix-p "," options)
  2050. (format "[%s]" (substring options 1)))
  2051. (t (format "[%s]" options)))
  2052. path))
  2053. (when (equal filetype "svg")
  2054. (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
  2055. "\\includesvg"
  2056. image-code
  2057. nil t))
  2058. (setq image-code (replace-regexp-in-string "\\.svg}"
  2059. "}"
  2060. image-code
  2061. nil t))))
  2062. ;; Return proper string, depending on FLOAT.
  2063. (pcase float
  2064. (`wrap (format "\\begin{wrapfigure}%s
  2065. %s%s
  2066. %s%s
  2067. %s\\end{wrapfigure}"
  2068. placement
  2069. (if caption-above-p caption "")
  2070. (if center "\\centering" "")
  2071. comment-include image-code
  2072. (if caption-above-p "" caption)))
  2073. (`sideways (format "\\begin{sidewaysfigure}
  2074. %s%s
  2075. %s%s
  2076. %s\\end{sidewaysfigure}"
  2077. (if caption-above-p caption "")
  2078. (if center "\\centering" "")
  2079. comment-include image-code
  2080. (if caption-above-p "" caption)))
  2081. (`multicolumn (format "\\begin{figure*}%s
  2082. %s%s
  2083. %s%s
  2084. %s\\end{figure*}"
  2085. placement
  2086. (if caption-above-p caption "")
  2087. (if center "\\centering" "")
  2088. comment-include image-code
  2089. (if caption-above-p "" caption)))
  2090. (`figure (format "\\begin{figure}%s
  2091. %s%s
  2092. %s%s
  2093. %s\\end{figure}"
  2094. placement
  2095. (if caption-above-p caption "")
  2096. (if center "\\centering" "")
  2097. comment-include image-code
  2098. (if caption-above-p "" caption)))
  2099. ((guard center)
  2100. (format "\\begin{center}
  2101. %s%s
  2102. %s\\end{center}"
  2103. (if caption-above-p caption "")
  2104. image-code
  2105. (if caption-above-p "" caption)))
  2106. (_
  2107. (concat (if caption-above-p caption "")
  2108. image-code
  2109. (if caption-above-p caption ""))))))
  2110. (defun org-latex-link (link desc info)
  2111. "Transcode a LINK object from Org to LaTeX.
  2112. DESC is the description part of the link, or the empty string.
  2113. INFO is a plist holding contextual information. See
  2114. `org-export-data'."
  2115. (let* ((type (org-element-property :type link))
  2116. (raw-path (replace-regexp-in-string
  2117. "%" "\\%" (org-element-property :path link) nil t))
  2118. ;; Ensure DESC really exists, or set it to nil.
  2119. (desc (and (not (string= desc "")) desc))
  2120. (imagep (org-export-inline-image-p
  2121. link (plist-get info :latex-inline-image-rules)))
  2122. (path (cond
  2123. ((member type '("http" "https" "ftp" "mailto" "doi"))
  2124. (concat type ":" raw-path))
  2125. ((string= type "file") (org-export-file-uri raw-path))
  2126. (t raw-path))))
  2127. (cond
  2128. ;; Link type is handled by a special function.
  2129. ((org-export-custom-protocol-maybe link desc 'latex))
  2130. ;; Image file.
  2131. (imagep (org-latex--inline-image link info))
  2132. ;; Radio link: Transcode target's contents and use them as link's
  2133. ;; description.
  2134. ((string= type "radio")
  2135. (let ((destination (org-export-resolve-radio-link link info)))
  2136. (if (not destination) desc
  2137. (format "\\hyperref[%s]{%s}"
  2138. (org-export-get-reference destination info)
  2139. desc))))
  2140. ;; Links pointing to a headline: Find destination and build
  2141. ;; appropriate referencing command.
  2142. ((member type '("custom-id" "fuzzy" "id"))
  2143. (let ((destination (if (string= type "fuzzy")
  2144. (org-export-resolve-fuzzy-link link info)
  2145. (org-export-resolve-id-link link info))))
  2146. (cl-case (org-element-type destination)
  2147. ;; Id link points to an external file.
  2148. (plain-text
  2149. (if desc (format "\\href{%s}{%s}" destination desc)
  2150. (format "\\url{%s}" destination)))
  2151. ;; Fuzzy link points nowhere.
  2152. ((nil)
  2153. (format (plist-get info :latex-link-with-unknown-path-format)
  2154. (or desc
  2155. (org-export-data
  2156. (org-element-property :raw-link link) info))))
  2157. ;; LINK points to a headline. If headlines are numbered
  2158. ;; and the link has no description, display headline's
  2159. ;; number. Otherwise, display description or headline's
  2160. ;; title.
  2161. (headline
  2162. (let ((label (org-latex--label destination info t)))
  2163. (if (and (not desc)
  2164. (org-export-numbered-headline-p destination info))
  2165. (format "\\ref{%s}" label)
  2166. (format "\\hyperref[%s]{%s}" label
  2167. (or desc
  2168. (org-export-data
  2169. (org-element-property :title destination) info))))))
  2170. ;; Fuzzy link points to a target. Do as above.
  2171. (otherwise
  2172. (let ((ref (org-latex--label destination info t)))
  2173. (if (not desc) (format "\\ref{%s}" ref)
  2174. (format "\\hyperref[%s]{%s}" ref desc)))))))
  2175. ;; Coderef: replace link with the reference name or the
  2176. ;; equivalent line number.
  2177. ((string= type "coderef")
  2178. (format (org-export-get-coderef-format path desc)
  2179. (org-export-resolve-coderef path info)))
  2180. ;; External link with a description part.
  2181. ((and path desc) (format "\\href{%s}{%s}" path desc))
  2182. ;; External link without a description part.
  2183. (path (format "\\url{%s}" path))
  2184. ;; No path, only description. Try to do something useful.
  2185. (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
  2186. ;;;; Node Property
  2187. (defun org-latex-node-property (node-property _contents _info)
  2188. "Transcode a NODE-PROPERTY element from Org to LaTeX.
  2189. CONTENTS is nil. INFO is a plist holding contextual
  2190. information."
  2191. (format "%s:%s"
  2192. (org-element-property :key node-property)
  2193. (let ((value (org-element-property :value node-property)))
  2194. (if value (concat " " value) ""))))
  2195. ;;;; Paragraph
  2196. (defun org-latex-paragraph (_paragraph contents _info)
  2197. "Transcode a PARAGRAPH element from Org to LaTeX.
  2198. CONTENTS is the contents of the paragraph, as a string. INFO is
  2199. the plist used as a communication channel."
  2200. contents)
  2201. ;;;; Plain List
  2202. (defun org-latex-plain-list (plain-list contents info)
  2203. "Transcode a PLAIN-LIST element from Org to LaTeX.
  2204. CONTENTS is the contents of the list. INFO is a plist holding
  2205. contextual information."
  2206. (let* ((type (org-element-property :type plain-list))
  2207. (attr (org-export-read-attribute :attr_latex plain-list))
  2208. (latex-type (let ((env (plist-get attr :environment)))
  2209. (cond (env (format "%s" env))
  2210. ((eq type 'ordered) "enumerate")
  2211. ((eq type 'descriptive) "description")
  2212. (t "itemize")))))
  2213. (org-latex--wrap-label
  2214. plain-list
  2215. (format "\\begin{%s}%s\n%s\\end{%s}"
  2216. latex-type
  2217. (or (plist-get attr :options) "")
  2218. contents
  2219. latex-type)
  2220. info)))
  2221. ;;;; Plain Text
  2222. (defun org-latex-plain-text (text info)
  2223. "Transcode a TEXT string from Org to LaTeX.
  2224. TEXT is the string to transcode. INFO is a plist holding
  2225. contextual information."
  2226. (let* ((specialp (plist-get info :with-special-strings))
  2227. (output
  2228. ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
  2229. (let ((case-fold-search nil))
  2230. (replace-regexp-in-string
  2231. "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
  2232. ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
  2233. ;; However, if special strings are used, be careful not
  2234. ;; to protect "\" in "\-" constructs.
  2235. (replace-regexp-in-string
  2236. (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
  2237. (lambda (m)
  2238. (cl-case (string-to-char m)
  2239. (?\\ "$\\\\backslash$\\1")
  2240. (?~ "\\\\textasciitilde{}")
  2241. (?^ "\\\\^{}")
  2242. (t "\\\\\\&")))
  2243. text)))))
  2244. ;; Activate smart quotes. Be sure to provide original TEXT string
  2245. ;; since OUTPUT may have been modified.
  2246. (when (plist-get info :with-smart-quotes)
  2247. (setq output (org-export-activate-smart-quotes output :latex info text)))
  2248. ;; Convert special strings.
  2249. (when specialp
  2250. (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
  2251. ;; Handle break preservation if required.
  2252. (when (plist-get info :preserve-breaks)
  2253. (setq output (replace-regexp-in-string
  2254. "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
  2255. ;; Return value.
  2256. output))
  2257. ;;;; Planning
  2258. (defun org-latex-planning (planning _contents info)
  2259. "Transcode a PLANNING element from Org to LaTeX.
  2260. CONTENTS is nil. INFO is a plist holding contextual
  2261. information."
  2262. (concat
  2263. "\\noindent"
  2264. (mapconcat
  2265. 'identity
  2266. (delq nil
  2267. (list
  2268. (let ((closed (org-element-property :closed planning)))
  2269. (when closed
  2270. (concat
  2271. (format "\\textbf{%s} " org-closed-string)
  2272. (format (plist-get info :latex-inactive-timestamp-format)
  2273. (org-timestamp-translate closed)))))
  2274. (let ((deadline (org-element-property :deadline planning)))
  2275. (when deadline
  2276. (concat
  2277. (format "\\textbf{%s} " org-deadline-string)
  2278. (format (plist-get info :latex-active-timestamp-format)
  2279. (org-timestamp-translate deadline)))))
  2280. (let ((scheduled (org-element-property :scheduled planning)))
  2281. (when scheduled
  2282. (concat
  2283. (format "\\textbf{%s} " org-scheduled-string)
  2284. (format (plist-get info :latex-active-timestamp-format)
  2285. (org-timestamp-translate scheduled)))))))
  2286. " ")
  2287. "\\\\"))
  2288. ;;;; Property Drawer
  2289. (defun org-latex-property-drawer (_property-drawer contents _info)
  2290. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  2291. CONTENTS holds the contents of the drawer. INFO is a plist
  2292. holding contextual information."
  2293. (and (org-string-nw-p contents)
  2294. (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
  2295. ;;;; Pseudo Element: LaTeX Matrices
  2296. ;; `latex-matrices' elements have the following properties:
  2297. ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
  2298. ;; `math').
  2299. (defun org-latex--wrap-latex-matrices (data info)
  2300. "Merge contiguous tables with the same mode within a pseudo-element.
  2301. DATA is a parse tree or a secondary string. INFO is a plist
  2302. containing export options. Modify DATA by side-effect and return
  2303. it."
  2304. (org-element-map data 'table
  2305. (lambda (table)
  2306. (when (eq (org-element-property :type table) 'org)
  2307. (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
  2308. (plist-get info :latex-default-table-mode))))
  2309. (when (and (member mode '("inline-math" "math"))
  2310. ;; Do not wrap twice the same table.
  2311. (not (eq (org-element-type
  2312. (org-element-property :parent table))
  2313. 'latex-matrices)))
  2314. (let* ((caption (and (not (string= mode "inline-math"))
  2315. (org-element-property :caption table)))
  2316. (matrices
  2317. (list 'latex-matrices
  2318. (list :caption caption
  2319. :markup
  2320. (cond ((string= mode "inline-math") 'inline)
  2321. (caption 'equation)
  2322. (t 'math)))))
  2323. (previous table)
  2324. (next (org-export-get-next-element table info)))
  2325. (org-element-insert-before matrices table)
  2326. ;; Swallow all contiguous tables sharing the same mode.
  2327. (while (and
  2328. (zerop (or (org-element-property :post-blank previous) 0))
  2329. (setq next (org-export-get-next-element previous info))
  2330. (eq (org-element-type next) 'table)
  2331. (eq (org-element-property :type next) 'org)
  2332. (string= (or (org-export-read-attribute
  2333. :attr_latex next :mode)
  2334. (plist-get info :latex-default-table-mode))
  2335. mode))
  2336. (org-element-extract-element previous)
  2337. (org-element-adopt-elements matrices previous)
  2338. (setq previous next))
  2339. (org-element-put-property
  2340. matrices :post-blank (org-element-property :post-blank previous))
  2341. (org-element-extract-element previous)
  2342. (org-element-adopt-elements matrices previous))))))
  2343. info)
  2344. data)
  2345. (defun org-latex-matrices (matrices contents _info)
  2346. "Transcode a MATRICES element from Org to LaTeX.
  2347. CONTENTS is a string. INFO is a plist used as a communication
  2348. channel."
  2349. (format (cl-case (org-element-property :markup matrices)
  2350. (inline "\\(%s\\)")
  2351. (equation "\\begin{equation}\n%s\\end{equation}")
  2352. (t "\\[\n%s\\]"))
  2353. contents))
  2354. (defun org-latex-matrices-tree-filter (tree _backend info)
  2355. (org-latex--wrap-latex-matrices tree info))
  2356. ;;;; Pseudo Object: LaTeX Math Block
  2357. ;; `latex-math-block' objects have the following property:
  2358. ;; `:post-blank'.
  2359. (defun org-latex--wrap-latex-math-block (data info)
  2360. "Merge contiguous math objects in a pseudo-object container.
  2361. DATA is a parse tree or a secondary string. INFO is a plist
  2362. containing export options. Modify DATA by side-effect and return it."
  2363. (let ((valid-object-p
  2364. ;; Non-nil when OBJ can be added to the latex math block.
  2365. (lambda (obj)
  2366. (pcase (org-element-type obj)
  2367. (`entity (org-element-property :latex-math-p obj))
  2368. (`latex-fragment
  2369. (let ((value (org-element-property :value obj)))
  2370. (or (string-prefix-p "\\(" value)
  2371. (string-match-p "\\`\\$[^$]" value))))
  2372. ((or `subscript `superscript) t)))))
  2373. (org-element-map data '(entity latex-fragment subscript superscript)
  2374. (lambda (object)
  2375. ;; Skip objects already wrapped.
  2376. (when (and (not (eq (org-element-type
  2377. (org-element-property :parent object))
  2378. 'latex-math-block))
  2379. (funcall valid-object-p object))
  2380. (let ((math-block (list 'latex-math-block nil))
  2381. (next-elements (org-export-get-next-element object info t))
  2382. (last object))
  2383. ;; Wrap MATH-BLOCK around OBJECT in DATA.
  2384. (org-element-insert-before math-block object)
  2385. (org-element-extract-element object)
  2386. (org-element-adopt-elements math-block object)
  2387. (when (zerop (or (org-element-property :post-blank object) 0))
  2388. ;; MATH-BLOCK swallows consecutive math objects.
  2389. (catch 'exit
  2390. (dolist (next next-elements)
  2391. (if (not (funcall valid-object-p next)) (throw 'exit nil)
  2392. (org-element-extract-element next)
  2393. (org-element-adopt-elements math-block next)
  2394. ;; Eschew the case: \beta$x$ -> \(\betax\).
  2395. (unless (memq (org-element-type next)
  2396. '(subscript superscript))
  2397. (org-element-put-property last :post-blank 1))
  2398. (setq last next)
  2399. (when (> (or (org-element-property :post-blank next) 0) 0)
  2400. (throw 'exit nil))))))
  2401. (org-element-put-property
  2402. math-block :post-blank (org-element-property :post-blank last)))))
  2403. info nil '(subscript superscript latex-math-block) t)
  2404. ;; Return updated DATA.
  2405. data))
  2406. (defun org-latex-math-block-tree-filter (tree _backend info)
  2407. (org-latex--wrap-latex-math-block tree info))
  2408. (defun org-latex-math-block-options-filter (info _backend)
  2409. (dolist (prop '(:author :date :title) info)
  2410. (plist-put info prop
  2411. (org-latex--wrap-latex-math-block (plist-get info prop) info))))
  2412. (defun org-latex-math-block (_math-block contents _info)
  2413. "Transcode a MATH-BLOCK object from Org to LaTeX.
  2414. CONTENTS is a string. INFO is a plist used as a communication
  2415. channel."
  2416. (when (org-string-nw-p contents)
  2417. (format "\\(%s\\)" (org-trim contents))))
  2418. ;;;; Quote Block
  2419. (defun org-latex-quote-block (quote-block contents info)
  2420. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  2421. CONTENTS holds the contents of the block. INFO is a plist
  2422. holding contextual information."
  2423. (org-latex--wrap-label
  2424. quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
  2425. ;;;; Radio Target
  2426. (defun org-latex-radio-target (radio-target text info)
  2427. "Transcode a RADIO-TARGET object from Org to LaTeX.
  2428. TEXT is the text of the target. INFO is a plist holding
  2429. contextual information."
  2430. (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
  2431. ;;;; Section
  2432. (defun org-latex-section (_section contents _info)
  2433. "Transcode a SECTION element from Org to LaTeX.
  2434. CONTENTS holds the contents of the section. INFO is a plist
  2435. holding contextual information."
  2436. contents)
  2437. ;;;; Special Block
  2438. (defun org-latex-special-block (special-block contents info)
  2439. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  2440. CONTENTS holds the contents of the block. INFO is a plist
  2441. holding contextual information."
  2442. (let ((type (org-element-property :type special-block))
  2443. (opt (org-export-read-attribute :attr_latex special-block :options))
  2444. (caption (org-latex--caption/label-string special-block info))
  2445. (caption-above-p (org-latex--caption-above-p special-block info)))
  2446. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  2447. (and caption-above-p caption)
  2448. contents
  2449. (and (not caption-above-p) caption)
  2450. (format "\\end{%s}" type))))
  2451. ;;;; Src Block
  2452. (defun org-latex-src-block (src-block _contents info)
  2453. "Transcode a SRC-BLOCK element from Org to LaTeX.
  2454. CONTENTS holds the contents of the item. INFO is a plist holding
  2455. contextual information."
  2456. (when (org-string-nw-p (org-element-property :value src-block))
  2457. (let* ((lang (org-element-property :language src-block))
  2458. (caption (org-element-property :caption src-block))
  2459. (caption-above-p (org-latex--caption-above-p src-block info))
  2460. (label (org-element-property :name src-block))
  2461. (custom-env (and lang
  2462. (cadr (assq (intern lang)
  2463. org-latex-custom-lang-environments))))
  2464. (num-start (org-export-get-loc src-block info))
  2465. (retain-labels (org-element-property :retain-labels src-block))
  2466. (attributes (org-export-read-attribute :attr_latex src-block))
  2467. (float (plist-get attributes :float))
  2468. (listings (plist-get info :latex-listings)))
  2469. (cond
  2470. ;; Case 1. No source fontification.
  2471. ((not listings)
  2472. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2473. (float-env
  2474. (cond ((string= "multicolumn" float)
  2475. (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
  2476. (plist-get info :latex-default-figure-position)
  2477. (if caption-above-p caption-str "")
  2478. (if caption-above-p "" caption-str)))
  2479. (caption (concat
  2480. (if caption-above-p caption-str "")
  2481. "%s"
  2482. (if caption-above-p "" (concat "\n" caption-str))))
  2483. (t "%s"))))
  2484. (format
  2485. float-env
  2486. (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
  2487. (org-export-format-code-default src-block info))))))
  2488. ;; Case 2. Custom environment.
  2489. (custom-env
  2490. (let ((caption-str (org-latex--caption/label-string src-block info))
  2491. (formatted-src (org-export-format-code-default src-block info)))
  2492. (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
  2493. (format "\\begin{%s}\n%s\\end{%s}\n"
  2494. custom-env
  2495. (concat (and caption-above-p caption-str)
  2496. formatted-src
  2497. (and (not caption-above-p) caption-str))
  2498. custom-env)
  2499. (format-spec custom-env
  2500. `((?s . ,formatted-src)
  2501. (?c . ,caption)
  2502. (?f . ,float)
  2503. (?l . ,(org-latex--label src-block info))
  2504. (?o . ,(or (plist-get attributes :options) "")))))))
  2505. ;; Case 3. Use minted package.
  2506. ((eq listings 'minted)
  2507. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2508. (float-env
  2509. (cond
  2510. ((string= "multicolumn" float)
  2511. (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
  2512. (if caption-above-p caption-str "")
  2513. (if caption-above-p "" caption-str)))
  2514. (caption
  2515. (concat (if caption-above-p caption-str "")
  2516. "%s"
  2517. (if caption-above-p "" (concat "\n" caption-str))))
  2518. (t "%s")))
  2519. (options (plist-get info :latex-minted-options))
  2520. (body
  2521. (format
  2522. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  2523. ;; Options.
  2524. (concat
  2525. (org-latex--make-option-string
  2526. (if (or (not num-start) (assoc "linenos" options))
  2527. options
  2528. (append
  2529. `(("linenos")
  2530. ("firstnumber" ,(number-to-string (1+ num-start))))
  2531. options)))
  2532. (let ((local-options (plist-get attributes :options)))
  2533. (and local-options (concat "," local-options))))
  2534. ;; Language.
  2535. (or (cadr (assq (intern lang)
  2536. (plist-get info :latex-minted-langs)))
  2537. (downcase lang))
  2538. ;; Source code.
  2539. (let* ((code-info (org-export-unravel-code src-block))
  2540. (max-width
  2541. (apply 'max
  2542. (mapcar 'length
  2543. (org-split-string (car code-info)
  2544. "\n")))))
  2545. (org-export-format-code
  2546. (car code-info)
  2547. (lambda (loc _num ref)
  2548. (concat
  2549. loc
  2550. (when ref
  2551. ;; Ensure references are flushed to the right,
  2552. ;; separated with 6 spaces from the widest line
  2553. ;; of code.
  2554. (concat (make-string (+ (- max-width (length loc)) 6)
  2555. ?\s)
  2556. (format "(%s)" ref)))))
  2557. nil (and retain-labels (cdr code-info)))))))
  2558. ;; Return value.
  2559. (format float-env body)))
  2560. ;; Case 4. Use listings package.
  2561. (t
  2562. (let ((lst-lang
  2563. (or (cadr (assq (intern lang)
  2564. (plist-get info :latex-listings-langs)))
  2565. lang))
  2566. (caption-str
  2567. (when caption
  2568. (let ((main (org-export-get-caption src-block))
  2569. (secondary (org-export-get-caption src-block t)))
  2570. (if (not secondary)
  2571. (format "{%s}" (org-export-data main info))
  2572. (format "{[%s]%s}"
  2573. (org-export-data secondary info)
  2574. (org-export-data main info))))))
  2575. (lst-opt (plist-get info :latex-listings-options)))
  2576. (concat
  2577. ;; Options.
  2578. (format
  2579. "\\lstset{%s}\n"
  2580. (concat
  2581. (org-latex--make-option-string
  2582. (append
  2583. lst-opt
  2584. (cond
  2585. ((and (not float) (plist-member attributes :float)) nil)
  2586. ((string= "multicolumn" float) '(("float" "*")))
  2587. ((and float (not (assoc "float" lst-opt)))
  2588. `(("float" ,(plist-get info :latex-default-figure-position)))))
  2589. `(("language" ,lst-lang))
  2590. (if label `(("label" ,label)) '(("label" " ")))
  2591. (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
  2592. `(("captionpos" ,(if caption-above-p "t" "b")))
  2593. (cond ((assoc "numbers" lst-opt) nil)
  2594. ((not num-start) '(("numbers" "none")))
  2595. (t `(("firstnumber" ,(number-to-string (1+ num-start)))
  2596. ("numbers" "left"))))))
  2597. (let ((local-options (plist-get attributes :options)))
  2598. (and local-options (concat "," local-options)))))
  2599. ;; Source code.
  2600. (format
  2601. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  2602. (let* ((code-info (org-export-unravel-code src-block))
  2603. (max-width
  2604. (apply 'max
  2605. (mapcar 'length
  2606. (org-split-string (car code-info) "\n")))))
  2607. (org-export-format-code
  2608. (car code-info)
  2609. (lambda (loc _num ref)
  2610. (concat
  2611. loc
  2612. (when ref
  2613. ;; Ensure references are flushed to the right,
  2614. ;; separated with 6 spaces from the widest line of
  2615. ;; code
  2616. (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
  2617. (format "(%s)" ref)))))
  2618. nil (and retain-labels (cdr code-info))))))))))))
  2619. ;;;; Statistics Cookie
  2620. (defun org-latex-statistics-cookie (statistics-cookie _contents _info)
  2621. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  2622. CONTENTS is nil. INFO is a plist holding contextual information."
  2623. (replace-regexp-in-string
  2624. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  2625. ;;;; Strike-Through
  2626. (defun org-latex-strike-through (_strike-through contents info)
  2627. "Transcode STRIKE-THROUGH from Org to LaTeX.
  2628. CONTENTS is the text with strike-through markup. INFO is a plist
  2629. holding contextual information."
  2630. (org-latex--text-markup contents 'strike-through info))
  2631. ;;;; Subscript
  2632. (defun org-latex--script-size (object info)
  2633. "Transcode a subscript or superscript object.
  2634. OBJECT is an Org object. INFO is a plist used as a communication
  2635. channel."
  2636. (let ((output ""))
  2637. (org-element-map (org-element-contents object)
  2638. (cons 'plain-text org-element-all-objects)
  2639. (lambda (obj)
  2640. (cl-case (org-element-type obj)
  2641. ((entity latex-fragment)
  2642. (let ((data (org-trim (org-export-data obj info))))
  2643. (string-match
  2644. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  2645. data)
  2646. (setq output
  2647. (concat output
  2648. (match-string 1 data)
  2649. (let ((blank (org-element-property :post-blank obj)))
  2650. (and blank (> blank 0) "\\ "))))))
  2651. (plain-text
  2652. (setq output
  2653. (format "%s\\text{%s}" output (org-export-data obj info))))
  2654. (otherwise
  2655. (setq output
  2656. (concat output
  2657. (org-export-data obj info)
  2658. (let ((blank (org-element-property :post-blank obj)))
  2659. (and blank (> blank 0) "\\ ")))))))
  2660. info nil org-element-recursive-objects)
  2661. ;; Result. Do not wrap into curly brackets if OUTPUT is a single
  2662. ;; character.
  2663. (concat (if (eq (org-element-type object) 'subscript) "_" "^")
  2664. (and (> (length output) 1) "{")
  2665. output
  2666. (and (> (length output) 1) "}"))))
  2667. (defun org-latex-subscript (subscript _contents info)
  2668. "Transcode a SUBSCRIPT object from Org to LaTeX.
  2669. CONTENTS is the contents of the object. INFO is a plist holding
  2670. contextual information."
  2671. (org-latex--script-size subscript info))
  2672. ;;;; Superscript
  2673. (defun org-latex-superscript (superscript _contents info)
  2674. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  2675. CONTENTS is the contents of the object. INFO is a plist holding
  2676. contextual information."
  2677. (org-latex--script-size superscript info))
  2678. ;;;; Table
  2679. ;;
  2680. ;; `org-latex-table' is the entry point for table transcoding. It
  2681. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  2682. ;; delegates the job to either `org-latex--table.el-table',
  2683. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  2684. ;; depending of the type of the table and the mode requested.
  2685. ;;
  2686. ;; `org-latex--align-string' is a subroutine used to build alignment
  2687. ;; string for Org tables.
  2688. (defun org-latex-table (table contents info)
  2689. "Transcode a TABLE element from Org to LaTeX.
  2690. CONTENTS is the contents of the table. INFO is a plist holding
  2691. contextual information."
  2692. (if (eq (org-element-property :type table) 'table.el)
  2693. ;; "table.el" table. Convert it using appropriate tools.
  2694. (org-latex--table.el-table table info)
  2695. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  2696. (plist-get info :latex-default-table-mode))))
  2697. (cond
  2698. ;; Case 1: Verbatim table.
  2699. ((string= type "verbatim")
  2700. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  2701. ;; Re-create table, without affiliated keywords.
  2702. (org-trim (org-element-interpret-data
  2703. `(table nil ,@(org-element-contents table))))))
  2704. ;; Case 2: Matrix.
  2705. ((or (string= type "math") (string= type "inline-math"))
  2706. (org-latex--math-table table info))
  2707. ;; Case 3: Standard table.
  2708. (t (concat (org-latex--org-table table contents info)
  2709. ;; When there are footnote references within the
  2710. ;; table, insert their definition just after it.
  2711. (org-latex--delayed-footnotes-definitions table info)))))))
  2712. (defun org-latex--align-string (table info)
  2713. "Return an appropriate LaTeX alignment string.
  2714. TABLE is the considered table. INFO is a plist used as
  2715. a communication channel."
  2716. (or (org-export-read-attribute :attr_latex table :align)
  2717. (let (align)
  2718. ;; Extract column groups and alignment from first (non-rule)
  2719. ;; row.
  2720. (org-element-map
  2721. (org-element-map table 'table-row
  2722. (lambda (row)
  2723. (and (eq (org-element-property :type row) 'standard) row))
  2724. info 'first-match)
  2725. 'table-cell
  2726. (lambda (cell)
  2727. (let ((borders (org-export-table-cell-borders cell info)))
  2728. ;; Check left border for the first cell only.
  2729. (when (and (memq 'left borders) (not align))
  2730. (push "|" align))
  2731. (push (cl-case (org-export-table-cell-alignment cell info)
  2732. (left "l")
  2733. (right "r")
  2734. (center "c"))
  2735. align)
  2736. (when (memq 'right borders) (push "|" align))))
  2737. info)
  2738. (apply 'concat (nreverse align)))))
  2739. (defun org-latex--org-table (table contents info)
  2740. "Return appropriate LaTeX code for an Org table.
  2741. TABLE is the table type element to transcode. CONTENTS is its
  2742. contents, as a string. INFO is a plist used as a communication
  2743. channel.
  2744. This function assumes TABLE has `org' as its `:type' property and
  2745. `table' as its `:mode' attribute."
  2746. (let* ((caption (org-latex--caption/label-string table info))
  2747. (attr (org-export-read-attribute :attr_latex table))
  2748. ;; Determine alignment string.
  2749. (alignment (org-latex--align-string table info))
  2750. ;; Determine environment for the table: longtable, tabular...
  2751. (table-env (or (plist-get attr :environment)
  2752. (plist-get info :latex-default-table-environment)))
  2753. ;; If table is a float, determine environment: table, table*
  2754. ;; or sidewaystable.
  2755. (float-env (unless (member table-env '("longtable" "longtabu"))
  2756. (let ((float (plist-get attr :float)))
  2757. (cond
  2758. ((and (not float) (plist-member attr :float)) nil)
  2759. ((or (string= float "sidewaystable")
  2760. (string= float "sideways")) "sidewaystable")
  2761. ((string= float "multicolumn") "table*")
  2762. ((or float
  2763. (org-element-property :caption table)
  2764. (org-string-nw-p (plist-get attr :caption)))
  2765. "table")))))
  2766. ;; Extract others display options.
  2767. (fontsize (let ((font (plist-get attr :font)))
  2768. (and font (concat font "\n"))))
  2769. ;; "tabular" environment doesn't allow to define a width.
  2770. (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
  2771. (spreadp (plist-get attr :spread))
  2772. (placement
  2773. (or (plist-get attr :placement)
  2774. (format "[%s]" (plist-get info :latex-default-figure-position))))
  2775. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2776. (plist-get info :latex-tables-centered)))
  2777. (caption-above-p (org-latex--caption-above-p table info)))
  2778. ;; Prepare the final format string for the table.
  2779. (cond
  2780. ;; Longtable.
  2781. ((equal "longtable" table-env)
  2782. (concat (and fontsize (concat "{" fontsize))
  2783. (format "\\begin{longtable}{%s}\n" alignment)
  2784. (and caption-above-p
  2785. (org-string-nw-p caption)
  2786. (concat caption "\\\\\n"))
  2787. contents
  2788. (and (not caption-above-p)
  2789. (org-string-nw-p caption)
  2790. (concat caption "\\\\\n"))
  2791. "\\end{longtable}\n"
  2792. (and fontsize "}")))
  2793. ;; Longtabu
  2794. ((equal "longtabu" table-env)
  2795. (concat (and fontsize (concat "{" fontsize))
  2796. (format "\\begin{longtabu}%s{%s}\n"
  2797. (if width
  2798. (format " %s %s "
  2799. (if spreadp "spread" "to") width) "")
  2800. alignment)
  2801. (and caption-above-p
  2802. (org-string-nw-p caption)
  2803. (concat caption "\\\\\n"))
  2804. contents
  2805. (and (not caption-above-p)
  2806. (org-string-nw-p caption)
  2807. (concat caption "\\\\\n"))
  2808. "\\end{longtabu}\n"
  2809. (and fontsize "}")))
  2810. ;; Others.
  2811. (t (concat (cond
  2812. (float-env
  2813. (concat (format "\\begin{%s}%s\n" float-env placement)
  2814. (if caption-above-p caption "")
  2815. (when centerp "\\centering\n")
  2816. fontsize))
  2817. ((and (not float-env) caption)
  2818. (concat
  2819. (and centerp "\\begin{center}\n" )
  2820. (if caption-above-p caption "")
  2821. (cond ((and fontsize centerp) fontsize)
  2822. (fontsize (concat "{" fontsize)))))
  2823. (centerp (concat "\\begin{center}\n" fontsize))
  2824. (fontsize (concat "{" fontsize)))
  2825. (cond ((equal "tabu" table-env)
  2826. (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
  2827. (if width (format
  2828. (if spreadp " spread %s " " to %s ")
  2829. width) "")
  2830. alignment
  2831. contents))
  2832. (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2833. table-env
  2834. (if width (format "{%s}" width) "")
  2835. alignment
  2836. contents
  2837. table-env)))
  2838. (cond
  2839. (float-env
  2840. (concat (if caption-above-p "" (concat "\n" caption))
  2841. (format "\n\\end{%s}" float-env)))
  2842. ((and (not float-env) caption)
  2843. (concat
  2844. (if caption-above-p "" (concat "\n" caption))
  2845. (and centerp "\n\\end{center}")
  2846. (and fontsize (not centerp) "}")))
  2847. (centerp "\n\\end{center}")
  2848. (fontsize "}")))))))
  2849. (defun org-latex--table.el-table (table info)
  2850. "Return appropriate LaTeX code for a table.el table.
  2851. TABLE is the table type element to transcode. INFO is a plist
  2852. used as a communication channel.
  2853. This function assumes TABLE has `table.el' as its `:type'
  2854. property."
  2855. (require 'table)
  2856. ;; Ensure "*org-export-table*" buffer is empty.
  2857. (with-current-buffer (get-buffer-create "*org-export-table*")
  2858. (erase-buffer))
  2859. (let ((output (with-temp-buffer
  2860. (insert (org-element-property :value table))
  2861. (goto-char 1)
  2862. (re-search-forward "^[ \t]*|[^|]" nil t)
  2863. (table-generate-source 'latex "*org-export-table*")
  2864. (with-current-buffer "*org-export-table*"
  2865. (org-trim (buffer-string))))))
  2866. (kill-buffer (get-buffer "*org-export-table*"))
  2867. ;; Remove left out comments.
  2868. (while (string-match "^%.*\n" output)
  2869. (setq output (replace-match "" t t output)))
  2870. (let ((attr (org-export-read-attribute :attr_latex table)))
  2871. (when (plist-get attr :rmlines)
  2872. ;; When the "rmlines" attribute is provided, remove all hlines
  2873. ;; but the the one separating heading from the table body.
  2874. (let ((n 0) (pos 0))
  2875. (while (and (< (length output) pos)
  2876. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2877. (cl-incf n)
  2878. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2879. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2880. (plist-get info :latex-tables-centered))))
  2881. (if (not centerp) output
  2882. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2883. (defun org-latex--math-table (table info)
  2884. "Return appropriate LaTeX code for a matrix.
  2885. TABLE is the table type element to transcode. INFO is a plist
  2886. used as a communication channel.
  2887. This function assumes TABLE has `org' as its `:type' property and
  2888. `inline-math' or `math' as its `:mode' attribute."
  2889. (let* ((attr (org-export-read-attribute :attr_latex table))
  2890. (env (or (plist-get attr :environment)
  2891. (plist-get info :latex-default-table-environment)))
  2892. (contents
  2893. (mapconcat
  2894. (lambda (row)
  2895. ;; Ignore horizontal rules.
  2896. (when (eq (org-element-property :type row) 'standard)
  2897. ;; Return each cell unmodified.
  2898. (concat
  2899. (mapconcat
  2900. (lambda (cell)
  2901. (substring (org-element-interpret-data cell) 0 -1))
  2902. (org-element-map row 'table-cell #'identity info) "&")
  2903. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2904. "\n")))
  2905. (org-element-map table 'table-row #'identity info) "")))
  2906. (concat
  2907. ;; Prefix.
  2908. (plist-get attr :math-prefix)
  2909. ;; Environment. Also treat special cases.
  2910. (cond ((member env '("array" "tabular"))
  2911. (let ((align (make-string
  2912. (cdr (org-export-table-dimensions table info)) ?c)))
  2913. (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
  2914. ((assoc env org-latex-table-matrix-macros)
  2915. (format "\\%s%s{\n%s}"
  2916. env
  2917. (or (plist-get attr :math-arguments) "")
  2918. contents))
  2919. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2920. ;; Suffix.
  2921. (plist-get attr :math-suffix))))
  2922. ;;;; Table Cell
  2923. (defun org-latex-table-cell (table-cell contents info)
  2924. "Transcode a TABLE-CELL element from Org to LaTeX.
  2925. CONTENTS is the cell contents. INFO is a plist used as
  2926. a communication channel."
  2927. (concat
  2928. (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
  2929. (if (and contents
  2930. scientific-format
  2931. (string-match orgtbl-exp-regexp contents))
  2932. ;; Use appropriate format string for scientific
  2933. ;; notation.
  2934. (format scientific-format
  2935. (match-string 1 contents)
  2936. (match-string 2 contents))
  2937. contents))
  2938. (when (org-export-get-next-element table-cell info) " & ")))
  2939. ;;;; Table Row
  2940. (defun org-latex-table-row (table-row contents info)
  2941. "Transcode a TABLE-ROW element from Org to LaTeX.
  2942. CONTENTS is the contents of the row. INFO is a plist used as
  2943. a communication channel."
  2944. (let* ((attr (org-export-read-attribute :attr_latex
  2945. (org-export-get-parent table-row)))
  2946. (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
  2947. (plist-get info :latex-tables-booktabs)))
  2948. (longtablep
  2949. (member (or (plist-get attr :environment)
  2950. (plist-get info :latex-default-table-environment))
  2951. '("longtable" "longtabu"))))
  2952. (if (eq (org-element-property :type table-row) 'rule)
  2953. (cond
  2954. ((not booktabsp) "\\hline")
  2955. ((not (org-export-get-previous-element table-row info)) "\\toprule")
  2956. ((not (org-export-get-next-element table-row info)) "\\bottomrule")
  2957. ((and longtablep
  2958. (org-export-table-row-ends-header-p
  2959. (org-export-get-previous-element table-row info) info))
  2960. "")
  2961. (t "\\midrule"))
  2962. (concat
  2963. ;; When BOOKTABS are activated enforce top-rule even when no
  2964. ;; hline was specifically marked.
  2965. (and booktabsp (not (org-export-get-previous-element table-row info))
  2966. "\\toprule\n")
  2967. contents "\\\\\n"
  2968. (cond
  2969. ;; Special case for long tables. Define header and footers.
  2970. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2971. (let ((columns (cdr (org-export-table-dimensions
  2972. (org-export-get-parent-table table-row) info))))
  2973. (format "%s
  2974. \\endfirsthead
  2975. \\multicolumn{%d}{l}{%s} \\\\
  2976. %s
  2977. %s \\\\\n
  2978. %s
  2979. \\endhead
  2980. %s\\multicolumn{%d}{r}{%s} \\\\
  2981. \\endfoot
  2982. \\endlastfoot"
  2983. (if booktabsp "\\midrule" "\\hline")
  2984. columns
  2985. (org-latex--translate "Continued from previous page" info)
  2986. (cond
  2987. ((not (org-export-table-row-starts-header-p table-row info))
  2988. "")
  2989. (booktabsp "\\toprule\n")
  2990. (t "\\hline\n"))
  2991. contents
  2992. (if booktabsp "\\midrule" "\\hline")
  2993. (if booktabsp "\\midrule" "\\hline")
  2994. columns
  2995. (org-latex--translate "Continued on next page" info))))
  2996. ;; When BOOKTABS are activated enforce bottom rule even when
  2997. ;; no hline was specifically marked.
  2998. ((and booktabsp (not (org-export-get-next-element table-row info)))
  2999. "\\bottomrule"))))))
  3000. ;;;; Target
  3001. (defun org-latex-target (target _contents info)
  3002. "Transcode a TARGET object from Org to LaTeX.
  3003. CONTENTS is nil. INFO is a plist holding contextual
  3004. information."
  3005. (format "\\label{%s}" (org-latex--label target info)))
  3006. ;;;; Timestamp
  3007. (defun org-latex-timestamp (timestamp _contents info)
  3008. "Transcode a TIMESTAMP object from Org to LaTeX.
  3009. CONTENTS is nil. INFO is a plist holding contextual
  3010. information."
  3011. (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
  3012. (format
  3013. (plist-get info
  3014. (cl-case (org-element-property :type timestamp)
  3015. ((active active-range) :latex-active-timestamp-format)
  3016. ((inactive inactive-range) :latex-inactive-timestamp-format)
  3017. (otherwise :latex-diary-timestamp-format)))
  3018. value)))
  3019. ;;;; Underline
  3020. (defun org-latex-underline (_underline contents info)
  3021. "Transcode UNDERLINE from Org to LaTeX.
  3022. CONTENTS is the text with underline markup. INFO is a plist
  3023. holding contextual information."
  3024. (org-latex--text-markup contents 'underline info))
  3025. ;;;; Verbatim
  3026. (defun org-latex-verbatim (verbatim _contents info)
  3027. "Transcode a VERBATIM object from Org to LaTeX.
  3028. CONTENTS is nil. INFO is a plist used as a communication
  3029. channel."
  3030. (org-latex--text-markup
  3031. (org-element-property :value verbatim) 'verbatim info))
  3032. ;;;; Verse Block
  3033. (defun org-latex-verse-block (verse-block contents info)
  3034. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  3035. CONTENTS is verse block contents. INFO is a plist holding
  3036. contextual information."
  3037. (org-latex--wrap-label
  3038. verse-block
  3039. ;; In a verse environment, add a line break to each newline
  3040. ;; character and change each white space at beginning of a line
  3041. ;; into a space of 1 em. Also change each blank line with
  3042. ;; a vertical space of 1 em.
  3043. (format "\\begin{verse}\n%s\\end{verse}"
  3044. (replace-regexp-in-string
  3045. "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
  3046. (replace-regexp-in-string
  3047. "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
  3048. (replace-regexp-in-string
  3049. "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
  3050. contents nil t) nil t) nil t))
  3051. info))
  3052. ;;; End-user functions
  3053. ;;;###autoload
  3054. (defun org-latex-export-as-latex
  3055. (&optional async subtreep visible-only body-only ext-plist)
  3056. "Export current buffer as a LaTeX buffer.
  3057. If narrowing is active in the current buffer, only export its
  3058. narrowed part.
  3059. If a region is active, export that region.
  3060. A non-nil optional argument ASYNC means the process should happen
  3061. asynchronously. The resulting buffer should be accessible
  3062. through the `org-export-stack' interface.
  3063. When optional argument SUBTREEP is non-nil, export the sub-tree
  3064. at point, extracting information from the headline properties
  3065. first.
  3066. When optional argument VISIBLE-ONLY is non-nil, don't export
  3067. contents of hidden elements.
  3068. When optional argument BODY-ONLY is non-nil, only write code
  3069. between \"\\begin{document}\" and \"\\end{document}\".
  3070. EXT-PLIST, when provided, is a property list with external
  3071. parameters overriding Org default settings, but still inferior to
  3072. file-local settings.
  3073. Export is done in a buffer named \"*Org LATEX Export*\", which
  3074. will be displayed when `org-export-show-temporary-export-buffer'
  3075. is non-nil."
  3076. (interactive)
  3077. (org-export-to-buffer 'latex "*Org LATEX Export*"
  3078. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  3079. ;;;###autoload
  3080. (defun org-latex-convert-region-to-latex ()
  3081. "Assume the current region has Org syntax, and convert it to LaTeX.
  3082. This can be used in any buffer. For example, you can write an
  3083. itemized list in Org syntax in an LaTeX buffer and use this
  3084. command to convert it."
  3085. (interactive)
  3086. (org-export-replace-region-by 'latex))
  3087. ;;;###autoload
  3088. (defun org-latex-export-to-latex
  3089. (&optional async subtreep visible-only body-only ext-plist)
  3090. "Export current buffer to a LaTeX file.
  3091. If narrowing is active in the current buffer, only export its
  3092. narrowed part.
  3093. If a region is active, export that region.
  3094. A non-nil optional argument ASYNC means the process should happen
  3095. asynchronously. The resulting file should be accessible through
  3096. the `org-export-stack' interface.
  3097. When optional argument SUBTREEP is non-nil, export the sub-tree
  3098. at point, extracting information from the headline properties
  3099. first.
  3100. When optional argument VISIBLE-ONLY is non-nil, don't export
  3101. contents of hidden elements.
  3102. When optional argument BODY-ONLY is non-nil, only write code
  3103. between \"\\begin{document}\" and \"\\end{document}\".
  3104. EXT-PLIST, when provided, is a property list with external
  3105. parameters overriding Org default settings, but still inferior to
  3106. file-local settings."
  3107. (interactive)
  3108. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3109. (org-export-to-file 'latex outfile
  3110. async subtreep visible-only body-only ext-plist)))
  3111. ;;;###autoload
  3112. (defun org-latex-export-to-pdf
  3113. (&optional async subtreep visible-only body-only ext-plist)
  3114. "Export current buffer to LaTeX then process through to PDF.
  3115. If narrowing is active in the current buffer, only export its
  3116. narrowed part.
  3117. If a region is active, export that region.
  3118. A non-nil optional argument ASYNC means the process should happen
  3119. asynchronously. The resulting file should be accessible through
  3120. the `org-export-stack' interface.
  3121. When optional argument SUBTREEP is non-nil, export the sub-tree
  3122. at point, extracting information from the headline properties
  3123. first.
  3124. When optional argument VISIBLE-ONLY is non-nil, don't export
  3125. contents of hidden elements.
  3126. When optional argument BODY-ONLY is non-nil, only write code
  3127. between \"\\begin{document}\" and \"\\end{document}\".
  3128. EXT-PLIST, when provided, is a property list with external
  3129. parameters overriding Org default settings, but still inferior to
  3130. file-local settings.
  3131. Return PDF file's name."
  3132. (interactive)
  3133. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3134. (org-export-to-file 'latex outfile
  3135. async subtreep visible-only body-only ext-plist
  3136. (lambda (file) (org-latex-compile file)))))
  3137. (defun org-latex-compile (texfile &optional snippet)
  3138. "Compile a TeX file.
  3139. TEXFILE is the name of the file being compiled. Processing is
  3140. done through the command specified in `org-latex-pdf-process',
  3141. which see. Output is redirected to \"*Org PDF LaTeX Output*\"
  3142. buffer.
  3143. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  3144. file used to preview a LaTeX snippet. In this case, do not
  3145. create a log buffer and do not remove log files.
  3146. Return PDF file name or raise an error if it couldn't be
  3147. produced."
  3148. (unless snippet (message "Processing LaTeX file %s..." texfile))
  3149. (let* ((compiler
  3150. (or (with-temp-buffer
  3151. (save-excursion (insert-file-contents texfile))
  3152. (and (search-forward-regexp (regexp-opt org-latex-compilers)
  3153. (line-end-position 2)
  3154. t)
  3155. (progn (beginning-of-line) (looking-at-p "%"))
  3156. (match-string 0)))
  3157. "pdflatex"))
  3158. (process (if (functionp org-latex-pdf-process) org-latex-pdf-process
  3159. ;; Replace "%latex" and "%bibtex" with,
  3160. ;; respectively, "%L" and "%B" so as to adhere to
  3161. ;; `format-spec' specifications.
  3162. (mapcar (lambda (command)
  3163. (replace-regexp-in-string
  3164. "%\\(?:bib\\|la\\)tex\\>"
  3165. (lambda (m) (upcase (substring m 0 2)))
  3166. command))
  3167. org-latex-pdf-process)))
  3168. (spec `((?B . ,(shell-quote-argument org-latex-bib-compiler))
  3169. (?L . ,(shell-quote-argument compiler))))
  3170. (log-buf-name "*Org PDF LaTeX Output*")
  3171. (log-buf (and (not snippet) (get-buffer-create log-buf-name)))
  3172. (outfile (org-compile-file texfile process "pdf"
  3173. (format "See %S for details" log-buf-name)
  3174. log-buf spec)))
  3175. (unless snippet
  3176. (when org-latex-remove-logfiles
  3177. (mapc #'delete-file
  3178. (directory-files
  3179. (file-name-directory texfile) t
  3180. (concat (regexp-quote (file-name-base outfile))
  3181. "\\(?:\\.[0-9]+\\)?\\."
  3182. (regexp-opt org-latex-logfiles-extensions)))))
  3183. (let ((warnings (org-latex--collect-warnings log-buf)))
  3184. (message (concat "PDF file produced"
  3185. (cond
  3186. ((eq warnings 'error) " with errors.")
  3187. (warnings (concat " with warnings: " warnings))
  3188. (t "."))))))
  3189. ;; Return output file name.
  3190. outfile))
  3191. (defun org-latex--collect-warnings (buffer)
  3192. "Collect some warnings from \"pdflatex\" command output.
  3193. BUFFER is the buffer containing output. Return collected
  3194. warnings types as a string, `error' if a LaTeX error was
  3195. encountered or nil if there was none."
  3196. (with-current-buffer buffer
  3197. (save-excursion
  3198. (goto-char (point-max))
  3199. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  3200. (if (re-search-forward "^!" nil t) 'error
  3201. (let ((case-fold-search t)
  3202. (warnings ""))
  3203. (dolist (warning org-latex-known-warnings)
  3204. (when (save-excursion (re-search-forward (car warning) nil t))
  3205. (setq warnings (concat warnings " " (cdr warning)))))
  3206. (org-string-nw-p (org-trim warnings))))))))
  3207. ;;;###autoload
  3208. (defun org-latex-publish-to-latex (plist filename pub-dir)
  3209. "Publish an Org file to LaTeX.
  3210. FILENAME is the filename of the Org file to be published. PLIST
  3211. is the property list for the given project. PUB-DIR is the
  3212. publishing directory.
  3213. Return output file name."
  3214. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  3215. ;;;###autoload
  3216. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  3217. "Publish an Org file to PDF (via LaTeX).
  3218. FILENAME is the filename of the Org file to be published. PLIST
  3219. is the property list for the given project. PUB-DIR is the
  3220. publishing directory.
  3221. Return output file name."
  3222. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  3223. ;; in working directory and then moved to publishing directory.
  3224. (org-publish-attachment
  3225. plist
  3226. (org-latex-compile
  3227. (org-publish-org-to
  3228. 'latex filename ".tex" plist (file-name-directory filename)))
  3229. pub-dir))
  3230. (provide 'ox-latex)
  3231. ;; Local variables:
  3232. ;; generated-autoload-file: "org-loaddefs.el"
  3233. ;; End:
  3234. ;;; ox-latex.el ends here