Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to set a counter based on the name of the current job. I've got the number I need by doing something like \substring{\jobname}{5}{5}} using the stringstrings package, and if I just output that into the document, I see the right value. However, naively trying to use it as an argument to \setcounter fails with the error "Missing number, treated as zero." I don't know enough about TeX internals to speculate intelligently on what's going on here, so I'll simply ask: How should I make this work?

Minimal example (save this as file3-example.tex):

\documentclass{article}
\usepackage{stringstrings}
\begin{document}
% \substring{\jobname}{5}{5} % this outputs 3
\setcounter{section}{\substring{\jobname}{5}{5}} % this fails
\section{Foo}
\end{document}
share|improve this question

3 Answers 3

up vote 8 down vote accepted

As already stated the \setcounter{section}{..} must only include material which expands to a number. The string manipulation macro requires assignments internally, I guess, which makes it not fully expandable, so it causes an error.

I would recommend the xstring package instead. Its macros can also store the returned string into a (then expandable!) macro:

\documentclass{article}
\usepackage{xstring}
\begin{document}
\StrChar{\jobname}{5}[\mysubstring]%
% Or: (useful if the number would be two digits long
%\StrMid{\jobname}{5}{5}[\mysubstring]%
\setcounter{section}{\mysubstring}%
\addtocounter{section}{-1}% because it is incremented in \section
\section{Foo}
\end{document}

Note the egreg's answer is very fine and does not require a most likely big package to be loaded. I just wanted to show a LaTeX-highlevel solution for people which prefer that.

share|improve this answer
    
Thanks! This is the solution I ended up using -- it "feels nicer" to be able to use a substring macro. –  Rahul Jul 10 '11 at 15:17

stringstrings works just fine. The key is to use substring in [q] quiet mode, so that the result is saved in the variable \thestring. Then, \thestring can be passed to setcounter

\documentclass{article}
\usepackage{stringstrings}
\begin{document}
\jobname
\substring[q]{\jobname}{5}{5} % this outputs 3
\setcounter{section}{\thestring}
\section{Foo}
\end{document}

The key to stringstrings usefulness is the ability to produce results as simple (able to edef) strings, rather than as tokens.

share|improve this answer

This can't work, because the argument of \setcounter must be, after expansion, a number, while \substring has to perform many calculations which require commands incompatible with the syntax of \setcounter.

To access the fifth character of \jobname you can do in an indirect way:

\def\extractfifth#1#2#3#4#5#6\relax{#5}

\setcounter{section}{\expandafter\extractfifth\jobname\relax}
\addtocounter{section}{-1}

The first line should go in the preamble. However in this way the section would be number 4, since it's \section that increments the counter. That's the purpose of the following \addtocounter.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.