无法将 `\setchapterpreamble` 包装在 `\NewEnviron` 中

无法将 `\setchapterpreamble` 包装在 `\NewEnviron` 中

我试图用 和 以统一的方式包含章节引文scrartclscrbook我的最终文档将使用 ,scrbook但是我scrartcl在写作阶段会用 来编译各个章节。我想要的很容易用 来实现\setchapterpreamble。所以我决定自定义\dictum以获得类似的输出scrartcl。根据加载的类,我定义了一个包装两者之一的环境。包装\dictum效果很好,但编译失败\setchapterpreamble。LaTeX 抱怨\BODY未定义!我尝试过\BODY用重命名\environbodyname,但这没有帮助。

这是一个失败的最小示例:

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

\usepackage{graphicx}
\usepackage{xcolor}
\addtokomafont{chapterprefix}{\raggedleft}
\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
% \@ifclassloaded{scrartcl}{% this works
%   \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}

笔记:我正在使用 XeLaTeX

答案1

您需要在扫描\BODY参数之前进行扩展。\setchapterpreamble

我还提出了一种更好的设置引文归属的方法。

\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
      \hskip2em\vadjust{}\nobreak\hfil---~\emph{#2}%
      \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}

在此处输入图片描述

将对象移动到右边距了解有关神秘代码的更多信息。

我仅使用真正必要的代码来最小化代码。

答案2

这是一个似乎有效的建议。

\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}

相关内容