Some output documents may need appropriate header information. For example, for LaTeX output, we need to write \usepackage{tikz} into the preamble if we use tikz graphics; this function sets the header information to be written into the output.
set_header(...)
The header vector in opts_knit
is set.
By default, knitr will set up the header automatically. For example, if
the tikz device is used, knitr will add \usepackage{tikz} to
the LaTeX preamble, and this is done by setting the header component
tikz
to be a character string: set_header(tikz =
'\usepackage{tikz}')
. Similary, when we highlight R code using the
highlight package (i.e. the chunk option highlight = TRUE
),
knitr will set the highlight
component of the header vector
automatically; if the output type is HTML, this component will be different
– instead of LaTeX commands, it contains CSS definitions.
For power users, all the components can be modified to adapt to a customized
type of output. For instance, we can change highlight
to LaTeX
definitions of the listings package (and modify the output hooks
accordingly), so we can decorate R code using the listings package.
set_header(tikz = "\\usepackage{tikz}")
opts_knit$get("header")
#> highlight
#> "\\definecolor{fgcolor}{rgb}{0, 0, 0}\n\\newcommand{\\hlnum}[1]{\\textcolor[rgb]{0.502,0,0.502}{\\textbf{#1}}}%\n\\newcommand{\\hlsng}[1]{\\textcolor[rgb]{0.651,0.522,0}{#1}}%\n\\newcommand{\\hlcom}[1]{\\textcolor[rgb]{1,0.502,0}{#1}}%\n\\newcommand{\\hlopt}[1]{\\textcolor[rgb]{1,0,0.502}{\\textbf{#1}}}%\n\\newcommand{\\hldef}[1]{\\textcolor[rgb]{0,0,0}{#1}}%\n\\newcommand{\\hlkwa}[1]{\\textcolor[rgb]{0.733,0.475,0.467}{\\textbf{#1}}}%\n\\newcommand{\\hlkwb}[1]{\\textcolor[rgb]{0.502,0.502,0.753}{\\textbf{#1}}}%\n\\newcommand{\\hlkwc}[1]{\\textcolor[rgb]{0,0.502,0.753}{#1}}%\n\\newcommand{\\hlkwd}[1]{\\textcolor[rgb]{0,0.267,0.4}{#1}}%\n\\let\\hlipl\\hlkwb"
#> tikz
#> "\\usepackage{tikz}"
#> framed
#> ""