4

This is my very first TikZ diagram, so please bear with my beginner's ignorance of things that are probably very basic. I'm trying to get a diagram close to the following:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,
    column 1/.style={anchor=east,align=right,color=purple},
    column 2/.style={anchor=center,text centered,align=center},
    column 3/.style={anchor=center,text centered,align=center},
minimum width=2cm,minimum height=2cm] (a) {

row1 & \node[draw]{text1}; & \node[draw]{text2}; \\
row2 & \node[draw]{double\\text3}; & \node[draw]{text4}; \\
{} & \node[anchor=south]{col1}; & \node[anchor=south]{col2}; \\
};
  \node at (1,2.5) [fill=white!100] {\emph{is}};
  \node at (1,0.5) [fill=white!100] {\emph{is not}};
\end{tikzpicture}
\end{document}

Which gives me:

enter image description here

Which is almost what I want. But, I was trying to get the column desriptions closer to border of the node above, and couldn't figure it out. I know from the pgf-manual that in order to move it up I have to use the "south" anchor but for some reason it wouldn't move.

The second thing (clarified):
The vertical border between node "text1" and node "text2" should not be drawn and in the row below the vertical border between node "text3" and node "text4" should be a dotted line.

Any help much appreciated!

1
  • You may wanna ask a separate question for the dotted line Sep 19, 2016 at 8:32

2 Answers 2

3

After your clarification I would suggest this:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[% define styles for row labels and col labels here
    row style/.style={color=purple},%
    col style/.style={},%
]
\matrix[matrix of nodes,%
    column 1/.style={anchor=center,text centered,align=center},%
    column 2/.style={anchor=center,text centered,align=center},%
    minimum width=2cm,minimum height=2cm] (m) {%
  text1 &  text2 \\
  text3 &  text4 \\
};
  % column labels
  \node[col style,below=2mm of m-2-1] {col1}; 
  \node[col style,below=2mm of m-2-2] {col2};

  % row labels
  \node[row style,left=2mm of m-1-1] {row1}; 
  \node[row style,left=2mm of m-2-1] {row2};  

  % border around matrix
  \draw (m-1-1.north west) rectangle (m-2-2.south east); 

  % vertical dotted line
  \draw[dotted] (m-2-1.north east) -- (m-2-1.south east);

  % horizontal line
  \draw (m-1-1.south west) -- (m-1-2.south east);

  % place label between text1 and text2
  \node at ($(m-1-1)!.5!(m-1-2)$) [fill=white!100] {\emph{is}};

  % place label between text3 and text4
  \node at ($(m-2-1)!.5!(m-2-2)$) [fill=white!100] {\emph{is not}};  
\end{tikzpicture}
\end{document}

enter image description here

3
  • I do like the idea of both column and row labels placed independently of the matrix. I've clarified what I meant with dotted line in the question.
    – jan
    Sep 19, 2016 at 9:11
  • Quick additional question: I was now trying to put the "is not" on two lines but {is\\not} doesn't work Do you have an idea why? (It worked in the original example for text3)
    – jan
    Sep 19, 2016 at 9:56
  • 1
    You need to tell tikz how to align the text e.g. \node[align=center] at ($(m-2-1)!.5!(m-2-2)$) [fill=white!100] {\emph{is} \\ \emph{not}}; Sep 19, 2016 at 10:03
5

An easy way of bringing those two nodes up is to reduce the row spacing using e.g. \\[-10pt]. See code below, with some comments here and there.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,
    column 1/.style={anchor=east,align=right,color=purple,minimum width=1cm}, % added minimum width=1cm here
    column 2/.style={anchor=center,text centered,align=center},
    column 3/.style={anchor=center,text centered,align=center},
    minimum width=2.5cm, % increased this to 2.5cm
    minimum height=2cm] (a) {
row1 & text1 & text2 \\  % node [-8pt] added
row2 & |[text width=15mm]| {double\\text3} & text4 \\[-15pt] % note [-15pt] added
{} & col1 & col2 \\
};



\draw (a-2-2.south west) rectangle (a-1-3.north east); % draw border around cells
\draw (a-1-2.south west) -- (a-1-3.south east); %  line between rows
\draw [dashed] (a-2-2.north east) -- (a-2-2.south east); % dashed vertical line

% place is,is not nodes midway between the text nodes
\node [fill=white!100] at (a-1-2.east) {\emph{is}};
\node [fill=white!100,align=center,font=\em] at (a-2-2.east) {is\\not};
\end{tikzpicture}
\end{document}
6
  • I think \node (a-2-2) {double\\text3}; should be {double\\text3}.
    – Ignasi
    Sep 19, 2016 at 8:54
  • @Ignasi I tried, it didn't work. But I'll double check. Sep 19, 2016 at 8:55
  • Nice trick: minimum width|height|size applied to matrix is also applied to inner nodes. I'd never thought it!
    – Ignasi
    Sep 19, 2016 at 8:55
  • @Ignasi That's from the original code. I'm a bit surprised it works, would have thought that using nodes={minimum size=..} would be needed. {double\\text} didn't work. Sep 19, 2016 at 8:58
  • I've tested and I don't know why but adding a text width {double\\text} works: |[text width = 15mm]|{double\\text}, you can also apply to nodes={...}.
    – Ignasi
    Sep 19, 2016 at 9:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .