2

I am trying to draw a table using tikz and the matrix tool, but when I plot in pdf the table is located out of the margins.

I am trying to localize the matrix at the position (0,0) but it is impossible to move. I used the template provided by "arrow shape table" example found in overleaf and from them I am modifying to my convinience.

Here is the code:

\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

First of all, tikzpicture coordinates are only relevant inside the tikzpicture. Once the figure is finished, it is considered like any other character by LaTeX. Therefore the use of \matrix (mat) at (0,0) [table] has no sense bucause it just means that the matrix center will be placed in coordinate (0,0) into the tikzpicture but it means nothing regarding its position on paper.

Your result is missplaced because you use \parbox[c]{\linewidth}{\centering Groups} inside nodes. As these nodes don't fit on one textline, the whole figure is aligned to right. But you don't have to declare such nodes, you can insert cells contents into the matrix node. Also you don't need to draw white lines between cells because you can declare an empty space with row sep and column sep.

Except the multicolumn node week 43 everything else can be made with the matrix it self. This is what is shown in following code. This code cuold be simplified using styles for rows or 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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.