ox-latex.el 112 KB

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