我想重新定义description
环境,以便它仅显示其中每个项目的标签。
所以我想要下面的代码
\begin{description}
\item [Truth] A sentence is true iff it expresses a true proposition.
\end{description}
屈服
真相
当文档被编译时,“真相”的格式与描述标签的格式无关。
用例是,我已获得许多重要术语的定义,并希望班上的学生通过复习来生成词汇表。为此,我想打印每个项目的标签,但将其余部分留空以供学生填写。
答案1
以下代码定义了hidedescription
仅使用传统环境打印<label>
与每个相关联的环境。\item[<label>]
description
\documentclass{article}
\usepackage{environ,regexpatch}
\makeatletter
\let\olditem\item% Store regular \item macro
\NewEnviron{hidedescription}{%
% Update each \item[..]... to \itemstart[..]...\itemend
\g@addto@macro{\BODY}{\itemend}% Last item end
\xpatchcmd*{\BODY}{\item}{\itemend\itemstart}{}{}% All items
\xpatchcmd{\BODY}{\itemend\itemstart}{\itemstart}{}{}% First item correction
\def\itemstart[##1]##2\itemend{% Redefine \item to capture contents
\olditem[##1]% Print regular item
}%
\description\BODY\enddescription% Process environment
}
\makeatother
\begin{document}
Text before \verb|hidedescription|:
\begin{hidedescription}
\item[A] quick brown fox jumped over the lazy dog
\item[quick] brown fox jumped over the lazy dog
\item[brown] fox jumped over the lazy dog
\item[fox] jumped over the lazy dog
\item[jumped] over the lazy dog
\item[over] the lazy dog
\item[the] lazy dog
\item[lazy] dog
\item[dog]
\end{hidedescription}
Text after \verb|hidedescription|.
\end{document}
其主要机制是替换原始输入
\begin{hidedescription}
\item[label1] description1
\item[label2] description2
...
\item[labeln] descriptionn
\end{hidedescription}
和
\begin{hidedescription}
\itemstart[label1] description1\itemend
\itemstart[label2] description2\itemend
...
\itemstart[labeln] descriptionn\itemend
\end{hidedescription}
这样,我们就可以定义一个\itemstart
需要特定输入模式的宏(因为\item
a 中的所有 sdescription
通常都有一个可选参数)。
还可以定义一个条件来打印hidedescription
环境\item
描述。
如果您希望完全替换(重新定义)环境description
,那么您可以使用以下设置:
\makeatletter
\let\olditem\item% Store regular \item macro
\let\olddescription\description% Copy description environment start
\let\endolddescription\enddescription% Copy description environment end
\RenewEnviron{description}{%
% Update each \item[..]... to \itemstart[..]...\itemend
\g@addto@macro{\BODY}{\itemend}% Last item end
\xpatchcmd*{\BODY}{\item}{\itemend\itemstart}{}{}% All items
\xpatchcmd{\BODY}{\itemend\itemstart}{\itemstart}{}{}% First item correction
\def\itemstart[##1]##2\itemend{% Redefine \item to capture contents
\olditem[##1]% Print regular item
}%
\olddescription\BODY\endolddescription% Process environment
}
\makeatother
答案2
将环境的内容排版到丢弃的框中;在框中\item
重新定义命令以添加到临时宏中。然后在真实环境中使用此宏description
。
限制:不支持嵌套列表。
\documentclass{article}
\usepackage{environ}
\makeatletter
\NewEnviron{description*}{%
\def\hidedescriptionitems{}%
\setbox0=\vbox{
\def\item[##1]{\g@addto@macro\hidedescriptionitems{\item[##1]}}%
\BODY
}%
\begin{description}\hidedescriptionitems\end{description}
}
\makeatother
\begin{document}
Text before \verb|description*|:
\begin{description*}
\item[A] quick brown fox jumped over the lazy dog
\item[quick] brown fox jumped over the lazy dog
\item[brown] fox jumped over the lazy dog
\item[fox] jumped over the lazy dog
\item[jumped] over the lazy dog
\item[over] the lazy dog
\item[the] lazy dog
\item[lazy] dog
\item[dog]
\end{description*}
Text after \verb|description*|.
\end{document}
(代码来自沃纳的回答)。
该版本还吞噬所有内部环境。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentEnvironment{description*}{+b}
{
\bernhard_hidedesc:n { #1 }
}
{}
\seq_new:N \l__bernhard_hidedesc_items_seq
\tl_new:N \l__bernhard_hidedesc_body_tl
\cs_generate_variant:Nn \seq_set_split:Nnn { NnV }
\cs_new_protected:Nn \bernhard_hidedesc:n
{
\seq_clear:N \l__bernhard_hidedesc_items_seq
\tl_set:Nn \l__bernhard_hidedesc_body_tl { #1 }
% get rid of all \begin{...}...\end{...}
\regex_replace_all:nnN { \c{begin}.*?\c{end}\{.*?\} } { } \l__bernhard_hidedesc_body_tl
% split at \item
\seq_set_split:NnV \l__bernhard_hidedesc_items_seq { \item } \l__bernhard_hidedesc_body_tl
% the first item is empty, get rid of it
\seq_pop_left:NN \l__bernhard_hidedesc_items_seq \l_tmpa_tl
\begin{description}
\seq_map_function:NN \l__bernhard_hidedesc_items_seq \__bernhard_hidedesc_item:n
\end{description}
}
\cs_new:Nn \__bernhard_hidedesc_item:n
{% all items are of the form [desc] <tokens>
\__bernhard_hidedesc_item:w #1 \q_stop
}
\cs_new:Npn \__bernhard_hidedesc_item:w [ #1 ] #2 \q_stop
{
\item[#1]
}
\ExplSyntaxOff
\begin{document}
Text before \verb|description*|:
\begin{description*}
\item[A] quick brown fox jumped over the lazy dog
\item[quick] brown fox jumped over the lazy dog
\item[brown] fox jumped over the lazy dog
\item[fox] jumped over the lazy dog
\item[jumped] over the lazy dog
\item[over] the lazy dog
\item[the] lazy dog
\begin{enumerate}
\item A
\item B
\end{enumerate}
\item[lazy] dog
\item[dog]
\end{description*}
Text after \verb|description*|.
\end{document}
在这两种情况下,输出都是