3

How can one hide/display all descriptions in a description environment at the same time.

\documentclass[]{article}

\begin{document}

\begin{description}
\item[Keep This] Discard This
\item[Keep This] Discard This
\end{description}

\end{document}
6
  • it's not clear what your question is really, you just have one begin end pair around each answer, so given latex syntax that's "normal" rather than "painstaking" isn't it? What markup do you want? Commented Dec 18, 2014 at 21:32
  • You may have to consider capturing the contents between \item and another \item (with special care taken for the last one). However, how do you handle [A] and [B] in the above case? Do you include both the problem and the answer in the same \item? Perhaps we need to see a more comprehensive example...
    – Werner
    Commented Dec 18, 2014 at 21:32
  • [A] and [B] should always be there, whereas the answers should selectively be there. One option (perhaps?) would be a modified description environment where item labels are always visible, but the items themselves can be selectively (as a group!) hidden or displayed.
    – Henry
    Commented Dec 18, 2014 at 21:36
  • @DavidCarlisle in the same way a description environment simplifies writing a typical list of items and their description, I'm sure an environment exists or can be created to do precisely the same thing where the descriptions can be hidden en masse...
    – Henry
    Commented Dec 18, 2014 at 21:38
  • @Henry: Do you have anything following [A] or [B] that does not belong inside the answer environment?
    – Werner
    Commented Dec 18, 2014 at 21:43

4 Answers 4

2

One option would be to redefine the way description works, capturing all its \item[<stuff>] content and disregarding everything else. This catch-and-release is possible through a local redefinition of \item and placing the entire environment inside a box. That way things are not set to the page, but you can process the environment.

The above brief discussion is implemented below:

enter image description here

\documentclass{article}

\let\olddescription\description% Store \description (\begin{description})
\let\endolddescription\enddescription% Store \enddescription (\end{description})
\newsavebox{\descriptionbox}
\makeatletter
\newcommand{\discarddescription}{%
  \renewenvironment{description}
    {\gdef\descriptionlist{}% Clear description list
     \begingroup% Begin local scope
     \def\item[####1]{\g@addto@macro\descriptionlist{\item[####1]}}% Redefine \item[.] to capture its argument
                                                                   % and store it in \descriptionlist
     \begin{lrbox}{\descriptionbox}}% Start capturing elements
    {\end{lrbox}% End capturing elements
     \endgroup% Close local scope
     \begin{olddescription}\descriptionlist\end{olddescription}}% Set captured items in regular description
}
\newcommand{\restoredescription}{%
  \let\description\olddescription% Restore \description (\begin{description})
  \let\enddescription\endolddescription% Restore \enddescription (\end{description})
}
\makeatother

\begin{document}

\begin{description}
  \item[Keep this] Definitely keep this
  \item[Keep that] Definitely keep that
\end{description}

\discarddescription

\begin{description}
  \item[Keep this] Discard this
  \item[Keep that] Discard that
\end{description}

\restoredescription

\begin{description}
  \item[Keep this] Definitely keep this
  \item[Keep that] Definitely keep that
\end{description}

\end{document}

Switches are provided to discard description content (via \discarddescription) and restore its original functionality (via \restoredescription).

4

If you want to completely hide all description environments you can simply redefine the description environment to ignore anything in its body via the environ package. So with \RenewEnviron{description}{}{}: you obtain:

enter image description here

If you still want the \item labels form the description environments then you can hide the answer environment in the same manner via \RenewEnviron{answer}{}:

enter image description here

Then, when you want the ansers you can use \NewEnviron{answer}{\BODY} to obtain:

enter image description here

Code: Hide description environment

\documentclass[]{article}
\usepackage{environ}
\usepackage{comment}

%\excludecomment{answer}
\includecomment{answer}

\RenewEnviron{description}{}{}

\begin{document}
Some text before.
\begin{description}
\item[A]
    \begin{answer}
    Answer A
    \end{answer}
\item[B]
    \begin{answer}
    Answer B
    \end{answer}
\end{description}
Some text after.
\end{document}

Code: Hide answer environment

\documentclass[]{article}
\usepackage{environ}

\NewEnviron{answer}{}%       <--- Use this to hide the answer environment
%\NewEnviron{answer}{\BODY}% <--- Use this if you want the answer environment

\begin{document}
Some text before.
\begin{description}
\item[A]
    \begin{answer}
    Answer A
    \end{answer}
\item[B]
    \begin{answer}
    Answer B
    \end{answer}
\end{description}
Some text after.
\end{document}
5
  • Hi Peter, I may have been unclear - I want [A] and [B] labels to remain (so a list of bold A, B, ... should be the result).
    – Henry
    Commented Dec 18, 2014 at 21:50
  • @Henry: As you still want the labels, I have updated solution to hide just the answer environment. Commented Dec 18, 2014 at 21:53
  • I'm sorry ok I'm going to update my question to be as clear as possible on this one!!
    – Henry
    Commented Dec 18, 2014 at 21:54
  • please see my updated question. The 'answer' environment was just my attempt (I tried to show my approach, but really I should have started with just my original problem).
    – Henry
    Commented Dec 18, 2014 at 21:56
  • @Henry: The alternate solution (which does not use the comment pacakge) already provides for this (but does requite the \item content to be an environment). Commented Dec 18, 2014 at 22:00
2

You can switch easily between different definitions of a command, added a % and deleting another one. I do this a lot, to change layout. Think of your tex document as a kind of database, keeping as much formatting information as possible in the header.

\documentclass[]{article}

%\newcommand*{\thing}[2]{\item[#1] #2}
\newcommand*{\thing}[2]{\item[#1]}

\begin{document}


\begin{description}
\thing{A}{A}
\thing{B}{B}
\end{description}

\end{document}
2

This exploits the fact that in a description one has to specify the optional argument. It's better to give personal environments a name different from the generic one anyway, so I defined an answers environment; each item is introduced by \answer.

\documentclass{article}
\usepackage{environ}

\newif\ifshowanswers
%\showanswerstrue % uncomment for showing answers

\NewEnviron{answers}{%
  \begin{description}
  \ifshowanswers
    \let\answer\item
    \BODY
  \else
    \expandafter\processitems\BODY\answer\processitems
  \fi
  \end{description}
}

\makeatletter
\long\def\processitems\answer[#1]#2\answer#3\processitems{%
  \item[#1]%
  \if\relax\detokenize{#3}\relax
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
  {\processitems\answer#3\processitems}% restart the recursion
}

\begin{document}

\begin{answers}
\answer[A] Discard answer text A
\answer[B] Discard answer text B
\end{answers}

% this is by way of example
\showanswerstrue

\begin{answers}
\answer[A] Discard answer text A
\answer[B] Discard answer text B
\end{answers}

\end{document}

enter image description here

1
  • This is an incredibly good answer, unfortunately I don't feel qualified to judge which one of the several is the 'best'. Thank you!
    – Henry
    Commented Dec 19, 2014 at 3:19

You must log in to answer this question.

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