Search code examples
rlatexknitrsweave

knitr: Chapter name is the name of R chunk code


I want to use the R chunk code output as the Chapter Name but could not figured out how to do this. Below is my minimum working example.

\documentclass{book}
\usepackage[T1]{fontenc}

\begin{document}

\chapter{cat(
<< label=Test1, echo=FALSE, results="asis">>=
2+1
@
)
}

Chapter name is the output of R chunk Code.

\end{document}

Solution

  • This works for me

    << label=Test1, echo=FALSE>>=
    cn <- 2+1
    @
    
    \chapter*{Chapter \Sexpr{cn}}