Problem with a counter inside a \ul command

6,894

Solution 1

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}

arabic counter underlined with soul

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.

Solution 2

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

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

Solution 3

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}

Solution 4

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.

Share:
6,894

Related videos on Youtube

Loic Rosnay
Author by

Loic Rosnay

Updated on January 26, 2020

Comments

  • Loic Rosnay
    Loic Rosnay almost 4 years

    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}
    
    • egreg
      egreg almost 12 years
      Use \thecount? :) But, above all, avoid underlining.
  • yo'
    yo' almost 12 years
    I consider the first variant with \arabcount better anyways.