ox-latex.el 110 KB

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