높이, 너비, 빨간색이나 녹색으로 표시될 셀과 같은 특정 매개변수를 수정하여 테이블을 생성하는 코드를 가질 수 있다면 좋을 것입니다. 내가 가지고 있는 코드는 이미 내가 찾은 것의 수정본이지만 색상이 지정된 셀을 작동시킬 수 없습니다. 오류는 "누락된 숫자, 0으로 처리됨"입니다. 저는 이제 막 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}