1

I am attempting to create a table in which cells of one of the columns contains a nested table as the cell value.

So far I have got this:

\documentclass{article}
\usepackage{array,multirow}
\begin{document}
\begin{table}
    \begin{tabular}{|c|cccc|c|}\hline
    Col. A & Col. B & & & & Col. C \\ \cline{1-6}
\multirow{4}{*}
    {Val. 1} 
    &   & a & b & c \\
    & b & 2 &   &   \\
    & c & 2 & 3 &   \\
    & d & 2 & 3 & 4 \\ \hline
\multirow{4}{*}
    {Val. 2} 
    &   & a & b & c \\
    & b & 2 &   &   \\
    & c & 2 & 3 &   \\
    & d & 2 & 3 & 4 \\ \hline
\end{tabular}
\end{table}
\end{document}

which creates something like this:

enter image description here

A few things I want to fix/improve on this:

  1. Despite having only 3 columns, I'd like to extend the table to cover the full width of text on the page? Does not need to span across the whole text width, but outer columns 'A' and 'C' should handle multi-line values comfortably.

  2. In Col. B, I'd like to "prettify" the inner table structure so that it becomes a 4x4 square shape with equal spacings between rows and columns, and the whole table centered within the cell.

  3. How do I center Col. B text in the table header, and center the values (possibly multi-line) of Col. C both horizontally and vertically in the cell?

If possible, I'd like to use as few extra package as possible. So if this can be done with array and multirow (and possibly multicol), then let's use them only.


UPDATE: thanks to @DavidCarlisle's answer, I produce the following in my output:

enter image description here

How can we make the shape of the nested tables in Col. B square? Can we set the width of each outer column somehow?

3
  • Please tell us more about the intended structure. E.g., should the outer columns (A, B, and C, be equally wide? If not, how should the widths be apportioned? What is column C supposed to contain? Are the vertical bars required, or may they be omitted? – Mico Nov 20 '14 at 18:29
  • @Mico, on a second thought, the whole table does not need to span across the whole text width (I will update my question to reflect this). There is no requirement on the widths of the outer columns, except that B needs to accommodate the inner table. The outer column C will contain number values, but there may be multiple of them for a single cell, so some flexibility of position these values may be desirable. Sorry about the vertical bars of the C column, but yes, they should be there to complete the table. – Skyork Nov 20 '14 at 18:39
  • your version looks very spaced out vertically, presumably not using the posted code, do you have doublespacing or arraystretch or ... – David Carlisle Nov 20 '14 at 20:10
3

enter image description here

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{table}
\setlength\extrarowheight{2pt}
    \begin{tabular}{|c|c|c|}\hline
    Col. A & Col. B &  Col. C \\ \hline
Val. 1&
$\begin{array}{@{}cccc@{}}
 & a & b & c \\
 b & 2 &   &   \\
 c & 2 & 3 &   \\
 d & 2 & 3 & 4
 \end{array}$& \\ \hline
    Val. 2&
$\begin{array}{@{}cccc@{}}
    & a & b & c \\
  b & 2 &   &   \\
  c & 2 & 3 &   \\
  d & 2 & 3 & 4 
\end{array}$&\\ \hline
\end{tabular}
\end{table}
\end{document}
3
  • thanks for the answer. One minor thing, how do I control the width of each outer column? At the moment, it seems they are controlled by the width of the widest content in a cell of the column? – Skyork Nov 20 '14 at 18:52
  • I've updated the question with my output using your solution, and there seems some difference in the width of Col. B between your and my outputs. Any suggestion how to adjust the width of the columns so that the nested table in Col. B looks square? Thanks. – Skyork Nov 20 '14 at 19:04
  • 1
    @Skyork just \renewcommand\arraystretch{1.1} or whatever factor is needed (but the version you post has already been grossly stretched, how come it is taller than it is wide? – David Carlisle Nov 20 '14 at 20:11

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.