ox-latex.el 127 KB

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