ox-latex.el 128 KB

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