[Tex/LaTex] Substitute italic ampersand with alternative ampersand in Linux Libertine O

ampersandfontspechyperreflibertinexetex

Based in this answer I want to create a command \amper that uses the alternative ampersand of Linux Libertine O and leaves the regular \& alone. The problem with the MWE below is that hyperref does not like the unicode substitution and consequently screws the title in the pdf-info up. I guess the best solution would be to substitute the regular italic ampersand of Linux Libertine with the alternative one, but I don't know how to do that.

Edit: I should add that the \amper command as defined here works perfectly with hyperref with fonts that have an alternative italic ampersand by default, such as Adobe Garamond Pro, Sabon etc.

\documentclass{article}

\usepackage{libertineotf}
\usepackage{xspace}

\usepackage{relsize}
\newcommand*\scname{sc}

\makeatletter
\DeclareRobustCommand{\amper}{%
\ifx\f@shape\scname
{\smaller[1.2]\char"E050}%
\else
\char"E050
\fi
\xspace}
\makeatother

\newcommand{\myTitle}{Italic Ampersand \amper Linux Libertine O\xspace}

\usepackage{hyperref}
\hypersetup{unicode=true,pdftitle={\myTitle}}

\begin{document}

\title{\myTitle}

\maketitle

Text \amper test.

\end{document}

Best Answer

The solution is to use \texorpdfstring:

\makeatletter
\DeclareRobustCommand{\amper}{%
  \texorpdfstring{%
    \begingroup
    \ifx\f@shape\scname
      \smaller[1.2]%
    \fi
    \char"E050
    \endgroup
    }{\&}\xspace
}
\makeatother

In this way hyperref will be able to use the normal ampersand for PDF strings.

Notice some small optimization to your macro: the test can be shorter, which is always better. Using \begingroup and \endgroup is a matter of taste, but doing is usually safer.

(Your comment about using \xspace at the end was of course correct)

There is a limitation, however: you can't use \amper in section titles, unless you change the setup not to use boldface, as the variant ampersand doesn't exist in the bold variant (at least on my machine).

Related Question