ox-latex.el 102 KB

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