tikz で for ループと条件文を使用するときに、欠落した数値がゼロエラーとして扱われる

tikz で for ループと条件文を使用するときに、欠落した数値がゼロエラーとして扱われる

高さや幅などの特定のパラメータを変更し、セルの色を赤や緑に変更することで、テーブルを生成してくれるコードがあればいいのですが。私が持っているコードは、すでに見つけたものを変更したものですが、色付きのセルを機能させることができません。エラーは「欠落した数値、ゼロとして扱われます」です。私は tikz を始めたばかりで、あまり理解していません。

\begin{figure}[H]
\centering
 \begin{tikzpicture}
[
    square/.style = {
        draw, 
        rectangle, 
        minimum size=\m, 
        outer sep=0, 
        inner sep=0, 
        font=\normalsize\bfseries,
        label={[anchor=north,yshift=1.1em,opacity=0.7]south:\scriptsize\label}
    },
]
\def\m{35pt}
\def\w{4}
\def\h{4}

% Array of personalized messages
\def\content{{
    {1, 0, 1, 1},
    {1, 1, 0, 0},
    {0, 0, 1, 0},
    {0, 1, 1, 1}
}}

% Separate arrays for x and y coordinates in greenCoords
\def\greenXCoords{{1, 1, 1, 1}} % x-coordinates
\def\greenYCoords{{1, 2, 3, 4}} % y-coordinates

% Y
\foreach \x in {1,...,\w}
    \foreach \y in {1,...,\h}
    {   
        % Check if the current coordinates match any green cells
        \foreach \i in {1,...,4}
        {
            \ifthenelse{\x=\greenXCoords[\i] \AND \y=\greenYCoords[\i]}
                {\def\fillColor{green!30}\break}
                {\def\fillColor{black!5}}
        }
        
        \pgfmathtruncatemacro{\label}{(\y-1) * \w + \x}
        \node [square, fill=\fillColor]  (Y\x,\y) at (\x*\m,-\y*\m) {\pgfmathparse{\content[\y-1][\x-1]}\pgfmathresult};
    }
\end{tikzpicture}
    \caption{Caption}
    \label{fig:enter-label}
\end{figure}

取得した表

答え1

配列構文\greenXCoords[\i]PGFMath構文理解されません\ifthenelse

これらを事前に評価する必要があります。

\pgfmathsetmacro\xTest{\greenXCoords[\i]}
\pgfmathsetmacro\yTest{\greenYCoords[\i]}
\ifthenelse{\x=\xTest \AND \y=\yTest}

またはPGFMath独自のifthenelse関数または、配列と条件文に対してまったく異なるアプローチを使用します。

あるいは、多数の配列やループをループせずにテーブルを構築します。(PGFMath 配列は最適なデータ構造ではありません。)


content独自の構造 (すでに行と列で構成されています) を使用して値を直接ループするソリューションを次に示します。

いくつかのスタイルとキーを使用すると、任意のコンテンツ、任意のサイズ、任意の組み合わせの緑色のセルを含むテーブルを作成できます。

ここでは、列の数(ラベルの計算用)を指定する必要がありますが、これを自動的に(最初の行/最長の行から?)決定したり、行の列数がすべて同じでない場合に穴を作成しない各要素をカウントアップしたりするソリューションが存在する可能性があります。

コード

\documentclass[tikz]{standalone}
\tikzset{
  % let's do this in our own namespace
  my table/.cd,
  % initial values and settings
  content/.initial={{1, 0, 1, 1},
                    {1, 1, 0, 0},
                    {0, 0, 1, 0},
                    {0, 1, 1, 1}},
  size/.initial=35pt,
  columns/.initial=4,
  square/.style={
    shape=rectangle, draw, minimum size=\pgfkeysvalueof{/tikz/my table/size},
    outer sep=+0pt, inner sep=+0pt, fill=black!5, font=\normalsize\bfseries},
  label/.style={
    label={[anchor=south, font=\scriptsize, lightgray]south:{#1}}},
  % styles to set which rows, cols and cells should be green!30
  green rows/.style={green row/.list={#1}},
  green cols/.style={green col/.list={#1}},
  green cells/.style={green cell/.list={#1}},
  green row/.style={/tikz/my table/rowstyle #1/.append style={fill=green!30}},
  green col/.style={/tikz/my table/colstyle #1/.append style={fill=green!30}},
  green cell/.style={/tikz/my table/style #1/.append style={fill=green!30}}}

% the macro with one optional (!) argument
\newcommand*\PrintTable[1][]{%
  \tikz[
    my table/.cd,#1,content/.get=\myTableContent,
    % make the xyz coordinate system dependent on the size of your squares
    /tikz/x=\pgfkeysvalueof{/tikz/my table/size},
    /tikz/y=\pgfkeysvalueof{/tikz/my table/size}]
  \foreach[count=\myTableY from 0,
           count=\myTableYY from 1]\myTableRow in \myTableContent
    \foreach[count=\myTableX]\myTableCol in \myTableRow
      \node[
        my table/square,
        my table/label=\inteval{\myTableY*
          \pgfkeysvalueof{/tikz/my table/columns}+\myTableX},
        my table/rowstyle \myTableYY/.try,        % 1. row style
        my table/colstyle \myTableX/.try,         % 2. col style
        my table/style \myTableYY-\myTableX/.try, % 3. cell style
      ] at (\myTableX,-\myTableY) {\myTableCol};%
}
\begin{document}
\PrintTable[green cells={1-1, 1-2, 1-3, 1-4}]
\PrintTable[green row=1, columns=3, content={{  1,   2,   3},
                                             { 19,  20,  21},
                                             {123, 456, 789}}]
\end{document}

出力

ここに画像の説明を入力してください ここに画像の説明を入力してください

関連情報