1

Referring to this discussion: Further titlesec-etoc customization, I've a further question. Consider the fine working code furnished to me by Gonzalo Medina:

\documentclass[a4paper,11pt]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{titlesec}
\usepackage{etoc}

\renewcommand{\thechapter}{\scshape\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}
{}
  {\addvspace{2ex}\leavevmode\leftskip 0cm\relax}
  {\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
    \normalsize\makebox[1cm][r]{\etocnumber\phantom{.}}\hspace{10pt}%
  \parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
  }
  {}
\etocsetstyle{section}
  {}
  {\leavevmode\leftskip 0cm\relax}
  {\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
    \normalsize\makebox[1cm][r]{\etocnumber.}\hspace{10pt}%
  \parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
  }
  {}


\begin{document}

\tableofcontents

\chapter{Chapter One}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}

\chapter{Chapter Two}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}
\section{Section Five}

\setcounter{page}{99}% just for the example

\chapter{Chapter Three}
\section{Section One}
\section{Section Two}
\section{Section Three}

\end{document}

If I don't need to put the chapter numbers, for the chapters begin all with respective section 1, I can remove them with a very small change in original code, i.e.

\etocsetstyle{chapter}
{}
  {\addvspace{2ex}\leavevmode\leftskip 0cm\relax}
  {\llap{\makebox[1.5cm][r]{\mdseries\phantom\etocpage}\hspace{.5cm}}%
    \normalsize\makebox[1cm][r]{\etocnumber\phantom{.}}\hspace{10pt}%
  \parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
  }
  {}

However, this way I lose the page number of Bibliography (and, I think, of the Index, if there is one), for they are starred chapters. So, is there a way to preserve those page number? (I solve this problem in another case, using titletoc/titlesec, but it's not the case for the present code with etoc). Thanx

ADDENDUM OCTOBER 2018

the same code compiled with xelates produces an anomalous protrusion in the first title chapter, I don't realize why:

\documentclass[a4paper,twoside,11pt]{report}
\usepackage{fontspec}
\setmainfont{EB Garamond}
\usepackage{polyglossia}
\setmainlanguage[babelshorthands=true]{italian}
\PolyglossiaSetup{italian}{indentfirst=false}

\usepackage{titlesec}
\usepackage{etoc}

\renewcommand{\thechapter}{\scshape\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}
  {}
  {\addvspace{2ex}\leavevmode\leftskip 0cm\relax}
  {\ifShowPage % conditional deciding the display or not of the page number
    \llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
   \fi
    \normalsize\makebox[1cm][r]{\etocnumber\phantom{.}}\hspace{10pt}%
  \parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
  }
  {}

\etocsetstyle{section}
  {}
  {\leavevmode\leftskip 0cm\relax}
  {\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
    \normalsize\makebox[1cm][r]{\etocnumber.}\hspace{10pt}%
  \parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
  }
  {}

\newif\ifShowPage

\begin{document}

\tableofcontents

\chapter{Chapter One}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}

\chapter{Chapter Two}
\section{Section One}
\section{Section Two}
\section{Section Three}

\end{document}

I'n not able to remove it... thanx enter image description here

2
  • The trivial add of a specific configuration for chapter* (something as \etocsetstyle{chapter*} {} {\addvspace{2ex}\leavevmode\leftskip 0cm\relax} {\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}% \normalsize\makebox[1cm][r]{\etocnumber\phantom{.}}\hspace{10pt}% \parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par } {}) do product no result, but an error: ! Missing \endcsname inserted. <to be read again> \Etoc@chapter*@ l.106 {} ?
    – user41063
    Commented Feb 17, 2015 at 9:57
  • Viz your comment, \etocsetstyle{chapter*}... raises an error because there was no prior \etocsetlevel{chapter*}{<level>}. Anyhow, this is irrelevant because \chapter* command does not write a \contentsline{chapter*}... line into the .toc file, it writes nothing at all. And etoc uses only data inscribed in the .toc file.
    – user4686
    Commented Mar 2, 2015 at 8:28

1 Answer 1

3

I am not sure about your specific situation, thus can only give general advice.

You mention \chapter* hence nothing is written to the .toc file. People usually make use of a \addcontentsline{toc}{chapter}{<Chapter title>} command after a \chapter* command to get an entry in the TOC.

To get the desired effect, one way to proceed is with a boolean \ifShowPage (I use here standard TeX's booleans you may prefer those provided by packages like etoolbox.) Then instruct the chapter style to examine the current status of this boolean and act appropriately

Thus, before an \addcontentsline{toc}{chapter}{<bibliography name>} you also do

\addtocontents{toc}{\protect\ShowPagetrue}

and etoc will act appropriately.

Example code (update to use \bibname and also avoid adding an extra chapter):

\documentclass[a4paper,11pt]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{titlesec}
\usepackage{etoc}

\renewcommand{\thechapter}{\scshape\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}
  {}
  {\addvspace{2ex}\leavevmode\leftskip 0cm\relax}
  {\ifShowPage % conditional deciding the display or not of the page number
    \llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
   \fi
    \normalsize\makebox[1cm][r]{\etocnumber\phantom{.}}\hspace{10pt}%
  \parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
  }
  {}

\etocsetstyle{section}
  {}
  {\leavevmode\leftskip 0cm\relax}
  {\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
    \normalsize\makebox[1cm][r]{\etocnumber.}\hspace{10pt}%
  \parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
  }
  {}

\newif\ifShowPage

\begin{document}

\tableofcontents

\chapter{Chapter One}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}

\chapter{Chapter Two}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}
\section{Section Five}

\setcounter{page}{99}% just for the example

\chapter{Chapter Three}
\section{Section One}
\section{Section Two}
\section{Section Three}

\cleardoublepage
\setcounter{page}{139}% just for the example

\begin{thebibliography}{99}
\addtocontents{toc}{\protect\ShowPagetrue}
\addcontentsline{toc}{chapter}{\bibname}
\bibitem{me2015} user41603, \emph{The Key to the Universe}, Cosmos Press, to appear.
\end{thebibliography}

\end{document}

table of contents using etoc

answer to updated question (october 2018)

As to the follow-up question, the explanation is simple: the TOC is typeset via paragraphs which obey the indentation. Except for the very first chapter entry in the TOC, because the document uses the indentfirst=false option which means to obey the standard LaTeX habit for anglo-saxon documents which is to not indent the first paragraph after a sectioning heading. Here the heading "Indice" of the TOC is responsible for acting as such.

Thus the indentation box is inserted on all lines except the very first.

To fix this there are many ways, e.g.

\makeatletter
\etocsetstyle{chapter}
    {\@afterindenttrue}
    {..}
    {..}
    {..}
\makeatother

The etoc macros for styling the chapter and section titles in the TOC in the MWE could (should?) have made provision to set the \parindent to 0pt to avoid such problems. One could also have used some \etociffirst{...}{...} to reinsert whitespace of \parindent width.

1
  • thanx a lot! you explain very well and solved my problem
    – user41063
    Commented Oct 20, 2018 at 18:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .