Operaciones matemáticas en los índices de variables indexadas.

Operaciones matemáticas en los índices de variables indexadas.

Tengo una variable indexada y no sé cómo realizar operaciones matemáticas simples en el índice, como x{\a-1}.

Estoy usando \tikzmathmucho; por lo tanto, mi MWE lo incluye:

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

\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) -- (10cm,10cm) node[at start, below]{\x{1}}; %This works

%\draw (\x{2-1},0) -- (10cm,10cm) node[at start, below]{\x{2-1}}; %This does not work

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

%\foreach \ind in {2,...,4}
%\draw (\x{\ind-1},0) -- (10cm,10cm) node[at start, below]{\x{\ind-1}}; %This does not work

\end{tikzpicture}
\end{document}

Respuesta1

La \xmacro no realiza operaciones aritméticas con su argumento, pero puedes obligarla a hacerlo.

\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}

ingrese la descripción de la imagen aquí

Explicación

Cuando lo haces \tikzmath{\x<argument>=<expression>}, TikZ define la macro \xy también una macro interna

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

que a su vez se expande a la expresión (calculada). La macro \xse define esencialmente para observar su argumento y componer el nombre de la macro interna a partir de él. No es necesario <argument>que sea una expresión matemática y, por lo tanto, no se realiza ningún intento de evaluarla.

Por lo tanto, es necesario realizar la evaluación (asumiendo que solo hay números enteros involucrados en el “subíndice”) antes de \xexpandirse. Esta es la tarea realizada por \usevar, que deja \xde lado, expande \the\numexpr#2\relax, devuelve un número entero y luego regresa a \x(su primer argumento, en general) que ahora "ve" el argumento calculado.

Respuesta2

Adición menor a la solución de @egreg:

Si quisiéramos evaluar solo las expresiones similares a {2*\ind+5}la solución de @egreg sería:

\newcommand{\useevalof}[1]{%
  \the\numexpr#1\relax%
}

Ejemplo: considere $A_{\ind-4}$. Si \indtiene un valor de 6, $A_{\useevalof{\ind-4}}$dará$A_2$

Esto es realmente útil para mí. Estoy desarrollando una biblioteca Tikz de mecánica estructural para ingenieros civiles. Mira a las imagenes de abajo. Los niveles están completamente automatizados.

Anotado: eliminado \expandafterdespués de la nota @egreg.

ingrese la descripción de la imagen aquí

información relacionada