13

I have some very long section titles and do not want their entries in the TOC to be hyphenated. example output

\documentclass{scrreprt}
%\usepackage[ngerman]{babel}
\usepackage{tocloft}
\cftsetindents{section}{0mm}{12mm}
\cftsetindents{chapter}{0mm}{12mm}

\begin{document}
\tableofcontents
\chapter{Introduction}
\section{This kind of problem, that kind of problem, other ones and further considerations}
\section{More problems, even more problems, much worse than these, headline hyphenation, and blablablablablablablablabla and XYZ-blablablablablablablablabla}
\end{document}

From some googling, I got the impression that \renewcommand{\cftsecfont}{\raggedright} should help, but it does not. Neither does \begingroup \raggedright \tableofcontents \endgroup from the only (not accepted) answer to a related question. (When I uncomment \usepackage{ngerman]{babel}, the hyphenation of "consid-erations" changes to "consi-derations", interestingly.)

The problem occurs also when I do not use tocloft or when I change the document class to book, for example (with hyphenation at different syllables).

Can I make hyphenation in the TOC go away completely, without resorting to re-defining it for every offending word?! Bonus points if the solution does not make long words go over the line like the final "blabla...", even more bonus points if word combinations that contain a hyphen to begin with ("XYZ-blabla") can be brought on the same line.

  • 1
    Try \makeatletter \renewcommand{\@tocrmarg}{2.55em plus1fil} \makeatother. The command \@tocrmarg fills up the Right side of the TOC MARGin; it needs to be bigger than the Page NUMber WIDTH, or \@pnumwidth. – jon Dec 14 '14 at 2:08
  • @jon - You should make this comment an answer. – Mico Dec 14 '14 at 3:56
  • Thanks @Mico. I added one (though I think Gonzalo is right). – jon Dec 14 '14 at 4:08
  • Perhaps this is relevant: tex.stackexchange.com/questions/139740/… – caw Aug 23 '17 at 0:29
  • It is not recommended to use package tocloft together with a KOMA-Script class. – esdd May 6 '18 at 11:01
8

Since you are using a KOMA class, the way to go is to use the tocstyle package:

enter image description here

The code:

\documentclass{scrreprt}
\usepackage[ngerman]{babel}
\usepackage[tocflat]{tocstyle}

\newtocstyle{raggedstyle}{%
  \settocstylefeature[0]{entryhook}{\bfseries}
  \settocstylefeature[0]{leaders}{\hfill}
  \settocfeature{raggedhook}{\raggedright}
  \settocfeature{spaceafternumber}{17pt}
}
\usetocstyle{raggedstyle}

\begin{document}

\tableofcontents

\chapter{Introduction}
\section{This kind of problem, that kind of problem, other ones and further considerations}
\section{More problems, even more problems, much worse than these, headline with no hyphenation and somo more text goes here for the example}

\end{document}

Refer to the package documentation for further adjustments.

  • Thank you! I had a look at tocstyle before, but felt I did not understand the manual well enough to try defining my own toc style. – arne.b Dec 14 '14 at 21:48
6

I think I'd be inclined to use Gonzalo Medina's answer given you are using scrreprt (and skip loading tocloft), but a tocloft solution isn't too tricky (and also useful for memoir users):

\documentclass{scrreprt}
\usepackage[ngerman]{babel}
\usepackage{tocloft}
\cftsetindents{section}{0mm}{12mm}
\cftsetindents{chapter}{0mm}{12mm}

\makeatletter
% \renewcommand{\@tocrmarg}{2.55em plus1fil}
\renewcommand{\@tocrmarg}{\@pnumwidth plus1fil} % <-- Revised
\makeatother

\begin{document}
\tableofcontents
\chapter{Introduction}
\section{This kind of problem, that kind of problem, other ones and further considerations}
\section{More problems, even more problems, much worse than these, headline hyphenation, and blablablablablablablablabla and XYZ-blablablablablablablablabla}

\end{document}

You should, in general, make sure the revised value for \@tocrmarg (TOC Right MARGin) is greater than that for \@pnumwidth (Page NUMber WIDTH).

  • 1
    You could use \@pnumwidth directly in the redefinition of \@tocrmarg, i.e., set \renewcommand{\@tocrmarg}{\@pnumwidth plus1fil}. – Mico Dec 14 '14 at 4:14
  • @Mico -- That's an obvious improvement. Good idea! – jon Dec 14 '14 at 4:32
2

A solution with the titletoc package:

\documentclass{scrreprt}
\usepackage[showframe]{geometry}
%\usepackage[ngerman]{babel}
\usepackage{ragged2e}
\usepackage{fmtcount}
\usepackage{titletoc}
 \titlecontents{chapter}[12mm]
 {\contentsmargin{10mm}\bigskip\sffamily\bfseries\Large}
 {\contentslabel[\MakeUppercase{\romannumeral\thecontentslabel}]{12mm}}
 {}
 {\hfill\contentspage}[\medskip]


 \titlecontents{section}[12mm]
 { \rightskip=10mm plus 1fil\hyphenpenalty=10000\contentsmargin{2mm}}%
 {\contentslabel[\thecontentslabel.]{12mm}}
 {}
 {\hskip2pt\titlerule*[6pt]{.}\contentspage}


\begin{document}
\tableofcontents

\setcounter{page}{100}
\chapter{Introduction}
\section{This kind of problem, that kind of problem, other ones and further considerations}
\section{More problems, even more problems, much worse than these, headline hyphenation, and blablablablablablablablabla and XYZblablablablablablablablabla}

\end{document} 

enter image description here

  • @Gonzalo Medina: Yes, it's a problem with my editor configuration, and I didn't check the code. Thanks for pointing that. It's corrected now. – Bernard Dec 14 '14 at 3:52
2

The usage of package tocloft together with a KOMA-Script class is not recommended. Here is a suggestion without this package and without the redefinition of an internal command:

\documentclass{scrreprt}
\RedeclareSectionCommands[
  tocindent=0mm,
  tocnumwidth=12mm,
  tocraggedentrytext
]{chapter,section}

\begin{document}
\tableofcontents
\chapter{Introduction}
\section{This kind of problem, that kind of problem, other ones and further considerations}
\section{More problems, even more problems, much worse than these, headline hyphenation, and blablablablablablablablabla and XYZ-blablablablablablablablabla}
\end{document}

Result:

enter image description here

You could also use class option toc=flat instead setting tocindent=0mm and tocnumwidth=12mm:

\documentclass[toc=flat]{scrreprt}
\RedeclareSectionCommands[
  tocraggedentrytext
]{chapter,section}

\begin{document}
\tableofcontents
\chapter{Introduction}
\section{This kind of problem, that kind of problem, other ones and further considerations}
\section{More problems, even more problems, much worse than these, headline hyphenation, and blablablablablablablablabla and XYZ-blablablablablablablablabla}
\end{document}

Note that there is an additional run needed for the alignment.

Result:

enter image description here

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.