2

Generated TOC has a weird alignment. How to fix?

\documentclass[a4paper,11pt]{article}
\usepackage{ucs}
\usepackage[utf8]{inputenc}
\usepackage[english, ukrainian]{babel}
\usepackage[T1,T2A]{fontenc}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage[hidelinks=true]{hyperref}

\begin{document}
 \tableofcontents
 \section{Загальні зауваження до конфігурації комплексів XXXXX01RACK01,  XXXXX01RACK02 и YYYYY01RACK01}
 \blindtext
 \section{General comments on the configuration for the following systems XXXXX01RACK01,  XXXXX01RACK02 и YYYYY01RACK01}
 \blindtext
\end{document}

The result: enter image description here

Skillmon
  • 60,462
gray380
  • 67

2 Answers2

2

To manually force a line break in ToC you can use the optional argument to \section and include a \protect\\ or \protect\linebreak there:

\documentclass[a4paper,11pt]{article}
\usepackage{ucs}
\usepackage[utf8]{inputenc}
\usepackage[english, ukrainian]{babel}
\usepackage[T1,T2A]{fontenc}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage[hidelinks=true]{hyperref}

\begin{document}
 \tableofcontents
 \section{foo}
 \section[Загальні зауваження до конфігурації комплексів\protect\\
 XXXXX01RACK01,  XXXXX01RACK02 и\protect\\ YYYYY01RACK01]{Загальні зауваження до конфігурації комплексів XXXXX01RACK01,  XXXXX01RACK02 и YYYYY01RACK01}
 \blindtext
 \section[General comments on the configuration for the following systems
 XXXXX01RACK01,  XXXXX01RACK02 и\protect\\YYYYY01RACK01]{General comments on the configuration for the following systems
 XXXXX01RACK01,  XXXXX01RACK02 и YYYYY01RACK01}
 \blindtext
\end{document}

But perhaps using this optional argument to use a shorter version of the \section heading which breaks naturally might be better.

enter image description here

Skillmon
  • 60,462
  • But this also breaks the section title in the header of the page. It produces a large space instead of breaking the line in the header. Is there any solution which will keep the header intact – Damitr May 07 '20 at 18:45
  • @Damitr instead of using \\ you could use a macro like \mylinebreakonlyintoc and use \newcommand*\mylinebreakonlyintoc{\space} in the preamble. Then put \begingroup\renewcommand*\mylinebreakonlyintoc{\\} before and \endgroup after \tableofcontents. – Skillmon May 08 '20 at 08:13
0

Apart from using the optional argument, you can also write a custom command to print a linebreak when you are not in a section header (cf. How to determine if a macro is used inside the argument of a sectioning command?). This can be done by patching the \section command to switch a toggle and testing for the toggle in the custom command. The advantage is that you don't have to repeat the section titles twice, but if you want to use this on other levels (e.g., subsection titles) then you need to patch these as well.

MWE (no Ukrainian, sorry):

\documentclass{article}
\usepackage{etoolbox}
\newtoggle{insidesection}
\togglefalse{insidesection}
\patchcmd{\section}{\bfseries}{\bfseries\toggletrue{insidesection}}{}{}
\newcommand{\mybreak}{\iftoggle{insidesection}{\ }{\\}}

\begin{document}
\tableofcontents
\section{My section\mybreak with linebreak}
\end{document}

Result:

enter image description here

Marijn
  • 37,699