我正在尝试编写一个函数,该函数绘制一个矩阵,其中包含一些 1(黑色)和 0(白色),并且 1 和 0 的分布具有一定的模式。我只想绘制 1,但前提是它们在矩阵的边界内,并且需要评估矩阵索引上的一些不等式来测试这一点。这是一个 MWE:
\documentclass{beamer}
\usepackage{tikz}
\usepackage{ifthen}
\usetikzlibrary{calc}
\newcommand{\LStepMatrix}[6] {
% #1 origin: top left corner of the matrix
% #2: N number of cols in the matrix
% #3: M number of rows in the matrix
% #4: L size of the step
% #5: The numeric sequence used for this matrix
% #6: scale factor for the figure
% Draw the zeros
\foreach \i in {1,2,...,#2} {
\foreach \j in {1,2,...,#3} {
\node[minimum width=#6cm,minimum height=#6cm,draw,inner sep=0] at ($#1+#6*(\i,-\j)$) {};
}
}
% Draw the ones in the positions of the numeric sequence
\foreach \i in {1,2,...,#3} { % Iterate over the rows
\foreach \j in {#5} {
%% \pgfmathparse{\i*#4-\j > -1? "gtz":"ltz"} \edef\mycol{\pgfmathresult}
%% \ifthenelse {\equal{\mycol}{gtz}} { % If we are inside the boundaries of the matrix print the bit
%% \node[minimum width=#6cm,minimum height=#6cm,fill=black,inner sep=0] at ($#1+#6*(\i*#4-\j+1,-\i)$) {};
%% } { %Otherwise don't print the bit
%% }
\pgfmathparse{\i*#4-\j} \edef\mycol{\pgfmathresult}
\ifthenelse {2>1} { % If we are inside the boundaries of the matrix print the bit
\node[minimum width=#6cm,minimum height=#6cm,fill=black,inner sep=0] at ($#1+#6*(\i*#4-\j+1,-\i)$) {};
} { %Otherwise don't print the bit
}
}
}
}
\begin{document}
\begin{frame}{}
\begin{tikzpicture}
\LStepMatrix{(0.5,7)}{64}{8}{7}{1,2,3,4,5,6,7,9,11,13,15,17,19,21}{0.15}
\end{tikzpicture}
\end{frame}
\end{document}
我正在尝试使用 \pgfmathparse 来评估我想要在矩阵第 i 行中绘制的位的位置。有一个测试 2>1 始终为真,即:它始终绘制位。如果我将该测试更改为 \mycol>0,Latex 不会接受它。我只想在矩阵边界内绘制位。
我找到了一个解决方案,上面有注释。我评估列的表达式,进行比较,并根据测试是在矩阵内部还是外部返回字符串 gtz 或 ltz。然后我在 \ifthenelse 中测试该字符串并相应地绘制或不绘制位。然而,我觉得这是一种次优的方法,需要定义一个中间字符串来确定测试的有效性。我如何评估表达式 \i*#4-\j 并根据此表达式的范围排版不同的代码?
答案1
据我所知,
\foreach \i in {1,2,...,#3} { % Iterate over the rows
\foreach \j in {#5} {
\pgfmathparse{\i*#4-\j > -1 ? 1 : 0}
\ifthenelse {\pgfmathresult>0}
{% If we are inside the boundaries of the matrix print the bit
\node[minimum width=#6cm,minimum height=#6cm,fill=black,inner sep=0]
at ($#1+#6*(\i*#4-\j+1,-\i)$) {};
}
{}% otherwise do nothing
}
}
可以。您尝试的测试的问题是\pgfmathparse{\i*#4-\j}
变成\pgfmathresult
了一个带有小数部分的数字,不适合 的数字测试\ifthenelse
。
答案2
我使用简单\ifnum
的\numexpr
:
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\LStepMatrix}[6] {%
% #1 origin: top left corner of the matrix
% #2: N number of cols in the matrix
% #3: M number of rows in the matrix
% #4: L size of the step
% #5: The numeric sequence used for this matrix
% #6: scale factor for the figure
% Draw the zeros
\foreach \i in {1,2,...,#2} {
\foreach \j in {1,2,...,#3} {
\node[minimum width=#6cm,minimum height=#6cm,draw,inner sep=0] at
($#1+#6*(\i,-\j)$) {};
}%
}%
% Draw the ones in the positions of the numeric sequence
\foreach \i in {1,2,...,#3} { % Iterate over the rows
\foreach \j in {#5} {
\ifnum \numexpr\i*#4-\j > -1
\node[minimum width=#6cm,minimum height=#6cm,fill=black,inner sep=0] at ($#1+#6*(\i*#4-\j+1,-\i)$) {};
\fi
\ifnum \numexpr\i*#4-\j > 1
\node[minimum width=#6cm,minimum height=#6cm,fill=black,inner sep=0] at ($#1+#6*(\i*#4-\j+1,-\i)$) {};
\fi
}%
}%
}
\begin{document}
\begin{frame}{}
\begin{tikzpicture}
\LStepMatrix{(0.5,7)}{64}{8}{7}{1,2,3,4,5,6,7,9,11,13,15,17,19,21}{0.15}
\end{tikzpicture}
\end{frame}
\end{document}