4

I want to use a part of the filename as a counter:

First I try to use a variable as a counter:

mwe_aa_03.tex:

\documentclass{scrartcl}
\usepackage[automark]{scrlayer-scrpage} 
\newcommand{\thatispage}{3} 
\addtocounter{section}{\thatispage} 
\ihead{Page \thatispage }  
\begin{document}
\section{section}
but this should be section 3
\end{document}

this works, but now I want to use the filename as a number:

mwe_bb_03.tex:

\documentclass{scrartcl}
\usepackage[automark]{scrlayer-scrpage} 
\usepackage{xstring}
\newcommand{\fancypagenumber}{\StrBehind[2]{\jobname}{\string_} }
\addtocounter{section}{\fancypagenumber}
\ihead{Page \fancypagenumber }  
\begin{document}
\section{section}
but this should be section 3
\end{document}

which does not work ("Missing number, treated as zero").

Can you explain what I am doing wrong? How can I make this string becomming a number?

2 Answers 2

6

There are two errors.

  1. You can't use \StrBehind inside the argument to \setcounter
  2. You need to set the counter to one less than the number you want, because \section steps it before typesetting the title.
\documentclass{scrartcl}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{xstring}

\StrBehind[2]{\jobname}{\string_}[\fancypagenumber]

\addtocounter{section}{\fancypagenumber}
\addtocounter{section}{-1}
\ihead{Page \fancypagenumber }

\begin{document}
\section{section}
but this should be section 3
\end{document}

enter image description here

4

An alternative without packages, you can make a command gobble the part that you want leaving just the number. Note that you need to use \detokenize{..} because of the category codes of the \jobname.

\expandafter\def\expandafter\removemewbb\detokenize{mwe_bb_}{}
\edef\fancypagenumber{\expandafter\removemewbb\jobname}
2
  • My filename changes from time to time. I only know for sure that the two dashes are there, and the number I want is in the third/last part for now. Therefor I will use the xstring code this time, but thanks for another intressting approach. Commented Nov 11, 2017 at 16:04
  • Also possible, but I understand that you use another solution. This definition would work \begingroup\edef\x{\endgroup\def\noexpand\removeuntilsecondunderscore#1\string_#2\string_{}}\x and then \edef\fancypagenumber{\expandafter\removeuntilsecondunderscore\jobname}.
    – Manuel
    Commented Nov 11, 2017 at 16:36

You must log in to answer this question.

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