我想定义\x{1}, \x{2}, ...
;那么为什么下面的方法不起作用?
\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary {math}
\begin{document}
\tikzmath{
int \i;
for \xValue in {2, 3, 6}{%
for \i in {1,...,3}{%%
\x{\i}=\xValue;
};};% %%
}
Test: x(1)= \x{1}, ~~~ x(2)= \x{2}, ~~~ x(3)= \x{3}
gives alway the last defined number.
I tried the same with 'evaluate' (TikZ-manual, page 704), but this has no effect:
%\tikzset{
%evaluate={
%int \i;
%real \x;
%for \xValue in {2, 3, 6}{%
% for \i in {1,...,3}{%%
% let \x{\i}=\xValue;
%};};% %%
%}}
%
%Test: x(1)= \x{1}, ~~~ x(2)= \x{2}, ~~~ x(3)= \x{3}.
%gives alway the last defined number.
\end{document}
答案1
LaTeX 没有内在的数组存储概念,所以当你说时\x{1}
,它不会想起\x
数组的第一个元素......而是调用\x
宏并将参数传递给它1
(假设\x
正在寻找一个参数)。
但是,诸如此类的包listofitems
建立了数组概念,称为“列表”:
\documentclass{article}
\usepackage{listofitems}
\begin{document}
\readlist*\x{2, 3, 6}
Test: x(1)= \x[1], ~~~ x(2)= \x[2], ~~~ x(3)= \x[3]
\end{document}
附录
至于如何tikzmath
获得相同的结果,也许这可以实现(虽然我不是专家tikzmath
):
\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary {math}
\begin{document}
\tikzmath{
int \i;
\i=0;
for \xValue in {2, 3, 6}{%
\i=\numexpr\i+1\relax;
\x{\i}=\xValue;
};% %%
}
Test: x(1)= \x{1}, ~~~ x(2)= \x{2}, ~~~ x(3)= \x{3}
\end{document}