最小不起作用的示例:
\documentclass{article}
\usepackage{amsmath,amsthm,amssymb,varwidth,xstring,environ}
\newsavebox{\tempbox} %Allows to use fbox in newenvironment
\makeatletter
\NewEnviron{result}[1][\textwidth]{%
\StrPosition{\BODY}{\qedhere}[\@Position]%
\IfEq{\@Position}{0}{%
\BODY
}{%
%% Does contain \qedhere
}%
}%
\makeatother
\begin{document}
\begin{result}[7cm]
abc $\mathcal{A}$
\end{result}
\end{document}
上面的代码由于未知原因无法运行。更奇怪的是,如果我使用原始代码:
\documentclass{article}
\usepackage{amsmath,amsthm,amssymb,varwidth,xstring,environ}
\newsavebox{\tempbox} %Allows to use fbox in newenvironment
\makeatletter
\NewEnviron{result}[1][\textwidth]{%
\StrPosition{\BODY}{\qedhere}[\@Position]%
\IfEq{\@Position}{0}{%
%% Does not contain \qedhere
\begin{center} %The box will be centered
\begin{lrbox}{\tempbox} %Allows to use fbox in newenvironment
\begin{varwidth}{#1} %Minipage that set its width to the text width it contains
\begin{center}
\BODY
\end{center}
\end{varwidth}
\end{lrbox}\fbox{\usebox{\tempbox}}
\end{center}
}{%
}%
}%
\makeatother
\begin{document}
% \begin{result}[7cm]
% abc $\mathcal{A}$
% \end{result}
\end{document}
如果我使用以下代码进行测试,它可以工作:
\begin{result}[7cm]
abc $\mapsto $
\end{result}
\begin{center}
\begin{lrbox}{\tempbox}
\begin{varwidth}{7cm}
\begin{center}
abc $\mathcal{A}$
\end{center}
\end{varwidth}
\end{lrbox}\fbox{\usebox{\tempbox}}
\end{center}
答案1
看起来 xstring 不喜欢 \mathcal。您可以使用内置的 LaTeX 工具:
\documentclass{article}
\usepackage{amsmath,amsthm,amssymb,varwidth}
\newsavebox{\tempbox} %Allows to use fbox in newenvironment
\ExplSyntaxOn
\NewDocumentEnvironment{result}{O{\textwidth}+b}
{
\tl_if_in:nnTF {#2}{\qedhere}
{}{#2}
}{}%
\ExplSyntaxOff
\begin{document}
\begin{result}[7cm]
abc $\mathcal{A}$
\end{result}
\end{document}