ox-latex.el 106 KB

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