答案1
你可以使用 Ti 矩阵钾Z:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
mymatrix/.style={
matrix of nodes,
column sep=2mm,
row sep={-\pgflinewidth},
nodes={
draw,
fill=blue!20,
text width=1.25ex,
text height=1.5ex,
text depth=0pt,
align=center,
anchor=center,
},
row 1/.style={
row sep=2mm,
nodes={
draw=none,
fill=none,
}
}
},
}
\begin{document}
\begin{tikzpicture}
\matrix[mymatrix] {
7 & 0 & 1 & 2 & 0 & 3 \\
7 & 7 & 7 & 2 & & 2 \\
{} & 0 & 0 & 0 & & 3 \\
{} & {} & 1 & 1 & & 1 \\
};
\end{tikzpicture}
\end{document}
答案2
如果您的任务需要排版类似表格的任意图表,那么使用一些宏来自动化该过程将是理想的选择。
代码
下面,\createCol
宏接受四个输入,并创建一个带有蓝色单元格的列。如果您想要一个顶部只有一个数字但没有蓝色单元格的列,您\noBlueCol
可以使用一个接受单个输入的宏。LaTeX 将表格视为文本(嗯,有点像——表格被放入盒子表格元素会按照行对齐(并插入到文本行中),而且每个表格的垂直大小相同,只要不分段,所有元素最终都会按行对齐。
\documentclass[12pt]{article}
\usepackage[table,svgnames]{xcolor}
\newcommand{\createCol}[4]{\begin{tabular}{c}
#1
\\
{}
\\\hline
\multicolumn{1}{|c|}{\cellcolor{LightBlue}#2}
\\\hline
\multicolumn{1}{|c|}{\cellcolor{LightBlue}#3}
\\\hline
\multicolumn{1}{|c|}{\cellcolor{LightBlue}#4}
\\\hline
\end{tabular}
}
\newcommand{\noBlueCol}[1]{\begin{tabular}{c}
#1
\\
{}
\\
\multicolumn{1}{c}{\phantom{1}}
\\
\multicolumn{1}{c}{}
\\
\multicolumn{1}{c}{}
\\
\end{tabular}
}
\begin{document}
\sffamily
\createCol{1}{2}{3}{4}
\createCol{1}{2}{}{}
\createCol{2}{4}{6}{}
\noBlueCol{2}
\createCol{3}{4}{5}{6}
\createCol{3}{2}{1}{0}
\end{document}
输出
答案3
使用方形单元格:
\documentclass{article}
\usepackage[table,svgnames]{xcolor}
\usepackage{array}
\newcommand{\mystrut}{%
\rule{0pt}{\dimexpr0.5\dp\strutbox+\fontcharht\font`0}%
\rule[-0.5\dp\strutbox]{0pt}{0.5\dp\strutbox}%
}
\newcommand{\stack}[4]{%
\begingroup\renewcommand{\arraystretch}{0}%
\setlength{\tabcolsep}{0pt}%
\begin{tabular}[t]{|>{\cellcolor{LightBlue}\mystrut}wc{\dimexpr\dp\strutbox+\fontcharht\font`0}|}
\multicolumn{1}{c}{#1}\\[1ex]
\hline
#2 \\ \hline
#3 \\ \hline
#4 \\ \hline
\end{tabular}%
\endgroup
}
\newcommand{\nostack}[1]{%
\makebox[\dimexpr\dp\strutbox+\fontcharht\font`0+2\arrayrulewidth]{#1}%
}
\begin{document}
\begin{center}
\stack{7}{7}{}{}\hfill
\stack{0}{7}{0}{}\hfill
\stack{1}{7}{0}{1}\hfill
\stack{2}{2}{0}{1}\hfill
\nostack{0}\hfill
\stack{3}{2}{3}{1}\hfill
\stack{0}{2}{3}{0}\hfill
\stack{4}{4}{3}{0}\hfill
\stack{2}{4}{2}{0}\hfill
\stack{3}{4}{2}{3}\hfill
\stack{0}{0}{2}{3}\hfill
\nostack{3}\hfill
\nostack{2}\hfill
\stack{1}{0}{1}{3}\hfill
\stack{2}{0}{1}{2}\hfill
\nostack{0}\hfill
\nostack{1}\hfill
\stack{7}{7}{1}{2}\hfill
\stack{0}{7}{0}{2}\hfill
\stack{1}{7}{0}{1}
\end{center}
\bigskip
\noindent
\mbox{\small
\stack{7}{7}{}{}\enspace
\stack{0}{7}{0}{}\enspace
\stack{1}{7}{0}{1}\enspace
\stack{2}{2}{0}{1}\enspace
\nostack{0}\enspace
\stack{3}{2}{3}{1}%
}
\end{document}