ox-latex.el 112 KB

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