TikZ 矩阵:在节点周围放置框架但防止边缘重叠

TikZ 矩阵:在节点周围放置框架但防止边缘重叠

我无法弄清楚如何防止每个边界与其他边界重叠,请参见下面的 MWE。如果有人有想法...

\begin{tikzpicture}
  \footnotesize
  \matrix (m) [matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth,
               nodes={rectangle, draw=gray, minimum height=2em, minimum width=2em,
                      anchor=center, align=center,
                      inner sep=0pt, outer sep=0pt}]
  {
    1 & 5 & 6 & 6 & 6 & 7 & 7 & 7 & 8 & 8 & 9 & 10 & 10 \\
    11 & 11 & 11 & 12 & 12 & 12 & 12 & 13 & 13 & 13 & 14 & \node [draw, circle, thick, blue] {14} ; \\
    14 & 14 & 15 & 16 & 17 & 17 & 17 & 18 & 18 & 18 & 18 & 19 \\
    19 & 20 & 20 & 20 & 23 & 26 & 29 & 29 & 31 & 32 & 32 & 34\\
  } ;
  \draw [thick, red] (m-1-1.north west) -- (m-1-13.north east) -- (m-1-13.south east) -- (m-2-11.north east) -- (m-2-11.south east) -- (m-2-1.south west) --  (m-1-1.north west)  ;
  \draw [thick, orange] (m-3-1.north west) -- (m-3-12.north east) -- (m-4-12.south east) -- (m-4-1.south west) -- cycle ;
\end{tikzpicture}

谢谢。

答案1

您可以适当调整row sepcolumn sep。现在它们正在-\pgflinewidth使它们重叠。使\pgflinewidth或均匀2\pgflinewidth

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
  \begin{tikzpicture}
  \footnotesize
  \matrix (m) [matrix of nodes, row sep=2\pgflinewidth, column sep=2\pgflinewidth,
               nodes={rectangle, draw=gray, minimum height=2em, minimum width=2em,
                      anchor=center, %align=center,
                      inner sep=0pt, outer sep=0pt}]
  {
    1 & 5 & 6 & 6 & 6 & 7 & 7 & 7 & 8 & 8 & 9 & 10 & 10 \\
    11 & 11 & 11 & 12 & 12 & 12 & 12 & 13 & 13 & 13 & 14 & \node [draw, circle, thick, blue] {14} ; \\
    14 & 14 & 15 & 16 & 17 & 17 & 17 & 18 & 18 & 18 & 18 & 19 \\
    19 & 20 & 20 & 20 & 23 & 26 & 29 & 29 & 31 & 32 & 32 & 34\\
  } ;
  \draw [thick, red] (m-1-1.north west) -- (m-1-13.north east) -- (m-1-13.south east) -- (m-1-11.south east) -- (m-2-11.south east) -- (m-2-1.south west) --  (m-1-1.north west)  ;
  \draw [thick, orange] (m-3-1.north west) -- (m-3-12.north east) -- (m-4-12.south east) -- (m-4-1.south west) -- cycle ;
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

感谢 Harish,他的解决方案简单而有效。我在排版这张表格时迷失了方向。我最终得到了这个解决方案,它也有效,但相当不优雅。好处是我没有“双线”:

\begin{tikzpicture}
  \footnotesize
  \matrix (m) [matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth,
               nodes={rectangle, draw=gray, minimum height=2em, minimum width=2em,
                      anchor=center, align=center,
                      inner sep=0pt, outer sep=0pt}]{
  1 & 5 & 6 & 6 & 6 & 7 & 7 & 7 & 8 & 8 & 9 & 10 & 10 \alt<3->{\\
  11 & 11 & 11 & 12 & 12 & 12 & 12 & 13 & 13 & 13 & 14 & \node [draw, circle, thick, blue, minimum size=1ex] {14} ; }{\\
  11 & 11 & 11 & 12 & 12 & 12 & 12 & 13 & 13 & 13 & 14 & 14 }\\
  14 & 14 & 15 & 16 & 17 & 17 & 17 & 18 & 18 & 18 & 18 & 19 \\
  19 & 20 & 20 & 20 & 23 & 26 & 29 & 29 & 31 & 32 & 32 & 34\\ } ;
  \draw<2-> [thick, red] (m-1-1.north west) -- (m-1-13.north east) -- (m-1-13.south east) -- (m-2-11.north east) -- ($ (m-2-11.south east)!.03!(m-2-11.north east) $) -- ($ (m-2-1.south west)!.015!(m-1-1.north west) $) -- cycle  ;
  \draw<2-> [thick, orange] (m-3-1.north west) -- (m-3-12.north east) -- (m-4-12.south east) -- (m-4-1.south west) -- cycle ;
\end{tikzpicture}

相关内容