ox-latex.el 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828
  1. ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2022 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 #'stringp)
  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 #'stringp)
  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. (if (executable-find "latexmk")
  1000. '("latexmk -f -pdf -%latex -interaction=nonstopmode -output-directory=%o %f")
  1001. '("%latex -interaction nonstopmode -output-directory %o %f"
  1002. "%latex -interaction nonstopmode -output-directory %o %f"
  1003. "%latex -interaction nonstopmode -output-directory %o %f"))
  1004. "Commands to process a LaTeX file to a PDF file.
  1005. This is a list of strings, each of them will be given to the
  1006. shell as a command. %f in the command will be replaced by the
  1007. relative file name, %F by the absolute file name, %b by the file
  1008. base name (i.e. without directory and extension parts), %o by the
  1009. base directory of the file, %O by the absolute file name of the
  1010. output file, %latex is the LaTeX compiler (see
  1011. `org-latex-compiler'), and %bib is the BibTeX-like compiler (see
  1012. `org-latex-bib-compiler').
  1013. The reason why this is a list is that it usually takes several
  1014. runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
  1015. does not have a clever mechanism to detect which of these
  1016. commands have to be run to get to a stable result, and it also
  1017. does not do any error checking.
  1018. Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
  1019. which calls the \"correct\" combinations of auxiliary programs.
  1020. Alternatively, this may be a Lisp function that does the
  1021. processing, so you could use this to apply the machinery of
  1022. AUCTeX or the Emacs LaTeX mode. This function should accept the
  1023. file name as its single argument."
  1024. :group 'org-export-pdf
  1025. :type '(choice
  1026. (repeat :tag "Shell command sequence"
  1027. (string :tag "Shell command"))
  1028. (const :tag "2 runs of latex"
  1029. ("%latex -interaction nonstopmode -output-directory %o %f"
  1030. "%latex -interaction nonstopmode -output-directory %o %f"))
  1031. (const :tag "3 runs of latex"
  1032. ("%latex -interaction nonstopmode -output-directory %o %f"
  1033. "%latex -interaction nonstopmode -output-directory %o %f"
  1034. "%latex -interaction nonstopmode -output-directory %o %f"))
  1035. (const :tag "latex,bibtex,latex,latex"
  1036. ("%latex -interaction nonstopmode -output-directory %o %f"
  1037. "%bib %b"
  1038. "%latex -interaction nonstopmode -output-directory %o %f"
  1039. "%latex -interaction nonstopmode -output-directory %o %f"))
  1040. (const :tag "texi2dvi"
  1041. ("cd %o; LATEX=\"%latex\" texi2dvi -p -b -V %b.tex"))
  1042. (const :tag "latexmk"
  1043. ("latexmk -f -pdf -%latex -interaction=nonstopmode -output-directory=%o %f"))
  1044. (function)))
  1045. (defcustom org-latex-logfiles-extensions
  1046. '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
  1047. "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
  1048. "The list of file extensions to consider as LaTeX logfiles.
  1049. The logfiles will be removed if `org-latex-remove-logfiles' is
  1050. non-nil."
  1051. :group 'org-export-latex
  1052. :version "26.1"
  1053. :package-version '(Org . "8.3")
  1054. :type '(repeat (string :tag "Extension")))
  1055. (defcustom org-latex-remove-logfiles t
  1056. "Non-nil means remove the logfiles produced by PDF production.
  1057. By default, logfiles are files with these extensions: .aux, .idx,
  1058. .log, .out, .toc, .nav, .snm and .vrb. To define the set of
  1059. logfiles to remove, set `org-latex-logfiles-extensions'."
  1060. :group 'org-export-latex
  1061. :type 'boolean)
  1062. (defcustom org-latex-known-warnings
  1063. '(("Reference.*?undefined" . "[undefined reference]")
  1064. ("Runaway argument" . "[runaway argument]")
  1065. ("Underfull \\hbox" . "[underfull hbox]")
  1066. ("Overfull \\hbox" . "[overfull hbox]")
  1067. ("Citation.*?undefined" . "[undefined citation]")
  1068. ("Undefined control sequence" . "[undefined control sequence]"))
  1069. "Alist of regular expressions and associated messages for the user.
  1070. The regular expressions are used to find possible warnings in the
  1071. log of a LaTeX-run. These warnings will be reported after
  1072. calling `org-latex-compile'."
  1073. :group 'org-export-latex
  1074. :version "26.1"
  1075. :package-version '(Org . "8.3")
  1076. :type '(repeat
  1077. (cons
  1078. (regexp :tag "Regexp")
  1079. (string :tag "Message"))))
  1080. ;;; Internal Functions
  1081. (defun org-latex--caption-above-p (element info)
  1082. "Non-nil when caption is expected to be located above ELEMENT.
  1083. INFO is a plist holding contextual information."
  1084. (let ((above (plist-get info :latex-caption-above)))
  1085. (if (symbolp above) above
  1086. (let ((type (org-element-type element)))
  1087. (memq (if (eq type 'link) 'image type) above)))))
  1088. (defun org-latex--label (datum info &optional force full)
  1089. "Return an appropriate label for DATUM.
  1090. DATUM is an element or a `target' type object. INFO is the
  1091. current export state, as a plist.
  1092. Return nil if element DATUM has no NAME or VALUE affiliated
  1093. keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
  1094. this case always return a unique label.
  1095. Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
  1096. (let* ((type (org-element-type datum))
  1097. (user-label
  1098. (org-element-property
  1099. (cl-case type
  1100. ((headline inlinetask) :CUSTOM_ID)
  1101. (target :value)
  1102. (otherwise :name))
  1103. datum))
  1104. (label
  1105. (and (or user-label force)
  1106. (if (and user-label (plist-get info :latex-prefer-user-labels))
  1107. user-label
  1108. (concat (pcase type
  1109. (`headline "sec:")
  1110. (`table "tab:")
  1111. (`latex-environment
  1112. (and (string-match-p
  1113. org-latex-math-environments-re
  1114. (org-element-property :value datum))
  1115. "eq:"))
  1116. (`latex-matrices "eq:")
  1117. (`paragraph
  1118. (and (org-element-property :caption datum)
  1119. "fig:"))
  1120. (_ nil))
  1121. (org-export-get-reference datum info))))))
  1122. (cond ((not full) label)
  1123. (label (format "\\label{%s}%s"
  1124. label
  1125. (if (eq type 'target) "" "\n")))
  1126. (t ""))))
  1127. (defun org-latex--caption/label-string (element info)
  1128. "Return caption and label LaTeX string for ELEMENT.
  1129. INFO is a plist holding contextual information. If there's no
  1130. caption nor label, return the empty string.
  1131. For non-floats, see `org-latex--wrap-label'."
  1132. (let* ((label (org-latex--label element info nil t))
  1133. (main (org-export-get-caption element))
  1134. (attr (org-export-read-attribute :attr_latex element))
  1135. (type (org-element-type element))
  1136. (nonfloat (or (and (plist-member attr :float)
  1137. (not (plist-get attr :float))
  1138. main)
  1139. (and (eq type 'src-block)
  1140. (not (plist-get attr :float))
  1141. (null (plist-get info :latex-listings)))))
  1142. (short (org-export-get-caption element t))
  1143. (caption-from-attr-latex (plist-get attr :caption)))
  1144. (cond
  1145. ((org-string-nw-p caption-from-attr-latex)
  1146. (concat caption-from-attr-latex "\n"))
  1147. ((and (not main) (equal label "")) "")
  1148. ((not main) label)
  1149. ;; Option caption format with short name.
  1150. (t
  1151. (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
  1152. "\\caption%s%s{%s%s}\n")
  1153. (let ((type* (if (eq type 'latex-environment)
  1154. (org-latex--environment-type element)
  1155. type)))
  1156. (if nonfloat
  1157. (cl-case type*
  1158. (paragraph "figure")
  1159. (image "figure")
  1160. (special-block "figure")
  1161. (src-block (if (plist-get info :latex-listings)
  1162. "listing"
  1163. "figure"))
  1164. (t (symbol-name type*)))
  1165. ""))
  1166. (if short (format "[%s]" (org-export-data short info)) "")
  1167. (org-trim label)
  1168. (org-export-data main info))))))
  1169. (defun org-latex-guess-inputenc (header)
  1170. "Set the coding system in inputenc to what the buffer is.
  1171. HEADER is the LaTeX header string. This function only applies
  1172. when specified inputenc option is \"AUTO\".
  1173. Return the new header, as a string."
  1174. (let* ((cs (or (ignore-errors
  1175. (latexenc-coding-system-to-inputenc
  1176. (or org-export-coding-system buffer-file-coding-system)))
  1177. "utf8")))
  1178. (if (not cs) header
  1179. ;; First translate if that is requested.
  1180. (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
  1181. ;; Then find the \usepackage statement and replace the option.
  1182. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  1183. cs header t nil 1))))
  1184. (defun org-latex-guess-babel-language (header info)
  1185. "Set Babel's language according to LANGUAGE keyword.
  1186. HEADER is the LaTeX header string. INFO is the plist used as
  1187. a communication channel.
  1188. Insertion of guessed language only happens when Babel package has
  1189. explicitly been loaded. Then it is added to the rest of
  1190. package's options.
  1191. The argument to Babel may be \"AUTO\" which is then replaced with
  1192. the language of the document or `org-export-default-language'
  1193. unless language in question is already loaded.
  1194. Return the new header."
  1195. (let ((language-code (plist-get info :language)))
  1196. ;; If no language is set or Babel package is not loaded, return
  1197. ;; HEADER as-is.
  1198. (if (or (not (stringp language-code))
  1199. (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
  1200. header
  1201. (let ((options (save-match-data
  1202. (org-split-string (match-string 1 header) ",[ \t]*")))
  1203. (language (cdr (assoc-string language-code
  1204. org-latex-babel-language-alist t))))
  1205. ;; If LANGUAGE is already loaded, return header without AUTO.
  1206. ;; Otherwise, replace AUTO with language or append language if
  1207. ;; AUTO is not present.
  1208. (replace-match
  1209. (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
  1210. (cond ((member language options) (delete "AUTO" options))
  1211. ((member "AUTO" options) options)
  1212. (t (append options (list language))))
  1213. ", ")
  1214. t nil header 1)))))
  1215. (defun org-latex-guess-polyglossia-language (header info)
  1216. "Set the Polyglossia language according to the LANGUAGE keyword.
  1217. HEADER is the LaTeX header string. INFO is the plist used as
  1218. a communication channel.
  1219. Insertion of guessed language only happens when the Polyglossia
  1220. package has been explicitly loaded.
  1221. The argument to Polyglossia may be \"AUTO\" which is then
  1222. replaced with the language of the document or
  1223. `org-export-default-language'. Note, the language is really set
  1224. using \setdefaultlanguage and not as an option to the package.
  1225. Return the new header."
  1226. (let ((language (plist-get info :language)))
  1227. ;; If no language is set or Polyglossia is not loaded, return
  1228. ;; HEADER as-is.
  1229. (if (or (not (stringp language))
  1230. (not (string-match
  1231. "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
  1232. header)))
  1233. header
  1234. (let* ((options (org-string-nw-p (match-string 1 header)))
  1235. (languages (and options
  1236. ;; Reverse as the last loaded language is
  1237. ;; the main language.
  1238. (nreverse
  1239. (delete-dups
  1240. (save-match-data
  1241. (org-split-string
  1242. (replace-regexp-in-string
  1243. "AUTO" language options t)
  1244. ",[ \t]*"))))))
  1245. (main-language-set
  1246. (string-match-p "\\\\setmainlanguage{.*?}" header)))
  1247. (replace-match
  1248. (concat "\\usepackage{polyglossia}\n"
  1249. (mapconcat
  1250. (lambda (l)
  1251. (let ((l (or (assoc l org-latex-polyglossia-language-alist)
  1252. l)))
  1253. (format (if main-language-set "\\setotherlanguage%s{%s}\n"
  1254. (setq main-language-set t)
  1255. "\\setmainlanguage%s{%s}\n")
  1256. (if (and (consp l) (= (length l) 3))
  1257. (format "[variant=%s]" (nth 2 l))
  1258. "")
  1259. (nth 1 l))))
  1260. languages
  1261. ""))
  1262. t t header 0)))))
  1263. (defun org-latex--remove-packages (pkg-alist info)
  1264. "Remove packages based on the current LaTeX compiler.
  1265. PKG-ALIST is a list of packages, as in `org-latex-packages-alist'
  1266. and `org-latex-default-packages-alist'. If the fourth argument
  1267. of a package is neither nil nor a member of the LaTeX compiler
  1268. associated to the document, the package is removed.
  1269. Return new list of packages."
  1270. (let ((compiler (or (plist-get info :latex-compiler) "")))
  1271. (if (not (member-ignore-case compiler org-latex-compilers)) pkg-alist
  1272. (cl-remove-if-not
  1273. (lambda (package)
  1274. (pcase package
  1275. (`(,_ ,_ ,_ nil) t)
  1276. (`(,_ ,_ ,_ ,compilers) (member-ignore-case compiler compilers))
  1277. (_ t)))
  1278. pkg-alist))))
  1279. (defun org-latex--find-verb-separator (s)
  1280. "Return a character not used in string S.
  1281. This is used to choose a separator for constructs like \\verb."
  1282. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1283. (cl-loop for c across ll
  1284. when (not (string-match (regexp-quote (char-to-string c)) s))
  1285. return (char-to-string c))))
  1286. (defun org-latex--make-option-string (options)
  1287. "Return a comma separated string of keywords and values.
  1288. OPTIONS is an alist where the key is the options keyword as
  1289. a string, and the value a list containing the keyword value, or
  1290. nil."
  1291. (mapconcat (lambda (pair)
  1292. (pcase-let ((`(,keyword ,value) pair))
  1293. (concat keyword
  1294. (and (> (length value) 0)
  1295. (concat "="
  1296. (if (string-match-p (rx (any "[]")) value)
  1297. (format "{%s}" value)
  1298. value))))))
  1299. options
  1300. ","))
  1301. (defun org-latex--wrap-label (element output info)
  1302. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  1303. INFO is the current export state, as a plist. This function
  1304. should not be used for floats. See
  1305. `org-latex--caption/label-string'."
  1306. (if (not (and (org-string-nw-p output) (org-element-property :name element)))
  1307. output
  1308. (concat (format "\\phantomsection\n\\label{%s}\n"
  1309. (org-latex--label element info))
  1310. output)))
  1311. (defun org-latex--protect-text (text)
  1312. "Protect special characters in string TEXT and return it."
  1313. (replace-regexp-in-string "[\\{}$%&_#~^]" "\\\\\\&" text))
  1314. (defun org-latex--text-markup (text markup info)
  1315. "Format TEXT depending on MARKUP text markup.
  1316. INFO is a plist used as a communication channel. See
  1317. `org-latex-text-markup-alist' for details."
  1318. (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
  1319. (cl-case fmt
  1320. ;; No format string: Return raw text.
  1321. ((nil) text)
  1322. ;; Handle the `verb' special case: Find an appropriate separator
  1323. ;; and use "\\verb" command.
  1324. (verb
  1325. (let ((separator (org-latex--find-verb-separator text)))
  1326. (concat "\\verb"
  1327. separator
  1328. (replace-regexp-in-string "\n" " " text)
  1329. separator)))
  1330. (protectedtexttt (org-latex--protect-texttt text))
  1331. ;; Else use format string.
  1332. (t (format fmt text)))))
  1333. (defun org-latex--protect-texttt (text)
  1334. "Protect special chars, then wrap TEXT in \"\\texttt{}\"."
  1335. (format "\\texttt{%s}"
  1336. (replace-regexp-in-string
  1337. "--\\|[\\{}$%&_#~^]"
  1338. (lambda (m)
  1339. (cond ((equal m "--") "-{}-")
  1340. ((equal m "\\") "\\textbackslash{}")
  1341. ((equal m "~") "\\textasciitilde{}")
  1342. ((equal m "^") "\\textasciicircum{}")
  1343. (t (org-latex--protect-text m))))
  1344. text nil t)))
  1345. (defun org-latex--delayed-footnotes-definitions (element info)
  1346. "Return footnotes definitions in ELEMENT as a string.
  1347. INFO is a plist used as a communication channel.
  1348. Footnotes definitions are returned within \"\\footnotetext{}\"
  1349. commands.
  1350. This function is used within constructs that don't support
  1351. \"\\footnote{}\" command (e.g., an item tag). In that case,
  1352. \"\\footnotemark\" is used within the construct and the function
  1353. just outside of it."
  1354. (mapconcat
  1355. (lambda (ref)
  1356. (let ((def (org-export-get-footnote-definition ref info)))
  1357. (format "\\footnotetext[%d]{%s%s}"
  1358. (org-export-get-footnote-number ref info)
  1359. (org-trim (org-latex--label def info t t))
  1360. (org-trim (org-export-data def info)))))
  1361. ;; Find every footnote reference in ELEMENT.
  1362. (letrec ((all-refs nil)
  1363. (search-refs
  1364. (lambda (data)
  1365. ;; Return a list of all footnote references never seen
  1366. ;; before in DATA.
  1367. (org-element-map data 'footnote-reference
  1368. (lambda (ref)
  1369. (when (org-export-footnote-first-reference-p ref info)
  1370. (push ref all-refs)
  1371. (when (eq (org-element-property :type ref) 'standard)
  1372. (funcall search-refs
  1373. (org-export-get-footnote-definition ref info)))))
  1374. info)
  1375. (reverse all-refs))))
  1376. (funcall search-refs element))
  1377. ""))
  1378. (defun org-latex--translate (s info)
  1379. "Translate string S according to specified language.
  1380. INFO is a plist used as a communication channel."
  1381. (org-export-translate s :latex info))
  1382. (defun org-latex--format-spec (info)
  1383. "Create a format-spec for document meta-data.
  1384. INFO is a plist used as a communication channel."
  1385. (let ((language (let ((lang (plist-get info :language)))
  1386. (or (cdr (assoc-string lang org-latex-babel-language-alist t))
  1387. (nth 1 (assoc-string lang org-latex-polyglossia-language-alist t))
  1388. lang))))
  1389. `((?a . ,(org-export-data (plist-get info :author) info))
  1390. (?t . ,(org-export-data (plist-get info :title) info))
  1391. (?s . ,(org-export-data (plist-get info :subtitle) info))
  1392. (?k . ,(org-export-data (org-latex--wrap-latex-math-block
  1393. (plist-get info :keywords) info)
  1394. info))
  1395. (?d . ,(org-export-data (org-latex--wrap-latex-math-block
  1396. (plist-get info :description) info)
  1397. info))
  1398. (?c . ,(plist-get info :creator))
  1399. (?l . ,language)
  1400. (?L . ,(capitalize language))
  1401. (?D . ,(org-export-get-date info)))))
  1402. (defun org-latex--insert-compiler (info)
  1403. "Insert LaTeX_compiler info into the document.
  1404. INFO is a plist used as a communication channel."
  1405. (let ((compiler (plist-get info :latex-compiler)))
  1406. (and (org-string-nw-p org-latex-compiler-file-string)
  1407. (member (or compiler "") org-latex-compilers)
  1408. (format org-latex-compiler-file-string compiler))))
  1409. ;;; Filters
  1410. (defun org-latex-matrices-tree-filter (tree _backend info)
  1411. (org-latex--wrap-latex-matrices tree info))
  1412. (defun org-latex-math-block-tree-filter (tree _backend info)
  1413. (org-latex--wrap-latex-math-block tree info))
  1414. (defun org-latex-math-block-options-filter (info _backend)
  1415. (dolist (prop '(:author :date :title) info)
  1416. (plist-put info prop
  1417. (org-latex--wrap-latex-math-block (plist-get info prop) info))))
  1418. (defun org-latex-clean-invalid-line-breaks (data _backend _info)
  1419. (replace-regexp-in-string
  1420. "\\(\\\\end{[A-Za-z0-9*]+}\\|^\\)[ \t]*\\\\\\\\[ \t]*$" "\\1"
  1421. data))
  1422. ;;; Template
  1423. ;;;###autoload
  1424. (defun org-latex-make-preamble (info &optional template snippet?)
  1425. "Return a formatted LaTeX preamble.
  1426. INFO is a plist used as a communication channel. Optional
  1427. argument TEMPLATE, when non-nil, is the header template string,
  1428. as expected by `org-splice-latex-header'. When SNIPPET? is
  1429. non-nil, only includes packages relevant to image generation, as
  1430. specified in `org-latex-default-packages-alist' or
  1431. `org-latex-packages-alist'."
  1432. (let* ((class (plist-get info :latex-class))
  1433. (class-template
  1434. (or template
  1435. (let* ((class-options (plist-get info :latex-class-options))
  1436. (header (nth 1 (assoc class (plist-get info :latex-classes)))))
  1437. (and (stringp header)
  1438. (if (not class-options) header
  1439. (replace-regexp-in-string
  1440. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  1441. class-options header t nil 1))))
  1442. (user-error "Unknown LaTeX class `%s'" class))))
  1443. (org-latex-guess-polyglossia-language
  1444. (org-latex-guess-babel-language
  1445. (org-latex-guess-inputenc
  1446. (org-element-normalize-string
  1447. (org-splice-latex-header
  1448. class-template
  1449. (org-latex--remove-packages org-latex-default-packages-alist info)
  1450. (org-latex--remove-packages org-latex-packages-alist info)
  1451. snippet?
  1452. (mapconcat #'org-element-normalize-string
  1453. (list (plist-get info :latex-header)
  1454. (and (not snippet?)
  1455. (plist-get info :latex-header-extra)))
  1456. ""))))
  1457. info)
  1458. info)))
  1459. (defun org-latex-template (contents info)
  1460. "Return complete document string after LaTeX conversion.
  1461. CONTENTS is the transcoded contents string. INFO is a plist
  1462. holding export options."
  1463. (let ((title (org-export-data (plist-get info :title) info))
  1464. (spec (org-latex--format-spec info)))
  1465. (concat
  1466. ;; Time-stamp.
  1467. (and (plist-get info :time-stamp-file)
  1468. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  1469. ;; LaTeX compiler.
  1470. (org-latex--insert-compiler info)
  1471. ;; Document class and packages.
  1472. (org-latex-make-preamble info)
  1473. ;; Possibly limit depth for headline numbering.
  1474. (let ((sec-num (plist-get info :section-numbers)))
  1475. (when (integerp sec-num)
  1476. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  1477. ;; Author.
  1478. (let ((author (and (plist-get info :with-author)
  1479. (let ((auth (plist-get info :author)))
  1480. (and auth (org-export-data auth info)))))
  1481. (email (and (plist-get info :with-email)
  1482. (org-export-data (plist-get info :email) info))))
  1483. (cond ((and author email (not (string= "" email)))
  1484. (format "\\author{%s\\thanks{%s}}\n" author email))
  1485. ((or author email) (format "\\author{%s}\n" (or author email)))))
  1486. ;; Date.
  1487. (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
  1488. (format "\\date{%s}\n" (org-export-data date info)))
  1489. ;; Title and subtitle.
  1490. (let* ((subtitle (plist-get info :subtitle))
  1491. (formatted-subtitle
  1492. (when subtitle
  1493. (format (plist-get info :latex-subtitle-format)
  1494. (org-export-data subtitle info))))
  1495. (separate (plist-get info :latex-subtitle-separate)))
  1496. (concat
  1497. (format "\\title{%s%s}\n" title
  1498. (if separate "" (or formatted-subtitle "")))
  1499. (when (and separate subtitle)
  1500. (concat formatted-subtitle "\n"))))
  1501. ;; Hyperref options.
  1502. (let ((template (plist-get info :latex-hyperref-template)))
  1503. (and (stringp template)
  1504. (format-spec template spec)))
  1505. ;; Document start.
  1506. "\\begin{document}\n\n"
  1507. ;; Title command.
  1508. (let* ((title-command (plist-get info :latex-title-command))
  1509. (command (and (stringp title-command)
  1510. (format-spec title-command spec))))
  1511. (org-element-normalize-string
  1512. (cond ((not (plist-get info :with-title)) nil)
  1513. ((string= "" title) nil)
  1514. ((not (stringp command)) nil)
  1515. ((string-match "\\(?:[^%]\\|^\\)%s" command)
  1516. (format command title))
  1517. (t command))))
  1518. ;; Table of contents.
  1519. (let ((depth (plist-get info :with-toc)))
  1520. (when depth
  1521. (concat (when (integerp depth)
  1522. (format "\\setcounter{tocdepth}{%d}\n" depth))
  1523. (plist-get info :latex-toc-command))))
  1524. ;; Document's body.
  1525. contents
  1526. ;; Creator.
  1527. (and (plist-get info :with-creator)
  1528. (concat (plist-get info :creator) "\n"))
  1529. ;; Document end.
  1530. "\\end{document}")))
  1531. ;;; Transcode Functions
  1532. ;;;; Bold
  1533. (defun org-latex-bold (_bold contents info)
  1534. "Transcode BOLD from Org to LaTeX.
  1535. CONTENTS is the text with bold markup. INFO is a plist holding
  1536. contextual information."
  1537. (org-latex--text-markup contents 'bold info))
  1538. ;;;; Center Block
  1539. (defun org-latex-center-block (center-block contents info)
  1540. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  1541. CONTENTS holds the contents of the center block. INFO is a plist
  1542. holding contextual information."
  1543. (org-latex--wrap-label
  1544. center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
  1545. ;;;; Clock
  1546. (defun org-latex-clock (clock _contents info)
  1547. "Transcode a CLOCK element from Org to LaTeX.
  1548. CONTENTS is nil. INFO is a plist holding contextual
  1549. information."
  1550. (concat
  1551. "\\noindent"
  1552. (format "\\textbf{%s} " org-clock-string)
  1553. (format (plist-get info :latex-inactive-timestamp-format)
  1554. (concat (org-timestamp-translate (org-element-property :value clock))
  1555. (let ((time (org-element-property :duration clock)))
  1556. (and time (format " (%s)" time)))))
  1557. "\\\\"))
  1558. ;;;; Code
  1559. (defun org-latex-code (code _contents info)
  1560. "Transcode a CODE object from Org to LaTeX.
  1561. CONTENTS is nil. INFO is a plist used as a communication
  1562. channel."
  1563. (org-latex--text-markup (org-element-property :value code) 'code info))
  1564. ;;;; Drawer
  1565. (defun org-latex-drawer (drawer contents info)
  1566. "Transcode a DRAWER element from Org to LaTeX.
  1567. CONTENTS holds the contents of the block. INFO is a plist
  1568. holding contextual information."
  1569. (let* ((name (org-element-property :drawer-name drawer))
  1570. (output (funcall (plist-get info :latex-format-drawer-function)
  1571. name contents)))
  1572. (org-latex--wrap-label drawer output info)))
  1573. ;;;; Dynamic Block
  1574. (defun org-latex-dynamic-block (dynamic-block contents info)
  1575. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1576. CONTENTS holds the contents of the block. INFO is a plist
  1577. holding contextual information. See `org-export-data'."
  1578. (org-latex--wrap-label dynamic-block contents info))
  1579. ;;;; Entity
  1580. (defun org-latex-entity (entity _contents _info)
  1581. "Transcode an ENTITY object from Org to LaTeX.
  1582. CONTENTS are the definition itself. INFO is a plist holding
  1583. contextual information."
  1584. (org-element-property :latex entity))
  1585. ;;;; Example Block
  1586. (defun org-latex-example-block (example-block _contents info)
  1587. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1588. CONTENTS is nil. INFO is a plist holding contextual
  1589. information."
  1590. (when (org-string-nw-p (org-element-property :value example-block))
  1591. (let ((environment (or (org-export-read-attribute
  1592. :attr_latex example-block :environment)
  1593. "verbatim")))
  1594. (org-latex--wrap-label
  1595. example-block
  1596. (format "\\begin{%s}\n%s\\end{%s}"
  1597. environment
  1598. (org-export-format-code-default example-block info)
  1599. environment)
  1600. info))))
  1601. ;;;; Export Block
  1602. (defun org-latex-export-block (export-block _contents _info)
  1603. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1604. CONTENTS is nil. INFO is a plist holding contextual information."
  1605. (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
  1606. (org-remove-indentation (org-element-property :value export-block))))
  1607. ;;;; Export Snippet
  1608. (defun org-latex-export-snippet (export-snippet _contents _info)
  1609. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1610. CONTENTS is nil. INFO is a plist holding contextual information."
  1611. (when (eq (org-export-snippet-backend export-snippet) 'latex)
  1612. (org-element-property :value export-snippet)))
  1613. ;;;; Fixed Width
  1614. (defun org-latex-fixed-width (fixed-width _contents info)
  1615. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1616. CONTENTS is nil. INFO is a plist holding contextual information."
  1617. (org-latex--wrap-label
  1618. fixed-width
  1619. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1620. (org-remove-indentation
  1621. (org-element-property :value fixed-width)))
  1622. info))
  1623. ;;;; Footnote Reference
  1624. (defun org-latex-footnote-reference (footnote-reference _contents info)
  1625. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1626. CONTENTS is nil. INFO is a plist holding contextual information."
  1627. (let ((label (org-element-property :label footnote-reference)))
  1628. (concat
  1629. ;; Insert separator between two footnotes in a row.
  1630. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1631. (when (eq (org-element-type prev) 'footnote-reference)
  1632. (plist-get info :latex-footnote-separator)))
  1633. (cond
  1634. ;; Use `:latex-footnote-defined-format' if the footnote has
  1635. ;; already been defined.
  1636. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1637. (format (plist-get info :latex-footnote-defined-format)
  1638. (org-latex--label
  1639. (org-export-get-footnote-definition footnote-reference info)
  1640. info t)))
  1641. ;; Use \footnotemark if reference is within another footnote
  1642. ;; reference, footnote definition, table cell, verse block, or
  1643. ;; item's tag.
  1644. ((or (org-element-lineage footnote-reference
  1645. '(footnote-reference footnote-definition
  1646. table-cell verse-block))
  1647. (eq 'item (org-element-type
  1648. (org-export-get-parent-element footnote-reference))))
  1649. "\\footnotemark")
  1650. ;; Otherwise, define it with \footnote command.
  1651. (t
  1652. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1653. (concat
  1654. (format "\\footnote{%s%s}" (org-trim (org-export-data def info))
  1655. ;; Only insert a \label if there exist another
  1656. ;; reference to def.
  1657. (cond ((not label) "")
  1658. ((org-element-map (plist-get info :parse-tree)
  1659. 'footnote-reference
  1660. (lambda (f)
  1661. (and (not (eq f footnote-reference))
  1662. (equal (org-element-property :label f) label)
  1663. (org-trim (org-latex--label def info t t))))
  1664. info t))
  1665. (t "")))
  1666. ;; Retrieve all footnote references within the footnote and
  1667. ;; add their definition after it, since LaTeX doesn't support
  1668. ;; them inside.
  1669. (org-latex--delayed-footnotes-definitions def info))))))))
  1670. ;;;; Headline
  1671. (defun org-latex-headline (headline contents info)
  1672. "Transcode a HEADLINE element from Org to LaTeX.
  1673. CONTENTS holds the contents of the headline. INFO is a plist
  1674. holding contextual information."
  1675. (unless (org-element-property :footnote-section-p headline)
  1676. (let* ((class (plist-get info :latex-class))
  1677. (level (org-export-get-relative-level headline info))
  1678. (numberedp (org-export-numbered-headline-p headline info))
  1679. (class-sectioning (assoc class (plist-get info :latex-classes)))
  1680. ;; Section formatting will set two placeholders: one for
  1681. ;; the title and the other for the contents.
  1682. (section-fmt
  1683. (let ((sec (if (functionp (nth 2 class-sectioning))
  1684. (funcall (nth 2 class-sectioning) level numberedp)
  1685. (nth (1+ level) class-sectioning))))
  1686. (cond
  1687. ;; No section available for that LEVEL.
  1688. ((not sec) nil)
  1689. ;; Section format directly returned by a function. Add
  1690. ;; placeholder for contents.
  1691. ((stringp sec) (concat sec "\n%s"))
  1692. ;; (numbered-section . unnumbered-section)
  1693. ((not (consp (cdr sec)))
  1694. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1695. ;; (numbered-open numbered-close)
  1696. ((= (length sec) 2)
  1697. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1698. ;; (num-in num-out no-num-in no-num-out)
  1699. ((= (length sec) 4)
  1700. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1701. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1702. ;; Create a temporary export back-end that hard-codes
  1703. ;; "\underline" within "\section" and alike.
  1704. (section-back-end
  1705. (org-export-create-backend
  1706. :parent 'latex
  1707. :transcoders
  1708. '((underline . (lambda (o c i) (format "\\underline{%s}" c)))
  1709. ;; LaTeX isn't happy when you try to use \verb inside the argument of other
  1710. ;; commands (like \section, etc.), and this causes compilation to fail.
  1711. ;; So, within headings it's a good idea to replace any instances of \verb
  1712. ;; with \texttt.
  1713. (code . (lambda (o _ _) (org-latex--protect-texttt (org-element-property :value o))))
  1714. (verbatim . (lambda (o _ _) (org-latex--protect-texttt (org-element-property :value o)))))))
  1715. (text
  1716. (org-export-data-with-backend
  1717. (org-element-property :title headline) section-back-end info))
  1718. (todo
  1719. (and (plist-get info :with-todo-keywords)
  1720. (let ((todo (org-element-property :todo-keyword headline)))
  1721. (and todo (org-export-data todo info)))))
  1722. (todo-type (and todo (org-element-property :todo-type headline)))
  1723. (tags (and (plist-get info :with-tags)
  1724. (org-export-get-tags headline info)))
  1725. (priority (and (plist-get info :with-priority)
  1726. (org-element-property :priority headline)))
  1727. ;; Create the headline text along with a no-tag version.
  1728. ;; The latter is required to remove tags from toc.
  1729. (full-text (funcall (plist-get info :latex-format-headline-function)
  1730. todo todo-type priority text tags info))
  1731. ;; Associate \label to the headline for internal links.
  1732. (headline-label (org-latex--label headline info t t))
  1733. (pre-blanks
  1734. (make-string (org-element-property :pre-blank headline) ?\n)))
  1735. (if (or (not section-fmt) (org-export-low-level-p headline info))
  1736. ;; This is a deep sub-tree: export it as a list item. Also
  1737. ;; export as items headlines for which no section format has
  1738. ;; been found.
  1739. (let ((low-level-body
  1740. (concat
  1741. ;; If headline is the first sibling, start a list.
  1742. (when (org-export-first-sibling-p headline info)
  1743. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1744. ;; Itemize headline
  1745. "\\item"
  1746. (and full-text
  1747. (string-match-p "\\`[ \t]*\\[" full-text)
  1748. "\\relax")
  1749. " " full-text "\n"
  1750. headline-label
  1751. pre-blanks
  1752. contents)))
  1753. ;; If headline is not the last sibling simply return
  1754. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
  1755. ;; any blank line.
  1756. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1757. (replace-regexp-in-string
  1758. "[ \t\n]*\\'"
  1759. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1760. low-level-body)))
  1761. ;; This is a standard headline. Export it as a section. Add
  1762. ;; an alternative heading when possible, and when this is not
  1763. ;; identical to the usual heading.
  1764. (let ((opt-title
  1765. (funcall (plist-get info :latex-format-headline-function)
  1766. todo todo-type priority
  1767. (org-export-data-with-backend
  1768. (org-export-get-alt-title headline info)
  1769. section-back-end info)
  1770. (and (eq (plist-get info :with-tags) t) tags)
  1771. info))
  1772. ;; Maybe end local TOC (see `org-latex-keyword').
  1773. (contents
  1774. (concat
  1775. contents
  1776. (let ((case-fold-search t)
  1777. (section
  1778. (let ((first (car (org-element-contents headline))))
  1779. (and (eq (org-element-type first) 'section) first))))
  1780. (org-element-map section 'keyword
  1781. (lambda (k)
  1782. (and (equal (org-element-property :key k) "TOC")
  1783. (let ((v (org-element-property :value k)))
  1784. (and (string-match-p "\\<headlines\\>" v)
  1785. (string-match-p "\\<local\\>" v)
  1786. (format "\\stopcontents[level-%d]" level)))))
  1787. info t)))))
  1788. (if (and opt-title
  1789. (not (equal opt-title full-text))
  1790. (string-match "\\`\\\\\\(.+?\\){" section-fmt))
  1791. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  1792. ;; Replace square brackets with parenthesis
  1793. ;; since square brackets are not supported in
  1794. ;; optional arguments.
  1795. (replace-regexp-in-string
  1796. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  1797. full-text
  1798. (concat headline-label pre-blanks contents))
  1799. ;; Impossible to add an alternative heading. Fallback to
  1800. ;; regular sectioning format string.
  1801. (format section-fmt full-text
  1802. (concat headline-label pre-blanks contents))))))))
  1803. (defun org-latex-format-headline-default-function
  1804. (todo _todo-type priority text tags _info)
  1805. "Default format function for a headline.
  1806. See `org-latex-format-headline-function' for details."
  1807. (concat
  1808. (and todo (format "{\\bfseries\\sffamily %s} " todo))
  1809. (and priority (format "\\framebox{\\#%c} " priority))
  1810. text
  1811. (and tags
  1812. (format "\\hfill{}\\textsc{%s}"
  1813. (mapconcat #'org-latex--protect-text tags ":")))))
  1814. ;;;; Horizontal Rule
  1815. (defun org-latex-horizontal-rule (horizontal-rule _contents info)
  1816. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1817. CONTENTS is nil. INFO is a plist holding contextual information."
  1818. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1819. (prev (org-export-get-previous-element horizontal-rule info)))
  1820. (concat
  1821. ;; Make sure the rule doesn't start at the end of the current
  1822. ;; line by separating it with a blank line from previous element.
  1823. (when (and prev
  1824. (let ((prev-blank (org-element-property :post-blank prev)))
  1825. (or (not prev-blank) (zerop prev-blank))))
  1826. "\n")
  1827. (org-latex--wrap-label
  1828. horizontal-rule
  1829. (format "\\noindent\\rule{%s}{%s}"
  1830. (or (plist-get attr :width) "\\textwidth")
  1831. (or (plist-get attr :thickness) "0.5pt"))
  1832. info))))
  1833. ;;;; Inline Src Block
  1834. (defun org-latex-inline-src-block (inline-src-block _contents info)
  1835. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1836. CONTENTS holds the contents of the item. INFO is a plist holding
  1837. contextual information."
  1838. (let* ((code (org-element-property :value inline-src-block))
  1839. (separator (org-latex--find-verb-separator code)))
  1840. (cl-case (plist-get info :latex-listings)
  1841. ;; Do not use a special package: transcode it verbatim, as code.
  1842. ((nil) (org-latex--text-markup code 'code info))
  1843. ;; Use minted package.
  1844. (minted
  1845. (let* ((org-lang (org-element-property :language inline-src-block))
  1846. (mint-lang (or (cadr (assq (intern org-lang)
  1847. (plist-get info :latex-minted-langs)))
  1848. (downcase org-lang)))
  1849. (options (org-latex--make-option-string
  1850. (plist-get info :latex-minted-options))))
  1851. (format "\\mintinline%s{%s}{%s}"
  1852. (if (string= options "") "" (format "[%s]" options))
  1853. mint-lang
  1854. code)))
  1855. ;; Use listings package.
  1856. (otherwise
  1857. ;; Maybe translate language's name.
  1858. (let* ((org-lang (org-element-property :language inline-src-block))
  1859. (lst-lang (or (cadr (assq (intern org-lang)
  1860. (plist-get info :latex-listings-langs)))
  1861. org-lang))
  1862. (options (org-latex--make-option-string
  1863. (append (plist-get info :latex-listings-options)
  1864. `(("language" ,lst-lang))))))
  1865. (concat (format "\\lstinline[%s]" options)
  1866. separator code separator))))))
  1867. ;;;; Inlinetask
  1868. (defun org-latex-inlinetask (inlinetask contents info)
  1869. "Transcode an INLINETASK element from Org to LaTeX.
  1870. CONTENTS holds the contents of the block. INFO is a plist
  1871. holding contextual information."
  1872. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1873. (todo (and (plist-get info :with-todo-keywords)
  1874. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1875. (and todo (org-export-data todo info)))))
  1876. (todo-type (org-element-property :todo-type inlinetask))
  1877. (tags (and (plist-get info :with-tags)
  1878. (org-export-get-tags inlinetask info)))
  1879. (priority (and (plist-get info :with-priority)
  1880. (org-element-property :priority inlinetask)))
  1881. (contents (concat (org-latex--label inlinetask info) contents)))
  1882. (funcall (plist-get info :latex-format-inlinetask-function)
  1883. todo todo-type priority title tags contents info)))
  1884. (defun org-latex-format-inlinetask-default-function
  1885. (todo _todo-type priority title tags contents _info)
  1886. "Default format function for inlinetasks.
  1887. See `org-latex-format-inlinetask-function' for details."
  1888. (let ((full-title
  1889. (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1890. (when priority (format "\\framebox{\\#%c} " priority))
  1891. title
  1892. (when tags
  1893. (format "\\hfill{}\\textsc{%s}"
  1894. (org-make-tag-string
  1895. (mapcar #'org-latex--protect-text tags)))))))
  1896. (concat "\\begin{center}\n"
  1897. "\\fbox{\n"
  1898. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1899. full-title "\n\n"
  1900. (and (org-string-nw-p contents)
  1901. (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
  1902. "\\end{minipage}\n"
  1903. "}\n"
  1904. "\\end{center}")))
  1905. ;;;; Italic
  1906. (defun org-latex-italic (_italic contents info)
  1907. "Transcode ITALIC from Org to LaTeX.
  1908. CONTENTS is the text with italic markup. INFO is a plist holding
  1909. contextual information."
  1910. (org-latex--text-markup contents 'italic info))
  1911. ;;;; Item
  1912. (defun org-latex-item (item contents info)
  1913. "Transcode an ITEM element from Org to LaTeX.
  1914. CONTENTS holds the contents of the item. INFO is a plist holding
  1915. contextual information."
  1916. (let* ((orderedp (eq (org-element-property
  1917. :type (org-export-get-parent item))
  1918. 'ordered))
  1919. (level
  1920. ;; Determine level of current item to determine the
  1921. ;; correct LaTeX counter to use (enumi, enumii...).
  1922. (let ((parent item) (level 0))
  1923. (while (memq (org-element-type
  1924. (setq parent (org-export-get-parent parent)))
  1925. '(plain-list item))
  1926. (when (and (eq (org-element-type parent) 'plain-list)
  1927. (eq (org-element-property :type parent)
  1928. 'ordered))
  1929. (cl-incf level)))
  1930. level))
  1931. (count (org-element-property :counter item))
  1932. (counter (and count
  1933. (< level 5)
  1934. (format "\\setcounter{enum%s}{%s}\n"
  1935. (nth (1- level) '("i" "ii" "iii" "iv"))
  1936. (1- count))))
  1937. (checkbox (cl-case (org-element-property :checkbox item)
  1938. (on "$\\boxtimes$")
  1939. (off "$\\square$")
  1940. (trans "$\\boxminus$")))
  1941. (tag (let ((tag (org-element-property :tag item)))
  1942. (and tag (org-export-data tag info))))
  1943. ;; If there are footnotes references in tag, be sure to add
  1944. ;; their definition at the end of the item. This workaround
  1945. ;; is necessary since "\footnote{}" command is not supported
  1946. ;; in tags.
  1947. (tag-footnotes
  1948. (or (and tag (org-latex--delayed-footnotes-definitions
  1949. (org-element-property :tag item) info))
  1950. "")))
  1951. (concat counter
  1952. "\\item"
  1953. (cond
  1954. ((and checkbox tag)
  1955. (format (if orderedp "{%s %s} %s" "[{%s %s}] %s")
  1956. checkbox tag tag-footnotes))
  1957. ((or checkbox tag)
  1958. (format (if orderedp "{%s} %s" "[{%s}] %s")
  1959. (or checkbox tag) tag-footnotes))
  1960. ;; Without a tag or a check-box, if CONTENTS starts with
  1961. ;; an opening square bracket, add "\relax" to "\item",
  1962. ;; unless the brackets comes from an initial export
  1963. ;; snippet (i.e. it is inserted willingly by the user).
  1964. ((and contents
  1965. (string-match-p "\\`[ \t]*\\[" contents)
  1966. (not (let ((e (car (org-element-contents item))))
  1967. (and (eq (org-element-type e) 'paragraph)
  1968. (let ((o (car (org-element-contents e))))
  1969. (and (eq (org-element-type o) 'export-snippet)
  1970. (eq (org-export-snippet-backend o)
  1971. 'latex)))))))
  1972. "\\relax ")
  1973. (t " "))
  1974. (and contents (org-trim contents)))))
  1975. ;;;; Keyword
  1976. (defun org-latex-keyword (keyword _contents info)
  1977. "Transcode a KEYWORD element from Org to LaTeX.
  1978. CONTENTS is nil. INFO is a plist holding contextual information."
  1979. (let ((key (org-element-property :key keyword))
  1980. (value (org-element-property :value keyword)))
  1981. (cond
  1982. ((string= key "LATEX") value)
  1983. ((string= key "INDEX") (format "\\index{%s}" value))
  1984. ((string= key "TOC")
  1985. (let ((case-fold-search t))
  1986. (cond
  1987. ((string-match-p "\\<headlines\\>" value)
  1988. (let* ((localp (string-match-p "\\<local\\>" value))
  1989. (parent (org-element-lineage keyword '(headline)))
  1990. (level (if (not (and localp parent)) 0
  1991. (org-export-get-relative-level parent info)))
  1992. (depth
  1993. (and (string-match "\\<[0-9]+\\>" value)
  1994. (format
  1995. "\\setcounter{tocdepth}{%d}"
  1996. (+ (string-to-number (match-string 0 value)) level)))))
  1997. (if (and localp parent)
  1998. ;; Start local TOC, assuming package "titletoc" is
  1999. ;; required.
  2000. (format "\\startcontents[level-%d]
  2001. \\printcontents[level-%d]{}{0}{%s}"
  2002. level level (or depth ""))
  2003. (concat depth (and depth "\n") "\\tableofcontents"))))
  2004. ((string-match-p "\\<tables\\>" value) "\\listoftables")
  2005. ((string-match-p "\\<listings\\>" value)
  2006. (cl-case (plist-get info :latex-listings)
  2007. ((nil) "\\listoffigures")
  2008. (minted "\\listoflistings")
  2009. (otherwise "\\lstlistoflistings")))))))))
  2010. ;;;; Latex Environment
  2011. (defun org-latex--environment-type (latex-environment)
  2012. "Return the TYPE of LATEX-ENVIRONMENT.
  2013. The TYPE is determined from the actual latex environment, and
  2014. could be a member of `org-latex-caption-above' or `math'."
  2015. (let* ((latex-begin-re "\\\\begin{\\([A-Za-z0-9*]+\\)}")
  2016. (value (org-remove-indentation
  2017. (org-element-property :value latex-environment)))
  2018. (env (or (and (string-match latex-begin-re value)
  2019. (match-string 1 value))
  2020. "")))
  2021. (cond
  2022. ((string-match-p org-latex-math-environments-re value) 'math)
  2023. ((string-match-p
  2024. (eval-when-compile
  2025. (regexp-opt '("table" "longtable" "tabular" "tabu" "longtabu")))
  2026. env)
  2027. 'table)
  2028. ((string-match-p "figure" env) 'image)
  2029. ((string-match-p
  2030. (eval-when-compile
  2031. (regexp-opt '("lstlisting" "listing" "verbatim" "minted")))
  2032. env)
  2033. 'src-block)
  2034. (t 'special-block))))
  2035. (defun org-latex-latex-environment (latex-environment _contents info)
  2036. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  2037. CONTENTS is nil. INFO is a plist holding contextual information."
  2038. (when (plist-get info :with-latex)
  2039. (let* ((value (org-remove-indentation
  2040. (org-element-property :value latex-environment)))
  2041. (type (org-latex--environment-type latex-environment))
  2042. (caption (if (eq type 'math)
  2043. (org-latex--label latex-environment info nil t)
  2044. (org-latex--caption/label-string latex-environment info)))
  2045. (caption-above-p
  2046. (memq type (append (plist-get info :latex-caption-above) '(math)))))
  2047. (if (not (or (org-element-property :name latex-environment)
  2048. (org-element-property :caption latex-environment)))
  2049. value
  2050. ;; Environment is labeled: label must be within the environment
  2051. ;; (otherwise, a reference pointing to that element will count
  2052. ;; the section instead). Also insert caption if `latex-environment'
  2053. ;; is not a math environment.
  2054. (with-temp-buffer
  2055. (insert value)
  2056. (if caption-above-p
  2057. (progn
  2058. (goto-char (point-min))
  2059. (forward-line))
  2060. (goto-char (point-max))
  2061. (forward-line -1))
  2062. (insert caption)
  2063. (buffer-string))))))
  2064. ;;;; Latex Fragment
  2065. (defun org-latex-latex-fragment (latex-fragment _contents _info)
  2066. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  2067. CONTENTS is nil. INFO is a plist holding contextual information."
  2068. (let ((value (org-element-property :value latex-fragment)))
  2069. ;; Trim math markers since the fragment is enclosed within
  2070. ;; a latex-math-block object anyway.
  2071. (cond ((string-match-p "\\`\\$[^$]" value) (substring value 1 -1))
  2072. ((string-prefix-p "\\(" value) (substring value 2 -2))
  2073. (t value))))
  2074. ;;;; Line Break
  2075. (defun org-latex-line-break (_line-break _contents _info)
  2076. "Transcode a LINE-BREAK object from Org to LaTeX.
  2077. CONTENTS is nil. INFO is a plist holding contextual information."
  2078. "\\\\\n")
  2079. ;;;; Link
  2080. (defun org-latex-image-link-filter (data _backend info)
  2081. (org-export-insert-image-links data info org-latex-inline-image-rules))
  2082. (defun org-latex--inline-image (link info)
  2083. "Return LaTeX code for an inline image.
  2084. LINK is the link pointing to the inline image. INFO is a plist
  2085. used as a communication channel."
  2086. (let* ((parent (org-export-get-parent-element link))
  2087. (path (let ((raw-path (org-element-property :path link)))
  2088. (if (not (file-name-absolute-p raw-path)) raw-path
  2089. (expand-file-name raw-path))))
  2090. (filetype (file-name-extension path))
  2091. (caption (org-latex--caption/label-string parent info))
  2092. (caption-above-p (org-latex--caption-above-p link info))
  2093. ;; Retrieve latex attributes from the element around.
  2094. (attr (org-export-read-attribute :attr_latex parent))
  2095. (float (let ((float (plist-get attr :float)))
  2096. (cond ((string= float "wrap") 'wrap)
  2097. ((string= float "sideways") 'sideways)
  2098. ((string= float "multicolumn") 'multicolumn)
  2099. ((and (plist-member attr :float) (not float)) 'nonfloat)
  2100. (float float)
  2101. ((or (org-element-property :caption parent)
  2102. (org-string-nw-p (plist-get attr :caption)))
  2103. 'figure)
  2104. (t 'nonfloat))))
  2105. (placement
  2106. (let ((place (plist-get attr :placement)))
  2107. (cond
  2108. (place (format "%s" place))
  2109. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  2110. ((eq float 'figure)
  2111. (format "[%s]" (plist-get info :latex-default-figure-position)))
  2112. (t ""))))
  2113. (center
  2114. (cond
  2115. ;; If link is an image link, do not center.
  2116. ((eq 'link (org-element-type (org-export-get-parent link))) nil)
  2117. ((plist-member attr :center) (plist-get attr :center))
  2118. (t (plist-get info :latex-images-centered))))
  2119. (comment-include (if (plist-get attr :comment-include) "%" ""))
  2120. ;; It is possible to specify scale or width and height in
  2121. ;; the ATTR_LATEX line, and also via default variables.
  2122. (scale (cond ((eq float 'wrap) "")
  2123. ((plist-get attr :scale))
  2124. (t (plist-get info :latex-image-default-scale))))
  2125. (width (cond ((org-string-nw-p scale) "")
  2126. ((plist-get attr :width))
  2127. ((plist-get attr :height) "")
  2128. ((eq float 'wrap) "0.48\\textwidth")
  2129. (t (plist-get info :latex-image-default-width))))
  2130. (height (cond ((org-string-nw-p scale) "")
  2131. ((plist-get attr :height))
  2132. ((or (plist-get attr :width)
  2133. (memq float '(figure wrap))) "")
  2134. (t (plist-get info :latex-image-default-height))))
  2135. (options (let ((opt (or (plist-get attr :options)
  2136. (plist-get info :latex-image-default-option))))
  2137. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  2138. (match-string 1 opt))))
  2139. image-code)
  2140. (if (member filetype '("tikz" "pgf"))
  2141. ;; For tikz images:
  2142. ;; - use \input to read in image file.
  2143. ;; - if options are present, wrap in a tikzpicture environment.
  2144. ;; - if width or height are present, use \resizebox to change
  2145. ;; the image size.
  2146. (progn
  2147. (setq image-code (format "\\input{%s}" path))
  2148. (when (org-string-nw-p options)
  2149. (setq image-code
  2150. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  2151. options
  2152. image-code)))
  2153. (setq image-code
  2154. (cond ((org-string-nw-p scale)
  2155. (format "\\scalebox{%s}{%s}" scale image-code))
  2156. ((or (org-string-nw-p width) (org-string-nw-p height))
  2157. (format "\\resizebox{%s}{%s}{%s}"
  2158. (if (org-string-nw-p width) width "!")
  2159. (if (org-string-nw-p height) height "!")
  2160. image-code))
  2161. (t image-code))))
  2162. ;; For other images:
  2163. ;; - add scale, or width and height to options.
  2164. ;; - include the image with \includegraphics.
  2165. (if (org-string-nw-p scale)
  2166. (setq options (concat options ",scale=" scale))
  2167. (when (org-string-nw-p width) (setq options (concat options ",width=" width)))
  2168. (when (org-string-nw-p height) (setq options (concat options ",height=" height))))
  2169. (let ((search-option (org-element-property :search-option link)))
  2170. (when (and search-option
  2171. (equal filetype "pdf")
  2172. (string-match-p "\\`[0-9]+\\'" search-option)
  2173. (not (string-match-p "page=" options)))
  2174. (setq options (concat options ",page=" search-option))))
  2175. (setq image-code
  2176. (format "\\includegraphics%s{%s}"
  2177. (cond ((not (org-string-nw-p options)) "")
  2178. ((string-prefix-p "," options)
  2179. (format "[%s]" (substring options 1)))
  2180. (t (format "[%s]" options)))
  2181. path))
  2182. (when (equal filetype "svg")
  2183. (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
  2184. "\\includesvg"
  2185. image-code
  2186. nil t))
  2187. (setq image-code (replace-regexp-in-string "\\.svg}"
  2188. "}"
  2189. image-code
  2190. nil t))))
  2191. ;; Return proper string, depending on FLOAT.
  2192. (pcase float
  2193. ((and (pred stringp) env-string)
  2194. (format "\\begin{%s}%s
  2195. %s%s
  2196. %s%s
  2197. %s\\end{%s}"
  2198. env-string
  2199. placement
  2200. (if caption-above-p caption "")
  2201. (if center "\\centering" "")
  2202. comment-include image-code
  2203. (if caption-above-p "" caption)
  2204. env-string))
  2205. (`wrap (format "\\begin{wrapfigure}%s
  2206. %s%s
  2207. %s%s
  2208. %s\\end{wrapfigure}"
  2209. placement
  2210. (if caption-above-p caption "")
  2211. (if center "\\centering" "")
  2212. comment-include image-code
  2213. (if caption-above-p "" caption)))
  2214. (`sideways (format "\\begin{sidewaysfigure}
  2215. %s%s
  2216. %s%s
  2217. %s\\end{sidewaysfigure}"
  2218. (if caption-above-p caption "")
  2219. (if center "\\centering" "")
  2220. comment-include image-code
  2221. (if caption-above-p "" caption)))
  2222. (`multicolumn (format "\\begin{figure*}%s
  2223. %s%s
  2224. %s%s
  2225. %s\\end{figure*}"
  2226. placement
  2227. (if caption-above-p caption "")
  2228. (if center "\\centering" "")
  2229. comment-include image-code
  2230. (if caption-above-p "" caption)))
  2231. (`figure (format "\\begin{figure}%s
  2232. %s%s
  2233. %s%s
  2234. %s\\end{figure}"
  2235. placement
  2236. (if caption-above-p caption "")
  2237. (if center "\\centering" "")
  2238. comment-include image-code
  2239. (if caption-above-p "" caption)))
  2240. ((guard center)
  2241. (format "\\begin{center}
  2242. %s%s
  2243. %s\\end{center}"
  2244. (if caption-above-p caption "")
  2245. image-code
  2246. (if caption-above-p "" caption)))
  2247. (_
  2248. (concat (if caption-above-p caption "")
  2249. image-code
  2250. (if caption-above-p caption ""))))))
  2251. (defun org-latex-link (link desc info)
  2252. "Transcode a LINK object from Org to LaTeX.
  2253. DESC is the description part of the link, or the empty string.
  2254. INFO is a plist holding contextual information. See
  2255. `org-export-data'."
  2256. (let* ((type (org-element-property :type link))
  2257. (raw-path (org-element-property :path link))
  2258. ;; Ensure DESC really exists, or set it to nil.
  2259. (desc (and (not (string= desc "")) desc))
  2260. (imagep (org-export-inline-image-p
  2261. link (plist-get info :latex-inline-image-rules)))
  2262. (path (org-latex--protect-text
  2263. (pcase type
  2264. ((or "http" "https" "ftp" "mailto" "doi")
  2265. (concat type ":" raw-path))
  2266. ("file"
  2267. (org-export-file-uri raw-path))
  2268. (_
  2269. raw-path)))))
  2270. (cond
  2271. ;; Link type is handled by a special function.
  2272. ((org-export-custom-protocol-maybe link desc 'latex info))
  2273. ;; Image file.
  2274. (imagep (org-latex--inline-image link info))
  2275. ;; Radio link: Transcode target's contents and use them as link's
  2276. ;; description.
  2277. ((string= type "radio")
  2278. (let ((destination (org-export-resolve-radio-link link info)))
  2279. (if (not destination) desc
  2280. (format "\\hyperref[%s]{%s}"
  2281. (org-export-get-reference destination info)
  2282. desc))))
  2283. ;; Links pointing to a headline: Find destination and build
  2284. ;; appropriate referencing command.
  2285. ((member type '("custom-id" "fuzzy" "id"))
  2286. (let ((destination
  2287. (if (string= type "fuzzy")
  2288. (org-export-resolve-fuzzy-link link info 'latex-matrices)
  2289. (org-export-resolve-id-link link info))))
  2290. (cl-case (org-element-type destination)
  2291. ;; Id link points to an external file.
  2292. (plain-text
  2293. (if desc (format "\\href{%s}{%s}" destination desc)
  2294. (format "\\url{%s}" destination)))
  2295. ;; Fuzzy link points nowhere.
  2296. ((nil)
  2297. (format (plist-get info :latex-link-with-unknown-path-format)
  2298. (or desc
  2299. (org-export-data
  2300. (org-element-property :raw-link link) info))))
  2301. ;; LINK points to a headline. If headlines are numbered
  2302. ;; and the link has no description, display headline's
  2303. ;; number. Otherwise, display description or headline's
  2304. ;; title.
  2305. (headline
  2306. (let ((label (org-latex--label destination info t)))
  2307. (if (and (not desc)
  2308. (org-export-numbered-headline-p destination info))
  2309. (format org-latex-reference-command label)
  2310. (format "\\hyperref[%s]{%s}" label
  2311. (or desc
  2312. (org-export-data
  2313. (org-element-property :title destination) info))))))
  2314. ;; Fuzzy link points to a target. Do as above.
  2315. (otherwise
  2316. (let ((ref (org-latex--label destination info t)))
  2317. (if (not desc) (format org-latex-reference-command ref)
  2318. (format "\\hyperref[%s]{%s}" ref desc)))))))
  2319. ;; Coderef: replace link with the reference name or the
  2320. ;; equivalent line number.
  2321. ((string= type "coderef")
  2322. (format (org-export-get-coderef-format path desc)
  2323. ;; Resolve with RAW-PATH since PATH could be tainted
  2324. ;; with `org-latex--protect-text' call above.
  2325. (org-export-resolve-coderef raw-path info)))
  2326. ;; External link with a description part.
  2327. ((and path desc) (format "\\href{%s}{%s}" path desc))
  2328. ;; External link without a description part.
  2329. (path (format "\\url{%s}" path))
  2330. ;; No path, only description. Try to do something useful.
  2331. (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
  2332. ;;;; Node Property
  2333. (defun org-latex-node-property (node-property _contents _info)
  2334. "Transcode a NODE-PROPERTY element from Org to LaTeX.
  2335. CONTENTS is nil. INFO is a plist holding contextual
  2336. information."
  2337. (format "%s:%s"
  2338. (org-element-property :key node-property)
  2339. (let ((value (org-element-property :value node-property)))
  2340. (if value (concat " " value) ""))))
  2341. ;;;; Paragraph
  2342. (defun org-latex-paragraph (_paragraph contents _info)
  2343. "Transcode a PARAGRAPH element from Org to LaTeX.
  2344. CONTENTS is the contents of the paragraph, as a string. INFO is
  2345. the plist used as a communication channel."
  2346. contents)
  2347. ;;;; Plain List
  2348. (defun org-latex-plain-list (plain-list contents info)
  2349. "Transcode a PLAIN-LIST element from Org to LaTeX.
  2350. CONTENTS is the contents of the list. INFO is a plist holding
  2351. contextual information."
  2352. (let* ((type (org-element-property :type plain-list))
  2353. (attr (org-export-read-attribute :attr_latex plain-list))
  2354. (latex-type (let ((env (plist-get attr :environment)))
  2355. (cond (env (format "%s" env))
  2356. ((eq type 'ordered) "enumerate")
  2357. ((eq type 'descriptive) "description")
  2358. (t "itemize")))))
  2359. (org-latex--wrap-label
  2360. plain-list
  2361. (format "\\begin{%s}%s\n%s\\end{%s}"
  2362. latex-type
  2363. (or (plist-get attr :options) "")
  2364. contents
  2365. latex-type)
  2366. info)))
  2367. ;;;; Plain Text
  2368. (defun org-latex-plain-text (text info)
  2369. "Transcode a TEXT string from Org to LaTeX.
  2370. TEXT is the string to transcode. INFO is a plist holding
  2371. contextual information."
  2372. (let* ((specialp (plist-get info :with-special-strings))
  2373. (output
  2374. ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
  2375. (let ((case-fold-search nil))
  2376. (replace-regexp-in-string
  2377. "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
  2378. ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
  2379. ;; However, if special strings are used, be careful not
  2380. ;; to protect "\" in "\-" constructs.
  2381. (replace-regexp-in-string
  2382. (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
  2383. (lambda (m)
  2384. (cl-case (string-to-char m)
  2385. (?\\ "$\\\\backslash$\\1")
  2386. (?~ "\\\\textasciitilde{}")
  2387. (?^ "\\\\^{}")
  2388. (t "\\\\\\&")))
  2389. text)))))
  2390. ;; Activate smart quotes. Be sure to provide original TEXT string
  2391. ;; since OUTPUT may have been modified.
  2392. (when (plist-get info :with-smart-quotes)
  2393. (setq output (org-export-activate-smart-quotes output :latex info text)))
  2394. ;; Convert special strings.
  2395. (when specialp
  2396. (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
  2397. ;; Handle break preservation if required.
  2398. (when (plist-get info :preserve-breaks)
  2399. (setq output (replace-regexp-in-string
  2400. "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
  2401. ;; Return value.
  2402. output))
  2403. ;;;; Planning
  2404. (defun org-latex-planning (planning _contents info)
  2405. "Transcode a PLANNING element from Org to LaTeX.
  2406. CONTENTS is nil. INFO is a plist holding contextual
  2407. information."
  2408. (concat
  2409. "\\noindent"
  2410. (mapconcat
  2411. 'identity
  2412. (delq nil
  2413. (list
  2414. (let ((closed (org-element-property :closed planning)))
  2415. (when closed
  2416. (concat
  2417. (format "\\textbf{%s} " org-closed-string)
  2418. (format (plist-get info :latex-inactive-timestamp-format)
  2419. (org-timestamp-translate closed)))))
  2420. (let ((deadline (org-element-property :deadline planning)))
  2421. (when deadline
  2422. (concat
  2423. (format "\\textbf{%s} " org-deadline-string)
  2424. (format (plist-get info :latex-active-timestamp-format)
  2425. (org-timestamp-translate deadline)))))
  2426. (let ((scheduled (org-element-property :scheduled planning)))
  2427. (when scheduled
  2428. (concat
  2429. (format "\\textbf{%s} " org-scheduled-string)
  2430. (format (plist-get info :latex-active-timestamp-format)
  2431. (org-timestamp-translate scheduled)))))))
  2432. " ")
  2433. "\\\\"))
  2434. ;;;; Property Drawer
  2435. (defun org-latex-property-drawer (_property-drawer contents _info)
  2436. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  2437. CONTENTS holds the contents of the drawer. INFO is a plist
  2438. holding contextual information."
  2439. (and (org-string-nw-p contents)
  2440. (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
  2441. ;;;; Pseudo Element: LaTeX Matrices
  2442. ;; `latex-matrices' elements have the following properties:
  2443. ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
  2444. ;; `math').
  2445. (defun org-latex--wrap-latex-matrices (data info)
  2446. "Merge contiguous tables with the same mode within a pseudo-element.
  2447. DATA is a parse tree or a secondary string. INFO is a plist
  2448. containing export options. Modify DATA by side-effect and return
  2449. it."
  2450. (org-element-map data 'table
  2451. (lambda (table)
  2452. (when (eq (org-element-property :type table) 'org)
  2453. (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
  2454. (plist-get info :latex-default-table-mode))))
  2455. (when (and (member mode '("inline-math" "math"))
  2456. ;; Do not wrap twice the same table.
  2457. (not (eq (org-element-type
  2458. (org-element-property :parent table))
  2459. 'latex-matrices)))
  2460. (let* ((caption (and (not (string= mode "inline-math"))
  2461. (org-element-property :caption table)))
  2462. (name (and (not (string= mode "inline-math"))
  2463. (org-element-property :name table)))
  2464. (matrices
  2465. (list 'latex-matrices
  2466. ;; Inherit name from the first table.
  2467. (list :name name
  2468. ;; FIXME: what syntax for captions?
  2469. ;;
  2470. ;; :caption caption
  2471. :markup
  2472. (cond ((string= mode "inline-math") 'inline)
  2473. ((or caption name) 'equation)
  2474. (t 'math)))))
  2475. (previous table)
  2476. (next (org-export-get-next-element table info)))
  2477. (org-element-insert-before matrices table)
  2478. ;; Swallow all contiguous tables sharing the same mode.
  2479. (while (and
  2480. (zerop (or (org-element-property :post-blank previous) 0))
  2481. (setq next (org-export-get-next-element previous info))
  2482. (eq (org-element-type next) 'table)
  2483. (eq (org-element-property :type next) 'org)
  2484. (string= (or (org-export-read-attribute
  2485. :attr_latex next :mode)
  2486. (plist-get info :latex-default-table-mode))
  2487. mode))
  2488. (org-element-put-property table :name nil)
  2489. (org-element-put-property table :caption nil)
  2490. (org-element-extract-element previous)
  2491. (org-element-adopt-elements matrices previous)
  2492. (setq previous next))
  2493. ;; Inherit `:post-blank' from the value of the last
  2494. ;; swallowed table. Set the latter's `:post-blank'
  2495. ;; value to 0 so as to not duplicate empty lines.
  2496. (org-element-put-property
  2497. matrices :post-blank (org-element-property :post-blank previous))
  2498. (org-element-put-property previous :post-blank 0)
  2499. (org-element-put-property table :name nil)
  2500. (org-element-put-property table :caption nil)
  2501. (org-element-extract-element previous)
  2502. (org-element-adopt-elements matrices previous))))))
  2503. info)
  2504. data)
  2505. (defun org-latex-matrices (matrices contents info)
  2506. "Transcode a MATRICES element from Org to LaTeX.
  2507. CONTENTS is a string. INFO is a plist used as a communication
  2508. channel."
  2509. (pcase (org-element-property :markup matrices)
  2510. (`inline (format "\\(%s\\)" contents))
  2511. (`equation
  2512. (let ((caption (org-latex--caption/label-string matrices info))
  2513. (caption-above? (org-latex--caption-above-p matrices info)))
  2514. (concat "\\begin{equation}\n"
  2515. (and caption-above? caption)
  2516. contents
  2517. (and (not caption-above?) caption)
  2518. "\\end{equation}")))
  2519. (_
  2520. (format "\\[\n%s\\]" contents))))
  2521. ;;;; Pseudo Object: LaTeX Math Block
  2522. ;; `latex-math-block' objects have the following property:
  2523. ;; `:post-blank'.
  2524. (defun org-latex--wrap-latex-math-block (data info)
  2525. "Merge contiguous math objects in a pseudo-object container.
  2526. DATA is a parse tree or a secondary string. INFO is a plist
  2527. containing export options. Modify DATA by side-effect and return it."
  2528. (let ((valid-object-p
  2529. ;; Non-nil when OBJECT can be added to a latex math block.
  2530. (lambda (object)
  2531. (pcase (org-element-type object)
  2532. (`entity (org-element-property :latex-math-p object))
  2533. (`latex-fragment
  2534. (let ((value (org-element-property :value object)))
  2535. (or (string-prefix-p "\\(" value)
  2536. (string-match-p "\\`\\$[^$]" value))))))))
  2537. (org-element-map data '(entity latex-fragment)
  2538. (lambda (object)
  2539. ;; Skip objects already wrapped.
  2540. (when (and (not (eq (org-element-type
  2541. (org-element-property :parent object))
  2542. 'latex-math-block))
  2543. (funcall valid-object-p object))
  2544. (let ((math-block (list 'latex-math-block nil))
  2545. (next-elements (org-export-get-next-element object info t))
  2546. (last object))
  2547. ;; Wrap MATH-BLOCK around OBJECT in DATA.
  2548. (org-element-insert-before math-block object)
  2549. (org-element-extract-element object)
  2550. (org-element-adopt-elements math-block object)
  2551. (when (zerop (or (org-element-property :post-blank object) 0))
  2552. ;; MATH-BLOCK swallows consecutive math objects.
  2553. (catch 'exit
  2554. (dolist (next next-elements)
  2555. (unless (funcall valid-object-p next) (throw 'exit nil))
  2556. (org-element-extract-element next)
  2557. (org-element-adopt-elements math-block next)
  2558. ;; Eschew the case: \beta$x$ -> \(\betax\).
  2559. (org-element-put-property last :post-blank 1)
  2560. (setq last next)
  2561. (when (> (or (org-element-property :post-blank next) 0) 0)
  2562. (throw 'exit nil)))))
  2563. (org-element-put-property
  2564. math-block :post-blank (org-element-property :post-blank last)))))
  2565. info nil '(latex-math-block) t)
  2566. ;; Return updated DATA.
  2567. data))
  2568. (defun org-latex-math-block (_math-block contents _info)
  2569. "Transcode a MATH-BLOCK object from Org to LaTeX.
  2570. CONTENTS is a string. INFO is a plist used as a communication
  2571. channel."
  2572. (when (org-string-nw-p contents)
  2573. (format "\\(%s\\)" (org-trim contents))))
  2574. ;;;; Quote Block
  2575. (defun org-latex-quote-block (quote-block contents info)
  2576. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  2577. CONTENTS holds the contents of the block. INFO is a plist
  2578. holding contextual information."
  2579. (let ((environment
  2580. (or (org-export-read-attribute :attr_latex quote-block :environment)
  2581. (plist-get info :latex-default-quote-environment)))
  2582. (options
  2583. (or (org-export-read-attribute :attr_latex quote-block :options)
  2584. "")))
  2585. (org-latex--wrap-label
  2586. quote-block (format "\\begin{%s}%s\n%s\\end{%s}"
  2587. environment
  2588. options
  2589. contents
  2590. environment)
  2591. info)))
  2592. ;;;; Radio Target
  2593. (defun org-latex-radio-target (radio-target text info)
  2594. "Transcode a RADIO-TARGET object from Org to LaTeX.
  2595. TEXT is the text of the target. INFO is a plist holding
  2596. contextual information."
  2597. (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
  2598. ;;;; Section
  2599. (defun org-latex-section (_section contents _info)
  2600. "Transcode a SECTION element from Org to LaTeX.
  2601. CONTENTS holds the contents of the section. INFO is a plist
  2602. holding contextual information."
  2603. contents)
  2604. ;;;; Special Block
  2605. (defun org-latex-special-block (special-block contents info)
  2606. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  2607. CONTENTS holds the contents of the block. INFO is a plist
  2608. holding contextual information."
  2609. (let ((type (org-element-property :type special-block))
  2610. (opt (org-export-read-attribute :attr_latex special-block :options))
  2611. (caption (org-latex--caption/label-string special-block info))
  2612. (caption-above-p (org-latex--caption-above-p special-block info)))
  2613. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  2614. (and caption-above-p caption)
  2615. contents
  2616. (and (not caption-above-p) caption)
  2617. (format "\\end{%s}" type))))
  2618. ;;;; Src Block
  2619. (defun org-latex-src-block (src-block _contents info)
  2620. "Transcode a SRC-BLOCK element from Org to LaTeX.
  2621. CONTENTS holds the contents of the item. INFO is a plist holding
  2622. contextual information."
  2623. (when (org-string-nw-p (org-element-property :value src-block))
  2624. (let* ((lang (org-element-property :language src-block))
  2625. (caption (org-element-property :caption src-block))
  2626. (caption-above-p (org-latex--caption-above-p src-block info))
  2627. (label (org-element-property :name src-block))
  2628. (custom-env (and lang
  2629. (cadr (assq (intern lang)
  2630. org-latex-custom-lang-environments))))
  2631. (num-start (org-export-get-loc src-block info))
  2632. (retain-labels (org-element-property :retain-labels src-block))
  2633. (attributes (org-export-read-attribute :attr_latex src-block))
  2634. (float (plist-get attributes :float))
  2635. (listings (plist-get info :latex-listings)))
  2636. (cond
  2637. ;; Case 1. No source fontification.
  2638. ((or (not lang) (not listings))
  2639. (let ((caption-str (org-latex--caption/label-string src-block info))
  2640. (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
  2641. (org-export-format-code-default src-block info))))
  2642. (cond ((string= "multicolumn" float)
  2643. (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
  2644. (plist-get info :latex-default-figure-position)
  2645. (if caption-above-p caption-str "")
  2646. verbatim
  2647. (if caption-above-p "" caption-str)))
  2648. (caption (concat
  2649. (if caption-above-p caption-str "")
  2650. verbatim
  2651. (if caption-above-p "" (concat "\n" caption-str))))
  2652. (t verbatim))))
  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. #'org-latex-compile)))
  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