2

I'm trying to learn Tikz and I'm running into some problems with the filldraw methods, circle and text. I want to have a ellipse in the background, with a circle on top, with a text inside the circle.

I can have the circle + text, or ellipse + circle but with the text hidden somehow. The code below is as far as I got, and it seems to be invalid, but I can't find why..

Any help is most welcomed !

Thank you

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgffor}

\begin{document}

\begin{tikzpicture}

\draw (0,0) -- (0,0) node {Lvl1};

\newcount\myX
\foreach \id [count=\x from 0] in {{00},{01},{10},{11}}
    \myX = \numexpr(3 + \x * 2)\relax;
    % the ellipse code
    \filldraw[fill=gray!20!white, thick] (\myX,0) circle [x radius=0.9, y radius=0.7];
    % the circle code
    \draw [thick,black] (\myX,0) circle [radius=0.5] node {\id};

\end{tikzpicture}

\end{document}
2

If the number of commands in \foreach loop is greater than 1, you need a pair of { and } (like in C/C++ and many other programming languages). In your original code, only

\myX = \numexpr(3 + \x * 2)\relax;

is recognized as inside the loop, hence the compilation error.

Adding { and }, you will have what you want:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgffor}

\begin{document}

\begin{tikzpicture}

\draw (0,0) -- (0,0) node {Lvl1};

\newcount\myX
\foreach \id [count=\x from 0] in {00,01,10,11} {
    \myX = \numexpr(3 + \x * 2)\relax;
    % the ellipse code
    \filldraw[fill=gray!20!white, thick] (\myX,0) circle [x radius=0.9, y radius=0.7];
    % the circle code
    \draw [thick,black] (\myX,0) circle [radius=0.5] node {\id};
}
\end{tikzpicture}

\end{document}

enter image description here

  • Lol. Thanks for spotting this, I need to spend more time learning the turing complete language that is Latex... – Nikkolasg Apr 3 at 18:55

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.