1

I try to create a TikZ-matrix like that:
enter image description here

where bot rows had the height ca. 3cm. row 2 column 1 is planed as something like a multicolum.
But in the first I have problems with the last column of row 1.

I get only:
enter image description here

How do I have to set the measures correctly?

BTW: For some other stuff, I need a coordinate as shown in the picture, so I think using a TikZ-matrix is no bad idea.

\documentclass[]{scrartcl}
\usepackage{lmodern}
\usepackage[showframe=true,
width=18cm, height=26cm,
]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc, matrix}
\usepackage{tikzpagenodes}
\pagestyle{empty}


\pgfmathsetlengthmacro\InnerSep{5pt}
\pgfmathsetlengthmacro\ColumnSep{3mm}

\begin{document}
\begin{tikzpicture}[
shift={(current page text area.center)},
overlay,remember picture,
nodes={inner sep=0pt,outer sep=0pt},
Header/.style={
nodes={draw=blue, 
minimum width=3cm, minimum height=3cm, text width=3cm-\InnerSep, }
},
]
% For comparison  %%%%%%%%%%%%%%%%%%
\node[anchor=north west, inner sep=0pt,outer sep=0pt,
cyan, draw, 
minimum width=1.0\textwidth, 
%text width=1.0\textwidth-\InnerSep,
text height=5.5cm,
minimum height=6cm,
%text depth=6cm-4mm,
align=left, 
] at (-0.5\textwidth, 0.5\textheight) (Textbox) {For comparison: This dimension should the matrix have!};
%%%%%%%%%%%%%%%%%%%%%%%%%

% Textbox
\matrix[matrix of nodes, nodes in empty cells,
%inner sep=\pgflinewidth,
column sep=\ColumnSep, 
anchor=north west, 
draw=red, 
nodes={inner sep=\InnerSep, outer sep=0pt, anchor=north west, align=center},
row 1 column 1/.style={Header, draw, very thick}, 
row 1 column 2/.style={Header}, 
row 1 column 3/.style={
nodes={draw=brown, 
text=blue, draw=blue, align=left, 
minimum width=\textwidth-2*3cm-2*\ColumnSep-2*\InnerSep-4*\pgflinewidth, 
minimum height=3cm, 
text width=\textwidth-2*3cm-2*\ColumnSep-4*\InnerSep-4*\pgflinewidth, 
text depth=3cm-2*\InnerSep, % Error
}
}, 
] at (-0.5\textwidth, 0.5\textheight)
{
X & Y & 3 3 3 3 \\
1 & 2 & 3 \\
};
\end{tikzpicture}
\end{document}
1

It has to be 3*\pgflinewidth instead of 4*\pgflinewidth. Why 3? Because 2 is too little and 4 is too much. ;-) OK, more seriously, only 50% of the line width is inside the node. So there are two errors going in the opposite directions. You did not take into account the outer boundaries, it seems, so you should have put 6*\pgflinewidth, but then you only need 50% of that which makes it 3*\pgflinewidth. Other than that you need to also set the text height if you set the text depth and want to get a certain target height.

\documentclass[]{scrartcl}
\usepackage{lmodern}
\usepackage[showframe=true,
width=18cm, height=26cm,
]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc, matrix}
\usepackage{tikzpagenodes}
\pagestyle{empty}


\pgfmathsetlengthmacro\InnerSep{5pt}
\pgfmathsetlengthmacro\ColumnSep{3mm}

\begin{document}
\begin{tikzpicture}[
shift={(current page text area.center)},
overlay,remember picture,
nodes={inner sep=0pt,outer sep=0pt},
Header/.style={
nodes={draw=blue, 
minimum width=3cm, minimum height=3cm, text width=3cm-\InnerSep, }
},
]
% For comparison  %%%%%%%%%%%%%%%%%%
\node[anchor=north west, inner sep=0pt,outer sep=0pt,
cyan, draw, 
minimum width=1.0\textwidth, 
%text width=1.0\textwidth-\InnerSep,
text height=5.5cm,
minimum height=6cm,
%text depth=6cm-4mm,
align=left, 
] at (-0.5\textwidth, 0.5\textheight) (Textbox) {For comparison: This dimension should the matrix have!};
%%%%%%%%%%%%%%%%%%%%%%%%%

% Textbox
\matrix[matrix of nodes, nodes in empty cells,
%inner sep=\pgflinewidth,
column sep=\ColumnSep, 
anchor=north west, 
draw=red, 
nodes={inner sep=\InnerSep, outer sep=0pt, anchor=north west, align=center},
row 1 column 1/.style={Header, draw, very thick}, 
row 1 column 2/.style={Header}, 
row 1 column 3/.style={
nodes={text=blue, draw=blue, align=left, 
minimum width=\textwidth-2*3cm-2*\ColumnSep-2*\InnerSep-3*\pgflinewidth, 
minimum height=3cm, 
text width=\textwidth-2*3cm-2*\ColumnSep-4*\InnerSep-3*\pgflinewidth, 
text height=0.5cm,
text depth=2.5cm-2*\InnerSep, 
}
}, 
] at (-0.5\textwidth, 0.5\textheight)
{
X & Y & 3 3 3 3 \\
1 & 2 & 3 \\
};
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • Aha... Seem the matrix itself (red line) has sth. like a 'inner sep', so the blue line of the last column is not exactly on the red line. How to get rid of that little difference? BTW: I am sorry I forgot to mark the 2nd problem (in the 2. picture) with the hight of the node in the last column. Did you see that the node is to height? – cis May 27 at 16:02
  • 2
    @cis Now I did. – user194703 May 27 at 16:12
  • Ah, very good. So the right blue line is not exactly on the red matrix-line: the problem should be, that I choose 'very thick' for the first node; so the value '3\pgflinewidth' is not correct either. But I think it should be good enough this way. – cis May 27 at 16:20
  • I do not unerstand, why you get the correct hight of the 3. node in row 1 by setting text depth=2.5cm-2*\InnerSep. I would have expected something like text depth=3cm-2*\InnerSep – cis May 27 at 19:04

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.