缺少數字,在 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}

輸出

在此輸入影像描述 在此輸入影像描述

相關內容