I'm trying to write a function that draws a matrix with a some ones (in black) and zeros (white) in it with a certain pattern to the distribution of ones and zeros. I want to draw ones, only if they are inside the boundaries of the matrix and need to evaluate some inequalities on the matrix indices to test that. Here is an 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}
I'm trying to use \pgfmathparse to evaluate the position of the bit I want to draw in the i'th row of the matrix. There is a test 2>1 that is always true, ie: it always draws the bit. If I change that test to \mycol>0 latex does not accept it. I only want to draw the bit if it is within the boundaries of the matrix.
I've found a solution which is commented above. I evaluate the expression for the column, perform the comparison and return a string gtz or ltz depending on whether the test is inside or outside the matrix. I then test that string in the \ifthenelse and draw, or don't draw the bit accordingly. However this strikes me as a kind of suboptimal way of doing it, needing to define an intermediate string to determine the validity of the test. How can I evaluate the expression \i*#4-\j and typeset different code according to the range of this expression?
Best Answer
As far as I can tell, something like
will work. The problem with the test you were trying is that
\pgfmathparse{\i*#4-\j}
makes\pgfmathresult
into a number with a fractional part, not suitable for the numeric test of\ifthenelse
.