# tocloft cross-references a section as a subsection

I have document which creates a new list of examples. The list has a label \label{sec:examples} which labels the title of the list, which is rendered as a section. However, cross referencing sec:examples results in a reference to a subsection.

This is a small but complete example that demonstrates the problem:

\documentclass[conference]{IEEEtran}

\usepackage{lipsum}
\usepackage[titles]{tocloft}

\newlistof[section]{examples}{exp}{\listexampletitle}

\newcommand{\examplelist}[1]{%
\refstepcounter{examples}
{\protect\numberline{\theexamples}#1}
}

\newcommand{\example}[1]{\examplelist{#1}\textbf{Example \theexamples}: #1}

\cftsetindents{examples}{1.5em}{3.0em}
\cftpagenumbersoff{examples}
\setlength{\cftexamplesnumwidth}{1.5cm}

\begin{document}

\title{Example IEEE with tocleft}

\author{}

\maketitle

\section{Introduction}
Examples are summarised in Section \ref{sec:examples}.

\example{A first example}

\section{A section}
\subsection{A subsection}
\lipsum[1]{}
\example{A second example}

\listofexamples

\end{document}


A picture of the relevant output is below, with the problematic line highlighted in yellow. The first line of the Introduction, which is rendered as:

Examples are summarised in Section II.I.


Examples are summarised in Section III.


How do I correct this?

Output:

-
@HarishKumar: I agree with your objection about the placement of the label, but your suggestion would reference the first section however! –  Christian Hupfer Aug 28 at 10:02
@HarishKumar: I think, we have a misunderstanding... I believe, the OP just wants to add a reference to the 'section' where the list appears. –  Christian Hupfer Aug 28 at 10:08
That's right, it's the list that needs to be referenced, not any particular example. I have added an image of the PDF to clarify. –  snim2 Aug 28 at 10:19
@ChristianHupfer Yes I misunderstood! :-( –  Harish Kumar Aug 28 at 10:43
@snim2 Your demand of Examples are summarised in Section II. should have been Examples are summarised in Section III. with regard to the current picture as List of Examples is III not II. –  Harish Kumar Aug 28 at 10:51

Somehow the \newlistof command seems to group (didn't check this, admittedly) the \refstepcounter command within. It's better to patch the \listofexamples, first stepping the section counter, than immediately correcting it back by decreasing it. This is achieved by the etoolbox package command \pretocmd, which is done in this case a little lazy, I ignored the tests ;-)

I changed the definition of \listofexamples etc. a little bit, making it a true section.

\documentclass[conference]{IEEEtran}

\usepackage{lipsum}
\usepackage[titles]{tocloft}

\usepackage{etoolbox}

\newcommand{\listexampletitle}{List of Examples}

\newlistof[section]{examples}{exp}{\section{\listexampletitle}}%

% Prepending the \listofexamples command with dummy \refstepcounter{section} etc.

\newcommand{\examplelist}[1]{%
\refstepcounter{examples}%
{\protect\numberline{\theexamples}#1}
}%

\newcommand{\example}[1]{\examplelist{#1}\textbf{Example \theexamples}: #1}

\cftsetindents{examples}{1.5em}{3.0em}%
\cftpagenumbersoff{examples}
\setlength{\cftexamplesnumwidth}{1.5cm}

\begin{document}

\title{Example IEEE with tocleft}

\author{}

\maketitle

\section{Introduction}
Examples are summarised in Section \ref{sec:examples}.

\example{A first example}

\section{A section}
\subsection{A subsection}
\lipsum[1]{}
\example{A second example}

\listofexamples\label{sec:examples}
\end{document}


-
Awesome, thanks! –  snim2 Aug 28 at 10:26
@snim2: Please test it, give feedback later on -- I will return to it on problems, but for now I am off ;-) –  Christian Hupfer Aug 28 at 10:27