\numexpr behavior in math mode and/or TikZ

You could use \edef\n{\the\numexpr\s-\m}, or just use it in the subscript:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=1.5]
  \foreach \s in {2,...,4}{
    \foreach \m in {1,...,\numexpr\s-1}{
    \draw (\s-\m,-\m) node {$a_{\m\the\numexpr\s-\m}$};
  }
}
\end{tikzpicture}

\end{document}

enter image description here


There is already one answer how to use \numexpr, one how to rewrite your loop to avoid it, and here is another possibility : you can use the tools provided by foreach to make calculations. You can use count and evaluate:

\documentclass[tikz,border=7pt]{standalone}
\begin{document}
  \begin{tikzpicture}[scale=1.5]
      \foreach[count=\t from 1] \s in {2,...,4} {
          \foreach[evaluate={\n=int(\s-\m)}] \m in {1,...,\t} {
              \draw (\n,-\m) node {$a_{\m\n}$};
          }
      }
  \end{tikzpicture}
\end{document}

enter image description here


Avoid additional calculations. Try to think of another way of using nested \foreachs.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1.5]
  \foreach \y in {1,...,3} {
    \foreach \x in {1,...,\numexpr4-\y} {
      \draw (\x,-\y) node {$a_{\y\x}$};
    }
  }
\end{tikzpicture}
\end{document}

enter image description here