Skip to content

Instantly share code, notes, and snippets.

@westernmagic
Created November 14, 2016 23:08
Show Gist options
  • Save westernmagic/fda42c45c1f849514a9d138c159413c7 to your computer and use it in GitHub Desktop.
Save westernmagic/fda42c45c1f849514a9d138c159413c7 to your computer and use it in GitHub Desktop.
\documentclass{article}
% Based of https://tex.stackexchange.com/questions/4502/onlyitems-how-to-select-specific-items-from-an-item-list#_=_ by Ryan Reich and Yossi Farjoun
\usepackage{xparse}
\usepackage{etoolbox}
\makeatletter
\let\itemLaTeX=\item
% Have to use a \newcount because (ugh) the LaTeX \setcounter is a global assignment
\newcount\itemsSoFar
\newcommand\itemonlyitems[1][]{%
% The \egroup closes off any \vbox'es from the previously ignored \item.
% The conditional \bgroup cancels it out when there aren't any.
\itemIf\bgroup\fi\egroup
\let\itemIf=\iffalse%\fi
\advance\itemsSoFar by 1 %
%\itemIf
% \def\next{\expandafter\itemLaTeX\itemOptions}%
%\else
% The vbox is set and then discarded, effectively ignoring an entire \item.
% This inner \itemLaTeX is necessary to avoid a glitch when we ignore the first \item of an itemize that itself contains a nested \itemize. Don't ask, I don't know.
% \def\next{\setbox0=\vbox\bgroup\itemLaTeX}%
%\fi
\next
}
\DeclareDocumentEnvironment{ignoreitems}{ >{\ReverseBoolean} s}{
\let\item=\itemonlyitems
\let\itemIf=\iftrue
\itemsSoFar = 0
\IfBooleanTF{#1}{
% The vbox is set and then discarded, effectively ignoring an entire \item.
% This inner \itemLaTeX is necessary to avoid a glitch when we ignore the first \item of an itemize that itself contains a nested \itemize. Don't ask, I don't know.
\def\next{\setbox0=\vbox\bgroup\itemLaTeX}
}{
%the \@gobble s are so that various numerated lists still work as if
%the items are missing (i.e, no side effects). reference: http://tex.stackexchange.com/questions/34811/how-to-skip-an-item-entirely-with-no-side-effects/34821#34821
\def\next{\setbox0=\vbox\bgroup\let\stepcounter=\@gobble\let\addtocounter\@gobbletwo\itemLaTeX}
}
}{
\itemIf\bgroup\fi\egroup
}
\DeclareDocumentEnvironment{ignoreitems*}{}{
\begin{ignoreitems}*
}{
\end{ignoreitems}
}
\AtBeginEnvironment{enumerate}{\let\item=\itemLaTeX}
\makeatother
\begin{document}
\begin{enumerate}
\item A
\begin{ignoreitems}
\item B
\begin{enumerate}
\item B1
\item B2
\end{enumerate}
\end{ignoreitems}
\item C
\begin{ignoreitems*}
\item D
\begin{enumerate}
\item D1
\item D2
\end{enumerate}
\end{ignoreitems*}
\item E
\end{enumerate}
\end{document}
\endinput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment