[Tex/LaTex] Getting numbers to appear in circles

circlestikz-pgf

I have a linked question that I previously found on here, but I cannot make any comment yet to post. The link being: Good way to make \textcircled numbers?

My question was how could we use this command more then once in our TeX file in different parts of our file without generating an error.

Example Code:

\documentclass{article}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\begin{document}
Numbers aligned with the text:  \circled{1} \circled{2} \circled{3} end.
\end{document}

The error message is this:

! LaTeX Error: Command \circled already defined.
               Or name \end... illegal, see p.192 of the manual.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...

1.222 ...circle,draw,inner sep=2pt] (char) {#1};}}

When removing the \newcommand* it shows this next error message:

! You can't use 'macro parameter character #' in restricted horizontal mode.
<argument> ...ircle,draw,inner sep=2pt] (char {##
                                                 1}

Can someone help out please.
Thank You so much.

Best Answer

As is mentioned in the comments, your example code compiles fine, but you mustn't use the \newcommand*\circled more than once. If you want more circled numbers, just use \circled{n} for each instance of a circled n.

My guess is that you did the following: You tried some code like

\documentclass{article}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\begin{document}
Numbers aligned with the text:  \circled{1} \circled{2} \circled{3} end.
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
Numbers aligned with the text:  \circled{1} \circled{2} \circled{3} end.
\end{document}

and got ! LaTeX Error: Command \circled already defined. This is expected behaviour; you should define \circled only once in the preamble. Then you tried removing the second \newcommand*, but you only removed that. Then indeed you get the error message

! You can't use `macro parameter character #' in restricted horizontal mode.

What you have to do is remove the full two lines of the definition, i.e., the lines

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
Related Question