Skip to content

Instantly share code, notes, and snippets.

@yihui
Last active June 30, 2021 18:01
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yihui/3422133 to your computer and use it in GitHub Desktop.
Save yihui/3422133 to your computer and use it in GitHub Desktop.
Preview all syntax highlighting themes in knitr (HTML and LaTeX)
library(knitr)
themes = knit_theme$get()
pat_brew() # use brew patterns <% %>
for (theme in themes) knit('theme.brew', paste('theme-', theme, '.Rhtml', sep = ''))
knit_patterns$restore() # clear patterns
mapply(knit, input = list.files(pattern = '\\.Rhtml$'))
writeLines(c(
'<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Highlight themes in knitr</title>
<meta name="author" content="Taiyun Wei and Yihui Xie" />
</head>
<body>',
paste(sprintf('<iframe src="%s" width="800" height="520" scrolling="auto" style="display: block; margin: auto;"></iframe>',
list.files(pattern = '^theme-.*\\.html$')), collapse = '<hr />\n'),
'</body>
</html>'),
con = 'index.html')
browseURL('index.html')
# knitr syntax highlighting theme examples overview
# Berry Boessenkool, berry-b@gmx.de, Sept 2014
library(knitr)
themes <- knit_theme$get()
Code <- "'R sample'; \"string2\" # comment. # examples from Tinn R
var_a = 1:100 ; var.b <- 1: 4.6 # Numbers, Identifier
1 + 1 - 1 * 1 / 1 ^ 1 < 6 & !TRUE; #h$k ? # Operator, Symbol
tryCatch(NA); NULL; TRUE; T; FALSE; if(F) 7 # Programming
mean; as.data.frame(iris) # Function, dataset
plot(4, col='blue', cex=0.5) # Plotting
foo = function(dummy=NA) if (TRUE) { for (i in 1:10) x <- NULL }"
#sqrt(-1) # warning #1 + \"a\" # error" only makes sense if eval=TRUE
for(i in 1:length(themes))
{
file <- paste0("theme-",themes[i],".Rnw")
# Header:
cat("\\documentclass{article}
\\usepackage[paperheight=3.3cm,paperwidth=14cm, left=0cm,right=0cm,
top=0cm,bottom=0cm]{geometry}
\\begin{document}\n", file=file)
# Code
cat(paste0("<<theme",i,", echo=FALSE>>=
knit_theme$set(knit_theme$get('", themes[i], "'))
@\n"), file=file, append=TRUE)
cat(paste0("<<code",i,", eval=FALSE>>=\n"), file=file, append=TRUE)
cat(paste(themes[i], " ;", Code), file=file, append=TRUE)
cat("\n@\n\n", file=file, append=TRUE)
# Enddoc:
cat("\\end{document}", file=file, append=TRUE)
#
# Make PDF
knit2pdf(input=paste0("theme-",themes[i],".Rnw"))
}
# combine_all
pdffiles <- dir(pattern=glob2rx("*.pdf"))
pdffiles <- pdffiles[-42] # "fine_blue" does not work...
file <- "2_combine_all.Rnw"
#
cat("\\documentclass[a4paper]{article}
\\usepackage[cm]{fullpage}
\\begin{document}
\\center
knitr syntax highlighting theme examples overview\\\\
Berry Boessenkool, berry-b@gmx.de, Sept 2014\n\n", file=file)
#
for(i in 1:length(pdffiles))
cat(paste0("\\includegraphics[width=.8\\linewidth]{",pdffiles[i],"}\n"),
file=file, append=TRUE)
#
cat("\\end{document}", file=file, append=TRUE)
# knit2pdf(input=file) # doesnt work for me, so I just knit that via Rstudio.
<!DOCTYPE html>
<html>
<head>
<title>Code highlight themes in knitr: <% theme %></title>
</head>
<body>
<p>Code highlight themes in knitr: "<a href="#" target="_blank" title="View in a new frame"><strong><% theme %></strong></a>"</p>
<!--begin.rcode include=FALSE
knit_theme$set('<% theme %>')
end.rcode-->
<!--begin.rcode eval=FALSE
## credit: https://gist.github.com/2155443
s=embed(rt(5,1),2);plot(t(replicate(1e4,s[4,]<<-(s[4,]+s[sample(3,1),])/2)),cex=.1)
end.rcode-->
<!--begin.rcode
1+1
x=sqrt(-1) # warning
message('knitr says hello to HTML!') # message
1+'a' # error
end.rcode-->
<p>Let's ask R what is the value of &pi;? Of course it is <!--rinline pi -->.</p>
</body>
</html>
@isomorphisms
Copy link

These are not the same as RStudio themes, correct? For example I don't see spacelab on here.

edit: oh ok, I get it. These here are the highlight parameter and those are the theme parameter.

@jemus42
Copy link

jemus42 commented Apr 5, 2019

Turns out the themes I like (e.g. molokai) are not available in rmarkdown :(

@francisbarton
Copy link

Turns out the themes I like (e.g. molokai) are not available in rmarkdown :(

Yeah it's a real shame that rendering Rmd with knitr doesn't have all the same themes and syntax highlight styles that are in theory available. Jim Hester's knitrBootstrap has different syntax themes available - including Molokai! - and some great features like automatic lightboxing of images, that aren't available in standard rmarkdown. It would be great if somehow a clever person could merge all these things in so that the full range of capabilities was available in standard Rmarkdown. But that is beyond my abilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment