1

I want to solve a problem where I want to use a table for illustration purposes. I have managed to solved the problem in a very cumbersome way but I think there has to be a "smart" way as well. My current solution is very fragile since it is dependent of sizes etc.

What I want (ASCII-style):

-----------------------------
|       Concept 1           |   <--- bold heading
-----------------------------
| C11  |   C12   |   C13    |   <--- three items in this row
-----------------------------   <--- in current solution, bot tab 1 and top tab 2
|  C2   |   |     C4        |   <--- bold heading
--------| C3|----------------   <--- C3 takes two lines, since no subconcepts
|C21|C22|   |C41|C42|C43|C44|   <--- seven items in this row
-----------------------------   <--- in current solution, bot tab 2 and top tab 3
|       Concept 5           |   <--- bold heading
-----------------------------
| C51  |   C52   |   C53    |
-----------------------------

I have abbreviated for space above. C2 => Concept 2, C11 => Concept 1 part 1 etc.

The above ASCII table I how I would have like to have it. And now I've solved it by making one table environment with three tabulars and playing around with parskips to make it look as one tabular.

Concepts 2, 3 and 4 belong together, so I don't want to change positions.

I guess it could be possible in one tabular with 21 columns (i.e. 7*3 to account for possible positions), and use multicol for everything. But surely there must be a better way?

Packages etc don't matter, but would be nice if it compiled in a memoir document.

Current solution psuedo-code:

begin{table}
  begin{tabularx}{*{3}{c}}
    multicol{3}{Concept 1} \\
    C11 & C12 & C13 \\
  end{tabularx}
  % Magic \par\vskip 
  begin{tabularx}{*{7}{c}}
    multicol{1}{Concept 2} & multirow{*}{2}{Concept 3} & multicol{4}{Concept 4} \\
    C21 & C21 & & C41 & C42 & C43 & C44 \\
  end{tabularx}
  % Magic \par\vskip
  begin{tabularx}{*{3}{c}}
    multicol{3}{Concept 5} \\
    C51 & C52 & C53 \\
  end{tabularx}
end{table}
  • 1
    There are thousands of examples in this site and elsewhere of how write LaTeX tables with \multicolumn (e.g. \multicolumn{2}{c}{text}) and \multirow (e.g. \multirow{2}{*}{text}, need the multirow package) . For the bold headings is enough change the cell content to \textbf{Concept 1}. After read something about these commands, edit the question to change the pseudocode with a real code in a compilable minimal working example (MWE) with you best try, if you still have some problem. – Fran Nov 8 '17 at 8:23
  • @Fran I think the difficulty lies in having an equally spaced 3 cell row and having an equally spaced 7 cell row in the same table, without using a ridiculous number of columns (21). – Dan Nov 8 '17 at 13:51
  • @Dan I am not so sure if the OP only show a pseudo code. I agree that 21 is a ridiculous number of columns, but that is not a difficulty, is a symptom of breach the KISS principle. – Fran Nov 8 '17 at 18:04
  • @Fran I intentionally used pseudo-code, because I only wanted to show the principle of the solution I used. Problem being that the only other solution I could imagine involving crazy amounts of columns. – flindeberg Nov 8 '17 at 23:01
  • @Dan Exactly :-) – flindeberg Nov 8 '17 at 23:04
2

There should be things that could not be done with LaTeX, and this is one of them.

enter image description here

\documentclass{article}
\let\xx\bfseries
\usepackage{array,tabulary,tabularx,multirow}
\newcolumntype{Y}{>{\arraybackslash\centering}X}
\renewcommand\arraystretch{1.4}
\begin{document}
\begin{tabulary}{\linewidth}{|c|c|c|}\hline
\multicolumn{3}{|c|}{\xx Concept 1}\\\hline
\multicolumn{3}{|c|}{\begin{tabularx}{\linewidth}{Y|Y|Y} 
C11 & C12 & C13 \end{tabularx}}\\\hline
\xx C2 & \multirow{2}{*}{\xx C3} & \xx C4 \\\cline{1-1}\cline{3-3}
\begin{tabularx}{.4\linewidth}{Y|Y} C21 & C22 \end{tabularx} 
& &
\begin{tabularx}{.5\linewidth}{Y|Y|Y|Y} 
C41 & C42 & C43 & C44 \end{tabularx} \\\hline
\multicolumn{3}{|c|}{\xx Concept 5}\\\hline
\multicolumn{3}{|c|}{\begin{tabularx}{\linewidth}{Y|Y|Y} 
C51 & C52 & C53 \end{tabularx}}\\\hline
\end{tabulary}
\end{document}
  • Is there a way to remove the .4\linewidth and .5\linewidth or is the only other solution to give them one third of the width each? – flindeberg Nov 8 '17 at 23:13
  • @flindeberg You must use that two lengths if you want control how wide should be the subconcepts cells of C2 and C4, but not necessarily with these values. For example try with .15\linewidth and .8\linewidth and you will see the effect. – Fran Nov 9 '17 at 0:17
1

Since virtually every single row uses a different column width, I used \makeboxes. The key is knowing the width of the widest line ahead of time.

You could do it with multiple tabularx environments, it would be just as busy.

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}
  \sbox0{\begin{tabular}{*7c}% get widest line
    C21 & C21 & Concept 3 & C41 & C42 & C43 & C44 
    \end{tabular}}
  \begin{tabular}{|c|}% wrapper
    \makebox[\wd0]{Concept 1}\\
    \makebox[.333\wd0]{C11}\makebox[.333\wd0]{C12}\makebox[.333\wd0]{C13}\\
    % Magic \par\vskip 
     \begin{tabular}{*7c}
      \multicolumn{2}{c}{Concept 2} & \multirow{2}{*}{Concept 3} & \multicolumn{4}{c}{Concept 4} \\
      C21 & C21 & & C41 & C42 & C43 & C44 \\
     \end{tabular}\\
    \makebox[\wd0]{Concept 5}\\
    \makebox[.333\wd0]{C51}\makebox[.333\wd0]{C52}\makebox[.333\wd0]{C13}\\
  \end{tabular}
\end{table}
\end{document}

demo

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.