3

I want to use some automatic cross referencing (I am using hyperref in the whole document which I customized) to write something like :

We see in \autoref{partname} that

To print :

We see in part I that

The problem is I redefined the part name for my TOC and here is the MWE for my problem :

\documentclass[12pt,a4paper]{book}

% Ecrire en français
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage{xspace} % Pour les espaces automatiques

% Part name in TOC
\usepackage{tocloft}
\renewcommand\cftpartpresnum{\hfill}
\renewcommand\cftpartleader{\hfill}
\renewcommand{\thepart}{Partie \Roman{part} --\hspace{-.4cm}}

\usepackage{hyperref}

\begin{document}
\tableofcontents
\part{First Part}\label{first}

\chapter{Intro}

We will deal with \autoref{first}

We will deal with \ref{first} 

When I add ponctuation : \autoref{first}. It is not beautiful. 
\end{document}

Giving me :

enter image description here

All I want is to change the capital P into a non capital letter, supress the dash and get ok with the space after. Since I can create a command, all I need is answer this precise question :

How do I get access to the part numbers, for example I. I tried with \getrefnumber but that gives "Partie I -"

Am I stuck with this problem or is there a mean of getting both my TOC and auto reference system ?

Thanks in advance.

  • 1
    xpatch isn't needed here – user31729 Apr 28 '16 at 16:08
  • I removed xpatch. @Mico I don't understand, there is no : in my labels ! – John Marjan Apr 28 '16 at 16:15
  • @JohnMarjan: that \hspace{...} in your \thepart is not really useful, in my point of view. The \autorefname is using \partname, not \thepart. By using \thepartie with the the negative \hspace value, your text It is not beautiful is shifted back and overprints the -- characters. – user31729 Apr 28 '16 at 16:36
  • @ChristianHupfer the \hspace was for adding space in the TOC, it was a sort of "trick" I used. – John Marjan Apr 28 '16 at 16:49
3

Don't use \hspace in a \the... command -- it screws up references (formats)

\documentclass[12pt,a4paper]{book}

% Ecrire en français
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage{xspace} % Pour les espaces automatiques

% Part name in TOC
\usepackage{tocloft}
\renewcommand\cftpartpresnum{\hfill}
\renewcommand\cftpartleader{\hfill}
\renewcommand{\thepart}{Partie \Roman{part}}% --\hspace{-0.4cm}}

\usepackage{hyperref}

\begin{document}
\part{First Part}\label{first}

\chapter{Intro} 


We will deal with \autoref{first}

We will deal with \ref{first} 

When I add ponctuation : \autoref{first}. It is beautiful! 

When I add ponctuation : \autoref{second}. It is not beautiful. 


% Now bad:

\renewcommand{\thepart}{Partie \Roman{part} --\hspace{-0.4cm}}
\part{Second Part}\label{second}

Foo



\end{document}

enter image description here

Update -- Curing the -- issue!

\documentclass[12pt,a4paper]{book}

% Ecrire en français
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage{xspace} % Pour les espaces automatiques


\usepackage{xpatch}

\newcommand{\partnamedivider}{\textendash}

\makeatletter
% Must be done here, before tocloft 'screws' up the definition of \@part
\xpatchcmd{\@part}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
  \addcontentsline{toc}{part}{\thepart\ \partnamedivider\ #1}
}{\typeout{Success!!!!!}}{\typeout{Failed!!!!!}}
\makeatother

% Part name in TOC
\usepackage{tocloft}
\renewcommand\cftpartpresnum{\hfill}
\renewcommand\cftpartleader{\hfill}
\renewcommand{\thepart}{Partie \Roman{part}}


\usepackage{hyperref}



\begin{document}
\tableofcontents
\part{First Part}\label{first}

\chapter{Intro} 


We will deal with \autoref{first}

We will deal with \ref{first} 

When I add ponctuation : \autoref{first}. It is beautiful! 

When I add ponctuation : \autoref{second}. It is not beautiful. 


% Now bad:

\renewcommand{\thepart}{Partie \Roman{part} --\hspace{-0.4cm}}
\part{Second Part}\label{second}

Foo



\end{document}
  • The problem is that I need the -- in the part name for my toc. I tried your document and the toc has changed. – John Marjan Apr 28 '16 at 16:53
  • @JohnMarjan: Your post does not reveal that -- is essential at all – user31729 Apr 28 '16 at 17:43
  • Thats because I fortgot \tableofcontentsin the MWE ^^ – John Marjan Apr 28 '16 at 17:45
  • 1
    @JohnMarjan: See the update at the bottom, please – user31729 Apr 28 '16 at 18:00
2

You should use cleveref, it's much more powerful. I used the patches from my other answer

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}

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

%%% patches to \@part and \@chapter must
%%% be done before loading hyperref
\makeatletter
\patchcmd{\@part}
  {\thepart\hspace{1em}}
  {\protect\partnumberline{\thepart}}
  {}{}
\patchcmd{\@chapter}
  {\numberline}
  {\chapternumberline}
  {}{}
\makeatother

\usepackage{hyperref}
\usepackage[french,capitalize,nameinlink]{cleveref}

\newcommand\partnumberline[1]{\textsc{Partie} #1 -- }
\newcommand\chapternumberline[1]{\textsc{\chaptername} #1 -- }

\renewcommand\cftpartafterpnum{\par\nobreak\bigskip}

\renewcommand\cftdotsep{2}
\renewcommand{\cftpartleader}{\cftdotfill{\cftdotsep}} 
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}

\begin{document}

\tableofcontents

\part{First Part}\label{first}

\chapter{Intro}

We will deal with \cref{first}

We will deal with \ref{first} 

When I add ponctuation : \cref{first}. It is not beautiful. 
\end{document}

enter image description here

  • I saw your other answer. It seems clear to me that cleverref is powerfull, but since I wrote a whole preambule with hyperref it is long to change everything (I customize the hyperref for figures, subfigures to make them small caps, for the equations to be like "equation (1)"). When I have time I will change for sure ! – John Marjan Apr 29 '16 at 16:19
1

I found a way of doing it :

\hyperref[<part label>]{Partie \Roman{part}}

is doing the job, then I just have to redefine a new command for it. Yes this in not elegant since there will be \autoref in the text and \MyCommand for parts.

Still if there is better, let me now :)

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.