# \newenvironment issues with environment comment

I am trying to setup the formatting for a LaTeX document (it's got the class memoir), so that it would be fairly simple to add to it in the future.

Two of the features I'd like are: Be able to mark certain parts of the text, so that I can produce two versions of the output, one with the complete text, and one without the marked parts of the text. My original idea was to use:

\newenvironment{\marked}{\begin{comment}}{\end{comment}}


and then just change it to

\newenvironment{\marked}{}{}


when I want the full text. However this won't work unless I add a comment block (\begin{comment}...\end{comment}) after I use the marked environment.

To have boxed areas of text. I can achieve this though using:

\begin{center}
\colorbox{yellow}{
\parbox{0.8\textwidth}{
...text here...
}
}
\end{center}


I would like to create another environment for this, but I'm not sure how to go about it as the text to be contained within the environment will be between open parentheses.

I'm sorry if these questions are overly simple. The only languages I've used before to format stuff are HTML and CSS.

-
That should be \newenvironment{marked}... not \marked. – Seamus Sep 20 '11 at 22:17
Welcome to TeX.sx! Your question was migrated here from another stackexchange site. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. – Martin Scharrer Sep 21 '11 at 3:51
Sorry, I got confused with \newcommand. – Peter Sep 21 '11 at 8:59

## migrated from stackoverflow.comSep 20 '11 at 22:07

The memoir class has a facility just for that:

\newcomment{marked}


It's sufficient to uncomment the third line to exclude the contents of all marked environments. Look at page 284 of memoir's documentation.

For the environment form of a colored background box:

\newsavebox{\coloredbgbox}
\newenvironment{yellowbox}
{\begin{lrbox}{\coloredbgbox}\begin{minipage}{0.8\textwidth}}
{\end{minipage}\end{lrbox}%
\begin{center}\colorbox{yellow}{\usebox{\coloredbgbox}}\end{center}}


With this definition, you can write

\begin{yellowbox}
Text to be produced on a yellow background as wide as 80\%
of the normal line width.
\end{yellowbox}


However, it's better to split different topics into different questions.

-

The comment environment uses verbatim mode to skip its area and can't therefore nested in the normal way. As egreg already pointed out their are specific macros to define an environment as comment.

Here my solution which is honestly very similar to egreg's but uses my adjustbox package to format the environment. It is IMHO a little bit more user friendly. Internally it does about the same as egreg's code. It also shows the required code for other classes in the case other people need something similar with normal LaTeX classes.

\documentclass{memoir}
\usepackage{xcolor}

\newenvironment{marked}{%
}{%
}

%% Uncomment to make 'marked' environments disappear. (memoir)
%\newcomment{marked}

%% Alternative for other classes
% \usepackage{comment}
% \includecomment{marked}
% \let\endmarked\relax

\begin{document}

Some text.
\begin{marked}
test
\end{marked}
Some other text.

\end{document}

-
Why do you use \let\endmarked\relax? – Marco Daniel Nov 26 '11 at 11:23
@MarcoDaniel: Because \includecomment doesn't clear the definition of the end macro and so the \end{adjustbox} inside \endmarked will remain and cause an error. The \let\endmarked\relax clears the end macro and avoids that. I consider this a shortcoming of the comment package. – Martin Scharrer Nov 26 '11 at 14:37
Your solution solved the problem also for mdframed. The package is very old and the syntax of the code isn't transparent for me. Thanks for the small explanation. (I also think it is a bug of comment.sty) – Marco Daniel Nov 26 '11 at 15:32

There are several ways to do the conditional text:

1. Use \ifdefined
2. Use \newif
3. Use NewEnviron from the environ package

To use \ifdefined use \def to ensure that the condition macro is defined. Then, when you don't want the conditional text, just comment the line \def\DisplayMarked{}:

\documentclass{article}
\def\DisplayMarked{}% Comment this to remove "...marked text..."

\begin{document}
Comment \textbackslash def to remove:
\ifdefined\DisplayMarked
.. marked text...
\fi
\end{document}


As Martin pointed out, you could also use \newif\ifDisplayMarked to define the condition. Then you can set this condition to true with \DisplayMarkedtrue and to false with \DisplayMarkedfalse. To use this condition, use \ifDisplayMarked and end the clause with an \fi. There is an optional \else that you can put in here in case there is something else you want to do for the false case.

\documentclass{article}
\newif\ifDisplayMarked%
\DisplayMarkedtrue%
%\DisplayMarkedfalse% Un-comment out to remove "...more text..."

\begin{document}
Un-comment \textbackslash DisplayMarkedfalse to remove:
\ifDisplayMarked
.. more text...
\fi
\end{document}


If you really want an environment for that, a simple way is to use the NewEnviron from the environ package that allows you to use \BODY for the body of the environment.

\documentclass{article}
\usepackage{environ}

\NewEnviron{Marked}{%
\BODY% Comment this to remove ...more marked tex...
}%

\begin{document}
Comment \textbackslash BODY to remove:
\begin{Marked}
.. more marked text...
\end{Marked}
\end{document}


## Boxed Areas of Text:

As far as your second question, it is a simple matter of using #1, which represents the first parameter of the macro.

\documentclass{article}
\usepackage{xcolor}

\newcommand{\MyBox}[1]{
\begin{center}%
\colorbox{yellow}{%
\parbox{0.8\linewidth}{%
#1%
}%
}%
\end{center}%
}%

\begin{document}
\MyBox{...text here...}
\end{document}

-
 You could just do \newif\ifDisplayMarked with \DisplayMarkredtrue or \DisplayMarkredfalse and then \ifDisplayMarked .. \fi. Also I personally (biased! ;-) ) would prefer adjustbox over environ. – Martin Scharrer♦ Sep 21 '11 at 4:54 Yep, good point. I learned ifdefined before \newif so that seems to come to mind first. I will include that option as well. – Peter Grill Sep 21 '11 at 5:09 Can I just ask why you add comments onto the end of each line? – Peter Sep 21 '11 at 8:58 I learned the hard way that those comments are required so that additional spaces do not get added and have gotten into the habit of adding them in my entire preamble. See tex capacity exceeded if remove % after macro. – Peter Grill Sep 21 '11 at 14:58

