1

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}

overflow

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.

overlow2

Mr. Ree
  • 121
  • 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
  • An \mbox is definitely without hyphens, so that won't help. – Steven B. Segletes Oct 27 '16 at 13:39
  • Concerning you custom commands: You might want to have a look at the cleveref package. With that you can automatically insert Table etc. – samcarter_is_at_topanswers.xyz Oct 27 '16 at 14:03
  • 4
    maybe http://tex.stackexchange.com/questions/5959/cool-text-highlighting-in-latex can help. – samcarter_is_at_topanswers.xyz Oct 27 '16 at 14:05
  • Thanks a lot @samcarter . I have tried the most voted answer in that question only to find out that it is based on soul package which fails either. But actually Martin Scharrer's answer (which is overlooked by myself earlier) will solve this question. – Mr. Ree Oct 28 '16 at 03:32

0 Answers0