ox-latex.el 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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 "26.1"
  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. (null (plist-get info :latex-listings)))))
  1105. (short (org-export-get-caption element t))
  1106. (caption-from-attr-latex (plist-get attr :caption)))
  1107. (cond
  1108. ((org-string-nw-p caption-from-attr-latex)
  1109. (concat caption-from-attr-latex "\n"))
  1110. ((and (not main) (equal label "")) "")
  1111. ((not main) label)
  1112. ;; Option caption format with short name.
  1113. (t
  1114. (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
  1115. "\\caption%s%s{%s%s}\n")
  1116. (if nonfloat
  1117. (cl-case type
  1118. (paragraph "figure")
  1119. (src-block (if (plist-get info :latex-listings)
  1120. "listing"
  1121. "figure"))
  1122. (t (symbol-name type)))
  1123. "")
  1124. (if short (format "[%s]" (org-export-data short info)) "")
  1125. label
  1126. (org-export-data main info))))))
  1127. (defun org-latex-guess-inputenc (header)
  1128. "Set the coding system in inputenc to what the buffer is.
  1129. HEADER is the LaTeX header string. This function only applies
  1130. when specified inputenc option is \"AUTO\".
  1131. Return the new header, as a string."
  1132. (let* ((cs (or (ignore-errors
  1133. (latexenc-coding-system-to-inputenc
  1134. (or org-export-coding-system buffer-file-coding-system)))
  1135. "utf8")))
  1136. (if (not cs) header
  1137. ;; First translate if that is requested.
  1138. (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
  1139. ;; Then find the \usepackage statement and replace the option.
  1140. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  1141. cs header t nil 1))))
  1142. (defun org-latex-guess-babel-language (header info)
  1143. "Set Babel's language according to LANGUAGE keyword.
  1144. HEADER is the LaTeX header string. INFO is the plist used as
  1145. a communication channel.
  1146. Insertion of guessed language only happens when Babel package has
  1147. explicitly been loaded. Then it is added to the rest of
  1148. package's options.
  1149. The argument to Babel may be \"AUTO\" which is then replaced with
  1150. the language of the document or `org-export-default-language'
  1151. unless language in question is already loaded.
  1152. Return the new header."
  1153. (let ((language-code (plist-get info :language)))
  1154. ;; If no language is set or Babel package is not loaded, return
  1155. ;; HEADER as-is.
  1156. (if (or (not (stringp language-code))
  1157. (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
  1158. header
  1159. (let ((options (save-match-data
  1160. (org-split-string (match-string 1 header) ",[ \t]*")))
  1161. (language (cdr (assoc-string language-code
  1162. org-latex-babel-language-alist t))))
  1163. ;; If LANGUAGE is already loaded, return header without AUTO.
  1164. ;; Otherwise, replace AUTO with language or append language if
  1165. ;; AUTO is not present.
  1166. (replace-match
  1167. (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
  1168. (cond ((member language options) (delete "AUTO" options))
  1169. ((member "AUTO" options) options)
  1170. (t (append options (list language))))
  1171. ", ")
  1172. t nil header 1)))))
  1173. (defun org-latex-guess-polyglossia-language (header info)
  1174. "Set the Polyglossia language according to the LANGUAGE keyword.
  1175. HEADER is the LaTeX header string. INFO is the plist used as
  1176. a communication channel.
  1177. Insertion of guessed language only happens when the Polyglossia
  1178. package has been explicitly loaded.
  1179. The argument to Polyglossia may be \"AUTO\" which is then
  1180. replaced with the language of the document or
  1181. `org-export-default-language'. Note, the language is really set
  1182. using \setdefaultlanguage and not as an option to the package.
  1183. Return the new header."
  1184. (let ((language (plist-get info :language)))
  1185. ;; If no language is set or Polyglossia is not loaded, return
  1186. ;; HEADER as-is.
  1187. (if (or (not (stringp language))
  1188. (not (string-match
  1189. "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
  1190. header)))
  1191. header
  1192. (let* ((options (org-string-nw-p (match-string 1 header)))
  1193. (languages (and options
  1194. ;; Reverse as the last loaded language is
  1195. ;; the main language.
  1196. (nreverse
  1197. (delete-dups
  1198. (save-match-data
  1199. (org-split-string
  1200. (replace-regexp-in-string
  1201. "AUTO" language options t)
  1202. ",[ \t]*"))))))
  1203. (main-language-set
  1204. (string-match-p "\\\\setmainlanguage{.*?}" header)))
  1205. (replace-match
  1206. (concat "\\usepackage{polyglossia}\n"
  1207. (mapconcat
  1208. (lambda (l)
  1209. (let ((l (or (assoc l org-latex-polyglossia-language-alist)
  1210. l)))
  1211. (format (if main-language-set "\\setotherlanguage%s{%s}\n"
  1212. (setq main-language-set t)
  1213. "\\setmainlanguage%s{%s}\n")
  1214. (if (and (consp l) (= (length l) 3))
  1215. (format "[variant=%s]" (nth 2 l))
  1216. "")
  1217. (nth 1 l))))
  1218. languages
  1219. ""))
  1220. t t header 0)))))
  1221. (defun org-latex--remove-packages (pkg-alist info)
  1222. "Remove packages based on the current LaTeX compiler.
  1223. If the fourth argument of an element is set in pkg-alist, and it
  1224. is not a member of the LaTeX compiler of the document, the packages
  1225. is removed. See also `org-latex-compiler'.
  1226. Return modified pkg-alist."
  1227. (let ((compiler (or (plist-get info :latex-compiler) "")))
  1228. (if (member-ignore-case compiler org-latex-compilers)
  1229. (delq nil
  1230. (mapcar
  1231. (lambda (pkg)
  1232. (unless (and
  1233. (listp pkg)
  1234. (let ((third (nth 3 pkg)))
  1235. (and third
  1236. (not (member-ignore-case
  1237. compiler
  1238. (if (listp third) third (list third)))))))
  1239. pkg))
  1240. pkg-alist))
  1241. pkg-alist)))
  1242. (defun org-latex--find-verb-separator (s)
  1243. "Return a character not used in string S.
  1244. This is used to choose a separator for constructs like \\verb."
  1245. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1246. (cl-loop for c across ll
  1247. when (not (string-match (regexp-quote (char-to-string c)) s))
  1248. return (char-to-string c))))
  1249. (defun org-latex--make-option-string (options)
  1250. "Return a comma separated string of keywords and values.
  1251. OPTIONS is an alist where the key is the options keyword as
  1252. a string, and the value a list containing the keyword value, or
  1253. nil."
  1254. (mapconcat (lambda (pair)
  1255. (pcase-let ((`(,keyword ,value) pair))
  1256. (concat keyword
  1257. (and (> (length value) 0)
  1258. (concat "=" value)))))
  1259. options
  1260. ","))
  1261. (defun org-latex--wrap-label (element output info)
  1262. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  1263. INFO is the current export state, as a plist. This function
  1264. should not be used for floats. See
  1265. `org-latex--caption/label-string'."
  1266. (if (not (and (org-string-nw-p output) (org-element-property :name element)))
  1267. output
  1268. (concat (format "\\phantomsection\n\\label{%s}\n"
  1269. (org-latex--label element info))
  1270. output)))
  1271. (defun org-latex--protect-text (text)
  1272. "Protect special characters in string TEXT and return it."
  1273. (replace-regexp-in-string "[\\{}$%&_#~^]" "\\\\\\&" text))
  1274. (defun org-latex--text-markup (text markup info)
  1275. "Format TEXT depending on MARKUP text markup.
  1276. INFO is a plist used as a communication channel. See
  1277. `org-latex-text-markup-alist' for details."
  1278. (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
  1279. (cl-case fmt
  1280. ;; No format string: Return raw text.
  1281. ((nil) text)
  1282. ;; Handle the `verb' special case: Find an appropriate separator
  1283. ;; and use "\\verb" command.
  1284. (verb
  1285. (let ((separator (org-latex--find-verb-separator text)))
  1286. (concat "\\verb"
  1287. separator
  1288. (replace-regexp-in-string "\n" " " text)
  1289. separator)))
  1290. ;; Handle the `protectedtexttt' special case: Protect some
  1291. ;; special chars and use "\texttt{%s}" format string.
  1292. (protectedtexttt
  1293. (format "\\texttt{%s}"
  1294. (replace-regexp-in-string
  1295. "--\\|[\\{}$%&_#~^]"
  1296. (lambda (m)
  1297. (cond ((equal m "--") "-{}-")
  1298. ((equal m "\\") "\\textbackslash{}")
  1299. ((equal m "~") "\\textasciitilde{}")
  1300. ((equal m "^") "\\textasciicircum{}")
  1301. (t (org-latex--protect-text m))))
  1302. text nil t)))
  1303. ;; Else use format string.
  1304. (t (format fmt text)))))
  1305. (defun org-latex--delayed-footnotes-definitions (element info)
  1306. "Return footnotes definitions in ELEMENT as a string.
  1307. INFO is a plist used as a communication channel.
  1308. Footnotes definitions are returned within \"\\footnotetext{}\"
  1309. commands.
  1310. This function is used within constructs that don't support
  1311. \"\\footnote{}\" command (e.g., an item tag). In that case,
  1312. \"\\footnotemark\" is used within the construct and the function
  1313. just outside of it."
  1314. (mapconcat
  1315. (lambda (ref)
  1316. (let ((def (org-export-get-footnote-definition ref info)))
  1317. (format "\\footnotetext[%d]{%s%s}"
  1318. (org-export-get-footnote-number ref info)
  1319. (org-trim (org-latex--label def info t t))
  1320. (org-trim (org-export-data def info)))))
  1321. ;; Find every footnote reference in ELEMENT.
  1322. (letrec ((all-refs nil)
  1323. (search-refs
  1324. (lambda (data)
  1325. ;; Return a list of all footnote references never seen
  1326. ;; before in DATA.
  1327. (org-element-map data 'footnote-reference
  1328. (lambda (ref)
  1329. (when (org-export-footnote-first-reference-p ref info)
  1330. (push ref all-refs)
  1331. (when (eq (org-element-property :type ref) 'standard)
  1332. (funcall search-refs
  1333. (org-export-get-footnote-definition ref info)))))
  1334. info)
  1335. (reverse all-refs))))
  1336. (funcall search-refs element))
  1337. ""))
  1338. (defun org-latex--translate (s info)
  1339. "Translate string S according to specified language.
  1340. INFO is a plist used as a communication channel."
  1341. (org-export-translate s :latex info))
  1342. (defun org-latex--format-spec (info)
  1343. "Create a format-spec for document meta-data.
  1344. INFO is a plist used as a communication channel."
  1345. (let ((language (let ((lang (plist-get info :language)))
  1346. (or (cdr (assoc-string lang org-latex-babel-language-alist t))
  1347. (nth 1 (assoc-string lang org-latex-polyglossia-language-alist t))
  1348. lang))))
  1349. `((?a . ,(org-export-data (plist-get info :author) info))
  1350. (?t . ,(org-export-data (plist-get info :title) info))
  1351. (?k . ,(org-export-data (org-latex--wrap-latex-math-block
  1352. (plist-get info :keywords) info)
  1353. info))
  1354. (?d . ,(org-export-data (org-latex--wrap-latex-math-block
  1355. (plist-get info :description) info)
  1356. info))
  1357. (?c . ,(plist-get info :creator))
  1358. (?l . ,language)
  1359. (?L . ,(capitalize language))
  1360. (?D . ,(org-export-get-date info)))))
  1361. (defun org-latex--insert-compiler (info)
  1362. "Insert LaTeX_compiler info into the document.
  1363. INFO is a plist used as a communication channel."
  1364. (let ((compiler (plist-get info :latex-compiler)))
  1365. (and (org-string-nw-p org-latex-compiler-file-string)
  1366. (member (or compiler "") org-latex-compilers)
  1367. (format org-latex-compiler-file-string compiler))))
  1368. ;;; Filters
  1369. (defun org-latex-matrices-tree-filter (tree _backend info)
  1370. (org-latex--wrap-latex-matrices tree info))
  1371. (defun org-latex-math-block-tree-filter (tree _backend info)
  1372. (org-latex--wrap-latex-math-block tree info))
  1373. (defun org-latex-math-block-options-filter (info _backend)
  1374. (dolist (prop '(:author :date :title) info)
  1375. (plist-put info prop
  1376. (org-latex--wrap-latex-math-block (plist-get info prop) info))))
  1377. (defun org-latex-clean-invalid-line-breaks (data _backend _info)
  1378. (replace-regexp-in-string
  1379. "\\(\\end{[A-Za-z0-9*]+}\\|^\\)[ \t]*\\\\\\\\[ \t]*$" "\\1"
  1380. data))
  1381. ;;; Template
  1382. ;;;###autoload
  1383. (defun org-latex-make-preamble (info &optional template snippet?)
  1384. "Return a formatted LaTeX preamble.
  1385. INFO is a plist used as a communication channel. Optional
  1386. argument TEMPLATE, when non-nil, is the header template string,
  1387. as expected by `org-splice-latex-header'. When SNIPPET? is
  1388. non-nil, only includes packages relevant to image generation, as
  1389. specified in `org-latex-default-packages-alist' or
  1390. `org-latex-packages-alist'."
  1391. (let* ((class (plist-get info :latex-class))
  1392. (class-options (plist-get info :latex-class-options))
  1393. (header (nth 1 (assoc class (plist-get info :latex-classes))))
  1394. (class-template
  1395. (or template
  1396. (and (stringp header)
  1397. (if (not class-options) header
  1398. (replace-regexp-in-string
  1399. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  1400. class-options header t nil 1)))
  1401. (user-error "Unknown LaTeX class `%s'" class))))
  1402. (org-latex-guess-polyglossia-language
  1403. (org-latex-guess-babel-language
  1404. (org-latex-guess-inputenc
  1405. (org-element-normalize-string
  1406. (org-splice-latex-header
  1407. class-template
  1408. (org-latex--remove-packages org-latex-default-packages-alist info)
  1409. (org-latex--remove-packages org-latex-packages-alist info)
  1410. snippet?
  1411. (mapconcat #'org-element-normalize-string
  1412. (list (plist-get info :latex-header)
  1413. (plist-get info :latex-header-extra)) ""))))
  1414. info)
  1415. info)))
  1416. (defun org-latex-template (contents info)
  1417. "Return complete document string after LaTeX conversion.
  1418. CONTENTS is the transcoded contents string. INFO is a plist
  1419. holding export options."
  1420. (let ((title (org-export-data (plist-get info :title) info))
  1421. (spec (org-latex--format-spec info)))
  1422. (concat
  1423. ;; Time-stamp.
  1424. (and (plist-get info :time-stamp-file)
  1425. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  1426. ;; LaTeX compiler.
  1427. (org-latex--insert-compiler info)
  1428. ;; Document class and packages.
  1429. (org-latex-make-preamble info)
  1430. ;; Possibly limit depth for headline numbering.
  1431. (let ((sec-num (plist-get info :section-numbers)))
  1432. (when (integerp sec-num)
  1433. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  1434. ;; Author.
  1435. (let ((author (and (plist-get info :with-author)
  1436. (let ((auth (plist-get info :author)))
  1437. (and auth (org-export-data auth info)))))
  1438. (email (and (plist-get info :with-email)
  1439. (org-export-data (plist-get info :email) info))))
  1440. (cond ((and author email (not (string= "" email)))
  1441. (format "\\author{%s\\thanks{%s}}\n" author email))
  1442. ((or author email) (format "\\author{%s}\n" (or author email)))))
  1443. ;; Date.
  1444. (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
  1445. (format "\\date{%s}\n" (org-export-data date info)))
  1446. ;; Title and subtitle.
  1447. (let* ((subtitle (plist-get info :subtitle))
  1448. (formatted-subtitle
  1449. (when subtitle
  1450. (format (plist-get info :latex-subtitle-format)
  1451. (org-export-data subtitle info))))
  1452. (separate (plist-get info :latex-subtitle-separate)))
  1453. (concat
  1454. (format "\\title{%s%s}\n" title
  1455. (if separate "" (or formatted-subtitle "")))
  1456. (when (and separate subtitle)
  1457. (concat formatted-subtitle "\n"))))
  1458. ;; Hyperref options.
  1459. (let ((template (plist-get info :latex-hyperref-template)))
  1460. (and (stringp template)
  1461. (format-spec template spec)))
  1462. ;; Document start.
  1463. "\\begin{document}\n\n"
  1464. ;; Title command.
  1465. (let* ((title-command (plist-get info :latex-title-command))
  1466. (command (and (stringp title-command)
  1467. (format-spec title-command spec))))
  1468. (org-element-normalize-string
  1469. (cond ((not (plist-get info :with-title)) nil)
  1470. ((string= "" title) nil)
  1471. ((not (stringp command)) nil)
  1472. ((string-match "\\(?:[^%]\\|^\\)%s" command)
  1473. (format command title))
  1474. (t command))))
  1475. ;; Table of contents.
  1476. (let ((depth (plist-get info :with-toc)))
  1477. (when depth
  1478. (concat (when (wholenump depth)
  1479. (format "\\setcounter{tocdepth}{%d}\n" depth))
  1480. (plist-get info :latex-toc-command))))
  1481. ;; Document's body.
  1482. contents
  1483. ;; Creator.
  1484. (and (plist-get info :with-creator)
  1485. (concat (plist-get info :creator) "\n"))
  1486. ;; Document end.
  1487. "\\end{document}")))
  1488. ;;; Transcode Functions
  1489. ;;;; Bold
  1490. (defun org-latex-bold (_bold contents info)
  1491. "Transcode BOLD from Org to LaTeX.
  1492. CONTENTS is the text with bold markup. INFO is a plist holding
  1493. contextual information."
  1494. (org-latex--text-markup contents 'bold info))
  1495. ;;;; Center Block
  1496. (defun org-latex-center-block (center-block contents info)
  1497. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  1498. CONTENTS holds the contents of the center block. INFO is a plist
  1499. holding contextual information."
  1500. (org-latex--wrap-label
  1501. center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
  1502. ;;;; Clock
  1503. (defun org-latex-clock (clock _contents info)
  1504. "Transcode a CLOCK element from Org to LaTeX.
  1505. CONTENTS is nil. INFO is a plist holding contextual
  1506. information."
  1507. (concat
  1508. "\\noindent"
  1509. (format "\\textbf{%s} " org-clock-string)
  1510. (format (plist-get info :latex-inactive-timestamp-format)
  1511. (concat (org-timestamp-translate (org-element-property :value clock))
  1512. (let ((time (org-element-property :duration clock)))
  1513. (and time (format " (%s)" time)))))
  1514. "\\\\"))
  1515. ;;;; Code
  1516. (defun org-latex-code (code _contents info)
  1517. "Transcode a CODE object from Org to LaTeX.
  1518. CONTENTS is nil. INFO is a plist used as a communication
  1519. channel."
  1520. (org-latex--text-markup (org-element-property :value code) 'code info))
  1521. ;;;; Drawer
  1522. (defun org-latex-drawer (drawer contents info)
  1523. "Transcode a DRAWER element from Org to LaTeX.
  1524. CONTENTS holds the contents of the block. INFO is a plist
  1525. holding contextual information."
  1526. (let* ((name (org-element-property :drawer-name drawer))
  1527. (output (funcall (plist-get info :latex-format-drawer-function)
  1528. name contents)))
  1529. (org-latex--wrap-label drawer output info)))
  1530. ;;;; Dynamic Block
  1531. (defun org-latex-dynamic-block (dynamic-block contents info)
  1532. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1533. CONTENTS holds the contents of the block. INFO is a plist
  1534. holding contextual information. See `org-export-data'."
  1535. (org-latex--wrap-label dynamic-block contents info))
  1536. ;;;; Entity
  1537. (defun org-latex-entity (entity _contents _info)
  1538. "Transcode an ENTITY object from Org to LaTeX.
  1539. CONTENTS are the definition itself. INFO is a plist holding
  1540. contextual information."
  1541. (org-element-property :latex entity))
  1542. ;;;; Example Block
  1543. (defun org-latex-example-block (example-block _contents info)
  1544. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1545. CONTENTS is nil. INFO is a plist holding contextual
  1546. information."
  1547. (when (org-string-nw-p (org-element-property :value example-block))
  1548. (let ((environment (or (org-export-read-attribute
  1549. :attr_latex example-block :environment)
  1550. "verbatim")))
  1551. (org-latex--wrap-label
  1552. example-block
  1553. (format "\\begin{%s}\n%s\\end{%s}"
  1554. environment
  1555. (org-export-format-code-default example-block info)
  1556. environment)
  1557. info))))
  1558. ;;;; Export Block
  1559. (defun org-latex-export-block (export-block _contents _info)
  1560. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1561. CONTENTS is nil. INFO is a plist holding contextual information."
  1562. (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
  1563. (org-remove-indentation (org-element-property :value export-block))))
  1564. ;;;; Export Snippet
  1565. (defun org-latex-export-snippet (export-snippet _contents _info)
  1566. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1567. CONTENTS is nil. INFO is a plist holding contextual information."
  1568. (when (eq (org-export-snippet-backend export-snippet) 'latex)
  1569. (org-element-property :value export-snippet)))
  1570. ;;;; Fixed Width
  1571. (defun org-latex-fixed-width (fixed-width _contents info)
  1572. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1573. CONTENTS is nil. INFO is a plist holding contextual information."
  1574. (org-latex--wrap-label
  1575. fixed-width
  1576. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1577. (org-remove-indentation
  1578. (org-element-property :value fixed-width)))
  1579. info))
  1580. ;;;; Footnote Reference
  1581. (defun org-latex-footnote-reference (footnote-reference _contents info)
  1582. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1583. CONTENTS is nil. INFO is a plist holding contextual information."
  1584. (let ((label (org-element-property :label footnote-reference)))
  1585. (concat
  1586. ;; Insert separator between two footnotes in a row.
  1587. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1588. (when (eq (org-element-type prev) 'footnote-reference)
  1589. (plist-get info :latex-footnote-separator)))
  1590. (cond
  1591. ;; Use `:latex-footnote-defined-format' if the footnote has
  1592. ;; already been defined.
  1593. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1594. (format (plist-get info :latex-footnote-defined-format)
  1595. (org-latex--label
  1596. (org-export-get-footnote-definition footnote-reference info)
  1597. info t)))
  1598. ;; Use \footnotemark if reference is within another footnote
  1599. ;; reference, footnote definition or table cell.
  1600. ((org-element-lineage footnote-reference
  1601. '(footnote-reference footnote-definition table-cell))
  1602. "\\footnotemark")
  1603. ;; Otherwise, define it with \footnote command.
  1604. (t
  1605. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1606. (concat
  1607. (format "\\footnote{%s%s}" (org-trim (org-export-data def info))
  1608. ;; Only insert a \label if there exist another
  1609. ;; reference to def.
  1610. (cond ((not label) "")
  1611. ((org-element-map (plist-get info :parse-tree) 'footnote-reference
  1612. (lambda (f)
  1613. (and (not (eq f footnote-reference))
  1614. (equal (org-element-property :label f) label)
  1615. (org-trim (org-latex--label def info t t))))
  1616. info t))
  1617. (t "")))
  1618. ;; Retrieve all footnote references within the footnote and
  1619. ;; add their definition after it, since LaTeX doesn't support
  1620. ;; them inside.
  1621. (org-latex--delayed-footnotes-definitions def info))))))))
  1622. ;;;; Headline
  1623. (defun org-latex-headline (headline contents info)
  1624. "Transcode a HEADLINE element from Org to LaTeX.
  1625. CONTENTS holds the contents of the headline. INFO is a plist
  1626. holding contextual information."
  1627. (unless (org-element-property :footnote-section-p headline)
  1628. (let* ((class (plist-get info :latex-class))
  1629. (level (org-export-get-relative-level headline info))
  1630. (numberedp (org-export-numbered-headline-p headline info))
  1631. (class-sectioning (assoc class (plist-get info :latex-classes)))
  1632. ;; Section formatting will set two placeholders: one for
  1633. ;; the title and the other for the contents.
  1634. (section-fmt
  1635. (let ((sec (if (functionp (nth 2 class-sectioning))
  1636. (funcall (nth 2 class-sectioning) level numberedp)
  1637. (nth (1+ level) class-sectioning))))
  1638. (cond
  1639. ;; No section available for that LEVEL.
  1640. ((not sec) nil)
  1641. ;; Section format directly returned by a function. Add
  1642. ;; placeholder for contents.
  1643. ((stringp sec) (concat sec "\n%s"))
  1644. ;; (numbered-section . unnumbered-section)
  1645. ((not (consp (cdr sec)))
  1646. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1647. ;; (numbered-open numbered-close)
  1648. ((= (length sec) 2)
  1649. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1650. ;; (num-in num-out no-num-in no-num-out)
  1651. ((= (length sec) 4)
  1652. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1653. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1654. ;; Create a temporary export back-end that hard-codes
  1655. ;; "\underline" within "\section" and alike.
  1656. (section-back-end
  1657. (org-export-create-backend
  1658. :parent 'latex
  1659. :transcoders
  1660. '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
  1661. (text
  1662. (org-export-data-with-backend
  1663. (org-element-property :title headline) section-back-end info))
  1664. (todo
  1665. (and (plist-get info :with-todo-keywords)
  1666. (let ((todo (org-element-property :todo-keyword headline)))
  1667. (and todo (org-export-data todo info)))))
  1668. (todo-type (and todo (org-element-property :todo-type headline)))
  1669. (tags (and (plist-get info :with-tags)
  1670. (org-export-get-tags headline info)))
  1671. (priority (and (plist-get info :with-priority)
  1672. (org-element-property :priority headline)))
  1673. ;; Create the headline text along with a no-tag version.
  1674. ;; The latter is required to remove tags from toc.
  1675. (full-text (funcall (plist-get info :latex-format-headline-function)
  1676. todo todo-type priority text tags info))
  1677. ;; Associate \label to the headline for internal links.
  1678. (headline-label (org-latex--label headline info t t))
  1679. (pre-blanks
  1680. (make-string (org-element-property :pre-blank headline) ?\n)))
  1681. (if (or (not section-fmt) (org-export-low-level-p headline info))
  1682. ;; This is a deep sub-tree: export it as a list item. Also
  1683. ;; export as items headlines for which no section format has
  1684. ;; been found.
  1685. (let ((low-level-body
  1686. (concat
  1687. ;; If headline is the first sibling, start a list.
  1688. (when (org-export-first-sibling-p headline info)
  1689. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1690. ;; Itemize headline
  1691. "\\item"
  1692. (and full-text
  1693. (string-match-p "\\`[ \t]*\\[" full-text)
  1694. "\\relax")
  1695. " " full-text "\n"
  1696. headline-label
  1697. pre-blanks
  1698. contents)))
  1699. ;; If headline is not the last sibling simply return
  1700. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
  1701. ;; any blank line.
  1702. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1703. (replace-regexp-in-string
  1704. "[ \t\n]*\\'"
  1705. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1706. low-level-body)))
  1707. ;; This is a standard headline. Export it as a section. Add
  1708. ;; an alternative heading when possible, and when this is not
  1709. ;; identical to the usual heading.
  1710. (let ((opt-title
  1711. (funcall (plist-get info :latex-format-headline-function)
  1712. todo todo-type priority
  1713. (org-export-data-with-backend
  1714. (org-export-get-alt-title headline info)
  1715. section-back-end info)
  1716. (and (eq (plist-get info :with-tags) t) tags)
  1717. info))
  1718. ;; Maybe end local TOC (see `org-latex-keyword').
  1719. (contents
  1720. (concat
  1721. contents
  1722. (let ((case-fold-search t)
  1723. (section
  1724. (let ((first (car (org-element-contents headline))))
  1725. (and (eq (org-element-type first) 'section) first))))
  1726. (org-element-map section 'keyword
  1727. (lambda (k)
  1728. (and (equal (org-element-property :key k) "TOC")
  1729. (let ((v (org-element-property :value k)))
  1730. (and (string-match-p "\\<headlines\\>" v)
  1731. (string-match-p "\\<local\\>" v)
  1732. (format "\\stopcontents[level-%d]" level)))))
  1733. info t)))))
  1734. (if (and opt-title
  1735. (not (equal opt-title full-text))
  1736. (string-match "\\`\\\\\\(.+?\\){" section-fmt))
  1737. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  1738. ;; Replace square brackets with parenthesis
  1739. ;; since square brackets are not supported in
  1740. ;; optional arguments.
  1741. (replace-regexp-in-string
  1742. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  1743. full-text
  1744. (concat headline-label pre-blanks contents))
  1745. ;; Impossible to add an alternative heading. Fallback to
  1746. ;; regular sectioning format string.
  1747. (format section-fmt full-text
  1748. (concat headline-label pre-blanks contents))))))))
  1749. (defun org-latex-format-headline-default-function
  1750. (todo _todo-type priority text tags _info)
  1751. "Default format function for a headline.
  1752. See `org-latex-format-headline-function' for details."
  1753. (concat
  1754. (and todo (format "{\\bfseries\\sffamily %s} " todo))
  1755. (and priority (format "\\framebox{\\#%c} " priority))
  1756. text
  1757. (and tags
  1758. (format "\\hfill{}\\textsc{%s}"
  1759. (mapconcat #'org-latex--protect-text tags ":")))))
  1760. ;;;; Horizontal Rule
  1761. (defun org-latex-horizontal-rule (horizontal-rule _contents info)
  1762. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1763. CONTENTS is nil. INFO is a plist holding contextual information."
  1764. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1765. (prev (org-export-get-previous-element horizontal-rule info)))
  1766. (concat
  1767. ;; Make sure the rule doesn't start at the end of the current
  1768. ;; line by separating it with a blank line from previous element.
  1769. (when (and prev
  1770. (let ((prev-blank (org-element-property :post-blank prev)))
  1771. (or (not prev-blank) (zerop prev-blank))))
  1772. "\n")
  1773. (org-latex--wrap-label
  1774. horizontal-rule
  1775. (format "\\noindent\\rule{%s}{%s}"
  1776. (or (plist-get attr :width) "\\textwidth")
  1777. (or (plist-get attr :thickness) "0.5pt"))
  1778. info))))
  1779. ;;;; Inline Src Block
  1780. (defun org-latex-inline-src-block (inline-src-block _contents info)
  1781. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1782. CONTENTS holds the contents of the item. INFO is a plist holding
  1783. contextual information."
  1784. (let* ((code (org-element-property :value inline-src-block))
  1785. (separator (org-latex--find-verb-separator code)))
  1786. (cl-case (plist-get info :latex-listings)
  1787. ;; Do not use a special package: transcode it verbatim.
  1788. ((nil) (format "\\texttt{%s}" (org-latex--text-markup code 'code info)))
  1789. ;; Use minted package.
  1790. (minted
  1791. (let* ((org-lang (org-element-property :language inline-src-block))
  1792. (mint-lang (or (cadr (assq (intern org-lang)
  1793. (plist-get info :latex-minted-langs)))
  1794. (downcase org-lang)))
  1795. (options (org-latex--make-option-string
  1796. (plist-get info :latex-minted-options))))
  1797. (format "\\mintinline%s{%s}{%s}"
  1798. (if (string= options "") "" (format "[%s]" options))
  1799. mint-lang
  1800. code)))
  1801. ;; Use listings package.
  1802. (otherwise
  1803. ;; Maybe translate language's name.
  1804. (let* ((org-lang (org-element-property :language inline-src-block))
  1805. (lst-lang (or (cadr (assq (intern org-lang)
  1806. (plist-get info :latex-listings-langs)))
  1807. org-lang))
  1808. (options (org-latex--make-option-string
  1809. (append (plist-get info :latex-listings-options)
  1810. `(("language" ,lst-lang))))))
  1811. (concat (format "\\lstinline[%s]" options)
  1812. separator code separator))))))
  1813. ;;;; Inlinetask
  1814. (defun org-latex-inlinetask (inlinetask contents info)
  1815. "Transcode an INLINETASK element from Org to LaTeX.
  1816. CONTENTS holds the contents of the block. INFO is a plist
  1817. holding contextual information."
  1818. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1819. (todo (and (plist-get info :with-todo-keywords)
  1820. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1821. (and todo (org-export-data todo info)))))
  1822. (todo-type (org-element-property :todo-type inlinetask))
  1823. (tags (and (plist-get info :with-tags)
  1824. (org-export-get-tags inlinetask info)))
  1825. (priority (and (plist-get info :with-priority)
  1826. (org-element-property :priority inlinetask)))
  1827. (contents (concat (org-latex--label inlinetask info) contents)))
  1828. (funcall (plist-get info :latex-format-inlinetask-function)
  1829. todo todo-type priority title tags contents info)))
  1830. (defun org-latex-format-inlinetask-default-function
  1831. (todo _todo-type priority title tags contents _info)
  1832. "Default format function for a inlinetasks.
  1833. See `org-latex-format-inlinetask-function' for details."
  1834. (let ((full-title
  1835. (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1836. (when priority (format "\\framebox{\\#%c} " priority))
  1837. title
  1838. (when tags
  1839. (format "\\hfill{}\\textsc{:%s:}"
  1840. (mapconcat #'org-latex--protect-text tags ":"))))))
  1841. (concat "\\begin{center}\n"
  1842. "\\fbox{\n"
  1843. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1844. full-title "\n\n"
  1845. (and (org-string-nw-p contents)
  1846. (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
  1847. "\\end{minipage}\n"
  1848. "}\n"
  1849. "\\end{center}")))
  1850. ;;;; Italic
  1851. (defun org-latex-italic (_italic contents info)
  1852. "Transcode ITALIC from Org to LaTeX.
  1853. CONTENTS is the text with italic markup. INFO is a plist holding
  1854. contextual information."
  1855. (org-latex--text-markup contents 'italic info))
  1856. ;;;; Item
  1857. (defun org-latex-item (item contents info)
  1858. "Transcode an ITEM element from Org to LaTeX.
  1859. CONTENTS holds the contents of the item. INFO is a plist holding
  1860. contextual information."
  1861. (let* ((counter
  1862. (let ((count (org-element-property :counter item))
  1863. (level
  1864. ;; Determine level of current item to determine the
  1865. ;; correct LaTeX counter to use (enumi, enumii...).
  1866. (let ((parent item) (level 0))
  1867. (while (memq (org-element-type
  1868. (setq parent (org-export-get-parent parent)))
  1869. '(plain-list item))
  1870. (when (and (eq (org-element-type parent) 'plain-list)
  1871. (eq (org-element-property :type parent)
  1872. 'ordered))
  1873. (cl-incf level)))
  1874. level)))
  1875. (and count
  1876. (< level 5)
  1877. (format "\\setcounter{enum%s}{%s}\n"
  1878. (nth (1- level) '("i" "ii" "iii" "iv"))
  1879. (1- count)))))
  1880. (checkbox (cl-case (org-element-property :checkbox item)
  1881. (on "$\\boxtimes$ ")
  1882. (off "$\\square$ ")
  1883. (trans "$\\boxminus$ ")))
  1884. (tag (let ((tag (org-element-property :tag item)))
  1885. ;; Check-boxes must belong to the tag.
  1886. (and tag (format "[{%s}] "
  1887. (concat checkbox
  1888. (org-export-data tag info)))))))
  1889. (concat counter
  1890. "\\item"
  1891. (cond
  1892. (tag)
  1893. (checkbox (concat " " checkbox))
  1894. ;; Without a tag or a check-box, if CONTENTS starts with
  1895. ;; an opening square bracket, add "\relax" to "\item",
  1896. ;; unless the brackets comes from an initial export
  1897. ;; snippet (i.e. it is inserted willingly by the user).
  1898. ((and contents
  1899. (string-match-p "\\`[ \t]*\\[" contents)
  1900. (not (let ((e (car (org-element-contents item))))
  1901. (and (eq (org-element-type e) 'paragraph)
  1902. (let ((o (car (org-element-contents e))))
  1903. (and (eq (org-element-type o) 'export-snippet)
  1904. (eq (org-export-snippet-backend o)
  1905. 'latex)))))))
  1906. "\\relax ")
  1907. (t " "))
  1908. (and contents (org-trim contents))
  1909. ;; If there are footnotes references in tag, be sure to
  1910. ;; add their definition at the end of the item. This
  1911. ;; workaround is necessary since "\footnote{}" command is
  1912. ;; not supported in tags.
  1913. (and tag
  1914. (org-latex--delayed-footnotes-definitions
  1915. (org-element-property :tag item) info)))))
  1916. ;;;; Keyword
  1917. (defun org-latex-keyword (keyword _contents info)
  1918. "Transcode a KEYWORD element from Org to LaTeX.
  1919. CONTENTS is nil. INFO is a plist holding contextual information."
  1920. (let ((key (org-element-property :key keyword))
  1921. (value (org-element-property :value keyword)))
  1922. (cond
  1923. ((string= key "LATEX") value)
  1924. ((string= key "INDEX") (format "\\index{%s}" value))
  1925. ((string= key "TOC")
  1926. (let ((case-fold-search t))
  1927. (cond
  1928. ((string-match-p "\\<headlines\\>" value)
  1929. (let* ((localp (string-match-p "\\<local\\>" value))
  1930. (parent (org-element-lineage keyword '(headline)))
  1931. (level (if (not (and localp parent)) 0
  1932. (org-export-get-relative-level parent info)))
  1933. (depth
  1934. (and (string-match "\\<[0-9]+\\>" value)
  1935. (format
  1936. "\\setcounter{tocdepth}{%d}"
  1937. (+ (string-to-number (match-string 0 value)) level)))))
  1938. (if (and localp parent)
  1939. ;; Start local TOC, assuming package "titletoc" is
  1940. ;; required.
  1941. (format "\\startcontents[level-%d]
  1942. \\printcontents[level-%d]{}{0}{%s}"
  1943. level level (or depth ""))
  1944. (concat depth (and depth "\n") "\\tableofcontents"))))
  1945. ((string-match-p "\\<tables\\>" value) "\\listoftables")
  1946. ((string-match-p "\\<listings\\>" value)
  1947. (cl-case (plist-get info :latex-listings)
  1948. ((nil) "\\listoffigures")
  1949. (minted "\\listoflistings")
  1950. (otherwise "\\lstlistoflistings")))))))))
  1951. ;;;; Latex Environment
  1952. (defun org-latex-latex-environment (latex-environment _contents info)
  1953. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1954. CONTENTS is nil. INFO is a plist holding contextual information."
  1955. (when (plist-get info :with-latex)
  1956. (let ((value (org-remove-indentation
  1957. (org-element-property :value latex-environment))))
  1958. (if (not (org-element-property :name latex-environment)) value
  1959. ;; Environment is labeled: label must be within the environment
  1960. ;; (otherwise, a reference pointing to that element will count
  1961. ;; the section instead).
  1962. (with-temp-buffer
  1963. (insert value)
  1964. (goto-char (point-min))
  1965. (forward-line)
  1966. (insert (org-latex--label latex-environment info nil t))
  1967. (buffer-string))))))
  1968. ;;;; Latex Fragment
  1969. (defun org-latex-latex-fragment (latex-fragment _contents _info)
  1970. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1971. CONTENTS is nil. INFO is a plist holding contextual information."
  1972. (let ((value (org-element-property :value latex-fragment)))
  1973. ;; Trim math markers since the fragment is enclosed within
  1974. ;; a latex-math-block object anyway.
  1975. (cond ((string-match-p "\\`\\$[^$]" value) (substring value 1 -1))
  1976. ((string-prefix-p "\\(" value) (substring value 2 -2))
  1977. (t value))))
  1978. ;;;; Line Break
  1979. (defun org-latex-line-break (_line-break _contents _info)
  1980. "Transcode a LINE-BREAK object from Org to LaTeX.
  1981. CONTENTS is nil. INFO is a plist holding contextual information."
  1982. "\\\\\n")
  1983. ;;;; Link
  1984. (defun org-latex--inline-image (link info)
  1985. "Return LaTeX code for an inline image.
  1986. LINK is the link pointing to the inline image. INFO is a plist
  1987. used as a communication channel."
  1988. (let* ((parent (org-export-get-parent-element link))
  1989. (path (let ((raw-path (org-element-property :path link)))
  1990. (if (not (file-name-absolute-p raw-path)) raw-path
  1991. (expand-file-name raw-path))))
  1992. (filetype (file-name-extension path))
  1993. (caption (org-latex--caption/label-string parent info))
  1994. (caption-above-p (org-latex--caption-above-p link info))
  1995. ;; Retrieve latex attributes from the element around.
  1996. (attr (org-export-read-attribute :attr_latex parent))
  1997. (float (let ((float (plist-get attr :float)))
  1998. (cond ((string= float "wrap") 'wrap)
  1999. ((string= float "sideways") 'sideways)
  2000. ((string= float "multicolumn") 'multicolumn)
  2001. ((and (plist-member attr :float) (not float)) 'nonfloat)
  2002. ((or float
  2003. (org-element-property :caption parent)
  2004. (org-string-nw-p (plist-get attr :caption)))
  2005. 'figure)
  2006. (t 'nonfloat))))
  2007. (placement
  2008. (let ((place (plist-get attr :placement)))
  2009. (cond
  2010. (place (format "%s" place))
  2011. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  2012. ((eq float 'figure)
  2013. (format "[%s]" (plist-get info :latex-default-figure-position)))
  2014. (t ""))))
  2015. (center
  2016. (if (plist-member attr :center) (plist-get attr :center)
  2017. (plist-get info :latex-images-centered)))
  2018. (comment-include (if (plist-get attr :comment-include) "%" ""))
  2019. ;; It is possible to specify width and height in the
  2020. ;; ATTR_LATEX line, and also via default variables.
  2021. (width (cond ((plist-get attr :width))
  2022. ((plist-get attr :height) "")
  2023. ((eq float 'wrap) "0.48\\textwidth")
  2024. (t (plist-get info :latex-image-default-width))))
  2025. (height (cond ((plist-get attr :height))
  2026. ((or (plist-get attr :width)
  2027. (memq float '(figure wrap))) "")
  2028. (t (plist-get info :latex-image-default-height))))
  2029. (options (let ((opt (or (plist-get attr :options)
  2030. (plist-get info :latex-image-default-option))))
  2031. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  2032. (match-string 1 opt))))
  2033. image-code)
  2034. (if (member filetype '("tikz" "pgf"))
  2035. ;; For tikz images:
  2036. ;; - use \input to read in image file.
  2037. ;; - if options are present, wrap in a tikzpicture environment.
  2038. ;; - if width or height are present, use \resizebox to change
  2039. ;; the image size.
  2040. (progn
  2041. (setq image-code (format "\\input{%s}" path))
  2042. (when (org-string-nw-p options)
  2043. (setq image-code
  2044. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  2045. options
  2046. image-code)))
  2047. (when (or (org-string-nw-p width) (org-string-nw-p height))
  2048. (setq image-code (format "\\resizebox{%s}{%s}{%s}"
  2049. (if (org-string-nw-p width) width "!")
  2050. (if (org-string-nw-p height) height "!")
  2051. image-code))))
  2052. ;; For other images:
  2053. ;; - add width and height to options.
  2054. ;; - include the image with \includegraphics.
  2055. (when (org-string-nw-p width)
  2056. (setq options (concat options ",width=" width)))
  2057. (when (org-string-nw-p height)
  2058. (setq options (concat options ",height=" height)))
  2059. (let ((search-option (org-element-property :search-option link)))
  2060. (when (and search-option
  2061. (equal filetype "pdf")
  2062. (string-match-p "\\`[0-9]+\\'" search-option)
  2063. (not (string-match-p "page=" options)))
  2064. (setq options (concat options ",page=" search-option))))
  2065. (setq image-code
  2066. (format "\\includegraphics%s{%s}"
  2067. (cond ((not (org-string-nw-p options)) "")
  2068. ((string-prefix-p "," options)
  2069. (format "[%s]" (substring options 1)))
  2070. (t (format "[%s]" options)))
  2071. path))
  2072. (when (equal filetype "svg")
  2073. (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
  2074. "\\includesvg"
  2075. image-code
  2076. nil t))
  2077. (setq image-code (replace-regexp-in-string "\\.svg}"
  2078. "}"
  2079. image-code
  2080. nil t))))
  2081. ;; Return proper string, depending on FLOAT.
  2082. (pcase float
  2083. (`wrap (format "\\begin{wrapfigure}%s
  2084. %s%s
  2085. %s%s
  2086. %s\\end{wrapfigure}"
  2087. placement
  2088. (if caption-above-p caption "")
  2089. (if center "\\centering" "")
  2090. comment-include image-code
  2091. (if caption-above-p "" caption)))
  2092. (`sideways (format "\\begin{sidewaysfigure}
  2093. %s%s
  2094. %s%s
  2095. %s\\end{sidewaysfigure}"
  2096. (if caption-above-p caption "")
  2097. (if center "\\centering" "")
  2098. comment-include image-code
  2099. (if caption-above-p "" caption)))
  2100. (`multicolumn (format "\\begin{figure*}%s
  2101. %s%s
  2102. %s%s
  2103. %s\\end{figure*}"
  2104. placement
  2105. (if caption-above-p caption "")
  2106. (if center "\\centering" "")
  2107. comment-include image-code
  2108. (if caption-above-p "" caption)))
  2109. (`figure (format "\\begin{figure}%s
  2110. %s%s
  2111. %s%s
  2112. %s\\end{figure}"
  2113. placement
  2114. (if caption-above-p caption "")
  2115. (if center "\\centering" "")
  2116. comment-include image-code
  2117. (if caption-above-p "" caption)))
  2118. ((guard center)
  2119. (format "\\begin{center}
  2120. %s%s
  2121. %s\\end{center}"
  2122. (if caption-above-p caption "")
  2123. image-code
  2124. (if caption-above-p "" caption)))
  2125. (_
  2126. (concat (if caption-above-p caption "")
  2127. image-code
  2128. (if caption-above-p caption ""))))))
  2129. (defun org-latex-link (link desc info)
  2130. "Transcode a LINK object from Org to LaTeX.
  2131. DESC is the description part of the link, or the empty string.
  2132. INFO is a plist holding contextual information. See
  2133. `org-export-data'."
  2134. (let* ((type (org-element-property :type link))
  2135. (raw-path (org-element-property :path link))
  2136. ;; Ensure DESC really exists, or set it to nil.
  2137. (desc (and (not (string= desc "")) desc))
  2138. (imagep (org-export-inline-image-p
  2139. link (plist-get info :latex-inline-image-rules)))
  2140. (path (org-latex--protect-text
  2141. (cond ((member type '("http" "https" "ftp" "mailto" "doi"))
  2142. (concat type ":" raw-path))
  2143. ((string= type "file") (org-export-file-uri raw-path))
  2144. (t raw-path)))))
  2145. (cond
  2146. ;; Link type is handled by a special function.
  2147. ((org-export-custom-protocol-maybe link desc 'latex))
  2148. ;; Image file.
  2149. (imagep (org-latex--inline-image link info))
  2150. ;; Radio link: Transcode target's contents and use them as link's
  2151. ;; description.
  2152. ((string= type "radio")
  2153. (let ((destination (org-export-resolve-radio-link link info)))
  2154. (if (not destination) desc
  2155. (format "\\hyperref[%s]{%s}"
  2156. (org-export-get-reference destination info)
  2157. desc))))
  2158. ;; Links pointing to a headline: Find destination and build
  2159. ;; appropriate referencing command.
  2160. ((member type '("custom-id" "fuzzy" "id"))
  2161. (let ((destination (if (string= type "fuzzy")
  2162. (org-export-resolve-fuzzy-link link info)
  2163. (org-export-resolve-id-link link info))))
  2164. (cl-case (org-element-type destination)
  2165. ;; Id link points to an external file.
  2166. (plain-text
  2167. (if desc (format "\\href{%s}{%s}" destination desc)
  2168. (format "\\url{%s}" destination)))
  2169. ;; Fuzzy link points nowhere.
  2170. ((nil)
  2171. (format (plist-get info :latex-link-with-unknown-path-format)
  2172. (or desc
  2173. (org-export-data
  2174. (org-element-property :raw-link link) info))))
  2175. ;; LINK points to a headline. If headlines are numbered
  2176. ;; and the link has no description, display headline's
  2177. ;; number. Otherwise, display description or headline's
  2178. ;; title.
  2179. (headline
  2180. (let ((label (org-latex--label destination info t)))
  2181. (if (and (not desc)
  2182. (org-export-numbered-headline-p destination info))
  2183. (format "\\ref{%s}" label)
  2184. (format "\\hyperref[%s]{%s}" label
  2185. (or desc
  2186. (org-export-data
  2187. (org-element-property :title destination) info))))))
  2188. ;; Fuzzy link points to a target. Do as above.
  2189. (otherwise
  2190. (let ((ref (org-latex--label destination info t)))
  2191. (if (not desc) (format "\\ref{%s}" ref)
  2192. (format "\\hyperref[%s]{%s}" ref desc)))))))
  2193. ;; Coderef: replace link with the reference name or the
  2194. ;; equivalent line number.
  2195. ((string= type "coderef")
  2196. (format (org-export-get-coderef-format path desc)
  2197. (org-export-resolve-coderef path info)))
  2198. ;; External link with a description part.
  2199. ((and path desc) (format "\\href{%s}{%s}" path desc))
  2200. ;; External link without a description part.
  2201. (path (format "\\url{%s}" path))
  2202. ;; No path, only description. Try to do something useful.
  2203. (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
  2204. ;;;; Node Property
  2205. (defun org-latex-node-property (node-property _contents _info)
  2206. "Transcode a NODE-PROPERTY element from Org to LaTeX.
  2207. CONTENTS is nil. INFO is a plist holding contextual
  2208. information."
  2209. (format "%s:%s"
  2210. (org-element-property :key node-property)
  2211. (let ((value (org-element-property :value node-property)))
  2212. (if value (concat " " value) ""))))
  2213. ;;;; Paragraph
  2214. (defun org-latex-paragraph (_paragraph contents _info)
  2215. "Transcode a PARAGRAPH element from Org to LaTeX.
  2216. CONTENTS is the contents of the paragraph, as a string. INFO is
  2217. the plist used as a communication channel."
  2218. contents)
  2219. ;;;; Plain List
  2220. (defun org-latex-plain-list (plain-list contents info)
  2221. "Transcode a PLAIN-LIST element from Org to LaTeX.
  2222. CONTENTS is the contents of the list. INFO is a plist holding
  2223. contextual information."
  2224. (let* ((type (org-element-property :type plain-list))
  2225. (attr (org-export-read-attribute :attr_latex plain-list))
  2226. (latex-type (let ((env (plist-get attr :environment)))
  2227. (cond (env (format "%s" env))
  2228. ((eq type 'ordered) "enumerate")
  2229. ((eq type 'descriptive) "description")
  2230. (t "itemize")))))
  2231. (org-latex--wrap-label
  2232. plain-list
  2233. (format "\\begin{%s}%s\n%s\\end{%s}"
  2234. latex-type
  2235. (or (plist-get attr :options) "")
  2236. contents
  2237. latex-type)
  2238. info)))
  2239. ;;;; Plain Text
  2240. (defun org-latex-plain-text (text info)
  2241. "Transcode a TEXT string from Org to LaTeX.
  2242. TEXT is the string to transcode. INFO is a plist holding
  2243. contextual information."
  2244. (let* ((specialp (plist-get info :with-special-strings))
  2245. (output
  2246. ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
  2247. (let ((case-fold-search nil))
  2248. (replace-regexp-in-string
  2249. "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
  2250. ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
  2251. ;; However, if special strings are used, be careful not
  2252. ;; to protect "\" in "\-" constructs.
  2253. (replace-regexp-in-string
  2254. (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
  2255. (lambda (m)
  2256. (cl-case (string-to-char m)
  2257. (?\\ "$\\\\backslash$\\1")
  2258. (?~ "\\\\textasciitilde{}")
  2259. (?^ "\\\\^{}")
  2260. (t "\\\\\\&")))
  2261. text)))))
  2262. ;; Activate smart quotes. Be sure to provide original TEXT string
  2263. ;; since OUTPUT may have been modified.
  2264. (when (plist-get info :with-smart-quotes)
  2265. (setq output (org-export-activate-smart-quotes output :latex info text)))
  2266. ;; Convert special strings.
  2267. (when specialp
  2268. (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
  2269. ;; Handle break preservation if required.
  2270. (when (plist-get info :preserve-breaks)
  2271. (setq output (replace-regexp-in-string
  2272. "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
  2273. ;; Return value.
  2274. output))
  2275. ;;;; Planning
  2276. (defun org-latex-planning (planning _contents info)
  2277. "Transcode a PLANNING element from Org to LaTeX.
  2278. CONTENTS is nil. INFO is a plist holding contextual
  2279. information."
  2280. (concat
  2281. "\\noindent"
  2282. (mapconcat
  2283. 'identity
  2284. (delq nil
  2285. (list
  2286. (let ((closed (org-element-property :closed planning)))
  2287. (when closed
  2288. (concat
  2289. (format "\\textbf{%s} " org-closed-string)
  2290. (format (plist-get info :latex-inactive-timestamp-format)
  2291. (org-timestamp-translate closed)))))
  2292. (let ((deadline (org-element-property :deadline planning)))
  2293. (when deadline
  2294. (concat
  2295. (format "\\textbf{%s} " org-deadline-string)
  2296. (format (plist-get info :latex-active-timestamp-format)
  2297. (org-timestamp-translate deadline)))))
  2298. (let ((scheduled (org-element-property :scheduled planning)))
  2299. (when scheduled
  2300. (concat
  2301. (format "\\textbf{%s} " org-scheduled-string)
  2302. (format (plist-get info :latex-active-timestamp-format)
  2303. (org-timestamp-translate scheduled)))))))
  2304. " ")
  2305. "\\\\"))
  2306. ;;;; Property Drawer
  2307. (defun org-latex-property-drawer (_property-drawer contents _info)
  2308. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  2309. CONTENTS holds the contents of the drawer. INFO is a plist
  2310. holding contextual information."
  2311. (and (org-string-nw-p contents)
  2312. (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
  2313. ;;;; Pseudo Element: LaTeX Matrices
  2314. ;; `latex-matrices' elements have the following properties:
  2315. ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
  2316. ;; `math').
  2317. (defun org-latex--wrap-latex-matrices (data info)
  2318. "Merge contiguous tables with the same mode within a pseudo-element.
  2319. DATA is a parse tree or a secondary string. INFO is a plist
  2320. containing export options. Modify DATA by side-effect and return
  2321. it."
  2322. (org-element-map data 'table
  2323. (lambda (table)
  2324. (when (eq (org-element-property :type table) 'org)
  2325. (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
  2326. (plist-get info :latex-default-table-mode))))
  2327. (when (and (member mode '("inline-math" "math"))
  2328. ;; Do not wrap twice the same table.
  2329. (not (eq (org-element-type
  2330. (org-element-property :parent table))
  2331. 'latex-matrices)))
  2332. (let* ((caption (and (not (string= mode "inline-math"))
  2333. (org-element-property :caption table)))
  2334. (matrices
  2335. (list 'latex-matrices
  2336. (list :caption caption
  2337. :markup
  2338. (cond ((string= mode "inline-math") 'inline)
  2339. (caption 'equation)
  2340. (t 'math)))))
  2341. (previous table)
  2342. (next (org-export-get-next-element table info)))
  2343. (org-element-insert-before matrices table)
  2344. ;; Swallow all contiguous tables sharing the same mode.
  2345. (while (and
  2346. (zerop (or (org-element-property :post-blank previous) 0))
  2347. (setq next (org-export-get-next-element previous info))
  2348. (eq (org-element-type next) 'table)
  2349. (eq (org-element-property :type next) 'org)
  2350. (string= (or (org-export-read-attribute
  2351. :attr_latex next :mode)
  2352. (plist-get info :latex-default-table-mode))
  2353. mode))
  2354. (org-element-extract-element previous)
  2355. (org-element-adopt-elements matrices previous)
  2356. (setq previous next))
  2357. ;; Inherit `:post-blank' from the value of the last
  2358. ;; swallowed table. Set the latter's `:post-blank'
  2359. ;; value to 0 so as to not duplicate empty lines.
  2360. (org-element-put-property
  2361. matrices :post-blank (org-element-property :post-blank previous))
  2362. (org-element-put-property previous :post-blank 0)
  2363. (org-element-extract-element previous)
  2364. (org-element-adopt-elements matrices previous))))))
  2365. info)
  2366. data)
  2367. (defun org-latex-matrices (matrices contents _info)
  2368. "Transcode a MATRICES element from Org to LaTeX.
  2369. CONTENTS is a string. INFO is a plist used as a communication
  2370. channel."
  2371. (format (cl-case (org-element-property :markup matrices)
  2372. (inline "\\(%s\\)")
  2373. (equation "\\begin{equation}\n%s\\end{equation}")
  2374. (t "\\[\n%s\\]"))
  2375. contents))
  2376. ;;;; Pseudo Object: LaTeX Math Block
  2377. ;; `latex-math-block' objects have the following property:
  2378. ;; `:post-blank'.
  2379. (defun org-latex--wrap-latex-math-block (data info)
  2380. "Merge contiguous math objects in a pseudo-object container.
  2381. DATA is a parse tree or a secondary string. INFO is a plist
  2382. containing export options. Modify DATA by side-effect and return it."
  2383. (let ((valid-object-p
  2384. ;; Non-nil when OBJ can be added to the latex math block B.
  2385. (lambda (obj b)
  2386. (pcase (org-element-type obj)
  2387. (`entity (org-element-property :latex-math-p obj))
  2388. (`latex-fragment
  2389. (let ((value (org-element-property :value obj)))
  2390. (or (string-prefix-p "\\(" value)
  2391. (string-match-p "\\`\\$[^$]" value))))
  2392. ((and type (or `subscript `superscript))
  2393. (not (memq type (mapcar #'org-element-type
  2394. (org-element-contents b)))))))))
  2395. (org-element-map data '(entity latex-fragment subscript superscript)
  2396. (lambda (object)
  2397. ;; Skip objects already wrapped.
  2398. (when (and (not (eq (org-element-type
  2399. (org-element-property :parent object))
  2400. 'latex-math-block))
  2401. (funcall valid-object-p object nil))
  2402. (let ((math-block (list 'latex-math-block nil))
  2403. (next-elements (org-export-get-next-element object info t))
  2404. (last object))
  2405. ;; Wrap MATH-BLOCK around OBJECT in DATA.
  2406. (org-element-insert-before math-block object)
  2407. (org-element-extract-element object)
  2408. (org-element-adopt-elements math-block object)
  2409. (when (zerop (or (org-element-property :post-blank object) 0))
  2410. ;; MATH-BLOCK swallows consecutive math objects.
  2411. (catch 'exit
  2412. (dolist (next next-elements)
  2413. (unless (funcall valid-object-p next math-block)
  2414. (throw 'exit nil))
  2415. (org-element-extract-element next)
  2416. (org-element-adopt-elements math-block next)
  2417. ;; Eschew the case: \beta$x$ -> \(\betax\).
  2418. (unless (memq (org-element-type next)
  2419. '(subscript superscript))
  2420. (org-element-put-property last :post-blank 1))
  2421. (setq last next)
  2422. (when (> (or (org-element-property :post-blank next) 0) 0)
  2423. (throw 'exit nil)))))
  2424. (org-element-put-property
  2425. math-block :post-blank (org-element-property :post-blank last)))))
  2426. info nil '(subscript superscript latex-math-block) t)
  2427. ;; Return updated DATA.
  2428. data))
  2429. (defun org-latex-math-block (_math-block contents _info)
  2430. "Transcode a MATH-BLOCK object from Org to LaTeX.
  2431. CONTENTS is a string. INFO is a plist used as a communication
  2432. channel."
  2433. (when (org-string-nw-p contents)
  2434. (format "\\(%s\\)" (org-trim contents))))
  2435. ;;;; Quote Block
  2436. (defun org-latex-quote-block (quote-block contents info)
  2437. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  2438. CONTENTS holds the contents of the block. INFO is a plist
  2439. holding contextual information."
  2440. (org-latex--wrap-label
  2441. quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
  2442. ;;;; Radio Target
  2443. (defun org-latex-radio-target (radio-target text info)
  2444. "Transcode a RADIO-TARGET object from Org to LaTeX.
  2445. TEXT is the text of the target. INFO is a plist holding
  2446. contextual information."
  2447. (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
  2448. ;;;; Section
  2449. (defun org-latex-section (_section contents _info)
  2450. "Transcode a SECTION element from Org to LaTeX.
  2451. CONTENTS holds the contents of the section. INFO is a plist
  2452. holding contextual information."
  2453. contents)
  2454. ;;;; Special Block
  2455. (defun org-latex-special-block (special-block contents info)
  2456. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  2457. CONTENTS holds the contents of the block. INFO is a plist
  2458. holding contextual information."
  2459. (let ((type (org-element-property :type special-block))
  2460. (opt (org-export-read-attribute :attr_latex special-block :options))
  2461. (caption (org-latex--caption/label-string special-block info))
  2462. (caption-above-p (org-latex--caption-above-p special-block info)))
  2463. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  2464. (and caption-above-p caption)
  2465. contents
  2466. (and (not caption-above-p) caption)
  2467. (format "\\end{%s}" type))))
  2468. ;;;; Src Block
  2469. (defun org-latex-src-block (src-block _contents info)
  2470. "Transcode a SRC-BLOCK element from Org to LaTeX.
  2471. CONTENTS holds the contents of the item. INFO is a plist holding
  2472. contextual information."
  2473. (when (org-string-nw-p (org-element-property :value src-block))
  2474. (let* ((lang (org-element-property :language src-block))
  2475. (caption (org-element-property :caption src-block))
  2476. (caption-above-p (org-latex--caption-above-p src-block info))
  2477. (label (org-element-property :name src-block))
  2478. (custom-env (and lang
  2479. (cadr (assq (intern lang)
  2480. org-latex-custom-lang-environments))))
  2481. (num-start (org-export-get-loc src-block info))
  2482. (retain-labels (org-element-property :retain-labels src-block))
  2483. (attributes (org-export-read-attribute :attr_latex src-block))
  2484. (float (plist-get attributes :float))
  2485. (listings (plist-get info :latex-listings)))
  2486. (cond
  2487. ;; Case 1. No source fontification.
  2488. ((not listings)
  2489. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2490. (float-env
  2491. (cond ((string= "multicolumn" float)
  2492. (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
  2493. (plist-get info :latex-default-figure-position)
  2494. (if caption-above-p caption-str "")
  2495. (if caption-above-p "" caption-str)))
  2496. (caption (concat
  2497. (if caption-above-p caption-str "")
  2498. "%s"
  2499. (if caption-above-p "" (concat "\n" caption-str))))
  2500. (t "%s"))))
  2501. (format
  2502. float-env
  2503. (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
  2504. (org-export-format-code-default src-block info))))))
  2505. ;; Case 2. Custom environment.
  2506. (custom-env
  2507. (let ((caption-str (org-latex--caption/label-string src-block info))
  2508. (formatted-src (org-export-format-code-default src-block info)))
  2509. (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
  2510. (format "\\begin{%s}\n%s\\end{%s}\n"
  2511. custom-env
  2512. (concat (and caption-above-p caption-str)
  2513. formatted-src
  2514. (and (not caption-above-p) caption-str))
  2515. custom-env)
  2516. (format-spec custom-env
  2517. `((?s . ,formatted-src)
  2518. (?c . ,caption)
  2519. (?f . ,float)
  2520. (?l . ,(org-latex--label src-block info))
  2521. (?o . ,(or (plist-get attributes :options) "")))))))
  2522. ;; Case 3. Use minted package.
  2523. ((eq listings 'minted)
  2524. (let* ((caption-str (org-latex--caption/label-string src-block info))
  2525. (float-env
  2526. (cond
  2527. ((string= "multicolumn" float)
  2528. (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
  2529. (plist-get info :latex-default-figure-position)
  2530. (if caption-above-p caption-str "")
  2531. (if caption-above-p "" caption-str)))
  2532. (caption
  2533. (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
  2534. (plist-get info :latex-default-figure-position)
  2535. (if caption-above-p caption-str "")
  2536. (if caption-above-p "" caption-str)))
  2537. ((string= "t" float)
  2538. (concat (format "\\begin{listing}[%s]\n"
  2539. (plist-get info :latex-default-figure-position))
  2540. "%s\n\\end{listing}"))
  2541. (t "%s")))
  2542. (options (plist-get info :latex-minted-options))
  2543. (body
  2544. (format
  2545. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  2546. ;; Options.
  2547. (concat
  2548. (org-latex--make-option-string
  2549. (if (or (not num-start) (assoc "linenos" options))
  2550. options
  2551. (append
  2552. `(("linenos")
  2553. ("firstnumber" ,(number-to-string (1+ num-start))))
  2554. options)))
  2555. (let ((local-options (plist-get attributes :options)))
  2556. (and local-options (concat "," local-options))))
  2557. ;; Language.
  2558. (or (cadr (assq (intern lang)
  2559. (plist-get info :latex-minted-langs)))
  2560. (downcase lang))
  2561. ;; Source code.
  2562. (let* ((code-info (org-export-unravel-code src-block))
  2563. (max-width
  2564. (apply 'max
  2565. (mapcar 'length
  2566. (org-split-string (car code-info)
  2567. "\n")))))
  2568. (org-export-format-code
  2569. (car code-info)
  2570. (lambda (loc _num ref)
  2571. (concat
  2572. loc
  2573. (when ref
  2574. ;; Ensure references are flushed to the right,
  2575. ;; separated with 6 spaces from the widest line
  2576. ;; of code.
  2577. (concat (make-string (+ (- max-width (length loc)) 6)
  2578. ?\s)
  2579. (format "(%s)" ref)))))
  2580. nil (and retain-labels (cdr code-info)))))))
  2581. ;; Return value.
  2582. (format float-env body)))
  2583. ;; Case 4. Use listings package.
  2584. (t
  2585. (let ((lst-lang
  2586. (or (cadr (assq (intern lang)
  2587. (plist-get info :latex-listings-langs)))
  2588. lang))
  2589. (caption-str
  2590. (when caption
  2591. (let ((main (org-export-get-caption src-block))
  2592. (secondary (org-export-get-caption src-block t)))
  2593. (if (not secondary)
  2594. (format "{%s}" (org-export-data main info))
  2595. (format "{[%s]%s}"
  2596. (org-export-data secondary info)
  2597. (org-export-data main info))))))
  2598. (lst-opt (plist-get info :latex-listings-options)))
  2599. (concat
  2600. ;; Options.
  2601. (format
  2602. "\\lstset{%s}\n"
  2603. (concat
  2604. (org-latex--make-option-string
  2605. (append
  2606. lst-opt
  2607. (cond
  2608. ((and (not float) (plist-member attributes :float)) nil)
  2609. ((string= "multicolumn" float) '(("float" "*")))
  2610. ((and float (not (assoc "float" lst-opt)))
  2611. `(("float" ,(plist-get info :latex-default-figure-position)))))
  2612. `(("language" ,lst-lang))
  2613. (if label
  2614. `(("label" ,(org-latex--label src-block info)))
  2615. '(("label" " ")))
  2616. (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
  2617. `(("captionpos" ,(if caption-above-p "t" "b")))
  2618. (cond ((assoc "numbers" lst-opt) nil)
  2619. ((not num-start) '(("numbers" "none")))
  2620. (t `(("firstnumber" ,(number-to-string (1+ num-start)))
  2621. ("numbers" "left"))))))
  2622. (let ((local-options (plist-get attributes :options)))
  2623. (and local-options (concat "," local-options)))))
  2624. ;; Source code.
  2625. (format
  2626. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  2627. (let* ((code-info (org-export-unravel-code src-block))
  2628. (max-width
  2629. (apply 'max
  2630. (mapcar 'length
  2631. (org-split-string (car code-info) "\n")))))
  2632. (org-export-format-code
  2633. (car code-info)
  2634. (lambda (loc _num ref)
  2635. (concat
  2636. loc
  2637. (when ref
  2638. ;; Ensure references are flushed to the right,
  2639. ;; separated with 6 spaces from the widest line of
  2640. ;; code
  2641. (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
  2642. (format "(%s)" ref)))))
  2643. nil (and retain-labels (cdr code-info))))))))))))
  2644. ;;;; Statistics Cookie
  2645. (defun org-latex-statistics-cookie (statistics-cookie _contents _info)
  2646. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  2647. CONTENTS is nil. INFO is a plist holding contextual information."
  2648. (replace-regexp-in-string
  2649. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  2650. ;;;; Strike-Through
  2651. (defun org-latex-strike-through (_strike-through contents info)
  2652. "Transcode STRIKE-THROUGH from Org to LaTeX.
  2653. CONTENTS is the text with strike-through markup. INFO is a plist
  2654. holding contextual information."
  2655. (org-latex--text-markup contents 'strike-through info))
  2656. ;;;; Subscript
  2657. (defun org-latex--script-size (object info)
  2658. "Transcode a subscript or superscript object.
  2659. OBJECT is an Org object. INFO is a plist used as a communication
  2660. channel."
  2661. (let ((output ""))
  2662. (org-element-map (org-element-contents object)
  2663. (cons 'plain-text org-element-all-objects)
  2664. (lambda (obj)
  2665. (cl-case (org-element-type obj)
  2666. ((entity latex-fragment)
  2667. (let ((data (org-trim (org-export-data obj info))))
  2668. (string-match
  2669. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  2670. data)
  2671. (setq output
  2672. (concat output
  2673. (match-string 1 data)
  2674. (let ((blank (org-element-property :post-blank obj)))
  2675. (and blank (> blank 0) "\\ "))))))
  2676. (plain-text
  2677. (setq output
  2678. (format "%s\\text{%s}" output (org-export-data obj info))))
  2679. (otherwise
  2680. (setq output
  2681. (concat output
  2682. (org-export-data obj info)
  2683. (let ((blank (org-element-property :post-blank obj)))
  2684. (and blank (> blank 0) "\\ ")))))))
  2685. info nil org-element-recursive-objects)
  2686. ;; Result. Do not wrap into curly brackets if OUTPUT is a single
  2687. ;; character.
  2688. (concat (if (eq (org-element-type object) 'subscript) "_" "^")
  2689. (and (> (length output) 1) "{")
  2690. output
  2691. (and (> (length output) 1) "}"))))
  2692. (defun org-latex-subscript (subscript _contents info)
  2693. "Transcode a SUBSCRIPT object from Org to LaTeX.
  2694. CONTENTS is the contents of the object. INFO is a plist holding
  2695. contextual information."
  2696. (org-latex--script-size subscript info))
  2697. ;;;; Superscript
  2698. (defun org-latex-superscript (superscript _contents info)
  2699. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  2700. CONTENTS is the contents of the object. INFO is a plist holding
  2701. contextual information."
  2702. (org-latex--script-size superscript info))
  2703. ;;;; Table
  2704. ;;
  2705. ;; `org-latex-table' is the entry point for table transcoding. It
  2706. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  2707. ;; delegates the job to either `org-latex--table.el-table',
  2708. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  2709. ;; depending of the type of the table and the mode requested.
  2710. ;;
  2711. ;; `org-latex--align-string' is a subroutine used to build alignment
  2712. ;; string for Org tables.
  2713. (defun org-latex-table (table contents info)
  2714. "Transcode a TABLE element from Org to LaTeX.
  2715. CONTENTS is the contents of the table. INFO is a plist holding
  2716. contextual information."
  2717. (if (eq (org-element-property :type table) 'table.el)
  2718. ;; "table.el" table. Convert it using appropriate tools.
  2719. (org-latex--table.el-table table info)
  2720. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  2721. (plist-get info :latex-default-table-mode))))
  2722. (cond
  2723. ;; Case 1: Verbatim table.
  2724. ((string= type "verbatim")
  2725. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  2726. ;; Re-create table, without affiliated keywords.
  2727. (org-trim (org-element-interpret-data
  2728. `(table nil ,@(org-element-contents table))))))
  2729. ;; Case 2: Matrix.
  2730. ((or (string= type "math") (string= type "inline-math"))
  2731. (org-latex--math-table table info))
  2732. ;; Case 3: Standard table.
  2733. (t (concat (org-latex--org-table table contents info)
  2734. ;; When there are footnote references within the
  2735. ;; table, insert their definition just after it.
  2736. (org-latex--delayed-footnotes-definitions table info)))))))
  2737. (defun org-latex--align-string (table info &optional math?)
  2738. "Return an appropriate LaTeX alignment string.
  2739. TABLE is the considered table. INFO is a plist used as
  2740. a communication channel. When optional argument MATH? is
  2741. non-nil, TABLE is meant to be a matrix, where all cells are
  2742. centered."
  2743. (or (org-export-read-attribute :attr_latex table :align)
  2744. (let (align)
  2745. ;; Extract column groups and alignment from first (non-rule)
  2746. ;; row.
  2747. (org-element-map
  2748. (org-element-map table 'table-row
  2749. (lambda (row)
  2750. (and (eq (org-element-property :type row) 'standard) row))
  2751. info 'first-match)
  2752. 'table-cell
  2753. (lambda (cell)
  2754. (let ((borders (org-export-table-cell-borders cell info)))
  2755. ;; Check left border for the first cell only.
  2756. (when (and (memq 'left borders) (not align))
  2757. (push "|" align))
  2758. (push (if math? "c" ;center cells in matrices
  2759. (cl-case (org-export-table-cell-alignment cell info)
  2760. (left "l")
  2761. (right "r")
  2762. (center "c")))
  2763. align)
  2764. (when (memq 'right borders) (push "|" align))))
  2765. info)
  2766. (apply 'concat (nreverse align)))))
  2767. (defun org-latex--org-table (table contents info)
  2768. "Return appropriate LaTeX code for an Org table.
  2769. TABLE is the table type element to transcode. CONTENTS is its
  2770. contents, as a string. INFO is a plist used as a communication
  2771. channel.
  2772. This function assumes TABLE has `org' as its `:type' property and
  2773. `table' as its `:mode' attribute."
  2774. (let* ((caption (org-latex--caption/label-string table info))
  2775. (attr (org-export-read-attribute :attr_latex table))
  2776. ;; Determine alignment string.
  2777. (alignment (org-latex--align-string table info))
  2778. ;; Determine environment for the table: longtable, tabular...
  2779. (table-env (or (plist-get attr :environment)
  2780. (plist-get info :latex-default-table-environment)))
  2781. ;; If table is a float, determine environment: table, table*
  2782. ;; or sidewaystable.
  2783. (float-env (unless (member table-env '("longtable" "longtabu"))
  2784. (let ((float (plist-get attr :float)))
  2785. (cond
  2786. ((and (not float) (plist-member attr :float)) nil)
  2787. ((or (string= float "sidewaystable")
  2788. (string= float "sideways")) "sidewaystable")
  2789. ((string= float "multicolumn") "table*")
  2790. ((or float
  2791. (org-element-property :caption table)
  2792. (org-string-nw-p (plist-get attr :caption)))
  2793. "table")))))
  2794. ;; Extract others display options.
  2795. (fontsize (let ((font (plist-get attr :font)))
  2796. (and font (concat font "\n"))))
  2797. ;; "tabular" environment doesn't allow to define a width.
  2798. (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
  2799. (spreadp (plist-get attr :spread))
  2800. (placement
  2801. (or (plist-get attr :placement)
  2802. (format "[%s]" (plist-get info :latex-default-figure-position))))
  2803. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2804. (plist-get info :latex-tables-centered)))
  2805. (caption-above-p (org-latex--caption-above-p table info)))
  2806. ;; Prepare the final format string for the table.
  2807. (cond
  2808. ;; Longtable.
  2809. ((equal "longtable" table-env)
  2810. (concat (and fontsize (concat "{" fontsize))
  2811. (format "\\begin{longtable}{%s}\n" alignment)
  2812. (and caption-above-p
  2813. (org-string-nw-p caption)
  2814. (concat caption "\\\\\n"))
  2815. contents
  2816. (and (not caption-above-p)
  2817. (org-string-nw-p caption)
  2818. (concat caption "\\\\\n"))
  2819. "\\end{longtable}\n"
  2820. (and fontsize "}")))
  2821. ;; Longtabu
  2822. ((equal "longtabu" table-env)
  2823. (concat (and fontsize (concat "{" fontsize))
  2824. (format "\\begin{longtabu}%s{%s}\n"
  2825. (if width
  2826. (format " %s %s "
  2827. (if spreadp "spread" "to") width) "")
  2828. alignment)
  2829. (and caption-above-p
  2830. (org-string-nw-p caption)
  2831. (concat caption "\\\\\n"))
  2832. contents
  2833. (and (not caption-above-p)
  2834. (org-string-nw-p caption)
  2835. (concat caption "\\\\\n"))
  2836. "\\end{longtabu}\n"
  2837. (and fontsize "}")))
  2838. ;; Others.
  2839. (t (concat (cond
  2840. (float-env
  2841. (concat (format "\\begin{%s}%s\n" float-env placement)
  2842. (if caption-above-p caption "")
  2843. (when centerp "\\centering\n")
  2844. fontsize))
  2845. ((and (not float-env) caption)
  2846. (concat
  2847. (and centerp "\\begin{center}\n" )
  2848. (if caption-above-p caption "")
  2849. (cond ((and fontsize centerp) fontsize)
  2850. (fontsize (concat "{" fontsize)))))
  2851. (centerp (concat "\\begin{center}\n" fontsize))
  2852. (fontsize (concat "{" fontsize)))
  2853. (cond ((equal "tabu" table-env)
  2854. (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
  2855. (if width (format
  2856. (if spreadp " spread %s " " to %s ")
  2857. width) "")
  2858. alignment
  2859. contents))
  2860. (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2861. table-env
  2862. (if width (format "{%s}" width) "")
  2863. alignment
  2864. contents
  2865. table-env)))
  2866. (cond
  2867. (float-env
  2868. (concat (if caption-above-p "" (concat "\n" caption))
  2869. (format "\n\\end{%s}" float-env)))
  2870. ((and (not float-env) caption)
  2871. (concat
  2872. (if caption-above-p "" (concat "\n" caption))
  2873. (and centerp "\n\\end{center}")
  2874. (and fontsize (not centerp) "}")))
  2875. (centerp "\n\\end{center}")
  2876. (fontsize "}")))))))
  2877. (defun org-latex--table.el-table (table info)
  2878. "Return appropriate LaTeX code for a table.el table.
  2879. TABLE is the table type element to transcode. INFO is a plist
  2880. used as a communication channel.
  2881. This function assumes TABLE has `table.el' as its `:type'
  2882. property."
  2883. (require 'table)
  2884. ;; Ensure "*org-export-table*" buffer is empty.
  2885. (with-current-buffer (get-buffer-create "*org-export-table*")
  2886. (erase-buffer))
  2887. (let ((output (with-temp-buffer
  2888. (insert (org-element-property :value table))
  2889. (goto-char 1)
  2890. (re-search-forward "^[ \t]*|[^|]" nil t)
  2891. (table-generate-source 'latex "*org-export-table*")
  2892. (with-current-buffer "*org-export-table*"
  2893. (org-trim (buffer-string))))))
  2894. (kill-buffer (get-buffer "*org-export-table*"))
  2895. ;; Remove left out comments.
  2896. (while (string-match "^%.*\n" output)
  2897. (setq output (replace-match "" t t output)))
  2898. (let ((attr (org-export-read-attribute :attr_latex table)))
  2899. (when (plist-get attr :rmlines)
  2900. ;; When the "rmlines" attribute is provided, remove all hlines
  2901. ;; but the the one separating heading from the table body.
  2902. (let ((n 0) (pos 0))
  2903. (while (and (< (length output) pos)
  2904. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2905. (cl-incf n)
  2906. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2907. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2908. (plist-get info :latex-tables-centered))))
  2909. (if (not centerp) output
  2910. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2911. (defun org-latex--math-table (table info)
  2912. "Return appropriate LaTeX code for a matrix.
  2913. TABLE is the table type element to transcode. INFO is a plist
  2914. used as a communication channel.
  2915. This function assumes TABLE has `org' as its `:type' property and
  2916. `inline-math' or `math' as its `:mode' attribute."
  2917. (let* ((attr (org-export-read-attribute :attr_latex table))
  2918. (env (or (plist-get attr :environment)
  2919. (plist-get info :latex-default-table-environment)))
  2920. (contents
  2921. (mapconcat
  2922. (lambda (row)
  2923. ;; Ignore horizontal rules.
  2924. (when (eq (org-element-property :type row) 'standard)
  2925. ;; Return each cell unmodified.
  2926. (concat
  2927. (mapconcat
  2928. (lambda (cell)
  2929. (substring (org-element-interpret-data cell) 0 -1))
  2930. (org-element-map row 'table-cell #'identity info) "&")
  2931. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2932. "\n")))
  2933. (org-element-map table 'table-row #'identity info) "")))
  2934. (concat
  2935. ;; Prefix.
  2936. (plist-get attr :math-prefix)
  2937. ;; Environment. Also treat special cases.
  2938. (cond ((member env '("array" "tabular"))
  2939. (format "\\begin{%s}{%s}\n%s\\end{%s}"
  2940. env (org-latex--align-string table info t) contents env))
  2941. ((assoc env org-latex-table-matrix-macros)
  2942. (format "\\%s%s{\n%s}"
  2943. env
  2944. (or (plist-get attr :math-arguments) "")
  2945. contents))
  2946. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2947. ;; Suffix.
  2948. (plist-get attr :math-suffix))))
  2949. ;;;; Table Cell
  2950. (defun org-latex-table-cell (table-cell contents info)
  2951. "Transcode a TABLE-CELL element from Org to LaTeX.
  2952. CONTENTS is the cell contents. INFO is a plist used as
  2953. a communication channel."
  2954. (concat
  2955. (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
  2956. (if (and contents
  2957. scientific-format
  2958. (string-match orgtbl-exp-regexp contents))
  2959. ;; Use appropriate format string for scientific
  2960. ;; notation.
  2961. (format scientific-format
  2962. (match-string 1 contents)
  2963. (match-string 2 contents))
  2964. contents))
  2965. (when (org-export-get-next-element table-cell info) " & ")))
  2966. ;;;; Table Row
  2967. (defun org-latex-table-row (table-row contents info)
  2968. "Transcode a TABLE-ROW element from Org to LaTeX.
  2969. CONTENTS is the contents of the row. INFO is a plist used as
  2970. a communication channel."
  2971. (let* ((attr (org-export-read-attribute :attr_latex
  2972. (org-export-get-parent table-row)))
  2973. (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
  2974. (plist-get info :latex-tables-booktabs)))
  2975. (longtablep
  2976. (member (or (plist-get attr :environment)
  2977. (plist-get info :latex-default-table-environment))
  2978. '("longtable" "longtabu"))))
  2979. (if (eq (org-element-property :type table-row) 'rule)
  2980. (cond
  2981. ((not booktabsp) "\\hline")
  2982. ((not (org-export-get-previous-element table-row info)) "\\toprule")
  2983. ((not (org-export-get-next-element table-row info)) "\\bottomrule")
  2984. ((and longtablep
  2985. (org-export-table-row-ends-header-p
  2986. (org-export-get-previous-element table-row info) info))
  2987. "")
  2988. (t "\\midrule"))
  2989. (concat
  2990. ;; When BOOKTABS are activated enforce top-rule even when no
  2991. ;; hline was specifically marked.
  2992. (and booktabsp (not (org-export-get-previous-element table-row info))
  2993. "\\toprule\n")
  2994. contents "\\\\\n"
  2995. (cond
  2996. ;; Special case for long tables. Define header and footers.
  2997. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2998. (let ((columns (cdr (org-export-table-dimensions
  2999. (org-export-get-parent-table table-row) info))))
  3000. (format "%s
  3001. \\endfirsthead
  3002. \\multicolumn{%d}{l}{%s} \\\\
  3003. %s
  3004. %s \\\\\n
  3005. %s
  3006. \\endhead
  3007. %s\\multicolumn{%d}{r}{%s} \\\\
  3008. \\endfoot
  3009. \\endlastfoot"
  3010. (if booktabsp "\\midrule" "\\hline")
  3011. columns
  3012. (org-latex--translate "Continued from previous page" info)
  3013. (cond
  3014. ((not (org-export-table-row-starts-header-p table-row info))
  3015. "")
  3016. (booktabsp "\\toprule\n")
  3017. (t "\\hline\n"))
  3018. contents
  3019. (if booktabsp "\\midrule" "\\hline")
  3020. (if booktabsp "\\midrule" "\\hline")
  3021. columns
  3022. (org-latex--translate "Continued on next page" info))))
  3023. ;; When BOOKTABS are activated enforce bottom rule even when
  3024. ;; no hline was specifically marked.
  3025. ((and booktabsp (not (org-export-get-next-element table-row info)))
  3026. "\\bottomrule"))))))
  3027. ;;;; Target
  3028. (defun org-latex-target (target _contents info)
  3029. "Transcode a TARGET object from Org to LaTeX.
  3030. CONTENTS is nil. INFO is a plist holding contextual
  3031. information."
  3032. (format "\\label{%s}" (org-latex--label target info)))
  3033. ;;;; Timestamp
  3034. (defun org-latex-timestamp (timestamp _contents info)
  3035. "Transcode a TIMESTAMP object from Org to LaTeX.
  3036. CONTENTS is nil. INFO is a plist holding contextual
  3037. information."
  3038. (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
  3039. (format
  3040. (plist-get info
  3041. (cl-case (org-element-property :type timestamp)
  3042. ((active active-range) :latex-active-timestamp-format)
  3043. ((inactive inactive-range) :latex-inactive-timestamp-format)
  3044. (otherwise :latex-diary-timestamp-format)))
  3045. value)))
  3046. ;;;; Underline
  3047. (defun org-latex-underline (_underline contents info)
  3048. "Transcode UNDERLINE from Org to LaTeX.
  3049. CONTENTS is the text with underline markup. INFO is a plist
  3050. holding contextual information."
  3051. (org-latex--text-markup contents 'underline info))
  3052. ;;;; Verbatim
  3053. (defun org-latex-verbatim (verbatim _contents info)
  3054. "Transcode a VERBATIM object from Org to LaTeX.
  3055. CONTENTS is nil. INFO is a plist used as a communication
  3056. channel."
  3057. (org-latex--text-markup
  3058. (org-element-property :value verbatim) 'verbatim info))
  3059. ;;;; Verse Block
  3060. (defun org-latex-verse-block (verse-block contents info)
  3061. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  3062. CONTENTS is verse block contents. INFO is a plist holding
  3063. contextual information."
  3064. (org-latex--wrap-label
  3065. verse-block
  3066. ;; In a verse environment, add a line break to each newline
  3067. ;; character and change each white space at beginning of a line
  3068. ;; into a space of 1 em. Also change each blank line with
  3069. ;; a vertical space of 1 em.
  3070. (format "\\begin{verse}\n%s\\end{verse}"
  3071. (replace-regexp-in-string
  3072. "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
  3073. (replace-regexp-in-string
  3074. "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
  3075. (replace-regexp-in-string
  3076. "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
  3077. contents nil t) nil t) nil t))
  3078. info))
  3079. ;;; End-user functions
  3080. ;;;###autoload
  3081. (defun org-latex-export-as-latex
  3082. (&optional async subtreep visible-only body-only ext-plist)
  3083. "Export current buffer as a LaTeX buffer.
  3084. If narrowing is active in the current buffer, only export its
  3085. narrowed part.
  3086. If a region is active, export that region.
  3087. A non-nil optional argument ASYNC means the process should happen
  3088. asynchronously. The resulting buffer should be accessible
  3089. through the `org-export-stack' interface.
  3090. When optional argument SUBTREEP is non-nil, export the sub-tree
  3091. at point, extracting information from the headline properties
  3092. first.
  3093. When optional argument VISIBLE-ONLY is non-nil, don't export
  3094. contents of hidden elements.
  3095. When optional argument BODY-ONLY is non-nil, only write code
  3096. between \"\\begin{document}\" and \"\\end{document}\".
  3097. EXT-PLIST, when provided, is a property list with external
  3098. parameters overriding Org default settings, but still inferior to
  3099. file-local settings.
  3100. Export is done in a buffer named \"*Org LATEX Export*\", which
  3101. will be displayed when `org-export-show-temporary-export-buffer'
  3102. is non-nil."
  3103. (interactive)
  3104. (org-export-to-buffer 'latex "*Org LATEX Export*"
  3105. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  3106. ;;;###autoload
  3107. (defun org-latex-convert-region-to-latex ()
  3108. "Assume the current region has Org syntax, and convert it to LaTeX.
  3109. This can be used in any buffer. For example, you can write an
  3110. itemized list in Org syntax in an LaTeX buffer and use this
  3111. command to convert it."
  3112. (interactive)
  3113. (org-export-replace-region-by 'latex))
  3114. ;;;###autoload
  3115. (defun org-latex-export-to-latex
  3116. (&optional async subtreep visible-only body-only ext-plist)
  3117. "Export current buffer to a LaTeX file.
  3118. If narrowing is active in the current buffer, only export its
  3119. narrowed part.
  3120. If a region is active, export that region.
  3121. A non-nil optional argument ASYNC means the process should happen
  3122. asynchronously. The resulting file should be accessible through
  3123. the `org-export-stack' interface.
  3124. When optional argument SUBTREEP is non-nil, export the sub-tree
  3125. at point, extracting information from the headline properties
  3126. first.
  3127. When optional argument VISIBLE-ONLY is non-nil, don't export
  3128. contents of hidden elements.
  3129. When optional argument BODY-ONLY is non-nil, only write code
  3130. between \"\\begin{document}\" and \"\\end{document}\".
  3131. EXT-PLIST, when provided, is a property list with external
  3132. parameters overriding Org default settings, but still inferior to
  3133. file-local settings."
  3134. (interactive)
  3135. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3136. (org-export-to-file 'latex outfile
  3137. async subtreep visible-only body-only ext-plist)))
  3138. ;;;###autoload
  3139. (defun org-latex-export-to-pdf
  3140. (&optional async subtreep visible-only body-only ext-plist)
  3141. "Export current buffer to LaTeX then process through to PDF.
  3142. If narrowing is active in the current buffer, only export its
  3143. narrowed part.
  3144. If a region is active, export that region.
  3145. A non-nil optional argument ASYNC means the process should happen
  3146. asynchronously. The resulting file should be accessible through
  3147. the `org-export-stack' interface.
  3148. When optional argument SUBTREEP is non-nil, export the sub-tree
  3149. at point, extracting information from the headline properties
  3150. first.
  3151. When optional argument VISIBLE-ONLY is non-nil, don't export
  3152. contents of hidden elements.
  3153. When optional argument BODY-ONLY is non-nil, only write code
  3154. between \"\\begin{document}\" and \"\\end{document}\".
  3155. EXT-PLIST, when provided, is a property list with external
  3156. parameters overriding Org default settings, but still inferior to
  3157. file-local settings.
  3158. Return PDF file's name."
  3159. (interactive)
  3160. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  3161. (org-export-to-file 'latex outfile
  3162. async subtreep visible-only body-only ext-plist
  3163. (lambda (file) (org-latex-compile file)))))
  3164. (defun org-latex-compile (texfile &optional snippet)
  3165. "Compile a TeX file.
  3166. TEXFILE is the name of the file being compiled. Processing is
  3167. done through the command specified in `org-latex-pdf-process',
  3168. which see. Output is redirected to \"*Org PDF LaTeX Output*\"
  3169. buffer.
  3170. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  3171. file used to preview a LaTeX snippet. In this case, do not
  3172. create a log buffer and do not remove log files.
  3173. Return PDF file name or raise an error if it couldn't be
  3174. produced."
  3175. (unless snippet (message "Processing LaTeX file %s..." texfile))
  3176. (let* ((compiler
  3177. (or (with-temp-buffer
  3178. (save-excursion (insert-file-contents texfile))
  3179. (and (search-forward-regexp (regexp-opt org-latex-compilers)
  3180. (line-end-position 2)
  3181. t)
  3182. (progn (beginning-of-line) (looking-at-p "%"))
  3183. (match-string 0)))
  3184. "pdflatex"))
  3185. (process (if (functionp org-latex-pdf-process) org-latex-pdf-process
  3186. ;; Replace "%latex" and "%bibtex" with,
  3187. ;; respectively, "%L" and "%B" so as to adhere to
  3188. ;; `format-spec' specifications.
  3189. (mapcar (lambda (command)
  3190. (replace-regexp-in-string
  3191. "%\\(?:bib\\|la\\)tex\\>"
  3192. (lambda (m) (upcase (substring m 0 2)))
  3193. command))
  3194. org-latex-pdf-process)))
  3195. (spec `((?B . ,(shell-quote-argument org-latex-bib-compiler))
  3196. (?L . ,(shell-quote-argument compiler))))
  3197. (log-buf-name "*Org PDF LaTeX Output*")
  3198. (log-buf (and (not snippet) (get-buffer-create log-buf-name)))
  3199. (outfile (org-compile-file texfile process "pdf"
  3200. (format "See %S for details" log-buf-name)
  3201. log-buf spec)))
  3202. (unless snippet
  3203. (when org-latex-remove-logfiles
  3204. (mapc #'delete-file
  3205. (directory-files
  3206. (file-name-directory outfile)
  3207. t
  3208. (concat (regexp-quote (file-name-base outfile))
  3209. "\\(?:\\.[0-9]+\\)?\\."
  3210. (regexp-opt org-latex-logfiles-extensions))
  3211. t)))
  3212. (let ((warnings (org-latex--collect-warnings log-buf)))
  3213. (message (concat "PDF file produced"
  3214. (cond
  3215. ((eq warnings 'error) " with errors.")
  3216. (warnings (concat " with warnings: " warnings))
  3217. (t "."))))))
  3218. ;; Return output file name.
  3219. outfile))
  3220. (defun org-latex--collect-warnings (buffer)
  3221. "Collect some warnings from \"pdflatex\" command output.
  3222. BUFFER is the buffer containing output. Return collected
  3223. warnings types as a string, `error' if a LaTeX error was
  3224. encountered or nil if there was none."
  3225. (with-current-buffer buffer
  3226. (save-excursion
  3227. (goto-char (point-max))
  3228. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  3229. (if (re-search-forward "^!" nil t) 'error
  3230. (let ((case-fold-search t)
  3231. (warnings ""))
  3232. (dolist (warning org-latex-known-warnings)
  3233. (when (save-excursion (re-search-forward (car warning) nil t))
  3234. (setq warnings (concat warnings " " (cdr warning)))))
  3235. (org-string-nw-p (org-trim warnings))))))))
  3236. ;;;###autoload
  3237. (defun org-latex-publish-to-latex (plist filename pub-dir)
  3238. "Publish an Org file to 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. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  3244. ;;;###autoload
  3245. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  3246. "Publish an Org file to PDF (via LaTeX).
  3247. FILENAME is the filename of the Org file to be published. PLIST
  3248. is the property list for the given project. PUB-DIR is the
  3249. publishing directory.
  3250. Return output file name."
  3251. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  3252. ;; in working directory and then moved to publishing directory.
  3253. (org-publish-attachment
  3254. plist
  3255. ;; Default directory could be anywhere when this function is
  3256. ;; called. We ensure it is set to source file directory during
  3257. ;; compilation so as to not break links to external documents.
  3258. (let ((default-directory (file-name-directory filename)))
  3259. (org-latex-compile
  3260. (org-publish-org-to
  3261. 'latex filename ".tex" plist (file-name-directory filename))))
  3262. pub-dir))
  3263. (provide 'ox-latex)
  3264. ;; Local variables:
  3265. ;; generated-autoload-file: "org-loaddefs.el"
  3266. ;; End:
  3267. ;;; ox-latex.el ends here