2

How can I use arithmetic inside a node's curly brackets? I have this, and it prints for example "0+1" in a circle. I would like to have circles that show the sum of their coordinates.

\begin{tikzpicture}

\foreach \x in {0,1,2,3}
\foreach \y in {0,1,2,3}
\draw [] (\x,\y) circle [radius=0.4] node {\x + \y};

\end{tikzpicture}
  • 1
    Chapter VIII of the Tikz manual is devoted to the mathematical engine... You should find there all you need. Basically, look for pgfmathparse, pgfmathresult and company. – Rmano Jun 10 '18 at 21:08
3

In this simple case where only integers are involved you can do the arithmetic using \numexpr.

\documentclass[tikz]{standalone}
\usepackage{pgffor}

\begin{document}

\begin{tikzpicture}

\foreach \x in {0,1,2,3}
\foreach \y in {0,1,2,3}
\draw [] (\x,\y) circle [radius=0.4] node {\the\numexpr\x + \y\relax};

\end{tikzpicture}
\end{document}

which gives

enter image description here

But there must be some TikZ way.

Note: \numexpr is available since 2004 by default with pdflatex with TeXLive distribution. But there is not much of user level documentation I think.

|improve this answer|||||

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.