问题
I'd like to have more control over the font size and line spacing used for code chunks and R output via LaTeX. Using the default render_latex(), I see I can set both globally using (in my premable)
\renewenvironment{knitrout}{\small\renewcommand{\baselinestretch}{.85}}{}
...
\begin{document}
But, say there are chunks I'd like to print in \footnotesize, or customize in some other way. I know I can do this with a LaTeX group, but not with the knitrout environment:
{\small
\renewcommand{\baselinestretch}{.85}
<<arth-csv, eval=FALSE, results='asis'>>=
ID,Treatment,Sex,Age,Improved
57,Treated,Male,27,Some
46,Treated,Male,29,None
...
71,Placebo,Female,68,Some
1,Placebo,Female,74,Marked
@
}
Would it be easier to do this using render_listings() and the listings package?
Can I use custom chunk options and chunk hooks to achieve better control?
回答1:
To partially answer my own question, I discovered the knitr size=
chunk option, which works, AFAIK, only with
render_latex()
and the highlight package. Thus, for a wide output to be set in footnote size, I can use
<<tv3, size="footnotesize">>=
TV <- TV[,,1:3,] # keep only ABC, CBS, NBC
TV <- TV[,,,3] # keep only Persist -- now a 3 way table
structable(TV)
@
Also, the example I used above with results='asis'
should have been done just with a verbatim
LaTeX environment,
not as as code chunk. Even though I had used eval=FALSE
, knitr tries to parse the text and generates a warning.
{\footnotesize
\renewcommand{\baselinestretch}{.85}
\begin{verbatim}
ID,Treatment,Sex,Age,Improved
57,Treated,Male,27,Some
46,Treated,Male,29,None
...
71,Placebo,Female,68,Some
1,Placebo,Female,74,Marked
\end{verbatim}
}
来源:https://stackoverflow.com/questions/20097553/knitr-chunk-options-to-control-line-spacing-font-size-in-output