ox-latex.el 108 KB

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