3

I'm trying to draw a set of colored balls with tikz, inside some text, but I'm struggling to align the nodes with the text. Here is a minimal working example:

\documentclass{standalone}
\usepackage{tikz,xifthen}

\begin{document}

This is a set of colored balls: { \tikz[baseline]{% \foreach \x/\color in {1/red,2/white,3/blue} { \node[circle,draw,fill=\color,line width=1pt,minimum size=0.25em,anchor=base] at (\x,0) {}; \ifthenelse{\NOT 1 = \x}{\node at ({\x-0.5},0) {,};}{} } } }.

\end{document}

This produces the following output:

tikz-balls

I would like to have the circles vertically centered with the text/math. I have tried various options for the anchor, but perhaps I am missing something really simple. Any pointers would be appreciated.

vikraman
  • 195

3 Answers3

4
\documentclass[border=1cm]{standalone}
\usepackage{tikz, xifthen}
\begin{document}
This is a set of colored balls:
\{, %, inserted to show baseline
\begin{tikzpicture}[baseline]
\foreach \x/\color in {1/red,2/white,3/blue} {
\node[circle, draw, fill=\color, line width=1pt, minimum size=0.25em, anchor=south] at (\x,0) {};
\ifthenelse{\NOT 1 = \x}{\node[anchor=base] at ({\x-0.5},0) {,};}{}
}
\end{tikzpicture}
,\}. %, inserted to show baseline
\end{document}

Three colored circles in a list

  • Let TeX do the talking, like you sugested, would have been better, only the circle should be by tikz and then treat it in maths as a symbol. Then the curlies also can be scaled appropriately in math mode. – yannisl Feb 28 '24 at 12:01
  • 1
    @yannisl: I fully agree. If this is a list, then the horizontal spacing is horrible wrong. It is hard to know OPs use case - why has it been done in a foreach with an if when there is only three!? -clearly there is something else going on. – hpekristiansen Feb 28 '24 at 12:26
1

With help from the comments, I came up with a solution by calculating the right coordinates: using a border of 0.1em and a radius of 0.25em, the center is at (0.25 + 0.1 + 0.1)/2 = 0.225, so I can position the circles at that specific y-coordinate and anchor to base.

\documentclass{standalone}
\usepackage{tikz,xifthen}

\begin{document}

This is a set of colored balls: { \tikz[baseline]{% \foreach \x/\color in {1/red,2/white,3/blue} { \node[circle,draw,fill=\color,line width=0.1em,minimum size=0.25em,anchor=base] at (\x,0.225em) {}; \ifthenelse{\NOT 1 = \x}{\node at ({\x-0.5},0) {,};}{} } } }.

\end{document}

enter image description here

vikraman
  • 195
1

With this post How to vertical center a TikZ node within a text line? and Àpprendre à programmer en tex on ctan p. 259

\begingroup
        \setbox0\hbox{$\vcenter{}$}%
        \xdef\htmath{\the\ht0}%
\endgroup

gives us the vertical offset of the mathematical axis.

With expl3, we prepare a sequence in which we put a tikz command, then we use this sequence separated by ",".

\documentclass{article}
\usepackage{tikz}

%%%%%%%%%%%%%%% \tikzset{% ball/.style args={#1}{% circle, draw, fill=#1, line width=1pt, minimum size=0.25em, node contents={}, } } \ExplSyntaxOn \clist_new:N \l_modulename_color_clist \seq_new:N \l_modulename_balls_seq

\NewDocumentCommand{\myballs}{ O{red,white,blue} } { % in the doc Apprendre à programmer en tex p.259 \begingroup \setbox0\hbox{$\vcenter{}$}% \xdef\htmath{\the\ht0}% \endgroup %The~mathematical~axis:~\htmath\par %%%%%%%%%%%%%%%%% \clist_set:Nn \l_modulename_color_clist {#1} \seq_clear:N \l_modulename_balls_seq \clist_map_inline:Nn \l_modulename_color_clist { \seq_put_right:Nn \l_modulename_balls_seq {\tikz[baseline=-\htmath]\node [ball=##1];} } \seq_use:Nn \l_modulename_balls_seq {,~} } \ExplSyntaxOff \begin{document} This is a set of colored balls: \myballs

\Huge and in with \verb|\Huge|: \myballs[yellow,green,black,purple] \end{document}

enter image description here

pascal974
  • 4,652