ox-latex.el 103 KB

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