Tikz 矩阵 - 表格超出范围的定位

Tikz 矩阵 - 表格超出范围的定位

我正在尝试使用 tikz 和矩阵工具绘制一个表格,但是当我在 pdf 中绘图时,表格位于边距之外。

我试图将矩阵定位在位置 (0,0),但无法移动。我使用了 overleaf 中“箭头形状表”示例提供的模板,并根据自己的方便对其进行了修改。

以下是代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc,fit,matrix,decorations.markings,decorations.pathreplacing}

\definecolor{colone}{RGB}{209,220,204}
\definecolor{coltwo}{RGB}{204,222,210}
\definecolor{colthree}{RGB}{207,233,232}
\definecolor{colfour}{RGB}{248,243,214}
\definecolor{colfive}{RGB}{245,238,197}
\definecolor{colsix}{RGB}{243,235,179}
\definecolor{colseven}{RGB}{241,231,163}

\tikzset{ 
table/.style={
  matrix of nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,text width=2cm,align=center},
  text depth=1.25ex,
  text height=2.5ex,
  nodes in empty cells}
}

\begin{document}
\begin{tikzpicture}
\matrix (mat) at (0,0) [table] 
{
    {}      & |[fill=colfour]|  & |[fill=colfour]|
      & |[fill=colfour]|  & |[fill=colfour]|  &                   \\
    % |[fill=colfive]|      & |[fill=colfive]|  & |[fill=colfive]|
    %   & |[fill=colfive]|  & |[fill=colfive]|  &                   \\
    % |[fill=colsix]|       & |[fill=colsix]|   & |[fill=colsix]|
    %   & |[fill=colsix]|   & |[fill=colsix]|   &   \\
    % |[fill=colseven]|     & |[fill=colseven]| & |[fill=colseven]|
    %   & |[fill=colseven]| & |[fill=colseven]| &  \\
    |[fill=colone]|       & |[fill=colthree]|   & |[fill=colthree]|
      & |[fill=colthree]|   & |[fill=colthree]|   &   \\
    % |[fill=colone]|       & |[fill=coltwo]|   & |[fill=colthree]|
    %   & |[fill=coltwo]|   & |[fill=colone]|   & \\
    % |[fill=colone]|       & |[fill=coltwo]|   & |[fill=colthree]|
    %   & |[fill=coltwo]|   & |[fill=colone]|   & \\
    % |[fill=colone]|       & |[fill=coltwo]|   & |[fill=colthree]|
    %   & |[fill=coltwo]|   & |[fill=colone]|   & \\
};

% horizontal rules
\foreach \row in {1,2}
% \draw[white] (mat-\row-1.north west) -- (mat-\row-5.north east);
% \draw[white] (mat-\row-1.south west) -- (mat-\row-5.south east);
\draw[white,ultra thick] (mat-1-1.north west) -- (mat-1-5.north east);
\draw[white,ultra thick] (mat-1-1.south west) -- (mat-1-5.south east);
\draw[white,ultra thick] (mat-2-1.south west) -- (mat-2-5.south east);

% vertical rules
\foreach \col in {1,2,3,4,5}
  \draw[white,ultra thick] (mat-2-\col.north west) -- (mat-2-\col.south west);

% Setup table Template
\node[below=1mm,fit=(mat-1-2)(mat-1-5)]{\parbox[c]{\linewidth}{\centering Week 43}};
\node[] at (mat-2-1){\parbox[c]{\linewidth}{\centering Groups}};
\node[] at (mat-2-2){\parbox[c]{\linewidth}{\centering Monday}};
\node[] at (mat-2-3){\parbox[c]{\linewidth}{\centering Tuesday}};
\node[] at (mat-2-4){\parbox[c]{\linewidth}{\centering Wednesday}};
\node[] at (mat-2-5){\parbox[c]{\linewidth}{\centering Thursday}};
\node[] at (mat-2-6){\parbox[c]{\linewidth}{\centering Friday}};


\end{tikzpicture}
\end{document}

答案1

首先,tikzpicture坐标只在 内相关tikzpicture。一旦图形完成, 就会像其他任何字符一样被 考虑LaTeX。因此,使用\matrix (mat) at (0,0) [table]毫无意义,因为它只是意味着矩阵中心将放置在 的坐标中(0,0)tikzpicture但它对于它在纸上的位置毫无意义。

