ox-latex.el 126 KB

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