[Tex/LaTex] Package soul Error: Reconstruction failed

colorsoul

I already read the discussions about this error, but nothing helpfull. My MWE

\documentclass{minimal}
\usepackage{xcolor}
\usepackage{soul}
%\soulregister{\cl}{7} irrelevant
%\soulregister{\sethlcolor}{7}
\definecolor{inlineBG}{HTML}{F1F1F1}
\newcommand{\cl}[2]{\textcolor[HTML]{#1}{#2}}

\newcommand{\codeinline}[1]{%
  \sethlcolor{inlineBG}{%
  \texttt{\hl{#1}}}%
}

\begin{document}

% ok :
\codeinline{ {\cl{FF0000}{Hello}} }

% Package soul Error: Reconstruction failed.
\codeinline{ {\cl{FF0000}{Hellooooooooooooooooooooooooooooooooooooooooo}} }

\codeinline{ {\cl{FF0000}{Hello World}} }

\end{document}

the first \codeinline works fine, not the others. How can i fix it ?

Best Answer

In a similar situation, I found the help text provided by the soul package quite useful. It suggests to wrap the text inside an mbox. Indeed, if you change your definition to

\newcommand{\codelinline}[1]{%
  \sethlcolor{inlineBG}{%
  \texttt{\hl{\mbox{#1}}}}%
}

your MWE works for me. There might be issues with hyphenation if the text inside \codeline is long. (Seems to be a limitation of soul generally.)