5

I am using a custom documentclass (based on JHEP), where there are no dots in table of contents automatically for subsections. I am adding section names manually to TOC. Is it possible to add somehow \dotfill to TOC like

\addcontentsline{toc}{subsection}{Section name\dotfill}

This does not work, dots do not reach page number. They go until the half of the line or so...

Also, tocloft package gives errors, cannot use it.

MWE:

\documentclass{JHEP}
\author{A.U. Thor}
\title{Dotty}
\abstract{Not enough dots}

\usepackage{hyperref}
\begin{document}
\tableofcontents
\addcontentsline{toc}{subsection}{Section name\dotfill}
\end{document}

enter image description here

lockstep
  • 250,273
  • Did you try the titletoc package? – Bernard Apr 30 '14 at 22:10
  • What controls this is actually the \l@section, \l@subsection, ... macros. Without seeing those or the actual (custom) document class, it would be difficult to assess what's going on. – Werner Apr 30 '14 at 23:20
  • @Bernard: No, i did not. i still want to set toc manually without any package only using \addcontentsline. – Tomas Carnogursky May 01 '14 at 07:36
  • Without a MWE is hard to guess as \documentclass{article} \begin{document} \tableofcontents \addcontentsline{toc}{section}{Section name\dotfill} \end{document} just work – Fran May 01 '14 at 08:09
  • @Fran your MWE doesnt work to me. maybe its the custom class, which is JHEP – Tomas Carnogursky May 01 '14 at 10:29
  • The following works OK here: \documentclass{JHEP} \author{a} \abstract{b} \title{c} \usepackage{hyperref} \begin{document} \tableofcontents \addcontentsline{toc}{section}{Section name\dotfill} \end{document} with JHEP.cls from http://www.ctan.org/pkg/jhep Edit: If you have some other version of the class, please add a link to it, and create a minimal (non-)working example – Torbjørn T. May 05 '14 at 19:09
  • @Torbjørn T. try \addcontentsline{toc}{subsection} instead of section and dots fill only half line. – Tomas Carnogursky May 09 '14 at 20:44
  • I see, sorry about that. Now, forgive me if I was presumptuous, but I added a link to the class on CTAN and an MWE to your question. Please make sure that it is the same class file that you use, and that the example code is OK. – Torbjørn T. May 10 '14 at 06:37
  • @TorbjørnT. its OK. However, it's not exactly the same class (i use a custom class not available online, but its modified and derived from JHEP), but Werner's workaround works to me as well. Thanks. – Tomas Carnogursky May 10 '14 at 11:43

1 Answers1

5

The following patch (via etoolbox) re-inserts the removed ToC-dots - yes, the document class specifically redefines \@tocline and removes the dots:

enter image description here

\documentclass{JHEP}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@tocline}{\hfill}{%
  \leaders\hbox{$\m@th
    \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
  mu$}\hfill}{}{}
\makeatother
\author{A.U. Thor}
\title{Dotty}
\abstract{Not enough dots}

\usepackage{hyperref}
\begin{document}
\tableofcontents
\addcontentsline{toc}{subsection}{Section name}
\end{document}

The above patch will insert dots in the ToC for \subsection, \subsubsection, \paragraph and \subparagraph (depending on the value of tocdepth), but not \section. For this you could add

\makeatletter
\patchcmd{\l@section}{\hfil}{%
  \leaders\hbox{$\m@th
    \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
  mu$}\hfill}{}{}
\makeatother

to the preamble as well. For more on leaders, see Want to fill line with repeating string.

Werner
  • 603,163