# Incompatibility between soul/tikz and accents package

The following code throws an error.

\documentclass{amsart}
\usepackage{accents}
\usepackage{tikz}
\usepackage{soul}
\def\ubar#1{\underaccent{\bar}{#1}}
\begin{document}
$\ubar{x}$
\hl{$\bar{x}$}
\hl{$\ubar{x}$}
\end{document}


If the line \hl{$\bar{x}$} is commented out, it runs fine, isolating the problem to be with the interaction between the \hl command and the \ubar command. Is there an easy workaround for this?

• Sorry, what does tikz do with that? It does not run through regardless of whether or not one loads tikz. – user194703 Sep 29 '19 at 17:32
• If you add tikz to marijn's MWE, you'll see that the ubar{x} command is highlighted instead of being underlined. – Leo Simon Sep 30 '19 at 2:45

As a variant of §22 of the soul documentation, you can put the content in a savebox before highlighting/underlining it. This means that linebreaks will not work but otherwise it works fine.

MWE, showing a manual savebox and a new command \hlu that performs the box operations:

\documentclass{amsart}
\usepackage{accents}
\usepackage{soul}
\newsavebox{\ubarbox}

\def\ubar#1{\underaccent{\bar}{#1}}
\def\hlu#1{\savebox{\ubarbox}{#1}\hl{{\usebox\ubarbox}}}

\begin{document}
\savebox{\ubarbox}{$\ubar{x}$}
\noindent ubar: $\ubar{x}$\\
hl bar: \hl{$\bar{x}$}\\
hl usebox: \hl{abc {\usebox\ubarbox}}\\
hlu: \hlu{$\ubar{x}$}\\
\end{document}


Result:

• This is great, thank you. I would ideally like to define \ubar in advance so that I can use the macro ubar both with and without the hl command. I tried this: \def\ubar#1{\savebox{\ubarbox}{$\underaccent{\bar}{#1}$}} but it doesn't work, i.e., both \ubar{x} and \hl{\ubar{x}} return blanks. Could you suggest how to do this. please? Thanks for all your help – Leo Simon Sep 30 '19 at 3:09
• @LeoSimon the problem is that both \underaccent and \hl do all kinds of redefinitions and substitutions when executing the command. This causes some interference which leads to the error that you encountered. A solution is to 'shield' the commands from eachother by using the savebox. This works by keeping the \underaccent command outside of the argument of the \hl, in my definition of \hlu the accent is in #1 and processing that is finished and stored in the box before \hl starts. With \hl{\ubar{x}} as you suggest the accent is back in the argument of \hl [...] – Marijn Sep 30 '19 at 7:02
• [...] so the original problem returns, i.e., that is not possible (or at least I did not find a way to do that so far). The \hlu command however works the same as \hl (only without line breaking), so whenever you want to highlight something that contains an underbar somewhere you can use \hlu and in other cases you can use \hl. Or if you do want line breaking for a line that contains an underbar you can put the underbar part in a savebox and use \hl as normal, as shown in my 'hl usebox' example. – Marijn Sep 30 '19 at 7:07
• Thanks so much for the response, I appreciate the time you spent! – Leo Simon Oct 1 '19 at 13:34