Pulling numbers out of a file name
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
\newcommand\misha[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.