2

I would like to print only the characters between Ch and - in the filename, so I use the packages currfile and xstring. As example :

\StrBetween[1,1]{\currfilename}{Ch}{-}

should gives 2 or 14 if the filename is "Ch2-2nde.tex" or "Ch14-2nde.tex", but it does not work. But still if I try \StrBetween[1,1]{Ch2-2nde.tex}{Ch}{-} or \StrBetween[1,1]{Ch14-2nde.tex}{Ch}{-} it works fine ! I don't understand ...

1
  • 1
    \currfilename returns characters with category code 12, that don't match Ch of category code 11. The solution by @SunilkumarKS works because the * variant applies \detokenize (after full expansion) and so Ch is transformed in category code 12 characters.
    – egreg
    Dec 16, 2015 at 11:45

2 Answers 2

3

You could use the starred option of \StrBetween command to obtain the result. This may be used when the input contains commands. The following example will help:

\StrBetween*[1,1]{\currfilename}{Ch}{-}
1
  • 1
    The purpose of * is different, see top of page 15 in the manual. This works, but for a different reason.
    – egreg
    Dec 16, 2015 at 11:41
3

The problem is that \currfilename returns a “stringified” file name; in TeXnical terms, all characters have category code 12. However, the characters Ch you are supplying in the second optional argument have category code 11, so there's no match.

One possibility is to use the *-variant of \StrBetween, that applies \detokenize to all mandatory arguments (after full expansion), so also Ch become category code 12 and the match is found.

Another possibility is to use l3regex.

\documentclass{article}
\usepackage{currfile,xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\extractnumber}{o}
 {
  \IfNoValueTF{#1}
   {
    \mathxlv_extract_number:N \l_tmpa_tl
    \tl_use:N \l_tmpa_tl
   }
   {
    \mathxlv_extract_number:N #1
   }
 }

\cs_new_protected:Nn \mathxlv_extract_number:N
 {
  \tl_set_eq:NN #1 \currfilename
  \regex_replace_once:nnN { \A Ch ([^-]*) .* \Z } { \1 } #1
 }
\ExplSyntaxOff

\begin{document}

\extractnumber

\extractnumber[\thisfilenumber]

\texttt{\meaning\thisfilenumber}

\end{document}

If the document is named Ch2-2nde.tex, the output is

enter image description here

If the document is named Ch14-abc-def.tex, the output is

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .