ox-latex.el 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034
  1. ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
  2. ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; See Org manual for details.
  19. ;;; Code:
  20. (eval-when-compile (require 'cl))
  21. (require 'ox)
  22. (require 'ox-publish)
  23. (defvar org-latex-default-packages-alist)
  24. (defvar org-latex-packages-alist)
  25. (defvar orgtbl-exp-regexp)
  26. ;;; Define Back-End
  27. (org-export-define-backend 'latex
  28. '((bold . org-latex-bold)
  29. (center-block . org-latex-center-block)
  30. (clock . org-latex-clock)
  31. (code . org-latex-code)
  32. (comment . (lambda (&rest args) ""))
  33. (comment-block . (lambda (&rest args) ""))
  34. (drawer . org-latex-drawer)
  35. (dynamic-block . org-latex-dynamic-block)
  36. (entity . org-latex-entity)
  37. (example-block . org-latex-example-block)
  38. (export-block . org-latex-export-block)
  39. (export-snippet . org-latex-export-snippet)
  40. (fixed-width . org-latex-fixed-width)
  41. (footnote-definition . org-latex-footnote-definition)
  42. (footnote-reference . org-latex-footnote-reference)
  43. (headline . org-latex-headline)
  44. (horizontal-rule . org-latex-horizontal-rule)
  45. (inline-src-block . org-latex-inline-src-block)
  46. (inlinetask . org-latex-inlinetask)
  47. (italic . org-latex-italic)
  48. (item . org-latex-item)
  49. (keyword . org-latex-keyword)
  50. (latex-environment . org-latex-latex-environment)
  51. (latex-fragment . org-latex-latex-fragment)
  52. (line-break . org-latex-line-break)
  53. (link . org-latex-link)
  54. (node-property . org-latex-node-property)
  55. (paragraph . org-latex-paragraph)
  56. (plain-list . org-latex-plain-list)
  57. (plain-text . org-latex-plain-text)
  58. (planning . org-latex-planning)
  59. (property-drawer . org-latex-property-drawer)
  60. (quote-block . org-latex-quote-block)
  61. (quote-section . org-latex-quote-section)
  62. (radio-target . org-latex-radio-target)
  63. (section . org-latex-section)
  64. (special-block . org-latex-special-block)
  65. (src-block . org-latex-src-block)
  66. (statistics-cookie . org-latex-statistics-cookie)
  67. (strike-through . org-latex-strike-through)
  68. (subscript . org-latex-subscript)
  69. (superscript . org-latex-superscript)
  70. (table . org-latex-table)
  71. (table-cell . org-latex-table-cell)
  72. (table-row . org-latex-table-row)
  73. (target . org-latex-target)
  74. (template . org-latex-template)
  75. (timestamp . org-latex-timestamp)
  76. (underline . org-latex-underline)
  77. (verbatim . org-latex-verbatim)
  78. (verse-block . org-latex-verse-block)
  79. ;; Pseudo objects.
  80. (latex-math-block . org-latex-math-block))
  81. :export-block '("LATEX" "TEX")
  82. :menu-entry
  83. '(?l "Export to LaTeX"
  84. ((?L "As LaTeX buffer" org-latex-export-as-latex)
  85. (?l "As LaTeX file" org-latex-export-to-latex)
  86. (?p "As PDF file" org-latex-export-to-pdf)
  87. (?o "As PDF file and open"
  88. (lambda (a s v b)
  89. (if a (org-latex-export-to-pdf t s v b)
  90. (org-open-file (org-latex-export-to-pdf nil s v b)))))))
  91. :options-alist '((:date "DATE" nil "\\today" t)
  92. (:latex-class "LATEX_CLASS" nil org-latex-default-class t)
  93. (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
  94. (:latex-header "LATEX_HEADER" nil nil newline)
  95. (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
  96. (:latex-hyperref-p nil "texht" org-latex-with-hyperref t))
  97. :filters-alist '((:filter-options . org-latex-math-block-options-filter)
  98. (:filter-parse-tree . org-latex-math-block-tree-filter)))
  99. ;;; Internal Variables
  100. (defconst org-latex-babel-language-alist
  101. '(("af" . "afrikaans")
  102. ("bg" . "bulgarian")
  103. ("bt-br" . "brazilian")
  104. ("ca" . "catalan")
  105. ("cs" . "czech")
  106. ("cy" . "welsh")
  107. ("da" . "danish")
  108. ("de" . "germanb")
  109. ("de-at" . "naustrian")
  110. ("de-de" . "ngerman")
  111. ("el" . "greek")
  112. ("en" . "english")
  113. ("en-au" . "australian")
  114. ("en-ca" . "canadian")
  115. ("en-gb" . "british")
  116. ("en-ie" . "irish")
  117. ("en-nz" . "newzealand")
  118. ("en-us" . "american")
  119. ("es" . "spanish")
  120. ("et" . "estonian")
  121. ("eu" . "basque")
  122. ("fi" . "finnish")
  123. ("fr" . "frenchb")
  124. ("fr-ca" . "canadien")
  125. ("gl" . "galician")
  126. ("hr" . "croatian")
  127. ("hu" . "hungarian")
  128. ("id" . "indonesian")
  129. ("is" . "icelandic")
  130. ("it" . "italian")
  131. ("la" . "latin")
  132. ("ms" . "malay")
  133. ("nl" . "dutch")
  134. ("nb" . "norsk")
  135. ("nn" . "nynorsk")
  136. ("no" . "norsk")
  137. ("pl" . "polish")
  138. ("pt" . "portuguese")
  139. ("ro" . "romanian")
  140. ("ru" . "russian")
  141. ("sa" . "sanskrit")
  142. ("sb" . "uppersorbian")
  143. ("sk" . "slovak")
  144. ("sl" . "slovene")
  145. ("sq" . "albanian")
  146. ("sr" . "serbian")
  147. ("sv" . "swedish")
  148. ("ta" . "tamil")
  149. ("tr" . "turkish")
  150. ("uk" . "ukrainian"))
  151. "Alist between language code and corresponding Babel option.")
  152. (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
  153. ("qbordermatrix" . "\\cr")
  154. ("kbordermatrix" . "\\\\"))
  155. "Alist between matrix macros and their row ending.")
  156. (defconst org-latex-pseudo-objects '(latex-math-block)
  157. "List of pseudo-object types introduced in the back-end.")
  158. ;;; User Configurable Variables
  159. (defgroup org-export-latex nil
  160. "Options for exporting Org mode files to LaTeX."
  161. :tag "Org Export LaTeX"
  162. :group 'org-export)
  163. ;;;; Preamble
  164. (defcustom org-latex-default-class "article"
  165. "The default LaTeX class."
  166. :group 'org-export-latex
  167. :type '(string :tag "LaTeX class"))
  168. (defcustom org-latex-classes
  169. '(("article"
  170. "\\documentclass[11pt]{article}"
  171. ("\\section{%s}" . "\\section*{%s}")
  172. ("\\subsection{%s}" . "\\subsection*{%s}")
  173. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  174. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  175. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  176. ("report"
  177. "\\documentclass[11pt]{report}"
  178. ("\\part{%s}" . "\\part*{%s}")
  179. ("\\chapter{%s}" . "\\chapter*{%s}")
  180. ("\\section{%s}" . "\\section*{%s}")
  181. ("\\subsection{%s}" . "\\subsection*{%s}")
  182. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  183. ("book"
  184. "\\documentclass[11pt]{book}"
  185. ("\\part{%s}" . "\\part*{%s}")
  186. ("\\chapter{%s}" . "\\chapter*{%s}")
  187. ("\\section{%s}" . "\\section*{%s}")
  188. ("\\subsection{%s}" . "\\subsection*{%s}")
  189. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  190. "Alist of LaTeX classes and associated header and structure.
  191. If #+LATEX_CLASS is set in the buffer, use its value and the
  192. associated information. Here is the structure of each cell:
  193. \(class-name
  194. header-string
  195. \(numbered-section . unnumbered-section)
  196. ...)
  197. The header string
  198. -----------------
  199. The HEADER-STRING is the header that will be inserted into the
  200. LaTeX file. It should contain the \\documentclass macro, and
  201. anything else that is needed for this setup. To this header, the
  202. following commands will be added:
  203. - Calls to \\usepackage for all packages mentioned in the
  204. variables `org-latex-default-packages-alist' and
  205. `org-latex-packages-alist'. Thus, your header definitions
  206. should avoid to also request these packages.
  207. - Lines specified via \"#+LATEX_HEADER:\" and
  208. \"#+LATEX_HEADER_EXTRA:\" keywords.
  209. If you need more control about the sequence in which the header
  210. is built up, or if you want to exclude one of these building
  211. blocks for a particular class, you can use the following
  212. macro-like placeholders.
  213. [DEFAULT-PACKAGES] \\usepackage statements for default packages
  214. [NO-DEFAULT-PACKAGES] do not include any of the default packages
  215. [PACKAGES] \\usepackage statements for packages
  216. [NO-PACKAGES] do not include the packages
  217. [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
  218. [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
  219. So a header like
  220. \\documentclass{article}
  221. [NO-DEFAULT-PACKAGES]
  222. [EXTRA]
  223. \\providecommand{\\alert}[1]{\\textbf{#1}}
  224. [PACKAGES]
  225. will omit the default packages, and will include the
  226. #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
  227. to \\providecommand, and then place \\usepackage commands based
  228. on the content of `org-latex-packages-alist'.
  229. If your header, `org-latex-default-packages-alist' or
  230. `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
  231. AUTO will automatically be replaced with a coding system derived
  232. from `buffer-file-coding-system'. See also the variable
  233. `org-latex-inputenc-alist' for a way to influence this mechanism.
  234. Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
  235. AUTO will be replaced with the language related to the language
  236. code specified by `org-export-default-language', which see. Note
  237. that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
  238. are permitted.
  239. The sectioning structure
  240. ------------------------
  241. The sectioning structure of the class is given by the elements
  242. following the header string. For each sectioning level, a number
  243. of strings is specified. A %s formatter is mandatory in each
  244. section string and will be replaced by the title of the section.
  245. Instead of a cons cell (numbered . unnumbered), you can also
  246. provide a list of 2 or 4 elements,
  247. \(numbered-open numbered-close)
  248. or
  249. \(numbered-open numbered-close unnumbered-open unnumbered-close)
  250. providing opening and closing strings for a LaTeX environment
  251. that should represent the document section. The opening clause
  252. should have a %s to represent the section title.
  253. Instead of a list of sectioning commands, you can also specify
  254. a function name. That function will be called with two
  255. parameters, the (reduced) level of the headline, and a predicate
  256. non-nil when the headline should be numbered. It must return
  257. a format string in which the section title will be added."
  258. :group 'org-export-latex
  259. :type '(repeat
  260. (list (string :tag "LaTeX class")
  261. (string :tag "LaTeX header")
  262. (repeat :tag "Levels" :inline t
  263. (choice
  264. (cons :tag "Heading"
  265. (string :tag " numbered")
  266. (string :tag "unnumbered"))
  267. (list :tag "Environment"
  268. (string :tag "Opening (numbered)")
  269. (string :tag "Closing (numbered)")
  270. (string :tag "Opening (unnumbered)")
  271. (string :tag "Closing (unnumbered)"))
  272. (function :tag "Hook computing sectioning"))))))
  273. (defcustom org-latex-inputenc-alist nil
  274. "Alist of inputenc coding system names, and what should really be used.
  275. For example, adding an entry
  276. (\"utf8\" . \"utf8x\")
  277. will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
  278. are written as utf8 files."
  279. :group 'org-export-latex
  280. :type '(repeat
  281. (cons
  282. (string :tag "Derived from buffer")
  283. (string :tag "Use this instead"))))
  284. (defcustom org-latex-title-command "\\maketitle"
  285. "The command used to insert the title just after \\begin{document}.
  286. If this string contains the formatting specification \"%s\" then
  287. it will be used as a formatting string, passing the title as an
  288. argument."
  289. :group 'org-export-latex
  290. :type 'string)
  291. (defcustom org-latex-toc-command "\\tableofcontents\n\n"
  292. "LaTeX command to set the table of contents, list of figures, etc.
  293. This command only applies to the table of contents generated with
  294. the toc:nil option, not to those generated with #+TOC keyword."
  295. :group 'org-export-latex
  296. :type 'string)
  297. (defcustom org-latex-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"
  1172. (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
  1173. "\\relax")
  1174. " " full-text "\n"
  1175. headline-label
  1176. pre-blanks
  1177. contents)))
  1178. ;; If headline is not the last sibling simply return
  1179. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
  1180. ;; any blank line.
  1181. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1182. (replace-regexp-in-string
  1183. "[ \t\n]*\\'"
  1184. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1185. low-level-body)))
  1186. ;; This is a standard headline. Export it as a section. Add
  1187. ;; an alternative heading when possible, and when this is not
  1188. ;; identical to the usual heading.
  1189. (let ((opt-title
  1190. (funcall org-latex-format-headline-function
  1191. todo todo-type priority
  1192. (org-export-data
  1193. (org-export-get-alt-title headline info) info)
  1194. (and (eq (plist-get info :with-tags) t) tags))))
  1195. (if (and numberedp opt-title
  1196. (not (equal opt-title full-text))
  1197. (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
  1198. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  1199. ;; Replace square brackets with parenthesis
  1200. ;; since square brackets are not supported in
  1201. ;; optional arguments.
  1202. (replace-regexp-in-string
  1203. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  1204. full-text
  1205. (concat headline-label pre-blanks contents))
  1206. ;; Impossible to add an alternative heading. Fallback to
  1207. ;; regular sectioning format string.
  1208. (format section-fmt full-text
  1209. (concat headline-label pre-blanks contents))))))))
  1210. (defun org-latex-format-headline-default-function
  1211. (todo todo-type priority text tags)
  1212. "Default format function for a headline.
  1213. See `org-latex-format-headline-function' for details."
  1214. (concat
  1215. (and todo (format "{\\bfseries\\sffamily %s} " todo))
  1216. (and priority (format "\\framebox{\\#%c} " priority))
  1217. text
  1218. (and tags
  1219. (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
  1220. ;;;; Horizontal Rule
  1221. (defun org-latex-horizontal-rule (horizontal-rule contents info)
  1222. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1223. CONTENTS is nil. INFO is a plist holding contextual information."
  1224. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1225. (prev (org-export-get-previous-element horizontal-rule info)))
  1226. (concat
  1227. ;; Make sure the rule doesn't start at the end of the current
  1228. ;; line by separating it with a blank line from previous element.
  1229. (when (and prev
  1230. (let ((prev-blank (org-element-property :post-blank prev)))
  1231. (or (not prev-blank) (zerop prev-blank))))
  1232. "\n")
  1233. (org-latex--wrap-label
  1234. horizontal-rule
  1235. (format "\\rule{%s}{%s}"
  1236. (or (plist-get attr :width) "\\linewidth")
  1237. (or (plist-get attr :thickness) "0.5pt"))))))
  1238. ;;;; Inline Src Block
  1239. (defun org-latex-inline-src-block (inline-src-block contents info)
  1240. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1241. CONTENTS holds the contents of the item. INFO is a plist holding
  1242. contextual information."
  1243. (let* ((code (org-element-property :value inline-src-block))
  1244. (separator (org-latex--find-verb-separator code)))
  1245. (cond
  1246. ;; Do not use a special package: transcode it verbatim.
  1247. ((not org-latex-listings)
  1248. (concat "\\verb" separator code separator))
  1249. ;; Use minted package.
  1250. ((eq org-latex-listings 'minted)
  1251. (let* ((org-lang (org-element-property :language inline-src-block))
  1252. (mint-lang (or (cadr (assq (intern org-lang)
  1253. org-latex-minted-langs))
  1254. org-lang))
  1255. (options (org-latex--make-option-string
  1256. org-latex-minted-options)))
  1257. (concat (format "\\mint%s{%s}"
  1258. (if (string= options "") "" (format "[%s]" options))
  1259. mint-lang)
  1260. separator code separator)))
  1261. ;; Use listings package.
  1262. (t
  1263. ;; Maybe translate language's name.
  1264. (let* ((org-lang (org-element-property :language inline-src-block))
  1265. (lst-lang (or (cadr (assq (intern org-lang)
  1266. org-latex-listings-langs))
  1267. org-lang))
  1268. (options (org-latex--make-option-string
  1269. (append org-latex-listings-options
  1270. `(("language" ,lst-lang))))))
  1271. (concat (format "\\lstinline[%s]" options)
  1272. separator code separator))))))
  1273. ;;;; Inlinetask
  1274. (defun org-latex-inlinetask (inlinetask contents info)
  1275. "Transcode an INLINETASK element from Org to LaTeX.
  1276. CONTENTS holds the contents of the block. INFO is a plist
  1277. holding contextual information."
  1278. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1279. (todo (and (plist-get info :with-todo-keywords)
  1280. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1281. (and todo (org-export-data todo info)))))
  1282. (todo-type (org-element-property :todo-type inlinetask))
  1283. (tags (and (plist-get info :with-tags)
  1284. (org-export-get-tags inlinetask info)))
  1285. (priority (and (plist-get info :with-priority)
  1286. (org-element-property :priority inlinetask))))
  1287. ;; If `org-latex-format-inlinetask-function' is provided, call it
  1288. ;; with appropriate arguments.
  1289. (if (not (eq org-latex-format-inlinetask-function 'ignore))
  1290. (funcall org-latex-format-inlinetask-function
  1291. todo todo-type priority title tags contents)
  1292. ;; Otherwise, use a default template.
  1293. (org-latex--wrap-label
  1294. inlinetask
  1295. (let ((full-title
  1296. (concat
  1297. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1298. (when priority (format "\\framebox{\\#%c} " priority))
  1299. title
  1300. (when tags (format "\\hfill{}\\textsc{:%s:}"
  1301. (mapconcat 'identity tags ":"))))))
  1302. (format (concat "\\begin{center}\n"
  1303. "\\fbox{\n"
  1304. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1305. "%s\n\n"
  1306. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  1307. "%s"
  1308. "\\end{minipage}\n"
  1309. "}\n"
  1310. "\\end{center}")
  1311. full-title contents))))))
  1312. ;;;; Italic
  1313. (defun org-latex-italic (italic contents info)
  1314. "Transcode ITALIC from Org to LaTeX.
  1315. CONTENTS is the text with italic markup. INFO is a plist holding
  1316. contextual information."
  1317. (org-latex--text-markup contents 'italic))
  1318. ;;;; Item
  1319. (defun org-latex-item (item contents info)
  1320. "Transcode an ITEM element from Org to LaTeX.
  1321. CONTENTS holds the contents of the item. INFO is a plist holding
  1322. contextual information."
  1323. (let* ((counter
  1324. (let ((count (org-element-property :counter item))
  1325. (level
  1326. ;; Determine level of current item to determine the
  1327. ;; correct LaTeX counter to use (enumi, enumii...).
  1328. (let ((parent item) (level 0))
  1329. (while (memq (org-element-type
  1330. (setq parent (org-export-get-parent parent)))
  1331. '(plain-list item))
  1332. (when (and (eq (org-element-type parent) 'plain-list)
  1333. (eq (org-element-property :type parent)
  1334. 'ordered))
  1335. (incf level)))
  1336. level)))
  1337. (and count
  1338. (< level 5)
  1339. (format "\\setcounter{enum%s}{%s}\n"
  1340. (nth (1- level) '("i" "ii" "iii" "iv"))
  1341. (1- count)))))
  1342. (checkbox (case (org-element-property :checkbox item)
  1343. (on "$\\boxtimes$ ")
  1344. (off "$\\square$ ")
  1345. (trans "$\\boxminus$ ")))
  1346. (tag (let ((tag (org-element-property :tag item)))
  1347. ;; Check-boxes must belong to the tag.
  1348. (and tag (format "[{%s}] "
  1349. (concat checkbox
  1350. (org-export-data tag info)))))))
  1351. (concat counter
  1352. "\\item"
  1353. (cond
  1354. (tag)
  1355. (checkbox (concat " " checkbox))
  1356. ;; Without a tag or a check-box, if CONTENTS starts with
  1357. ;; an opening square bracket, add "\relax" to "\item",
  1358. ;; unless the brackets comes from an initial export
  1359. ;; snippet (i.e. it is inserted willingly by the user).
  1360. ((and contents
  1361. (org-string-match-p "\\`[ \t]*\\[" contents)
  1362. (not (let ((e (car (org-element-contents item))))
  1363. (and (eq (org-element-type e) 'paragraph)
  1364. (let ((o (car (org-element-contents e))))
  1365. (and (eq (org-element-type o) 'export-snippet)
  1366. (eq (org-export-snippet-backend o)
  1367. 'latex)))))))
  1368. "\\relax ")
  1369. (t " "))
  1370. (and contents (org-trim contents))
  1371. ;; If there are footnotes references in tag, be sure to
  1372. ;; add their definition at the end of the item. This
  1373. ;; workaround is necessary since "\footnote{}" command is
  1374. ;; not supported in tags.
  1375. (and tag
  1376. (org-latex--delayed-footnotes-definitions
  1377. (org-element-property :tag item) info)))))
  1378. ;;;; Keyword
  1379. (defun org-latex-keyword (keyword contents info)
  1380. "Transcode a KEYWORD element from Org to LaTeX.
  1381. CONTENTS is nil. INFO is a plist holding contextual information."
  1382. (let ((key (org-element-property :key keyword))
  1383. (value (org-element-property :value keyword)))
  1384. (cond
  1385. ((string= key "LATEX") value)
  1386. ((string= key "INDEX") (format "\\index{%s}" value))
  1387. ((string= key "TOC")
  1388. (let ((value (downcase value)))
  1389. (cond
  1390. ((string-match "\\<headlines\\>" value)
  1391. (let ((depth (or (and (string-match "[0-9]+" value)
  1392. (string-to-number (match-string 0 value)))
  1393. (plist-get info :with-toc))))
  1394. (concat
  1395. (when (wholenump depth)
  1396. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1397. "\\tableofcontents")))
  1398. ((string= "tables" value) "\\listoftables")
  1399. ((string= "listings" value)
  1400. (cond
  1401. ((eq org-latex-listings 'minted) "\\listoflistings")
  1402. (org-latex-listings "\\lstlistoflistings")
  1403. ;; At the moment, src blocks with a caption are wrapped
  1404. ;; into a figure environment.
  1405. (t "\\listoffigures")))))))))
  1406. ;;;; Latex Environment
  1407. (defun org-latex-latex-environment (latex-environment contents info)
  1408. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1409. CONTENTS is nil. INFO is a plist holding contextual information."
  1410. (when (plist-get info :with-latex)
  1411. (let ((label (org-element-property :name latex-environment))
  1412. (value (org-remove-indentation
  1413. (org-element-property :value latex-environment))))
  1414. (if (not (org-string-nw-p label)) value
  1415. ;; Environment is labeled: label must be within the environment
  1416. ;; (otherwise, a reference pointing to that element will count
  1417. ;; the section instead).
  1418. (with-temp-buffer
  1419. (insert value)
  1420. (goto-char (point-min))
  1421. (forward-line)
  1422. (insert
  1423. (format "\\label{%s}\n" (org-export-solidify-link-text label)))
  1424. (buffer-string))))))
  1425. ;;;; Latex Fragment
  1426. (defun org-latex-latex-fragment (latex-fragment contents info)
  1427. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1428. CONTENTS is nil. INFO is a plist holding contextual information."
  1429. (let ((value (org-element-property :value latex-fragment)))
  1430. ;; Trim math markers since the fragment is enclosed within
  1431. ;; a latex-math-block object anyway.
  1432. (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
  1433. (match-string 2 value))
  1434. ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
  1435. (match-string 1 value))
  1436. (t value))))
  1437. ;;;; Line Break
  1438. (defun org-latex-line-break (line-break contents info)
  1439. "Transcode a LINE-BREAK object from Org to LaTeX.
  1440. CONTENTS is nil. INFO is a plist holding contextual information."
  1441. "\\\\\n")
  1442. ;;;; Link
  1443. (defun org-latex--inline-image (link info)
  1444. "Return LaTeX code for an inline image.
  1445. LINK is the link pointing to the inline image. INFO is a plist
  1446. used as a communication channel."
  1447. (let* ((parent (org-export-get-parent-element link))
  1448. (path (let ((raw-path (org-element-property :path link)))
  1449. (if (not (file-name-absolute-p raw-path)) raw-path
  1450. (expand-file-name raw-path))))
  1451. (filetype (file-name-extension path))
  1452. (caption (org-latex--caption/label-string parent info))
  1453. ;; Retrieve latex attributes from the element around.
  1454. (attr (org-export-read-attribute :attr_latex parent))
  1455. (float (let ((float (plist-get attr :float)))
  1456. (cond ((and (not float) (plist-member attr :float)) nil)
  1457. ((string= float "wrap") 'wrap)
  1458. ((string= float "multicolumn") 'multicolumn)
  1459. ((or float
  1460. (org-element-property :caption parent)
  1461. (org-string-nw-p (plist-get attr :caption)))
  1462. 'figure))))
  1463. (placement
  1464. (let ((place (plist-get attr :placement)))
  1465. (cond (place (format "%s" place))
  1466. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  1467. ((eq float 'figure)
  1468. (format "[%s]" org-latex-default-figure-position))
  1469. (t ""))))
  1470. (comment-include (if (plist-get attr :comment-include) "%" ""))
  1471. ;; It is possible to specify width and height in the
  1472. ;; ATTR_LATEX line, and also via default variables.
  1473. (width (cond ((plist-get attr :width))
  1474. ((plist-get attr :height) "")
  1475. ((eq float 'wrap) "0.48\\textwidth")
  1476. (t org-latex-image-default-width)))
  1477. (height (cond ((plist-get attr :height))
  1478. ((or (plist-get attr :width)
  1479. (memq float '(figure wrap))) "")
  1480. (t org-latex-image-default-height)))
  1481. (options (let ((opt (or (plist-get attr :options)
  1482. org-latex-image-default-option)))
  1483. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  1484. (match-string 1 opt))))
  1485. image-code)
  1486. (if (member filetype '("tikz" "pgf"))
  1487. ;; For tikz images:
  1488. ;; - use \input to read in image file.
  1489. ;; - if options are present, wrap in a tikzpicture environment.
  1490. ;; - if width or height are present, use \resizebox to change
  1491. ;; the image size.
  1492. (progn
  1493. (setq image-code (format "\\input{%s}" path))
  1494. (when (org-string-nw-p options)
  1495. (setq image-code
  1496. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  1497. options
  1498. image-code)))
  1499. (when (or (org-string-nw-p width) (org-string-nw-p height))
  1500. (setq image-code (format "\\resizebox{%s}{%s}{%s}"
  1501. (if (org-string-nw-p width) width "!")
  1502. (if (org-string-nw-p height) height "!")
  1503. image-code))))
  1504. ;; For other images:
  1505. ;; - add width and height to options.
  1506. ;; - include the image with \includegraphics.
  1507. (when (org-string-nw-p width)
  1508. (setq options (concat options ",width=" width)))
  1509. (when (org-string-nw-p height)
  1510. (setq options (concat options ",height=" height)))
  1511. (setq image-code
  1512. (format "\\includegraphics%s{%s}"
  1513. (cond ((not (org-string-nw-p options)) "")
  1514. ((= (aref options 0) ?,)
  1515. (format "[%s]"(substring options 1)))
  1516. (t (format "[%s]" options)))
  1517. path))
  1518. (when (equal filetype "svg")
  1519. (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
  1520. "\\includesvg"
  1521. image-code
  1522. nil t))
  1523. (setq image-code (replace-regexp-in-string "\\.svg}"
  1524. "}"
  1525. image-code
  1526. nil t))))
  1527. ;; Return proper string, depending on FLOAT.
  1528. (case float
  1529. (wrap (format "\\begin{wrapfigure}%s
  1530. \\centering
  1531. %s%s
  1532. %s\\end{wrapfigure}" placement comment-include image-code caption))
  1533. (multicolumn (format "\\begin{figure*}%s
  1534. \\centering
  1535. %s%s
  1536. %s\\end{figure*}" placement comment-include image-code caption))
  1537. (figure (format "\\begin{figure}%s
  1538. \\centering
  1539. %s%s
  1540. %s\\end{figure}" placement comment-include image-code caption))
  1541. (otherwise image-code))))
  1542. (defun org-latex-link (link desc info)
  1543. "Transcode a LINK object from Org to LaTeX.
  1544. DESC is the description part of the link, or the empty string.
  1545. INFO is a plist holding contextual information. See
  1546. `org-export-data'."
  1547. (let* ((type (org-element-property :type link))
  1548. (raw-path (org-element-property :path link))
  1549. ;; Ensure DESC really exists, or set it to nil.
  1550. (desc (and (not (string= desc "")) desc))
  1551. (imagep (org-export-inline-image-p
  1552. link org-latex-inline-image-rules))
  1553. (path (cond
  1554. ((member type '("http" "https" "ftp" "mailto"))
  1555. (concat type ":" raw-path))
  1556. ((string= type "file")
  1557. (if (not (file-name-absolute-p raw-path)) raw-path
  1558. (concat "file://" (expand-file-name raw-path))))
  1559. (t raw-path)))
  1560. protocol)
  1561. (cond
  1562. ;; Image file.
  1563. (imagep (org-latex--inline-image link info))
  1564. ;; Radio link: Transcode target's contents and use them as link's
  1565. ;; description.
  1566. ((string= type "radio")
  1567. (let ((destination (org-export-resolve-radio-link link info)))
  1568. (when destination
  1569. (format "\\hyperref[%s]{%s}"
  1570. (org-export-solidify-link-text path)
  1571. (org-export-data (org-element-contents destination) info)))))
  1572. ;; Links pointing to a headline: Find destination and build
  1573. ;; appropriate referencing command.
  1574. ((member type '("custom-id" "fuzzy" "id"))
  1575. (let ((destination (if (string= type "fuzzy")
  1576. (org-export-resolve-fuzzy-link link info)
  1577. (org-export-resolve-id-link link info))))
  1578. (case (org-element-type destination)
  1579. ;; Id link points to an external file.
  1580. (plain-text
  1581. (if desc (format "\\href{%s}{%s}" destination desc)
  1582. (format "\\url{%s}" destination)))
  1583. ;; Fuzzy link points nowhere.
  1584. ('nil
  1585. (format org-latex-link-with-unknown-path-format
  1586. (or desc
  1587. (org-export-data
  1588. (org-element-property :raw-link link) info))))
  1589. ;; LINK points to a headline. If headlines are numbered
  1590. ;; and the link has no description, display headline's
  1591. ;; number. Otherwise, display description or headline's
  1592. ;; title.
  1593. (headline
  1594. (let ((label
  1595. (format "sec-%s"
  1596. (mapconcat
  1597. 'number-to-string
  1598. (org-export-get-headline-number destination info)
  1599. "-"))))
  1600. (if (and (plist-get info :section-numbers) (not desc))
  1601. (format "\\ref{%s}" label)
  1602. (format "\\hyperref[%s]{%s}" label
  1603. (or desc
  1604. (org-export-data
  1605. (org-element-property :title destination) info))))))
  1606. ;; Fuzzy link points to a target. Do as above.
  1607. (otherwise
  1608. (let ((path (org-export-solidify-link-text path)))
  1609. (if (not desc) (format "\\ref{%s}" path)
  1610. (format "\\hyperref[%s]{%s}" path desc)))))))
  1611. ;; Coderef: replace link with the reference name or the
  1612. ;; equivalent line number.
  1613. ((string= type "coderef")
  1614. (format (org-export-get-coderef-format path desc)
  1615. (org-export-resolve-coderef path info)))
  1616. ;; Link type is handled by a special function.
  1617. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1618. (funcall protocol (org-link-unescape path) desc 'latex))
  1619. ;; External link with a description part.
  1620. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1621. ;; External link without a description part.
  1622. (path (format "\\url{%s}" path))
  1623. ;; No path, only description. Try to do something useful.
  1624. (t (format org-latex-link-with-unknown-path-format desc)))))
  1625. ;;;; Node Property
  1626. (defun org-latex-node-property (node-property contents info)
  1627. "Transcode a NODE-PROPERTY element from Org to LaTeX.
  1628. CONTENTS is nil. INFO is a plist holding contextual
  1629. information."
  1630. (format "%s:%s"
  1631. (org-element-property :key node-property)
  1632. (let ((value (org-element-property :value node-property)))
  1633. (if value (concat " " value) ""))))
  1634. ;;;; Paragraph
  1635. (defun org-latex-paragraph (paragraph contents info)
  1636. "Transcode a PARAGRAPH element from Org to LaTeX.
  1637. CONTENTS is the contents of the paragraph, as a string. INFO is
  1638. the plist used as a communication channel."
  1639. contents)
  1640. ;;;; Plain List
  1641. (defun org-latex-plain-list (plain-list contents info)
  1642. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1643. CONTENTS is the contents of the list. INFO is a plist holding
  1644. contextual information."
  1645. (let* ((type (org-element-property :type plain-list))
  1646. (attr (org-export-read-attribute :attr_latex plain-list))
  1647. (latex-type (let ((env (plist-get attr :environment)))
  1648. (cond (env (format "%s" env))
  1649. ((eq type 'ordered) "enumerate")
  1650. ((eq type 'descriptive) "description")
  1651. (t "itemize")))))
  1652. (org-latex--wrap-label
  1653. plain-list
  1654. (format "\\begin{%s}%s\n%s\\end{%s}"
  1655. latex-type
  1656. (or (plist-get attr :options) "")
  1657. contents
  1658. latex-type))))
  1659. ;;;; Plain Text
  1660. (defun org-latex-plain-text (text info)
  1661. "Transcode a TEXT string from Org to LaTeX.
  1662. TEXT is the string to transcode. INFO is a plist holding
  1663. contextual information."
  1664. (let ((specialp (plist-get info :with-special-strings))
  1665. (output text))
  1666. ;; Protect %, #, &, $, _, { and }.
  1667. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
  1668. (setq output
  1669. (replace-match
  1670. (format "\\%s" (match-string 2 output)) nil t output 2)))
  1671. ;; Protect ^.
  1672. (setq output
  1673. (replace-regexp-in-string
  1674. "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
  1675. ;; Protect \. If special strings are used, be careful not to
  1676. ;; protect "\" in "\-" constructs.
  1677. (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
  1678. (setq output
  1679. (replace-regexp-in-string
  1680. (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
  1681. "$\\backslash$" output nil t 1)))
  1682. ;; Protect ~.
  1683. (setq output
  1684. (replace-regexp-in-string
  1685. "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
  1686. ;; Activate smart quotes. Be sure to provide original TEXT string
  1687. ;; since OUTPUT may have been modified.
  1688. (when (plist-get info :with-smart-quotes)
  1689. (setq output (org-export-activate-smart-quotes output :latex info text)))
  1690. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1691. (let ((case-fold-search nil)
  1692. (start 0))
  1693. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
  1694. (setq output (replace-match
  1695. (format "\\%s{}" (match-string 1 output)) nil t output)
  1696. start (match-end 0))))
  1697. ;; Convert special strings.
  1698. (when specialp
  1699. (setq output
  1700. (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
  1701. ;; Handle break preservation if required.
  1702. (when (plist-get info :preserve-breaks)
  1703. (setq output (replace-regexp-in-string
  1704. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
  1705. ;; Return value.
  1706. output))
  1707. ;;;; Planning
  1708. (defun org-latex-planning (planning contents info)
  1709. "Transcode a PLANNING element from Org to LaTeX.
  1710. CONTENTS is nil. INFO is a plist holding contextual
  1711. information."
  1712. (concat
  1713. "\\noindent"
  1714. (mapconcat
  1715. 'identity
  1716. (delq nil
  1717. (list
  1718. (let ((closed (org-element-property :closed planning)))
  1719. (when closed
  1720. (concat
  1721. (format "\\textbf{%s} " org-closed-string)
  1722. (format org-latex-inactive-timestamp-format
  1723. (org-translate-time
  1724. (org-element-property :raw-value closed))))))
  1725. (let ((deadline (org-element-property :deadline planning)))
  1726. (when deadline
  1727. (concat
  1728. (format "\\textbf{%s} " org-deadline-string)
  1729. (format org-latex-active-timestamp-format
  1730. (org-translate-time
  1731. (org-element-property :raw-value deadline))))))
  1732. (let ((scheduled (org-element-property :scheduled planning)))
  1733. (when scheduled
  1734. (concat
  1735. (format "\\textbf{%s} " org-scheduled-string)
  1736. (format org-latex-active-timestamp-format
  1737. (org-translate-time
  1738. (org-element-property :raw-value scheduled))))))))
  1739. " ")
  1740. "\\\\"))
  1741. ;;;; Property Drawer
  1742. (defun org-latex-property-drawer (property-drawer contents info)
  1743. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  1744. CONTENTS holds the contents of the drawer. INFO is a plist
  1745. holding contextual information."
  1746. (and (org-string-nw-p contents)
  1747. (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
  1748. ;;;; Pseudo Object: LaTeX Math Block
  1749. (defun org-latex--wrap-latex-math-block (data info)
  1750. "Merge contiguous math objects in a pseudo-object container.
  1751. DATA is a parse tree or a secondary string. INFO is a plist
  1752. containing export options. Modify DATA by side-effect and return it."
  1753. (let ((valid-object-p
  1754. (function
  1755. ;; Non-nil when OBJ can be added to the latex math block.
  1756. (lambda (obj)
  1757. (case (org-element-type obj)
  1758. (entity (org-element-property :latex-math-p obj))
  1759. (latex-fragment
  1760. (let ((value (org-element-property :value obj)))
  1761. (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
  1762. (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
  1763. ((subscript superscript) t))))))
  1764. (org-element-map data '(entity latex-fragment subscript superscript)
  1765. (lambda (object)
  1766. ;; Skip objects already wrapped.
  1767. (when (and (not (eq (org-element-type
  1768. (org-element-property :parent object))
  1769. 'latex-math-block))
  1770. (funcall valid-object-p object))
  1771. (let ((math-block (list 'latex-math-block nil))
  1772. (next-elements (org-export-get-next-element object info t))
  1773. (last object))
  1774. ;; Wrap MATH-BLOCK around OBJECT in DATA.
  1775. (org-element-insert-before math-block object)
  1776. (org-element-extract-element object)
  1777. (org-element-adopt-elements math-block object)
  1778. (when (zerop (or (org-element-property :post-blank object) 0))
  1779. ;; MATH-BLOCK swallows consecutive math objects.
  1780. (catch 'exit
  1781. (dolist (next next-elements)
  1782. (if (not (funcall valid-object-p next)) (throw 'exit nil)
  1783. (org-element-extract-element next)
  1784. (org-element-adopt-elements math-block next)
  1785. ;; Eschew the case: \beta$x$ -> \(\betax\).
  1786. (unless (memq (org-element-type next)
  1787. '(subscript superscript))
  1788. (org-element-put-property last :post-blank 1))
  1789. (setq last next)
  1790. (when (> (or (org-element-property :post-blank next) 0) 0)
  1791. (throw 'exit nil))))))
  1792. (org-element-put-property
  1793. math-block :post-blank (org-element-property :post-blank last)))))
  1794. info nil '(subscript superscript latex-math-block) t)
  1795. ;; Return updated DATA.
  1796. data))
  1797. (defun org-latex-math-block-tree-filter (tree backend info)
  1798. (org-latex--wrap-latex-math-block tree info))
  1799. (defun org-latex-math-block-options-filter (info backend)
  1800. (dolist (prop '(:author :date :title) info)
  1801. (plist-put info prop
  1802. (org-latex--wrap-latex-math-block (plist-get info prop) info))))
  1803. (defun org-latex-math-block (math-block contents info)
  1804. "Transcode a MATH-BLOCK object from Org to LaTeX.
  1805. CONTENTS is a string. INFO is a plist used as a communication
  1806. channel."
  1807. (when (org-string-nw-p contents)
  1808. (format "\\(%s\\)" (org-trim contents))))
  1809. ;;;; Quote Block
  1810. (defun org-latex-quote-block (quote-block contents info)
  1811. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1812. CONTENTS holds the contents of the block. INFO is a plist
  1813. holding contextual information."
  1814. (org-latex--wrap-label
  1815. quote-block
  1816. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1817. ;;;; Quote Section
  1818. (defun org-latex-quote-section (quote-section contents info)
  1819. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1820. CONTENTS is nil. INFO is a plist holding contextual information."
  1821. (let ((value (org-remove-indentation
  1822. (org-element-property :value quote-section))))
  1823. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1824. ;;;; Radio Target
  1825. (defun org-latex-radio-target (radio-target text info)
  1826. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1827. TEXT is the text of the target. INFO is a plist holding
  1828. contextual information."
  1829. (format "\\label{%s}%s"
  1830. (org-export-solidify-link-text
  1831. (org-element-property :value radio-target))
  1832. text))
  1833. ;;;; Section
  1834. (defun org-latex-section (section contents info)
  1835. "Transcode a SECTION element from Org to LaTeX.
  1836. CONTENTS holds the contents of the section. INFO is a plist
  1837. holding contextual information."
  1838. contents)
  1839. ;;;; Special Block
  1840. (defun org-latex-special-block (special-block contents info)
  1841. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1842. CONTENTS holds the contents of the block. INFO is a plist
  1843. holding contextual information."
  1844. (let ((type (downcase (org-element-property :type special-block)))
  1845. (opt (org-export-read-attribute :attr_latex special-block :options)))
  1846. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  1847. ;; Insert any label or caption within the block
  1848. ;; (otherwise, a reference pointing to that element will
  1849. ;; count the section instead).
  1850. (org-latex--caption/label-string special-block info)
  1851. contents
  1852. (format "\\end{%s}" type))))
  1853. ;;;; Src Block
  1854. (defun org-latex-src-block (src-block contents info)
  1855. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1856. CONTENTS holds the contents of the item. INFO is a plist holding
  1857. contextual information."
  1858. (when (org-string-nw-p (org-element-property :value src-block))
  1859. (let* ((lang (org-element-property :language src-block))
  1860. (caption (org-element-property :caption src-block))
  1861. (label (org-element-property :name src-block))
  1862. (custom-env (and lang
  1863. (cadr (assq (intern lang)
  1864. org-latex-custom-lang-environments))))
  1865. (num-start (case (org-element-property :number-lines src-block)
  1866. (continued (org-export-get-loc src-block info))
  1867. (new 0)))
  1868. (retain-labels (org-element-property :retain-labels src-block))
  1869. (attributes (org-export-read-attribute :attr_latex src-block))
  1870. (float (plist-get attributes :float)))
  1871. (cond
  1872. ;; Case 1. No source fontification.
  1873. ((not org-latex-listings)
  1874. (let* ((caption-str (org-latex--caption/label-string src-block info))
  1875. (float-env
  1876. (cond ((and (not float) (plist-member attributes :float)) "%s")
  1877. ((string= "multicolumn" float)
  1878. (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
  1879. org-latex-default-figure-position
  1880. caption-str))
  1881. ((or caption float)
  1882. (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
  1883. caption-str))
  1884. (t "%s"))))
  1885. (format
  1886. float-env
  1887. (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1888. (org-export-format-code-default src-block info))))))
  1889. ;; Case 2. Custom environment.
  1890. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1891. custom-env
  1892. (org-export-format-code-default src-block info)
  1893. custom-env))
  1894. ;; Case 3. Use minted package.
  1895. ((eq org-latex-listings 'minted)
  1896. (let* ((caption-str (org-latex--caption/label-string src-block info))
  1897. (float-env
  1898. (cond ((and (not float) (plist-member attributes :float)) "%s")
  1899. ((string= "multicolumn" float)
  1900. (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
  1901. caption-str))
  1902. ((or caption float)
  1903. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1904. caption-str))
  1905. (t "%s")))
  1906. (body
  1907. (format
  1908. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1909. ;; Options.
  1910. (org-latex--make-option-string
  1911. (if (or (not num-start)
  1912. (assoc "linenos" org-latex-minted-options))
  1913. org-latex-minted-options
  1914. (append
  1915. `(("linenos")
  1916. ("firstnumber" ,(number-to-string (1+ num-start))))
  1917. org-latex-minted-options)))
  1918. ;; Language.
  1919. (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
  1920. ;; Source code.
  1921. (let* ((code-info (org-export-unravel-code src-block))
  1922. (max-width
  1923. (apply 'max
  1924. (mapcar 'length
  1925. (org-split-string (car code-info)
  1926. "\n")))))
  1927. (org-export-format-code
  1928. (car code-info)
  1929. (lambda (loc num ref)
  1930. (concat
  1931. loc
  1932. (when ref
  1933. ;; Ensure references are flushed to the right,
  1934. ;; separated with 6 spaces from the widest line
  1935. ;; of code.
  1936. (concat (make-string (+ (- max-width (length loc)) 6)
  1937. ?\s)
  1938. (format "(%s)" ref)))))
  1939. nil (and retain-labels (cdr code-info)))))))
  1940. ;; Return value.
  1941. (format float-env body)))
  1942. ;; Case 4. Use listings package.
  1943. (t
  1944. (let ((lst-lang
  1945. (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
  1946. (caption-str
  1947. (when caption
  1948. (let ((main (org-export-get-caption src-block))
  1949. (secondary (org-export-get-caption src-block t)))
  1950. (if (not secondary)
  1951. (format "{%s}" (org-export-data main info))
  1952. (format "{[%s]%s}"
  1953. (org-export-data secondary info)
  1954. (org-export-data main info)))))))
  1955. (concat
  1956. ;; Options.
  1957. (format
  1958. "\\lstset{%s}\n"
  1959. (org-latex--make-option-string
  1960. (append
  1961. org-latex-listings-options
  1962. (cond
  1963. ((and (not float) (plist-member attributes :float)) nil)
  1964. ((string= "multicolumn" float) '(("float" "*")))
  1965. ((and float (not (assoc "float" org-latex-listings-options)))
  1966. `(("float" ,org-latex-default-figure-position))))
  1967. `(("language" ,lst-lang))
  1968. (when label `(("label" ,label)))
  1969. (when caption-str `(("caption" ,caption-str)))
  1970. (cond ((assoc "numbers" org-latex-listings-options) nil)
  1971. ((not num-start) '(("numbers" "none")))
  1972. ((zerop num-start) '(("numbers" "left")))
  1973. (t `(("numbers" "left")
  1974. ("firstnumber"
  1975. ,(number-to-string (1+ num-start)))))))))
  1976. ;; Source code.
  1977. (format
  1978. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1979. (let* ((code-info (org-export-unravel-code src-block))
  1980. (max-width
  1981. (apply 'max
  1982. (mapcar 'length
  1983. (org-split-string (car code-info) "\n")))))
  1984. (org-export-format-code
  1985. (car code-info)
  1986. (lambda (loc num ref)
  1987. (concat
  1988. loc
  1989. (when ref
  1990. ;; Ensure references are flushed to the right,
  1991. ;; separated with 6 spaces from the widest line of
  1992. ;; code
  1993. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1994. (format "(%s)" ref)))))
  1995. nil (and retain-labels (cdr code-info))))))))))))
  1996. ;;;; Statistics Cookie
  1997. (defun org-latex-statistics-cookie (statistics-cookie contents info)
  1998. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1999. CONTENTS is nil. INFO is a plist holding contextual information."
  2000. (replace-regexp-in-string
  2001. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  2002. ;;;; Strike-Through
  2003. (defun org-latex-strike-through (strike-through contents info)
  2004. "Transcode STRIKE-THROUGH from Org to LaTeX.
  2005. CONTENTS is the text with strike-through markup. INFO is a plist
  2006. holding contextual information."
  2007. (org-latex--text-markup contents 'strike-through))
  2008. ;;;; Subscript
  2009. (defun org-latex--script-size (object info)
  2010. "Transcode a subscript or superscript object.
  2011. OBJECT is an Org object. INFO is a plist used as a communication
  2012. channel."
  2013. (let ((type (org-element-type object))
  2014. (output ""))
  2015. (org-element-map (org-element-contents object)
  2016. (cons 'plain-text org-element-all-objects)
  2017. (lambda (obj)
  2018. (case (org-element-type obj)
  2019. ((entity latex-fragment)
  2020. (let ((data (org-trim (org-export-data obj info))))
  2021. (string-match
  2022. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  2023. data)
  2024. (setq output
  2025. (concat output
  2026. (match-string 1 data)
  2027. (let ((blank (org-element-property :post-blank obj)))
  2028. (and blank (> blank 0) "\\ "))))))
  2029. (plain-text
  2030. (setq output
  2031. (format "%s\\text{%s}" output (org-export-data obj info))))
  2032. (otherwise
  2033. (setq output
  2034. (concat output
  2035. (org-export-data obj info)
  2036. (let ((blank (org-element-property :post-blank obj)))
  2037. (and blank (> blank 0) "\\ ")))))))
  2038. info nil org-element-recursive-objects)
  2039. ;; Result. Do not wrap into curly brackets if OUTPUT is a single
  2040. ;; character.
  2041. (concat (if (eq (org-element-type object) 'subscript) "_" "^")
  2042. (and (> (length output) 1) "{")
  2043. output
  2044. (and (> (length output) 1) "}"))))
  2045. (defun org-latex-subscript (subscript contents info)
  2046. "Transcode a SUBSCRIPT object from Org to LaTeX.
  2047. CONTENTS is the contents of the object. INFO is a plist holding
  2048. contextual information."
  2049. (org-latex--script-size subscript info))
  2050. ;;;; Superscript
  2051. (defun org-latex-superscript (superscript contents info)
  2052. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  2053. CONTENTS is the contents of the object. INFO is a plist holding
  2054. contextual information."
  2055. (org-latex--script-size superscript info))
  2056. ;;;; Table
  2057. ;;
  2058. ;; `org-latex-table' is the entry point for table transcoding. It
  2059. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  2060. ;; delegates the job to either `org-latex--table.el-table',
  2061. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  2062. ;; depending of the type of the table and the mode requested.
  2063. ;;
  2064. ;; `org-latex--align-string' is a subroutine used to build alignment
  2065. ;; string for Org tables.
  2066. (defun org-latex-table (table contents info)
  2067. "Transcode a TABLE element from Org to LaTeX.
  2068. CONTENTS is the contents of the table. INFO is a plist holding
  2069. contextual information."
  2070. (if (eq (org-element-property :type table) 'table.el)
  2071. ;; "table.el" table. Convert it using appropriate tools.
  2072. (org-latex--table.el-table table info)
  2073. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  2074. org-latex-default-table-mode)))
  2075. (cond
  2076. ;; Case 1: Verbatim table.
  2077. ((string= type "verbatim")
  2078. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  2079. ;; Re-create table, without affiliated keywords.
  2080. (org-trim (org-element-interpret-data
  2081. `(table nil ,@(org-element-contents table))
  2082. org-latex-pseudo-objects))))
  2083. ;; Case 2: Matrix.
  2084. ((or (string= type "math") (string= type "inline-math"))
  2085. (org-latex--math-table table info))
  2086. ;; Case 3: Standard table.
  2087. (t (concat (org-latex--org-table table contents info)
  2088. ;; When there are footnote references within the
  2089. ;; table, insert their definition just after it.
  2090. (org-latex--delayed-footnotes-definitions table info)))))))
  2091. (defun org-latex--align-string (table info)
  2092. "Return an appropriate LaTeX alignment string.
  2093. TABLE is the considered table. INFO is a plist used as
  2094. a communication channel."
  2095. (or (org-export-read-attribute :attr_latex table :align)
  2096. (let (align)
  2097. ;; Extract column groups and alignment from first (non-rule)
  2098. ;; row.
  2099. (org-element-map
  2100. (org-element-map table 'table-row
  2101. (lambda (row)
  2102. (and (eq (org-element-property :type row) 'standard) row))
  2103. info 'first-match)
  2104. 'table-cell
  2105. (lambda (cell)
  2106. (let ((borders (org-export-table-cell-borders cell info)))
  2107. ;; Check left border for the first cell only.
  2108. (when (and (memq 'left borders) (not align))
  2109. (push "|" align))
  2110. (push (case (org-export-table-cell-alignment cell info)
  2111. (left "l")
  2112. (right "r")
  2113. (center "c"))
  2114. align)
  2115. (when (memq 'right borders) (push "|" align))))
  2116. info)
  2117. (apply 'concat (nreverse align)))))
  2118. (defun org-latex--org-table (table contents info)
  2119. "Return appropriate LaTeX code for an Org table.
  2120. TABLE is the table type element to transcode. CONTENTS is its
  2121. contents, as a string. INFO is a plist used as a communication
  2122. channel.
  2123. This function assumes TABLE has `org' as its `:type' property and
  2124. `table' as its `:mode' attribute."
  2125. (let* ((caption (org-latex--caption/label-string table info))
  2126. (attr (org-export-read-attribute :attr_latex table))
  2127. ;; Determine alignment string.
  2128. (alignment (org-latex--align-string table info))
  2129. ;; Determine environment for the table: longtable, tabular...
  2130. (table-env (or (plist-get attr :environment)
  2131. org-latex-default-table-environment))
  2132. ;; If table is a float, determine environment: table, table*
  2133. ;; or sidewaystable.
  2134. (float-env (unless (member table-env '("longtable" "longtabu"))
  2135. (let ((float (plist-get attr :float)))
  2136. (cond
  2137. ((and (not float) (plist-member attr :float)) nil)
  2138. ((string= float "sidewaystable") "sidewaystable")
  2139. ((string= float "multicolumn") "table*")
  2140. ((or float
  2141. (org-element-property :caption table)
  2142. (org-string-nw-p (plist-get attr :caption)))
  2143. "table")))))
  2144. ;; Extract others display options.
  2145. (fontsize (let ((font (plist-get attr :font)))
  2146. (and font (concat font "\n"))))
  2147. (width (plist-get attr :width))
  2148. (spreadp (plist-get attr :spread))
  2149. (placement (or (plist-get attr :placement)
  2150. (format "[%s]" org-latex-default-figure-position)))
  2151. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2152. org-latex-tables-centered)))
  2153. ;; Prepare the final format string for the table.
  2154. (cond
  2155. ;; Longtable.
  2156. ((equal "longtable" table-env)
  2157. (concat (and fontsize (concat "{" fontsize))
  2158. (format "\\begin{longtable}{%s}\n" alignment)
  2159. (and org-latex-table-caption-above
  2160. (org-string-nw-p caption)
  2161. (concat caption "\\\\\n"))
  2162. contents
  2163. (and (not org-latex-table-caption-above)
  2164. (org-string-nw-p caption)
  2165. (concat caption "\\\\\n"))
  2166. "\\end{longtable}\n"
  2167. (and fontsize "}")))
  2168. ;; Longtabu
  2169. ((equal "longtabu" table-env)
  2170. (concat (and fontsize (concat "{" fontsize))
  2171. (format "\\begin{longtabu}%s{%s}\n"
  2172. (if width
  2173. (format " %s %s "
  2174. (if spreadp "spread" "to") width) "")
  2175. alignment)
  2176. (and org-latex-table-caption-above
  2177. (org-string-nw-p caption)
  2178. (concat caption "\\\\\n"))
  2179. contents
  2180. (and (not org-latex-table-caption-above)
  2181. (org-string-nw-p caption)
  2182. (concat caption "\\\\\n"))
  2183. "\\end{longtabu}\n"
  2184. (and fontsize "}")))
  2185. ;; Others.
  2186. (t (concat (cond
  2187. (float-env
  2188. (concat (format "\\begin{%s}%s\n" float-env placement)
  2189. (if org-latex-table-caption-above caption "")
  2190. (when centerp "\\centering\n")
  2191. fontsize))
  2192. (centerp (concat "\\begin{center}\n" fontsize))
  2193. (fontsize (concat "{" fontsize)))
  2194. (cond ((equal "tabu" table-env)
  2195. (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
  2196. (if width (format
  2197. (if spreadp " spread %s " " to %s ")
  2198. width) "")
  2199. alignment
  2200. contents))
  2201. (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2202. table-env
  2203. (if width (format "{%s}" width) "")
  2204. alignment
  2205. contents
  2206. table-env)))
  2207. (cond
  2208. (float-env
  2209. (concat (if org-latex-table-caption-above "" caption)
  2210. (format "\n\\end{%s}" float-env)))
  2211. (centerp "\n\\end{center}")
  2212. (fontsize "}")))))))
  2213. (defun org-latex--table.el-table (table info)
  2214. "Return appropriate LaTeX code for a table.el table.
  2215. TABLE is the table type element to transcode. INFO is a plist
  2216. used as a communication channel.
  2217. This function assumes TABLE has `table.el' as its `:type'
  2218. property."
  2219. (require 'table)
  2220. ;; Ensure "*org-export-table*" buffer is empty.
  2221. (with-current-buffer (get-buffer-create "*org-export-table*")
  2222. (erase-buffer))
  2223. (let ((output (with-temp-buffer
  2224. (insert (org-element-property :value table))
  2225. (goto-char 1)
  2226. (re-search-forward "^[ \t]*|[^|]" nil t)
  2227. (table-generate-source 'latex "*org-export-table*")
  2228. (with-current-buffer "*org-export-table*"
  2229. (org-trim (buffer-string))))))
  2230. (kill-buffer (get-buffer "*org-export-table*"))
  2231. ;; Remove left out comments.
  2232. (while (string-match "^%.*\n" output)
  2233. (setq output (replace-match "" t t output)))
  2234. (let ((attr (org-export-read-attribute :attr_latex table)))
  2235. (when (plist-get attr :rmlines)
  2236. ;; When the "rmlines" attribute is provided, remove all hlines
  2237. ;; but the the one separating heading from the table body.
  2238. (let ((n 0) (pos 0))
  2239. (while (and (< (length output) pos)
  2240. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2241. (incf n)
  2242. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2243. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2244. org-latex-tables-centered)))
  2245. (if (not centerp) output
  2246. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2247. (defun org-latex--math-table (table info)
  2248. "Return appropriate LaTeX code for a matrix.
  2249. TABLE is the table type element to transcode. INFO is a plist
  2250. used as a communication channel.
  2251. This function assumes TABLE has `org' as its `:type' property and
  2252. `inline-math' or `math' as its `:mode' attribute.."
  2253. (let* ((caption (org-latex--caption/label-string table info))
  2254. (attr (org-export-read-attribute :attr_latex table))
  2255. (inlinep (equal (plist-get attr :mode) "inline-math"))
  2256. (env (or (plist-get attr :environment)
  2257. org-latex-default-table-environment))
  2258. (contents
  2259. (mapconcat
  2260. (lambda (row)
  2261. ;; Ignore horizontal rules.
  2262. (when (eq (org-element-property :type row) 'standard)
  2263. ;; Return each cell unmodified.
  2264. (concat
  2265. (mapconcat
  2266. (lambda (cell)
  2267. (substring
  2268. (org-element-interpret-data cell org-latex-pseudo-objects)
  2269. 0 -1))
  2270. (org-element-map row 'table-cell 'identity info) "&")
  2271. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2272. "\n")))
  2273. (org-element-map table 'table-row 'identity info) ""))
  2274. ;; Variables related to math clusters (contiguous math tables
  2275. ;; of the same type).
  2276. (mode (org-export-read-attribute :attr_latex table :mode))
  2277. (prev (org-export-get-previous-element table info))
  2278. (next (org-export-get-next-element table info))
  2279. (same-mode-p
  2280. (lambda (table)
  2281. ;; Non-nil when TABLE has the same mode as current table.
  2282. (string= (or (org-export-read-attribute :attr_latex table :mode)
  2283. org-latex-default-table-mode)
  2284. mode))))
  2285. (concat
  2286. ;; Opening string. If TABLE is in the middle of a table cluster,
  2287. ;; do not insert any.
  2288. (cond ((and prev
  2289. (eq (org-element-type prev) 'table)
  2290. (memq (org-element-property :post-blank prev) '(0 nil))
  2291. (funcall same-mode-p prev))
  2292. nil)
  2293. (inlinep "\\(")
  2294. ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
  2295. (t "\\["))
  2296. ;; Prefix.
  2297. (or (plist-get attr :math-prefix) "")
  2298. ;; Environment. Also treat special cases.
  2299. (cond ((equal env "array")
  2300. (let ((align (org-latex--align-string table info)))
  2301. (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
  2302. ((assoc env org-latex-table-matrix-macros)
  2303. (format "\\%s%s{\n%s}"
  2304. env
  2305. (or (plist-get attr :math-arguments) "")
  2306. contents))
  2307. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2308. ;; Suffix.
  2309. (or (plist-get attr :math-suffix) "")
  2310. ;; Closing string. If TABLE is in the middle of a table cluster,
  2311. ;; do not insert any. If it closes such a cluster, be sure to
  2312. ;; close the cluster with a string matching the opening string.
  2313. (cond ((and next
  2314. (eq (org-element-type next) 'table)
  2315. (memq (org-element-property :post-blank table) '(0 nil))
  2316. (funcall same-mode-p next))
  2317. nil)
  2318. (inlinep "\\)")
  2319. ;; Find cluster beginning to know which environment to use.
  2320. ((let ((cluster-beg table) prev)
  2321. (while (and (setq prev (org-export-get-previous-element
  2322. cluster-beg info))
  2323. (memq (org-element-property :post-blank prev)
  2324. '(0 nil))
  2325. (funcall same-mode-p prev))
  2326. (setq cluster-beg prev))
  2327. (and (or (org-element-property :caption cluster-beg)
  2328. (org-element-property :name cluster-beg))
  2329. "\n\\end{equation}")))
  2330. (t "\\]")))))
  2331. ;;;; Table Cell
  2332. (defun org-latex-table-cell (table-cell contents info)
  2333. "Transcode a TABLE-CELL element from Org to LaTeX.
  2334. CONTENTS is the cell contents. INFO is a plist used as
  2335. a communication channel."
  2336. (concat (if (and contents
  2337. org-latex-table-scientific-notation
  2338. (string-match orgtbl-exp-regexp contents))
  2339. ;; Use appropriate format string for scientific
  2340. ;; notation.
  2341. (format org-latex-table-scientific-notation
  2342. (match-string 1 contents)
  2343. (match-string 2 contents))
  2344. contents)
  2345. (when (org-export-get-next-element table-cell info) " & ")))
  2346. ;;;; Table Row
  2347. (defun org-latex-table-row (table-row contents info)
  2348. "Transcode a TABLE-ROW element from Org to LaTeX.
  2349. CONTENTS is the contents of the row. INFO is a plist used as
  2350. a communication channel."
  2351. ;; Rules are ignored since table separators are deduced from
  2352. ;; borders of the current row.
  2353. (when (eq (org-element-property :type table-row) 'standard)
  2354. (let* ((attr (org-export-read-attribute :attr_latex
  2355. (org-export-get-parent table-row)))
  2356. (longtablep (member (or (plist-get attr :environment)
  2357. org-latex-default-table-environment)
  2358. '("longtable" "longtabu")))
  2359. (booktabsp (if (plist-member attr :booktabs)
  2360. (plist-get attr :booktabs)
  2361. org-latex-tables-booktabs))
  2362. ;; TABLE-ROW's borders are extracted from its first cell.
  2363. (borders (org-export-table-cell-borders
  2364. (car (org-element-contents table-row)) info)))
  2365. (concat
  2366. ;; When BOOKTABS are activated enforce top-rule even when no
  2367. ;; hline was specifically marked.
  2368. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2369. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  2370. contents "\\\\\n"
  2371. (cond
  2372. ;; Special case for long tables. Define header and footers.
  2373. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2374. (format "%s
  2375. \\endfirsthead
  2376. \\multicolumn{%d}{l}{%s} \\\\
  2377. %s
  2378. %s \\\\\n
  2379. %s
  2380. \\endhead
  2381. %s\\multicolumn{%d}{r}{%s} \\\\
  2382. \\endfoot
  2383. \\endlastfoot"
  2384. (if booktabsp "\\midrule" "\\hline")
  2385. (cdr (org-export-table-dimensions
  2386. (org-export-get-parent-table table-row) info))
  2387. (org-latex--translate "Continued from previous page" info)
  2388. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2389. ((and (memq 'top borders)
  2390. (memq 'above borders)) "\\hline\n")
  2391. (t ""))
  2392. contents
  2393. (if booktabsp "\\midrule" "\\hline")
  2394. (if booktabsp "\\midrule" "\\hline")
  2395. ;; Number of columns.
  2396. (cdr (org-export-table-dimensions
  2397. (org-export-get-parent-table table-row) info))
  2398. (org-latex--translate "Continued on next page" info)))
  2399. ;; When BOOKTABS are activated enforce bottom rule even when
  2400. ;; no hline was specifically marked.
  2401. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  2402. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  2403. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  2404. ;;;; Target
  2405. (defun org-latex-target (target contents info)
  2406. "Transcode a TARGET object from Org to LaTeX.
  2407. CONTENTS is nil. INFO is a plist holding contextual
  2408. information."
  2409. (format "\\label{%s}"
  2410. (org-export-solidify-link-text (org-element-property :value target))))
  2411. ;;;; Timestamp
  2412. (defun org-latex-timestamp (timestamp contents info)
  2413. "Transcode a TIMESTAMP object from Org to LaTeX.
  2414. CONTENTS is nil. INFO is a plist holding contextual
  2415. information."
  2416. (let ((value (org-latex-plain-text
  2417. (org-timestamp-translate timestamp) info)))
  2418. (case (org-element-property :type timestamp)
  2419. ((active active-range) (format org-latex-active-timestamp-format value))
  2420. ((inactive inactive-range)
  2421. (format org-latex-inactive-timestamp-format value))
  2422. (otherwise (format org-latex-diary-timestamp-format value)))))
  2423. ;;;; Underline
  2424. (defun org-latex-underline (underline contents info)
  2425. "Transcode UNDERLINE from Org to LaTeX.
  2426. CONTENTS is the text with underline markup. INFO is a plist
  2427. holding contextual information."
  2428. (org-latex--text-markup contents 'underline))
  2429. ;;;; Verbatim
  2430. (defun org-latex-verbatim (verbatim contents info)
  2431. "Transcode a VERBATIM object from Org to LaTeX.
  2432. CONTENTS is nil. INFO is a plist used as a communication
  2433. channel."
  2434. (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  2435. ;;;; Verse Block
  2436. (defun org-latex-verse-block (verse-block contents info)
  2437. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2438. CONTENTS is verse block contents. INFO is a plist holding
  2439. contextual information."
  2440. (org-latex--wrap-label
  2441. verse-block
  2442. ;; In a verse environment, add a line break to each newline
  2443. ;; character and change each white space at beginning of a line
  2444. ;; into a space of 1 em. Also change each blank line with
  2445. ;; a vertical space of 1 em.
  2446. (progn
  2447. (setq contents (replace-regexp-in-string
  2448. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  2449. (replace-regexp-in-string
  2450. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  2451. (while (string-match "^[ \t]+" contents)
  2452. (let ((new-str (format "\\hspace*{%dem}"
  2453. (length (match-string 0 contents)))))
  2454. (setq contents (replace-match new-str nil t contents))))
  2455. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  2456. ;;; End-user functions
  2457. ;;;###autoload
  2458. (defun org-latex-export-as-latex
  2459. (&optional async subtreep visible-only body-only ext-plist)
  2460. "Export current buffer as a LaTeX buffer.
  2461. If narrowing is active in the current buffer, only export its
  2462. narrowed part.
  2463. If a region is active, export that region.
  2464. A non-nil optional argument ASYNC means the process should happen
  2465. asynchronously. The resulting buffer should be accessible
  2466. through the `org-export-stack' interface.
  2467. When optional argument SUBTREEP is non-nil, export the sub-tree
  2468. at point, extracting information from the headline properties
  2469. first.
  2470. When optional argument VISIBLE-ONLY is non-nil, don't export
  2471. contents of hidden elements.
  2472. When optional argument BODY-ONLY is non-nil, only write code
  2473. between \"\\begin{document}\" and \"\\end{document}\".
  2474. EXT-PLIST, when provided, is a property list with external
  2475. parameters overriding Org default settings, but still inferior to
  2476. file-local settings.
  2477. Export is done in a buffer named \"*Org LATEX Export*\", which
  2478. will be displayed when `org-export-show-temporary-export-buffer'
  2479. is non-nil."
  2480. (interactive)
  2481. (org-export-to-buffer 'latex "*Org LATEX Export*"
  2482. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  2483. ;;;###autoload
  2484. (defun org-latex-convert-region-to-latex ()
  2485. "Assume the current region has org-mode syntax, and convert it to LaTeX.
  2486. This can be used in any buffer. For example, you can write an
  2487. itemized list in org-mode syntax in an LaTeX buffer and use this
  2488. command to convert it."
  2489. (interactive)
  2490. (org-export-replace-region-by 'latex))
  2491. ;;;###autoload
  2492. (defun org-latex-export-to-latex
  2493. (&optional async subtreep visible-only body-only ext-plist)
  2494. "Export current buffer to a LaTeX file.
  2495. If narrowing is active in the current buffer, only export its
  2496. narrowed part.
  2497. If a region is active, export that region.
  2498. A non-nil optional argument ASYNC means the process should happen
  2499. asynchronously. The resulting file should be accessible through
  2500. the `org-export-stack' interface.
  2501. When optional argument SUBTREEP is non-nil, export the sub-tree
  2502. at point, extracting information from the headline properties
  2503. first.
  2504. When optional argument VISIBLE-ONLY is non-nil, don't export
  2505. contents of hidden elements.
  2506. When optional argument BODY-ONLY is non-nil, only write code
  2507. between \"\\begin{document}\" and \"\\end{document}\".
  2508. EXT-PLIST, when provided, is a property list with external
  2509. parameters overriding Org default settings, but still inferior to
  2510. file-local settings."
  2511. (interactive)
  2512. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2513. (org-export-to-file 'latex outfile
  2514. async subtreep visible-only body-only ext-plist)))
  2515. ;;;###autoload
  2516. (defun org-latex-export-to-pdf
  2517. (&optional async subtreep visible-only body-only ext-plist)
  2518. "Export current buffer to LaTeX then process through to PDF.
  2519. If narrowing is active in the current buffer, only export its
  2520. narrowed part.
  2521. If a region is active, export that region.
  2522. A non-nil optional argument ASYNC means the process should happen
  2523. asynchronously. The resulting file should be accessible through
  2524. the `org-export-stack' interface.
  2525. When optional argument SUBTREEP is non-nil, export the sub-tree
  2526. at point, extracting information from the headline properties
  2527. first.
  2528. When optional argument VISIBLE-ONLY is non-nil, don't export
  2529. contents of hidden elements.
  2530. When optional argument BODY-ONLY is non-nil, only write code
  2531. between \"\\begin{document}\" and \"\\end{document}\".
  2532. EXT-PLIST, when provided, is a property list with external
  2533. parameters overriding Org default settings, but still inferior to
  2534. file-local settings.
  2535. Return PDF file's name."
  2536. (interactive)
  2537. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2538. (org-export-to-file 'latex outfile
  2539. async subtreep visible-only body-only ext-plist
  2540. (lambda (file) (org-latex-compile file)))))
  2541. (defun org-latex-compile (texfile &optional snippet)
  2542. "Compile a TeX file.
  2543. TEXFILE is the name of the file being compiled. Processing is
  2544. done through the command specified in `org-latex-pdf-process'.
  2545. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  2546. file used to preview a LaTeX snippet. In this case, do not
  2547. create a log buffer and do not bother removing log files.
  2548. Return PDF file name or an error if it couldn't be produced."
  2549. (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
  2550. (full-name (file-truename texfile))
  2551. (out-dir (file-name-directory texfile))
  2552. ;; Properly set working directory for compilation.
  2553. (default-directory (if (file-name-absolute-p texfile)
  2554. (file-name-directory full-name)
  2555. default-directory))
  2556. errors)
  2557. (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
  2558. (save-window-excursion
  2559. (cond
  2560. ;; A function is provided: Apply it.
  2561. ((functionp org-latex-pdf-process)
  2562. (funcall org-latex-pdf-process (shell-quote-argument texfile)))
  2563. ;; A list is provided: Replace %b, %f and %o with appropriate
  2564. ;; values in each command before applying it. Output is
  2565. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2566. ((consp org-latex-pdf-process)
  2567. (let ((outbuf (and (not snippet)
  2568. (get-buffer-create "*Org PDF LaTeX Output*"))))
  2569. (mapc
  2570. (lambda (command)
  2571. (shell-command
  2572. (replace-regexp-in-string
  2573. "%b" (shell-quote-argument base-name)
  2574. (replace-regexp-in-string
  2575. "%f" (shell-quote-argument full-name)
  2576. (replace-regexp-in-string
  2577. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2578. outbuf))
  2579. org-latex-pdf-process)
  2580. ;; Collect standard errors from output buffer.
  2581. (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
  2582. (t (error "No valid command to process to PDF")))
  2583. (let ((pdffile (concat out-dir base-name ".pdf")))
  2584. ;; Check for process failure. Provide collected errors if
  2585. ;; possible.
  2586. (if (not (file-exists-p pdffile))
  2587. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2588. (when errors (concat ": " errors))))
  2589. ;; Else remove log files, when specified, and signal end of
  2590. ;; process to user, along with any error encountered.
  2591. (when (and (not snippet) org-latex-remove-logfiles)
  2592. (dolist (file (directory-files
  2593. out-dir t
  2594. (concat (regexp-quote base-name)
  2595. "\\(?:\\.[0-9]+\\)?"
  2596. "\\."
  2597. (regexp-opt org-latex-logfiles-extensions))))
  2598. (delete-file file)))
  2599. (message (concat "Process completed"
  2600. (if (not errors) "."
  2601. (concat " with errors: " errors)))))
  2602. ;; Return output file name.
  2603. pdffile))))
  2604. (defun org-latex--collect-errors (buffer)
  2605. "Collect some kind of errors from \"pdflatex\" command output.
  2606. BUFFER is the buffer containing output.
  2607. Return collected error types as a string, or nil if there was
  2608. none."
  2609. (with-current-buffer buffer
  2610. (save-excursion
  2611. (goto-char (point-max))
  2612. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  2613. (let ((case-fold-search t)
  2614. (errors ""))
  2615. (dolist (latex-error org-latex-known-errors)
  2616. (when (save-excursion (re-search-forward (car latex-error) nil t))
  2617. (setq errors (concat errors " " (cdr latex-error)))))
  2618. (and (org-string-nw-p errors) (org-trim errors)))))))
  2619. ;;;###autoload
  2620. (defun org-latex-publish-to-latex (plist filename pub-dir)
  2621. "Publish an Org file to LaTeX.
  2622. FILENAME is the filename of the Org file to be published. PLIST
  2623. is the property list for the given project. PUB-DIR is the
  2624. publishing directory.
  2625. Return output file name."
  2626. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  2627. ;;;###autoload
  2628. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  2629. "Publish an Org file to PDF (via LaTeX).
  2630. FILENAME is the filename of the Org file to be published. PLIST
  2631. is the property list for the given project. PUB-DIR is the
  2632. publishing directory.
  2633. Return output file name."
  2634. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  2635. ;; in working directory and then moved to publishing directory.
  2636. (org-publish-attachment
  2637. plist
  2638. (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
  2639. pub-dir))
  2640. (provide 'ox-latex)
  2641. ;; Local variables:
  2642. ;; generated-autoload-file: "org-loaddefs.el"
  2643. ;; End:
  2644. ;;; ox-latex.el ends here