如何隐藏/显示全部描述description
。
\documentclass[]{article}
\begin{document}
\begin{description}
\item[Keep This] Discard This
\item[Keep This] Discard This
\end{description}
\end{document}
答案1
一个选择是重新定义description
工作方式,捕捉其所有\item[<stuff>]
内容,并忽略一切else。通过局部重新定义并将整个环境放在一个框内,可以实现这种捕获和释放\item
。这样,事情就不会被设置到页面上,但你可以处理环境。
以上简单讨论具体实现如下:
\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}
提供开关来丢弃description
内容(通过\discarddescription
)并恢复其原始功能(通过\restoredescription
)。
答案2
如果你想完全隐藏全部 description
环境你可以简单地重新定义description
环境来忽略其主体中的任何内容包裹environ
.因此通过\RenewEnviron{description}{}{}
:你得到:
如果您仍然想要环境\item
中的标签,description
那么您可以answer
通过相同的方式隐藏环境\RenewEnviron{answer}{}
:
然后,当您想要时,ansers
您可以使用\NewEnviron{answer}{\BODY}
来获取:
代码:隐藏description
环境
\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}
代码:隐藏answer
环境
\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}
答案3
您可以轻松地在命令的不同定义之间切换,添加 % 并删除另一个。我经常这样做,以更改布局。将您的 tex 文档视为一种数据库,在标题中保留尽可能多的格式信息。
\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}
答案4
这利用了这样一个事实:在description
一个有指定可选参数。无论如何,最好给个人环境一个不同于通用环境的名称,因此我定义了一个answers
环境;每个项目都由 引入\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}