ox-latex.el 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950
  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" nil)))
  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. (concat "\\begin{center}\n"
  1300. "\\fbox{\n"
  1301. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1302. full-title "\n\n"
  1303. (and (org-string-nw-p contents)
  1304. (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
  1305. "\\end{minipage}\n"
  1306. "}\n"
  1307. "\\end{center}"))))))
  1308. ;;;; Italic
  1309. (defun org-latex-italic (italic contents info)
  1310. "Transcode ITALIC from Org to LaTeX.
  1311. CONTENTS is the text with italic markup. INFO is a plist holding
  1312. contextual information."
  1313. (org-latex--text-markup contents 'italic))
  1314. ;;;; Item
  1315. (defun org-latex-item (item contents info)
  1316. "Transcode an ITEM element from Org to LaTeX.
  1317. CONTENTS holds the contents of the item. INFO is a plist holding
  1318. contextual information."
  1319. (let* ((counter
  1320. (let ((count (org-element-property :counter item))
  1321. (level
  1322. ;; Determine level of current item to determine the
  1323. ;; correct LaTeX counter to use (enumi, enumii...).
  1324. (let ((parent item) (level 0))
  1325. (while (memq (org-element-type
  1326. (setq parent (org-export-get-parent parent)))
  1327. '(plain-list item))
  1328. (when (and (eq (org-element-type parent) 'plain-list)
  1329. (eq (org-element-property :type parent)
  1330. 'ordered))
  1331. (incf level)))
  1332. level)))
  1333. (and count
  1334. (< level 5)
  1335. (format "\\setcounter{enum%s}{%s}\n"
  1336. (nth (1- level) '("i" "ii" "iii" "iv"))
  1337. (1- count)))))
  1338. (checkbox (case (org-element-property :checkbox item)
  1339. (on "$\\boxtimes$ ")
  1340. (off "$\\square$ ")
  1341. (trans "$\\boxminus$ ")))
  1342. (tag (let ((tag (org-element-property :tag item)))
  1343. ;; Check-boxes must belong to the tag.
  1344. (and tag (format "[{%s}] "
  1345. (concat checkbox
  1346. (org-export-data tag info)))))))
  1347. (concat counter
  1348. "\\item"
  1349. (cond
  1350. (tag)
  1351. (checkbox (concat " " checkbox))
  1352. ;; Without a tag or a check-box, if CONTENTS starts with
  1353. ;; an opening square bracket, add "\relax" to "\item",
  1354. ;; unless the brackets comes from an initial export
  1355. ;; snippet (i.e. it is inserted willingly by the user).
  1356. ((and contents
  1357. (org-string-match-p "\\`[ \t]*\\[" contents)
  1358. (not (let ((e (car (org-element-contents item))))
  1359. (and (eq (org-element-type e) 'paragraph)
  1360. (let ((o (car (org-element-contents e))))
  1361. (and (eq (org-element-type o) 'export-snippet)
  1362. (eq (org-export-snippet-backend o)
  1363. 'latex)))))))
  1364. "\\relax ")
  1365. (t " "))
  1366. (and contents (org-trim contents))
  1367. ;; If there are footnotes references in tag, be sure to
  1368. ;; add their definition at the end of the item. This
  1369. ;; workaround is necessary since "\footnote{}" command is
  1370. ;; not supported in tags.
  1371. (and tag
  1372. (org-latex--delayed-footnotes-definitions
  1373. (org-element-property :tag item) info)))))
  1374. ;;;; Keyword
  1375. (defun org-latex-keyword (keyword contents info)
  1376. "Transcode a KEYWORD element from Org to LaTeX.
  1377. CONTENTS is nil. INFO is a plist holding contextual information."
  1378. (let ((key (org-element-property :key keyword))
  1379. (value (org-element-property :value keyword)))
  1380. (cond
  1381. ((string= key "LATEX") value)
  1382. ((string= key "INDEX") (format "\\index{%s}" value))
  1383. ((string= key "TOC")
  1384. (let ((value (downcase value)))
  1385. (cond
  1386. ((string-match "\\<headlines\\>" value)
  1387. (let ((depth (or (and (string-match "[0-9]+" value)
  1388. (string-to-number (match-string 0 value)))
  1389. (plist-get info :with-toc))))
  1390. (concat
  1391. (when (wholenump depth)
  1392. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1393. "\\tableofcontents")))
  1394. ((string= "tables" value) "\\listoftables")
  1395. ((string= "listings" value)
  1396. (cond
  1397. ((eq org-latex-listings 'minted) "\\listoflistings")
  1398. (org-latex-listings "\\lstlistoflistings")
  1399. ;; At the moment, src blocks with a caption are wrapped
  1400. ;; into a figure environment.
  1401. (t "\\listoffigures")))))))))
  1402. ;;;; Latex Environment
  1403. (defun org-latex-latex-environment (latex-environment contents info)
  1404. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1405. CONTENTS is nil. INFO is a plist holding contextual information."
  1406. (when (plist-get info :with-latex)
  1407. (let ((label (org-element-property :name latex-environment))
  1408. (value (org-remove-indentation
  1409. (org-element-property :value latex-environment))))
  1410. (if (not (org-string-nw-p label)) value
  1411. ;; Environment is labeled: label must be within the environment
  1412. ;; (otherwise, a reference pointing to that element will count
  1413. ;; the section instead).
  1414. (with-temp-buffer
  1415. (insert value)
  1416. (goto-char (point-min))
  1417. (forward-line)
  1418. (insert
  1419. (format "\\label{%s}\n" (org-export-solidify-link-text label)))
  1420. (buffer-string))))))
  1421. ;;;; Latex Fragment
  1422. (defun org-latex-latex-fragment (latex-fragment contents info)
  1423. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1424. CONTENTS is nil. INFO is a plist holding contextual information."
  1425. (when (plist-get info :with-latex)
  1426. (org-element-property :value latex-fragment)))
  1427. ;;;; Line Break
  1428. (defun org-latex-line-break (line-break contents info)
  1429. "Transcode a LINE-BREAK object from Org to LaTeX.
  1430. CONTENTS is nil. INFO is a plist holding contextual information."
  1431. "\\\\\n")
  1432. ;;;; Link
  1433. (defun org-latex--inline-image (link info)
  1434. "Return LaTeX code for an inline image.
  1435. LINK is the link pointing to the inline image. INFO is a plist
  1436. used as a communication channel."
  1437. (let* ((parent (org-export-get-parent-element link))
  1438. (path (let ((raw-path (org-element-property :path link)))
  1439. (if (not (file-name-absolute-p raw-path)) raw-path
  1440. (expand-file-name raw-path))))
  1441. (filetype (file-name-extension path))
  1442. (caption (org-latex--caption/label-string parent info))
  1443. ;; Retrieve latex attributes from the element around.
  1444. (attr (org-export-read-attribute :attr_latex parent))
  1445. (float (let ((float (plist-get attr :float)))
  1446. (cond ((and (not float) (plist-member attr :float)) nil)
  1447. ((string= float "wrap") 'wrap)
  1448. ((string= float "multicolumn") 'multicolumn)
  1449. ((or float
  1450. (org-element-property :caption parent)
  1451. (org-string-nw-p (plist-get attr :caption)))
  1452. 'figure))))
  1453. (placement
  1454. (let ((place (plist-get attr :placement)))
  1455. (cond (place (format "%s" place))
  1456. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  1457. ((eq float 'figure)
  1458. (format "[%s]" org-latex-default-figure-position))
  1459. (t ""))))
  1460. (comment-include (if (plist-get attr :comment-include) "%" ""))
  1461. ;; It is possible to specify width and height in the
  1462. ;; ATTR_LATEX line, and also via default variables.
  1463. (width (cond ((plist-get attr :width))
  1464. ((plist-get attr :height) "")
  1465. ((eq float 'wrap) "0.48\\textwidth")
  1466. (t org-latex-image-default-width)))
  1467. (height (cond ((plist-get attr :height))
  1468. ((or (plist-get attr :width)
  1469. (memq float '(figure wrap))) "")
  1470. (t org-latex-image-default-height)))
  1471. (options (let ((opt (or (plist-get attr :options)
  1472. org-latex-image-default-option)))
  1473. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  1474. (match-string 1 opt))))
  1475. image-code)
  1476. (if (member filetype '("tikz" "pgf"))
  1477. ;; For tikz images:
  1478. ;; - use \input to read in image file.
  1479. ;; - if options are present, wrap in a tikzpicture environment.
  1480. ;; - if width or height are present, use \resizebox to change
  1481. ;; the image size.
  1482. (progn
  1483. (setq image-code (format "\\input{%s}" path))
  1484. (when (org-string-nw-p options)
  1485. (setq image-code
  1486. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  1487. options
  1488. image-code)))
  1489. (when (or (org-string-nw-p width) (org-string-nw-p height))
  1490. (setq image-code (format "\\resizebox{%s}{%s}{%s}"
  1491. (if (org-string-nw-p width) width "!")
  1492. (if (org-string-nw-p height) height "!")
  1493. image-code))))
  1494. ;; For other images:
  1495. ;; - add width and height to options.
  1496. ;; - include the image with \includegraphics.
  1497. (when (org-string-nw-p width)
  1498. (setq options (concat options ",width=" width)))
  1499. (when (org-string-nw-p height)
  1500. (setq options (concat options ",height=" height)))
  1501. (setq image-code
  1502. (format "\\includegraphics%s{%s}"
  1503. (cond ((not (org-string-nw-p options)) "")
  1504. ((= (aref options 0) ?,)
  1505. (format "[%s]"(substring options 1)))
  1506. (t (format "[%s]" options)))
  1507. path))
  1508. (when (equal filetype "svg")
  1509. (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
  1510. "\\includesvg"
  1511. image-code
  1512. nil t))
  1513. (setq image-code (replace-regexp-in-string "\\.svg}"
  1514. "}"
  1515. image-code
  1516. nil t))))
  1517. ;; Return proper string, depending on FLOAT.
  1518. (case float
  1519. (wrap (format "\\begin{wrapfigure}%s
  1520. \\centering
  1521. %s%s
  1522. %s\\end{wrapfigure}" placement comment-include image-code caption))
  1523. (multicolumn (format "\\begin{figure*}%s
  1524. \\centering
  1525. %s%s
  1526. %s\\end{figure*}" placement comment-include image-code caption))
  1527. (figure (format "\\begin{figure}%s
  1528. \\centering
  1529. %s%s
  1530. %s\\end{figure}" placement comment-include image-code caption))
  1531. (otherwise image-code))))
  1532. (defun org-latex-link (link desc info)
  1533. "Transcode a LINK object from Org to LaTeX.
  1534. DESC is the description part of the link, or the empty string.
  1535. INFO is a plist holding contextual information. See
  1536. `org-export-data'."
  1537. (let* ((type (org-element-property :type link))
  1538. (raw-path (replace-regexp-in-string
  1539. "%" "\\%" (org-element-property :path link) nil t))
  1540. ;; Ensure DESC really exists, or set it to nil.
  1541. (desc (and (not (string= desc "")) desc))
  1542. (imagep (org-export-inline-image-p
  1543. link org-latex-inline-image-rules))
  1544. (path (cond
  1545. ((member type '("http" "https" "ftp" "mailto"))
  1546. (concat type ":" raw-path))
  1547. ((and (string= type "file") (file-name-absolute-p raw-path))
  1548. (concat "file:" raw-path))
  1549. (t raw-path)))
  1550. protocol)
  1551. (cond
  1552. ;; Image file.
  1553. (imagep (org-latex--inline-image link info))
  1554. ;; Radio link: Transcode target's contents and use them as link's
  1555. ;; description.
  1556. ((string= type "radio")
  1557. (let ((destination (org-export-resolve-radio-link link info)))
  1558. (if (not destination) desc
  1559. (format "\\hyperref[%s]{%s}"
  1560. (org-export-solidify-link-text
  1561. (org-element-property :value destination))
  1562. desc))))
  1563. ;; Links pointing to a headline: Find destination and build
  1564. ;; appropriate referencing command.
  1565. ((member type '("custom-id" "fuzzy" "id"))
  1566. (let ((destination (if (string= type "fuzzy")
  1567. (org-export-resolve-fuzzy-link link info)
  1568. (org-export-resolve-id-link link info))))
  1569. (case (org-element-type destination)
  1570. ;; Id link points to an external file.
  1571. (plain-text
  1572. (if desc (format "\\href{%s}{%s}" destination desc)
  1573. (format "\\url{%s}" destination)))
  1574. ;; Fuzzy link points nowhere.
  1575. ('nil
  1576. (format org-latex-link-with-unknown-path-format
  1577. (or desc
  1578. (org-export-data
  1579. (org-element-property :raw-link link) info))))
  1580. ;; LINK points to a headline. If headlines are numbered
  1581. ;; and the link has no description, display headline's
  1582. ;; number. Otherwise, display description or headline's
  1583. ;; title.
  1584. (headline
  1585. (let ((label
  1586. (format "sec-%s"
  1587. (mapconcat
  1588. 'number-to-string
  1589. (org-export-get-headline-number destination info)
  1590. "-"))))
  1591. (if (and (plist-get info :section-numbers) (not desc))
  1592. (format "\\ref{%s}" label)
  1593. (format "\\hyperref[%s]{%s}" label
  1594. (or desc
  1595. (org-export-data
  1596. (org-element-property :title destination) info))))))
  1597. ;; Fuzzy link points to a target. Do as above.
  1598. (otherwise
  1599. (let ((path (org-export-solidify-link-text path)))
  1600. (if (not desc) (format "\\ref{%s}" path)
  1601. (format "\\hyperref[%s]{%s}" path desc)))))))
  1602. ;; Coderef: replace link with the reference name or the
  1603. ;; equivalent line number.
  1604. ((string= type "coderef")
  1605. (format (org-export-get-coderef-format path desc)
  1606. (org-export-resolve-coderef path info)))
  1607. ;; Link type is handled by a special function.
  1608. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1609. (funcall protocol (org-link-unescape path) desc 'latex))
  1610. ;; External link with a description part.
  1611. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1612. ;; External link without a description part.
  1613. (path (format "\\url{%s}" path))
  1614. ;; No path, only description. Try to do something useful.
  1615. (t (format org-latex-link-with-unknown-path-format desc)))))
  1616. ;;;; Paragraph
  1617. (defun org-latex-paragraph (paragraph contents info)
  1618. "Transcode a PARAGRAPH element from Org to LaTeX.
  1619. CONTENTS is the contents of the paragraph, as a string. INFO is
  1620. the plist used as a communication channel."
  1621. contents)
  1622. ;;;; Plain List
  1623. (defun org-latex-plain-list (plain-list contents info)
  1624. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1625. CONTENTS is the contents of the list. INFO is a plist holding
  1626. contextual information."
  1627. (let* ((type (org-element-property :type plain-list))
  1628. (attr (org-export-read-attribute :attr_latex plain-list))
  1629. (latex-type (let ((env (plist-get attr :environment)))
  1630. (cond (env (format "%s" env))
  1631. ((eq type 'ordered) "enumerate")
  1632. ((eq type 'descriptive) "description")
  1633. (t "itemize")))))
  1634. (org-latex--wrap-label
  1635. plain-list
  1636. (format "\\begin{%s}%s\n%s\\end{%s}"
  1637. latex-type
  1638. (or (plist-get attr :options) "")
  1639. contents
  1640. latex-type))))
  1641. ;;;; Plain Text
  1642. (defun org-latex-plain-text (text info)
  1643. "Transcode a TEXT string from Org to LaTeX.
  1644. TEXT is the string to transcode. INFO is a plist holding
  1645. contextual information."
  1646. (let ((specialp (plist-get info :with-special-strings))
  1647. (output text))
  1648. ;; Protect %, #, &, $, _, { and }.
  1649. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
  1650. (setq output
  1651. (replace-match
  1652. (format "\\%s" (match-string 2 output)) nil t output 2)))
  1653. ;; Protect ^.
  1654. (setq output
  1655. (replace-regexp-in-string
  1656. "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
  1657. ;; Protect \. If special strings are used, be careful not to
  1658. ;; protect "\" in "\-" constructs.
  1659. (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
  1660. (setq output
  1661. (replace-regexp-in-string
  1662. (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
  1663. "$\\backslash$" output nil t 1)))
  1664. ;; Protect ~.
  1665. (setq output
  1666. (replace-regexp-in-string
  1667. "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
  1668. ;; Activate smart quotes. Be sure to provide original TEXT string
  1669. ;; since OUTPUT may have been modified.
  1670. (when (plist-get info :with-smart-quotes)
  1671. (setq output (org-export-activate-smart-quotes output :latex info text)))
  1672. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1673. (let ((case-fold-search nil)
  1674. (start 0))
  1675. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
  1676. (setq output (replace-match
  1677. (format "\\%s{}" (match-string 1 output)) nil t output)
  1678. start (match-end 0))))
  1679. ;; Convert special strings.
  1680. (when specialp
  1681. (setq output
  1682. (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
  1683. ;; Handle break preservation if required.
  1684. (when (plist-get info :preserve-breaks)
  1685. (setq output (replace-regexp-in-string
  1686. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
  1687. ;; Return value.
  1688. output))
  1689. ;;;; Planning
  1690. (defun org-latex-planning (planning contents info)
  1691. "Transcode a PLANNING element from Org to LaTeX.
  1692. CONTENTS is nil. INFO is a plist holding contextual
  1693. information."
  1694. (concat
  1695. "\\noindent"
  1696. (mapconcat
  1697. 'identity
  1698. (delq nil
  1699. (list
  1700. (let ((closed (org-element-property :closed planning)))
  1701. (when closed
  1702. (concat
  1703. (format "\\textbf{%s} " org-closed-string)
  1704. (format org-latex-inactive-timestamp-format
  1705. (org-translate-time
  1706. (org-element-property :raw-value closed))))))
  1707. (let ((deadline (org-element-property :deadline planning)))
  1708. (when deadline
  1709. (concat
  1710. (format "\\textbf{%s} " org-deadline-string)
  1711. (format org-latex-active-timestamp-format
  1712. (org-translate-time
  1713. (org-element-property :raw-value deadline))))))
  1714. (let ((scheduled (org-element-property :scheduled planning)))
  1715. (when scheduled
  1716. (concat
  1717. (format "\\textbf{%s} " org-scheduled-string)
  1718. (format org-latex-active-timestamp-format
  1719. (org-translate-time
  1720. (org-element-property :raw-value scheduled))))))))
  1721. " ")
  1722. "\\\\"))
  1723. ;;;; Quote Block
  1724. (defun org-latex-quote-block (quote-block contents info)
  1725. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1726. CONTENTS holds the contents of the block. INFO is a plist
  1727. holding contextual information."
  1728. (org-latex--wrap-label
  1729. quote-block
  1730. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1731. ;;;; Quote Section
  1732. (defun org-latex-quote-section (quote-section contents info)
  1733. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1734. CONTENTS is nil. INFO is a plist holding contextual information."
  1735. (let ((value (org-remove-indentation
  1736. (org-element-property :value quote-section))))
  1737. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1738. ;;;; Radio Target
  1739. (defun org-latex-radio-target (radio-target text info)
  1740. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1741. TEXT is the text of the target. INFO is a plist holding
  1742. contextual information."
  1743. (format "\\label{%s}%s"
  1744. (org-export-solidify-link-text
  1745. (org-element-property :value radio-target))
  1746. text))
  1747. ;;;; Section
  1748. (defun org-latex-section (section contents info)
  1749. "Transcode a SECTION element from Org to LaTeX.
  1750. CONTENTS holds the contents of the section. INFO is a plist
  1751. holding contextual information."
  1752. contents)
  1753. ;;;; Special Block
  1754. (defun org-latex-special-block (special-block contents info)
  1755. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1756. CONTENTS holds the contents of the block. INFO is a plist
  1757. holding contextual information."
  1758. (let ((type (downcase (org-element-property :type special-block)))
  1759. (opt (org-export-read-attribute :attr_latex special-block :options)))
  1760. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  1761. ;; Insert any label or caption within the block
  1762. ;; (otherwise, a reference pointing to that element will
  1763. ;; count the section instead).
  1764. (org-latex--caption/label-string special-block info)
  1765. contents
  1766. (format "\\end{%s}" type))))
  1767. ;;;; Src Block
  1768. (defun org-latex-src-block (src-block contents info)
  1769. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1770. CONTENTS holds the contents of the item. INFO is a plist holding
  1771. contextual information."
  1772. (when (org-string-nw-p (org-element-property :value src-block))
  1773. (let* ((lang (org-element-property :language src-block))
  1774. (caption (org-element-property :caption src-block))
  1775. (label (org-element-property :name src-block))
  1776. (custom-env (and lang
  1777. (cadr (assq (intern lang)
  1778. org-latex-custom-lang-environments))))
  1779. (num-start (case (org-element-property :number-lines src-block)
  1780. (continued (org-export-get-loc src-block info))
  1781. (new 0)))
  1782. (retain-labels (org-element-property :retain-labels src-block))
  1783. (attributes (org-export-read-attribute :attr_latex src-block))
  1784. (float (plist-get attributes :float)))
  1785. (cond
  1786. ;; Case 1. No source fontification.
  1787. ((not org-latex-listings)
  1788. (let* ((caption-str (org-latex--caption/label-string src-block info))
  1789. (float-env
  1790. (cond ((and (not float) (plist-member attributes :float)) "%s")
  1791. ((string= "multicolumn" float)
  1792. (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
  1793. org-latex-default-figure-position
  1794. caption-str))
  1795. ((or caption float)
  1796. (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
  1797. caption-str))
  1798. (t "%s"))))
  1799. (format
  1800. float-env
  1801. (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1802. (org-export-format-code-default src-block info))))))
  1803. ;; Case 2. Custom environment.
  1804. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1805. custom-env
  1806. (org-export-format-code-default src-block info)
  1807. custom-env))
  1808. ;; Case 3. Use minted package.
  1809. ((eq org-latex-listings 'minted)
  1810. (let* ((caption-str (org-latex--caption/label-string src-block info))
  1811. (float-env
  1812. (cond ((and (not float) (plist-member attributes :float)) "%s")
  1813. ((string= "multicolumn" float)
  1814. (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
  1815. caption-str))
  1816. ((or caption float)
  1817. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1818. caption-str))
  1819. (t "%s")))
  1820. (body
  1821. (format
  1822. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1823. ;; Options.
  1824. (org-latex--make-option-string
  1825. (if (or (not num-start)
  1826. (assoc "linenos" org-latex-minted-options))
  1827. org-latex-minted-options
  1828. (append
  1829. `(("linenos")
  1830. ("firstnumber" ,(number-to-string (1+ num-start))))
  1831. org-latex-minted-options)))
  1832. ;; Language.
  1833. (or (cadr (assq (intern lang) org-latex-minted-langs))
  1834. (downcase lang))
  1835. ;; Source code.
  1836. (let* ((code-info (org-export-unravel-code src-block))
  1837. (max-width
  1838. (apply 'max
  1839. (mapcar 'length
  1840. (org-split-string (car code-info)
  1841. "\n")))))
  1842. (org-export-format-code
  1843. (car code-info)
  1844. (lambda (loc num ref)
  1845. (concat
  1846. loc
  1847. (when ref
  1848. ;; Ensure references are flushed to the right,
  1849. ;; separated with 6 spaces from the widest line
  1850. ;; of code.
  1851. (concat (make-string (+ (- max-width (length loc)) 6)
  1852. ?\s)
  1853. (format "(%s)" ref)))))
  1854. nil (and retain-labels (cdr code-info)))))))
  1855. ;; Return value.
  1856. (format float-env body)))
  1857. ;; Case 4. Use listings package.
  1858. (t
  1859. (let ((lst-lang
  1860. (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
  1861. (caption-str
  1862. (when caption
  1863. (let ((main (org-export-get-caption src-block))
  1864. (secondary (org-export-get-caption src-block t)))
  1865. (if (not secondary)
  1866. (format "{%s}" (org-export-data main info))
  1867. (format "{[%s]%s}"
  1868. (org-export-data secondary info)
  1869. (org-export-data main info)))))))
  1870. (concat
  1871. ;; Options.
  1872. (format
  1873. "\\lstset{%s}\n"
  1874. (org-latex--make-option-string
  1875. (append
  1876. org-latex-listings-options
  1877. (cond
  1878. ((and (not float) (plist-member attributes :float)) nil)
  1879. ((string= "multicolumn" float) '(("float" "*")))
  1880. ((and float (not (assoc "float" org-latex-listings-options)))
  1881. `(("float" ,org-latex-default-figure-position))))
  1882. `(("language" ,lst-lang))
  1883. (if label `(("label" ,label)) '(("label" " ")))
  1884. (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
  1885. (cond ((assoc "numbers" org-latex-listings-options) nil)
  1886. ((not num-start) '(("numbers" "none")))
  1887. ((zerop num-start) '(("numbers" "left")))
  1888. (t `(("numbers" "left")
  1889. ("firstnumber"
  1890. ,(number-to-string (1+ num-start)))))))))
  1891. ;; Source code.
  1892. (format
  1893. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1894. (let* ((code-info (org-export-unravel-code src-block))
  1895. (max-width
  1896. (apply 'max
  1897. (mapcar 'length
  1898. (org-split-string (car code-info) "\n")))))
  1899. (org-export-format-code
  1900. (car code-info)
  1901. (lambda (loc num ref)
  1902. (concat
  1903. loc
  1904. (when ref
  1905. ;; Ensure references are flushed to the right,
  1906. ;; separated with 6 spaces from the widest line of
  1907. ;; code
  1908. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1909. (format "(%s)" ref)))))
  1910. nil (and retain-labels (cdr code-info))))))))))))
  1911. ;;;; Statistics Cookie
  1912. (defun org-latex-statistics-cookie (statistics-cookie contents info)
  1913. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1914. CONTENTS is nil. INFO is a plist holding contextual information."
  1915. (replace-regexp-in-string
  1916. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  1917. ;;;; Strike-Through
  1918. (defun org-latex-strike-through (strike-through contents info)
  1919. "Transcode STRIKE-THROUGH from Org to LaTeX.
  1920. CONTENTS is the text with strike-through markup. INFO is a plist
  1921. holding contextual information."
  1922. (org-latex--text-markup contents 'strike-through))
  1923. ;;;; Subscript
  1924. (defun org-latex--script-size (object info)
  1925. "Transcode a subscript or superscript object.
  1926. OBJECT is an Org object. INFO is a plist used as a communication
  1927. channel."
  1928. (let ((in-script-p
  1929. ;; Non-nil if object is already in a sub/superscript.
  1930. (let ((parent object))
  1931. (catch 'exit
  1932. (while (setq parent (org-export-get-parent parent))
  1933. (let ((type (org-element-type parent)))
  1934. (cond ((memq type '(subscript superscript))
  1935. (throw 'exit t))
  1936. ((memq type org-element-all-elements)
  1937. (throw 'exit nil))))))))
  1938. (type (org-element-type object))
  1939. (output ""))
  1940. (org-element-map (org-element-contents object)
  1941. (cons 'plain-text org-element-all-objects)
  1942. (lambda (obj)
  1943. (case (org-element-type obj)
  1944. ((entity latex-fragment)
  1945. (let ((data (org-trim (org-export-data obj info))))
  1946. (string-match
  1947. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  1948. data)
  1949. (setq output
  1950. (concat output
  1951. (match-string 1 data)
  1952. (let ((blank (org-element-property :post-blank obj)))
  1953. (and blank (> blank 0) "\\ "))))))
  1954. (plain-text
  1955. (setq output
  1956. (format "%s\\text{%s}" output (org-export-data obj info))))
  1957. (otherwise
  1958. (setq output
  1959. (concat output
  1960. (org-export-data obj info)
  1961. (let ((blank (org-element-property :post-blank obj)))
  1962. (and blank (> blank 0) "\\ ")))))))
  1963. info nil org-element-recursive-objects)
  1964. ;; Result. Do not wrap into math mode if already in a subscript
  1965. ;; or superscript. Do not wrap into curly brackets if OUTPUT is
  1966. ;; a single character. Also merge consecutive subscript and
  1967. ;; superscript into the same math snippet.
  1968. (concat (and (not in-script-p)
  1969. (let ((prev (org-export-get-previous-element object info)))
  1970. (or (not prev)
  1971. (not (eq (org-element-type prev)
  1972. (if (eq type 'subscript) 'superscript
  1973. 'subscript)))
  1974. (let ((blank (org-element-property :post-blank prev)))
  1975. (and blank (> blank 0)))))
  1976. "$")
  1977. (if (eq (org-element-type object) 'subscript) "_" "^")
  1978. (and (> (length output) 1) "{")
  1979. output
  1980. (and (> (length output) 1) "}")
  1981. (and (not in-script-p)
  1982. (or (let ((blank (org-element-property :post-blank object)))
  1983. (and blank (> blank 0)))
  1984. (not (eq (org-element-type
  1985. (org-export-get-next-element object info))
  1986. (if (eq type 'subscript) 'superscript
  1987. 'subscript))))
  1988. "$"))))
  1989. (defun org-latex-subscript (subscript contents info)
  1990. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1991. CONTENTS is the contents of the object. INFO is a plist holding
  1992. contextual information."
  1993. (org-latex--script-size subscript info))
  1994. ;;;; Superscript
  1995. (defun org-latex-superscript (superscript contents info)
  1996. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1997. CONTENTS is the contents of the object. INFO is a plist holding
  1998. contextual information."
  1999. (org-latex--script-size superscript info))
  2000. ;;;; Table
  2001. ;;
  2002. ;; `org-latex-table' is the entry point for table transcoding. It
  2003. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  2004. ;; delegates the job to either `org-latex--table.el-table',
  2005. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  2006. ;; depending of the type of the table and the mode requested.
  2007. ;;
  2008. ;; `org-latex--align-string' is a subroutine used to build alignment
  2009. ;; string for Org tables.
  2010. (defun org-latex-table (table contents info)
  2011. "Transcode a TABLE element from Org to LaTeX.
  2012. CONTENTS is the contents of the table. INFO is a plist holding
  2013. contextual information."
  2014. (if (eq (org-element-property :type table) 'table.el)
  2015. ;; "table.el" table. Convert it using appropriate tools.
  2016. (org-latex--table.el-table table info)
  2017. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  2018. org-latex-default-table-mode)))
  2019. (cond
  2020. ;; Case 1: Verbatim table.
  2021. ((string= type "verbatim")
  2022. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  2023. ;; Re-create table, without affiliated keywords.
  2024. (org-trim (org-element-interpret-data
  2025. `(table nil ,@(org-element-contents table))))))
  2026. ;; Case 2: Matrix.
  2027. ((or (string= type "math") (string= type "inline-math"))
  2028. (org-latex--math-table table info))
  2029. ;; Case 3: Standard table.
  2030. (t (concat (org-latex--org-table table contents info)
  2031. ;; When there are footnote references within the
  2032. ;; table, insert their definition just after it.
  2033. (org-latex--delayed-footnotes-definitions table info)))))))
  2034. (defun org-latex--align-string (table info)
  2035. "Return an appropriate LaTeX alignment string.
  2036. TABLE is the considered table. INFO is a plist used as
  2037. a communication channel."
  2038. (or (org-export-read-attribute :attr_latex table :align)
  2039. (let (align)
  2040. ;; Extract column groups and alignment from first (non-rule)
  2041. ;; row.
  2042. (org-element-map
  2043. (org-element-map table 'table-row
  2044. (lambda (row)
  2045. (and (eq (org-element-property :type row) 'standard) row))
  2046. info 'first-match)
  2047. 'table-cell
  2048. (lambda (cell)
  2049. (let ((borders (org-export-table-cell-borders cell info)))
  2050. ;; Check left border for the first cell only.
  2051. (when (and (memq 'left borders) (not align))
  2052. (push "|" align))
  2053. (push (case (org-export-table-cell-alignment cell info)
  2054. (left "l")
  2055. (right "r")
  2056. (center "c"))
  2057. align)
  2058. (when (memq 'right borders) (push "|" align))))
  2059. info)
  2060. (apply 'concat (nreverse align)))))
  2061. (defun org-latex--org-table (table contents info)
  2062. "Return appropriate LaTeX code for an Org table.
  2063. TABLE is the table type element to transcode. CONTENTS is its
  2064. contents, as a string. INFO is a plist used as a communication
  2065. channel.
  2066. This function assumes TABLE has `org' as its `:type' property and
  2067. `table' as its `:mode' attribute."
  2068. (let* ((caption (org-latex--caption/label-string table info))
  2069. (attr (org-export-read-attribute :attr_latex table))
  2070. ;; Determine alignment string.
  2071. (alignment (org-latex--align-string table info))
  2072. ;; Determine environment for the table: longtable, tabular...
  2073. (table-env (or (plist-get attr :environment)
  2074. org-latex-default-table-environment))
  2075. ;; If table is a float, determine environment: table, table*
  2076. ;; or sidewaystable.
  2077. (float-env (unless (member table-env '("longtable" "longtabu"))
  2078. (let ((float (plist-get attr :float)))
  2079. (cond
  2080. ((and (not float) (plist-member attr :float)) nil)
  2081. ((string= float "sidewaystable") "sidewaystable")
  2082. ((string= float "multicolumn") "table*")
  2083. ((or float
  2084. (org-element-property :caption table)
  2085. (org-string-nw-p (plist-get attr :caption)))
  2086. "table")))))
  2087. ;; Extract others display options.
  2088. (fontsize (let ((font (plist-get attr :font)))
  2089. (and font (concat font "\n"))))
  2090. (width (plist-get attr :width))
  2091. (spreadp (plist-get attr :spread))
  2092. (placement (or (plist-get attr :placement)
  2093. (format "[%s]" org-latex-default-figure-position)))
  2094. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2095. org-latex-tables-centered)))
  2096. ;; Prepare the final format string for the table.
  2097. (cond
  2098. ;; Longtable.
  2099. ((equal "longtable" table-env)
  2100. (concat (and fontsize (concat "{" fontsize))
  2101. (format "\\begin{longtable}{%s}\n" alignment)
  2102. (and org-latex-table-caption-above
  2103. (org-string-nw-p caption)
  2104. (concat caption "\\\\\n"))
  2105. contents
  2106. (and (not org-latex-table-caption-above)
  2107. (org-string-nw-p caption)
  2108. (concat caption "\\\\\n"))
  2109. "\\end{longtable}\n"
  2110. (and fontsize "}")))
  2111. ;; Longtabu
  2112. ((equal "longtabu" table-env)
  2113. (concat (and fontsize (concat "{" fontsize))
  2114. (format "\\begin{longtabu}%s{%s}\n"
  2115. (if width
  2116. (format " %s %s "
  2117. (if spreadp "spread" "to") width) "")
  2118. alignment)
  2119. (and org-latex-table-caption-above
  2120. (org-string-nw-p caption)
  2121. (concat caption "\\\\\n"))
  2122. contents
  2123. (and (not org-latex-table-caption-above)
  2124. (org-string-nw-p caption)
  2125. (concat caption "\\\\\n"))
  2126. "\\end{longtabu}\n"
  2127. (and fontsize "}")))
  2128. ;; Others.
  2129. (t (concat (cond
  2130. (float-env
  2131. (concat (format "\\begin{%s}%s\n" float-env placement)
  2132. (if org-latex-table-caption-above caption "")
  2133. (when centerp "\\centering\n")
  2134. fontsize))
  2135. (centerp (concat "\\begin{center}\n" fontsize))
  2136. (fontsize (concat "{" fontsize)))
  2137. (cond ((equal "tabu" table-env)
  2138. (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
  2139. (if width (format
  2140. (if spreadp " spread %s " " to %s ")
  2141. width) "")
  2142. alignment
  2143. contents))
  2144. (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2145. table-env
  2146. (if width (format "{%s}" width) "")
  2147. alignment
  2148. contents
  2149. table-env)))
  2150. (cond
  2151. (float-env
  2152. (concat (if org-latex-table-caption-above "" caption)
  2153. (format "\n\\end{%s}" float-env)))
  2154. (centerp "\n\\end{center}")
  2155. (fontsize "}")))))))
  2156. (defun org-latex--table.el-table (table info)
  2157. "Return appropriate LaTeX code for a table.el table.
  2158. TABLE is the table type element to transcode. INFO is a plist
  2159. used as a communication channel.
  2160. This function assumes TABLE has `table.el' as its `:type'
  2161. property."
  2162. (require 'table)
  2163. ;; Ensure "*org-export-table*" buffer is empty.
  2164. (with-current-buffer (get-buffer-create "*org-export-table*")
  2165. (erase-buffer))
  2166. (let ((output (with-temp-buffer
  2167. (insert (org-element-property :value table))
  2168. (goto-char 1)
  2169. (re-search-forward "^[ \t]*|[^|]" nil t)
  2170. (table-generate-source 'latex "*org-export-table*")
  2171. (with-current-buffer "*org-export-table*"
  2172. (org-trim (buffer-string))))))
  2173. (kill-buffer (get-buffer "*org-export-table*"))
  2174. ;; Remove left out comments.
  2175. (while (string-match "^%.*\n" output)
  2176. (setq output (replace-match "" t t output)))
  2177. (let ((attr (org-export-read-attribute :attr_latex table)))
  2178. (when (plist-get attr :rmlines)
  2179. ;; When the "rmlines" attribute is provided, remove all hlines
  2180. ;; but the the one separating heading from the table body.
  2181. (let ((n 0) (pos 0))
  2182. (while (and (< (length output) pos)
  2183. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2184. (incf n)
  2185. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2186. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2187. org-latex-tables-centered)))
  2188. (if (not centerp) output
  2189. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2190. (defun org-latex--math-table (table info)
  2191. "Return appropriate LaTeX code for a matrix.
  2192. TABLE is the table type element to transcode. INFO is a plist
  2193. used as a communication channel.
  2194. This function assumes TABLE has `org' as its `:type' property and
  2195. `inline-math' or `math' as its `:mode' attribute.."
  2196. (let* ((caption (org-latex--caption/label-string table info))
  2197. (attr (org-export-read-attribute :attr_latex table))
  2198. (inlinep (equal (plist-get attr :mode) "inline-math"))
  2199. (env (or (plist-get attr :environment)
  2200. org-latex-default-table-environment))
  2201. (contents
  2202. (mapconcat
  2203. (lambda (row)
  2204. ;; Ignore horizontal rules.
  2205. (when (eq (org-element-property :type row) 'standard)
  2206. ;; Return each cell unmodified.
  2207. (concat
  2208. (mapconcat
  2209. (lambda (cell)
  2210. (substring (org-element-interpret-data cell) 0 -1))
  2211. (org-element-map row 'table-cell 'identity info) "&")
  2212. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2213. "\n")))
  2214. (org-element-map table 'table-row 'identity info) ""))
  2215. ;; Variables related to math clusters (contiguous math tables
  2216. ;; of the same type).
  2217. (mode (org-export-read-attribute :attr_latex table :mode))
  2218. (prev (org-export-get-previous-element table info))
  2219. (next (org-export-get-next-element table info))
  2220. (same-mode-p
  2221. (lambda (table)
  2222. ;; Non-nil when TABLE has the same mode as current table.
  2223. (string= (or (org-export-read-attribute :attr_latex table :mode)
  2224. org-latex-default-table-mode)
  2225. mode))))
  2226. (concat
  2227. ;; Opening string. If TABLE is in the middle of a table cluster,
  2228. ;; do not insert any.
  2229. (cond ((and prev
  2230. (eq (org-element-type prev) 'table)
  2231. (memq (org-element-property :post-blank prev) '(0 nil))
  2232. (funcall same-mode-p prev))
  2233. nil)
  2234. (inlinep "\\(")
  2235. ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
  2236. (t "\\["))
  2237. ;; Prefix.
  2238. (or (plist-get attr :math-prefix) "")
  2239. ;; Environment. Also treat special cases.
  2240. (cond ((equal env "array")
  2241. (let ((align (org-latex--align-string table info)))
  2242. (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
  2243. ((assoc env org-latex-table-matrix-macros)
  2244. (format "\\%s%s{\n%s}"
  2245. env
  2246. (or (plist-get attr :math-arguments) "")
  2247. contents))
  2248. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2249. ;; Suffix.
  2250. (or (plist-get attr :math-suffix) "")
  2251. ;; Closing string. If TABLE is in the middle of a table cluster,
  2252. ;; do not insert any. If it closes such a cluster, be sure to
  2253. ;; close the cluster with a string matching the opening string.
  2254. (cond ((and next
  2255. (eq (org-element-type next) 'table)
  2256. (memq (org-element-property :post-blank table) '(0 nil))
  2257. (funcall same-mode-p next))
  2258. nil)
  2259. (inlinep "\\)")
  2260. ;; Find cluster beginning to know which environment to use.
  2261. ((let ((cluster-beg table) prev)
  2262. (while (and (setq prev (org-export-get-previous-element
  2263. cluster-beg info))
  2264. (memq (org-element-property :post-blank prev)
  2265. '(0 nil))
  2266. (funcall same-mode-p prev))
  2267. (setq cluster-beg prev))
  2268. (and (or (org-element-property :caption cluster-beg)
  2269. (org-element-property :name cluster-beg))
  2270. "\n\\end{equation}")))
  2271. (t "\\]")))))
  2272. ;;;; Table Cell
  2273. (defun org-latex-table-cell (table-cell contents info)
  2274. "Transcode a TABLE-CELL element from Org to LaTeX.
  2275. CONTENTS is the cell contents. INFO is a plist used as
  2276. a communication channel."
  2277. (concat (if (and contents
  2278. org-latex-table-scientific-notation
  2279. (string-match orgtbl-exp-regexp contents))
  2280. ;; Use appropriate format string for scientific
  2281. ;; notation.
  2282. (format org-latex-table-scientific-notation
  2283. (match-string 1 contents)
  2284. (match-string 2 contents))
  2285. contents)
  2286. (when (org-export-get-next-element table-cell info) " & ")))
  2287. ;;;; Table Row
  2288. (defun org-latex-table-row (table-row contents info)
  2289. "Transcode a TABLE-ROW element from Org to LaTeX.
  2290. CONTENTS is the contents of the row. INFO is a plist used as
  2291. a communication channel."
  2292. ;; Rules are ignored since table separators are deduced from
  2293. ;; borders of the current row.
  2294. (when (eq (org-element-property :type table-row) 'standard)
  2295. (let* ((attr (org-export-read-attribute :attr_latex
  2296. (org-export-get-parent table-row)))
  2297. (longtablep (member (or (plist-get attr :environment)
  2298. org-latex-default-table-environment)
  2299. '("longtable" "longtabu")))
  2300. (booktabsp (if (plist-member attr :booktabs)
  2301. (plist-get attr :booktabs)
  2302. org-latex-tables-booktabs))
  2303. ;; TABLE-ROW's borders are extracted from its first cell.
  2304. (borders (org-export-table-cell-borders
  2305. (car (org-element-contents table-row)) info)))
  2306. (concat
  2307. ;; When BOOKTABS are activated enforce top-rule even when no
  2308. ;; hline was specifically marked.
  2309. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2310. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  2311. contents "\\\\\n"
  2312. (cond
  2313. ;; Special case for long tables. Define header and footers.
  2314. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2315. (format "%s
  2316. \\endhead
  2317. %s\\multicolumn{%d}{r}{Continued on next page} \\\\
  2318. \\endfoot
  2319. \\endlastfoot"
  2320. (if booktabsp "\\midrule" "\\hline")
  2321. (if booktabsp "\\midrule" "\\hline")
  2322. ;; Number of columns.
  2323. (cdr (org-export-table-dimensions
  2324. (org-export-get-parent-table table-row) info))))
  2325. ;; When BOOKTABS are activated enforce bottom rule even when
  2326. ;; no hline was specifically marked.
  2327. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  2328. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  2329. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  2330. ;;;; Target
  2331. (defun org-latex-target (target contents info)
  2332. "Transcode a TARGET object from Org to LaTeX.
  2333. CONTENTS is nil. INFO is a plist holding contextual
  2334. information."
  2335. (format "\\label{%s}"
  2336. (org-export-solidify-link-text (org-element-property :value target))))
  2337. ;;;; Timestamp
  2338. (defun org-latex-timestamp (timestamp contents info)
  2339. "Transcode a TIMESTAMP object from Org to LaTeX.
  2340. CONTENTS is nil. INFO is a plist holding contextual
  2341. information."
  2342. (let ((value (org-latex-plain-text
  2343. (org-timestamp-translate timestamp) info)))
  2344. (case (org-element-property :type timestamp)
  2345. ((active active-range) (format org-latex-active-timestamp-format value))
  2346. ((inactive inactive-range)
  2347. (format org-latex-inactive-timestamp-format value))
  2348. (otherwise (format org-latex-diary-timestamp-format value)))))
  2349. ;;;; Underline
  2350. (defun org-latex-underline (underline contents info)
  2351. "Transcode UNDERLINE from Org to LaTeX.
  2352. CONTENTS is the text with underline markup. INFO is a plist
  2353. holding contextual information."
  2354. (org-latex--text-markup contents 'underline))
  2355. ;;;; Verbatim
  2356. (defun org-latex-verbatim (verbatim contents info)
  2357. "Transcode a VERBATIM object from Org to LaTeX.
  2358. CONTENTS is nil. INFO is a plist used as a communication
  2359. channel."
  2360. (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  2361. ;;;; Verse Block
  2362. (defun org-latex-verse-block (verse-block contents info)
  2363. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2364. CONTENTS is verse block contents. INFO is a plist holding
  2365. contextual information."
  2366. (org-latex--wrap-label
  2367. verse-block
  2368. ;; In a verse environment, add a line break to each newline
  2369. ;; character and change each white space at beginning of a line
  2370. ;; into a space of 1 em. Also change each blank line with
  2371. ;; a vertical space of 1 em.
  2372. (progn
  2373. (setq contents (replace-regexp-in-string
  2374. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  2375. (replace-regexp-in-string
  2376. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  2377. (while (string-match "^[ \t]+" contents)
  2378. (let ((new-str (format "\\hspace*{%dem}"
  2379. (length (match-string 0 contents)))))
  2380. (setq contents (replace-match new-str nil t contents))))
  2381. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  2382. ;;; End-user functions
  2383. ;;;###autoload
  2384. (defun org-latex-export-as-latex
  2385. (&optional async subtreep visible-only body-only ext-plist)
  2386. "Export current buffer as a LaTeX buffer.
  2387. If narrowing is active in the current buffer, only export its
  2388. narrowed part.
  2389. If a region is active, export that region.
  2390. A non-nil optional argument ASYNC means the process should happen
  2391. asynchronously. The resulting buffer should be accessible
  2392. through the `org-export-stack' interface.
  2393. When optional argument SUBTREEP is non-nil, export the sub-tree
  2394. at point, extracting information from the headline properties
  2395. first.
  2396. When optional argument VISIBLE-ONLY is non-nil, don't export
  2397. contents of hidden elements.
  2398. When optional argument BODY-ONLY is non-nil, only write code
  2399. between \"\\begin{document}\" and \"\\end{document}\".
  2400. EXT-PLIST, when provided, is a property list with external
  2401. parameters overriding Org default settings, but still inferior to
  2402. file-local settings.
  2403. Export is done in a buffer named \"*Org LATEX Export*\", which
  2404. will be displayed when `org-export-show-temporary-export-buffer'
  2405. is non-nil."
  2406. (interactive)
  2407. (org-export-to-buffer 'latex "*Org LATEX Export*"
  2408. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  2409. ;;;###autoload
  2410. (defun org-latex-convert-region-to-latex ()
  2411. "Assume the current region has org-mode syntax, and convert it to LaTeX.
  2412. This can be used in any buffer. For example, you can write an
  2413. itemized list in org-mode syntax in an LaTeX buffer and use this
  2414. command to convert it."
  2415. (interactive)
  2416. (org-export-replace-region-by 'latex))
  2417. ;;;###autoload
  2418. (defun org-latex-export-to-latex
  2419. (&optional async subtreep visible-only body-only ext-plist)
  2420. "Export current buffer to a LaTeX file.
  2421. If narrowing is active in the current buffer, only export its
  2422. narrowed part.
  2423. If a region is active, export that region.
  2424. A non-nil optional argument ASYNC means the process should happen
  2425. asynchronously. The resulting file should be accessible through
  2426. the `org-export-stack' interface.
  2427. When optional argument SUBTREEP is non-nil, export the sub-tree
  2428. at point, extracting information from the headline properties
  2429. first.
  2430. When optional argument VISIBLE-ONLY is non-nil, don't export
  2431. contents of hidden elements.
  2432. When optional argument BODY-ONLY is non-nil, only write code
  2433. between \"\\begin{document}\" and \"\\end{document}\".
  2434. EXT-PLIST, when provided, is a property list with external
  2435. parameters overriding Org default settings, but still inferior to
  2436. file-local settings."
  2437. (interactive)
  2438. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2439. (org-export-to-file 'latex outfile
  2440. async subtreep visible-only body-only ext-plist)))
  2441. ;;;###autoload
  2442. (defun org-latex-export-to-pdf
  2443. (&optional async subtreep visible-only body-only ext-plist)
  2444. "Export current buffer to LaTeX then process through to PDF.
  2445. If narrowing is active in the current buffer, only export its
  2446. narrowed part.
  2447. If a region is active, export that region.
  2448. A non-nil optional argument ASYNC means the process should happen
  2449. asynchronously. The resulting file should be accessible through
  2450. the `org-export-stack' interface.
  2451. When optional argument SUBTREEP is non-nil, export the sub-tree
  2452. at point, extracting information from the headline properties
  2453. first.
  2454. When optional argument VISIBLE-ONLY is non-nil, don't export
  2455. contents of hidden elements.
  2456. When optional argument BODY-ONLY is non-nil, only write code
  2457. between \"\\begin{document}\" and \"\\end{document}\".
  2458. EXT-PLIST, when provided, is a property list with external
  2459. parameters overriding Org default settings, but still inferior to
  2460. file-local settings.
  2461. Return PDF file's name."
  2462. (interactive)
  2463. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2464. (org-export-to-file 'latex outfile
  2465. async subtreep visible-only body-only ext-plist
  2466. (lambda (file) (org-latex-compile file)))))
  2467. (defun org-latex-compile (texfile &optional snippet)
  2468. "Compile a TeX file.
  2469. TEXFILE is the name of the file being compiled. Processing is
  2470. done through the command specified in `org-latex-pdf-process'.
  2471. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  2472. file used to preview a LaTeX snippet. In this case, do not
  2473. create a log buffer and do not bother removing log files.
  2474. Return PDF file name or an error if it couldn't be produced."
  2475. (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
  2476. (full-name (file-truename texfile))
  2477. (out-dir (file-name-directory texfile))
  2478. ;; Properly set working directory for compilation.
  2479. (default-directory (if (file-name-absolute-p texfile)
  2480. (file-name-directory full-name)
  2481. default-directory))
  2482. errors)
  2483. (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
  2484. (save-window-excursion
  2485. (cond
  2486. ;; A function is provided: Apply it.
  2487. ((functionp org-latex-pdf-process)
  2488. (funcall org-latex-pdf-process (shell-quote-argument texfile)))
  2489. ;; A list is provided: Replace %b, %f and %o with appropriate
  2490. ;; values in each command before applying it. Output is
  2491. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2492. ((consp org-latex-pdf-process)
  2493. (let ((outbuf (and (not snippet)
  2494. (get-buffer-create "*Org PDF LaTeX Output*"))))
  2495. (mapc
  2496. (lambda (command)
  2497. (shell-command
  2498. (replace-regexp-in-string
  2499. "%b" (shell-quote-argument base-name)
  2500. (replace-regexp-in-string
  2501. "%f" (shell-quote-argument full-name)
  2502. (replace-regexp-in-string
  2503. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2504. outbuf))
  2505. org-latex-pdf-process)
  2506. ;; Collect standard errors from output buffer.
  2507. (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
  2508. (t (error "No valid command to process to PDF")))
  2509. (let ((pdffile (concat out-dir base-name ".pdf")))
  2510. ;; Check for process failure. Provide collected errors if
  2511. ;; possible.
  2512. (if (not (file-exists-p pdffile))
  2513. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2514. (when errors (concat ": " errors))))
  2515. ;; Else remove log files, when specified, and signal end of
  2516. ;; process to user, along with any error encountered.
  2517. (when (and (not snippet) org-latex-remove-logfiles)
  2518. (dolist (file (directory-files
  2519. out-dir t
  2520. (concat (regexp-quote base-name)
  2521. "\\(?:\\.[0-9]+\\)?"
  2522. "\\."
  2523. (regexp-opt org-latex-logfiles-extensions))))
  2524. (delete-file file)))
  2525. (message (concat "Process completed"
  2526. (if (not errors) "."
  2527. (concat " with errors: " errors)))))
  2528. ;; Return output file name.
  2529. pdffile))))
  2530. (defun org-latex--collect-errors (buffer)
  2531. "Collect some kind of errors from \"pdflatex\" command output.
  2532. BUFFER is the buffer containing output.
  2533. Return collected error types as a string, or nil if there was
  2534. none."
  2535. (with-current-buffer buffer
  2536. (save-excursion
  2537. (goto-char (point-max))
  2538. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  2539. (let ((case-fold-search t)
  2540. (errors ""))
  2541. (dolist (latex-error org-latex-known-errors)
  2542. (when (save-excursion (re-search-forward (car latex-error) nil t))
  2543. (setq errors (concat errors " " (cdr latex-error)))))
  2544. (and (org-string-nw-p errors) (org-trim errors)))))))
  2545. ;;;###autoload
  2546. (defun org-latex-publish-to-latex (plist filename pub-dir)
  2547. "Publish an Org file to LaTeX.
  2548. FILENAME is the filename of the Org file to be published. PLIST
  2549. is the property list for the given project. PUB-DIR is the
  2550. publishing directory.
  2551. Return output file name."
  2552. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  2553. ;;;###autoload
  2554. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  2555. "Publish an Org file to PDF (via LaTeX).
  2556. FILENAME is the filename of the Org file to be published. PLIST
  2557. is the property list for the given project. PUB-DIR is the
  2558. publishing directory.
  2559. Return output file name."
  2560. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  2561. ;; in working directory and then moved to publishing directory.
  2562. (org-publish-attachment
  2563. plist
  2564. (org-latex-compile
  2565. (org-publish-org-to
  2566. 'latex filename ".tex" plist (file-name-directory filename)))
  2567. pub-dir))
  2568. (provide 'ox-latex)
  2569. ;; Local variables:
  2570. ;; generated-autoload-file: "org-loaddefs.el"
  2571. ;; End:
  2572. ;;; ox-latex.el ends here