[Tex/LaTex] Cell lines of table not aligned cleanly

multicolumnmultirowtables

I'm trying to make a small table, but where only certain cell lines are drawn. I followed a general code outline I found elsewhere (the "Spanning in both directions simultaneously" part of this document: http://en.wikibooks.org/wiki/LaTeX/Tables), but when I compile, the ends of the lines defining the cells "overhang" – they don't match cleanly. (Disclaimer: I'm not sure this is the best way to draw this table, but it's what I've come up with.) Here is the code:

\documentclass{beamer}
\usepackage{multirow}
\begin{document} 
\centering
\begin{tabular}{ccc|c|}
& & \multicolumn{2}{c}{$Y$} \\ 
\cline{3-4}
& & \multicolumn{1}{|c|}{1} & 2  \\
\cline{2-4}
\multicolumn{1}{c}{\multirow{2}{*}{$T$}} &
\multicolumn{1}{|c|}{1} & $q_{11}$ & $q_{12}$ \\
\cline{2-4}
\multicolumn{1}{c}{} &
\multicolumn{1}{|c|}{2} & $q_{21}$ & $q_{22}$ \\ 
\cline{2-4}
\end{tabular}
\end{document}

enter image description here

It happens whether I compile with TeXnicCenter on a Windows PC or TeXShop on a Mac. The table doesn't look good whether I view the PDF after compiling within the program, or if I open it separately with Adobe.

Best Answer

In comments you say the lines look wore in your example, but I can't debug that (although the most common reason is having code after the last \\ and before \end{tabular} which makes a spurious unfinished last row so the vertical lines hang down.

But the rules in a latex table are always at the right edge of the column (except the first column which has two rules) so when using \multicolumn you should always add the rule on the right never on the left except the first column, otherwise, in general the vertical rules will not line up.

\documentclass{beamer}
\usepackage{multirow}
\begin{document} 
\centering

$\begin{array}{c|c|c|c|}
\multicolumn{2}{c}{}& \multicolumn{2}{c}{Y} \\ 
\cline{3-4}
\multicolumn{1}{c}{}&& 1 & 2  \\
\cline{2-4}
\multirow{2}{*}{T} &
1& q_{11} & q_{12} \\
\cline{2-4}
&
2 & q_{21} & q_{22} \\ 
\cline{2-4}
\end{array}$
\end{document}

enter image description here