Debian Codegraph @

inst/examples/knitr-themes.Rnw

raw
1 %% LyX 2.2.1 created this file. For more info, see http://www.lyx.org/.
2 %% Do not edit unless you really know what you are doing.
3 \documentclass[nohyper,justified]{tufte-handout}
4 \usepackage[T1]{fontenc}
5 \usepackage{url}
6
7 \makeatletter
8
9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
10
11 \title{Customizing Syntax Highlighting Themes}
12 \author{Yihui Xie \& Ramnath Vaidyanathan}
13
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
15 \renewcommand{\textfraction}{0.05}
16 \renewcommand{\topfraction}{0.8}
17 \renewcommand{\bottomfraction}{0.8}
18 \renewcommand{\floatpagefraction}{0.75}
19
20 \usepackage[breaklinks=true,pdfstartview=FitH]{hyperref}
21
22 \makeatother
23
24 \begin{document}
25 <<setup, include=FALSE, cache=FALSE>>=
26 library(knitr)
27 opts_chunk$set(fig.path='figure/theme-', cache.path='cache/theme-', cache=TRUE)
28 options(formatR.arrow=TRUE,width=78)
29 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)})
30 @
31 <<solarized-theme, cache=FALSE, echo=FALSE>>=
32 thm = knit_theme$get("solarized-dark")
33 knit_theme$set(thm)
34 @
35 \maketitle
36 \begin{abstract}
37 This manual\footnote{source code at \url{https://github.com/yihui/knitr/blob/master/inst/examples/knitr-themes.Rnw}}
38 shows how to customize syntax highlighting of source code using themes.
39 It walks the user through the basics of syntax highlighting in \textbf{knitr},
40 and the use of built-in themes.
41 \end{abstract}
42 The \textbf{knitr} package uses the \textbf{highr} package to highlight
43 source code in a document. In short, the \textbf{highr} package parses
44 the source code, tokenizes it into grammar symbols, and formats their
45 appearance using CSS or \LaTeX{} commands.
46
47 \section{Usage}
48
49 We can use the object \texttt{knit\_theme} to set / get a theme. See
50 \texttt{?knit\_theme} for the usage. For example, we set the theme
51 of this document to be \texttt{solarized-dark}:
52
53 <<solarized-theme>>=
54 @
55
56 \section{Built-in Themes}
57
58 \begin{margintable}
59 <<eclipse-css, comment=NA, echo=FALSE>>=
60 cat(c(readLines('../themes/edit-eclipse.css', n=30),'...'),sep='\n')
61 @
62 \end{margintable}
63
64 The listing on the right shows the CSS file for one of the themes,
65 \texttt{edit-eclipse}, which was adapted from Andre Simon's excellent
66 highlighter\footnote{\url{http://www.andre-simon.de/}}. The \textbf{knitr}
67 package comes pre-loaded with a number of themes based on this highlighter.
68 Here is list of all available code themes\footnote{For a preview of all themes, see \url{https://gist.github.com/yihui/3422133}}:
69
70 <<all-themes>>=
71 knit_theme$get()
72 @
73
74 Shown below is how the \texttt{solarized-dark} theme looks like when
75 applied to R code:
76
77 <<demo-code, eval = FALSE>>=
78 library(XML)
79 library(reshape)
80 # SCRAPE THE DATA FROM WEB
81
82 base_url = "http://www.mlsoccer.com/stats/%s/reg"
83 years = 1996:2010
84 options(width = 40)
85
86 #' Function to save data for each year
87 save_data = function(y){
88 url = sprintf(base_url, y)
89 tab = readHTMLTable(url, header = FALSE, stringsAsFactors = FALSE);
90 pos = max(grep("United", tab));
91 tab = tab[[pos]];
92 tab$year = y;
93 return(tab)
94 }
95
96 team.list = lapply(years, save_data);
97 mls = merge_recurse(team.list);
98 @
99
100 \section{Misc}
101
102 One thing to consider is the foreground color of plots when we use
103 dark themes; we need to make it lighter, otherwise the graphical elements
104 will be difficult to be read. We can access the foreground color of
105 the theme in the list returned by \texttt{knit\_theme\$get(theme)},
106 e.g., for this document:
107
108 <<fg-color>>=
109 ## the object thm is from the first chunk
110 thm$foreground # foreground color
111 thm$background # background color
112 @
113
114 When we make plots, we may use these colors, e.g.
115
116 <<plot-color, fig.width=6, fig.height=4, out.width='.8\\linewidth', par=TRUE>>=
117 ## can design a chunk hook to set foreground color automatically
118 fgcolor=thm$foreground
119 par(fg=fgcolor, col.axis=fgcolor, col.lab=fgcolor)
120 plot(rnorm(100),pch=19)
121 @
122
123 Of course, we do not need to worry about these colors when we use
124 a white background for the plots.
125 \end{document}