由于您使用了内部节点,因此结果位置错误\parbox[c]{\linewidth}{\centering Groups}。由于这些节点无法容纳在一行文本中,因此整个图形将向右对齐。但您不必声明这样的nodes,您可以将单元格内容插入matrix节点中。此外,您无需在单元格之间绘制白线,因为您可以使用 和 声明空白row sep区域column sep

除了多列节点之外,week 43其他所有节点都可以用它自己制作matrix。这就是以下代码中显示的内容。可以使用stylesforrows或简化此代码columns

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc,fit,matrix,decorations.markings,decorations.pathreplacing}

\definecolor{colone}{RGB}{209,220,204}
\definecolor{coltwo}{RGB}{204,222,210}
\definecolor{colthree}{RGB}{207,233,232}
\definecolor{colfour}{RGB}{248,243,214}
\definecolor{colfive}{RGB}{245,238,197}
\definecolor{colsix}{RGB}{243,235,179}
\definecolor{colseven}{RGB}{241,231,163}

\tikzset{ 
table/.style={
  matrix of nodes,
  row sep=.8pt,
  column sep=.8pt,
  nodes={rectangle,text width=2cm,align=center},
  text depth=1.25ex,
  text height=2.5ex,
  nodes in empty cells}
}

\begin{document}
\begin{tikzpicture}
\matrix (mat) at (0,0) [table] 
{
    {}      & |[fill=colfour]|  & |[fill=colfour]|
      & |[fill=colfour]|  & |[fill=colfour]|  &                   \\
    % |[fill=colfive]|      & |[fill=colfive]|  & |[fill=colfive]|
    %   & |[fill=colfive]|  & |[fill=colfive]|  &                   \\
    % |[fill=colsix]|       & |[fill=colsix]|   & |[fill=colsix]|
    %   & |[fill=colsix]|   & |[fill=colsix]|   &   \\
    % |[fill=colseven]|     & |[fill=colseven]| & |[fill=colseven]|
    %   & |[fill=colseven]| & |[fill=colseven]| &  \\
    |[fill=colone]|  Groups     & |[fill=colthree]| Monday  & |[fill=colthree]|
    Tuesday  & |[fill=colthree]| Wednesday  & |[fill=colthree]| Thursday  & |[fill=colthree]| Friday  \\
    % |[fill=colone]|       & |[fill=coltwo]|   & |[fill=colthree]|
    %   & |[fill=coltwo]|   & |[fill=colone]|   & \\
    % |[fill=colone]|       & |[fill=coltwo]|   & |[fill=colthree]|
    %   & |[fill=coltwo]|   & |[fill=colone]|   & \\
    % |[fill=colone]|       & |[fill=coltwo]|   & |[fill=colthree]|
    %   & |[fill=coltwo]|   & |[fill=colone]|   & \\
};
%
%% horizontal rules
%\foreach \row in {1,2}
%%% \draw[white] (mat-\row-1.north west) -- (mat-\row-5.north east);
%%% \draw[white] (mat-\row-1.south west) -- (mat-\row-5.south east);
%\draw[white,ultra thick] (mat-1-1.north west) -- (mat-1-6.north east);
%\draw[white,ultra thick] (mat-1-1.south west) -- (mat-1-6.south east);
%\draw[white,ultra thick] (mat-2-1.south west) -- (mat-2-6.south east);
%
%% vertical rules
%\foreach \col in {1,2,3,4,5}
%  \draw[white,ultra thick] (mat-2-\col.north west) -- (mat-2-\col.south west);
%
%% Setup table Template
\node[fill=colfour,fit=(mat-1-2)(mat-1-6), inner sep=0pt, label=center:{Week 43}]{};
%\node[] at (mat-2-1){\parbox[c]{\linewidth}{\centering Groups}};
%\node[] at (mat-2-2){\parbox[c]{\linewidth}{\centering Monday}};
%\node[] at (mat-2-3){\parbox[c]{\linewidth}{\centering Tuesday}};
%\node[] at (mat-2-4){\parbox[c]{\linewidth}{\centering Wednesday}};
%\node[] at (mat-2-5){\parbox[c]{\linewidth}{\centering Thursday}};
%\node[] at (mat-2-6){\parbox[c]{\linewidth}{\centering Friday}};
%
%
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容