ox-latex.el 131 KB

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