3

I'm trying to align three labeled circles horizontally. Using absolute positions works although I would want to have a margin between the circles and its labels. However I think it would be nice to be able to position it relative to each other since the label lengths are variable.

I'm currently using the positioning library of tikz but apparently it can't position relative to a filldraw command or I'm just using it incorrectly. It just places the circles on top of each other. I really don't have much experience using tikz, so any help is much appreciated!

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

% absolute
\begin{tikzpicture}[scale=0.50]
        \filldraw[fill=white!40!red,thin] (0,0) circle (12pt) 
        node[anchor=west] {= executing};
        \filldraw[fill=white!40!yellow,thin] (8,0) circle (12pt)
        node [anchor=west]{=ready};
        \filldraw[fill=white!40!green,thin] (14,0) circle (12pt)
        node[anchor=west]{=stalling};
\end{tikzpicture}

% relative
\begin{tikzpicture}[scale=0.50]
        \filldraw[fill=white!40!red,thin] (0,0) circle (12pt);
        \node[right = 6pt] {= executing};
        \filldraw[fill=white!40!yellow,thin, right = 24pt] circle (12pt);
        \node [right = 6pt]{=ready};
        \filldraw[fill=white!40!green,thin, right = 24pt] circle (12pt);
        \node[right = 6pt]{=stalling};
\end{tikzpicture}

\end{document}

2 Answers 2

5

You are on the right track (+1)! I suggest the following changes of your solution:

  • define common style for all circles with option for fill color
  • write text (explanation) for circles as labels
  • give name to labels
  • positioning of nodes in respect to labels
\documentclass[border=3.141592mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}[
  node distance = 1em,
dot/.style args = {#1/#2}{circle, fill=#1!70, minimum size=12pt,
                          label={[name=@dot]right:{= #2}}, node contents={}}
                        ]
\node   [dot=red/executing];
\node   [dot=yellow/ready, right=of @dot];
\node   [dot=green/stalling, right=of @dot];
    \end{tikzpicture}
\end{document}

enter image description here

or with small variation of picture's styles:

    \begin{tikzpicture}[
  node distance = 1em,
dot/.style args = {#1/#2}{circle, fill=#1!70, minimum size=12pt, 
                          label={[name=@dot]right:{: #2}}, node contents={}},
every label/.append style = {inner xsep=1pt}
                    ]

enter image description here

2
  • Looks much cleaner, thank you very much!
    – SimonH
    Jan 19 at 14:25
  • @LPrc, well, the code is a bit shorter then yours ;-),
    – Zarko
    Jan 19 at 14:27
1

Ok so I figured out circles can also be drawn using \node command and supplying circle as parameter. Doing it like so works as intended. If there is a better or cleaner solution, let me know.

\begin{tikzpicture}
        \node[circle,fill=white!40!red,thin,minimum size=12pt] (circ1) (0,0) {}; 
        \node[right = 4pt] (txt1) {= executing};
        \node[circle,fill=white!40!yellow,thin,minimum size=12pt,right = 4pt of txt1] (circ2) {}; 
        \node[right = 4pt of circ2] (txt2) {= ready};
        \node[circle,fill=white!40!green,thin,minimum size=12pt,right = 4pt of txt2] (circ3) {}; 
        \node[right = 4pt of circ3] {= stalling};
\end{tikzpicture}

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.