2

I hope someone can help me with my problem. I have created a matrix using Tikz as I have to be able to cross out columns and rows, however, I cannot get rid of a double horizontal line LaTeX inserts in between the second and third row (but NOT in the first column) and I cannot see where it comes from. Except from that double line, the matrix is exactly the way I want it to be.

This is my code at the moment for the matrix:

\documentclass{article}
\usepackage[english]{babel} 
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\centering
\begin{tikzpicture}

 \matrix (magic) [%
   matrix of nodes,
   nodes=draw,
   text width=6mm,
   text  centered,
   column 1/.style={
    nodes={
        text width=20mm,
        minimum width=20mm,
        fill=white
    }
},
 ] {%
     & (dd) & (cd) & (dc) & (cc) \\
   cooperate & 0 & 4 & 0 & 4 \\
   defect & 1 & 6 & 1 & 6\\
 };

 \draw[thin,red] (magic-2-1.west) -- (magic-2-5.east);

\end{tikzpicture}
\end{document}
2

A tikz matrix is not a tabular, all nodes keep their size and the matrix just aligns them. In this case, cooperate nodes is higher than other nodes in its row. That's the reason for the double line. Lines are node's border not a (tabular) \hline. One solution consist in fixing a minimum height which forces all nodes to have similar size:

\documentclass{article}
\usepackage[english]{babel} 
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\centering
\begin{tikzpicture}

 \matrix (magic) [%
   matrix of nodes,
   nodes={anchor=center, draw, minimum height=8mm},
   text width=6mm,
   text  centered,
   column 1/.style={
    nodes={
        text width=20mm,
        minimum width=20mm,
        fill=white
    }
},
 ] {%
     & (dd) & (cd) & (dc) & (cc) \\
   cooperate & 0 & 4 & 0 & 4 \\
   defect & 1 & 6 & 1 & 6\\
 };

 \draw[thin,red] (magic-2-1.west) -- (magic-2-5.east);

\end{tikzpicture}
\end{document}

enter image description here

If you want that all lines show same width, insert row sep=-\pgflinewidth, column sep=-\pgflinewidth in matrix options.

0

As suggested by ferahfeza, adding text height=1.5ex,text depth=.25ex aligns your texts and solves your problem.

Ignasi arrived first, however, as he said, I've added row sep=-\pgflinewidth, column sep=-\pgflinewidth to avoid thick lines.

\documentclass{article} 
\usepackage[english]{babel}
\usepackage{tikz} 
\usetikzlibrary{matrix} 
\begin{document} 
    \centering 
    \begin{tikzpicture}[text height=1.5ex,text depth=.25ex] 
    \matrix (magic) [% 
        matrix of nodes, nodes=draw, text width=6mm, text centered,
        row sep=-\pgflinewidth, column sep=-\pgflinewidth,
        column 1/.style={ 
            nodes={ 
                text width=20mm, minimum width=20mm, fill=white 
                } 
            }, 
        ] {% 
            & (dd) & (cd) & (dc) & (cc) \\ 
            cooperate & 0 & 4 & 0 & 4 \\ 
            defect & 1 & 6 & 1 & 6\\ 
        }; 
    \draw[thin,red] (magic-2-1.west) -- (magic-2-5.east); 
    \end{tikzpicture}
\end{document}

enter image description here

  • @Inge You're welcome! Indeed, it's much better! – CarLaTeX Mar 23 '17 at 17:50

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.