# Cannot wrap \setchapterpreamble in \NewEnviron

I am trying to include chapter quotes in a uniform way with scrartcl as well as scrbook. My final document will use scrbook however I am compiling individual chapters with scrartcl during the writing phase. What I want is easily achieved with \setchapterpreamble. So I decided to customise \dictum to get a similar output with scrartcl. Depending on which class is loaded, I define an environment that wraps either of the two. Wrapping \dictum works well, but compilation fails with \setchapterpreamble. LaTeX complains \BODY is undefined! I have tried renaming \BODY with \environbodyname, but that didn't help.

Here is a minimal example that fails:

\documentclass[11pt,a4paper,twoside,chapterprefix=true]{scrbook} % w/ \setchapterpreamble
% \documentclass[11pt,a4paper,twoside]{scrartcl}                   % w/ \dictum

\usepackage{graphicx}
\usepackage{xcolor}
\renewcommand*{\chapterformat}{%
\mbox{%
% \scalebox{1.5}{\chapappifchapterprefix{\nobreakspace}}%
\scalebox{5}{\color{gray}\thechapter\autodot}\enskip
}
}

% title page info
\author{Suvayu}
\date{\today}
\title{Some title}

% customise dictum
\renewcommand*{\dictumwidth}{0.6\textwidth}
\renewcommand*{\raggeddictum}{\centering}
\renewcommand*{\dictumrule}{\vskip 0.3em}
\renewcommand*{\dictumauthorformat}[1]{— #1\par}

% documentclass aware quote environment for KOMA-script
\usepackage{environ}

% this is what I really want to do
% \makeatletter
%   \NewEnviron{myquote}[1]{\dictum[#1]{\BODY}}
% }{% this does not
%   \NewEnviron{myquote}[1]{%
%     \setchapterpreamble[ol][0.6\textwidth]{%
%       \BODY
%       \null\hfill — \emph{#1}
%     }
%   }
% }
% \makeatother

% TEST: this does not work
\NewEnviron{myquote}[1]{%
\setchapterpreamble[ol][0.6\textwidth]{%
\BODY
\null\hfill — \emph{#1}
}
}

\begin{document}
\frontmatter
\maketitle
\mainmatter

% this does not work
\begin{myquote}{Alejandro Jodorowsky, on Dune}
You want to make the most fantastic art of movie?
Try. If you fail, is not important. We need to try.
\end{myquote}

% TEST: this works
% \setchapterpreamble[ol][0.6\linewidth]{%
%   You want to make the most fantastic art of movie? \\
%   Try. If you fail, is not important. We need to try. \\[1.5em]
%   \null\hfill --- \emph{Alejandro Jodorowsky, on Dune}
% }

\chapter{Some chapter}
Foo, bar, baz

\end{document}


Note: I'm using XeLaTeX

• @esdd @egreg Both solutions work, and AFAIU both work by expanding \BODY beforehand. I don't understand why this is necessary though (sply. since it's not needed with \dictum). Also I don't quite understand how \protected@xdef works, any reference? – suvayu Sep 17 '15 at 12:51

You need to expand \BODY before the argument to \setchapterpreamble is scanned.

I also propose a better way for setting the quote attribution.

\documentclass[11pt,a4paper,twoside,chapterprefix=true]{scrbook}

\usepackage{environ}

\NewEnviron{myquote}[1]{%
\expandafter\myquoteaux\expandafter{\BODY}{#1}%
}
\newcommand{\myquoteaux}[2]{%
\setchapterpreamble[ol][0.6\textwidth]{%
#1%
{{\unskip\nobreak\hfil\penalty50
\parfillskip=0pt \finalhyphendemerits=0 \par
\penalty 10000 \parskip=0pt\noindent}}\ignorespaces
}%
}

\begin{document}

\mainmatter

\begin{myquote}{Alejandro Jodorowsky, on Dune}
You want to make the most fantastic art of movie?
Try. If you fail, is not important. We need to try.
\end{myquote}

\chapter{Some chapter}
Foo, bar, baz

\end{document}


I minimized the code using only what's really necessary.

Here is a suggestion that seems to work.

\documentclass{scrbook}
\usepackage{xltxtra}

\usepackage{environ}
\newcommand*\BODYx{}
\makeatletter
\NewEnviron{myquote}[1]{%
\protected@xdef\BODYx{\BODY}
\setchapterpreamble[ol][0.6\textwidth]{%
\BODYx
\null\hfill — \emph{#1}%
}%
}
\makeatother

\begin{document}

\begin{myquote}{Alejandro Jodorowsky, on Dune}
You want to make the most fantastic art of movie?
Try. If you fail, is not important. We need to try.
\end{myquote}

\chapter{Some chapter}
Foo, bar, baz
\end{document}