# losing ligatures when switching font series or color in the middle of a word

For pedagogical purposes, I would like to highlight some characters in a word, through bold or red color or whatever will work.

Because I use handwriting fonts (German Schulschriften wedn and French Cursive frcursive), I am bound to ligatures working well. Unfortunately, ligatures are blocked if you switch series or color in the middle of a word.

I would be happy with a XeLaTeX or LuaLaTeX solution. If needed, also with a PDFLaTeX solution. And as said, any kind of highlighting would be fine.

Here the MWE:

\documentclass[varwidth]{standalone}
\usepackage[ngerman,frenchb]{babel}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\setmonofont[Scale=.75]{Linux Libertine Mono O}
\setsansfont[Scale=MatchLowercase]{Linux Biolinum O}
\usepackage{wedn}
\newcommand{\schschrift}{\selectlanguage{ngerman} \wedn\upshape}
\usepackage{frcursive}
\usepackage{xcolor}

\begin{document}
\newcommand{\fex}[3]{#1\textbf{#2}#3}
% \renewcommand{\fex}[3]{#1{\bfseries #2}#3}

{
\schschrift
\fex{W}{ö}{rter} -- Wörter
}
{
\cursive
\fex{incr}{oy}{able} -- incroyable
}

\renewcommand{\fex}[3]{#1\textcolor{red}{#2}#3}

{
\schschrift
\fex{W}{ö}{rter} -- Wörter
}
{
\cursive
\fex{incr}{oy}{able} -- incroyable
}

\renewcommand{\fex}[3]{#1#2#3}

{
\schschrift
\fex{W}{ö}{rter} -- Wörter
}
{
\cursive
\fex{incr}{oy}{able} -- incroyable
}
\end{document}


You can see that, in the first 2 lines, W and ö don't bind well, as well as the r and o + y and a, when put in bold or in red. Compare to the non-highlighted versions.

You can try the luacolor package. It doesn't insert whatsits and so doesn't split ligatures or disturbs the kerning. But it can not color one part of a ligature in another color than the rest. A ligature is a single glyph and must have a single color. So you can get quite surprising results (the last example is perhaps a bug):

\documentclass{article}
\usepackage[ngerman,french]{babel}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\setmonofont[Scale=.75]{Linux Libertine Mono O}
\setsansfont[Scale=MatchLowercase]{Linux Biolinum O}
\usepackage{wedn}
\newcommand{\schschrift}{\selectlanguage{ngerman} \wedn\upshape}
\usepackage{frcursive}
\usepackage{xcolor}
\usepackage{luacolor}
\begin{document}

fi\textcolor{red}{ff}

f\textcolor{red}{if}f

\newcommand{\fex}[3]{#1\textcolor{red}{#2}#3}

{
\schschrift
W\textcolor{red}{ö}rter -- Wörter
}

{\cursive i\textcolor{red}{croy} in\textcolor{red}{croy}
i\textcolor{red}{nc}roy}
\end{document}


Be careful with your special fonts: T1-encoding is not well suited for lualatex (and at least of the fonts seems to use a non-standard T1-encoding).

• Thanks Ulrike. Unfortunately, I can't accept this answer. The color doesn't disturb ligatures anymore, but ligatures do now disturb color. I'm currently working on a solution where highlighting is achieved through a different background color. – benjamin Mar 12 '19 at 7:44
• Other notes: frcursive has very long ligatures indeed. And LuaLaTeX is not very happy with this font. – benjamin Mar 12 '19 at 9:33

I've come up with a solution using a background color.

Basically, it prints the word twice on top of each other. This is achieved with \rlap for the first print (do not forget the \leavevmode at the very beginning of a paragraph, or use \mbox[0pt]{l}{...} alternatively). This first print prints the first part with \hphantom, then the second part (the highlighted one) in gray on gray. After that, the second print prints the whole word in the normal foreground color on top of it, so that the middle part appears in the foreground color on top of a gray background. Because the presence or absence of ligatures changes the width of the word (or word parts), the alignment between the 2 versions has to be adjusted manually through a bit of kerning.

\documentclass[varwidth,landscape]{standalone}
\usepackage[ngerman,frenchb]{babel}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}

\usepackage{frcursive}
\usepackage{wela,wedn}
\newcommand{\schschrift}{\wedn\upshape\bfseries}

\usepackage{xcolor}

\definecolor{FR}{rgb}{.2717,.4566,.2717}
\definecolor{DE}{rgb}{.4566,.2717,.2717}
\definecolor{fond}{gray}{.8}
\setlength{\fboxsep}{.1pt}

\begin{document}
{
\cursive\color{FR}

\leavevmode\rlap{%
\hphantom{pa}\kern-.3ex%
\colorbox{fond}{\textcolor{fond}{tt}}%
}%
patte

\leavevmode\rlap{%
\hphantom{incr}\kern-.3ex%
\colorbox{fond}{\textcolor{fond}{oy}}%
}%
incroyable
}
}

{
\selectlanguage{ngerman}\schschrift\color{DE}

\leavevmode\rlap{%
\hphantom{W}%
\colorbox{fond}{\textcolor{fond}{ö}}%
}Wörter

\renewcommand{\schschrift}{\wela\upshape\bfseries}\schschrift\color{DE}

\leavevmode\rlap{%
\hphantom{W}\kern-.3ex%
\colorbox{fond}{\textcolor{fond}{ö}}%
}Wörter
}
\end{document}


Screenshot:

One problem remains: When the glyph extands beyond its bounding box, it can look strange, see the <ö> of the first Wörter. But I'm happy for now and will not open a new question on this minor issue.

Edit:

Thanks to this answer, I could replace the background \colorbox and its little issue with a rule of the right dimensions (needs the package calc):

\leavevmode\rlap{%
\hphantom{W}\kern-.3ex%
\textcolor{fond}{\rule[\depthof{ö}*{-1}]{\widthof{ö}}{\totalheightof{ö}}}%
}%
Wörter