ox-latex.el 110 KB

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