org-e-latex.el 95 KB

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