ox-latex.el 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989
  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",
  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. ;; - `:booktabs', `:center' and `:rmlines' values are booleans. They
  59. ;; toggle, respectively "booktabs" usage (assuming the package is
  60. ;; properly loaded), table centering and removal of every horizontal
  61. ;; rule but the first one (in a "table.el" table only).
  62. ;;
  63. ;; - `:math-prefix', `:math-suffix' and `:math-arguments' are string
  64. ;; which will be inserted, respectively, before the table within the
  65. ;; math environment, after the table within the math environment,
  66. ;; and between the macro name and the contents of the table. The
  67. ;; latter attribute is necessary to matrix macros that require more
  68. ;; than one argument (i.e. "qbordermatrix").
  69. ;;
  70. ;; Plain lists accept two optional attributes: `:environment' and
  71. ;; `:options'. The first one allows to use a non-standard environment
  72. ;; (i.e. "inparaenum"). The second one allows to specify optional
  73. ;; arguments for that environment (square brackets are not mandatory).
  74. ;;
  75. ;; Images accept `:float', `:placement', `:comment-include', `:width',
  76. ;; and `:height', and `:options' as attributes. `:float' accepts
  77. ;; a symbol among `wrap', `multicolumn', and `figure', which defines
  78. ;; the float environment for the image (if unspecified, an image with
  79. ;; a caption will be set in a "figure" environment).
  80. ;; `:comment-include' is a boolean that toggles whether to comment out
  81. ;; the code which actually includes the image. `:placement' is
  82. ;; a string that will be used as argument for the environment chosen.
  83. ;; `:width' and `:height' control the width and height of the image.
  84. ;; `:options' is a string that will be used as the optional argument
  85. ;; for "includegraphics" macro or, in the case of tikz images, used as
  86. ;; the optional argument for a `tikzpicture' environment which will
  87. ;; surround the "\input" picture code.
  88. ;;
  89. ;; Special blocks accept `:options' as attribute. Its value will be
  90. ;; appended as-is to the opening string of the environment created.
  91. ;;
  92. ;; This back-end also offers enhanced support for footnotes. Thus, it
  93. ;; handles nested footnotes, footnotes in tables and footnotes in item
  94. ;; descriptions.
  95. ;;
  96. ;; Smart quotes are activated by default.
  97. ;;; Code:
  98. (eval-when-compile (require 'cl))
  99. (require 'ox)
  100. (require 'ox-publish)
  101. (defvar org-latex-default-packages-alist)
  102. (defvar org-latex-packages-alist)
  103. (defvar orgtbl-exp-regexp)
  104. ;;; Define Back-End
  105. (org-export-define-backend latex
  106. ((bold . org-latex-bold)
  107. (center-block . org-latex-center-block)
  108. (clock . org-latex-clock)
  109. (code . org-latex-code)
  110. (comment . (lambda (&rest args) ""))
  111. (comment-block . (lambda (&rest args) ""))
  112. (drawer . org-latex-drawer)
  113. (dynamic-block . org-latex-dynamic-block)
  114. (entity . org-latex-entity)
  115. (example-block . org-latex-example-block)
  116. (export-block . org-latex-export-block)
  117. (export-snippet . org-latex-export-snippet)
  118. (fixed-width . org-latex-fixed-width)
  119. (footnote-definition . org-latex-footnote-definition)
  120. (footnote-reference . org-latex-footnote-reference)
  121. (headline . org-latex-headline)
  122. (horizontal-rule . org-latex-horizontal-rule)
  123. (inline-src-block . org-latex-inline-src-block)
  124. (inlinetask . org-latex-inlinetask)
  125. (italic . org-latex-italic)
  126. (item . org-latex-item)
  127. (keyword . org-latex-keyword)
  128. (latex-environment . org-latex-latex-environment)
  129. (latex-fragment . org-latex-latex-fragment)
  130. (line-break . org-latex-line-break)
  131. (link . org-latex-link)
  132. (paragraph . org-latex-paragraph)
  133. (plain-list . org-latex-plain-list)
  134. (plain-text . org-latex-plain-text)
  135. (planning . org-latex-planning)
  136. (property-drawer . (lambda (&rest args) ""))
  137. (quote-block . org-latex-quote-block)
  138. (quote-section . org-latex-quote-section)
  139. (radio-target . org-latex-radio-target)
  140. (section . org-latex-section)
  141. (special-block . org-latex-special-block)
  142. (src-block . org-latex-src-block)
  143. (statistics-cookie . org-latex-statistics-cookie)
  144. (strike-through . org-latex-strike-through)
  145. (subscript . org-latex-subscript)
  146. (superscript . org-latex-superscript)
  147. (table . org-latex-table)
  148. (table-cell . org-latex-table-cell)
  149. (table-row . org-latex-table-row)
  150. (target . org-latex-target)
  151. (template . org-latex-template)
  152. (timestamp . org-latex-timestamp)
  153. (underline . org-latex-underline)
  154. (verbatim . org-latex-verbatim)
  155. (verse-block . org-latex-verse-block))
  156. :export-block ("LATEX" "TEX")
  157. :menu-entry
  158. (?l "Export to LaTeX"
  159. ((?L "As LaTeX buffer" org-latex-export-as-latex)
  160. (?l "As LaTeX file" org-latex-export-to-latex)
  161. (?p "As PDF file" org-latex-export-to-pdf)
  162. (?o "As PDF file and open"
  163. (lambda (a s v b)
  164. (if a (org-latex-export-to-pdf t s v b)
  165. (org-open-file (org-latex-export-to-pdf nil s v b)))))))
  166. :options-alist ((:date-format nil nil org-latex-date-timestamp-format)
  167. (:latex-class "LATEX_CLASS" nil org-latex-default-class t)
  168. (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
  169. (:latex-header "LATEX_HEADER" nil nil newline)
  170. (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
  171. (:latex-hyperref-p nil "texht" org-latex-with-hyperref t)
  172. ;; Redefine regular options.
  173. (:date "DATE" nil "\\today" t)
  174. (:with-smart-quotes nil "'" t)))
  175. ;;; Internal Variables
  176. (defconst org-latex-babel-language-alist
  177. '(("af" . "afrikaans")
  178. ("bg" . "bulgarian")
  179. ("bt-br" . "brazilian")
  180. ("ca" . "catalan")
  181. ("cs" . "czech")
  182. ("cy" . "welsh")
  183. ("da" . "danish")
  184. ("de" . "germanb")
  185. ("de-at" . "naustrian")
  186. ("de-de" . "ngerman")
  187. ("el" . "greek")
  188. ("en" . "english")
  189. ("en-au" . "australian")
  190. ("en-ca" . "canadian")
  191. ("en-gb" . "british")
  192. ("en-ie" . "irish")
  193. ("en-nz" . "newzealand")
  194. ("en-us" . "american")
  195. ("es" . "spanish")
  196. ("et" . "estonian")
  197. ("eu" . "basque")
  198. ("fi" . "finnish")
  199. ("fr" . "frenchb")
  200. ("fr-ca" . "canadien")
  201. ("gl" . "galician")
  202. ("hr" . "croatian")
  203. ("hu" . "hungarian")
  204. ("id" . "indonesian")
  205. ("is" . "icelandic")
  206. ("it" . "italian")
  207. ("la" . "latin")
  208. ("ms" . "malay")
  209. ("nl" . "dutch")
  210. ("no-no" . "nynorsk")
  211. ("pl" . "polish")
  212. ("pt" . "portuguese")
  213. ("ro" . "romanian")
  214. ("ru" . "russian")
  215. ("sa" . "sanskrit")
  216. ("sb" . "uppersorbian")
  217. ("sk" . "slovak")
  218. ("sl" . "slovene")
  219. ("sq" . "albanian")
  220. ("sr" . "serbian")
  221. ("sv" . "swedish")
  222. ("ta" . "tamil")
  223. ("tr" . "turkish")
  224. ("uk" . "ukrainian"))
  225. "Alist between language code and corresponding Babel option.")
  226. (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
  227. ("qbordermatrix" . "\\cr")
  228. ("kbordermatrix" . "\\\\"))
  229. "Alist between matrix macros and their row ending.")
  230. ;;; User Configurable Variables
  231. (defgroup org-export-latex nil
  232. "Options for exporting Org mode files to LaTeX."
  233. :tag "Org Export LaTeX"
  234. :group 'org-export)
  235. ;;;; Preamble
  236. (defcustom org-latex-default-class "article"
  237. "The default LaTeX class."
  238. :group 'org-export-latex
  239. :type '(string :tag "LaTeX class"))
  240. (defcustom org-latex-classes
  241. '(("article"
  242. "\\documentclass[11pt]{article}"
  243. ("\\section{%s}" . "\\section*{%s}")
  244. ("\\subsection{%s}" . "\\subsection*{%s}")
  245. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  246. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  247. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  248. ("report"
  249. "\\documentclass[11pt]{report}"
  250. ("\\part{%s}" . "\\part*{%s}")
  251. ("\\chapter{%s}" . "\\chapter*{%s}")
  252. ("\\section{%s}" . "\\section*{%s}")
  253. ("\\subsection{%s}" . "\\subsection*{%s}")
  254. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  255. ("book"
  256. "\\documentclass[11pt]{book}"
  257. ("\\part{%s}" . "\\part*{%s}")
  258. ("\\chapter{%s}" . "\\chapter*{%s}")
  259. ("\\section{%s}" . "\\section*{%s}")
  260. ("\\subsection{%s}" . "\\subsection*{%s}")
  261. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  262. "Alist of LaTeX classes and associated header and structure.
  263. If #+LaTeX_CLASS is set in the buffer, use its value and the
  264. associated information. Here is the structure of each cell:
  265. \(class-name
  266. header-string
  267. \(numbered-section . unnumbered-section\)
  268. ...\)
  269. The header string
  270. -----------------
  271. The HEADER-STRING is the header that will be inserted into the
  272. LaTeX file. It should contain the \\documentclass macro, and
  273. anything else that is needed for this setup. To this header, the
  274. following commands will be added:
  275. - Calls to \\usepackage for all packages mentioned in the
  276. variables `org-latex-default-packages-alist' and
  277. `org-latex-packages-alist'. Thus, your header definitions
  278. should avoid to also request these packages.
  279. - Lines specified via \"#+LaTeX_HEADER:\"
  280. If you need more control about the sequence in which the header
  281. is built up, or if you want to exclude one of these building
  282. blocks for a particular class, you can use the following
  283. macro-like placeholders.
  284. [DEFAULT-PACKAGES] \\usepackage statements for default packages
  285. [NO-DEFAULT-PACKAGES] do not include any of the default packages
  286. [PACKAGES] \\usepackage statements for packages
  287. [NO-PACKAGES] do not include the packages
  288. [EXTRA] the stuff from #+LaTeX_HEADER
  289. [NO-EXTRA] do not include #+LaTeX_HEADER stuff
  290. So a header like
  291. \\documentclass{article}
  292. [NO-DEFAULT-PACKAGES]
  293. [EXTRA]
  294. \\providecommand{\\alert}[1]{\\textbf{#1}}
  295. [PACKAGES]
  296. will omit the default packages, and will include the
  297. #+LaTeX_HEADER lines, then have a call to \\providecommand, and
  298. then place \\usepackage commands based on the content of
  299. `org-latex-packages-alist'.
  300. If your header, `org-latex-default-packages-alist' or
  301. `org-latex-packages-alist' inserts
  302. \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
  303. replaced with a coding system derived from
  304. `buffer-file-coding-system'. See also the variable
  305. `org-latex-inputenc-alist' for a way to influence this mechanism.
  306. The sectioning structure
  307. ------------------------
  308. The sectioning structure of the class is given by the elements
  309. following the header string. For each sectioning level, a number
  310. of strings is specified. A %s formatter is mandatory in each
  311. section string and will be replaced by the title of the section.
  312. Instead of a cons cell \(numbered . unnumbered\), you can also
  313. provide a list of 2 or 4 elements,
  314. \(numbered-open numbered-close\)
  315. or
  316. \(numbered-open numbered-close unnumbered-open unnumbered-close\)
  317. providing opening and closing strings for a LaTeX environment
  318. that should represent the document section. The opening clause
  319. should have a %s to represent the section title.
  320. Instead of a list of sectioning commands, you can also specify
  321. a function name. That function will be called with two
  322. parameters, the \(reduced) level of the headline, and a predicate
  323. non-nil when the headline should be numbered. It must return
  324. a format string in which the section title will be added."
  325. :group 'org-export-latex
  326. :type '(repeat
  327. (list (string :tag "LaTeX class")
  328. (string :tag "LaTeX header")
  329. (repeat :tag "Levels" :inline t
  330. (choice
  331. (cons :tag "Heading"
  332. (string :tag " numbered")
  333. (string :tag "unnumbered"))
  334. (list :tag "Environment"
  335. (string :tag "Opening (numbered)")
  336. (string :tag "Closing (numbered)")
  337. (string :tag "Opening (unnumbered)")
  338. (string :tag "Closing (unnumbered)"))
  339. (function :tag "Hook computing sectioning"))))))
  340. (defcustom org-latex-inputenc-alist nil
  341. "Alist of inputenc coding system names, and what should really be used.
  342. For example, adding an entry
  343. (\"utf8\" . \"utf8x\")
  344. will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
  345. are written as utf8 files."
  346. :group 'org-export-latex
  347. :type '(repeat
  348. (cons
  349. (string :tag "Derived from buffer")
  350. (string :tag "Use this instead"))))
  351. (defcustom org-latex-date-timestamp-format nil
  352. "Time-stamp format string to use for DATE keyword.
  353. The format string, when specified, only applies if date consists
  354. in a single time-stamp. Otherwise its value will be ignored.
  355. See `format-time-string' for details on how to build this
  356. string."
  357. :group 'org-export-latex
  358. :type '(choice
  359. (string :tag "Time-stamp format string")
  360. (const :tag "No format string" nil)))
  361. (defcustom org-latex-title-command "\\maketitle"
  362. "The command used to insert the title just after \\begin{document}.
  363. If this string contains the formatting specification \"%s\" then
  364. it will be used as a formatting string, passing the title as an
  365. argument."
  366. :group 'org-export-latex
  367. :type 'string)
  368. (defcustom org-latex-toc-command "\\tableofcontents\n\n"
  369. "LaTeX command to set the table of contents, list of figures, etc.
  370. This command only applies to the table of contents generated with
  371. the toc:nil option, not to those generated with #+TOC keyword."
  372. :group 'org-export-latex
  373. :type 'string)
  374. (defcustom org-latex-with-hyperref t
  375. "Toggle insertion of \\hypersetup{...} in the preamble."
  376. :group 'org-export-latex
  377. :type 'boolean)
  378. ;;;; Headline
  379. (defcustom org-latex-format-headline-function
  380. 'org-latex-format-headline-default-function
  381. "Function for formatting the headline's text.
  382. This function will be called with 5 arguments:
  383. TODO the todo keyword (string or nil).
  384. TODO-TYPE the type of todo (symbol: `todo', `done', nil)
  385. PRIORITY the priority of the headline (integer or nil)
  386. TEXT the main headline text (string).
  387. TAGS the tags as a list of strings (list of strings or nil).
  388. The function result will be used in the section format string.
  389. Use `org-latex-format-headline-default-function' by default,
  390. which format headlines like for Org version prior to 8.0."
  391. :group 'org-export-latex
  392. :version "24.4"
  393. :package-version '(Org . "8.0")
  394. :type 'function)
  395. ;;;; Footnotes
  396. (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
  397. "Text used to separate footnotes."
  398. :group 'org-export-latex
  399. :type 'string)
  400. ;;;; Timestamps
  401. (defcustom org-latex-active-timestamp-format "\\textit{%s}"
  402. "A printf format string to be applied to active timestamps."
  403. :group 'org-export-latex
  404. :type 'string)
  405. (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
  406. "A printf format string to be applied to inactive timestamps."
  407. :group 'org-export-latex
  408. :type 'string)
  409. (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
  410. "A printf format string to be applied to diary timestamps."
  411. :group 'org-export-latex
  412. :type 'string)
  413. ;;;; Links
  414. (defcustom org-latex-image-default-option ""
  415. "Default option for images."
  416. :group 'org-export-latex
  417. :version "24.4"
  418. :package-version '(Org . "8.0")
  419. :type 'string)
  420. (defcustom org-latex-image-default-width ".9\\linewidth"
  421. "Default width for images.
  422. This value will not be used if a height is provided."
  423. :group 'org-export-latex
  424. :version "24.4"
  425. :package-version '(Org . "8.0")
  426. :type 'string)
  427. (defcustom org-latex-image-default-height ""
  428. "Default height for images.
  429. This value will not be used if a width is provided, or if the
  430. image is wrapped within a \"figure\" or \"wrapfigure\"
  431. environment."
  432. :group 'org-export-latex
  433. :version "24.4"
  434. :package-version '(Org . "8.0")
  435. :type 'string)
  436. (defcustom org-latex-default-figure-position "htb"
  437. "Default position for latex figures."
  438. :group 'org-export-latex
  439. :type 'string)
  440. (defcustom org-latex-inline-image-rules
  441. '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\)\\'"))
  442. "Rules characterizing image files that can be inlined into LaTeX.
  443. A rule consists in an association whose key is the type of link
  444. to consider, and value is a regexp that will be matched against
  445. link's path.
  446. Note that, by default, the image extension *actually* allowed
  447. depend on the way the LaTeX file is processed. When used with
  448. pdflatex, pdf, jpg and png images are OK. When processing
  449. through dvi to Postscript, only ps and eps are allowed. The
  450. default we use here encompasses both."
  451. :group 'org-export-latex
  452. :version "24.4"
  453. :package-version '(Org . "8.0")
  454. :type '(alist :key-type (string :tag "Type")
  455. :value-type (regexp :tag "Path")))
  456. (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
  457. "Format string for links with unknown path type."
  458. :group 'org-export-latex
  459. :type 'string)
  460. ;;;; Tables
  461. (defcustom org-latex-default-table-environment "tabular"
  462. "Default environment used to build tables."
  463. :group 'org-export-latex
  464. :version "24.4"
  465. :package-version '(Org . "8.0")
  466. :type 'string)
  467. (defcustom org-latex-default-table-mode 'table
  468. "Default mode for tables.
  469. Value can be a symbol among:
  470. `table' Regular LaTeX table.
  471. `math' In this mode, every cell is considered as being in math
  472. mode and the complete table will be wrapped within a math
  473. environment. It is particularly useful to write matrices.
  474. `inline-math' This mode is almost the same as `math', but the
  475. math environment will be inlined.
  476. `verbatim' The table is exported as it appears in the Org
  477. buffer, within a verbatim environment.
  478. This value can be overridden locally with, i.e. \":mode math\" in
  479. LaTeX attributes.
  480. When modifying this variable, it may be useful to change
  481. `org-latex-default-table-environment' accordingly."
  482. :group 'org-export-latex
  483. :version "24.4"
  484. :package-version '(Org . "8.0")
  485. :type '(choice (const :tag "Table" table)
  486. (const :tag "Matrix" math)
  487. (const :tag "Inline matrix" inline-math)
  488. (const :tag "Verbatim" verbatim)))
  489. (defcustom org-latex-tables-centered t
  490. "When non-nil, tables are exported in a center environment."
  491. :group 'org-export-latex
  492. :type 'boolean)
  493. (defcustom org-latex-tables-booktabs nil
  494. "When non-nil, display tables in a formal \"booktabs\" style.
  495. This option assumes that the \"booktabs\" package is properly
  496. loaded in the header of the document. This value can be ignored
  497. locally with \":booktabs t\" and \":booktabs nil\" LaTeX
  498. attributes."
  499. :group 'org-export-latex
  500. :version "24.4"
  501. :package-version '(Org . "8.0")
  502. :type 'boolean)
  503. (defcustom org-latex-table-caption-above t
  504. "When non-nil, place caption string at the beginning of the table.
  505. Otherwise, place it near the end."
  506. :group 'org-export-latex
  507. :type 'boolean)
  508. (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
  509. "Format string to display numbers in scientific notation.
  510. The format should have \"%s\" twice, for mantissa and exponent
  511. \(i.e., \"%s\\\\times10^{%s}\").
  512. When nil, no transformation is made."
  513. :group 'org-export-latex
  514. :version "24.4"
  515. :package-version '(Org . "8.0")
  516. :type '(choice
  517. (string :tag "Format string")
  518. (const :tag "No formatting")))
  519. ;;;; Text markup
  520. (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
  521. (code . verb)
  522. (italic . "\\emph{%s}")
  523. (strike-through . "\\st{%s}")
  524. (underline . "\\underline{%s}")
  525. (verbatim . protectedtexttt))
  526. "Alist of LaTeX expressions to convert text markup.
  527. The key must be a symbol among `bold', `code', `italic',
  528. `strike-through', `underline' and `verbatim'. The value is
  529. a formatting string to wrap fontified text with.
  530. Value can also be set to the following symbols: `verb' and
  531. `protectedtexttt'. For the former, Org will use \"\\verb\" to
  532. create a format string and select a delimiter character that
  533. isn't in the string. For the latter, Org will use \"\\texttt\"
  534. to typeset and try to protect special characters.
  535. If no association can be found for a given markup, text will be
  536. returned as-is."
  537. :group 'org-export-latex
  538. :type 'alist
  539. :options '(bold code italic strike-through underline verbatim))
  540. ;;;; Drawers
  541. (defcustom org-latex-format-drawer-function nil
  542. "Function called to format a drawer in LaTeX code.
  543. The function must accept two parameters:
  544. NAME the drawer name, like \"LOGBOOK\"
  545. CONTENTS the contents of the drawer.
  546. The function should return the string to be exported.
  547. For example, the variable could be set to the following function
  548. in order to mimic default behaviour:
  549. \(defun org-latex-format-drawer-default \(name contents\)
  550. \"Format a drawer element for LaTeX export.\"
  551. contents\)"
  552. :group 'org-export-latex
  553. :type 'function)
  554. ;;;; Inlinetasks
  555. (defcustom org-latex-format-inlinetask-function nil
  556. "Function called to format an inlinetask in LaTeX code.
  557. The function must accept six parameters:
  558. TODO the todo keyword, as a string
  559. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  560. PRIORITY the inlinetask priority, as a string
  561. NAME the inlinetask name, as a string.
  562. TAGS the inlinetask tags, as a list of strings.
  563. CONTENTS the contents of the inlinetask, as a string.
  564. The function should return the string to be exported.
  565. For example, the variable could be set to the following function
  566. in order to mimic default behaviour:
  567. \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
  568. \"Format an inline task element for LaTeX export.\"
  569. \(let ((full-title
  570. \(concat
  571. \(when todo
  572. \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
  573. \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
  574. title
  575. \(when tags
  576. \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
  577. \(mapconcat 'identity tags \":\")))))
  578. \(format (concat \"\\\\begin{center}\\n\"
  579. \"\\\\fbox{\\n\"
  580. \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
  581. \"%s\\n\\n\"
  582. \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
  583. \"%s\"
  584. \"\\\\end{minipage}}\"
  585. \"\\\\end{center}\")
  586. full-title contents))"
  587. :group 'org-export-latex
  588. :type 'function)
  589. ;; Src blocks
  590. (defcustom org-latex-listings nil
  591. "Non-nil means export source code using the listings package.
  592. This package will fontify source code, possibly even with color.
  593. If you want to use this, you also need to make LaTeX use the
  594. listings package, and if you want to have color, the color
  595. package. Just add these to `org-latex-packages-alist', for
  596. example using customize, or with something like:
  597. \(require 'ox-latex)
  598. \(add-to-list 'org-latex-packages-alist '\(\"\" \"listings\"))
  599. \(add-to-list 'org-latex-packages-alist '\(\"\" \"color\"))
  600. Alternatively,
  601. \(setq org-latex-listings 'minted)
  602. causes source code to be exported using the minted package as
  603. opposed to listings. If you want to use minted, you need to add
  604. the minted package to `org-latex-packages-alist', for example
  605. using customize, or with
  606. \(require 'ox-latex)
  607. \(add-to-list 'org-latex-packages-alist '\(\"\" \"minted\"))
  608. In addition, it is necessary to install pygments
  609. \(http://pygments.org), and to configure the variable
  610. `org-latex-pdf-process' so that the -shell-escape option is
  611. passed to pdflatex."
  612. :group 'org-export-latex
  613. :type '(choice
  614. (const :tag "Use listings" t)
  615. (const :tag "Use minted" 'minted)
  616. (const :tag "Export verbatim" nil)))
  617. (defcustom org-latex-listings-langs
  618. '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
  619. (c "C") (cc "C++")
  620. (fortran "fortran")
  621. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  622. (html "HTML") (xml "XML")
  623. (tex "TeX") (latex "TeX")
  624. (shell-script "bash")
  625. (gnuplot "Gnuplot")
  626. (ocaml "Caml") (caml "Caml")
  627. (sql "SQL") (sqlite "sql"))
  628. "Alist mapping languages to their listing language counterpart.
  629. The key is a symbol, the major mode symbol without the \"-mode\".
  630. The value is the string that should be inserted as the language
  631. parameter for the listings package. If the mode name and the
  632. listings name are the same, the language does not need an entry
  633. in this list - but it does not hurt if it is present."
  634. :group 'org-export-latex
  635. :type '(repeat
  636. (list
  637. (symbol :tag "Major mode ")
  638. (string :tag "Listings language"))))
  639. (defcustom org-latex-listings-options nil
  640. "Association list of options for the latex listings package.
  641. These options are supplied as a comma-separated list to the
  642. \\lstset command. Each element of the association list should be
  643. a list containing two strings: the name of the option, and the
  644. value. For example,
  645. (setq org-latex-listings-options
  646. '((\"basicstyle\" \"\\small\")
  647. (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
  648. will typeset the code in a small size font with underlined, bold
  649. black keywords.
  650. Note that the same options will be applied to blocks of all
  651. languages."
  652. :group 'org-export-latex
  653. :type '(repeat
  654. (list
  655. (string :tag "Listings option name ")
  656. (string :tag "Listings option value"))))
  657. (defcustom org-latex-minted-langs
  658. '((emacs-lisp "common-lisp")
  659. (cc "c++")
  660. (cperl "perl")
  661. (shell-script "bash")
  662. (caml "ocaml"))
  663. "Alist mapping languages to their minted language counterpart.
  664. The key is a symbol, the major mode symbol without the \"-mode\".
  665. The value is the string that should be inserted as the language
  666. parameter for the minted package. If the mode name and the
  667. listings name are the same, the language does not need an entry
  668. in this list - but it does not hurt if it is present.
  669. Note that minted uses all lower case for language identifiers,
  670. and that the full list of language identifiers can be obtained
  671. with:
  672. pygmentize -L lexers"
  673. :group 'org-export-latex
  674. :type '(repeat
  675. (list
  676. (symbol :tag "Major mode ")
  677. (string :tag "Minted language"))))
  678. (defcustom org-latex-minted-options nil
  679. "Association list of options for the latex minted package.
  680. These options are supplied within square brackets in
  681. \\begin{minted} environments. Each element of the alist should
  682. be a list containing two strings: the name of the option, and the
  683. value. For example,
  684. \(setq org-latex-minted-options
  685. '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
  686. will result in src blocks being exported with
  687. \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
  688. as the start of the minted environment. Note that the same
  689. options will be applied to blocks of all languages."
  690. :group 'org-export-latex
  691. :type '(repeat
  692. (list
  693. (string :tag "Minted option name ")
  694. (string :tag "Minted option value"))))
  695. (defvar org-latex-custom-lang-environments nil
  696. "Alist mapping languages to language-specific LaTeX environments.
  697. It is used during export of src blocks by the listings and minted
  698. latex packages. For example,
  699. \(setq org-latex-custom-lang-environments
  700. '\(\(python \"pythoncode\"\)\)\)
  701. would have the effect that if org encounters begin_src python
  702. during latex export it will output
  703. \\begin{pythoncode}
  704. <src block body>
  705. \\end{pythoncode}")
  706. ;;;; Compilation
  707. (defcustom org-latex-pdf-process
  708. '("pdflatex -interaction nonstopmode -output-directory %o %f"
  709. "pdflatex -interaction nonstopmode -output-directory %o %f"
  710. "pdflatex -interaction nonstopmode -output-directory %o %f")
  711. "Commands to process a LaTeX file to a PDF file.
  712. This is a list of strings, each of them will be given to the
  713. shell as a command. %f in the command will be replaced by the
  714. full file name, %b by the file base name (i.e. without directory
  715. and extension parts) and %o by the base directory of the file.
  716. The reason why this is a list is that it usually takes several
  717. runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
  718. does not have a clever mechanism to detect which of these
  719. commands have to be run to get to a stable result, and it also
  720. does not do any error checking.
  721. By default, Org uses 3 runs of `pdflatex' to do the processing.
  722. If you have texi2dvi on your system and if that does not cause
  723. the infamous egrep/locale bug:
  724. http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
  725. then `texi2dvi' is the superior choice. Org does offer it as one
  726. of the customize options.
  727. Alternatively, this may be a Lisp function that does the
  728. processing, so you could use this to apply the machinery of
  729. AUCTeX or the Emacs LaTeX mode. This function should accept the
  730. file name as its single argument."
  731. :group 'org-export-pdf
  732. :type '(choice
  733. (repeat :tag "Shell command sequence"
  734. (string :tag "Shell command"))
  735. (const :tag "2 runs of pdflatex"
  736. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  737. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  738. (const :tag "3 runs of pdflatex"
  739. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  740. "pdflatex -interaction nonstopmode -output-directory %o %f"
  741. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  742. (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
  743. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  744. "bibtex %b"
  745. "pdflatex -interaction nonstopmode -output-directory %o %f"
  746. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  747. (const :tag "2 runs of xelatex"
  748. ("xelatex -interaction nonstopmode -output-directory %o %f"
  749. "xelatex -interaction nonstopmode -output-directory %o %f"))
  750. (const :tag "3 runs of xelatex"
  751. ("xelatex -interaction nonstopmode -output-directory %o %f"
  752. "xelatex -interaction nonstopmode -output-directory %o %f"
  753. "xelatex -interaction nonstopmode -output-directory %o %f"))
  754. (const :tag "xelatex,bibtex,xelatex,xelatex"
  755. ("xelatex -interaction nonstopmode -output-directory %o %f"
  756. "bibtex %b"
  757. "xelatex -interaction nonstopmode -output-directory %o %f"
  758. "xelatex -interaction nonstopmode -output-directory %o %f"))
  759. (const :tag "texi2dvi"
  760. ("texi2dvi -p -b -c -V %f"))
  761. (const :tag "rubber"
  762. ("rubber -d --into %o %f"))
  763. (function)))
  764. (defcustom org-latex-logfiles-extensions
  765. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  766. "The list of file extensions to consider as LaTeX logfiles.
  767. The logfiles will be remove if `org-latex-remove-logfiles' is
  768. non-nil."
  769. :group 'org-export-latex
  770. :type '(repeat (string :tag "Extension")))
  771. (defcustom org-latex-remove-logfiles t
  772. "Non-nil means remove the logfiles produced by PDF production.
  773. By default, logfiles are files with these extensions: .aux, .idx,
  774. .log, .out, .toc, .nav, .snm and .vrb. To define the set of
  775. logfiles to remove, set `org-latex-logfiles-extensions'."
  776. :group 'org-export-latex
  777. :type 'boolean)
  778. (defcustom org-latex-known-errors
  779. '(("Reference.*?undefined" . "[undefined reference]")
  780. ("Citation.*?undefined" . "[undefined citation]")
  781. ("Undefined control sequence" . "[undefined control sequence]")
  782. ("^! LaTeX.*?Error" . "[LaTeX error]")
  783. ("^! Package.*?Error" . "[package error]")
  784. ("Runaway argument" . "Runaway argument"))
  785. "Alist of regular expressions and associated messages for the user.
  786. The regular expressions are used to find possible errors in the
  787. log of a latex-run."
  788. :group 'org-export-latex
  789. :version "24.4"
  790. :package-version '(Org . "8.0")
  791. :type '(repeat
  792. (cons
  793. (string :tag "Regexp")
  794. (string :tag "Message"))))
  795. ;;; Internal Functions
  796. (defun org-latex--caption/label-string (element info)
  797. "Return caption and label LaTeX string for ELEMENT.
  798. INFO is a plist holding contextual information. If there's no
  799. caption nor label, return the empty string.
  800. For non-floats, see `org-latex--wrap-label'."
  801. (let* ((label (org-element-property :name element))
  802. (label-str (if (not (org-string-nw-p label)) ""
  803. (format "\\label{%s}"
  804. (org-export-solidify-link-text label))))
  805. (main (org-export-get-caption element))
  806. (short (org-export-get-caption element t)))
  807. (cond
  808. ((and (not main) (equal label-str "")) "")
  809. ((not main) (concat label-str "\n"))
  810. ;; Option caption format with short name.
  811. (short (format "\\caption[%s]{%s%s}\n"
  812. (org-export-data short info)
  813. label-str
  814. (org-export-data main info)))
  815. ;; Standard caption format.
  816. (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
  817. (defun org-latex-guess-inputenc (header)
  818. "Set the coding system in inputenc to what the buffer is.
  819. HEADER is the LaTeX header string. This function only applies
  820. when specified inputenc option is \"AUTO\".
  821. Return the new header, as a string."
  822. (let* ((cs (or (ignore-errors
  823. (latexenc-coding-system-to-inputenc
  824. (or org-export-coding-system buffer-file-coding-system)))
  825. "utf8")))
  826. (if (not cs) header
  827. ;; First translate if that is requested.
  828. (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
  829. ;; Then find the \usepackage statement and replace the option.
  830. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  831. cs header t nil 1))))
  832. (defun org-latex-guess-babel-language (header info)
  833. "Set Babel's language according to LANGUAGE keyword.
  834. HEADER is the LaTeX header string. INFO is the plist used as
  835. a communication channel.
  836. Insertion of guessed language only happens when Babel package has
  837. explicitly been loaded. Then it is added to the rest of
  838. package's options.
  839. Return the new header."
  840. (let ((language-code (plist-get info :language)))
  841. ;; If no language is set or Babel package is not loaded, return
  842. ;; HEADER as-is.
  843. (if (or (not (stringp language-code))
  844. (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
  845. header
  846. (let ((options (save-match-data
  847. (org-split-string (match-string 1 header) ",")))
  848. (language (cdr (assoc language-code
  849. org-latex-babel-language-alist))))
  850. ;; If LANGUAGE is already loaded, return header. Otherwise,
  851. ;; append LANGUAGE to other options.
  852. (if (member language options) header
  853. (replace-match (mapconcat 'identity
  854. (append options (list language))
  855. ",")
  856. nil nil header 1))))))
  857. (defun org-latex--find-verb-separator (s)
  858. "Return a character not used in string S.
  859. This is used to choose a separator for constructs like \\verb."
  860. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  861. (loop for c across ll
  862. when (not (string-match (regexp-quote (char-to-string c)) s))
  863. return (char-to-string c))))
  864. (defun org-latex--make-option-string (options)
  865. "Return a comma separated string of keywords and values.
  866. OPTIONS is an alist where the key is the options keyword as
  867. a string, and the value a list containing the keyword value, or
  868. nil."
  869. (mapconcat (lambda (pair)
  870. (concat (first pair)
  871. (when (> (length (second pair)) 0)
  872. (concat "=" (second pair)))))
  873. options
  874. ","))
  875. (defun org-latex--wrap-label (element output)
  876. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  877. This function shouldn't be used for floats. See
  878. `org-latex--caption/label-string'."
  879. (let ((label (org-element-property :name element)))
  880. (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
  881. (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
  882. output))))
  883. (defun org-latex--text-markup (text markup)
  884. "Format TEXT depending on MARKUP text markup.
  885. See `org-latex-text-markup-alist' for details."
  886. (let ((fmt (cdr (assq markup org-latex-text-markup-alist))))
  887. (cond
  888. ;; No format string: Return raw text.
  889. ((not fmt) text)
  890. ;; Handle the `verb' special case: Find and appropriate separator
  891. ;; and use "\\verb" command.
  892. ((eq 'verb fmt)
  893. (let ((separator (org-latex--find-verb-separator text)))
  894. (concat "\\verb" separator text separator)))
  895. ;; Handle the `protectedtexttt' special case: Protect some
  896. ;; special chars and use "\texttt{%s}" format string.
  897. ((eq 'protectedtexttt fmt)
  898. (let ((start 0)
  899. (trans '(("\\" . "\\textbackslash{}")
  900. ("~" . "\\textasciitilde{}")
  901. ("^" . "\\textasciicircum{}")))
  902. (rtn "")
  903. char)
  904. (while (string-match "[\\{}$%&_#~^]" text)
  905. (setq char (match-string 0 text))
  906. (if (> (match-beginning 0) 0)
  907. (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
  908. (setq text (substring text (1+ (match-beginning 0))))
  909. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  910. rtn (concat rtn char)))
  911. (setq text (concat rtn text)
  912. fmt "\\texttt{%s}")
  913. (while (string-match "--" text)
  914. (setq text (replace-match "-{}-" t t text)))
  915. (format fmt text)))
  916. ;; Else use format string.
  917. (t (format fmt text)))))
  918. (defun org-latex--delayed-footnotes-definitions (element info)
  919. "Return footnotes definitions in ELEMENT as a string.
  920. INFO is a plist used as a communication channel.
  921. Footnotes definitions are returned within \"\\footnotetxt{}\"
  922. commands.
  923. This function is used within constructs that don't support
  924. \"\\footnote{}\" command (i.e. an item's tag). In that case,
  925. \"\\footnotemark\" is used within the construct and the function
  926. just outside of it."
  927. (mapconcat
  928. (lambda (ref)
  929. (format
  930. "\\footnotetext[%s]{%s}"
  931. (org-export-get-footnote-number ref info)
  932. (org-trim
  933. (org-export-data
  934. (org-export-get-footnote-definition ref info) info))))
  935. ;; Find every footnote reference in ELEMENT.
  936. (let* (all-refs
  937. search-refs ; For byte-compiler.
  938. (search-refs
  939. (function
  940. (lambda (data)
  941. ;; Return a list of all footnote references never seen
  942. ;; before in DATA.
  943. (org-element-map data 'footnote-reference
  944. (lambda (ref)
  945. (when (org-export-footnote-first-reference-p ref info)
  946. (push ref all-refs)
  947. (when (eq (org-element-property :type ref) 'standard)
  948. (funcall search-refs
  949. (org-export-get-footnote-definition ref info)))))
  950. info)
  951. (reverse all-refs)))))
  952. (funcall search-refs element))
  953. ""))
  954. ;;; Template
  955. (defun org-latex-template (contents info)
  956. "Return complete document string after LaTeX conversion.
  957. CONTENTS is the transcoded contents string. INFO is a plist
  958. holding export options."
  959. (let ((title (org-export-data (plist-get info :title) info)))
  960. (concat
  961. ;; Time-stamp.
  962. (and (plist-get info :time-stamp-file)
  963. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  964. ;; Document class and packages.
  965. (let ((class (plist-get info :latex-class))
  966. (class-options (plist-get info :latex-class-options)))
  967. (org-element-normalize-string
  968. (let* ((header (nth 1 (assoc class org-latex-classes)))
  969. (document-class-string
  970. (and (stringp header)
  971. (if (not class-options) header
  972. (replace-regexp-in-string
  973. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  974. class-options header t nil 1)))))
  975. (if (not document-class-string)
  976. (user-error "Unknown LaTeX class `%s'" class)
  977. (org-latex-guess-babel-language
  978. (org-latex-guess-inputenc
  979. (org-splice-latex-header
  980. document-class-string
  981. org-latex-default-packages-alist
  982. org-latex-packages-alist nil
  983. (concat (plist-get info :latex-header)
  984. (plist-get info :latex-header-extra))))
  985. info)))))
  986. ;; Possibly limit depth for headline numbering.
  987. (let ((sec-num (plist-get info :section-numbers)))
  988. (when (integerp sec-num)
  989. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  990. ;; Author.
  991. (let ((author (and (plist-get info :with-author)
  992. (let ((auth (plist-get info :author)))
  993. (and auth (org-export-data auth info)))))
  994. (email (and (plist-get info :with-email)
  995. (org-export-data (plist-get info :email) info))))
  996. (cond ((and author email (not (string= "" email)))
  997. (format "\\author{%s\\thanks{%s}}\n" author email))
  998. ((or author email) (format "\\author{%s}\n" (or author email)))))
  999. ;; Date.
  1000. (let ((date (and (plist-get info :with-date) (plist-get info :date))))
  1001. (format "\\date{%s}\n"
  1002. (cond ((not date) "")
  1003. ;; If `:date' consists in a single timestamp and
  1004. ;; `:date-format' is provided, apply it.
  1005. ((and (plist-get info :date-format)
  1006. (not (cdr date))
  1007. (eq (org-element-type (car date)) 'timestamp))
  1008. (org-timestamp-format
  1009. (car date) (plist-get info :date-format)))
  1010. (t (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 (file-name-absolute-p raw-path)
  1642. (concat "file://" (expand-file-name raw-path))
  1643. (concat "file://" raw-path)))
  1644. (t raw-path)))
  1645. protocol)
  1646. (cond
  1647. ;; Image file.
  1648. (imagep (org-latex--inline-image link info))
  1649. ;; Radio link: Transcode target's contents and use them as link's
  1650. ;; description.
  1651. ((string= type "radio")
  1652. (let ((destination (org-export-resolve-radio-link link info)))
  1653. (when destination
  1654. (format "\\hyperref[%s]{%s}"
  1655. (org-export-solidify-link-text path)
  1656. (org-export-data (org-element-contents destination) info)))))
  1657. ;; Links pointing to a headline: Find destination and build
  1658. ;; appropriate referencing command.
  1659. ((member type '("custom-id" "fuzzy" "id"))
  1660. (let ((destination (if (string= type "fuzzy")
  1661. (org-export-resolve-fuzzy-link link info)
  1662. (org-export-resolve-id-link link info))))
  1663. (case (org-element-type destination)
  1664. ;; Id link points to an external file.
  1665. (plain-text
  1666. (if desc (format "\\href{file://%s}{%s}" destination desc)
  1667. (format "\\url{file://%s}" destination)))
  1668. ;; Fuzzy link points nowhere.
  1669. ('nil
  1670. (format org-latex-link-with-unknown-path-format
  1671. (or desc
  1672. (org-export-data
  1673. (org-element-property :raw-link link) info))))
  1674. ;; Fuzzy link points to an invisible target.
  1675. (keyword nil)
  1676. ;; LINK points to a headline. If headlines are numbered
  1677. ;; and the link has no description, display headline's
  1678. ;; number. Otherwise, display description or headline's
  1679. ;; title.
  1680. (headline
  1681. (let ((label
  1682. (format "sec-%s"
  1683. (mapconcat
  1684. 'number-to-string
  1685. (org-export-get-headline-number destination info)
  1686. "-"))))
  1687. (if (and (plist-get info :section-numbers) (not desc))
  1688. (format "\\ref{%s}" label)
  1689. (format "\\hyperref[%s]{%s}" label
  1690. (or desc
  1691. (org-export-data
  1692. (org-element-property :title destination) info))))))
  1693. ;; Fuzzy link points to a target. Do as above.
  1694. (otherwise
  1695. (let ((path (org-export-solidify-link-text path)))
  1696. (if (not desc) (format "\\ref{%s}" path)
  1697. (format "\\hyperref[%s]{%s}" path desc)))))))
  1698. ;; Coderef: replace link with the reference name or the
  1699. ;; equivalent line number.
  1700. ((string= type "coderef")
  1701. (format (org-export-get-coderef-format path desc)
  1702. (org-export-resolve-coderef path info)))
  1703. ;; Link type is handled by a special function.
  1704. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1705. (funcall protocol (org-link-unescape path) desc 'latex))
  1706. ;; External link with a description part.
  1707. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1708. ;; External link without a description part.
  1709. (path (format "\\url{%s}" path))
  1710. ;; No path, only description. Try to do something useful.
  1711. (t (format org-latex-link-with-unknown-path-format desc)))))
  1712. ;;;; Paragraph
  1713. (defun org-latex-paragraph (paragraph contents info)
  1714. "Transcode a PARAGRAPH element from Org to LaTeX.
  1715. CONTENTS is the contents of the paragraph, as a string. INFO is
  1716. the plist used as a communication channel."
  1717. contents)
  1718. ;;;; Plain List
  1719. (defun org-latex-plain-list (plain-list contents info)
  1720. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1721. CONTENTS is the contents of the list. INFO is a plist holding
  1722. contextual information."
  1723. (let* ((type (org-element-property :type plain-list))
  1724. (attr (org-export-read-attribute :attr_latex plain-list))
  1725. (latex-type (let ((env (plist-get attr :environment)))
  1726. (cond (env (format "%s" env))
  1727. ((eq type 'ordered) "enumerate")
  1728. ((eq type 'unordered) "itemize")
  1729. ((eq type 'descriptive) "description")))))
  1730. (org-latex--wrap-label
  1731. plain-list
  1732. (format "\\begin{%s}%s\n%s\\end{%s}"
  1733. latex-type
  1734. ;; Put optional arguments, if any inside square brackets
  1735. ;; when necessary.
  1736. (let ((options (format "%s" (or (plist-get attr :options) ""))))
  1737. (cond ((equal options "") "")
  1738. ((string-match "\\`\\[.*\\]\\'" options) options)
  1739. (t (format "[%s]" options))))
  1740. contents
  1741. latex-type))))
  1742. ;;;; Plain Text
  1743. (defun org-latex-plain-text (text info)
  1744. "Transcode a TEXT string from Org to LaTeX.
  1745. TEXT is the string to transcode. INFO is a plist holding
  1746. contextual information."
  1747. (let ((specialp (plist-get info :with-special-strings))
  1748. (output text))
  1749. ;; Protect %, #, &, $, ~, ^, _, { and }.
  1750. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" output)
  1751. (setq output
  1752. (replace-match
  1753. (format "\\%s" (match-string 2 output)) nil t output 2)))
  1754. ;; Protect \. If special strings are used, be careful not to
  1755. ;; protect "\" in "\-" constructs.
  1756. (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
  1757. (setq output
  1758. (replace-regexp-in-string
  1759. (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
  1760. "$\\backslash$" output nil t 1)))
  1761. ;; Activate smart quotes. Be sure to provide original TEXT string
  1762. ;; since OUTPUT may have been modified.
  1763. (when (plist-get info :with-smart-quotes)
  1764. (setq output (org-export-activate-smart-quotes output :latex info text)))
  1765. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1766. (let ((case-fold-search nil)
  1767. (start 0))
  1768. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
  1769. (setq output (replace-match
  1770. (format "\\%s{}" (match-string 1 output)) nil t output)
  1771. start (match-end 0))))
  1772. ;; Convert special strings.
  1773. (when specialp
  1774. (setq output
  1775. (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
  1776. ;; Handle break preservation if required.
  1777. (when (plist-get info :preserve-breaks)
  1778. (setq output (replace-regexp-in-string
  1779. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
  1780. ;; Return value.
  1781. output))
  1782. ;;;; Planning
  1783. (defun org-latex-planning (planning contents info)
  1784. "Transcode a PLANNING element from Org to LaTeX.
  1785. CONTENTS is nil. INFO is a plist holding contextual
  1786. information."
  1787. (concat
  1788. "\\noindent"
  1789. (mapconcat
  1790. 'identity
  1791. (delq nil
  1792. (list
  1793. (let ((closed (org-element-property :closed planning)))
  1794. (when closed
  1795. (concat
  1796. (format "\\textbf{%s} " org-closed-string)
  1797. (format org-latex-inactive-timestamp-format
  1798. (org-translate-time
  1799. (org-element-property :raw-value closed))))))
  1800. (let ((deadline (org-element-property :deadline planning)))
  1801. (when deadline
  1802. (concat
  1803. (format "\\textbf{%s} " org-deadline-string)
  1804. (format org-latex-active-timestamp-format
  1805. (org-translate-time
  1806. (org-element-property :raw-value deadline))))))
  1807. (let ((scheduled (org-element-property :scheduled planning)))
  1808. (when scheduled
  1809. (concat
  1810. (format "\\textbf{%s} " org-scheduled-string)
  1811. (format org-latex-active-timestamp-format
  1812. (org-translate-time
  1813. (org-element-property :raw-value scheduled))))))))
  1814. " ")
  1815. "\\\\"))
  1816. ;;;; Quote Block
  1817. (defun org-latex-quote-block (quote-block contents info)
  1818. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1819. CONTENTS holds the contents of the block. INFO is a plist
  1820. holding contextual information."
  1821. (org-latex--wrap-label
  1822. quote-block
  1823. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1824. ;;;; Quote Section
  1825. (defun org-latex-quote-section (quote-section contents info)
  1826. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1827. CONTENTS is nil. INFO is a plist holding contextual information."
  1828. (let ((value (org-remove-indentation
  1829. (org-element-property :value quote-section))))
  1830. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1831. ;;;; Radio Target
  1832. (defun org-latex-radio-target (radio-target text info)
  1833. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1834. TEXT is the text of the target. INFO is a plist holding
  1835. contextual information."
  1836. (format "\\label{%s}%s"
  1837. (org-export-solidify-link-text
  1838. (org-element-property :value radio-target))
  1839. text))
  1840. ;;;; Section
  1841. (defun org-latex-section (section contents info)
  1842. "Transcode a SECTION element from Org to LaTeX.
  1843. CONTENTS holds the contents of the section. INFO is a plist
  1844. holding contextual information."
  1845. contents)
  1846. ;;;; Special Block
  1847. (defun org-latex-special-block (special-block contents info)
  1848. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1849. CONTENTS holds the contents of the block. INFO is a plist
  1850. holding contextual information."
  1851. (let ((type (downcase (org-element-property :type special-block)))
  1852. (opt (org-export-read-attribute :attr_latex special-block :options)))
  1853. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  1854. ;; Insert any label or caption within the block
  1855. ;; (otherwise, a reference pointing to that element will
  1856. ;; count the section instead).
  1857. (org-latex--caption/label-string special-block info)
  1858. contents
  1859. (format "\\end{%s}" type))))
  1860. ;;;; Src Block
  1861. (defun org-latex-src-block (src-block contents info)
  1862. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1863. CONTENTS holds the contents of the item. INFO is a plist holding
  1864. contextual information."
  1865. (when (org-string-nw-p (org-element-property :value src-block))
  1866. (let* ((lang (org-element-property :language src-block))
  1867. (caption (org-element-property :caption src-block))
  1868. (label (org-element-property :name src-block))
  1869. (custom-env (and lang
  1870. (cadr (assq (intern lang)
  1871. org-latex-custom-lang-environments))))
  1872. (num-start (case (org-element-property :number-lines src-block)
  1873. (continued (org-export-get-loc src-block info))
  1874. (new 0)))
  1875. (retain-labels (org-element-property :retain-labels src-block)))
  1876. (cond
  1877. ;; Case 1. No source fontification.
  1878. ((not org-latex-listings)
  1879. (let ((caption-str (org-latex--caption/label-string src-block info))
  1880. (float-env (and caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
  1881. (format
  1882. (or float-env "%s")
  1883. (concat caption-str
  1884. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1885. (org-export-format-code-default src-block info))))))
  1886. ;; Case 2. Custom environment.
  1887. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1888. custom-env
  1889. (org-export-format-code-default src-block info)
  1890. custom-env))
  1891. ;; Case 3. Use minted package.
  1892. ((eq org-latex-listings 'minted)
  1893. (let ((float-env
  1894. (when (or label caption)
  1895. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1896. (org-latex--caption/label-string src-block info))))
  1897. (body
  1898. (format
  1899. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1900. ;; Options.
  1901. (org-latex--make-option-string
  1902. (if (or (not num-start)
  1903. (assoc "linenos" org-latex-minted-options))
  1904. org-latex-minted-options
  1905. (append `(("linenos")
  1906. ("firstnumber" ,(number-to-string (1+ num-start))))
  1907. org-latex-minted-options)))
  1908. ;; Language.
  1909. (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
  1910. ;; Source code.
  1911. (let* ((code-info (org-export-unravel-code src-block))
  1912. (max-width
  1913. (apply 'max
  1914. (mapcar 'length
  1915. (org-split-string (car code-info)
  1916. "\n")))))
  1917. (org-export-format-code
  1918. (car code-info)
  1919. (lambda (loc num ref)
  1920. (concat
  1921. loc
  1922. (when ref
  1923. ;; Ensure references are flushed to the right,
  1924. ;; separated with 6 spaces from the widest line
  1925. ;; of code.
  1926. (concat (make-string (+ (- max-width (length loc)) 6)
  1927. ?\s)
  1928. (format "(%s)" ref)))))
  1929. nil (and retain-labels (cdr code-info)))))))
  1930. ;; Return value.
  1931. (if float-env (format float-env body) body)))
  1932. ;; Case 4. Use listings package.
  1933. (t
  1934. (let ((lst-lang
  1935. (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
  1936. (caption-str
  1937. (when caption
  1938. (let ((main (org-export-get-caption src-block))
  1939. (secondary (org-export-get-caption src-block t)))
  1940. (if (not secondary)
  1941. (format "{%s}" (org-export-data main info))
  1942. (format "{[%s]%s}"
  1943. (org-export-data secondary info)
  1944. (org-export-data main info)))))))
  1945. (concat
  1946. ;; Options.
  1947. (format "\\lstset{%s}\n"
  1948. (org-latex--make-option-string
  1949. (append
  1950. org-latex-listings-options
  1951. `(("language" ,lst-lang))
  1952. (when label `(("label" ,label)))
  1953. (when caption-str `(("caption" ,caption-str)))
  1954. (cond ((assoc "numbers" org-latex-listings-options) nil)
  1955. ((not num-start) '(("numbers" "none")))
  1956. ((zerop num-start) '(("numbers" "left")))
  1957. (t `(("numbers" "left")
  1958. ("firstnumber"
  1959. ,(number-to-string (1+ num-start)))))))))
  1960. ;; Source code.
  1961. (format
  1962. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1963. (let* ((code-info (org-export-unravel-code src-block))
  1964. (max-width
  1965. (apply 'max
  1966. (mapcar 'length
  1967. (org-split-string (car code-info) "\n")))))
  1968. (org-export-format-code
  1969. (car code-info)
  1970. (lambda (loc num ref)
  1971. (concat
  1972. loc
  1973. (when ref
  1974. ;; Ensure references are flushed to the right,
  1975. ;; separated with 6 spaces from the widest line of
  1976. ;; code
  1977. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1978. (format "(%s)" ref)))))
  1979. nil (and retain-labels (cdr code-info))))))))))))
  1980. ;;;; Statistics Cookie
  1981. (defun org-latex-statistics-cookie (statistics-cookie contents info)
  1982. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1983. CONTENTS is nil. INFO is a plist holding contextual information."
  1984. (replace-regexp-in-string
  1985. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  1986. ;;;; Strike-Through
  1987. (defun org-latex-strike-through (strike-through contents info)
  1988. "Transcode STRIKE-THROUGH from Org to LaTeX.
  1989. CONTENTS is the text with strike-through markup. INFO is a plist
  1990. holding contextual information."
  1991. (org-latex--text-markup contents 'strike-through))
  1992. ;;;; Subscript
  1993. (defun org-latex-subscript (subscript contents info)
  1994. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1995. CONTENTS is the contents of the object. INFO is a plist holding
  1996. contextual information."
  1997. (if (= (length contents) 1) (format "$_%s$" contents)
  1998. ;; Handle multiple objects in SUBSCRIPT by creating a subscript
  1999. ;; command for each of them.
  2000. (let ((prev-blanks 0))
  2001. (mapconcat
  2002. (lambda (obj)
  2003. (case (org-element-type obj)
  2004. ((entity latex-fragment)
  2005. (setq prev-blanks (org-element-property :post-blank obj))
  2006. (let ((data (org-trim (org-export-data obj info))))
  2007. (string-match
  2008. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  2009. data)
  2010. (format "$_{%s}$" (match-string 1 data))))
  2011. (plain-text
  2012. (format "$_\\mathrm{%s}$"
  2013. (concat (make-string prev-blanks ? )
  2014. ;; mathrm command doesn't handle spaces,
  2015. ;; so we have to enforce them.
  2016. (replace-regexp-in-string
  2017. " " "\\\\ " (org-export-data obj info)))))
  2018. (otherwise
  2019. (setq prev-blanks (org-element-property :post-blank obj))
  2020. (format "$_{%s}$" (org-export-data obj info)))))
  2021. (org-element-contents subscript) ""))))
  2022. ;;;; Superscript
  2023. (defun org-latex-superscript (superscript contents info)
  2024. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  2025. CONTENTS is the contents of the object. INFO is a plist holding
  2026. contextual information."
  2027. (if (= (length contents) 1) (format "$^%s$" contents)
  2028. ;; Handle multiple objects in SUPERSCRIPT by creating
  2029. ;; a superscript command for each of them.
  2030. (let ((prev-blanks 0))
  2031. (mapconcat
  2032. (lambda (obj)
  2033. (case (org-element-type obj)
  2034. ((entity latex-fragment)
  2035. (setq prev-blanks (org-element-property :post-blank obj))
  2036. (let ((data (org-trim (org-export-data obj info))))
  2037. (string-match
  2038. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  2039. data)
  2040. (format "$^{%s}$" (match-string 1 data))))
  2041. (plain-text
  2042. (format "$^\\mathrm{%s}$"
  2043. (concat (make-string prev-blanks ? )
  2044. ;; mathrm command doesn't handle spaces,
  2045. ;; so we have to enforce them.
  2046. (replace-regexp-in-string
  2047. " " "\\\\ " (org-export-data obj info)))))
  2048. (otherwise
  2049. (setq prev-blanks (org-element-property :post-blank obj))
  2050. (format "$^{%s}$" (org-export-data obj info)))))
  2051. (org-element-contents superscript) ""))))
  2052. ;;;; Table
  2053. ;;
  2054. ;; `org-latex-table' is the entry point for table transcoding. It
  2055. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  2056. ;; delegates the job to either `org-latex--table.el-table',
  2057. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  2058. ;; depending of the type of the table and the mode requested.
  2059. ;;
  2060. ;; `org-latex--align-string' is a subroutine used to build alignment
  2061. ;; string for Org tables.
  2062. (defun org-latex-table (table contents info)
  2063. "Transcode a TABLE element from Org to LaTeX.
  2064. CONTENTS is the contents of the table. INFO is a plist holding
  2065. contextual information."
  2066. (if (eq (org-element-property :type table) 'table.el)
  2067. ;; "table.el" table. Convert it using appropriate tools.
  2068. (org-latex--table.el-table table info)
  2069. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  2070. org-latex-default-table-mode)))
  2071. (cond
  2072. ;; Case 1: Verbatim table.
  2073. ((string= type "verbatim")
  2074. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  2075. ;; Re-create table, without affiliated keywords.
  2076. (org-trim (org-element-interpret-data
  2077. `(table nil ,@(org-element-contents table))))))
  2078. ;; Case 2: Matrix.
  2079. ((or (string= type "math") (string= type "inline-math"))
  2080. (org-latex--math-table table info))
  2081. ;; Case 3: Standard table.
  2082. (t (concat (org-latex--org-table table contents info)
  2083. ;; When there are footnote references within the
  2084. ;; table, insert their definition just after it.
  2085. (org-latex--delayed-footnotes-definitions table info)))))))
  2086. (defun org-latex--align-string (table info)
  2087. "Return an appropriate LaTeX alignment string.
  2088. TABLE is the considered table. INFO is a plist used as
  2089. a communication channel."
  2090. (or (org-export-read-attribute :attr_latex table :align)
  2091. (let (align)
  2092. ;; Extract column groups and alignment from first (non-rule)
  2093. ;; row.
  2094. (org-element-map
  2095. (org-element-map table 'table-row
  2096. (lambda (row)
  2097. (and (eq (org-element-property :type row) 'standard) row))
  2098. info 'first-match)
  2099. 'table-cell
  2100. (lambda (cell)
  2101. (let ((borders (org-export-table-cell-borders cell info)))
  2102. ;; Check left border for the first cell only.
  2103. (when (and (memq 'left borders) (not align))
  2104. (push "|" align))
  2105. (push (case (org-export-table-cell-alignment cell info)
  2106. (left "l")
  2107. (right "r")
  2108. (center "c"))
  2109. align)
  2110. (when (memq 'right borders) (push "|" align))))
  2111. info)
  2112. (apply 'concat (nreverse align)))))
  2113. (defun org-latex--org-table (table contents info)
  2114. "Return appropriate LaTeX code for an Org table.
  2115. TABLE is the table type element to transcode. CONTENTS is its
  2116. contents, as a string. INFO is a plist used as a communication
  2117. channel.
  2118. This function assumes TABLE has `org' as its `:type' property and
  2119. `table' as its `:mode' attribute."
  2120. (let* ((caption (org-latex--caption/label-string table info))
  2121. (attr (org-export-read-attribute :attr_latex table))
  2122. ;; Determine alignment string.
  2123. (alignment (org-latex--align-string table info))
  2124. ;; Determine environment for the table: longtable, tabular...
  2125. (table-env (or (plist-get attr :environment)
  2126. org-latex-default-table-environment))
  2127. ;; If table is a float, determine environment: table, table*
  2128. ;; or sidewaystable.
  2129. (float-env (unless (equal "longtable" table-env)
  2130. (let ((float (plist-get attr :float)))
  2131. (cond
  2132. ((string= float "sidewaystable") "sidewaystable")
  2133. ((string= float "multicolumn") "table*")
  2134. ((or (string= float "table")
  2135. (org-element-property :caption table))
  2136. "table")))))
  2137. ;; Extract others display options.
  2138. (fontsize (let ((font (plist-get attr :font)))
  2139. (and font (concat font "\n"))))
  2140. (width (plist-get attr :width))
  2141. (placement (or (plist-get attr :placement)
  2142. (format "[%s]" org-latex-default-figure-position)))
  2143. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2144. org-latex-tables-centered)))
  2145. ;; Prepare the final format string for the table.
  2146. (cond
  2147. ;; Longtable.
  2148. ((equal "longtable" table-env)
  2149. (concat (and fontsize (concat "{" fontsize))
  2150. (format "\\begin{longtable}{%s}\n" alignment)
  2151. (and org-latex-table-caption-above
  2152. (org-string-nw-p caption)
  2153. (concat caption "\\\\\n"))
  2154. contents
  2155. (and (not org-latex-table-caption-above)
  2156. (org-string-nw-p caption)
  2157. (concat caption "\\\\\n"))
  2158. "\\end{longtable}\n"
  2159. (and fontsize "}")))
  2160. ;; Others.
  2161. (t (concat (cond
  2162. (float-env
  2163. (concat (format "\\begin{%s}%s\n" float-env placement)
  2164. (if org-latex-table-caption-above caption "")
  2165. (when centerp "\\centering\n")
  2166. fontsize))
  2167. (centerp (concat "\\begin{center}\n" fontsize))
  2168. (fontsize (concat "{" fontsize)))
  2169. (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2170. table-env
  2171. (if width (format "{%s}" width) "")
  2172. alignment
  2173. contents
  2174. table-env)
  2175. (cond
  2176. (float-env
  2177. (concat (if org-latex-table-caption-above "" caption)
  2178. (format "\n\\end{%s}" float-env)))
  2179. (centerp "\n\\end{center}")
  2180. (fontsize "}")))))))
  2181. (defun org-latex--table.el-table (table info)
  2182. "Return appropriate LaTeX code for a table.el table.
  2183. TABLE is the table type element to transcode. INFO is a plist
  2184. used as a communication channel.
  2185. This function assumes TABLE has `table.el' as its `:type'
  2186. property."
  2187. (require 'table)
  2188. ;; Ensure "*org-export-table*" buffer is empty.
  2189. (with-current-buffer (get-buffer-create "*org-export-table*")
  2190. (erase-buffer))
  2191. (let ((output (with-temp-buffer
  2192. (insert (org-element-property :value table))
  2193. (goto-char 1)
  2194. (re-search-forward "^[ \t]*|[^|]" nil t)
  2195. (table-generate-source 'latex "*org-export-table*")
  2196. (with-current-buffer "*org-export-table*"
  2197. (org-trim (buffer-string))))))
  2198. (kill-buffer (get-buffer "*org-export-table*"))
  2199. ;; Remove left out comments.
  2200. (while (string-match "^%.*\n" output)
  2201. (setq output (replace-match "" t t output)))
  2202. (let ((attr (org-export-read-attribute :attr_latex table)))
  2203. (when (plist-get attr :rmlines)
  2204. ;; When the "rmlines" attribute is provided, remove all hlines
  2205. ;; but the the one separating heading from the table body.
  2206. (let ((n 0) (pos 0))
  2207. (while (and (< (length output) pos)
  2208. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2209. (incf n)
  2210. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2211. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2212. org-latex-tables-centered)))
  2213. (if (not centerp) output
  2214. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2215. (defun org-latex--math-table (table info)
  2216. "Return appropriate LaTeX code for a matrix.
  2217. TABLE is the table type element to transcode. INFO is a plist
  2218. used as a communication channel.
  2219. This function assumes TABLE has `org' as its `:type' property and
  2220. `inline-math' or `math' as its `:mode' attribute.."
  2221. (let* ((caption (org-latex--caption/label-string table info))
  2222. (attr (org-export-read-attribute :attr_latex table))
  2223. (inlinep (eq (plist-get attr :mode) 'inline-math))
  2224. (env (or (plist-get attr :environment)
  2225. org-latex-default-table-environment))
  2226. (contents
  2227. (mapconcat
  2228. (lambda (row)
  2229. ;; Ignore horizontal rules.
  2230. (when (eq (org-element-property :type row) 'standard)
  2231. ;; Return each cell unmodified.
  2232. (concat
  2233. (mapconcat
  2234. (lambda (cell)
  2235. (substring (org-element-interpret-data cell) 0 -1))
  2236. (org-element-map row 'table-cell 'identity info) "&")
  2237. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2238. "\n")))
  2239. (org-element-map table 'table-row 'identity info) ""))
  2240. ;; Variables related to math clusters (contiguous math tables
  2241. ;; of the same type).
  2242. (mode (org-export-read-attribute :attr_latex table :mode))
  2243. (prev (org-export-get-previous-element table info))
  2244. (next (org-export-get-next-element table info))
  2245. (same-mode-p
  2246. (lambda (table)
  2247. ;; Non-nil when TABLE has the same mode as current table.
  2248. (string= (or (org-export-read-attribute :attr_latex table :mode)
  2249. org-latex-default-table-mode)
  2250. mode))))
  2251. (concat
  2252. ;; Opening string. If TABLE is in the middle of a table cluster,
  2253. ;; do not insert any.
  2254. (cond ((and prev
  2255. (eq (org-element-type prev) 'table)
  2256. (memq (org-element-property :post-blank prev) '(0 nil))
  2257. (funcall same-mode-p prev))
  2258. nil)
  2259. (inlinep "\\(")
  2260. ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
  2261. (t "\\["))
  2262. ;; Prefix.
  2263. (or (plist-get attr :math-prefix) "")
  2264. ;; Environment. Also treat special cases.
  2265. (cond ((equal env "array")
  2266. (let ((align (org-latex--align-string table info)))
  2267. (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
  2268. ((assoc env org-latex-table-matrix-macros)
  2269. (format "\\%s%s{\n%s}"
  2270. env
  2271. (or (plist-get attr :math-arguments) "")
  2272. contents))
  2273. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2274. ;; Suffix.
  2275. (or (plist-get attr :math-suffix) "")
  2276. ;; Closing string. If TABLE is in the middle of a table cluster,
  2277. ;; do not insert any. If it closes such a cluster, be sure to
  2278. ;; close the cluster with a string matching the opening string.
  2279. (cond ((and next
  2280. (eq (org-element-type next) 'table)
  2281. (memq (org-element-property :post-blank table) '(0 nil))
  2282. (funcall same-mode-p next))
  2283. nil)
  2284. (inlinep "\\)")
  2285. ;; Find cluster beginning to know which environment to use.
  2286. ((let ((cluster-beg table) prev)
  2287. (while (and (setq prev (org-export-get-previous-element
  2288. cluster-beg info))
  2289. (memq (org-element-property :post-blank prev)
  2290. '(0 nil))
  2291. (funcall same-mode-p prev))
  2292. (setq cluster-beg prev))
  2293. (and (or (org-element-property :caption cluster-beg)
  2294. (org-element-property :name cluster-beg))
  2295. "\n\\end{equation}")))
  2296. (t "\\]")))))
  2297. ;;;; Table Cell
  2298. (defun org-latex-table-cell (table-cell contents info)
  2299. "Transcode a TABLE-CELL element from Org to LaTeX.
  2300. CONTENTS is the cell contents. INFO is a plist used as
  2301. a communication channel."
  2302. (concat (if (and contents
  2303. org-latex-table-scientific-notation
  2304. (string-match orgtbl-exp-regexp contents))
  2305. ;; Use appropriate format string for scientific
  2306. ;; notation.
  2307. (format org-latex-table-scientific-notation
  2308. (match-string 1 contents)
  2309. (match-string 2 contents))
  2310. contents)
  2311. (when (org-export-get-next-element table-cell info) " & ")))
  2312. ;;;; Table Row
  2313. (defun org-latex-table-row (table-row contents info)
  2314. "Transcode a TABLE-ROW element from Org to LaTeX.
  2315. CONTENTS is the contents of the row. INFO is a plist used as
  2316. a communication channel."
  2317. ;; Rules are ignored since table separators are deduced from
  2318. ;; borders of the current row.
  2319. (when (eq (org-element-property :type table-row) 'standard)
  2320. (let* ((attr (org-export-read-attribute :attr_latex
  2321. (org-export-get-parent table-row)))
  2322. (longtablep (string= (or (plist-get attr :environment)
  2323. org-latex-default-table-environment)
  2324. "longtable"))
  2325. (booktabsp (if (plist-member attr :booktabs)
  2326. (plist-get attr :booktabs)
  2327. org-latex-tables-booktabs))
  2328. ;; TABLE-ROW's borders are extracted from its first cell.
  2329. (borders (org-export-table-cell-borders
  2330. (car (org-element-contents table-row)) info)))
  2331. (concat
  2332. ;; When BOOKTABS are activated enforce top-rule even when no
  2333. ;; hline was specifically marked.
  2334. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2335. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  2336. contents "\\\\\n"
  2337. (cond
  2338. ;; Special case for long tables. Define header and footers.
  2339. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2340. (format "%s
  2341. \\endhead
  2342. %s\\multicolumn{%d}{r}{Continued on next page} \\\\
  2343. \\endfoot
  2344. \\endlastfoot"
  2345. (if booktabsp "\\midrule" "\\hline")
  2346. (if booktabsp "\\midrule" "\\hline")
  2347. ;; Number of columns.
  2348. (cdr (org-export-table-dimensions
  2349. (org-export-get-parent-table table-row) info))))
  2350. ;; When BOOKTABS are activated enforce bottom rule even when
  2351. ;; no hline was specifically marked.
  2352. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  2353. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  2354. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  2355. ;;;; Target
  2356. (defun org-latex-target (target contents info)
  2357. "Transcode a TARGET object from Org to LaTeX.
  2358. CONTENTS is nil. INFO is a plist holding contextual
  2359. information."
  2360. (format "\\label{%s}"
  2361. (org-export-solidify-link-text (org-element-property :value target))))
  2362. ;;;; Timestamp
  2363. (defun org-latex-timestamp (timestamp contents info)
  2364. "Transcode a TIMESTAMP object from Org to LaTeX.
  2365. CONTENTS is nil. INFO is a plist holding contextual
  2366. information."
  2367. (let ((value (org-latex-plain-text
  2368. (org-timestamp-translate timestamp) info)))
  2369. (case (org-element-property :type timestamp)
  2370. ((active active-range) (format org-latex-active-timestamp-format value))
  2371. ((inactive inactive-range)
  2372. (format org-latex-inactive-timestamp-format value))
  2373. (otherwise (format org-latex-diary-timestamp-format value)))))
  2374. ;;;; Underline
  2375. (defun org-latex-underline (underline contents info)
  2376. "Transcode UNDERLINE from Org to LaTeX.
  2377. CONTENTS is the text with underline markup. INFO is a plist
  2378. holding contextual information."
  2379. (org-latex--text-markup contents 'underline))
  2380. ;;;; Verbatim
  2381. (defun org-latex-verbatim (verbatim contents info)
  2382. "Transcode a VERBATIM object from Org to LaTeX.
  2383. CONTENTS is nil. INFO is a plist used as a communication
  2384. channel."
  2385. (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  2386. ;;;; Verse Block
  2387. (defun org-latex-verse-block (verse-block contents info)
  2388. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2389. CONTENTS is verse block contents. INFO is a plist holding
  2390. contextual information."
  2391. (org-latex--wrap-label
  2392. verse-block
  2393. ;; In a verse environment, add a line break to each newline
  2394. ;; character and change each white space at beginning of a line
  2395. ;; into a space of 1 em. Also change each blank line with
  2396. ;; a vertical space of 1 em.
  2397. (progn
  2398. (setq contents (replace-regexp-in-string
  2399. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  2400. (replace-regexp-in-string
  2401. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  2402. (while (string-match "^[ \t]+" contents)
  2403. (let ((new-str (format "\\hspace*{%dem}"
  2404. (length (match-string 0 contents)))))
  2405. (setq contents (replace-match new-str nil t contents))))
  2406. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  2407. ;;; End-user functions
  2408. ;;;###autoload
  2409. (defun org-latex-export-as-latex
  2410. (&optional async subtreep visible-only body-only ext-plist)
  2411. "Export current buffer as a LaTeX buffer.
  2412. If narrowing is active in the current buffer, only export its
  2413. narrowed part.
  2414. If a region is active, export that region.
  2415. A non-nil optional argument ASYNC means the process should happen
  2416. asynchronously. The resulting buffer should be accessible
  2417. through the `org-export-stack' interface.
  2418. When optional argument SUBTREEP is non-nil, export the sub-tree
  2419. at point, extracting information from the headline properties
  2420. first.
  2421. When optional argument VISIBLE-ONLY is non-nil, don't export
  2422. contents of hidden elements.
  2423. When optional argument BODY-ONLY is non-nil, only write code
  2424. between \"\\begin{document}\" and \"\\end{document}\".
  2425. EXT-PLIST, when provided, is a property list with external
  2426. parameters overriding Org default settings, but still inferior to
  2427. file-local settings.
  2428. Export is done in a buffer named \"*Org LATEX Export*\", which
  2429. will be displayed when `org-export-show-temporary-export-buffer'
  2430. is non-nil."
  2431. (interactive)
  2432. (if async
  2433. (org-export-async-start
  2434. (lambda (output)
  2435. (with-current-buffer (get-buffer-create "*Org LATEX Export*")
  2436. (erase-buffer)
  2437. (insert output)
  2438. (goto-char (point-min))
  2439. (LaTeX-mode)
  2440. (org-export-add-to-stack (current-buffer) 'latex)))
  2441. `(org-export-as 'latex ,subtreep ,visible-only ,body-only
  2442. ',ext-plist))
  2443. (let ((outbuf
  2444. (org-export-to-buffer 'latex "*Org LATEX Export*"
  2445. subtreep visible-only body-only ext-plist)))
  2446. (with-current-buffer outbuf (LaTeX-mode))
  2447. (when org-export-show-temporary-export-buffer
  2448. (switch-to-buffer-other-window outbuf)))))
  2449. ;;;###autoload
  2450. (defun org-latex-export-to-latex
  2451. (&optional async subtreep visible-only body-only ext-plist)
  2452. "Export current buffer to a LaTeX file.
  2453. If narrowing is active in the current buffer, only export its
  2454. narrowed part.
  2455. If a region is active, export that region.
  2456. A non-nil optional argument ASYNC means the process should happen
  2457. asynchronously. The resulting file should be accessible through
  2458. the `org-export-stack' interface.
  2459. When optional argument SUBTREEP is non-nil, export the sub-tree
  2460. at point, extracting information from the headline properties
  2461. first.
  2462. When optional argument VISIBLE-ONLY is non-nil, don't export
  2463. contents of hidden elements.
  2464. When optional argument BODY-ONLY is non-nil, only write code
  2465. between \"\\begin{document}\" and \"\\end{document}\".
  2466. EXT-PLIST, when provided, is a property list with external
  2467. parameters overriding Org default settings, but still inferior to
  2468. file-local settings.
  2469. Return output file's name."
  2470. (interactive)
  2471. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2472. (if async
  2473. (org-export-async-start
  2474. (lambda (f) (org-export-add-to-stack f 'latex))
  2475. `(expand-file-name
  2476. (org-export-to-file
  2477. 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
  2478. (org-export-to-file
  2479. 'latex outfile subtreep visible-only body-only ext-plist))))
  2480. ;;;###autoload
  2481. (defun org-latex-export-to-pdf
  2482. (&optional async subtreep visible-only body-only ext-plist)
  2483. "Export current buffer to LaTeX then process through to PDF.
  2484. If narrowing is active in the current buffer, only export its
  2485. narrowed part.
  2486. If a region is active, export that region.
  2487. A non-nil optional argument ASYNC means the process should happen
  2488. asynchronously. The resulting file should be accessible through
  2489. the `org-export-stack' interface.
  2490. When optional argument SUBTREEP is non-nil, export the sub-tree
  2491. at point, extracting information from the headline properties
  2492. first.
  2493. When optional argument VISIBLE-ONLY is non-nil, don't export
  2494. contents of hidden elements.
  2495. When optional argument BODY-ONLY is non-nil, only write code
  2496. between \"\\begin{document}\" and \"\\end{document}\".
  2497. EXT-PLIST, when provided, is a property list with external
  2498. parameters overriding Org default settings, but still inferior to
  2499. file-local settings.
  2500. Return PDF file's name."
  2501. (interactive)
  2502. (if async
  2503. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2504. (org-export-async-start
  2505. (lambda (f) (org-export-add-to-stack f 'latex))
  2506. `(expand-file-name
  2507. (org-latex-compile
  2508. (org-export-to-file
  2509. 'latex ,outfile ,subtreep ,visible-only ,body-only
  2510. ',ext-plist)))))
  2511. (org-latex-compile
  2512. (org-latex-export-to-latex
  2513. nil subtreep visible-only body-only ext-plist))))
  2514. (defun org-latex-compile (texfile &optional snippet)
  2515. "Compile a TeX file.
  2516. TEXFILE is the name of the file being compiled. Processing is
  2517. done through the command specified in `org-latex-pdf-process'.
  2518. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  2519. file used to preview a LaTeX snippet. In this case, do not
  2520. create a log buffer and do not bother removing log files.
  2521. Return PDF file name or an error if it couldn't be produced."
  2522. (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
  2523. (full-name (file-truename texfile))
  2524. (out-dir (file-name-directory texfile))
  2525. ;; Make sure `default-directory' is set to TEXFILE directory,
  2526. ;; not to whatever value the current buffer may have.
  2527. (default-directory (file-name-directory full-name))
  2528. errors)
  2529. (unless snippet (message (format "Processing LaTeX file %s ..." texfile)))
  2530. (save-window-excursion
  2531. (cond
  2532. ;; A function is provided: Apply it.
  2533. ((functionp org-latex-pdf-process)
  2534. (funcall org-latex-pdf-process (shell-quote-argument texfile)))
  2535. ;; A list is provided: Replace %b, %f and %o with appropriate
  2536. ;; values in each command before applying it. Output is
  2537. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2538. ((consp org-latex-pdf-process)
  2539. (let ((outbuf (and (not snippet)
  2540. (get-buffer-create "*Org PDF LaTeX Output*"))))
  2541. (mapc
  2542. (lambda (command)
  2543. (shell-command
  2544. (replace-regexp-in-string
  2545. "%b" (shell-quote-argument base-name)
  2546. (replace-regexp-in-string
  2547. "%f" (shell-quote-argument full-name)
  2548. (replace-regexp-in-string
  2549. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2550. outbuf))
  2551. org-latex-pdf-process)
  2552. ;; Collect standard errors from output buffer.
  2553. (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
  2554. (t (error "No valid command to process to PDF")))
  2555. (let ((pdffile (concat out-dir base-name ".pdf")))
  2556. ;; Check for process failure. Provide collected errors if
  2557. ;; possible.
  2558. (if (not (file-exists-p pdffile))
  2559. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2560. (when errors (concat ": " errors))))
  2561. ;; Else remove log files, when specified, and signal end of
  2562. ;; process to user, along with any error encountered.
  2563. (when (and (not snippet) org-latex-remove-logfiles)
  2564. (dolist (ext org-latex-logfiles-extensions)
  2565. (let ((file (concat out-dir base-name "." ext)))
  2566. (when (file-exists-p file) (delete-file file)))))
  2567. (message (concat "Process completed"
  2568. (if (not errors) "."
  2569. (concat " with errors: " errors)))))
  2570. ;; Return output file name.
  2571. pdffile))))
  2572. (defun org-latex--collect-errors (buffer)
  2573. "Collect some kind of errors from \"pdflatex\" command output.
  2574. BUFFER is the buffer containing output.
  2575. Return collected error types as a string, or nil if there was
  2576. none."
  2577. (with-current-buffer buffer
  2578. (save-excursion
  2579. (goto-char (point-max))
  2580. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  2581. (let ((case-fold-search t)
  2582. (errors ""))
  2583. (dolist (latex-error org-latex-known-errors)
  2584. (when (save-excursion (re-search-forward (car latex-error) nil t))
  2585. (setq errors (concat errors " " (cdr latex-error)))))
  2586. (and (org-string-nw-p errors) (org-trim errors)))))))
  2587. ;;;###autoload
  2588. (defun org-latex-publish-to-latex (plist filename pub-dir)
  2589. "Publish an Org file to LaTeX.
  2590. FILENAME is the filename of the Org file to be published. PLIST
  2591. is the property list for the given project. PUB-DIR is the
  2592. publishing directory.
  2593. Return output file name."
  2594. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  2595. ;;;###autoload
  2596. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  2597. "Publish an Org file to PDF (via LaTeX).
  2598. FILENAME is the filename of the Org file to be published. PLIST
  2599. is the property list for the given project. PUB-DIR is the
  2600. publishing directory.
  2601. Return output file name."
  2602. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  2603. ;; in working directory and then moved to publishing directory.
  2604. (org-publish-attachment
  2605. plist
  2606. (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
  2607. pub-dir))
  2608. (provide 'ox-latex)
  2609. ;; Local variables:
  2610. ;; generated-autoload-file: "org-loaddefs.el"
  2611. ;; End:
  2612. ;;; ox-latex.el ends here