Removing a number of tokens from the input stream
If you don't expect \eatline
to be called inside a group ending with }
and you want to skip spaces, you can grab the next token(s) as an argument, examine it and decide.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\eatline}{m}
{
\uf_eatline:n { #1 }
}
\cs_new_protected:Nn \uf_eatline:n
{
\regex_match:nnTF { [0-9,] } { #1 }
{
\uf_eatline:n
}
{ #1 }
}
\ExplSyntaxOff
\newcommand\see[2]{, \emph{see} #1}
\begin{document}
\begin{itemize}
\item Texta\eatline116,
153
\item Textb\eatline\see{Texta}{162}
\end{itemize}
Wanted output:
\begin{itemize}
\item Texta
\item Textb\see{Texta}{162}
\end{itemize}
\end{document}
If the token is not a digit or a comma, it is reinserted back, otherwise another token is examined.
Something like {abc}
would be reinserted without braces, but it doesn't seem a problem in this context.
The problem with }
could be treated at the beginning with a single \peek_catcode:NTF
check.
If you're not going to have page numbers larger than 2^31-1
then this might work.
The \uf_eatline:
command starts a group, then sets the \catcode
of ,
, , and
^^M
to 9, then uses a primitive integer assignment to consume the remaining numbers (expanding tokens in this process) until something that is not a number is found. Finally, the catcodes are put back to normal with \afterassignment\endgroup
. This should work even if no numbers follow the \eatline
function and in the {Whatever\eatline}
case that egreg mentioned.
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_set_eq:NN \uf_after_assignment:N \tex_afterassignment:D
\int_new:N \l_uf_tmpa_int
\cs_new:Nn \uf_eatline:
{
\group_begin:
\char_set_catcode_ignore:n { `\, }
\char_set_catcode_ignore:n { `\ }
\char_set_catcode_ignore:n { `\^^M }
\tex_endlinechar:D = 32
\uf_after_assignment:N
\group_end:
\l_uf_tmpa_int = 0
}
\cs_set_eq:NN\eatline\uf_eatline:
\ExplSyntaxOff
\newcommand\see[2]{, \emph{see} #1}
\begin{document}
\begin{itemize}
\item Texta\eatline116,
153
\item Textb\eatline\see{Texta}{162}
\end{itemize}
Wanted output:
\begin{itemize}
\item Texta
\item Textb\see{Texta}{162}
\end{itemize}
\end{document}