This is a followup of another question that seems to find no easy answer. My goal is to achieve highlighting a sub-part of a word without breaking the ligatures and without the ligatures breaking the highlighting. 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. This first print prints the first part in white on white, then the second part (the highlighted one) in gray on gray, then the third part again in white on white. 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 (parts), the alignment between the 2 versions has to be adjusted manually through a bit of kerning.
While using this in a table is fine (which is my goal anyway), there is an issue when it is called at the very beginning of a paragraph. There, \rlap seems to add a linebreak. This linebreak disappears if a command appears before, like \noindent or \rule{0pt}{0pt}. Has anyone an explanation? and a way to avoid the workaround with putting \rule{0pt}{0pt} in the macro?
\documentclass[varwidth,landscape]{standalone}
\usepackage[ngerman,frenchb]{babel}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\usepackage{frcursive}
\usepackage{xcolor}
\definecolor{FR}{rgb}{.2717,.4566,.2717}
\definecolor{fond}{gray}{.8}
\setlength{\fboxsep}{.1pt}
\newcommand{\ancfex}[4][]{#2#3#4}\let\fex\ancfex
\newcommand{\fexI}[4][0pt]{%
\rlap{\textcolor{white}{#2}\kern#1\colorbox{fond}{\textcolor{fond}{#3}}\textcolor{white}{#4}}%
#2#3#4%
}
\newcommand{\fexII}[1]{\let\fex\fexI#1\let\fex\ancfex}
\newcommand{\mf}[1]{{\color{FR}\bfseries\textcursive{#1}}}
\begin{document}
\newcommand{\csvcoli}{\fex[-.3ex]{pa}{tt}{e}, \fex[-.2ex]{}{p}{atte}, \fex{pat}{te}{}}
% in the real example, the material to highlight is collected through csvsimple,
% hence the name \csvcoli
It's all fine in a table:
\noindent
\begin{tabular}{|l|l|l|}\hline
\csvcoli & \mf{\csvcoli} & \mf{\fexII{\csvcoli}}\\\hline
\end{tabular}
It's strange at the very beginning of a paragraph:
\mf{\fexII{\csvcoli}}
\mf{\fexII{\csvcoli}} -- \mf{\fexII{\csvcoli}}
\verb|\noindent| and \verb|\rule{0pt}{0pt}| prevent this behavior:
\noindent
\mf{\fexII{\csvcoli}}
\rule{0pt}{0pt}\mf{\fexII{\csvcoli}}
\renewcommand{\fexI}[4][0pt]{%
\rule{0pt}{0pt}%
\rlap{\textcolor{white}{#2}\kern#1\colorbox{fond}{\textcolor{fond}{#3}}\textcolor{white}{#4}}%
#2#3#4%
}
\mf{\fexII{\csvcoli}}
\mf{\fexII{\csvcoli}} -- \mf{\fexII{\csvcoli}}
\end{document}
screenshot:

\rlapdoesn't start a paragraph. Use\makebox[0pt][l]{...}instead or (as you do that inside a definition) add\leavevmodein front of\rlap. – egreg Mar 12 '19 at 09:52\hphantomas well as other optimisations in my answer to my other question. – benjamin Mar 12 '19 at 12:45