1

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.

4
  • Thanks for adding the picture, @David Carlisle. However, your picture appears fine. This is why I'm confused - I wonder if it's my specific program/computer that causes this?
    – Meg
    Apr 2, 2014 at 18:49
  • Additionally, in reality, I have other content in the frame in addition to this table, and when I compile that frame, the lines "hang off" horizontally, not vertically (as I personally get when I compile the code I provided above).
    – Meg
    Apr 2, 2014 at 18:50
  • well the pic was made from an unedited version of your code. Try doing as I did and cut and paste your example off the website into a new document and try that. Perhaps you have some invisible control characters messing up your table? Apr 2, 2014 at 18:56
  • No, strangely enough, it still has overhanging lines even when I copy and paste it from here, and put it in an entirely new window within TeXnicCenter, that I just opened and saved fresh for this purpose.
    – Meg
    Apr 2, 2014 at 19:39

2 Answers 2

5

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

1
  • Perfect! Than you so much! You did what I wanted to do, but didn't understand how to do: I wanted to get rid of the two & on the first line so I could indeed specify $\begin{array}{c|c|c|c|}$ as you did, but didn't think to use multicolumn in their place (obvious now). Thanks again!
    – Meg
    Apr 2, 2014 at 19:35
0

You can do that table easily with {NiceArray} of nicematrix.

\documentclass{beamer}
\usepackage{nicematrix}

\begin{document}

$\begin{NiceArray}{ccc}[first-row,first-col,corners=NW,hvlines]
&   & \Block{1-2}{Y}  \\
&   & 1      & 2      \\
\Block{2-1}{T}
& 1 & q_{11} & q_{12} \\
& 2 & q_{21} & q_{22} 
\end{NiceArray}$

\end{document}

Output of the first code

If you want all the columns with the same width, it's possible with the key columns-width=auto.

\documentclass{beamer}
\usepackage{nicematrix}

\begin{document}

$\begin{NiceArray}{ccc}%
  [first-row,first-col,corners=NW,hvlines,columns-width=auto]
&   & \Block{1-2}{Y}  \\
&   & 1      & 2      \\
\Block{2-1}{T}
& 1 & q_{11} & q_{12} \\
& 2 & q_{21} & q_{22} 
\end{NiceArray}$

\end{document}

Output of the second code

You must log in to answer this question.

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