1

I want to create a table with the following layout

enter image description here

So far, I have produced this:

enter image description here

with the code at the end of this post. My problem is apparent: I want the columns with X and Y to have the same width (i.e. their individual widths should be half of the width of the 3rd column in the header row), but I don't know how to do this. What's a simple solution here?

\documentclass{article}
\usepackage{multirow,hhline}

\begin{document}
  \begin{table}[h!]
    \renewcommand{\arraystretch}{1.5}
    \begin{tabular}{c||c||c|c}
      \begin{tabular}{c}
        Some 
        \\
        text
      \end{tabular}
      &
      \begin{tabular}{c}
        Some more \\ text
      \end{tabular}
      &
      \multicolumn{2}{c}{%
        \begin{tabular}{c}
          Even more text
        \end{tabular}
      }
      \\
      \hhline{=||=||==}
      \multirow{3}{*}{ A }
      & item A1
      & X 
      & Y
      \\
      \cline{2-4}
      & item A2
      & X
      & Y
      \\
      \cline{2-4}
      & item A3
      &
      \multicolumn{2}{c}{%
        if $P = NP$: 42
      }
      \\
      \hhline{=||=||==}
      \multirow{2}{*}{ B }
      & item B1
      & \multicolumn{2}{c}{ Bla }
      \\
      \cline{2-4}
      & item B2
      & \multicolumn{2}{c}{ Blu }
    \end{tabular}
  \end{table}

\end{document}
1
  • using makecell package for breaking text in the headers instead of more tables -- and the following additional column syntax -- see the answer below -- the answer contains an edit to contain the table size to 42% of the textwidth
    – js bibra
    Jun 2 at 16:23
1

enter image description here

\documentclass{article}
\usepackage{multirow,hhline,makecell}

\begin{document}
  \begin{table}[h!]
    \renewcommand{\arraystretch}{1.5}
    \begin{tabular}{>{\centering\arraybackslash}p{0.25\textwidth}||>{\centering\arraybackslash}p{0.25\textwidth}||>{\centering\arraybackslash}p{0.25\textwidth}|>{\centering\arraybackslash}p{0.25\textwidth}}
      \makecell{ Some \\ text}
      &
      \makecell {Some more \\ text}
      &
      \multicolumn{2}{c}{%
        \begin{tabular}{c}
          Even more text
        \end{tabular}
      }
      \\
      \hhline{=||=||==}
      \multirow{3}{*}{ A }
      & item A1
      & X 
      & Y
      \\
      \cline{2-4}
      & item A2
      & X
      & Y
      \\
      \cline{2-4}
      & item A3
      &
      \multicolumn{2}{c}{%
        if $P = NP$: 42
      }
      \\
      \hhline{=||=||==}
      \multirow{2}{*}{ B }
      & item B1
      & \multicolumn{2}{c}{ Bla }
      \\
      \cline{2-4}
      & item B2
      & \multicolumn{2}{c}{ Blu }
    \end{tabular}
  \end{table}

\end{document}

EDIT

If you want to contain the width of the table you could reduce the size of each column

enter image description here

\documentclass{article}
\usepackage{multirow,hhline,makecell}

\begin{document}
  \begin{table}[h!]
    \renewcommand{\arraystretch}{1.5}
    \begin{tabular}{>{\centering\arraybackslash}p{0.07\textwidth}||>{\centering\arraybackslash}p{0.15\textwidth}||>{\centering\arraybackslash}p{0.1\textwidth}|>{\centering\arraybackslash}p{0.1\textwidth}}
      \makecell{ Some \\ text}
      &
      \makecell {Some more \\ text}
      &
      \multicolumn{2}{c}{%
        \begin{tabular}{c}
          Even more text
        \end{tabular}
      }
      \\
      \hhline{=||=||==}
      \multirow{3}{*}{ A }
      & item A1
      & X 
      & Y
      \\
      \cline{2-4}
      & item A2
      & X
      & Y
      \\
      \cline{2-4}
      & item A3
      &
      \multicolumn{2}{c}{%
        if $P = NP$: 42
      }
      \\
      \hhline{=||=||==}
      \multirow{2}{*}{ B }
      & item B1
      & \multicolumn{2}{c}{ Bla }
      \\
      \cline{2-4}
      & item B2
      & \multicolumn{2}{c}{ Blu }
    \end{tabular}
  \end{table}

\end{document}
1
  • 1
    Very nice, thank you. I didn't know about makecell, certainly a lot better than an infinite amount of tabulars haha..
    – Jo Mo
    Jun 2 at 16:49
2

You can keep your code with c||c||c|c parameter of tabular and you can add the width to the X item manually:

\hbox to1.2cm{\hss X\hss} % instead X

This can be done only at one occurrence of X, the X's in next rows are centered automatically. Of course, you have to guess the appropriate value (1.2cm here), but guessed values are in the accepted solution too, note: p{0.07\textwidth}

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.