Problemas com margens na matriz tikz

Problemas com margens na matriz tikz

Estou fazendo um jogo de tabuleiro quadrado, que exige que o texto nas células seja girado (para que todos os jogadores possam lê-los). O problema que tenho é que ao girar a célula aparecem alguns espaços extras, que não consegui eliminar. Conforme sugerido em outras respostas neste site, usei

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

e

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

onde pertence. Mas ainda recebo o erro. Meu código é:

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

Minha saída:

insira a descrição da imagem aqui

Como posso me livrar de todos os espaços em branco, para que a rotação não atrapalhe meu quadro?

Responder1

uma solução para o problema básico é dada emSigur Comente. No entanto, seu código pode ser simplificado e torná-lo mais conciso:

  • definindo estilos separadamente para cada coluna. com isto todos os comandos locais para rotação e muitos para cores tornam-se supérfluos
  • é melhor definir o estilo dos nós em um só lugar, como opções dematrix
  • uma vez que o conteúdo dos nós são números, a prescrição de text depthsupérfluos
  • ser nós quadrados é suficiente para determinar o tamanho mínimo dos nós

mwe resultante é então:

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

o resultado é:

insira a descrição da imagem aqui

Responder2

Isenção de responsabilidade: crédito total para Sigur, que resolveu o problema. Tudo o que fiz foi fazer alguns pequenos ajustes adicionais. O principal problema é resolvido configurando anchor=center, conforme apontado por Sigur, caso contrário os centros de rotação não são os mesmos. As pequenas alterações estão marcadas no código.

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

insira a descrição da imagem aqui

Feliz jogo!

informação relacionada