1

I'm trying to define new counter lnum and display the counter number in circle.

I found circled command from here tex.stackexchange but I have a problem.

Here's my test code:

\documentclass{article}
\usepackage{tikz}
\newcommand*{\circled}[1]{\tikz[baseline=(char.base)]{%
            \node[shape=circle,fill=blue!20,draw,inner sep=2pt] (char) {#1};}}
\newcounter{lnum}
\renewcommand{\thelnum}{\circled{\arabic{lnum}}}

\begin{document}

\thelnum % fine
\refstepcounter{lnum}\thelnum % error here

\end{document}

As you can see from the comment, without \refstepcounter, it works fine. However, with \refstepcounter, it doesn't work. Here's error message:

! Undefined control sequence.
\tikz@deactivatthings ->\def ;
                              {\tikz@nonactivesemicolon }\def :{\tikz@nonact...
l.11 \refstepcounter{lnum}
                          \thelnum
?

Why this doesn't work? And, how can I fix it?

  • Do not \renewcommand{\thelnum}, it is self-recursive. – Symbol 1 Jun 21 '17 at 14:01
  • In \refstepcounter the \thelnum is expanded into a label definition. That is, it must be possible to write \edef\test{\thelnum} and that will not work as a Tikz node. – StefanH Jun 21 '17 at 14:49
  • @Symbol1 I would say you can redefine \thelnum as long as it is expandable inside TeX. For example \renewcommand\thelnum{LNUM: \arabic{lnum}} should work. I do not see how it is self-recursive. – StefanH Jun 21 '17 at 14:58
  • @StefanH: Sorry I am not precise, or even misleading --- Anyway: The fact that you have to REnewcommand \thelnum suggests that this command is already defined and can be used anywhere, anytime. Just because \arabic{lnum} works well does not mean that you are safe. In fact, \refstepcounter does invoke \thelnum and that is where everything goes wrong. – Symbol 1 Jun 21 '17 at 15:24
  • @Symbol1 That I agree with completely – StefanH Jun 21 '17 at 16:14
1

The problem is that the command \thelnum is expanded inside \refstepcounter{lnum}. It is essentally

\edef\@currentlabel{\p@lnum\thelnum}

where \p@lnum is a prefix. If \thelnum is a Tikz node that will not be able to expand in this assignment. By testing the definition

\edef\test{\tikz \node[draw]{Test};}

one gets a similar error as in the question.

To get the printed numbers circled there can be another command writing it:

\documentclass{article}
\usepackage{tikz}
\newcommand*{\circled}[1]{\tikz[baseline=(char.base)]
  \node[shape=circle,fill=blue!20,draw,inner sep=2pt] (char) {#1};
}
\newcounter{lnum}
\newcommand{\mylnum}{\circled{\arabic{lnum}}}
\begin{document}
\mylnum % fine
\refstepcounter{lnum}
\mylnum % error here
\end{document}

Of course cross-references to the value will not be printed in circles, but that can be done by hand:

\refstepcounter{lnum}
\label{circ:test}
[...]
\circled{\ref{circ:test}}
| improve this answer | |
  • You are right. I simply forgot what \refstepcounter does. – Symbol 1 Jun 21 '17 at 15:37
2

Here's a ready-made solution from the package pifont

\begin{dingautolist}{"0AC}
   \item First
   \item Second
\end{dingautolist}

Using the starting value "0B6 you can get white digits on black circles. The digits range from 1 to 10, no higher number are available in the Zapf Dingbats font.

| improve this answer | |
1

\refstepcounter stores the expansion of \thelnum in \@currentlabel for references. So it is not a good idea if \thelnum does complicated things. Either never use \refstepcounter with your counter (use \stepcounter instead). Or define some other command that adds the circle:

\documentclass{article}
\usepackage{tikz}
\newcommand*{\circled}[1]{\tikz[baseline=(char.base)]{%
            \node[shape=circle,fill=blue!20,draw,inner sep=2pt] (char) {#1};}}
\newcounter{lnum}
\newcommand{\thecirclnum}{\circled{\arabic{lnum}}}

\begin{document}

\thecirclnum 

\refstepcounter{lnum}\label{test}

\thecirclnum, \ref{test}

\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.