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 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:

\newif\ifshow
\showfalse
\newenvironment{myenv}
{\ifshow\textbf{Content of myenv:}}
{\fi}

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

3 Answers 3

up vote 17 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.

\usepackage{comment}
\ifshow
  \newenvironment{myenv}{\textbf{Content of myenv:}}{}
\else
  \excludecomment{myenv}
\fi
share|improve this answer

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

\usepackage{environ}
\NewEnviron{myenv}{
  \ifshow
    \textbf{Content of myenv: \BODY}
  \fi}
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

\newcommand\suppress[1]{} 

in combination with

\newcommand\myOptElse[3]{\ifthenelse{\boolean{#1}}%
{#2\suppress{#3}}%
{\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.

Example:

%Declare the boolean variable    
\newboolean{showmyenv} %Testblöcke anzeigen
%
%Set to OFF
\setboolean{showmyenv}{false}
\myOptElse{showmyenv}{MyEnv content}{Alternative content} 
%the above will show "Alternative content", if you do not want any, leave it empty
%
%Set to ON
\setboolean{showmyenv}{true}
\myOptElse{showmyenv}{MyEnv content}{Alternative content} 
%the above will show "MyEnv content"
share|improve this answer
2  
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

 
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.