ox-latex.el 127 KB

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