# Problem with a counter inside a \ul command

The following MWE gives a compilation error. If i move the \arabic{count} outside from the \ul command, it works

\documentclass{article}
\usepackage{soul}
\begin{document}

\newcounter{count}%
\setul{0.5ex}{0.3ex}%
\ul{text \arabic{count}}

\end{document}

-
Use \thecount? :) But, above all, avoid underlining. –  egreg Jan 25 '12 at 9:40

It works if you define a macro for \arabic{count} and use that:

\documentclass{article}
\usepackage{soul}
\newcommand{\arabcount}{\arabic{count}}
\begin{document}
\newcounter{count}%
\setul{0.5ex}{0.3ex}%
\ul{text \arabcount}
\end{document}


Often it helps, to register a command for soul by

\soulregister{command name}{number of arguments}


so it won't be analyzed by soul but executed. But in this case for \arabic it still gives an error, so I suggest the macro solution.

-
I consider the first variant with \arabcount better anyways. –  tohecz Jan 25 '12 at 9:57

Slightly different from the solution by Stefan, also using a macro and possibly saving a bit of typing. We define a macro \ull to replace \ul

\documentclass{article}
\usepackage{soul}
\begin{document}

\newcounter{count}%
\setul{0.5ex}{0.3ex}%
\def\ull#1{\def\acount{\arabic{count}} \ul{#1 \acount}}
\ull{text}
\end{document}


If by any chance you want to increment the counter within the macro use as follows:

\documentclass{article}
\usepackage{soul}
\begin{document}
\newcounter{count}%
\setcounter{count}{0}
\setul{0.5ex}{0.3ex}%
\def\ull#1{\def\acount{\arabic{count}} \ul{#1 \acount}\stepcounter{count}}
\ull{text}
\ull{text}
\end{document}

-

One can also simply put extra {} around \arabic{count} :

\ul{text {\arabic{count}}}

-

One can use commands inside \ul as long as they expand completely to characters; so

\ul{Some text \thecount}


works. It's really not possible to use other commands, apart font switching and accent making ones; a list of preregistered commands of that kind is already included in soul, but others can be registered

\soulregister{\myfontswitch}{1}


if, for example, you have

\newcommand{\myfontswitch}[1]{\textbf{\scshape\small #1}}


(just to give a silly example). But, for example,

\newcommand{\mytext}{\mbox{xyz}}


will give Reconstruction failed without \soulregister{\mytext}{0} and no underline with it.

-