ox-latex.el 103 KB

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