我希望能够有这样一段代码,通过修改某些参数(例如高度和宽度)以及将单元格颜色设为红色或绿色,为我生成一个表格。我手头的代码已经是我发现的某个东西的修改版,但我无法让彩色单元格正常工作。错误是“缺少数字,视为零”。我刚开始使用 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}