ox-latex.el 112 KB

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