title: Can I use a knitr inline expression in the title of a latex document?
body: <p>I'd like to use a Knitr/Sweave in-line call (<code>\Sexpr{}</code>) in the title of a LaTeX document, after the \begin{document} command but before the \maketitle command. The in-line R code would extract one or two pieces of information from an R data-frame created early in the R script I'm embedding in LaTeX.</p>
  
  <p>I have a couple of Knitr chunks that create a data.frame from which I derive the information I want to put in the Title. I've tried placing these chunks between LaTeX's \begin{document} call and the \title code, like this:</p>
  
  <pre><code>\documentclass
  [LaTex Preamble]
  \begin{document}
  [%% Knitr chunks that initialize an R data-frame]
  \title \Sexpr{--a snippet of R code that extracts an element from the data-frame --}
  \maketitle
  ... (rest of the LaTeX document)
  </code></pre>
  
  <p>and I've also tried putting the Knitr chunks in the preamble to the LaTeX code before \begin{document} statement.</p>
  
  <p>But in Knitr seems to ignore code (other than initialization) that is placed ahead of the \maketitle call in LaTeX, so the in-line snippets included the title look like errors to Latex and it halts output.</p>
  
  <p>I can't find any information in the Knitr documentation on including in-line code in the Title of a LaTeX document.</p>
  
  <p>Any ideas?</p>
  
  <hr>
  
  <p>OK: Found the solution thanks to the hint from @ben-bolker below. Ben uses the formatting of R chunks before output to an RNW file (in a 2-step Knitr process: latex -> rnw -> pdf) . But I'm compiling the LaTeX file to PDF in one-step without going to an RNW file from inside TeXShop (on Mac OSX). I found that I could get Ben's example to work using the RNW delimiters (&lt;&lt;>>=) and one-step compiling. But I couldn't mix the usual LaTeX chunk-delimiters (%%begin.rcode and %% end.rcode) and the RNW in-line statement hook (\Sexpr{}). The latter didn't work no matter how I fiddled with it. Eventually I found that the correct in-line hook for LaTeX is <strong>\\rinline{}</strong>. </p>
  
  <p>It's not very clear in the Knitr documentation that this is the required format for LaTeX and I found it eventually mainly thanks to Ben's example. Best, Peter</p>
  
  <hr>
  
  <p><strong>Update 2</strong> ... and then there's RTFM (or the 'cheat sheet' in this case): <a href="http://cran.r-project.org/web/packages/knitr/vignettes/knitr-refcard.pdf" rel="noreferrer">http://cran.r-project.org/web/packages/knitr/vignettes/knitr-refcard.pdf</a></p>