ox-latex.el 134 KB

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