That is a challenging problem. As Ben Bolker pointed out, it is a one-way flow: when R is done, nothing will come back from LaTeX to R again, so R will not be able to know the value of \ref{SetSeed}
in LaTeX.
However, I do not think it is completely impossible, because you actually have the *.aux
file generated from LaTeX, which you can parse with R for the solution numbers, and update the raw R script from purl()
with this information. One approach is that you use the same label for the R chunk as you used for the Example
environment, and you will get a code chunk in the output like:
## @knitr SetSeed
# R code
Hopefully you will also see this in the *.aux
file after you have run LaTeX on the *.tex
file:
\newlabel{SetSeed}{{1}{1}}
Then you replace SetSeed
in the R code with Solution 1.1
. In all, you need some post-processing of the R script.
You can directly set your listings
settings in your .Rnw
file. Here I have defined a new style Rsetings
.
\lstdefinestyle{Rsettings}{
basicstyle=\ttfamily,
breaklines=true,
showstringspaces=false,
keywords={if, else, function, theFunction, tmp}, % Write as many keywords
otherkeywords={},
commentstyle=\itshape\color{Rcommentcolor},
keywordstyle=\color{keywordcolor},
moredelim=[s][\color{delimcolor}]{"}{"},
}
where the colors are defined as follows.
\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}
A few words about the key-value list in the \lstset
.
- All characters in between and including the
"
are typeset in \color{delimcolor}
- You can set up
otherkeywords
and morekeywords
if you are using more of these in your actual document. (Sorry, I am not yet very familiar with R since I am just starting with it.)
- Click here to learn more about the
listings
package or you can type and enter texdoc listings
in your terminal.
With these, you can now re-write your .Rnw
file as
\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{inconsolata}
%-----------------
%Define the colors you want to use
\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}
%-----------------
%Set up your listings. You can type `texdoc listings` in your terminal
\lstdefinestyle{Rsettings}{
language=R,
basicstyle=\ttfamily,
breaklines=true,
showstringspaces=false,
keywords={if, else, function, theFunction, tmp},
otherkeywords={},
commentstyle=\itshape\color{Rcommentcolor},
keywordstyle=\color{keywordcolor},
moredelim=[s][\color{delimcolor}]{"}{"},
}
\title{Function listings with linebreaks and code highlighting}
\begin{document}
\maketitle
Two ways of printing the code.
<<echo=FALSE>>=
options(width=60)
listing <- function(x, options) {
paste("\\begin{lstlisting}[style=Rsettings]\n",
x, "\\end{lstlisting}\n", sep = "")
}
knit_hooks$set(source=listing, output=listing)
@
<<tidy=TRUE,highlight=FALSE>>=
theFunction <- function(x) {
tmp <- "A really long string that should be line-broken but it would be nice to also see code highlighting colors. The function is in the real code sourced, but for the sake of easier reproducibility, it is written here instead."
}
theFunction
@
\end{document}
And here is the output.
Best Answer
You can include LaTeX code verbatim in RMarkdown source files, and when you output to a format that uses LaTeX (i.e., pdf), the LaTeX is treated exactly as you expect.
However, if you're exporting to a non-LaTeX format, such as a word doc or html, then the exporter will just ignore the LaTeX.