ox-latex.el 103 KB

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