9

I am trying to redefine the figure environment, which works fine in general. However, if the body of the figure environment to be replaced includes a tabular environment inside a conditional expression, I receive the error

"Incomplete \ifx; all text was ignored after line 11"

I find this pretty surprising, since my renewed environment does not even used that body. It does not strictly depend on tabular, a tikzpicture raises the same error. Also, it does not depend on \ifpdf, other valid conditionals raise the same error, too.

This is my code:

\documentclass{article}
\usepackage{environ}
\RenewEnviron{figure}{It's gone!}

\usepackage{ifpdf}
\begin{document}
\begin{figure}
\ifpdf
    \begin{tabular}{l}
        This works without RenewEnviron, ifpdf or tabular!
    \end{tabular}
\else\fi
\end{figure}
\end{document}
bers
  • 5,404

2 Answers2

5

At one point the package fails to protect its internal conditional testing from stray if.. tokens in the source. As it has scanned as far as the nested \begin it has scanned over the \ifpdf but not yet seen the matching \fi. A small redefinition moves the collected tokens out of the internal \ifx test:


\documentclass{article}
\usepackage{environ}
\makeatletter
\long\def\Collect@@Body#1\end#2{%
  \edef\begin@stack{%
    \Push@Begins#1\begin\end\expandafter\@gobble\begin@stack}%
  \ifx\@empty\begin@stack
    \endgroup
    \@checkend{#2}%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
   {\Addto@Envbody{#1}}%
   {\Addto@Envbody{#1\end{#2}}}%
  \process@envbody}
\makeatother

\RenewEnviron{figure}{It's gone!}

\usepackage{ifpdf} \begin{document} \begin{figure} \ifpdf \begin{tabular}{l} This works without RenewEnviron, ifpdf or tabular! \end{tabular} \else\fi \end{figure} \end{document}

David Carlisle
  • 757,742
2

The environ package might be considered obsolete, now that the LaTeX kernel allows \NewDocumentEnvironment and \RenewDocumentEnvironment to take an argument specifier b (standing for “body”).

The syntax is a bit different

\documentclass{article}
\usepackage{ifpdf}

\RenewDocumentEnvironment{figure} {+b}% argument specifiers {It's gone!}% begin part {}% end part

\begin{document}

\begin{figure} \ifpdf \begin{tabular}{l} This works without RenewEnviron, ifpdf or tabular! \end{tabular} \else\fi \end{figure}

\end{document}

because the “end part” has to be specified (differently from environ). The + prefix means that empty lines are allowed in the environment.

egreg
  • 1,121,712