Removing a number of tokens from the input stream
I need a command which removes all following tokens which are digits, comma, spaces or newlines and stops if it encounters something else. I can do it by nesting a lot of peek_charcode_remove_ignore_spaces:NTF
, but is there a more elegant way to map e.g. over a clist?
documentclass{article}
usepackage{expl3}
ExplSyntaxOn
clist_const:Nnc_uf_page_clist{1,2,3,4,5,6,7,8,9,0,{,}}
cs_new:Nnuf_eatline: {
peek_charcode_remove_ignore_spaces:NTF 0 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF 1 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF 6 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF , { uf_eatline: }
{
%and more nesting ...
}
}
}
}
}
cs_set_eq:NNeatlineuf_eatline:
ExplSyntaxOff
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{Texta}{162}
end{itemize}
end{document}
loops latex3
add a comment |
I need a command which removes all following tokens which are digits, comma, spaces or newlines and stops if it encounters something else. I can do it by nesting a lot of peek_charcode_remove_ignore_spaces:NTF
, but is there a more elegant way to map e.g. over a clist?
documentclass{article}
usepackage{expl3}
ExplSyntaxOn
clist_const:Nnc_uf_page_clist{1,2,3,4,5,6,7,8,9,0,{,}}
cs_new:Nnuf_eatline: {
peek_charcode_remove_ignore_spaces:NTF 0 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF 1 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF 6 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF , { uf_eatline: }
{
%and more nesting ...
}
}
}
}
}
cs_set_eq:NNeatlineuf_eatline:
ExplSyntaxOff
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{Texta}{162}
end{itemize}
end{document}
loops latex3
Do you expect something like{Whatevereatline}
?
– egreg
1 hour ago
@egreg No. it is from an index, where I want to remove the page numbers (well probably I don't want it anymore, I think I simply misunderstood a request, but I would still be interested to see how to loop in such cases).
– Ulrike Fischer
1 hour ago
add a comment |
I need a command which removes all following tokens which are digits, comma, spaces or newlines and stops if it encounters something else. I can do it by nesting a lot of peek_charcode_remove_ignore_spaces:NTF
, but is there a more elegant way to map e.g. over a clist?
documentclass{article}
usepackage{expl3}
ExplSyntaxOn
clist_const:Nnc_uf_page_clist{1,2,3,4,5,6,7,8,9,0,{,}}
cs_new:Nnuf_eatline: {
peek_charcode_remove_ignore_spaces:NTF 0 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF 1 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF 6 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF , { uf_eatline: }
{
%and more nesting ...
}
}
}
}
}
cs_set_eq:NNeatlineuf_eatline:
ExplSyntaxOff
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{Texta}{162}
end{itemize}
end{document}
loops latex3
I need a command which removes all following tokens which are digits, comma, spaces or newlines and stops if it encounters something else. I can do it by nesting a lot of peek_charcode_remove_ignore_spaces:NTF
, but is there a more elegant way to map e.g. over a clist?
documentclass{article}
usepackage{expl3}
ExplSyntaxOn
clist_const:Nnc_uf_page_clist{1,2,3,4,5,6,7,8,9,0,{,}}
cs_new:Nnuf_eatline: {
peek_charcode_remove_ignore_spaces:NTF 0 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF 1 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF 6 { uf_eatline: }
{
peek_charcode_remove_ignore_spaces:NTF , { uf_eatline: }
{
%and more nesting ...
}
}
}
}
}
cs_set_eq:NNeatlineuf_eatline:
ExplSyntaxOff
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{Texta}{162}
end{itemize}
end{document}
loops latex3
loops latex3
asked 2 hours ago
Ulrike FischerUlrike Fischer
190k8297678
190k8297678
Do you expect something like{Whatevereatline}
?
– egreg
1 hour ago
@egreg No. it is from an index, where I want to remove the page numbers (well probably I don't want it anymore, I think I simply misunderstood a request, but I would still be interested to see how to loop in such cases).
– Ulrike Fischer
1 hour ago
add a comment |
Do you expect something like{Whatevereatline}
?
– egreg
1 hour ago
@egreg No. it is from an index, where I want to remove the page numbers (well probably I don't want it anymore, I think I simply misunderstood a request, but I would still be interested to see how to loop in such cases).
– Ulrike Fischer
1 hour ago
Do you expect something like
{Whatevereatline}
?– egreg
1 hour ago
Do you expect something like
{Whatevereatline}
?– egreg
1 hour ago
@egreg No. it is from an index, where I want to remove the page numbers (well probably I don't want it anymore, I think I simply misunderstood a request, but I would still be interested to see how to loop in such cases).
– Ulrike Fischer
1 hour ago
@egreg No. it is from an index, where I want to remove the page numbers (well probably I don't want it anymore, I think I simply misunderstood a request, but I would still be interested to see how to loop in such cases).
– Ulrike Fischer
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
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
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{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.
That's naturally a much saner way to solve the concrete problem - I wonder why I thought that I need to peek ahead. But I guess it also means that there is no (easy) way to use a peek command in a loop over a clist or something like this?
– Ulrike Fischer
59 mins ago
@UlrikeFischer No, but something about this has been discussed in the list.
– egreg
57 mins ago
add a comment |
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 afterassignmentendgroup
. This should work even if no numbers follow the eatline
function and in the {Whatevereatline}
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:NNeatlineuf_eatline:
ExplSyntaxOff
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{Texta}{162}
end{itemize}
end{document}
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f472229%2fremoving-a-number-of-tokens-from-the-input-stream%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{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.
That's naturally a much saner way to solve the concrete problem - I wonder why I thought that I need to peek ahead. But I guess it also means that there is no (easy) way to use a peek command in a loop over a clist or something like this?
– Ulrike Fischer
59 mins ago
@UlrikeFischer No, but something about this has been discussed in the list.
– egreg
57 mins ago
add a comment |
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
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{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.
That's naturally a much saner way to solve the concrete problem - I wonder why I thought that I need to peek ahead. But I guess it also means that there is no (easy) way to use a peek command in a loop over a clist or something like this?
– Ulrike Fischer
59 mins ago
@UlrikeFischer No, but something about this has been discussed in the list.
– egreg
57 mins ago
add a comment |
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
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{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 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
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{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.
answered 1 hour ago
egregegreg
715k8619003187
715k8619003187
That's naturally a much saner way to solve the concrete problem - I wonder why I thought that I need to peek ahead. But I guess it also means that there is no (easy) way to use a peek command in a loop over a clist or something like this?
– Ulrike Fischer
59 mins ago
@UlrikeFischer No, but something about this has been discussed in the list.
– egreg
57 mins ago
add a comment |
That's naturally a much saner way to solve the concrete problem - I wonder why I thought that I need to peek ahead. But I guess it also means that there is no (easy) way to use a peek command in a loop over a clist or something like this?
– Ulrike Fischer
59 mins ago
@UlrikeFischer No, but something about this has been discussed in the list.
– egreg
57 mins ago
That's naturally a much saner way to solve the concrete problem - I wonder why I thought that I need to peek ahead. But I guess it also means that there is no (easy) way to use a peek command in a loop over a clist or something like this?
– Ulrike Fischer
59 mins ago
That's naturally a much saner way to solve the concrete problem - I wonder why I thought that I need to peek ahead. But I guess it also means that there is no (easy) way to use a peek command in a loop over a clist or something like this?
– Ulrike Fischer
59 mins ago
@UlrikeFischer No, but something about this has been discussed in the list.
– egreg
57 mins ago
@UlrikeFischer No, but something about this has been discussed in the list.
– egreg
57 mins ago
add a comment |
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 afterassignmentendgroup
. This should work even if no numbers follow the eatline
function and in the {Whatevereatline}
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:NNeatlineuf_eatline:
ExplSyntaxOff
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{Texta}{162}
end{itemize}
end{document}
add a comment |
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 afterassignmentendgroup
. This should work even if no numbers follow the eatline
function and in the {Whatevereatline}
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:NNeatlineuf_eatline:
ExplSyntaxOff
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{Texta}{162}
end{itemize}
end{document}
add a comment |
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 afterassignmentendgroup
. This should work even if no numbers follow the eatline
function and in the {Whatevereatline}
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:NNeatlineuf_eatline:
ExplSyntaxOff
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{Texta}{162}
end{itemize}
end{document}
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 afterassignmentendgroup
. This should work even if no numbers follow the eatline
function and in the {Whatevereatline}
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:NNeatlineuf_eatline:
ExplSyntaxOff
newcommandsee[2]{, emph{see} #1}
begin{document}
begin{itemize}
item Textaeatline116,
153
item Textbeatlinesee{Texta}{162}
end{itemize}
Wanted output:
begin{itemize}
item Texta
item Textbsee{Texta}{162}
end{itemize}
end{document}
answered 1 hour ago
Phelype OleinikPhelype Oleinik
22.1k54482
22.1k54482
add a comment |
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f472229%2fremoving-a-number-of-tokens-from-the-input-stream%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you expect something like
{Whatevereatline}
?– egreg
1 hour ago
@egreg No. it is from an index, where I want to remove the page numbers (well probably I don't want it anymore, I think I simply misunderstood a request, but I would still be interested to see how to loop in such cases).
– Ulrike Fischer
1 hour ago