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,他解決了這個問題。我所做的只是做了一些小的額外調整。正如 Sigur 指出的,主要問題是透過設定 來解決的anchor=center,否則旋轉中心不相同。程式碼中標記了細微的更改。

\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}

在此輸入影像描述

遊戲愉快!

相關內容