tikz 矩阵中的边距问题

tikz 矩阵中的边距问题

我正在制作一款方形棋盘游戏,需要旋转单元格中的文本(以便所有玩家都能阅读)。我遇到的问题是,当我旋转单元格时,会出现一些额外的空格,我无法消除这些空格。正如本网站其他答案所建议的那样,我使用了

anchor=base,minimum width=1.75cm,minimum height=1.75cm,text height=2ex,text depth=0.25ex

column sep=0cm, row sep=0cm, outer sep=0pt,inner sep=0

它应该在它应该在的地方。但我仍然得到错误。我的代码是:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}
\usepackage{amsthm}
\usepackage{colortbl}
\begin{document}

\begin{tikzpicture}[element/.style={anchor=base,minimum width=1.75cm,minimum height=1.75cm,draw=black,line width=1pt,text height=2ex,text depth=0.25ex}]
\matrix (m) [matrix of nodes,nodes={element},column sep=0cm, row sep=0cm, outer sep=0pt,inner sep=0]{
|[draw,rotate=-90]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=-90]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 & |[draw,rotate=-90]|3 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=-90]|31 & |[draw,fill=blue!20,rotate=180]|23 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=-90]|2 & |[draw,fill=red!20,rotate=180]|9 & |[draw,rotate=-90]|12 & |[draw,fill=red!20,rotate=180]|19 & |[draw,fill=red!20,rotate=180]|22 & |[draw,rotate=-90]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=-90]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=-90]|3 & |[draw,fill=blue!20,rotate=180]|8 & |[draw,rotate=-90]|13 & |[draw,fill=yellow!20,rotate=180]|18 & |[draw,fill=red!20,rotate=180]|23 & |[draw,rotate=-90]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=-90]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=-90]|4 & |[draw,fill=red!20,rotate=180]|7 & |[draw,rotate=-90]|14 & & & & & |[draw,rotate=-90]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21\\
|[draw,rotate=-90]|5 & |[draw,fill=red!20,rotate=180]|6 & |[draw,rotate=-90]|15 & & & & & |[draw,rotate=-90]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=-90]|4 & |[draw,fill=red!20,rotate=180]|7 & |[draw,rotate=-90]|14 & & & & & |[draw,rotate=-90]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21\\
|[draw,rotate=-90]|5 & |[draw,fill=red!20,rotate=180]|6 & |[draw,rotate=-90]|15 & & & & & |[draw,rotate=-90]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=-90]|3 & |[draw,fill=blue!20,rotate=180]|8 & |[draw,rotate=-90]|13 & |[draw,fill=yellow!20,rotate=180]|18 & |[draw,fill=red!20,rotate=180]|23 & |[draw,rotate=-90]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=-90]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=-90]|4 & |[draw,fill=red!20,rotate=180]|7 & |[draw,rotate=-90]|14 & |[draw,fill=red!20,rotate=180]|17 & |[draw,fill=blue!20,rotate=180]|24 & |[draw,rotate=-90]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=-90]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21\\
|[draw,rotate=-90]|5 & |[draw,fill=red!20,rotate=180]|6 & |[draw,rotate=-90]|15 & |[draw,fill=blue!20,rotate=180]|16 & |[draw,fill=yellow!20,rotate=180]|25 & |[draw,rotate=-90]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=-90]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
};

\end{tikzpicture}

\end{document}

我的输出:

在此处输入图片描述

我怎样才能除掉所有空白处,以便旋转不会弄乱我的板子?

答案1

基本问题的解决方案如下西格尔 注释。但是,可以通过以下方式简化代码,使其更加简洁:

  • 为每一列分别定义样式。这样,所有用于旋转的本地命令和许多用于颜色的命令都变得多余
  • 最好在一个地方定义节点样式,作为选项matrix
  • 由于节点的内容是数字,因此规定 text depth多余的
  • 为节点的平方足以确定节点的最小尺寸

得到的 mwe 为:

