我想隐藏\item
列表中的某些内容。我目前的尝试:
\documentclass{article}
\usepackage{ifthen}
\newboolean{enablehide}
\setboolean{enablehide}{true}
\newcommand{\hideit}{%
\ifthenelse{\boolean{enablehide}}{%
\begingroup
\setbox0\hbox\bgroup}{}
}
\newcommand{\showit}{\ifthenelse{\boolean{enablehide}}{%
\egroup
\setbox1\hbox{}%
\ht1=\ht0
\wd1=\wd0
\dp1=\dp0
\box1
\endgroup}{}
}
\begin{document}
\begin{enumerate}
\item not hide
\hideit
\item hide
\showit
\item not hide
\end{enumerate}
\end{document}
错误: 出了点问题 - 可能缺少 \ item。\ item h
答案1
这是一个可能的解决方案:您想要(可能)隐藏的项目以 为前缀\hideit
。您可以将布尔值设置为 false,\showitems
并且\hideit
不会执行任何操作。
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\hideit}{}
{
\gaweiliex_hide:
}
\NewDocumentCommand{\hideitems}{}
{
\bool_set_true:N \l_gaweiliex_hide_bool
}
\NewDocumentCommand{\showitems}{}
{
\bool_set_false:N \l_gaweiliex_hide_bool
}
\bool_new:N \l_gaweiliex_hide_bool
\cs_new_protected:Nn \gaweiliex_hide:
{
\bool_if:NT \l_gaweiliex_hide_bool
{
\peek_regex_replace_once:nn
% search \item followed by anything until finding
% \item or \hideit or \end{<current environment>}
{ \c{item}.*?(\c{item}|\c{hideit}|\c{end}\{\u{@currenvir}\}) }
% replace by the matching item
{ \1 }
}
}
\ExplSyntaxOff
\hideitems % initialize
\begin{document}
\begin{enumerate}
\item This is shown 1
\hideit\item This is hidden
\hideit\item This is also hidden
\item This is shown 2
\hideit\item This is hidden
\item This is shown 3
\hideit\item This is hidden
\end{enumerate}
\begin{itemize}
\item This is shown 1
\hideit\item This is hidden
\hideit\item This is also hidden
\item This is shown 2
\hideit\item This is hidden
\item This is shown 3
\hideit\item This is hidden
\end{itemize}
\showitems
\begin{enumerate}
\item This is shown 1
\hideit\item This is hidden
\hideit\item This is also hidden
\item This is shown 2
\hideit\item This is hidden
\item This is shown 3
\hideit\item This is hidden
\end{enumerate}
\begin{itemize}
\item This is shown 1
\hideit\item This is hidden
\hideit\item This is also hidden
\item This is shown 2
\hideit\item This is hidden
\item This is shown 3
\hideit\item This is hidden
\end{itemize}
\end{document}