ox-latex.el 112 KB

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