I'm writing a book with multiple chapters, all children of the main book.Rnw
file, and each chapter can have
multiple child Rnw files for sections, examples, etc. For ease of reference in the draft document, it is very useful to display the path name of the current child Rnw file in the LateX header.
This question follows up on a similar one, Need the filename of the Rnw when knitr runs in rStudio, where it was suggested to use something like
<<>>=
infile <- knitr:::knit_concord$get("infile")
@
However, I've been unable to make this work automagically with knitr, as I could do when using straight LaTeX. There, whenever a file is \input{}
or \include{}
ed, a global LaTeX symbol, @filef@und
is automatically updated. This allows page headers to be updated using the following code in the preamble:
%% Page Headings
\makeatletter
\usepackage{fancyhdr}
\pagestyle{fancy}
%
%% The next two lines are only for draft printing
\def\infoleft{\quad [{\small\ttfamily\@filef@und}]}
\def\inforight{[\number\month-\number\day-\number\year]\quad}
%
\renewcommand{\chaptermark}[1]{%
\markboth{\thechapter\ #1}{}}
\renewcommand{\sectionmark}[1]{%
\markright{\thesection\ #1}}
\lhead[\fancyplain{}{\bfseries\sffamily\thepage}]%
{\fancyplain{}{{\bfseries\sffamily\rightmark}\infoleft}}
\rhead[\fancyplain{}{\inforight{\bfseries\sffamily\leftmark}}]%
{\fancyplain{}{\bfseries\sffamily\thepage}}
\cfoot{}
\makeatother
I think what I'm looking for is a chunk hook that will record the path name of a child document in a global R variable and then gdef
that into LaTeX using Sexpr{}
. Something like:
<<ch02-ex1, child="ch02/ex1.Rnw">>=
@
triggers something like below, giving infile = ch02/ex1.Rnw
<<>>=
infile <- knitr:::knit_concord$get("infile")
@
and then inserts something like the following into the .tex output.
\gdef\infile\Sexpr{infile}