2

Minimal not working example:

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

The code above doesn't work for an unknown reason. Enven stranger is that if I use my original code :

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

and if I test with the following code, it works :

\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 Answer 1

2

Looks as if xstring doesn't like the \mathcal. You could use the in-built LaTeX tools:

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.