经过长时间的搜索,我仍然无法弄清楚如何确定和使用len
这样的数组:
\def\mar{ 1,2 }
\path let \p1=($(rec.west)-(rec.east)$),
\p2=($(rec.north)-(rec.south)$),
\p3=($len{\mar}$),
\n1 = {veclen(\p1)*0.16} %width
,\n2 = {veclen(\p2)/\p3} %height
in node[mynode,
minimum width=\n1
,minimum height=\n2
,anchor=north west
] at (rec.north west) {foobar
};
答案1
我不知道 TikZ 中是否有数组长度计数器(它可能在 CVS 版本中引入)。因此,请向xstring
包中,我们计算逗号并添加一。
\documentclass{article}
\usepackage{tikz,xstring}
\usetikzlibrary{calc}
\def\mar{1,2,4,5,7}
\StrCount{\mar}{,}[\arrlength]
\begin{document}
\begin{tikzpicture}
\node[draw,minimum width=2cm,minimum height=5cm] (rec) at (2cm,1cm) {rec};
\path let \p1=($(rec.west)-(rec.east)$),
\p2=($(rec.north)-(rec.south)$),
\n{arrlen}={\arrlength+1},
\n1 = {veclen(\p1)*0.16}, %width
\n2 = {veclen(\p2)/\n{arrlen}} %height
in node[draw,
minimum width=\n1
,minimum height=\n2
,anchor=north west
] at (rec.north west) {foobar
};
\end{tikzpicture}
I have found \arrlength\space commas in the array.
\end{document}
答案2
就像 PolGab 在他的评论中所写的那样,你有一些可能性可以将数组与 pgfmath 一起使用。以下是来自 pgfmanual 的一些示例:
\def\myarray{{7,-3,4,-9,11}} \pgfmathparse{\myarray[3]} \pgfmathresult \def\print#1{\pgfmathparse{#1}\pgfmathresult} \def\identitymatrix{{{1,0,0},{0,1,0},{0,0,1}}} \tikz[x=0.5cm,y=0.5cm]\foreach \i in {0,1,2} \foreach \j in {0,1,2} \node at (\j,-\i) [anchor=base] {\print{\identitymatrix[\i][\j]}}; \pgfmathparse{array({9,13,17,21},2)} \pgfmathresult
坏消息: len
数组的 不存在于 pgfmath 中。将这种可能性添加到 pgfmath 中并不复杂。这是一种无需新包的简单方法。它不是很有效,但很简单。
\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\def\getlen#1{%
\pgfmathsetmacro{\lenarray}{0}%
\foreach \i in #1{%
\pgfmathtruncatemacro{\lenarray}{\lenarray+1}%
\global\let\lenarray\lenarray}%
}
\begin{tikzpicture}
\node[draw,minimum width=2cm,minimum height=5cm,fill=blue!30,opacity=.5 ] (rec) at (2cm,1cm) {REC};
\def\mar{1,2}
\getlen{\mar}
\path let \p1=($(rec.west)-(rec.east)$),
\p2=($(rec.north)-(rec.south)$),
\n1 = {veclen(\p1)*0.16},
\n2 = {veclen(\p2)/\lenarray}
in node[draw,minimum width = \n1,
minimum height = \n2,
anchor=north west,fill=orange!30,opacity=.5
] at (rec.north west) {foobar};
\end{tikzpicture}
\end{document}
答案3
下面使用\foreach
循环来计算成员数,并忽略由于多余逗号而导致的空成员。因此,对于如下定义
\def\mar{1,2 , 4,, 5 ,7,,,}
本报告有 5 名成员:
代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}
\usetikzlibrary{calc}
\def\mar{1,2 , 4,, 5 ,7,,,}
\makeatletter
\newcounter{@LengthOfArray}%
\newcommand{\GetArrayLength}{\arabic{@LengthOfArray}}%
\newcommand*{\SetArrayLength}[1]{%
% Counts the number of array members. This ignore any empty
% array members created by extraneous commas (such as those
% as result of a double comma, or a trailing comma.
\setcounter{@LengthOfArray}{0}%
\foreach \x in #1 {%
\IfStrEq{\x}{}{}{% only increment when non-empty
\stepcounter{@LengthOfArray}%
}%
}%
}%
\makeatother
\begin{document}
\SetArrayLength{\mar}
\begin{tikzpicture}
\node[draw,minimum width=2cm,minimum height=5cm] (rec) at (2cm,1cm) {rec};
\path let \p1=($(rec.west)-(rec.east)$),
\p2=($(rec.north)-(rec.south)$),
\n{arrlen}={\GetArrayLength},
\n1 = {veclen(\p1)*0.16}, %width
\n2 = {veclen(\p2)/\n{arrlen}} %height
in node[draw,
minimum width=\n1
,minimum height=\n2
,anchor=north west
] at (rec.north west) {foobar
};
\end{tikzpicture}
\Large
There are \GetArrayLength\space members in the array.
\end{document}