TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom environment which adds some text around its content, but I also want it to be able to show or hide its contents depending on a boolean.

I have tried doing the following:

{\ifshow\textbf{Content of myenv:}}

But the first \begin{myenv} raises an Incomplete \iffalse; all text was ignored after line X. It would seem that the \iffalse is causing the \end{myenv} to be ignored, so the \fi is never written.

Any idea on how could I accomplish this?

share|improve this question
up vote 23 down vote accepted

I usually do it the other way around, i.e. define "different environments" based on the if. I use the comment package for excluding the contents of the environment.

  \newenvironment{myenv}{\textbf{Content of myenv:}}{}
share|improve this answer

Another way to do this is to use the environ package and conditionally keep or toss the \BODY.

    \textbf{Content of myenv: \BODY}
share|improve this answer
I think \NewEnviron{myenv}{}{ should be \NewEnviron{myenv}{%. – Martin Scharrer Apr 11 '11 at 9:39
I wonder what's the difference in efficiency is between \NewEnviron environments and the comment package. The first one might be significant slower if there are a lot of sub-environments. AFAIK it scans for \end until the \end{myenv} is reached. The comment package skips everything verbatim instead, which has the initial catcode-change overhead. – Martin Scharrer Apr 11 '11 at 9:41
@Martin: you're right about the syntax. For some reason I thought you could have opening code. As for the efficiency, I hadn't thought about it—I bet it wouldn't make a big difference until you get up to 100 pages or so. I use the first method but it was before I found out about environ so I thought I would throw it out there. – Matthew Leingang Apr 11 '11 at 11:58
The first argument is the opening code. You can set some end-code with the trailing optional argument \NewEnviron{myenv}{code \BODY code}[<end-of-env code>]. – Martin Scharrer Apr 11 '11 at 12:15

I use


in combination with

{\suppress{#2}#3}}% if #1 then #2 else #3

and a boolean variable. This enables me to change between #2 and #3 with an ON/OFF switch #1.


%Declare the boolean variable    
\newboolean{showmyenv} %Testblöcke anzeigen
%Set to OFF
\myOptElse{showmyenv}{MyEnv content}{Alternative content} 
%the above will show "Alternative content", if you do not want any, leave it empty
%Set to ON
\myOptElse{showmyenv}{MyEnv content}{Alternative content} 
%the above will show "MyEnv content"
share|improve this answer
1. You need to include the ifthenelse package for your code. However, generally the commands from etoolbox should be preferred. 2. The questioner asks how to apply this to environments. Can you give a complete working document? – Andrew Swann Jun 5 '13 at 8:22

Your Answer


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.