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 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}
share|improve this question
    
Useful Related Question: Convert string to file name and use in \href –  Peter Grill Mar 27 '12 at 16:37
add comment

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer
    
Thanks a lot! I did not know it works this way around, but it does the trick. –  Jenny Mar 27 '12 at 9:46
add comment

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.