4

This question is a follow-up from Solution environment via true/false switch where Ian helped me to define a solution environment which can be hidden. I tried it as well for a proof environment, but proof is already defined. I tried to work with \RenewEnviron and followed How to ignore everything in the document environment? to get past the first error, but now I get LaTeX Error: \env@proof@save@env undefined. I am looking for a fix (rather than a suggestion to stick with the already defined environment or to use a different name).

\documentclass{scrartcl}

\usepackage[american]{babel}
\usepackage{environ}
\usepackage{amsthm}
\usepackage{amsmath}

\newif\ifshowproof
\showprooftrue

% proof environment
\makeatletter
\providecommand{\env@document@save@env}{}%
\providecommand{\env@document@process}{}%
\RenewEnviron{proof}[1][showproof]{%
  \csname if#1\endcsname
    \trivlist
    \item\relax{\sffamily\bfseries Proof}\par\noindent
    \BODY
    \endtrivlist
  \fi
}
\makeatother

\begin{document}
Proof with hide feature:
\begin{proof}[false]
  This should not appear
\end{proof}

Proof without hide feature:
\begin{proof}
  This should appear
\end{proof}
\end{document}

1 Answers1

6

In my experience, \RenewEnviron has never worked. The only way I know to make it work is to undefine the previous commands and use \NewEnviron:

\documentclass{scrartcl}

\usepackage[american]{babel}
\usepackage{environ}
\usepackage{amsthm}
\usepackage{amsmath}

\newif\ifshowproof
\showprooftrue

% proof environment
% remove the meaning of \proof and \endproof
\let\proof\relax
\let\endproof\relax
% now we can redefine proof
\NewEnviron{proof}[1][showproof]{%
  \csname if#1\endcsname
    \trivlist
    \item\relax{\sffamily\bfseries Proof}\par\noindent
    \BODY
    \endtrivlist
  \fi
}

\begin{document}
Proof with hide feature:
\begin{proof}[false]
  This should not appear
\end{proof}

Proof without hide feature:
\begin{proof}
  This should appear
\end{proof}
\end{document}

However, I would retain the original proof environment, so the QED mechanism remains in place.

\documentclass{scrartcl}

\usepackage[american]{babel}
\usepackage{environ,letltxmacro}
\usepackage{amsthm}
\usepackage{amsmath}

\newif\ifshowproof
\showprooftrue

% proof environment
\makeatletter
\LetLtxMacro\amsthmproof\proof
\LetLtxMacro\amsthmendproof\endproof
\let\proof\relax
\let\endproof\relax
\NewEnviron{proof}[1][showproof]{%
  \csname if#1\endcsname
    \amsthmproof[\normalfont\bfseries Proof\spacefactor3000 ]
    \BODY
    \amsthmendproof
  \fi
}
\makeatother

\begin{document}
Proof with hide feature:
\begin{proof}[false]
  This should not appear
\end{proof}

Proof without hide feature:
\begin{proof}
  This should appear
\end{proof}
\end{document}

Remove the \spacefactor3000 bit (don't forget the space, otherwise) if you want the full stop after Proof.

If you really want a line break after Proof (but you shouldn't), add \mbox{}\par\noindent after the closing bracket.

enter image description here

egreg
  • 1,121,712
  • As you know, I'm not an expert here. But I think with \pushQED{\qed} before \csname and \popQED before \endtrivlist, one can get the qedsymbol as well. – Marius Hofert Apr 26 '14 at 19:34
  • I posted a follow-up question which shows that a \label{} introduces an additional horizontal space (http://tex.stackexchange.com/questions/173664/horizontal-space-in-a-new-environment-after-label) – Marius Hofert Apr 26 '14 at 20:15
  • @MariusHofert Yes, of course. That's because you want a new line after Proof, which you don't want, do you? ;-) – egreg Apr 26 '14 at 20:16
  • ahh... of course... my hope was that a label does not change the spacing, but apparently it does. hmmm... I would like the new line after the label in case there is one :-) – Marius Hofert Apr 26 '14 at 20:18
  • @MariusHofert Besides, what does a \label do there? – egreg Apr 26 '14 at 20:20
  • good point. To refer to the page where the proof starts :-) [this is in a large document and I saw that some of the proofs had a label] – Marius Hofert Apr 26 '14 at 20:21