I am writing in RMarkdown, knitting to md, and converting the md to pdf via Pandoc with a latex template I defined. Here is how the following chunk looks in my pdf:```{r mock-structure, results='asis', include=TRUE, tidy=TRUE, background="#F7F7F7"}set.seed(18495)# clusters, sites, medical providers, and intitial encounters(c <- k.clusters) # total number of clusters (defined earlier)spc <- sample(1:4, size = c, TRUE) # number sites per clusters <- sum(spc) # total number of sitesmps <- sample(2:10, size = s, TRUE) # number of medical providers per sitem <- sum(mps) # total number of medical providersN <- sample(0:50, size = m, TRUE) # number of initial encounters per provideri <- sum(N) # total number of initial encounters```I'd like it to look more like this example from the knitr manual.Specifically, I would like to change the code decoration and add the background. I thought including background="#F7F7F7" in the chunk options would do it, but no luck.I also tried defining the following to use a theme, but no luck. I'm not sure I'm using it correctly.require(knitr)thm <- knit_theme$get("print")knit_theme$set(thm)opts_knit$set(out.format = "latex")I verified that I have the framed latex package installed, and I included it in my latex template preamble. Does anyone notice obvious errors? Am I doing something incorrectly on the Rmd side? Is this a latex template problem?Any examples out there from folks with the same setup? Rstudio Rmd to md to pdf via Pandoc?ThanksEric--
You received this message because you are subscribed to the Google Groups "knitr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knitr+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Mar 18, 2013 at 11:10 AM, Eric Green <epg...@gmail.com> wrote:
Thanks, Carl. I think your suggestion to modify how code chunks are handled in my latex template points me in the correct direction. I was initially confused because all of the other code chunk options in Rstudio seem to pass along to Pandoc (rmd to md to pdf), but I could not get the "background" option to work.
Correct, many options are identical for all output formats (e.g. cache, comment, echo, includes, dependson, warning, etc); background happens to be unique to LaTeX.
The difficulty is that there isn't one agreed upon environment to represent code blocks in LaTeX. Knitr creates its own custom latex environment, knitrout, when used in tex format with the background option. Various syntax highlighting packages for LaTeX have invented their own environments, e.g. "minted".
Pandoc happens to use the environments "Shaded" and "Highlighted" for code blocks. We can set custom colors for these by (re)defining them in the latex template file as I mentioned earlier, e.g. see the header of this template
Though Pandoc already comes built in with a bunch of toggles for its own highlighting. e.g. if you want a faint background to your code block you might simply prefer something like pandoc --highlight-style=tango
On a side note, I'm not sure I follow your comment about mixing LaTex code and markdown. In my Rmd file, I mix Rmarkdown (i.e., titles and body text) with R code chunks and LaTex code for tables and figures. This all passes fine to Pandoc to create the pdf. I think I am just misunderstanding your point, but I follow your overall message.
Good point -- telling R to generate LaTeX tables with results="asis" is kind of a hack that will mix LaTeX and and markdown in the output. It is a hack because the knitr doesn't know you are generating special latex code, it just knows you want the output of the function call to be rendered "asis". That would be sensible if the document was already in LaTeX.
Think about the file that results -- it has a funny mixture of markdown and latex. Luckily in your case pandoc is smart enough (or stupid enough) to just ignore the latex code it sees when it expected markdown. That behavior cannot be guaranteed though -- for instance, here is a little example where we generate a latex table in a markdown file that will cause pandoc to choke:
For your workflow, you would do better to generate pandoc.tables (from the pander package) in your R markdown so that you have always have a sane (pandoc-flavored) markdown file to pass to pandoc, rather than some unpredictable chimera.
Overall, I think it is good to try and abstract styling from content (the way markdown does), but funny things can happen once you start trying to tune the styling from within your content file (the R markdown file). Much better to let the markdown be markdown and tune the appearance by using the external tools (latex template files, CSS files, pandoc highlighter options, etc).
Hope this helps.
Carl
Eric,
Correct, there's no shading in that example, I just meant it as a way of customizing the shaded environment. You can check out the tex files pandoc creates using the various options to --highlight-style for more examples. For instance, the "tango" style creates a shaded background by generating latex header that looks like this:
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{{#1}}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{{#1}}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{{#1}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{{#1}}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\RegionMarkerTok}[1]{{#1}}
\newcommand{\ErrorTok}[1]{\textbf{{#1}}}
\newcommand{\NormalTok}[1]{{#1}}
Clearly you can customize these templates and pass them to pandoc to generate a syntax scheme that is different than one of the built-in themes.