ox-latex.el 103 KB

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