15

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.

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.

3
  • 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

10

The memoir class has a facility just for that:

\newcomment{marked}
\commentson{marked}
%\commentsoff{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.

8

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}
4
  • 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
7

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}
\usepackage{adjustbox}

\newenvironment{marked}{%
    \begin{adjustbox}{minipage=0.8\textwidth,cfbox=yellow,env=center}
}{%
    \end{adjustbox}
}

%% Uncomment to make 'marked' environments disappear. (memoir)
%\newcomment{marked}
%\commentson{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}
4
  • 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
4

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}

The other answers address your other questions

4
  • 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
4

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

\newif\ifcomm\commtrue

prepare your environment as

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

So, you can put into your text

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

and adjust things with \commtrue or \commfalse .

1
  • 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 .