I am writing an article in Sweave (knitr), which is a translator from the statistical language R to TeX. Whenever I produce a figure in Sweave it returns something like the following code for a figure.
\begin{figure}
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
{
\centering \includegraphics[width=\marginparwidth]{example-image-a}
}
\end{knitrout}
\caption{A caption.}
\label{fig:a-label}
\end{figure}
What I'd like to do is to implement a custom marginfigure environment via the scrlayer-notecolumn package using a solution similar to the one here. I am running into Too many {'s. errors, however, that I have not been able to recover.
What I would like to have is something like this:
\begin{marginfigure}
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
{
\centering \includegraphics[width=\marginparwidth]{example-image-a}
}
\end{knitrout}
\end{marginfigure}
for which I have coded the marginfigure environment as follows:
\usepackage{environ}
\makeatletter
\NewEnviron{marginfigure}{%
\expandafter\@marginfigure\expandafter{\BODY}%
}
\newcommand*\@marginfigure[1]{%
\makenote*{%
\begin{nonfloatfigure}#1\end{nonfloatfigure}%
}%
}
\newenvironment{nonfloatfigure}{%
\par\noindent\begin{minipage}{\linewidth}
\def\@captype{figure}%
}{%
\end{minipage}
}
Doing
\makenote*{
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
{
\centering \includegraphics[width=\marginparwidth]{example-image-a}
}
\end{knitrout}
}
on the other hand, works just fine.
The following is a not working example:
\documentclass{scrartcl}
\usepackage{scrlayer-scrpage}
\usepackage{scrlayer-notecolumn}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{environ}
\makeatletter
\NewEnviron{marginfigure}{%
\expandafter\@marginfigure\expandafter{\BODY}%
}
\newcommand*\@marginfigure[1]{%
\makenote*{%
\begin{nonfloatfigure}#1\end{nonfloatfigure}%
}%
}
\newenvironment{nonfloatfigure}{%
\par\noindent\begin{minipage}{\linewidth}
\def\@captype{figure}%
}{%
\end{minipage}
}
\newenvironment{knitrout}{}{} % provided from Sweave
\title{Hello}
\author{Me}
\begin{document}
\maketitle
% Does not work
\begin{marginfigure}
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
{
\centering \includegraphics[width=\marginparwidth]{example-image-a}
}
\end{knitrout}
\end{marginfigure}
\end{document}
