ox-latex.el 119 KB

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