67

I have some section headings occupying two lines and I'm not satisfied with the way XeLaTeX breaks the heading. If I manually add \\ the output looks fine, but I get the following warning:

[hyperref] Token not allowed in a PDF string (PDFDocEncoding): removing `\\'. 

My code looks like that:

\section{this is a very long title I \\ want to break manually}

Is there a way to have a line-break there without the warning?

2
  • 3
    You could use \texorpdfstring within the \section argument.
    – Stefan Kottwitz
    Mar 21 '11 at 20:38
  • 4
    The warning is telling you that the bookmark created by hyperref is not goin to have a line break in it. Mar 21 '11 at 20:40
69

Use \texorpdfstring; the first argument can contain (La)TeX code and the second one (text only), will be used for the bookmarks:

\section[this is a very long title I want to break manually]{\texorpdfstring{this is a very long title I\\ want to break manually}{this is a very long title I want to break manually}}

If a ToC is to be produced and the break line is not desired there, a better option is to to use the optional argument of \section (this will be used for the headers, the ToC and the bookmarks):

\section[this is a very long title I want to break manually]{this is a very long title I\\ want to break manually}
3
  • Using the optional argument of the \section command should be enough to suppress the warning. It is a good idea to do this anyway, as Gonzalo mentions, to avoid the line break in the printed table of contents, if you have one. Sep 23 '11 at 18:08
  • 1
    Using this solution in a beamer document still raise an error: ! Argument of \@gobble has an extra }. Is it something specific to beamer ?
    – Ger
    May 28 '18 at 11:35
  • For me, it doesn't raise any error, but the Summary page still contains the line breaks :( May 4 '19 at 9:26
32

I recently ran into a similar issue where my section title was being split across two lines, but only one or two words were ending up on the second line. I wanted to manually insert a line break so that I could balance it better. I ended up using non-breaking spaces to force the line break to happen earlier instead:

\section{this is a very long title I want~to~break~manually}

This worked better for my table of contents as well, since the section title fit all on one line there (in the smaller font), but the non-breaking spaces forced LaTeX to split the section title more evenly across two lines in the document body.

3
  • 4
    Works with any LaTeX installation, avoids side effects, easy to tweak, easy to understand. Apr 27 '16 at 17:38
  • Such an elegant solution!
    – Doris
    Apr 30 '18 at 20:15
  • Nice. It's working fine for me and not affecting the ToC! Thanks!
    – A. Cedano
    Mar 1 '21 at 23:04
6

Just using \\ in the .tex file is enough to break the line in the heading when compiling to dvi file. But when compiling to pdf, the line breaks also in the table of contents and it looks strange. Thus, the best solution I found was converting dvi to pdf directly.

*\section{this is a very long title \\ I want to break manually}*
3
  • 2
    Welcome to TeX.sx! A tip: If you indent lines by 4 spaces or enclose words in backticks `, they'll be marked as code, as can be seen in my edit. You can also highlight the code and click the "code" button (with "{}" on it).
    – doncherry
    Oct 14 '12 at 0:10
  • 1
    Although you're right about compiling the file with latex (the different behavior is probably due to the different drivers loaded by hyperref), the OP mentioned the usage of xelatex, which produces PDF directly.
    – henrique
    Oct 14 '12 at 0:50
  • 1
    Is there a solution if I'm using XeLatex? May 4 '19 at 9:39

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.