ox-latex.el 116 KB

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