我想要一个包含方形单元格的数组,并用颜色填充单元格。W. Robertson 已经实现了包含方形单元格的表格http://www.tug.org/pracjourn/2005-2/robertson/robertson.pdf:
\newlength\celldim \newlength\fontheight \newlength\extraheight
\newcounter{sqcolumns}
\newcolumntype{S}{ @{}
>{\centering \rule[-0.5\extraheight]{0pt}{\fontheight + \extraheight}}
p{\celldim} @{} }
\newcolumntype{Z}{ @{} >{\centering} p{\celldim} @{} }
\newenvironment{squarecells}[1]
{\setlength\celldim{2em}%
\settoheight\fontheight{A}%
\setlength\extraheight{\celldim - \fontheight}%
\setcounter{sqcolumns}{#1 - 1}%
\begin{tabular}{|S|*{\value{sqcolumns}}{Z|}}\hline}
% squarecells tabular goes here
{\end{tabular}}
\newcommand\nl{\tabularnewline\hline}
因此我写道:
\documentclass[a4paper,12pt]{report}
\usepackage[MeX]{polski}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{calc}
\newlength\celldim \newlength\fontheight \newlength\extraheight
\newcounter{sqcolumns}
\newcolumntype{S}{ @{}
>{\centering \rule[-0.5\extraheight]{0pt}{\fontheight + \extraheight}}
p{\celldim} @{} }
\newcolumntype{Z}{ @{} >{\centering} p{\celldim} @{} }
\newenvironment{squarecells}[1]
{\setlength\celldim{4em}%
\settoheight\fontheight{A}%
\setlength\extraheight{\celldim - \fontheight}%
\setcounter{sqcolumns}{#1 - 1}%
\begin{tabular}{|S|*{\value{sqcolumns}}{Z|}}\hline}
% squarecells tabular goes here
{\end{tabular}}
\newcommand\nl{\tabularnewline\hline}
\begin{document}
\begin{squarecells}{4}
\cellcolor{red}16 & 3 & 2 & 13 \nl
5 & 10 & 11 & 8 \nl
9 & 6 + 2 & 7 & 12 \nl
4 & 15 & 14 & 1 \nl
\end{squarecells}
\end{document}
输出为:
单元格中红色的填充比应有的要多一点。问题是,如何正确地用颜色填充单元格?或者是否有其他好的解决方案来创建带有方形单元格的表格?
答案1
如果你对这种方法没有定论array
,而且可以明确指定方块的大小,那么你可以使用 TikZ 来实现这一点。根据问题的答案TikZ 矩阵作为表格的替代品,你可以定义一个style
调整 TikZmatrix
以使所有单元格相互连接并具有相同的高度和宽度:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\tikzset{square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={draw,
minimum height=#1,
anchor=center,
text width=#1,
align=center,
inner sep=0pt
},
},
square matrix/.default=1.2cm
}
\matrix[square matrix]
{
16 & 3 & 2 & 13 \\
5 & 10 &|[fill=yellow]| 11 & 8 \\
9 & 6 + 2 & 7 & 12 \\
4 & 15 & 14 & 1 \\
};
\end{tikzpicture}
\end{document}
要调整元素的大小,请使用square matrix=<length>
。要用不同的颜色填充元素,请|[fill=<colour>]|
在要调整的单元格中使用。
答案2
如果在您的代码中,我将(我已加载的)替换{tabular}
为键(别名:),则我直接得到预期的输出。原因是在后台使用 PGF/Tikz 为单元格着色,而不使用)。{NiceTabular}
nicematrix
color-inside
colortbl-like
nicematrix
colortbl
\documentclass[a4paper,12pt]{report}
\usepackage[MeX]{polski}
\usepackage[table]{xcolor}
\usepackage{nicematrix}
\usepackage{calc}
\newlength\celldim \newlength\fontheight \newlength\extraheight
\newcounter{sqcolumns}
\newcolumntype{S}{ @{}
>{\centering \rule[-0.5\extraheight]{0pt}{\fontheight + \extraheight}}
p{\celldim} @{} }
\newcolumntype{Z}{ @{} >{\centering} p{\celldim} @{} }
\newenvironment{squarecells}[1]
{\setlength\celldim{4em}%
\settoheight\fontheight{A}%
\setlength\extraheight{\celldim - \fontheight}%
\setcounter{sqcolumns}{#1 - 1}%
\begin{NiceTabular}[color-inside]{|S|*{\value{sqcolumns}}{Z|}}\hline}
% squarecells tabular goes here
{\end{NiceTabular}}
\newcommand\nl{\tabularnewline\hline}
\begin{document}
\begin{squarecells}{4}
\cellcolor{red}16 & 3 & 2 & 13 \nl
5 & 10 & 11 & 8 \nl
9 & 6 + 2 & 7 & 12 \nl
4 & 15 & 14 & 1 \nl
\end{squarecells}
\end{document}
您需要进行多次编译(因为nicematrix
在后台使用了 PGF/Tikz)。