1

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}
Community
  • 1
  • 1
user101089
  • 3,756
  • 1
  • 26
  • 53

1 Answers1

0

Instead of knitr:::knit_concord$get("infile"), I think you should be using current_input() nowadays. But then you'd need to write a simple function for the second part, i.e.

setInfile <- function() cat("\\gdef\\infile ", current_input())

and put a call to this in an "asis" chunk at the top of the child. (I haven't tried this; you may need some sanitizing of the current_input() result so that line gives legal LaTeX. You may also need another call in the parent to switch back.

user2554330
  • 37,248
  • 4
  • 43
  • 90