13

Although I do need line breaks within my title for my document's title page (I'm using the book class), I'd like to avoid hardcoding line breaks in the argument passed to the \title command, for three reasons:

  1. I always endeavour to separate form and content, for maintainability reasons.
  2. The title may need to be inserted elsewhere in the document, in places where line breaks within it are not warranted.
  3. I pass my title to\hypersetup{pdftitle=...}; hyperref returns a warning,

Token not allowed in a PDF string (PDFDocEnconding):(hyper ref) removing `\\'

if the title contains some \\, and I want to limit the number of compilation warnings to the bare minimum.

How can I effect line breaks at specific places within the title, upon inserting the latter somewhere in my document, without hardcoding \\'s in the \title{...} command? What's the best practice?

\documentclass{book}

\author{Me, me, me}
%\title{Me, myself, and I}      % compiles without mishap; strictly content
\title{Me,\\ myself, \\ and I}  % compilation warnings occur; form and content are mixed

\usepackage{kantlipsum}
\usepackage{hyperref}
\makeatletter
\hypersetup{
    pdftitle = \@title,
}
\makeatother

\begin{document}
    \maketitle
    \kant
\end{document}
jub0bs
  • 58,916
  • @AndrewSwann I didn't think the question warranted an MWE, but sure; it's on the way. – jub0bs Aug 09 '13 at 12:19
  • @Jubobs, I think Andrew Swann may have had the same though as me, I'd like to figure out why you need a forced line break in the title, rather than a subtitle – Chris H Aug 09 '13 at 12:21
  • 2
    Well for one thing, the title macro depends on the documentclass, and may or may not take an optional argument that could be used. Then there is \textorpdfstring for hyperref... – Andrew Swann Aug 09 '13 at 12:23
  • @ChrisH I can sympathisze with the general thought. Title layout on the page and its line breaks is quite critical with regard to emphasing/deemphasing certain words etc. – Andrew Swann Aug 09 '13 at 12:25
  • @AndrewSwann You're right: using \texorpdfstring would obviate the warning. However, what about my 2nd point? What if I need to format my title in a different way, somewhere else than on the title page? – jub0bs Aug 09 '13 at 12:30
  • @AndrewSwann, fair enough, I've tended to find that (on the rare occasions when I have the final choice) a subtitle does the job. For journal articles the choice of line breaking is likely to be taken away anyway, along with being able to add a subtitle. – Chris H Aug 09 '13 at 12:32
  • You could use a custom command \titlecr that is initially \\, but is \let to \relax just after \maketitle (assuming you have saved \@title), or redefined to something more appropriate in other applications. – Andrew Swann Aug 09 '13 at 12:44
  • if you always want certain phrases "tied together", use the ~ (tilde = tie) instead of a space between them. sometimes this works to encourage a line break, but sometimes you end up with an overfull box and have to break the line explicitly. depends on how the \title argument is processed; the stretch has to be defined with sufficient flexibility, which depends on the document class. – barbara beeton Aug 09 '13 at 12:53
  • @barbarabeeton Thanks. I like this idea, and it seems to do the trick in my case. Would you care to post it as an answer? – jub0bs Aug 09 '13 at 12:57

2 Answers2

12

if you always want certain phrases "tied together", use the ~ (tilde = tie) instead of a space between them. sometimes this works to encourage a line break, but sometimes you end up with an overfull box and still have to break the line explicitly.

(this is the same approach recommended in the texbook for keeping things like "Dr.~No" and "Figure~1" from breaking apart at the end of a line.)

the actual result depends on how the \title argument is processed; the stretch has to be defined with sufficient flexibility, which depends on the document class.

  • This tilde trick let me line-break a section title properly, in a place where \ would accidentally introduce a page break at the line break! – Camille Goudeseune Oct 21 '15 at 21:46
9

The easiest way is using option pdfusetitle and without spaces around \\ in \title:

\documentclass{book}

\usepackage{kantlipsum}
\usepackage[pdfusetitle]{hyperref}

\author{Me, me, me}
\title{Me,\\myself,\\and I}

\begin{document}
    \maketitle
    \kant
\end{document}

Result of pdfinfo:

Title:          Me, myself, and I
Author:         Me, me, me

Otherwise, there is \texorpdfstring:

\title{My,\texorpdfstring{\\}{ }myself,\texorpdfstring{\\}{ }and I}

Or \pdfstringdefDisableCommands can be used, e.g.:

\pdfstringdefDisableCommands{\def\\#1{ #1}}

That is the definition that is used for the title with option pdfusetitle, it supports the following forms:

\title{Me,\\myself,\\and I}
\title{Me,\\ myself,\\ and I}

If you prefer more spaces:

\pdfstringdefDisableCommands{\let\\\@firstofone}
\title{Me, \\ myself, \\ and I}

The trick with the argument is, that TeX does not accept a space as undelimited argument. Thus the space after \\ is gobbled.

The spaces are one of the reasons, that \\ is not provided by hyperref and a warning is given. The usual tricks (\unskip, \ignorespaces, ...) are not working inside the generation of PDF strings.

Heiko Oberdiek
  • 271,626
  • Such a great answer! Thanks. I guess I can also use \texorpdfstring to include nonbreaking spaces in the title as recommended by Barbara. – jub0bs Aug 09 '13 at 13:33
  • 1
    @Jubobs: ~ is supported by hyperref, no need for \texorpdfstring. – Heiko Oberdiek Aug 09 '13 at 13:34
  • @HeikoOberdiek As a substitute for \ignorespaces inside the generation of PDF strings, you could use \romannumeral-^^ \q. Of couse, that leaves a preceeding space: this could be a problem. Alternatively, one could implement \unskip by adding a step after the full expansion and after stripping braces which would strip all occurrences of <space>\unskip. – Bruno Le Floch Aug 09 '13 at 18:27