5

I am trying to write a command that allows me to reference a subsection with a generated label.

The idea is that when I declare a step \step{Long Name With Many Uppercase Letters}, the generated label will be \label{step:long_name_with_many_uppercase_letters}.

I already found Generate labels based on macro argument, but I fear the answer based on using \@firstofone does not work for my case.

My current approach is the following:

\makeatletter
% Step command
\newcounter{step}[section]
\setcounter{step}{0}

\newcommand{\step}[1]{%
  \subsection{#1} 
  \refstepcounter{step} 
  \label{step:\MakeLowercase{\StrSubstitute[0]{#1}{ }{\_}}}
}
\makeatother

Unfortunately, using the label command with other command inside gives me the following errors:

Missing \endcsname inserted \step{First Step}
TeX capacity exceeded, sorry [input stack size=5000] \step{First Step}

If I remove the \label command, it works (but of course gives me warnings about undefined references).

A minimal example looks like this:

\documentclass{scrbook}

\usepackage{xstring}

\makeatletter
% Step command
\newcounter{stepctr}[section]
\setcounter{stepctr}{0}

\newcommand{\step}[1]{%
  \subsection{#1} 
  \refstepcounter{stepctr} 
  \label{step:\MakeLowercase{\StrSubstitute[0]{#1}{ }{\_}}}
}
\makeatother

\begin{document}

\chapter{My only chapter}

\section{A section where I reference steps}

I'd like to reference step number \ref{step:first_step} and \ref{step:third_step} here.

\section{A section where I define steps}

\step{First Step}

Foo.

\step{Second Step}

Bar.

\step{Third Step}

Whatever. 

\end{document}
1

1 Answer 1

5

You want to lowercase the argument of label, not have \label containing a lowercase command in its argument so invert the order of the expressions:

\documentclass{scrbook}

\usepackage{xstring}


% Step command
\newcounter{stepctr}[section]
\setcounter{stepctr}{0}

\newcommand{\step}[1]{%
  \subsection{#1} %
  \refstepcounter{stepctr}%
  {\lccode`\ `\_\lowercase{\label{step:#1}}}}


\begin{document}

\chapter{My only chapter}

\section{A section where I reference steps}

I'd like to reference step number \ref{step:first_step} and \ref{step:third_step} here.

\section{A section where I define steps}

\step{First Step}

Foo.

\step{Second Step}

Bar.

\step{Third Step}

Whatever. 

\end{document}
1
  • Thanks a lot! I did not know it works this way around, but it does the trick.
    – Jenny
    Mar 27, 2012 at 9:46

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.