首页>LaTeX>source

我正在尝试使用下面的代码渲染石头网格,但是当我在 m-3-1中移除石头时 ,它上面和右边的石头(特别是 m-1-5 )将偏离中心:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes.geometric}
\newcommand\stone[0]{|[circle, shading=ball, ball color=black!80!white, minimum size=.8cm]|} % https://tex.stackexchange.com/a/184068/45824
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,
             anchor=south west,
             column sep={1cm,between origins},
             row sep={1cm,between origins},
             nodes in empty cells,
            ]
{
        &        &        &        & \stone \\
        &        &        &        &        \\
 \stone &        &        &        &        \\
};
\draw[step=1cm,color=gray] (0,0) grid (5,3);
\draw[thick,red,->] (m-1-1) -> (m-1-5);
\draw[thick,red,->] (m-1-5) -> (m-3-5);
\end{tikzpicture}
\end{document}

如何确保所有宝石都正确对齐?

最新回答
  • 2019-12-5
    1 #

    问题原因

    首先,您的"工作"代码甚至不输出预期的图表:圆圈尚未对齐

    添加选项 draw   到"不工作"代码中的每个节点都给我们

    可以解释问题:你的节点是正确对齐的,但矩阵本身并不在正确的位置。

    解决方案

      Use \tikzset

      添加选项 inner sep

      使用 minimum size   节点选项

      使用锚 center   对于箭头(见下文)

      \documentclass{minimal}
      \usepackage{tikz}
      \usetikzlibrary{matrix,shapes.geometric}
      \tikzset{stone/.style={circle,shading=ball,ball color=black!80!white,minimum size=.8cm}}
      \newcommand\stone{|[stone]|} % https://tex.stackexchange.com/a/184068/45824
      \begin{document}
      \begin{tikzpicture}
      \matrix (m) [matrix of nodes,
                   anchor=south west,
                   column sep={1cm,between origins},
                   row sep={1cm,between origins},
                   nodes in empty cells,
                   nodes={minimum size=1cm},
                   inner sep=0pt
                  ]
      {
              &        &        &        & \stone \\
              &        &        &        &        \\
              &        &        &        &        \\
      };
      \draw[step=1cm,color=gray] (0,0) grid (5,3);
      \draw[thick,red,->] (m-1-1.center) -> (m-1-5);
      \draw[thick,red,->] (m-1-5) -> (m-3-5.center);
      \end{tikzpicture}
      \end{document}
      

相关问题

  • LaTeX tikz pgf:我想使用tikzpicture在LaTeX中绘制m-way搜索树/ B树
  • LaTeXmk输出的时间戳