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 know that this question is basically identical to that of Hide custom environment content based on boolean

Except I want to avoid using the environ package, which, otherwise, would be the best option in my opinion, and very simple to implement what I want.

I am using the xifthen package, to easily manage my booleans, I know I could use TeX privatives, i.e. \newif\if<name>, but it gets a little complicated when I want to create booleans as arguments without using \csname and expandafter, which I don't fully understand.

I am wanting to hide some content, based on a boolean, and in other environments change it 's formatting.

I tried at first:

\newboolean{full}
% ...
% a little later
% ...
\newenvironment{full}{%
    \ifthenelse{\boolean{full}}{%
}{%
    }{}
}

Which of course didn't work, so I tried:

\newboolean{full}
% ...
% a little later
% ...
\newenvironment{full}{%
    \ifthenelse{\boolean{full}}\begingroup
}{%
    \endgroup{}
}

Hoping that did the trick, but coming up with an Incomplete \iffalse error

I then tried a TeX based approach:

\newboolean{full}
% ...
% a little later
% ...
\def\full{\iffull\begingroup}
\def\endfull{\fi\endgroup}

resulting in the same error. Can someone lend me a hand? I've been searching the net for about an hour or so not getting anywhere.

I stress again, I want to avoid using other packages, I would like to using pure TeX/LaTeX commands here.

Thanks.


Edit

I have been using @MarcoDaniel for a while now, but decided to try and make it more my own, using more LaTeX then TeX code.

I have complicated it a bit, to make it more general so that I can have multiple environment working in a similar way, DRY programming, etc..

But It's not working, and I was wondering if people can see my mistake.

The currently used (fully working) code:

\newenvironment{full}{%
    \ifthenelse{\boolean{full}}%
     {\let\full@i\relax\let\endfull@i\relax}%
    {\def\full@i{\setbox\z@\vbox\bgroup}%
      \def\endfull@i{\egroup}}%
    \full@i%
}{%
  \endfull@i%
}

The code I'm trying (but failing) to get to work:

\newcommand{\BooleanHideEnvironment}[1]{%
    \newenvironment{#1}{%
        \ifthenelse{\boolean{#1}}{%
            \newenvironment{#1@body}{}{}
        }{%
            \newsavebox{#1@bin}
            \newenvironment{#1@body}{\savebox{#1@bin}\begingroup}{\endgroup}
        }
        \begin{#1@body}
    }{\end{#1@body}}
}

\BooleanHideEnvironment{full}
\BooleanHideEnvironment{brief}
\BooleanHideEnvironment{refs}

Any further thoughts where I am going wrong?

To note, all the booleans are already defined, and this is located in a *.cls file, and like a *.sty file, does not require the \makeatletter and \makeatother.

share|improve this question
    
The only really reliable way to skip an environment is to use the 'body collection' approach. Thus you are either going to need to more-or-less copy-paste a reasonable amount of code or load a package that does that for you. The latter approach is easier: could I ask the specific reason for needing to avoid loading other packages? –  Joseph Wright Sep 22 '12 at 12:45
    
**@DavidCarlisle from a deleted post: ** You don't want to allocate a new box every time, you will run out of registers. the box is never used anyway so just use box \z@ as in the original. Also \savebox{#1@bin} takes the content as a macro argument not an environment that is why you need lrbox environment or the \setbox primitive. Finally there are several % missing from ends of lines. This doesn't look like an answer, perhaps you should delete the text as an answer and post again as a new question. –  parnmatt Oct 2 '12 at 14:24

2 Answers 2

up vote 9 down vote accepted

Here a suggestion:

\documentclass{scrartcl}
\usepackage{kantlipsum}
\usepackage{xifthen}
\newboolean{full}

\makeatletter
\newenvironment{full}{%
    \ifthenelse{\boolean{full}}%
     {\let\full@i\relax\let\endfull@i\relax}%
    {\def\full@i{\setbox\z@\vbox\bgroup}%
      \def\endfull@i{\egroup}}%
    \full@i%
}{%
  \endfull@i%
}
\makeatother
\begin{document}
\kant[1]
\begin{full}
\kant[2]
\end{full}

Follow text
\end{document}

How it works:

inside the environment full will be defined a second environment full@i which depends on the value of the boolean full.

If the boolean full is true, the inner definition of the environment full@i is relax. If the boolean false the complete contents will be save in the box \z@ which won't be printed.

share|improve this answer
    
@DavidCarlisle: Indeed but there is no need of usebox or box. So you don't care about text with etc. –  Marco Daniel Sep 22 '12 at 13:01
    
@DavidCarlisle: You can add this as a possibility –  Marco Daniel Sep 22 '12 at 13:12

There is no perfectly general way to do this.

There are essentially three ways.

  1. Change catcodes so that \ and {} are not special and then make a macro with argument delimited by the string \end{full} that works but has the same restrictions as verbatim as what is allowed in the content and where the environment might appear.

  2. You can make an argument that is delimited by #1\end#2 that checks #2 is full and if so stops and if not recursively calls itself until it finds the end of the right environment. This is what environ package offers or tabularx and align and similar AMS environments do the same. This is perhaps closest to what you want but the details of setting it up are a bit tricky and using an existing version from a package would seem like a good idea.

  3. Or you could use the standard latex lrbox environment to save the contents of the box and then in the end code use \ifthenelse to use or not the box. This is perhaps the simplest and most robust method but you have to decide if you are going to use just lrbox in which case only horizontal (LR) mode material is allowed, so no displayed lists or equations, or if you are going to use lrbox together with minipage in which case you have to decide the width of the box, you can make it \textwidth but then you can't use the environment in list environments or other places where the line width is reduced. (Perhaps \linewidth would be a better default width). Also you have to decide whether you are going to simply use the box with \usebox which is easy but will not allow page breaking, or if you are going to unbox the contents which is a more advanced topic which allows page breaking but it isn't always easy to get the spacing correct.

share|improve this answer
3  
In his answer @MarcoDaniel shows an interesting variant of (3) which only boxes the content in the case it is not used, so you don't have to worry about setting an accurate text width or unboxing. –  David Carlisle Sep 22 '12 at 13:07

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.