GeneralError while using \linebreak inside a heading

LaTeX specific issues not fitting into one of the other forums of this category.
AleCes
Posts: 286
Joined: Sat Nov 13, 2010 9:54 pm

Error while using \linebreak inside a heading

Post by AleCes »

localghost wrote:
AleCes wrote:[…] This is no solution: if I remove \hbox{-}, and leave a simple hyphen in place, the date will be split and that is unintended.
What about either customizing the ToC itself or the page geometry?
What do you mean by "customizing the ToC itself"? Why can't we just get this line break working?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Error while using \linebreak inside a heading

Post by localghost »

AleCes wrote:What do you mean by "customizing the ToC itself"? […]
E.g. influencing the width of entries for the headings and thus the line breaks. But using geometry for other (more appropriate) page dimensions would also be a good start.
AleCes wrote:[…] Why can't we just get this line break working?
Good question. At the moment I have no straightforward solution.
AleCes
Posts: 286
Joined: Sat Nov 13, 2010 9:54 pm

Error while using \linebreak inside a heading

Post by AleCes »

I can use geometry, but the layout is fine for me, I don't want to change it, actually I don't understand why \\ works but \linebreak doesn't. If some package somehow doesn't like line breaks, \\ should give me an error as well, but it doesn't!
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Error while using \linebreak inside a heading

Post by cgnieder »

AleCes wrote:[...] I don't understand why \\ works but \linebreak doesn't. [..]
Because \linebreak is a fragile command which in a moving argument such as a heading must be protected with \protect while \\ is robust, i.e. it has been defined via \DeclareRobustCommand. This has nothing to do with any package.

Code: Select all

\documentclass{article}
\newcommand*\cs[1]{\texttt{\textbackslash#1}}
\begin{document}
\tableofcontents

% error:
% \section{A heading\linebreak with a \cs{linebreak}}

% works:
\section{A heading \protect\linebreak with a \cs{linebreak}}

% works:
\section{A heading \\ with \cs{\textbackslash}}
\end{document}
Regards
site moderator & package author
AleCes
Posts: 286
Joined: Sat Nov 13, 2010 9:54 pm

Re: Error while using \linebreak inside a heading

Post by AleCes »

Thanks, cgnieder, it works! ;)
Post Reply