4

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:

enter image description here

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.

  • 1
    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
3

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. enter image description here

2

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:

enter image description here

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.