\documentclass{standalone}
%\usepackage[table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{amsthm}

\begin{document}
    \begin{tikzpicture}
\matrix (m) [matrix of nodes,
             nodes={draw, line width=1pt,
                    minimum size=4ex, outer sep=0pt,
                    anchor=center},
             column sep=-1pt, row sep=-1pt,
             column 1/.style = {nodes={rotate=-90}},
             column 2/.style = {nodes={rotate=180}},
             column 3/.style = {nodes={rotate=-90}},
             column 4/.style = {nodes={rotate=180}},
             column 5/.style = {nodes={rotate=180}},
             column 6/.style = {nodes={rotate=-90}},
             column 7/.style = {nodes={fill=blue!20,rotate=180}},
             column 8/.style = {nodes={rotate=-90}},
             column 9/.style = {nodes={fill=blue!20,rotate=180}},
            column 10/.style = {nodes={fill=red!20,rotate=-90}},
            ]{
1 & |[fill=blue!20]|10 & 11 & |[fill=blue!20]|20   & |[fill=red!20]|21 & 3 & 10 & 31 & 23 & 21 \\
%
2 & |[fill=red!20]|9    & 12  & |[fill=red!20]|19    & |[fill=red!20]|22 & 1 & 10 & 11 & 20 & 21 \\
%
3 & |[fill=blue!20]|8   & 13  & |[fill=yellow!20]|18 & |[fill=red!20]|23 & 1 & 10 & 11 & 20 & 21 \\
%
4 & |[fill=red!20]|7    & 14  & & & & & 11 & 20 & 21 \\
%
5 & |[fill=red!20]|6    & 15  & & & & & 11 & 20 & 21 \\
%
4 & |[fill=red!20]|7    & 14  & & & & & 11 & 20 & 21 \\
%
5 & |[fill=red!20]|6    & 15  & & & & & 11 & 20 & 21 \\
%
3 & |[fill=blue!20]|8   & 13  & |[fill=yellow!20]|18 & |[fill=red!20]|23  & 1 & 10 & 11 & 20 & 21 \\
%
4 & |[fill=red!20]|7    & 14  & |[fill=red!20]|17    & |[fill=blue!20]|24 & 1 & 10 & 11 & 20 & 21 \\
%
5 & |[fill=red!20]|6 & 15 & |[fill=blue!20]|16 & |[fill=yellow!20]|25 & 1 & 10 & 11 & 20 & 21 \\
};
    \end{tikzpicture}

结果是:

在此处输入图片描述

答案2

免责声明:Sigur 解决了这个问题,功不可没。我所做的只是做了一些额外的小调整。anchor=center正如 Sigur 所指出的,主要问题通过设置来解决,否则旋转中心会不一样。代码中标记了小改动。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,shapes,positioning}
\usepackage{amsthm}
\usepackage{colortbl}
\begin{document}

\begin{tikzpicture}[element/.style={regular polygon,
regular polygon sides=4, %<- make sure that the nodes end up being quadratic
anchor=center,%<- Sigur
minimum width=1.75cm,
draw=black,line width=1pt,text height=2ex,text depth=0.25ex
}]
\matrix (m) [matrix of nodes,nodes={element},column sep=0cm, row sep=0cm, 
outer sep=0pt,inner sep=0,row sep=-1pt,column sep=-1pt]{%<- to have the same line width everywhere
|[draw,rotate=270]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=270]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 & |[draw,rotate=270]|3 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=270]|31 & |[draw,fill=blue!20,rotate=180]|23 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=270]|2 & |[draw,fill=red!20,rotate=180]|9 & |[draw,rotate=270]|12 & |[draw,fill=red!20,rotate=180]|19 & |[draw,fill=red!20,rotate=180]|22 & |[draw,rotate=270]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=270]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=270]|3 & |[draw,fill=blue!20,rotate=180]|8 & |[draw,rotate=270]|13 & |[draw,fill=yellow!20,rotate=180]|18 & |[draw,fill=red!20,rotate=180]|23 & |[draw,rotate=270]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=270]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=270]|4 & |[draw,fill=red!20,rotate=180]|7 & |[draw,rotate=270]|14 & & & & & |[draw,rotate=270]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21\\
|[draw,rotate=270]|5 & |[draw,fill=red!20,rotate=180]|6 & |[draw,rotate=270]|15 & & & & & |[draw,rotate=270]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=270]|4 & |[draw,fill=red!20,rotate=180]|7 & |[draw,rotate=270]|14 & & & & & |[draw,rotate=270]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21\\
|[draw,rotate=270]|5 & |[draw,fill=red!20,rotate=180]|6 & |[draw,rotate=270]|15 & & & & & |[draw,rotate=270]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=270]|3 & |[draw,fill=blue!20,rotate=180]|8 & |[draw,rotate=270]|13 & |[draw,fill=yellow!20,rotate=180]|18 & |[draw,fill=red!20,rotate=180]|23 & |[draw,rotate=270]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=270]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
|[draw,rotate=270]|4 & |[draw,fill=red!20,rotate=180]|7 & |[draw,rotate=270]|14 & |[draw,fill=red!20,rotate=180]|17 & |[draw,fill=blue!20,rotate=180]|24 & |[draw,rotate=270]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=270]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21\\
|[draw,rotate=270]|5 & |[draw,fill=red!20,rotate=180]|6 & |[draw,rotate=270]|15 & |[draw,fill=blue!20,rotate=180]|16 & |[draw,fill=yellow!20,rotate=180]|25 & |[draw,rotate=270]|1 & |[draw,fill=blue!20,rotate=180]|10 & |[draw,rotate=270]|11 & |[draw,fill=blue!20,rotate=180]|20 & |[draw,fill=red!20,rotate=180]|21 \\
};
\end{tikzpicture}

\end{document}

在此处输入图片描述

祝您游戏愉快!

相关内容