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:


and then just change it to


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:

            ...text here...

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.

Also - how would I go about adding padding to the inside of my box?

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

  • 1
    That should be \newenvironment{marked}... not \marked.
    – Seamus
    Sep 20, 2011 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. Sep 21, 2011 at 3:51
  • Sorry, I got confused with \newcommand.
    – Peter
    Sep 21, 2011 at 8:59

5 Answers 5


The memoir class has a facility just for that:


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:


With this definition, you can write

Text to be produced on a yellow background as wide as 80\%
of the normal line width.

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


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

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

Comment \textbackslash def to remove:
   .. marked text...

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.

%\DisplayMarkedfalse% Un-comment out to remove "...more text..."

Un-comment \textbackslash DisplayMarkedfalse to remove:
   .. more text...

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.


    \BODY% Comment this to remove `...more marked tex...`

Comment \textbackslash BODY to remove:
   .. more marked text...

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.



\MyBox{...text here...}
  • You could just do \newif\ifDisplayMarked with \DisplayMarkredtrue or \DisplayMarkredfalse and then \ifDisplayMarked .. \fi. Also I personally (biased! ;-) ) would prefer adjustbox over environ. Sep 21, 2011 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. Sep 21, 2011 at 5:09
  • Can I just ask why you add comments onto the end of each line?
    – Peter
    Sep 21, 2011 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. Sep 21, 2011 at 14:58

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.



%% Uncomment to make 'marked' environments disappear. (memoir)

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


Some text.
Some other text.

  • 1
    Why do you use \let\endmarked\relax? Nov 26, 2011 at 11:23
  • 6
    @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. Nov 26, 2011 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) Nov 26, 2011 at 15:32
  • I was stranded on the internet until I read "The \let\endmarked\relax clears the end macro" and it saved my day!
    – PatrickT
    Nov 5, 2017 at 17:23

The definition


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


The other answers address your other questions

  • 1
    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). Sep 21, 2011 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, 2011 at 4:14
  • I meant \newenvironment{marked}{}{} in my comment.
    – cmhughes
    Sep 21, 2011 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}. Sep 21, 2011 at 4:38

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


prepare your environment as


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, 2012 at 5:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .