Math operations in the indices of indexed variables

The \x macro doesn't perform arithmetic on its argument, but you can make it do it.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}

\newcommand{\usevar}[2]{%
  \expandafter#1\expandafter{\the\numexpr#2\relax}%
}

\begin{document}

\begin{tikzpicture}
\tikzmath{
\x1 = 1;
\x2 = 3;
\x3 = 5;
\x4 = 7;
%Since units are not given in \tikzmat, they will be evaluated as cm in tikz enviroment below.
}

\draw (\x{1},0) -- (10,5) node[at start, below]{\x{1}};

\draw[red] (\usevar\x{2-1},0) -- (4,5) node[at start, below]{\usevar\x{2-1}};

\foreach \ind in {2,...,4}
\draw (\x{\ind},0) -- (10,10) node[at start, below]{\x{\ind}};

\foreach \ind in {2,...,4}
\draw[red] (\usevar\x{\ind-1},0) -- (5,5) node[at start, below]{\usevar\x{\ind-1}};

\end{tikzpicture}
\end{document}

enter image description here

Explanation

When you do \tikzmath{\x<argument>=<expression>}, TikZ defines the macro \x and also an internal macro

\tikz@math@var@indexed@x@<argument>

that in turn expands to the (computed) expression. The macro \x is essentially defined to look at its argument and to compose the internal macro name from it. The <argument> needn't be a math expression and therefore no attempt at evaluating it is done.

Thus you need to perform the evaluation (assuming only integers are involved in the “subscript”) before \x is expanded. This is the task performed by \usevar, which sets \x aside, expands \the\numexpr#2\relax, that returns an integer, and then goes back to \x (its first argument, in general) which now “sees” the computed argument.

Tags:

Tikz Pgf