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