3
votes

I am using the LaTeX package problems to create solution sets from a database of problems. The database is structured like a bibliography database, in a .bib file. The whole system works beautifully for regular LaTeX, but now some of my solutions have R code (in knitr chunks) in them.

The default sequence of knitting/TeXing/BibTeXing in RStudio is not working-- the code ends up in the document verbatim, along with mangled versions of the chunk delimiters. So, I need to find the right workflow of steps to ensure that the code makes it through.

This package seems to be very set on having two files, one for the database and one for the .tex/.rnw, so I can't make a working example, but something like this:

\documentclass{article}
\usepackage[solution]{problems}
\Database{

@QUESTION{1.1,
    problem = {1.1},
    solution = {This solution only uses TeX, like $\bar{x}$. It works fine.}}

@QUESTION{1.2,
    problem = {1.2},
    solution = {This solution includes code
        <<>>=
            head(iris)
        @
        It does not work.
    }}}

\begin{document}
Problems for this week were 1.1 and 1.2
\problems{1.1}
\problem{1.2}
\end{document}
1

1 Answers

3
votes

You will have to knit the .bib file first and then run LaTeX and BibTeX.

While you usually have a .Rnw file that is knitted to .tex and then let LaTeX tools process the .tex and the .bib file you will have to start with a (let's call it) .Rbib file that is knitted to .bib and then processed by LaTeX.

For simplicity, I give the file I called .Rbib above the name bibliography.Rnw but you can choose any extension you like. I chose .Rnw because the syntax used inside is the same as in .Rnw files.

As dummy entries for the bib-file I use data from verbosus.com and added some knitr code.

The first chunk sets global chunk options to prevent knitr from adding the code of the chunks or any markup to the output file. The next chunk shows how for example the title field could be filled with generated content and the \Sexpr{} part is an example how this could be used to add some dynamic text.

<<setup>>=
library(knitr)
opts_knit$set(
  echo = FALSE,
  results = "asis"
)
@

article{article,
  author  = {Peter Adams},
  title   = {
<<echo=FALSE, results = "asis">>=
  cat("The title of the work")
@
},
  journal = {"The title of the journal"},
  year    = 1993,
  number  = 2,
  pages   = {201-213},
  month   = 7,
  note    = {An optional note},
  volume  = 4
}

@book{book,
  author    = {Peter Babington},
  title     = {\Sexpr{"The title of the work"},
  publisher = {The name of the publisher},
  year      = 1993,
  volume    = 4,
  series    = 10,
  address   = {The address},
  edition   = 3,
  month     = 7,
  note      = {An optional note},
  isbn      = {3257227892}
}

It is important to have the chunk option results = "asis" and to use cat() instead of print(). Otherwise, there would be unwanted characters in the output.

If this is saved as bibliography.Rnw the following is enough to get a .bib file with the chunks evaluated:

knit(input = "bibliography.Rnw", output = "bibliography.bib")

After that only standard LaTeX compilation remains.