1

I'm trying to write a class where the arguments of the \title{} and \author{} commands, used for the title page, automatically call \hypersetup{} to set the title and the author in the PDF metadata.

So far I use this piece of code (the \apptocmd command is from etoolbox)

\apptocmd{\title}{
  \hypersetup{
    pdftitle={#1}
  }
}{}{}

It works well, but often it happens that the user wants to insert a newline in the title to tune its appearance:

\title{My very long long \\ title}

While everything still works, I get a warning about the newline being an unsupported token for pdf strings. The user is then forced to do:

\title{My very long long \texorpdfstring{\\}{} title}

to have the warning silenced.

So the question is: can I automatically remove (or better, replace) tokens that are not allowed in PDF strings according to hyperref, and to pass the filtered string to \hypersetup to avoid the warning?

If a generic solution is not possible or too difficult, I'd want at the very least to just remove newlines or to replace them with spaces (I've tried to simply redefine the \\ command in a group before calling \hypersetup but I still get the warning).

2

1 Answer 1

3

It can be done with hyperref's \pdfstringdefDisableCommands:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{hyperref,xpatch}

\xapptocmd{\title}{%
  \hypersetup{ pdftitle={#1} }%
}{}{}

\pdfstringdefDisableCommands{%
  \def\\{}%
}

\title{My very long long \\ title}
\author{Me}
\date{\today}

\begin{document}

\maketitle

\end{document}

The example does not remove the extra space in the pdftitle, though…

6
  • It seems that LaTeX still takes care of the extra spaces.
    – Klaidonis
    Commented Feb 25, 2021 at 18:07
  • @Klaidonis you could input it surrounded by no spaces \title{My very long long\\title} and disable it with \def\\{ }
    – cgnieder
    Commented Feb 25, 2021 at 19:30
  • That's exactly what I do, but even with additional spaces, the output is a single space nevertheless. Perhaps, by converting it to a "literal string," the result would be different.
    – Klaidonis
    Commented Feb 25, 2021 at 21:44
  • @Klaidonis Why would you want more than one space after another?
    – cgnieder
    Commented Feb 25, 2021 at 22:26
  • Sorry for the confusion. I do not want the additional spaces, but if, in this case, the title was defined with one or more spaces around `\`, then we still get the single space. I was trying to add this as a remark to your last line in the answer.
    – Klaidonis
    Commented Feb 25, 2021 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .