12

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

Fritz
  • 6,273
  • 31
  • 55
Alperin
  • 121
  • 1
  • 3

7 Answers7

16

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

JLDiaz
  • 55,732
7

You can use the node's label option and center its position. You only have to adjust the inner and outer seps of the nodes to your needings:

\newcommand*\circled[1]{\tikz{\node[shape=circle,draw,inner sep=8pt, label={center:#1}] (char) {};}}
\circled{$X'$} \circled{$1'$} \circled{$1$} \circled{$2'$} \circled{$2$} 

This gives you:

enter image description here

moospit
  • 4,456
6

For your tikz code, as Harish Kumr says, you need to set a minimum width -- for the characters that you are using 2em seems about right.

If you only want to circle the numbers from 1-10 then a better option is to use the pifont package:

enter image description here

Here is a macro for producing the entries in the first column, for example:

\newcommand\Circnum[1]{%
   \ifcase#1\relax#1\or\ding{172}\or\ding{173}\or\ding{174}
   \or\ding{175}\or\ding{176}\or\ding{177}\or\ding{178}
   \or\ding{179}\or\ding{180}\or\ding{181}
   \or #1\typeout{!! #1 is too big to circle!}
   \fi
}
\Circnum0
\Circnum1
\Circnum2
\Circnum3
\Circnum4
\Circnum5
\Circnum6
\Circnum7
\Circnum8
\Circnum9
\Circnum{10}
5

A PSTricks solutoin:

\documentclass{article}

\usepackage{pstricks}

\def\Circle(#1,#2)#3{
  \pscircle(#1,#2){0.6}
  \rput(#1,#2){#3}}

\begin{document}

\begin{pspicture}(5.7,1.2) % adjust accoring to needs
  \Circle(0.6,0.6){$a+b$}
  \Circle(2.1,0.6){$f(x)$}
  \Circle(3.6,0.6){$\displaystyle\int$}
  \Circle(5.1,0.6){$\nabla$}
\end{pspicture}

\end{document}

output1

With numbers:

\documentclass{article}

\usepackage{pstricks}
\usepackage{multido}

% macro
\def\Circle(#1,#2)#3{
  \pscircle(#1,#2){0.4}
  \rput(#1,#2){#3}}
% parameters
\def\width{6}
\def\height{6}

\begin{document}

\begin{pspicture}(5.8,5.8) % adjust accoring to needs -- number of circles minus 0.2
  \multido{\iA = 1+1, \rA = 0.4+1.0}{\width}{%
    \multido{\iB = \iA+\width, \rB = 0.4+1.0}{\height}{%
      \Circle(\rA,\rB){$\iB$}}}
\end{pspicture}

\end{document}

output2

5

Just use unicode! It works only for numbers, though.

①②⑪⑫

Ingo
  • 20,035
4

Add minimum width=2em to the options as in

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

Further, official tikz way of aligning descenders is to use text depth. Use text depth=0.55ex, and compare with that of JLDiaz.

\documentclass{article}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=0pt,minimum width=2em, text depth=0.55ex] (char) {#1};}
}
\begin{document}
   \circled{$X'$} \circled{$1'$} \circled{$1$} \circled{$2'$} \circled{$2$}

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

enter image description here

4

The simplest code with PSTricks just for fun.

\documentclass[preview,border=12pt,12pt,varwidth]{standalone}
\usepackage{pst-node}
\psset{radius=12pt}
\begin{document}
\begin{enumerate}
\foreach \i in {1,...,20}{\item \Circlenode{node\i}{\i}}
\end{enumerate}
\end{document}

enter image description here