Debian Codegraph @

inst/examples/knitr-themes.lyx

raw
1 #LyX 2.1 created this file. For more info see http://www.lyx.org/
2 \lyxformat 474
3 \begin_document
4 \begin_header
5 \textclass tufte-handout
6 \begin_preamble
7 \renewcommand{\textfraction}{0.05}
8 \renewcommand{\topfraction}{0.8}
9 \renewcommand{\bottomfraction}{0.8}
10 \renewcommand{\floatpagefraction}{0.75}
11
12 \usepackage[breaklinks=true,pdfstartview=FitH]{hyperref}
13 \end_preamble
14 \options justified
15 \use_default_options true
16 \begin_modules
17 knitr
18 \end_modules
19 \maintain_unincluded_children false
20 \language english
21 \language_package none
22 \inputencoding default
23 \fontencoding global
24 \font_roman default
25 \font_sans default
26 \font_typewriter default
27 \font_math auto
28 \font_default_family default
29 \use_non_tex_fonts false
30 \font_sc false
31 \font_osf false
32 \font_sf_scale 100
33 \font_tt_scale 88
34 \graphics default
35 \default_output_format default
36 \output_sync 0
37 \bibtex_command default
38 \index_command default
39 \paperfontsize default
40 \spacing single
41 \use_hyperref false
42 \pdf_author "Ramnath Vaidyanathan"
43 \pdf_bookmarks true
44 \pdf_bookmarksnumbered true
45 \pdf_bookmarksopen true
46 \pdf_bookmarksopenlevel 1
47 \pdf_breaklinks false
48 \pdf_pdfborder true
49 \pdf_colorlinks true
50 \pdf_backref false
51 \pdf_pdfusetitle true
52 \pdf_quoted_options "citecolor = blue, linkcolor = magenta, urlcolor = green"
53 \papersize default
54 \use_geometry true
55 \use_package amsmath 1
56 \use_package amssymb 1
57 \use_package cancel 1
58 \use_package esint 1
59 \use_package mathdots 1
60 \use_package mathtools 1
61 \use_package mhchem 1
62 \use_package stackrel 1
63 \use_package stmaryrd 1
64 \use_package undertilde 1
65 \cite_engine basic
66 \cite_engine_type default
67 \biblio_style plain
68 \use_bibtopic false
69 \use_indices false
70 \paperorientation portrait
71 \suppress_date false
72 \justification true
73 \use_refstyle 0
74 \index Index
75 \shortcut idx
76 \color #008000
77 \end_index
78 \leftmargin 1in
79 \topmargin 1in
80 \rightmargin 1in
81 \bottommargin 1in
82 \secnumdepth 2
83 \tocdepth 2
84 \paragraph_separation indent
85 \paragraph_indentation default
86 \quotes_language english
87 \papercolumns 1
88 \papersides 1
89 \paperpagestyle default
90 \tracking_changes false
91 \output_changes false
92 \html_math_output 0
93 \html_css_as_file 0
94 \html_be_strict false
95 \end_header
96
97 \begin_body
98
99 \begin_layout Standard
100 \begin_inset ERT
101 status open
102
103 \begin_layout Plain Layout
104
105 <<setup, include=FALSE, cache=FALSE>>=
106 \end_layout
107
108 \begin_layout Plain Layout
109
110 library(knitr)
111 \end_layout
112
113 \begin_layout Plain Layout
114
115 opts_chunk$set(fig.path='figure/theme-', cache.path='cache/theme-', cache=TRUE)
116 \end_layout
117
118 \begin_layout Plain Layout
119
120 options(formatR.arrow=TRUE,width=78)
121 \end_layout
122
123 \begin_layout Plain Layout
124
125 knit_hooks$set(par=function(before, options, envir){if (before) par(mar=c(4,4,.1,.
126 1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3)})
127 \end_layout
128
129 \begin_layout Plain Layout
130
131 @
132 \end_layout
133
134 \begin_layout Plain Layout
135
136 <<solarized-theme, cache=FALSE, echo=FALSE>>=
137 \end_layout
138
139 \begin_layout Plain Layout
140
141 thm = knit_theme$get("solarized-dark")
142 \end_layout
143
144 \begin_layout Plain Layout
145
146 knit_theme$set(thm)
147 \end_layout
148
149 \begin_layout Plain Layout
150
151 @
152 \end_layout
153
154 \end_inset
155
156
157 \end_layout
158
159 \begin_layout Title
160 Customizing Syntax Highlighting Themes
161 \end_layout
162
163 \begin_layout Author
164 Yihui Xie & Ramnath Vaidyanathan
165 \end_layout
166
167 \begin_layout Abstract
168 This manual
169 \begin_inset Foot
170 status open
171
172 \begin_layout Plain Layout
173 source code at
174 \begin_inset Flex URL
175 status collapsed
176
177 \begin_layout Plain Layout
178
179 https://github.com/yihui/knitr/blob/master/inst/examples/knitr-themes.Rnw
180 \end_layout
181
182 \end_inset
183
184
185 \end_layout
186
187 \end_inset
188
189 shows how to customize syntax highlighting of source code using themes.
190 It walks the user through the basics of syntax highlighting in
191 \series bold
192 knitr
193 \series default
194 , and the use of built-in themes.
195 \end_layout
196
197 \begin_layout Standard
198 The
199 \series bold
200 knitr
201 \series default
202 package uses the
203 \series bold
204 highr
205 \series default
206 package to highlight source code in a document.
207 In short, the
208 \series bold
209 highr
210 \series default
211 package parses the source code, tokenizes it into grammar symbols, and
212 formats their appearance using CSS or LaTeX commands.
213
214 \end_layout
215
216 \begin_layout Section
217 Usage
218 \end_layout
219
220 \begin_layout Standard
221 We can use the object
222 \family typewriter
223 knit_theme
224 \family default
225 to set / get a theme.
226 See
227 \family typewriter
228 ?knit_theme
229 \family default
230 for the usage.
231 For example, we set the theme of this document to be
232 \family typewriter
233 solarized-dark
234 \family default
235 :
236 \end_layout
237
238 \begin_layout Standard
239 \begin_inset ERT
240 status open
241
242 \begin_layout Plain Layout
243
244 <<solarized-theme>>=
245 \end_layout
246
247 \begin_layout Plain Layout
248
249 @
250 \end_layout
251
252 \end_inset
253
254
255 \end_layout
256
257 \begin_layout Section
258 Built-in Themes
259 \end_layout
260
261 \begin_layout Standard
262 \begin_inset Float margintable
263 wide false
264 sideways false
265 status open
266
267 \begin_layout Plain Layout
268 \begin_inset ERT
269 status open
270
271 \begin_layout Plain Layout
272
273 <<eclipse-css, comment=NA, echo=FALSE>>=
274 \end_layout
275
276 \begin_layout Plain Layout
277
278 cat(c(readLines('../themes/edit-eclipse.css', n=30),'...'),sep='
279 \backslash
280 n')
281 \end_layout
282
283 \begin_layout Plain Layout
284
285 @
286 \end_layout
287
288 \end_inset
289
290
291 \end_layout
292
293 \end_inset
294
295
296 \end_layout
297
298 \begin_layout Standard
299 The listing on the right shows the CSS file for one of the themes,
300 \family typewriter
301 edit-eclipse
302 \family default
303 , which was adapted from Andre Simon's excellent highlighter
304 \begin_inset Foot
305 status open
306
307 \begin_layout Plain Layout
308 \begin_inset Flex URL
309 status collapsed
310
311 \begin_layout Plain Layout
312
313 http://www.andre-simon.de/
314 \end_layout
315
316 \end_inset
317
318
319 \end_layout
320
321 \end_inset
322
323 .
324 The
325 \series bold
326 knitr
327 \series default
328 package comes pre-loaded with a number of themes based on this highlighter.
329 Here is list of all available code themes
330 \begin_inset Foot
331 status open
332
333 \begin_layout Plain Layout
334 For a preview of all themes, see
335 \begin_inset Flex URL
336 status collapsed
337
338 \begin_layout Plain Layout
339
340 https://gist.github.com/yihui/3422133
341 \end_layout
342
343 \end_inset
344
345
346 \end_layout
347
348 \end_inset
349
350 :
351 \end_layout
352
353 \begin_layout Standard
354 \begin_inset ERT
355 status open
356
357 \begin_layout Plain Layout
358
359 <<all-themes>>=
360 \end_layout
361
362 \begin_layout Plain Layout
363
364 knit_theme$get()
365 \end_layout
366
367 \begin_layout Plain Layout
368
369 @
370 \end_layout
371
372 \end_inset
373
374
375 \end_layout
376
377 \begin_layout Standard
378 Shown below is how the
379 \family typewriter
380 solarized-dark
381 \family default
382 theme looks like when applied to R code:
383 \end_layout
384
385 \begin_layout Standard
386 \begin_inset ERT
387 status open
388
389 \begin_layout Plain Layout
390
391 <<demo-code, eval = FALSE>>=
392 \end_layout
393
394 \begin_layout Plain Layout
395
396 library(XML)
397 \end_layout
398
399 \begin_layout Plain Layout
400
401 library(reshape)
402 \end_layout
403
404 \begin_layout Plain Layout
405
406 # SCRAPE THE DATA FROM WEB
407 \end_layout
408
409 \begin_layout Plain Layout
410
411 \end_layout
412
413 \begin_layout Plain Layout
414
415 base_url = "http://www.mlsoccer.com/stats/%s/reg"
416 \end_layout
417
418 \begin_layout Plain Layout
419
420 years = 1996:2010
421 \end_layout
422
423 \begin_layout Plain Layout
424
425 options(width = 40)
426 \end_layout
427
428 \begin_layout Plain Layout
429
430 \end_layout
431
432 \begin_layout Plain Layout
433
434 #' Function to save data for each year
435 \end_layout
436
437 \begin_layout Plain Layout
438
439 save_data = function(y){
440 \end_layout
441
442 \begin_layout Plain Layout
443
444 url = sprintf(base_url, y)
445 \end_layout
446
447 \begin_layout Plain Layout
448
449 tab = readHTMLTable(url, header = FALSE, stringsAsFactors = FALSE);
450 \end_layout
451
452 \begin_layout Plain Layout
453
454 pos = max(grep("United", tab));
455 \end_layout
456
457 \begin_layout Plain Layout
458
459 tab = tab[[pos]];
460 \end_layout
461
462 \begin_layout Plain Layout
463
464 tab$year = y;
465 \end_layout
466
467 \begin_layout Plain Layout
468
469 return(tab)
470 \end_layout
471
472 \begin_layout Plain Layout
473
474 }
475 \end_layout
476
477 \begin_layout Plain Layout
478
479
480 \end_layout
481
482 \begin_layout Plain Layout
483
484 team.list = lapply(years, save_data);
485 \end_layout
486
487 \begin_layout Plain Layout
488
489 mls = merge_recurse(team.list);
490 \end_layout
491
492 \begin_layout Plain Layout
493
494 @
495 \end_layout
496
497 \end_inset
498
499
500 \end_layout
501
502 \begin_layout Section
503 Misc
504 \end_layout
505
506 \begin_layout Standard
507 One thing to consider is the foreground color of plots when we use dark
508 themes; we need to make it lighter, otherwise the graphical elements will
509 be difficult to be read.
510 We can access the foreground color of the theme in the list returned by
511
512 \family typewriter
513 knit_theme$get(theme)
514 \family default
515 , e.g., for this document:
516 \end_layout
517
518 \begin_layout Standard
519 \begin_inset ERT
520 status open
521
522 \begin_layout Plain Layout
523
524 <<fg-color>>=
525 \end_layout
526
527 \begin_layout Plain Layout
528
529 ## the object thm is from the first chunk
530 \end_layout
531
532 \begin_layout Plain Layout
533
534 thm$foreground # foreground color
535 \end_layout
536
537 \begin_layout Plain Layout
538
539 thm$background # background color
540 \end_layout
541
542 \begin_layout Plain Layout
543
544 @
545 \end_layout
546
547 \end_inset
548
549
550 \end_layout
551
552 \begin_layout Standard
553 When we make plots, we may use these colors, e.g.
554 \end_layout
555
556 \begin_layout Standard
557 \begin_inset ERT
558 status open
559
560 \begin_layout Plain Layout
561
562 <<plot-color, fig.width=6, fig.height=4, out.width='.8
563 \backslash
564
565 \backslash
566 linewidth', par=TRUE>>=
567 \end_layout
568
569 \begin_layout Plain Layout
570
571 ## can design a chunk hook to set foreground color automatically
572 \end_layout
573
574 \begin_layout Plain Layout
575
576 fgcolor=thm$foreground
577 \end_layout
578
579 \begin_layout Plain Layout
580
581 par(fg=fgcolor, col.axis=fgcolor, col.lab=fgcolor)
582 \end_layout
583
584 \begin_layout Plain Layout
585
586 plot(rnorm(100),pch=19)
587 \end_layout
588
589 \begin_layout Plain Layout
590
591 @
592 \end_layout
593
594 \end_inset
595
596
597 \end_layout
598
599 \begin_layout Standard
600 Of course, we do not need to worry about these colors when we use a white
601 background for the plots.
602 \end_layout
603
604 \end_body
605 \end_document