4

I have some trouble with expanding variables within a protected label. I need a automatisated caption with a label all inside a command. Within the caption I want to have a two digit number (padzeroes). For this I have to use a temporary variable.

\newcommand{\Spezifikation}[5]{ %
\setcounter{tmpa}{#2}
%
\begin{minipage}{0.85\linewidth}
\captionof{spezifikation}{S\textunderscore#1
        \hspace{-1.5ex}
        \textunderscore\hspace{-0.85ex}\padzeroes[2]{\decimal{tmpa}}}
\protect\label{spec: #5}
\end{minipage}
}

That is the part in the .aux file:

test.aux:\@writefile{lspec}{\defcounter {refsection}{0}\relax }\@writefile{lspec}{\contentsline {spezifikation}{\numberline {}{\ignorespaces S\textunderscore SYS \hspace  {-1.5ex} \textunderscore \hspace  {-0.85ex}\padzeroes [2]{\@decimal {1}}\relax }}{11}{spezifikation.1}}
test.aux:\newlabel{spec: SYS-Modularisierung}{{1}{11}{S\textunderscore SYS \hspace {-1.5ex} \textunderscore \hspace {-0.85ex}\padzeroes [2]{\decimal {tmpa}}\relax \relax }{spezifikation.1}{}}

And the .lspec file

test.lspec:\contentsline {spezifikation}{\numberline {}{\ignorespaces S\textunderscore SYS \hspace {-1.5ex} \textunderscore \hspace {-0.85ex}\padzeroes [2]{\@decimal {1}}\relax }}{11}{spezifikation.1}

I think, the problem is the expanding of tmpa at test.aux:\newlabel{... At the other lines it was well expanded to the number behind.

How can I fix it?


Addition 1:

If I change

\padzeroes [2]{\decimal {tmpa}}

to

\padzeroes[2]{\decimalnum{#2}}

I got this error:

Missing \endcsname inserted

Normally it goes. But ot in this case (in a caption). I don't understand why.


Addition 2

Here comes a short executable:

\documentclass[,12pt,a4paper,twoside,onecolumn,notitlepage]{article} 
\usepackage[onehalfspacing]{setspace}
\usepackage[a4paper,
            inner=40mm, outer=20mm,
            top=25mm, bottom=25mm,
            headheight=15mm, headsep=7mm
            ]{geometry}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % Load the TS1-encoding e.g. through textcomp 
\usepackage{verbatim, environ}
\setlength{\parindent}{0mm}

\usepackage{newfloat,tocloft,caption,multicol,suffix,fmtcount,nameref}
\newcounter{tmpa}


\newcommand{\Spezifikation}[5]{ %
\setcounter{tmpa}{#2}
%
\begin{center}
\begin{minipage}{0.85\linewidth}
\vspace*{0.7ex}
\captionof{spezifikation}{S\textunderscore#1
        \hspace{-1.5ex}
        \textunderscore\hspace{-0.85ex}\padzeroes[2]{\decimal{tmpa}}}
\protect\label{spec: #5}
\textit{Inhalt:} #3\\[0.5ex]
\textit{Referenz:} #4\\[0.5ex]
\textit{EOS}
\vspace{0.7ex}
\end{minipage}
\end{center}
}
%
% float-Object einer Spezifikation
%
%    Listenendung: lspec
%
\DeclareFloatingEnvironment[fileext   = lspec, %
                            placement = h] %
                            {spezifikation}
\DeclareCaptionFormat{DCFs}{\raggedright \textit{Spec.-Nr.:} #3}
\captionsetup[spezifikation]{format = DCFs,
                             font = bf,
                             listformat = empty,
                             position = above}  

\begin{document}
\Spezifikation{hallo}{1}{Text}{Text}{Label1} 
\Spezifikation{hallo}{2}{Text}{Text}{Label2} 
\Spezifikation{hallo}{3}{Text}{Text}{Label3} 
\Spezifikation{hallo}{4}{Text}{Text}{Label4} 
\nameref{spec: Label1} 
\end{document}

You will see I reference to the first spec but it gives me the last. I think this is because of tmpa. This shows the aux:

\@writefile{lspec}{\contentsline {spezifikation}{\numberline {}{\ignorespaces S\textunderscore hallo \hspace  {-1.5ex} \textunderscore \hspace  {-0.85ex}\padzeroes [2]{\@decimal {1}}\relax }}{1}}
\newlabel{spec: Label1}{{1}{1}{S\textunderscore hallo \hspace {-1.5ex} \textunderscore \hspace {-0.85ex}\padzeroes [2]{\decimal {tmpa}}\relax \relax }{}{}}
\@writefile{lspec}{\contentsline {spezifikation}{\numberline {}{\ignorespaces S\textunderscore hallo \hspace  {-1.5ex} \textunderscore \hspace  {-0.85ex}\padzeroes [2]{\@decimal {2}}\relax }}{1}}
\newlabel{spec: Label2}{{2}{1}{S\textunderscore hallo \hspace {-1.5ex} \textunderscore \hspace {-0.85ex}\padzeroes [2]{\decimal {tmpa}}\relax \relax }{}{}}
\@writefile{lspec}{\contentsline {spezifikation}{\numberline {}{\ignorespaces S\textunderscore hallo \hspace  {-1.5ex} \textunderscore \hspace  {-0.85ex}\padzeroes [2]{\@decimal {3}}\relax }}{1}}
\newlabel{spec: Label3}{{3}{1}{S\textunderscore hallo \hspace {-1.5ex} \textunderscore \hspace {-0.85ex}\padzeroes [2]{\decimal {tmpa}}\relax \relax }{}{}}
\@writefile{lspec}{\contentsline {spezifikation}{\numberline {}{\ignorespaces S\textunderscore hallo \hspace  {-1.5ex} \textunderscore \hspace  {-0.85ex}\padzeroes [2]{\@decimal {4}}\relax }}{1}}
\newlabel{spec: Label4}{{4}{1}{S\textunderscore hallo \hspace {-1.5ex} \textunderscore \hspace {-0.85ex}\padzeroes [2]{\decimal {tmpa}}\relax \relax }{}{}}

The reference are right but it shows me the caption-text wrong. Because of the not expanding tmpa it gives me the last entry of tmpa.

7
  • Welcome to TeX.SX! Without knowing the definition of \padzeroes and \decimal it's difficult to say.
    – egreg
    Nov 10 '13 at 22:01
  • Please edit your question to be a complete small document that shows the problem, your fragments include many non standard commands such as \decimal \defcounter etc, so we can't really even guess how they work or what the problem is. Nov 10 '13 at 22:01
  • \padzeroes and \decimal comes with the fmtcount-pacakage
    – Alex44
    Nov 10 '13 at 22:15
  • @Alex44 Neither \padzeroes nor \decimal are fully expandable. If you look in the .aux file, you'll see that \newlabel has a literal \padzeroes [2]{\decimal {tmpa}}.
    – egreg
    Nov 10 '13 at 22:25
4

The problem is not in \protect\label which is not needed. The real issue is in what is written in the .aux file; the caption text is not expanded, so \padzeroes[2]\decimal{tmpa} is written as is.

You have to expand the caption text, but using \padzeroes[2]{\decimal{tmpa}} would prevent it anyway, because the two commands are not fully expandable.

Here's a working version:

\documentclass[,12pt,a4paper,twoside,onecolumn,notitlepage]{article} 
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[onehalfspacing]{setspace}
\usepackage[a4paper,
            inner=40mm, outer=20mm,
            top=25mm, bottom=25mm,
            headheight=15mm, headsep=7mm
            ]{geometry}
\usepackage{textcomp} % Load the TS1-encoding e.g. through textcomp 
\usepackage{verbatim, environ}
\setlength{\parindent}{0mm}

\usepackage{newfloat,tocloft,caption,multicol,suffix,fmtcount,nameref}
\newcounter{tmpa}

\makeatletter
\newcommand{\mypad}[1]{\two@digits{\value{#1}}}

\newcommand{\Spezifikation}[5]{%
  \setcounter{tmpa}{#2}%
  \begin{center}
  \begin{minipage}{0.85\linewidth}
  \vspace*{0.7ex}
  \begingroup\protected@edef\x{\endgroup
    \protect\captionof{spezifikation}{S\textunderscore#1%
      \textunderscore\mypad{tmpa}}}\x\label{spec: #5}
    \textit{Inhalt:} #3\\[0.5ex]
  \textit{Referenz:} #4\\[0.5ex]
  \textit{EOS}
  \vspace{0.7ex}
  \end{minipage}
  \end{center}
}
\makeatother
%
% float-Object einer Spezifikation
%
%    Listenendung: lspec
%
\DeclareFloatingEnvironment[fileext   = lspec, %
                            placement = h] %
                            {spezifikation}
\DeclareCaptionFormat{DCFs}{\raggedright \textit{Spec.-Nr.:} #3}
\captionsetup[spezifikation]{format = DCFs,
                             font = bf,
                             listformat = empty,
                             position = above}  

\begin{document}
\Spezifikation{hallo}{1}{Text}{Text}{Label1} 
\Spezifikation{hallo}{2}{Text}{Text}{Label2} 
\Spezifikation{hallo}{3}{Text}{Text}{Label3} 
\Spezifikation{hallo}{4}{Text}{Text}{Label4} 
\nameref{spec: Label1} 
\end{document}

Watch out for spurious spaces in your definitions. I removed the negative \hspace commands that were necessary only because of them.


I add an expandable padding macro; extend it if you need more than five digits.

\documentclass{article}

\makeatletter
\newcommand{\epadzeroes}[2]{%
  \ifcase#1\or\or
    \etwo@digits{#2}\or
    \ethree@digits{#2}\or
    \efour@digits{#2}\or
    \efive@digits{#2}\fi
  \number#2%
}
\newcommand\etwo@digits[1]{%
  \ifnum#1<10 0\fi
}
\newcommand\ethree@digits[1]{%
  \ifnum#1<100 0\fi
  \ifnum#1<10 0\fi
}
\newcommand\efour@digits[1]{%
  \ifnum#1<1000 0\fi
  \ifnum#1<100 0\fi
  \ifnum#1<10 0\fi
}
\newcommand\efive@digits[1]{%
  \ifnum#1<10000 0\fi
  \ifnum#1<1000 0\fi
  \ifnum#1<100 0\fi
  \ifnum#1<10 0\fi
}
\makeatother

\newcounter{tmpa}

\begin{document}
\setcounter{tmpa}{5}
\epadzeroes{2}{\value{tmpa}}

\epadzeroes{3}{\value{tmpa}}

\setcounter{tmpa}{9999}
\epadzeroes{5}{\value{tmpa}}

\end{document}
2
  • It is crazy - I don't know why (I'll say I don't understand it really :( ). But it goes. How you get the zeros in front of the value? In a other case I need five digits.
    – Alex44
    Nov 10 '13 at 22:48
  • I have all implementated and it goes well. Next time i will looking for the @. I don't know what it done. Thank you
    – Alex44
    Nov 11 '13 at 0:21

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.