ox-latex.el 123 KB

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