2

From this question I learned how to create a table with labels for the rows and columns. But, I'm still struggling to get this to look exactly how I'd like.

I'd like to extend this solution to an arbitrary number of columns, for example I tried to extend it to 3 columns in the latex code below. But, it doesn't seem that I'm able to get the lines to also extend. Could someone help push me the rest of the way?

\begin{tabular}{*6c}
& &\multicolumn{4}{c}{$P$}\\
& & $A$ &$B$ & $C$ \\\cline{3-4}
\multirow{2}{*}& $A$ & \multicolumn{1}{|c|}{$0$} & \multicolumn{1}{c|}{$500$} & \multicolumn{1}{c|}{$500$} \\\cline{3-4}
& $D$ & \multicolumn{1}{|c|}{($100$)} &\multicolumn{1}{c|}{($200$)} & \multicolumn{1}{c|}{($200$)}\\\cline{3-4}
\end{tabular}

table

Mico
  • 506,678

3 Answers3

2

You could use tabularray:

\documentclass{article} 
\usepackage{tabularray}

\begin{document} \begin{table}[ht] \caption{Caption text here} [ \begin{tblr}{ colspec={{4}{c}}, hline{3-Z}={2-Z}{solid}, vline{2-Z}={3-Z}{solid}, } & \SetCell[c=3]{c} P \ & A & B & C \ A & 0 & 500 & 500 \ D & (100) &(200) & (200) \ \end{tblr} ] \end{table} \begin{table}[ht] \caption{If you need one column less} [ \begin{tblr}{ colspec={{3}{c}}, hline{3-Z}={2-Z}{solid}, vline{2-Z}={3-Z}{solid}, } & \SetCell[c=2]{c} P \ & A & B \ A & 0 & 500 \ D & (100) &(200) \ \end{tblr} ] \end{table} \end{document}

enter image description here

CarLaTeX
  • 62,716
2

With {NiceArray} of nicematrix and its built-in command \Block.

\documentclass{article}
\usepackage{nicematrix}
\setlength\extrarowheight{2pt}

\begin{document}

\begin{table}[ht] \caption{Caption text here} \centering $\begin{NiceArray}{ccccc} & \Block{1-3}{P}\[1ex] & A & B & C & \ A & \Block[hvlines]{2-3}{} 0 & 500 & 500 \ D & (100) &(200) & (200) \ \end{NiceArray}$ \end{table}

\end{document}

Output of the above code

F. Pantigny
  • 40,250
1

Your code specifies the table as having 6 columns; however, the screenshot suggest that there are just 4 columns. I can't detect a need for \multirow. And, since all of the table's contents seem to "math-y", I'd employ an array environment instead of a tabular environment.

The following code makes the necessary adjustments.

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage{array} % for '\extrarowheight' macro
\setlength\extrarowheight{2pt} % for a less cramped "look"
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro

\begin{document}

\begin{table}[ht] \caption{Caption text here} \centering $\begin{array}{*{4}{c|}} \mc{} & \multicolumn{3}{c}{P}\[1ex] \mc{} & \mc{A} & \mc{B} & \mc{C} \ \cline{2-4} A & 0 & 500 & 500 \ \cline{2-4} D & (100) &(200) & (200) \ \cline{2-4} \end{array}$ \end{table}

\end{document}

Mico
  • 506,678
  • Thanks. Now, is there a way to add a caption in this array environment? I'm not familiar with the array environment. In tabular it's pretty easy, but when I try to insert \caption{....} after \begin{array}, it doesn't work as expected. – underdog987 Dec 09 '23 at 23:14
  • @underdog987 - I've edited my answer to add some LaTeX code that (a) embeds the array in a table environment and (b) provides a dummy \caption statement. Note that it's the table environment, not the tabular environment, that provides the \caption machinery. – Mico Dec 10 '23 at 00:40
  • Thanks it works!! I'm trying to combine the the array that you gave me above, with a table (similar to the one in my original question) in a single figure. Ideally the table would be one subfigure, and the array would be a separate subfigure, and a caption for each one. I tried it myself, but I'm getting the error Not in outer par mode.. Is there an easy way to do this? I'm trying to avoid starting a new question unless you think it's necessary. Thanks for your help @Mico. – underdog987 Dec 10 '23 at 01:23
  • @underdog987 - So, basically, you want to place a table (with either a tabular or an array inside) next to a figure? Please confirm. – Mico Dec 10 '23 at 04:47
  • There are two things I want to display: a table (created using tabular), and an array (which apparently can be wrapped into a table environment as you showed above).
    Perhaps "figure" isn't the right word, but I just want them displayed next to each other with a separate caption for each, and one caption for both. I hope I'm using the terminology correctly here. I saw your answer here, and so I believe this can be done using subtable, but correct me if I'm wrong.
    – underdog987 Dec 10 '23 at 16:34
  • @underdog987 - We appear to have missed each other in the chat room. Anyway, here is what I wrote: "I think there may be some confusion because of LaTeX terminology/jargon. 'tabular' and 'array' environments are both "tables" in ordinary (non-LaTeX) parlance. A LaTeX 'table' is a particular type of a LaTeX "float" (sorry, more jargon). A 'table' environment generally contains one or more tabular and/or array environments. If you load the 'subcaption' package, you can make use of its 'subtable' machinery." (continued) – Mico Dec 10 '23 at 17:42
  • @underdog987 -- (continued) If you want, you can place two subtable environments side by side, and make each one contain a tabular or array envrionment along with an associated \caption directive." – Mico Dec 10 '23 at 17:43