How to display highlighted text generated by user command properly? [duplicate]

I want to highlight some things to do or alternative words/phrases for future refinement in a draft.

I have tried to highlight text with \hl in soul package or \colorbox in xcolor package. But \colorbox is not able to detect line breaks automatically, while \hl always results in compilation errors when there are user commands.

I have tried to add curly braces around the user command, but it didn't work.

I have tried to use \mbox to wrap the command, but the result is the same as \colorbox.

\documentclass{article}

\usepackage{color}
\usepackage{soul}

\newcommand{\reftab}[1]{\mbox{Table \ref{#1}}}
\newcommand{\reftabb}[1]{Table \ref{#1}}

\soulregister{\reftab}{1}
\soulregister{\reftabb}{1}

\begin{document}

\begin{table}
\caption{tab \label{tab1}}
\end{table}

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa aaaaaaa \hl{{\reftab{tab1}}}

\noindent\makebox[\linewidth]{\rule{\textwidth}{1pt}}

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa aaaaaaa \hl{\mbox{\reftabb{tab1}}}

\end{document}


The picture above shows that the highlighted text is out of margin (the width of the line is text width), is it possible to make it wrap properly?

Even with microtype package, the output is still not desirable.

• Line breaking looks for hyphen points to assist it. There are few in your example. You can force valid linebreaking with \sloppy or the sloppypar environment, but it comes at the expense of excessive inter-word spacing. The overfull box is a warning, perhaps, to restructure your document. – Steven B. Segletes Oct 27 '16 at 11:10
• I have tried to use microtype package which works well in normal cases, but it will fail here if the highlighted text is long. @StevenB.Segletes – Mr. Ree Oct 27 '16 at 13:04
• I would suspect It is because the act of highlighting breaks the hyphen-ability of the content. In that case, your available hyphen points become greatly diminished. – Steven B. Segletes Oct 27 '16 at 13:14
• I added another example with enough available hyphen points. It works fine without the \mbox when there is no user command. @StevenB.Segletes – Mr. Ree Oct 27 '16 at 13:27
• maybe tex.stackexchange.com/questions/5959/… can help. – user36296 Oct 27 '16 at 14:05