我正在尝试用 LaTeX 创建棋盘。我使用 TikZ 的矩阵来完成这项任务。如果矩阵中只有文本或只有填充,它就可以工作。但是,当我同时使用文本和矩阵时,一切都开始奇怪地移动。我该如何解决这个问题?
下面是我正在使用的代码及其输出。
\documentclass[12pt, a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
%This one is the problematic one
\begin{minipage}{0.32\textwidth}
\begin{tikzpicture}
\draw[xstep=1cm,ystep=1,color=gray] (0,0) grid (4,4);
\matrix[matrix of nodes,
inner sep=0pt,
anchor=south west,
nodes={inner sep=0pt,text width=1cm,align=center,minimum height=1cm}
]{
|[fill=black!255]| & A & |[fill=black!255]| & \\
& |[fill=black!255]| & B & |[fill=black!255]| \\
|[fill=black!255]| & C & |[fill=black!255]| & \\
& |[fill=black!255]| & D & |[fill=black!255]| \\
};
\end{tikzpicture}
\end{minipage}
% Nothing's wrong if I only add text and fill on the highest layer
\begin{minipage}{0.32\textwidth}
\begin{tikzpicture}
\draw[xstep=1cm,ystep=1,color=gray] (0,0) grid (4,4);
\matrix[matrix of nodes,
inner sep=0pt,
anchor=south west,
nodes={inner sep=0pt,text width=1cm,align=center,minimum height=1cm}
]{
|[fill=black!255]| & A & & \\
& & B & \\
& C & & \\
& & D & \\
};
\end{tikzpicture}
\end{minipage}
% Also, everything's okay if there are only fills with no text
\begin{minipage}{0.32\textwidth}
\begin{tikzpicture}
\draw[xstep=1cm,ystep=1,color=gray] (0,0) grid (4,4);
\matrix[matrix of nodes,
inner sep=0pt,
anchor=south west,
nodes={inner sep=0pt,text width=1cm,align=center,minimum height=1cm}
]{
|[fill=black!255]| & & |[fill=black!255]| & \\
& |[fill=black!255]| & & |[fill=black!255]| \\
|[fill=black!255]| & & |[fill=black!255]| & \\
& |[fill=black!255]| & & |[fill=black!255]| \\
};
\end{tikzpicture}
\end{minipage}
\end{document}
答案1
这是我的方法。我添加了一些内容:,,,,;[anchor=center]
并删除了一些不必要的选项。[nodes in empty cells]
c/.style
[row sep=-\pgflinewidth]
[column sep=-\pgflinewidth]
\documentclass[12pt, a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\tikzset{c/.style={fill=yellow}}
\tikzset{mystyle/.style={matrix of nodes,
nodes in empty cells,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={draw,minimum width=1cm,minimum height=1cm,anchor=center}}}
\begin{tikzpicture}
\matrix[mystyle]{
|[c]|&A &|[c]|&\\
&|[c]|&B &|[c]|\\
|[c]|&C &|[c]|&\\
&|[c]|&D &|[c]|\\
};
\end{tikzpicture}
\begin{tikzpicture}
\matrix[mystyle]{
|[c]|& &|[c]|&\\
&|[c]|& &|[c]|\\
|[c]|& &|[c]|&\\
&|[c]|& &|[c]|\\
};
\end{tikzpicture}
\begin{tikzpicture}
\matrix[mystyle]{
&A &&\\
&&B &\\
&C &&\\
&&D &\\
};
\end{tikzpicture}
\end{document}
答案2
首先minipage
:
\documentclass[12pt, border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes,
nodes in empty cells, % <---
nodes={minimum size=1cm, anchor=center,
inner sep=0pt, outer sep=0pt},
draw, inner sep=0pt
]
{
|[fill]| & A & |[fill]| & \\
& |[fill]| & B & |[fill]| \\
|[fill]| & C & |[fill]| & \\
& |[fill]| & D & |[fill]| \\
};
\end{tikzpicture}
\end{document}
答案3
只是对 Zarko 的答案做了一点改动。可以结合行和列的样式来定义棋盘,而不必具体指定fill
所需的单元格。
\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes,
nodes in empty cells, % <---
every odd column/.style={every odd row/.append style={nodes={fill=black}}},
every even column/.style={every even row/.append style={nodes={fill=black}}},
nodes={minimum size=1cm, anchor=center,
inner sep=0pt, outer sep=0pt},
draw, inner sep=0pt
]
{
& A & & \\
& & B & \\
& C & & \\
& & D & \\
};
\end{tikzpicture}
\end{document}