ox-latex.el 105 KB

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