4

I want to define some variables calculated in a loop to draw a path in tikzpicture. The variables are defined by some length (in pt) and the (section) title. For example, let's say we have:

  • 5pt, Section 1
  • 10pt, Section 2
  • 15pt, Section 3

This is done by a small calculation & reference on the section labels (to get the title):

\setcounter{sectionCounter}{0}\relax\loop\ifnum\thesectionCounter<3%
\newcount\frameNumber
\frameNumber=...
\dimen1=\textwidth
\multiply\dimen1 by \frameNumber
\divide\dimen1 by \inserttotalframenumber
\edef\progressbarwidthx{\the\dimen1}
\def\titlexyz{\nameref{\secLabel}}

\expandafter\def\csname sWidth\the\value{sectionCounter} \endcsname{\progressbarwidthx}
\expandafter\def\csname sTitle\the\value{sectionCounter} \endcsname{\titlexyz}%

\stepcounter{sectionCounter}\relax\repeat%
\leavevmode%

But if I call the variables after the loop by

\csname sWidth0 \endcsname
\csname sTitle0 \endcsname; %      
\csname sWidth1 \endcsname
\csname sTitle1 \endcsname; %        
\csname sWidth2 \endcsname
\csname sTitle2 \endcsname

all have assigned the same value from the last iteration: 15pt & Section 3

In the fist iteration sWidth0 & sTitle0 get the right value 5pt & Section 1, but already in the second iteration sWidth0 & sWidth1 and sTitle0 & sTitle1 get have the same values 10pt and Section 2, respectively.

Where is the problem?

Edit: I a generate this by hand the error does not occur:

\documentclass{minimal}

\begin{document}
\newcounter{sectionCounter}
\setcounter{sectionCounter}{0}

\expandafter\newcommand\csname ary\the\value{sectionCounter} \endcsname{first}
\expandafter\newcommand\csname arz\the\value{sectionCounter} \endcsname{firstx}
\stepcounter{sectionCounter}
\expandafter\newcommand\csname ary\the\value{sectionCounter} \endcsname{second}
\expandafter\newcommand\csname arz\the\value{sectionCounter} \endcsname{secondx}

\csname ary0 \endcsname
\csname ary1 \endcsname

\csname arz0 \endcsname
\csname arz1 \endcsname

\end{document}

gives

first
second

firstx
secondx
  • 1
    Their value is \progressbarwidthx and \titlexyz respectively, not the expansion of these (because of the use of \def instead of \edef. The expansion takes place when you print them. – Pieter van Oostrum Jan 1 '17 at 19:53
5

Try

\expandafter\edef\csname sWidth\the\value{sectionCounter} \endcsname{\progressbarwidthx}
\expandafter\edef\csname sTitle\the\value{sectionCounter} \endcsname{\titlexyz}%
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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