ox-latex.el 104 KB

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