Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

1291 lines (959 sloc) 18.906 kB
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass tufte-handout
\begin_preamble
\renewcommand{\textfraction}{0.05}
\renewcommand{\topfraction}{0.8}
\renewcommand{\bottomfraction}{0.8}
\renewcommand{\floatpagefraction}{0.75}
\usepackage[breaklinks=true,pdfstartview=FitH]{hyperref}
\end_preamble
\options justified
\use_default_options true
\begin_modules
knitr
\end_modules
\maintain_unincluded_children false
\language english
\language_package none
\inputencoding default
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 88
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\pdf_author "Ramnath Vaidyanathan"
\pdf_bookmarks true
\pdf_bookmarksnumbered true
\pdf_bookmarksopen true
\pdf_bookmarksopenlevel 1
\pdf_breaklinks false
\pdf_pdfborder true
\pdf_colorlinks true
\pdf_backref false
\pdf_pdfusetitle true
\pdf_quoted_options "citecolor = blue, linkcolor = magenta, urlcolor = green"
\papersize default
\use_geometry true
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\leftmargin 1in
\topmargin 1in
\rightmargin 1in
\bottommargin 1in
\secnumdepth 2
\tocdepth 2
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<setup, include=FALSE, cache=FALSE>>=
\end_layout
\begin_layout Plain Layout
library(knitr)
\end_layout
\begin_layout Plain Layout
opts_chunk$set(fig.path='figure/theme-', cache.path='cache/theme-', cache=TRUE)
\end_layout
\begin_layout Plain Layout
options(formatR.arrow=TRUE,width=78)
\end_layout
\begin_layout Plain Layout
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)})
\end_layout
\begin_layout Plain Layout
@
\end_layout
\begin_layout Plain Layout
<<solarized-theme, cache=FALSE, echo=FALSE>>=
\end_layout
\begin_layout Plain Layout
thm = knit_theme$get("solarized-dark")
\end_layout
\begin_layout Plain Layout
knit_theme$set(thm)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Title
Customizing Syntax Highlighting Themes
\end_layout
\begin_layout Author
Yihui Xie & Ramnath Vaidyanathan
\end_layout
\begin_layout Abstract
This manual
\begin_inset Foot
status open
\begin_layout Plain Layout
source code at
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
https://github.com/yihui/knitr/blob/master/inst/examples/knitr-themes.Rnw
\end_layout
\end_inset
\end_layout
\end_inset
shows how to customize syntax highlighting of source code using themes.
It walks the user through the basics of syntax highlighting in
\series bold
knitr
\series default
, the use of built-in themes as well as those from
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://www.eclipsecolorthemes.org/
\end_layout
\end_inset
.
\end_layout
\begin_layout Standard
The
\series bold
knitr
\series default
package uses the
\series bold
highlight
\series default
package by Romain Francois to highlight source code in a document.
In short, the
\series bold
highlight
\series default
package parses the source code using the
\noun on
bison
\noun default
parser, tokenizes it into grammar symbols, and formats their appearance
based on a CSS style file.
\end_layout
\begin_layout Section
Usage
\end_layout
\begin_layout Standard
We can use the object
\family typewriter
knit_theme
\family default
to set / get a theme.
See
\family typewriter
?knit_theme
\family default
for the usage.
For example, we set the theme of this document to be
\family typewriter
solarized-dark
\family default
:
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<solarized-theme>>=
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Section
Built-in Themes
\end_layout
\begin_layout Standard
\begin_inset Float margintable
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset ERT
status open
\begin_layout Plain Layout
<<eclipse-css, comment=NA, echo=FALSE>>=
\end_layout
\begin_layout Plain Layout
cat(c(readLines('../themes/edit-eclipse.css', n=30),'...'),sep='
\backslash
n')
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The listing on the right shows the CSS file for one of the themes,
\family typewriter
edit-eclipse
\family default
, which was adapted from Andre Simon's excellent highlighter
\begin_inset Foot
status open
\begin_layout Plain Layout
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://www.andre-simon.de/
\end_layout
\end_inset
\end_layout
\end_inset
.
The
\series bold
knitr
\series default
package comes pre-loaded with a number of themes based on this highlighter.
Here is list of all available code themes
\begin_inset Foot
status open
\begin_layout Plain Layout
For a preview of all themes, see
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
https://gist.github.com/yihui/3422133
\end_layout
\end_inset
\end_layout
\end_inset
:
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<all-themes>>=
\end_layout
\begin_layout Plain Layout
knit_theme$get()
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Shown below is how the
\family typewriter
solarized-dark
\family default
theme looks like when applied to R code:
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<demo-code, eval = FALSE>>=
\end_layout
\begin_layout Plain Layout
library(XML)
\end_layout
\begin_layout Plain Layout
library(plyr)
\end_layout
\begin_layout Plain Layout
library(reshape)
\end_layout
\begin_layout Plain Layout
# SCRAPE THE DATA FROM WEB
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
base_url = "http://www.mlsoccer.com/stats/%s/reg"
\end_layout
\begin_layout Plain Layout
years = 1996:2010
\end_layout
\begin_layout Plain Layout
options(width = 40)
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
#' Function to save data for each year
\end_layout
\begin_layout Plain Layout
save_data = function(y){
\end_layout
\begin_layout Plain Layout
url = sprintf(base_url, y)
\end_layout
\begin_layout Plain Layout
tab = readHTMLTable(url, header = FALSE, stringsAsFactors = FALSE);
\end_layout
\begin_layout Plain Layout
pos = max(grep("United", tab));
\end_layout
\begin_layout Plain Layout
tab = tab[[pos]];
\end_layout
\begin_layout Plain Layout
tab$year = y;
\end_layout
\begin_layout Plain Layout
return(tab)
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
team.list = llply(years, save_data);
\end_layout
\begin_layout Plain Layout
mls = merge_recurse(team.list);
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Section
Eclipse Color Themes
\end_layout
\begin_layout Standard
A rich repository of syntax highlighting themes for use with Eclipse IDE
is available at
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://www.eclipsecolorthemes.org/
\end_layout
\end_inset
.
In order to use one of these themes with
\series bold
knitr
\series default
, it needs to be converted into a CSS file that can be parsed by
\series bold
highlight
\series default
.
This is achieved by mapping language tokens across the two formats (Table
\begin_inset CommandInset ref
LatexCommand ref
reference "mar:Token-Mapping"
\end_inset
) to create a CSS file.
\end_layout
\begin_layout Standard
\begin_inset Float margintable
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
renewcommand
\backslash
arraystretch{1.3}
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Token mapping
\begin_inset CommandInset label
LatexCommand label
name "mar:Token-Mapping"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\noindent
\begin_inset Tabular
<lyxtabular version="3" rows="11" columns="2">
<features booktabs="true" tabularvalignment="middle">
<column alignment="left" valignment="top" width="0">
<column alignment="left" valignment="top" width="0pt">
<row>
<cell alignment="left" valignment="top" topline="true" bottomline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\series bold
Highlight (CSS)
\end_layout
\end_inset
</cell>
<cell alignment="left" valignment="top" topline="true" bottomline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\series bold
Eclipse (XML)
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="left" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
background
\end_layout
\end_inset
</cell>
<cell alignment="left" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
background
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
kwa
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
keyword
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
kwb
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
localVariableDeclaration
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
kwc
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
parameterVariable
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
kwd
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
method
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
num
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
number
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
str
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
string
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
com
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
singleLineComment
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
opt
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
operator
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="left" valignment="top" bottomline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
std
\end_layout
\end_inset
</cell>
<cell alignment="left" valignment="top" bottomline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
foreground
\end_layout
\end_inset
</cell>
</row>
</lyxtabular>
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
If you want to use an eclipse theme that is not shipped with
\series bold
knitr
\series default
, you can use the utility function
\emph on
eclipse_theme()
\emph default
to automatically download, map and save the theme as a CSS file.
For example, if you want to use the theme Oughsumm
\begin_inset Foot
status open
\begin_layout Plain Layout
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://www.eclipsecolorthemes.org/?view=theme&id=1885
\end_layout
\end_inset
(theme id is 1885)
\end_layout
\end_inset
, you can use the code below in your first chunk:
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<eclipse-theme, eval=FALSE>>=
\end_layout
\begin_layout Plain Layout
css = eclipse_theme(1885)
\end_layout
\begin_layout Plain Layout
thm = knit_theme$get(css)
\end_layout
\begin_layout Plain Layout
knit_theme$set(thm)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Note this requires Internet connection, and we can put the code to download
the theme in a cached chunk, so it will be downloaded only once, then we
set the theme in an uncached chunk using the parsed theme object.
For example, download the theme:
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
%% please use cache=TRUE and echo=FALSE for this chunk
\end_layout
\begin_layout Plain Layout
<<download-theme, eval=FALSE, message=FALSE>>=
\end_layout
\begin_layout Plain Layout
(css = eclipse_theme(1885))
\end_layout
\begin_layout Plain Layout
thm = knit_theme$get(css)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Then apply the theme:
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
%% cache=FALSE for this chunk
\end_layout
\begin_layout Plain Layout
<<apply-theme, eval=FALSE>>=
\end_layout
\begin_layout Plain Layout
knit_theme$set(thm)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Even we do not have Internet connection the next time, we can still use
the theme.
\end_layout
\begin_layout Section
Misc
\end_layout
\begin_layout Standard
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
\family typewriter
knit_theme$get(theme)
\family default
, e.g., for this document:
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<fg-color>>=
\end_layout
\begin_layout Plain Layout
## the object thm is from the first chunk
\end_layout
\begin_layout Plain Layout
thm$foreground # foreground color
\end_layout
\begin_layout Plain Layout
thm$background # background color
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
When we make plots, we may use these colors, e.g.
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<plot-color, fig.width=6, fig.height=4, out.width='.8
\backslash
\backslash
linewidth', par=TRUE>>=
\end_layout
\begin_layout Plain Layout
## can design a chunk hook to set foreground color automatically
\end_layout
\begin_layout Plain Layout
fgcolor=thm$foreground
\end_layout
\begin_layout Plain Layout
par(fg=fgcolor, col.axis=fgcolor, col.lab=fgcolor)
\end_layout
\begin_layout Plain Layout
plot(rnorm(100),pch=19)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Of course, we do not need to worry about these colors when we use a white
background for the plots.
\end_layout
\end_body
\end_document
Jump to Line
Something went wrong with that request. Please try again.