如何隱藏/顯示全部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>]
內容並忽略一切別的。透過局部重新定義\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}