ox-latex.el 109 KB

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