As suggested by Peter Grill, just use the environ package and define a boolean as

\newif\ifcomm\commtrue


\NewEnviron{marked}{\ifcomm\BODY\fi}


So, you can put into your text

\begin{marked} (stuff) \end{marked}


and adjust things with \commtrue or \commfalse .

-
 You could even put these definitions within a package, define an option for the package and connect the option to the boolean \ifcomm. I found this scheme as quite handy. – rcabane Aug 26 '12 at 5:45

The definition

\newenvironment{\marked}{\begin{comment}}{\end{comment}}


will not work- firstly, \marked should be marked. For the rest of the definition, you could use:

\newenvironment{marked}{\comment}{\endcomment}


 In the case of the comment environment from the comment package or (I guess) from the memoir class, this doesn't work. It uses verbatim mode to skip the content and then neither nesting nor \endcomment works because the \end{comment} is searched for as verbatim text. You need to use \includecomment{marked} (comment package) or \commentson{marked} (memoir class) to make the required internal definitions, so \end{marked} is used as end-marker (no pun intended). – Martin Scharrer♦ Sep 21 '11 at 4:00 @MartinScharrer: I think I've misunderstood - in the testing I did, the marked environment worked as expected: as written, it will comment out code within, and could be toggled to \newenvironment{\marked}{}{} so that the code within is shown. Have I misunderstood something? – cmhughes Sep 21 '11 at 4:14 I meant \newenvironment{marked}{}{} in my comment. – cmhughes Sep 21 '11 at 4:36 Ok, looking on the memoir implementation \comment seems to look at the current environment name (\@currenvir) to detect the end of the comment environment. So your solutions works fine. Sorry for the noise. That's also the reason why it doesn't work with \begin{comment}. – Martin Scharrer♦ Sep 21 '11 at 4:38