# Titlesec & Titletoc: dangerous relationships

\documentclass[a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{cfr-lm}

\usepackage{titlesec}
\renewcommand{\thechapter}{\scshape\roman{chapter}}
\titleformat{\chapter}[display]
{\normalfont\fontsize{11pt}{12pt}\selectfont}{\thechapter}{0pt}{}
%%{\normalfont\fontsize{11pt}{12pt}\selectfont}{\chaptertitlename\space \thechapter}{11pt}{}
\renewcommand\thesection{\arabic{section}}
\titleformat{\section}
{\normalfont\fontsize{11pt}{12pt}\selectfont}{\thesection}{1em}{}
\titleformat{\subsection}
{\normalfont\fontsize{11pt}{12pt}\selectfont\itshape}{}{1em}{}
\titlespacing{\chapter}{0pt}{0pt}{3cm}

\usepackage{titletoc}
\titlecontents{chapter}
[1.5em] % ie, 1.5em (chapter) + 2.3em
{}
{\contentslabel{2.3em}}
{\hspace*{-2.3em}}
{}
%{\titlerule*[1pc]{.}\contentspage}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% codice Pignalberi
\contentsmargin{0pt}
\titlecontents{chapter}[4pc]
{}
{}
{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\tableofcontents

\chapter{Chapter 1 title}
Some text some text some text some text some text

\section{Section a title}
Some text some text some text some text some text

\section{Section b title}
Some text some text some text some text some text

\chapter{Chapter 2 title}
Some text some text some text some text some text

\section{Section c title}
Some text some text some text some text some text

\section{Section d title}
Some text some text some text some text some text

\chapter{Chapter 3 title}
Some text some text some text some text some text

\section{Section e title}
Some text some text some text some text some text

\section{Section f title}
Some text some text some text some text some text
\end{document}


I tried to change, according to my needs, and the index format is ok. As a second step, I'd like to put page numbers in the ToC on left side. But the code I found (in my MWE "codice Pignalberi") interferes with the previous one. For I'm not able to understand, following the titlesec/titletoc manual, where and how to coordinate the two sides of this code, I hope in your help.

===================================================

UPDATE

All works very fine now, thanks to Gonzalo's code. I improved two aspect (add more space before chapter titles and remove chapter numbers, leaving only sections' ones: now it looks me more useful to the reader). The code is the following one:

<pre>\usepackage{titlesec}
\usepackage[rightlabels,dotinlabels]{titletoc}

\renewcommand{\thechapter}{\roman{chapter}}
\renewcommand\thesection{\arabic{section}}

\titleformat{\chapter}[display]
{\normalfont\fontsize{11}{12}\selectfont}
{\scshape\thechapter}
{0pt}
{}
\titleformat{\section}
{\normalfont\fontsize{11}{12}\selectfont}
{\thesection}
{1em}
{}
\titleformat{\subsection}
{\normalfont\fontsize{11}{12}\selectfont\itshape}
{}
{1em}
{}

\titlespacing{\chapter}
{0pt}{0pt}{3cm}

\titlecontents{chapter}[-3pc]
{\makebox[3pc][r]{\hfill}%
}
{}
{}
{}
\titlecontents{section}[-3pc]
{\makebox[4pc][r]{\thecontentspage\hfill}%
}
{}
{}
{}</pre>


Lastly, I'd like to align right and not left all numbers (pages and sections), for actually the titles of the section are not well aligned using a proportional font. Consider that the titletoc options rightlabels,dotinlabels seem to me not working. Thanx a lot! You can see the problem here:

==========================================================

UPDATE 2

I tried to use etoc copying your code into my .tex file, but the result is very strange:

==========================================================

UPDATE 3

The troubles above are due to bidi package, for in that file I use Hebrew and Phoenician too. In a "normal" file etoc code works fine. Only a last question: I have to read very well etoc documentation, which is rather large. But how is it possible to align right page numbers too? thanx!!

-
Don't load titletoc? Basically, you give 2 definitions of what to do in the TOC for chapters. This obviously creates problems. Assuming you want the second definition, then, dropping the first should work. Obviously if you want the first, you'd drop the second instead. I guess I'm not understanding the question... – cfr Mar 23 '14 at 15:20
The first code is to give a different format to ToC; it keeps the page numbers in the right. The secondo code ("Pignalbei code") is able to put page numbers on left side, even if it produces further effects. I'd like to mantain my format and translate page numbers in left side. I realize that the two codes are not compatible; but keeping my original format, I don't know how move page numbers on left side... – user41063 Mar 23 '14 at 15:27
So you want page numbers on the left. And where should the chapter number go then? – Gonzalo Medina Mar 23 '14 at 16:31
What format are you trying to achieve exactly? Since those two lots of code specify different formats for the same elements of the TOC, I'm not very clear how you want to combine them. Which format do you want to maintain? – cfr Mar 23 '14 at 16:43
I'd like to have all numbers on le left, not only chapters, as in this page: [url]liceofranchetti.it/out.png[/url] – user41063 Mar 23 '14 at 18:06

This is just a slight variant on that part of Gonzalo Medina's answer which uses etoc. I don't know how it behaves in interaction with bidi.

With etoc things become easier once one has a supply of examples.

Here I have added some graphical aid to understand the layout, particularly for vertical alignment. Section page numbers go to the left margin. Chapters and section numbers are right aligned and there is a 1em horizontal separation from the actual heading. The chapter names start at 1cm from the left margin and the section names at 2cm. Long names wrap at a distance of 1cm from the right text margin.

The update (added at the end) is in response to comments about treating unnumbered chapters and displaying page numbers also for some selected chapters. (both are here illustrated on one unnumbered extra chapter).

\documentclass[a4paper]{report}
\usepackage[margin=2cm,paperwidth=14cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{cfr-lm}
\usepackage{titlesec}
\usepackage{etoc}
\usepackage{color}

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand\thesection{\arabic{section}}

\titleformat{\chapter}[display]
{\normalfont\fontsize{11}{12}\selectfont}
{\scshape\thechapter}
{0pt}
{}
\titleformat{\section}
{\normalfont\fontsize{11}{12}\selectfont}
{\thesection}
{1em}
{}
\titleformat{\subsection}
{\normalfont\fontsize{11}{12}\selectfont\itshape}
{}
{1em}
{}

\titlespacing{\chapter}
{0pt}{0pt}{3cm}

\etocsetstyle{chapter}
% FOR VISUALIZING LAYOUT:
% {\fboxsep-\fboxrule\noindent\normalsize
%        \fbox{\makebox[1cm][r]{\strut
%                          \smash{\color{blue}\vrule depth12cm}\hspace{1em}%
%                          \smash{\color{green}\vrule depth12cm}}}%
%        \fbox{\makebox[1cm][r]{\color{blue}\strut
%                          \smash{\color{blue}\vrule depth12cm}\hspace{1em}%
%                          \smash{\color{green}\vrule depth12cm}}}%
%        \fbox{\makebox[1cm]{\strut}}(debugging layout)\hfill
%        \fbox{\makebox[1cm][l]{\strut\smash{\color{red}\vrule
%              depth12cm}}}%
%   \hrule}
{}% <- uncomment the above and suppress this line if debugging layout
{\pagebreak[3]\vskip25pt\leftskip 1cm\relax
\rightskip 1cm\relax
\parindent 0pt\relax
}
{\Large\leavevmode
\llap{\makebox[1cm][r]{\normalsize\etocnumber.\hspace{1em}}}%
\etocname\par\nopagebreak\medskip }
{\bigskip}

\etocsetstyle{section}
{}
{\leftskip 2cm\relax}
{\normalsize\leavevmode
\llap{\makebox[1cm][r]{\mdseries\etocpage}%
\hspace{1cm}%
\makebox[1cm][r]{\etocnumber.\hspace{1em}}}%
\etocname\par\pagebreak[1]\smallskip }
{}

\begin{document}
\tableofcontents

\chapter{Vita e Opere}
\setcounter{page}{98}% just for the example
Some text some text some text some text some text

\section{Infanzia, studi a Lipsia e soggiorno a Parigi}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\chapter{Seggni (caratteri'') e idee}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\clearpage

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\end{document}


\documentclass[a4paper]{report}
\usepackage[margin=2cm,paperwidth=14cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{cfr-lm}
\usepackage{titlesec}
\usepackage{etoc}
\usepackage{color}

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand\thesection{\arabic{section}}

\titleformat{\chapter}[display]
{\normalfont\fontsize{11}{12}\selectfont}
{\scshape\thechapter}
{0pt}
{}
\titleformat{\section}
{\normalfont\fontsize{11}{12}\selectfont}
{\thesection}
{1em}
{}
\titleformat{\subsection}
{\normalfont\fontsize{11}{12}\selectfont\itshape}
{}
{1em}
{}

\titlespacing{\chapter}
{0pt}{0pt}{3cm}

%% ETOC SPECIFICATIONS

% conditional to display chapter page number
% a priori set to false
\newif\ifDisplayInTocChapterPageNumber

\etocsetstyle{chapter}
{}
{\pagebreak[3]\vskip25pt\leftskip 1cm\relax
\rightskip 1cm\relax
\parindent 0pt\relax
}
{\Large\leavevmode
\llap{\ifDisplayInTocChapterPageNumber
\makebox[1cm][r]{\mdseries\normalsize\etocpage}%
\fi
\makebox[1cm][r]{\normalsize\etocifnumbered{\etocnumber.}{}%
\hspace{1em}}}%
\etocname\par\nopagebreak\medskip }
{\bigskip}

\etocsetstyle{section}
{}
{\leftskip 2cm\relax}
{\normalsize\leavevmode
\llap{\makebox[1cm][r]{\mdseries\etocpage}%
\hspace{1cm}%
\makebox[1cm][r]{\etocnumber.\hspace{1em}}}%
\etocname\par\pagebreak[1]%\smallskip
}
{}

\begin{document}
\tableofcontents

\chapter{Vita e Opere}
\setcounter{page}{98}% just for the example
Some text some text some text some text some text

\section{Infanzia, studi a Lipsia e soggiorno a Parigi}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\chapter{Seggni (caratteri'') e idee}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\clearpage

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\chapter*{Un capitolo non numerato e col numero di pagina nel margine dell'indice}
\addcontentsline{toc}{chapter}{Un capitolo non numerato e col numero di
pagina nel margine dell'indice}
\setcounter{section}{0}

\section{Ultimi anni}
Some text some text some text some text some text

\end{document}

-
Ah, I was just reading the edit 3 to the question, but now you've provided a solution. I just want to thank you for letting me know the etoc package (in the last days I´ve seen some of the answers you've provided using it). I had never before used it myself, but with this question I decided to give it a try. It's really powerful! – Gonzalo Medina Mar 24 '14 at 14:54
@GonzaloMedina :-) the manual has examples but some are not developed enough, others are too specific (like doing a tree or a table). I now stabilized on the kind of technique used in section 5 of the manual a second example, or the one from section 31.5 using depth tags. – jfbu Mar 24 '14 at 15:09
Great! I made some little changes to format (i.e. titles in 11pt as the text...). A question and a problem. 1) How decrease line-spacing in the ToC? I'd like to have the same normal line-spacing of the normal text. 2) I've two chapters, Bibliography and Index (with imakeidx) which are \chapter*. Your code produces the titles with a (worthless) period before. More, I know that in the other chapters there is no page number (that's ok for me), but in these two cases (there is no \section here) it would be suitable to supply it. Is it feasible? – user41063 Mar 24 '14 at 16:30
By trials and errors I solved for line-spacing (discovering the role of \smallskip inside the code). It remains the problem of starred chapters – user41063 Mar 24 '14 at 16:37
@user41063 see updated answer. The \etocifnumbered{YES}{NO} switch allows to adapt code to presence or absence of a sectioning number. For decidnig to display or no the page number, one can use a specific boolean, and add it to the .toc file. see the code. – jfbu Mar 24 '14 at 17:40

Below I show two options; the first one uses etoc and the second one, titletoc. The final desired formatting is still not completely clear to me. The question asks to place page numbers for chapter os the left, and then an image is provided in the comments in which chapter entries don't have page numbers to the left? Anyway, the example code below allow for easy customizations to achieve whatever format is desired.

One possibility using the powerful etoc package:

\documentclass[a4paper]{report}
\usepackage[margin=2cm,paperwidth=14cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{cfr-lm}
\usepackage{titlesec}
\usepackage{etoc}

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand\thesection{\arabic{section}}

\titleformat{\chapter}[display]
{\normalfont\fontsize{11}{12}\selectfont}
{\scshape\thechapter}
{0pt}
{}
\titleformat{\section}
{\normalfont\fontsize{11}{12}\selectfont}
{\thesection}
{1em}
{}
\titleformat{\subsection}
{\normalfont\fontsize{11}{12}\selectfont\itshape}
{}
{1em}
{}

\titlespacing{\chapter}
{0pt}{0pt}{3cm}

\etocsetstyle{chapter}
{}
{\vskip25pt\leavevmode\leftskip 0cm\relax}
{\parbox[t]{.5cm}{\etocnumber.\hfill}%
\parbox[t]{\dimexpr\linewidth-1cm-15pt\relax}{\Large\etocname}\hfill\par\medskip
}
{}
\etocsetstyle{section}
{}
{\leavevmode\leftskip 0cm\relax}
{\llap{\parbox[t]{1.5cm}{\mdseries\hfill\etocpage}\hspace{.5cm}}%
\parbox[t]{\dimexpr\linewidth-1cm-15pt\relax}{\etocname\par}\hfill\par
}
{}

\begin{document}
\tableofcontents

\chapter{Vita e Opere}
\setcounter{page}{98}% just for the example
Some text some text some text some text some text

\section{Infanzia, studi a Lipsia e soggiorno a Parigi}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\chapter{Seggni (caratteri'') e idee}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\clearpage

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\end{document}


Here's another possibility using titletoc:

\documentclass[a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{cfr-lm}
\usepackage{titlesec}
\usepackage{titletoc}

\renewcommand{\thechapter}{\roman{chapter}}
\renewcommand\thesection{\arabic{section}}

\titleformat{\chapter}[display]
{\normalfont\fontsize{11}{12}\selectfont}
{\scshape\thechapter}
{0pt}
{}
\titleformat{\section}
{\normalfont\fontsize{11}{12}\selectfont}
{\thesection}
{1em}
{}
\titleformat{\subsection}
{\normalfont\fontsize{11}{12}\selectfont\itshape}
{}
{1em}
{}

\titlespacing{\chapter}
{0pt}{0pt}{3cm}

\titlecontents{chapter}[-4pc]
{\makebox[2pc][l]{\hfill\thecontentspage}\hspace{2pc}%
}
{}
{}
{}
\titlecontents{section}[-4pc]
{\makebox[2pc][l]{\hfill\thecontentspage}\hspace{3pc}%
}
{}
{}
{}

\begin{document}
\tableofcontents

\chapter{Chapter 1 title}
\setcounter{page}{98}% just for the example
Some text some text some text some text some text

\section{Section a title}
Some text some text some text some text some text

\section{Section b title}
Some text some text some text some text some text

\chapter{Chapter 2 title}
Some text some text some text some text some text

\section{Section c title}
Some text some text some text some text some text

\section{Section d title}
Some text some text some text some text some text

\chapter{Chapter 3 title}
Some text some text some text some text some text

\section{Section e title}
Some text some text some text some text some text

\section{Section f title}
Some text some text some text some text some text

\end{document}


-
I'm very grateful to u. Please, see my adds above... – user41063 Mar 23 '14 at 21:09
@user41063 I've updated my answer with the new requirements and using the etoc package. In some minutes I'll alos add the option with titletoc. But, you should consider using etoc; it's a really poerful package :) – Gonzalo Medina Mar 23 '14 at 23:03
@user41063 Answer updated with titletox. However, this option has some problems (longer titles won't behave well). My suggestion would be to switch to the etoc solution instead. – Gonzalo Medina Mar 23 '14 at 23:33
You're right, etoc works better. However, it produces a strange result on my file. I put image above, in my originale post – user41063 Mar 24 '14 at 0:00
@user41063 I recommend using \tstyle for the numbering here. It is too tabular-like for proportional figures to look right (but you probably want to keep OSF for consistency if these are used for numbering in the document itself). – cfr Mar 24 '14 at 2:50

If I've understood correctly your layout, here is a basic solution based on titletoc:

\newcommand\boxedpage{\makebox[3pc][r]{\thecontentspage}}

\titlecontents{chapter}[0pc]
{\contentslabel[\boxedpage\boxedtocnum]{6pc}}
{}{}{}
\titlecontents{section}[3pc]
{}
{\contentslabel[\boxedpage\hspace*{3pc}\boxedtocnum]{9pc}}
{}{}{}


Just play with dimens.

EDIT. Here is a complete example with titletoc. Remember you can control the numbering with secnumdepth, and the included entries in the TOC with tocdepth. Remember also with titletoc you can change the format of TOC entries in the middle of the document (for subsequent sections), if for some reason you need it. As before, just play with dimens.

\documentclass{book}

\renewcommand{\thesection}{\arabic{section}}

\usepackage{titletoc}

\newcommand\boxedpage{\makebox[3pc][r]{\thecontentspage}}

\titlecontents{chapter}[3pc]
{\contentslabel[\boxedpage\boxedtocnum]{6pc}}
{\contentslabel[\boxedpage\hspace*{3pc}]{6pc}}
{}{}
\titlecontents{section}[6pc]
{}
{\contentslabel[\boxedpage\hspace*{3pc}\boxedtocnum]{9pc}}
{\contentslabel[\boxedpage\hspace*{6pc}]{9pc}}
{}{}

\newcommand*\asection{
\section{A Honovver, text text al servizio del ducchi Johann
Friedrich ed Ernst August.}
Some text some text some text some text some text\par}

\begin{document}
\tableofcontents

\chapter{Vita e Opere}
\setcounter{page}{98}% just for the example

\asection\asection

\chapter{Seggni (caratteri'') e idee}

\asection\asection\asection\asection\asection
\asection\asection\asection\asection\asection

% From now on, unnumbered headers, but still in the toc
% down to section
\setcounter{secnumdepth}{-1}
\chapter{Seggni (caratteri'') e idee}

@ javier Compiling, I receive the following error: (./zzz.bbl) [1] (./zzz.toc ! Undefined control sequence. l.43 \DisplayInTocChapterPageNumbertrue ? – user41063 Mar 24 '14 at 22:46