Fetching contributors…
Cannot retrieve contributors at this time
127 lines (101 sloc) 3.96 KB
%% LyX 2.2.1 created this file. For more info, see
%% Do not edit unless you really know what you are doing.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
\title{Customizing Syntax Highlighting Themes}
\author{Yihui Xie \& Ramnath Vaidyanathan}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
<<setup, include=FALSE, cache=FALSE>>=
opts_chunk$set(fig.path='figure/theme-', cache.path='cache/theme-', cache=TRUE)
knit_hooks$set(par=function(before, options, envir){if (before) par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3)})
<<solarized-theme, cache=FALSE, echo=FALSE>>=
thm = knit_theme$get("solarized-dark")
This manual\footnote{source code at \url{}}
shows how to customize syntax highlighting of source code using themes.
It walks the user through the basics of syntax highlighting in \textbf{knitr},
and the use of built-in themes.
The \textbf{knitr} package uses the \textbf{highr} package to highlight
source code in a document. In short, the \textbf{highr} package parses
the source code, tokenizes it into grammar symbols, and formats their
appearance using CSS or \LaTeX{} commands.
We can use the object \texttt{knit\_theme} to set / get a theme. See
\texttt{?knit\_theme} for the usage. For example, we set the theme
of this document to be \texttt{solarized-dark}:
\section{Built-in Themes}
<<eclipse-css, comment=NA, echo=FALSE>>=
cat(c(readLines('../themes/edit-eclipse.css', n=30),'...'),sep='\n')
The listing on the right shows the CSS file for one of the themes,
\texttt{edit-eclipse}, which was adapted from Andre Simon's excellent
highlighter\footnote{\url{}}. The \textbf{knitr}
package comes pre-loaded with a number of themes based on this highlighter.
Here is list of all available code themes\footnote{For a preview of all themes, see \url{}}:
Shown below is how the \texttt{solarized-dark} theme looks like when
applied to R code:
<<demo-code, eval = FALSE>>=
base_url = ""
years = 1996:2010
options(width = 40)
#' Function to save data for each year
save_data = function(y){
url = sprintf(base_url, y)
tab = readHTMLTable(url, header = FALSE, stringsAsFactors = FALSE);
pos = max(grep("United", tab));
tab = tab[[pos]];
tab$year = y;
team.list = llply(years, save_data);
mls = merge_recurse(team.list);
One thing to consider is the foreground color of plots when we use
dark themes; we need to make it lighter, otherwise the graphical elements
will be difficult to be read. We can access the foreground color of
the theme in the list returned by \texttt{knit\_theme\$get(theme)},
e.g., for this document:
## the object thm is from the first chunk
thm$foreground # foreground color
thm$background # background color
When we make plots, we may use these colors, e.g.
<<plot-color, fig.width=6, fig.height=4, out.width='.8\\linewidth', par=TRUE>>=
## can design a chunk hook to set foreground color automatically
par(fg=fgcolor, col.axis=fgcolor, col.lab=fgcolor)
Of course, we do not need to worry about these colors when we use
a white background for the plots.