ox-latex.el 112 KB

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