Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
hyperref is often picky about encoding indeed. I've had such problems, too, where I had to encode characters manually for chapter/section titles so they wouldn't get screwed in the PDF bookmarks... –  ℝaphink Jun 1 '12 at 17:27

2 Answers 2

up vote 5 down vote accepted

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).

share|improve this answer
    
Thanks, that works but requires another \xspace after \&, i.e. {\&\xspace}, otherwise the whitespace in the pdf-title is missing. Could you please make a little comment with regards to what you optimized? –  Jörg Jun 1 '12 at 20:42

It is generally a bad idea to access alternate glyphs using private use area code points, instead you should use the appropriate OpenType features, in this case the alternate ampersand can be activated with Alternate=1 font option, now using a simplified \amper definition:

\newfontfamily\amperfont[Alternate=1]{Linux Libertine O}    
\makeatletter
\DeclareRobustCommand{\amper}{{\amperfont\ifx\f@shape\scname\smaller[1.2]\fi\&}\xspace}
\makeatother

Which, as a side effect, will solve your hyperref issue.

share|improve this answer
    
I'm afraid that does not work at all. I just get the normal ampersand in my MWE –  Jörg Jun 2 '12 at 8:25
    
When using fontspec and loading the font directly (not via the libertineotf package) I get the appropriate warning: "icu-feature-not-exist-in-font" * OpenType feature 'Alternate=1' (+salt=2) not available for font. –  Jörg Jun 2 '12 at 8:29
    
Okay, the solution is to use Style=Alternate instead of Alternate=1. The fontspec manual (p. 29, example 30) states to use Alternate=0 with Linux Libertine, but for some reason that gives the normal ampersand as well. –  Jörg Jun 2 '12 at 8:35
    
Your version of Libertine fonts might be different than mine. –  Khaled Hosny Jun 2 '12 at 23:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.