When I define two new level using titlesec, the reference doesn't work correctly.

This is an example:

\documentclass{article}

\usepackage{titlesec}

% First level
\titleclass{\exemple}{straight}[\subsection]
\newcounter{exemple}[section]
\titlespacing{\exemple}{0pt}{\baselineskip}{1em}

\renewcommand{\theexemple}{\thesection.\roman{exemple}}

% -------------------------------------------------------------------
% second one
\titleclass{\suiteexemple}{straight}[\subsection]
\newcounter{suiteexemple}[section]

\titlespacing{\suiteexemple}{0pt}{\baselineskip}{1em}
\renewcommand{\thesuiteexemple}{\thesection.\roman{suiteexemple}}
% -------------------------------------------------------------------

\begin{document}

\section{one} \label{sec:one}

...

\exemple{First exemple}
\label{ex:one}

\exemple{Second example}

\section{two}

...

Reference to exemple : \ref{ex:one}

\end{document}


The reference is displayed as "1", but it should be "1.1" (the exemple counter is not displayed).

If you comment the code between the % ------ the reference is correctly displayed.

Do you know any workaround on this problem?

-
I get "Reference to exemple : 1" (just copied an pasted from the PDF file). – egreg Apr 27 '13 at 15:06
Yep, but it should be "Reference to exemple : 1.1" – Paul Pichaureau Apr 27 '13 at 17:08
Sorry I don't know how to include the result of a compilation. – Paul Pichaureau Apr 27 '13 at 17:08

The problem is in how \titleclass works.

With \titleclass{\exemple}{straight}[\subsection] you're inserting a new numbering level between \subsection and \subsubsection. So \example becomes level 3 and \subsubsection is shifted to level 4.

With the following \titleclass{\subexemple}{straight}[\subsection] you do the same, so \subexemple becomes level 3 and \exemple is shifted to level 5.

Therefore, with the usual value of 3 for secnumdepth, \exemple loses its numbering and the \label{ex:one} command will refer to the last "refstepped" command which happened at \section{one}.

You probably have better to define \suiteexemple in terms of \example* or saying

\titleclass{\suiteexemple}{straight}[\exemple]

-
Ok, I get it! I thought \titleclass...[\subsection] define something "at the same level" than \subsection I should define both command in term of \subsection – Paul Pichaureau May 1 '13 at 6:57