Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to build one environment for proofs in my lessons.

\begin{proof}
    ...
\end{proof}

This environment could have one content containing one macro named \lines like in the following example. \lines{n} draws n horizontal rules on which the pupils can write during the lesson.

\begin{proof}
    bla, bla,...
    \lines{4}
    bla, bla,...
    \lines{7}
    bla, bla,...
\end{proof}

I would like to display,or not, proof environments that contains one or more macros \lines so as to have two versions of my lessons : one for the pupils and one showing only texts that I've typed.

Is it possible ?

More precisely, I want to not display the entire proof environment if the \lines macro is used.

share|improve this question
1  
So you want to not display the entire proof environment if the \lines macro is used, or do you want a method to change the \lines macro to do nothing? –  Peter Grill Apr 4 '12 at 18:57
    
I want to not display the entire proof environment if the \lines macro is used. I've updated my question. –  projetmbc Apr 4 '12 at 19:02
    
Do have a minimal example (one theorem and proof with lines)? The reason I ask is because this may be easy if you defined your own proof environment. Otherwise, interaction with packages like amsthm or ntheorem might be required. –  Werner Apr 4 '12 at 19:05
    
For the moment I've not yet build the proof environment. I think to use amsthm or ntheorem that seem to be the good choices. What is the better one ? My proof environment would be a standard one with one left square at the end. –  projetmbc Apr 4 '12 at 19:07
add comment

2 Answers 2

up vote 10 down vote accepted

You can use the environ package along with \newtoggle from the etoolbox package. The code below yields:

enter image description here

The environ package is used here so as to collect the body of the environment in the \BODY macro.

Further Enhancements:

  • As mentioned in the comments, since the material in the environments containing the \lines macro is typeset (even though it may get discarded), any macros that have global side effects are still executed. These need to be disabled at the start of the \sbox so that they do not have any unintended side effects. Below I have disabled the \label and \stepcounter macros, but there may be many others that need to be disabled, depending on the application. The only macro that should not be disabled really is of course the \lines macro.

Notes:

  • The \lines macro does not yet draw the lines. The appropriate code needs to be added.
  • The solution here used the \newtoggle from the etoolbox package, as I find that syntax more readable. This can be adapted to the numerous other options for evaluating conditionals such as those mentioned in LaTeX conditional expression.

Code:

\documentclass{article}
\usepackage{amsthm}
\usepackage{environ}

\usepackage{etoolbox}
\newtoggle{LinesMacroUsed}%

\newcommand*{\lines}[1]{\global\toggletrue{LinesMacroUsed}}%

\newbox{\MyBody}
\NewEnviron{MyProof}{%
    \global\togglefalse{LinesMacroUsed}%
    \sbox{\MyBody}{%
        % Disable any commands here so that there are not any unwanted side effects
            \renewcommand{\ref}[1]{}%
            \renewcommand{\stepcounter}[1]{}%
        % Typeset the body onto a box to see if the \lines macro was called
            \vbox{\BODY}%
    }%
    \iftoggle{LinesMacroUsed}{%
    }{%
        \begin{proof}%
            \BODY% Using the \savebox here has issues for case of multiple lines
        \end{proof}%
    }%
}{}%

\begin{document}
\begin{MyProof}
    bla, bla,...
    \lines{4}
    bla, bla,...
    \lines{7}
    bla, bla,...
\end{MyProof}
%
\begin{MyProof}
    bla, bla, (without using lines macro)

    bla, bla, (without using lines macro)

    bla, bla, (without using lines macro)
\end{MyProof}
%
\begin{MyProof}
    bla, bla,...
    \lines{4}
    bla, bla,...
    \lines{7}
    bla, bla,...
\end{MyProof}
%
\end{document}
share|improve this answer
2  
There are some limitations with this method: 1. the body may not contain material that cannot appear in restricted horizontal mode, and 2. if the body contains material that has global side effects (like incrementing latex counters, defining labels, etc) the side effect will happen once when the content is ignored and twice when it isn't ignored. –  mpg Apr 5 '12 at 23:25
    
You could say \sbox{\MyBody}{\vbox{\BODY}} instead, so that vertical mode material won't disturb. Maybe also redefining \stepcounter and \label so that they are no-ops. –  egreg Apr 5 '12 at 23:47
    
Yeah, sure and the user can add any other commands likely to occur and that may cause problems, which may be convenient or not depending on the variety of material likely to appear in a MyProof. By the way, even with \vbox, the trick still causes issues with floats, but obviously float commands can be disabled too for the test pass. –  mpg Apr 6 '12 at 9:09
add comment

Using environ to grab the environment's body looks like the obvious thing to do. Then I suggest scanning the body, checking for a \lines token. This can be done easily using a macro with delimited argument if we're sure the \lines token will never be inside braces. Otherwise, using ted is a safe option: do a dummy substitution to count the number of occurences of \lines in the environment's body.

\documentclass[a4paper]{article}
\usepackage{amsthm}
\usepackage{environ, ted}

\makeatletter
\NewEnviron{Proof}{%
  \Substitute*{\BODY}{\lines}{}%
  \ifnum\ted@count=\z@
    \begin{proof}
      \BODY
    \end{proof}
  \fi
}
\makeatother

\newcommand*\lines[1]{(#1)}

\begin{document}

\begin{Proof}
  bli
  \begin{equation}
    2 + 2 = 4
  \end{equation}
  {\large \lines{1}}
  bli
\end{Proof}

\begin{Proof}
  bla
  \begin{equation}
    2 + 2 = 4
  \end{equation}
  bla
\end{Proof}

\arabic{equation}

\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.