Pulling numbers out of a file name
up vote
5
down vote
favorite
A long time ago, I wanted to solve the problem where I would typeset my homework assignments, but copy the file "hw1.tex" to make "hw2.tex", and forget to change the title "Homework 1" at the top of the page.
So I wrote a function (that now I no longer 100% remember how it works, or maybe I got parts of it from a place like StackExchange and never did fully understand it):
usepackage{substr}
newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}
Now I can make Homework hwnum
the title. When I compile "hw1.tex", the title is "Homework 1", and when I compile "hw2.tex", the title becomes "Homework 2".
Minimal working example (assuming you first save it as a file with the appropriate name):
documentclass{article}
usepackage{substr}
newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}
begin{document}
This is Homework hwnum.
end{document}
These days, I have to typeset more than just homework assignments, so I'd like to have this be a bit more flexible.
- It would be nicer, though not very important, if I didn't have to change the prefix "hw" in the command if I want to deal with filenames such as "day1.tex", "day2.tex", and so on.
- What I really want is to extract more than one number: for example, "Lecture 7" and "Chapter 3" (or whatever) from a filename such as "ch3lec7.tex".
- Putting these together, an ideal function would just be able to locate all the numbers in the filename, no matter which non-numbers they're separated by. The same function could pull out 3 and 7 whether it's given the filename "ch3lec7.tex" or "week3day7.tex".
What is a way for me to do at least #2, but also if possible #1 or #3?
strings jobname
New contributor
add a comment |
up vote
5
down vote
favorite
A long time ago, I wanted to solve the problem where I would typeset my homework assignments, but copy the file "hw1.tex" to make "hw2.tex", and forget to change the title "Homework 1" at the top of the page.
So I wrote a function (that now I no longer 100% remember how it works, or maybe I got parts of it from a place like StackExchange and never did fully understand it):
usepackage{substr}
newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}
Now I can make Homework hwnum
the title. When I compile "hw1.tex", the title is "Homework 1", and when I compile "hw2.tex", the title becomes "Homework 2".
Minimal working example (assuming you first save it as a file with the appropriate name):
documentclass{article}
usepackage{substr}
newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}
begin{document}
This is Homework hwnum.
end{document}
These days, I have to typeset more than just homework assignments, so I'd like to have this be a bit more flexible.
- It would be nicer, though not very important, if I didn't have to change the prefix "hw" in the command if I want to deal with filenames such as "day1.tex", "day2.tex", and so on.
- What I really want is to extract more than one number: for example, "Lecture 7" and "Chapter 3" (or whatever) from a filename such as "ch3lec7.tex".
- Putting these together, an ideal function would just be able to locate all the numbers in the filename, no matter which non-numbers they're separated by. The same function could pull out 3 and 7 whether it's given the filename "ch3lec7.tex" or "week3day7.tex".
What is a way for me to do at least #2, but also if possible #1 or #3?
strings jobname
New contributor
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
A long time ago, I wanted to solve the problem where I would typeset my homework assignments, but copy the file "hw1.tex" to make "hw2.tex", and forget to change the title "Homework 1" at the top of the page.
So I wrote a function (that now I no longer 100% remember how it works, or maybe I got parts of it from a place like StackExchange and never did fully understand it):
usepackage{substr}
newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}
Now I can make Homework hwnum
the title. When I compile "hw1.tex", the title is "Homework 1", and when I compile "hw2.tex", the title becomes "Homework 2".
Minimal working example (assuming you first save it as a file with the appropriate name):
documentclass{article}
usepackage{substr}
newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}
begin{document}
This is Homework hwnum.
end{document}
These days, I have to typeset more than just homework assignments, so I'd like to have this be a bit more flexible.
- It would be nicer, though not very important, if I didn't have to change the prefix "hw" in the command if I want to deal with filenames such as "day1.tex", "day2.tex", and so on.
- What I really want is to extract more than one number: for example, "Lecture 7" and "Chapter 3" (or whatever) from a filename such as "ch3lec7.tex".
- Putting these together, an ideal function would just be able to locate all the numbers in the filename, no matter which non-numbers they're separated by. The same function could pull out 3 and 7 whether it's given the filename "ch3lec7.tex" or "week3day7.tex".
What is a way for me to do at least #2, but also if possible #1 or #3?
strings jobname
New contributor
A long time ago, I wanted to solve the problem where I would typeset my homework assignments, but copy the file "hw1.tex" to make "hw2.tex", and forget to change the title "Homework 1" at the top of the page.
So I wrote a function (that now I no longer 100% remember how it works, or maybe I got parts of it from a place like StackExchange and never did fully understand it):
usepackage{substr}
newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}
Now I can make Homework hwnum
the title. When I compile "hw1.tex", the title is "Homework 1", and when I compile "hw2.tex", the title becomes "Homework 2".
Minimal working example (assuming you first save it as a file with the appropriate name):
documentclass{article}
usepackage{substr}
newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}
begin{document}
This is Homework hwnum.
end{document}
These days, I have to typeset more than just homework assignments, so I'd like to have this be a bit more flexible.
- It would be nicer, though not very important, if I didn't have to change the prefix "hw" in the command if I want to deal with filenames such as "day1.tex", "day2.tex", and so on.
- What I really want is to extract more than one number: for example, "Lecture 7" and "Chapter 3" (or whatever) from a filename such as "ch3lec7.tex".
- Putting these together, an ideal function would just be able to locate all the numbers in the filename, no matter which non-numbers they're separated by. The same function could pull out 3 and 7 whether it's given the filename "ch3lec7.tex" or "week3day7.tex".
What is a way for me to do at least #2, but also if possible #1 or #3?
strings jobname
strings jobname
New contributor
New contributor
edited 1 hour ago
New contributor
asked 4 hours ago
Misha Lavrov
1264
1264
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
The code below uses regular expressions from LaTeX3 to extract all of the numbers in the filename and then makes them available as misha{1}
, misha{2}
, .... There is no error checking so, for example, if you have misha{100}
in your document then this command will fail silently, doing nothing.
If you save the code below as the file ch3lec7.tex
then run it you will get the output:
Here is the code:
documentclass{article}
usepackage{expl3}
ExplSyntaxOn
cs_generate_variant:Nn regex_extract_all:nnN {nVN}
seq_new:N l_misha_seq
regex_extract_all:nVN {d+} c_sys_jobname_str l_misha_seq
newcommandmisha[1]{seq_item:Nn l_misha_seq {#1}}
ExplSyntaxOff
begin{document}
Chapter misha{1}, lecture misha{2}.
end{document}
The work is all done by the command regex_extract_all:nVN
, which puts all of the numbers in jobname
into an internal LaTeX3 sequence. (As egreg pointed out, LaTeX3 stores the filename in the string constant c_sys_jobname_str
.) The command misha{k}
prints the k
th element of this sequence.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
The code below uses regular expressions from LaTeX3 to extract all of the numbers in the filename and then makes them available as misha{1}
, misha{2}
, .... There is no error checking so, for example, if you have misha{100}
in your document then this command will fail silently, doing nothing.
If you save the code below as the file ch3lec7.tex
then run it you will get the output:
Here is the code:
documentclass{article}
usepackage{expl3}
ExplSyntaxOn
cs_generate_variant:Nn regex_extract_all:nnN {nVN}
seq_new:N l_misha_seq
regex_extract_all:nVN {d+} c_sys_jobname_str l_misha_seq
newcommandmisha[1]{seq_item:Nn l_misha_seq {#1}}
ExplSyntaxOff
begin{document}
Chapter misha{1}, lecture misha{2}.
end{document}
The work is all done by the command regex_extract_all:nVN
, which puts all of the numbers in jobname
into an internal LaTeX3 sequence. (As egreg pointed out, LaTeX3 stores the filename in the string constant c_sys_jobname_str
.) The command misha{k}
prints the k
th element of this sequence.
add a comment |
up vote
4
down vote
The code below uses regular expressions from LaTeX3 to extract all of the numbers in the filename and then makes them available as misha{1}
, misha{2}
, .... There is no error checking so, for example, if you have misha{100}
in your document then this command will fail silently, doing nothing.
If you save the code below as the file ch3lec7.tex
then run it you will get the output:
Here is the code:
documentclass{article}
usepackage{expl3}
ExplSyntaxOn
cs_generate_variant:Nn regex_extract_all:nnN {nVN}
seq_new:N l_misha_seq
regex_extract_all:nVN {d+} c_sys_jobname_str l_misha_seq
newcommandmisha[1]{seq_item:Nn l_misha_seq {#1}}
ExplSyntaxOff
begin{document}
Chapter misha{1}, lecture misha{2}.
end{document}
The work is all done by the command regex_extract_all:nVN
, which puts all of the numbers in jobname
into an internal LaTeX3 sequence. (As egreg pointed out, LaTeX3 stores the filename in the string constant c_sys_jobname_str
.) The command misha{k}
prints the k
th element of this sequence.
add a comment |
up vote
4
down vote
up vote
4
down vote
The code below uses regular expressions from LaTeX3 to extract all of the numbers in the filename and then makes them available as misha{1}
, misha{2}
, .... There is no error checking so, for example, if you have misha{100}
in your document then this command will fail silently, doing nothing.
If you save the code below as the file ch3lec7.tex
then run it you will get the output:
Here is the code:
documentclass{article}
usepackage{expl3}
ExplSyntaxOn
cs_generate_variant:Nn regex_extract_all:nnN {nVN}
seq_new:N l_misha_seq
regex_extract_all:nVN {d+} c_sys_jobname_str l_misha_seq
newcommandmisha[1]{seq_item:Nn l_misha_seq {#1}}
ExplSyntaxOff
begin{document}
Chapter misha{1}, lecture misha{2}.
end{document}
The work is all done by the command regex_extract_all:nVN
, which puts all of the numbers in jobname
into an internal LaTeX3 sequence. (As egreg pointed out, LaTeX3 stores the filename in the string constant c_sys_jobname_str
.) The command misha{k}
prints the k
th element of this sequence.
The code below uses regular expressions from LaTeX3 to extract all of the numbers in the filename and then makes them available as misha{1}
, misha{2}
, .... There is no error checking so, for example, if you have misha{100}
in your document then this command will fail silently, doing nothing.
If you save the code below as the file ch3lec7.tex
then run it you will get the output:
Here is the code:
documentclass{article}
usepackage{expl3}
ExplSyntaxOn
cs_generate_variant:Nn regex_extract_all:nnN {nVN}
seq_new:N l_misha_seq
regex_extract_all:nVN {d+} c_sys_jobname_str l_misha_seq
newcommandmisha[1]{seq_item:Nn l_misha_seq {#1}}
ExplSyntaxOff
begin{document}
Chapter misha{1}, lecture misha{2}.
end{document}
The work is all done by the command regex_extract_all:nVN
, which puts all of the numbers in jobname
into an internal LaTeX3 sequence. (As egreg pointed out, LaTeX3 stores the filename in the string constant c_sys_jobname_str
.) The command misha{k}
prints the k
th element of this sequence.
edited 31 mins ago
answered 1 hour ago
Andrew
30.2k34380
30.2k34380
add a comment |
add a comment |
Misha Lavrov is a new contributor. Be nice, and check out our Code of Conduct.
Misha Lavrov is a new contributor. Be nice, and check out our Code of Conduct.
Misha Lavrov is a new contributor. Be nice, and check out our Code of Conduct.
Misha Lavrov is a new contributor. Be nice, and check out our Code of Conduct.
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f464025%2fpulling-numbers-out-of-a-file-name%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