[Tex/LaTex] Basic arithmetic in tikz

tikz-pgf

How can I add two numbers in tikz?
Multiplication seems simple enough…

I want to "stagger" the nodes in the following figure, so they nodes in one layer are inbetween the nodes of the layer below instead of directly on top of them.

\documentclass{article}
\usepackage{tikz}
\begin{document}


\def\layersep{1.5cm}
\def\unitsep{1.5cm}

\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
    \tikzstyle{neuron}=[circle,draw,fill=white!25,minimum size=25pt,inner sep=0pt]
    \foreach \x in {1,...,3}
    \path   node[neuron] (L_2_\x) at (\x*\unitsep,2*\layersep){};
    \foreach \x in {1,...,2}
    \path   node[neuron] (L_3_\x) at (\x*\unitsep,3*\layersep){};
    \foreach \x in {1,...,1}
    \path   node[neuron] (L_4_\x) at (\x*\unitsep,4*\layersep){};
\end{tikzpicture}
\end{document}

I tried these, but they don't work:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}


\def\layersep{1.5cm}
\def\unitsep{1.5cm}

\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
    \tikzstyle{neuron}=[circle,draw,fill=white!25,minimum size=25pt,inner sep=0pt]
    \foreach \x in {1,...,3}
    \path   node[neuron] (L_2_\x) at (\x*\unitsep,2*\layersep){};
    \foreach \x in {1,...,2}
    \path   node[neuron] (L_3_\x) at ({.5+\x}*\unitsep,3*\layersep){};
    \foreach \x in {1,...,1}
    \path   node[neuron] (L_4_\x) at ($1+\x$*\unitsep,4*\layersep){};
\end{tikzpicture}
\end{document}

Best Answer

Like this?

layered neurons

Note that \tikzstyle is deprecated and ought not be used.

\documentclass[border=5pt, multi, tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\def\layersep{1.5cm}
\def\unitsep{1.5cm}

\begin{tikzpicture}
  [
    shorten >=1pt,->,
    draw=black!50,
    node distance=\layersep,
    neuron/.style={circle,draw,fill=white!25,minimum size=25pt,inner sep=0pt}
  ]
  \foreach \x in {1,...,3}
  \path   node[neuron] (L_2_\x) at (\x*\unitsep,2*\layersep){};
  \foreach \x in {1,...,2}
  \path   node[neuron] (L_3_\x) at ({(.5+\x)*\unitsep},3*\layersep){};
  \foreach \x in {1,...,1}
  \path   node[neuron] (L_4_\x) at ({(1+\x)*\unitsep},4*\layersep){};
\end{tikzpicture}

\end{document}

In fact calc isn't needed here. calc is needed when you want to do arithmetic with coordinates e.g. 'add' point (a) to point (b). If you are doing arithmetic within coordinates but with numbers, it plays no role. And you only want dollar signs when either using calc to do arithmetic with coordinates or typesetting maths, as opposed to doing it. So the dollars here are causing trouble and had to go.

Then remember that () represents a sub-calculation, if you like. At least, maybe that's not the official way to explain it, but it is how I think of it. So if we want to add x to y and multiply by z, then we want to do the addition first x+y and to group it, use (x+y) and multiply by z, (x+y)*z. But, in a coordinate specification, this will confuse TikZ as it doesn't expect a ( here. So we need to protect the calculation {(x+y)*z} within the coordinate specification.

Note that my terminology here doubtless leaves much to be desired - I am not using the technically correct terms. (I said 'protect' but not in the \protect sense....) But maybe it helps a bit to think of how things get nested here.