[Tex/LaTex] How to draw circled integers with the same size by TikZ

tikz-pgf

I used the statement

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

to draw some circled integers.

Ex.

\circled{$X'$} \circled{$1'$} \circled{$1$} \circled{$2'$} \circled{$2$} 

But these are not same size.

How can I fix it?

output

Best Answer

There are subtle points relating to the basline aligment.

In the answer by moospit, no baseline is specified for the circled nodes, so they will be using the bottom of the circle as alignment. This produces bad results when used with other characters in the same line:

\renewcommand*\circled[1]{\tikz{\node[shape=circle,draw,minimum size=2.2em, label={center:#1}] (char) {};}}

\leavevmode\rlap{\hbox to 8cm{\hrulefill}} % This is to draw the baseline of the line
\circled{$X'$} \circled{$1'$} \circled{$1$} \circled{$2'$} \circled{$2$} \circled{p} p

Bad result

If we add a baseline key to the \tikz command it still doesn't work right, because the nodes have no real content because it is typeset via label, and thus the baseline of each node is through its center:

\renewcommand*\circled[1]{\tikz[baseline=(char.base)]{\node[shape=circle,draw,minimum size=2.2em, label={center:#1}] (char) {};}}

\leavevmode\rlap{\hbox to 8cm{\hrulefill}}
\circled{$X'$} \circled{$1'$} \circled{$1$} \circled{$2'$} \circled{$2$} \circled{p} p

Still bad result

In a comment, I proposed to simply add a minimum size key to the node. This is basically what Harish Kumar does in its answer. This approach correctly aligns the baseline of the text insde each node with the baseline of the line. However, for the node containing only "p", the circle around it is not properly placed:

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[anchor=text, shape=circle,draw, inner sep=0pt, minimum size=2.2em] (char) {#1};}}

\leavevmode\rlap{\hbox to 8cm{\hrulefill}}
\circled{$X'$} \circled{$1'$} \circled{$1$} \circled{$2'$} \circled{$2$} \circled{p} p

Almost!

This is because that node has descenders, but not ascenders. This can be fixed if each node includes a \strut which is an invisible line of the appropiate height. The \strut can be included as part of the \circled command:

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[anchor=text, shape=circle,draw, inner sep=0pt, minimum size=2.2em] (char) {#1\strut};}}

\leavevmode\rlap{\hbox to 8cm{\hrulefill}}
\circled{$X'$} \circled{$1'$} \circled{$1$} \circled{$2'$} \circled{$2$} \circled{p} p

Result