ox-latex.el 131 KB

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