# Cross reference to counter in section title

I am trying to use a custom counter in Latex in order to auto number Use Cases in section titles (eg. \section{UC <counter>: use case title}). I also want a \usecaseref command to substitute a reference by the full use case name: UCx: use case title.

This is what I got so far:

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage{hyperref}

\newcounter{UCcounter}

\newcommand{\usecaseprint}[1]{UC \arabic{UCcounter}: #1}

\newcommand{\usecaseincrease}{\refstepcounter{UCcounter}}

\newcommand{\usecaseref}[1]{\emph{\nameref{#1}}}

\begin{document}

\usecaseincrease
\section{\usecaseprint{First Use Case Name}\label{sec:first}}

This is the forwards reference: \usecaseref{sec:second}.

\usecaseincrease
\section{\usecaseprint{Second Use Case Name} \label{sec:second} }

This is the backwards reference: \usecaseref{sec:first}

\end{document}


This however produces the following:

As you can see, when calling the \nameref command, it uses the current value of the UCcounter instead of the value of the counter in the section title.

I already tried to replace the \newcommand occurences with \DeclareRobustCommand, but that doesn't change anything.

• If you look in the .aux file, you'll see that the section title reported in it is \usecaseprint {First Use Case Name} which will then use the current value for the counter. Do those section titles contain other things than \usecaseprint{...}? – egreg Mar 8 '14 at 23:39
• As egreg stated: The problem is, that \usecaseprint{} is written unexpanded to the .aux file, so it will call the counter value at its current value, not at the moment, the statement was written to the file. The source of the problem is inside the label command. – user31729 Mar 9 '14 at 2:14
• @egreg: I am using the solution posted by "latex", but I want to use Umlaute in my titles. Even if I am writing {\"a}, the command \immediate\write1{} writes ä into .aux and I get the error Package inputenc Error: Unicode char \u8: ... not set up for use with LaTeX ... What can I do? – Make42 Feb 4 at 19:07
• @Make42 That's a job for \protected@write – egreg Feb 4 at 20:15
• @egreg: I tried that but that does not yet work. I put \makeatletter ... \makeatother around the entire command. I don't get errors anymore, but also nothing into the aux-file. In a minimal working example I actually get a different error. I put this into a new question: tex.stackexchange.com/questions/527063/… – Make42 Feb 5 at 9:45

Here is a 'by-pass', using a hack by writing a direct \newlabel command to the .aux file.

This is tested for hyperref only and not meant to be portable at all!

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage{hyperref}

\newcounter{UCcounter}
\setcounter{UCcounter}{0}

% Change to arabic counter format
\renewcommand{\theUCcounter}{\arabic{UCcounter}}

\newcommand{\usecaseref}[1]{\emph{\nameref{#1}}}

% Change behaviour (except calling of \theUCcounter) etc. at will
\newcommand{\usecaseprint}[1]{%
UC \theUCcounter: #1}%

\let\LaTeXStandardSection\section

\renewcommand{\section}[1]{%
\refstepcounter{UCcounter}%
\LaTeXStandardSection{\usecaseprint{#1}}%
% Bypass the normal label command with hyperrefs label style -- this uses the counter value at the
% moment the section is begun, not afterwards!
\immediate\write1{%  Directly write to aux - file -- I hope, this is portable
\string\newlabel{sec::uc::\theUCcounter}{{\thesection}{\thepage}{\usecaseprint{#1}}{section.\thesection}{}}}%
}%

\begin{document}

\section{First Use Case Name}

This is the forwards reference: \usecaseref{sec::uc::5}

\section{Second Use Case Name}
This is the backwards reference: \usecaseref{sec::uc::1}

\section{Third Use Case Name}
This is the backwards reference: \usecaseref{sec::uc::2}

\newpage

\section{Fourth Use Case Name}%\label{sec:second}
This is  yet another forward reference: \usecaseref{sec::uc::6}

\section{Fifth Use Case Name}%\label{sec:second}
This is  yet another backwards reference: \usecaseref{sec::uc::3}

\section{Sixth Use Case Name}%\label{sec:second}
This is  yet another backwards reference: \usecaseref{sec::uc::1}

\end{document}


I redefined the \section command, but it will only work with its default argument at the moment!

I also changed to automatic labelling for convenience purposes.

Thx!

I refined Christian H.'s solution to be able to use normal sections as well. Therefore I created a new command instead of redefining the \section command. This command also takes as a second argument a labelname, so I can refer by a logical name instead of by number. Here's the final solution:

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage{hyperref}

\newcounter{UCcounter}
\setcounter{UCcounter}{0}

% Change to arabic counter format
\renewcommand{\theUCcounter}{\arabic{UCcounter}}

\newcommand{\usecaseref}[1]{\emph{\nameref{#1}}}

% Change behaviour (except calling of \theUCcounter) etc. at will
\newcommand{\usecaseprint}[1]{%
UC \theUCcounter: #1}%

% Define a new command for section titles with Use Cases
% First argument = Use Case Name; second argument = label
\newcommand{\UCsection}[2]{%
\refstepcounter{UCcounter}%
\section{\usecaseprint{#1}}%
% Bypass the normal label command with hyperrefs label style -- this uses the counter value at the
% moment the section is begun, not afterwards!
\immediate\write1{%  Directly write to aux - file -- I hope, this is portable
\string\newlabel{uc:#2}{{\thesection}{\thepage}{\usecaseprint{#1}}{section.\thesection}{}}}%
}%

\begin{document}

\UCsection{First Use Case Name}{first}

This is the forwards reference: \usecaseref{uc:fifth}

\UCsection{Second Use Case Name}{sec}
This is the backwards reference: \usecaseref{uc:first}

\section{Normal Section}

\UCsection{Third Use Case Name}{third}
This is the backwards reference: \usecaseref{uc:sec}

\newpage

\section{Another Normal Section}

\UCsection{Fourth Use Case Name}{fourth}
This is  yet another forward reference: \usecaseref{uc:six}

\UCsection{Fifth Use Case Name}{fifth}
This is  yet another backwards reference: \usecaseref{uc:third}

\section{Yet another normal section}

\UCsection{Sixth Use Case Name}{six}
This is  yet another backwards reference: \usecaseref{uc:first}

\end{document}


This produces: