4

In a table with 4 rows and 4 columns I try to merge different cells in every row while preserving the table layout. As there are never more than 3 cells in a row, a table column seems to be dropped.

If I add an empty row containing 4 cells there are 4 columns. Question: is it possible to preserve all columns without adding an empty row? Thank you.

Here is my goal:

-------------------
| 1               |
-------------------
| 2          | 14 |
-------------------
| 3 | 7      | 15 |
-------------------
| 4 | 8 | 12      |
-------------------

And here, what I get:

 --------------
| 1          |
--------------
| 2     | 14 |
--------------
| 3 | 7 | 15 |
--------------
| 4 | 8 | 12 |
--------------

The code:

\documentclass[10pt]{article}
\usepackage{multicol}
\begin{document}

\section{A}
{\begin{tabular}{|l|l|l|l|}
  \hline \multicolumn{4}{|l|}{1} \\
  \hline \multicolumn{3}{|l|}{2} & 14 \\
  \hline 3 & \multicolumn{2}{|l|}{7} & 15 \\
  \hline 4 & 8 & \multicolumn{2}{|l|}{12} \\
  \hline
\end{tabular}}

\section{B}
{\begin{tabular}{|l|l|l|l|}
  \hline \multicolumn{4}{|l|}{1} \\
  \hline \multicolumn{3}{|l|}{2} & 14 \\
  \hline &   &   &   \\
  \hline 3 & \multicolumn{2}{|l|}{7} & 15 \\
  \hline 4 & 8 & \multicolumn{2}{|l|}{12} \\
  \hline
\end{tabular}}

\section{Goal}

I would like to have a table as in section B but without the third (empty) row.

\end{document}
2

The following is probably what you're after:

enter image description here

\documentclass[10pt]{article}
\begin{document}

\section{A}
\begin{tabular}{|l|l|l|l|}
  \hline \multicolumn{4}{|l|}{1} \\
  \hline \multicolumn{3}{|l|}{2} & 14 \\
  \hline 3 & \multicolumn{2}{|l|}{7} & 15 \\
  \hline 4 & 8 & \multicolumn{2}{|l|}{12} \\
  \hline
\end{tabular}

\section{B}
\begin{tabular}{|l|l|l|l|}
  & & \phantom{12} & \\[-\normalbaselineskip] % fake/empty row
  \hline \multicolumn{4}{|l|}{1} \\
  \hline \multicolumn{3}{|l|}{2} & 14 \\
  \hline 3 & \multicolumn{2}{|l|}{7} & 15 \\
  \hline 4 & 8 & \multicolumn{2}{|l|}{12} \\
  \hline
\end{tabular}

\section{Goal}

I would like to have a table as in section B but without the third (empty) row.

\end{document}​

The motivation behind this solution stems from what is commonly used in the tabbing environment where you specify the tabbing intervals and then \kill the row.

Note that you don't need multicol for this, since the default tabular is supported in LaTeX.

  • thanks for the multicol package remark! I like the fake row approach (because I must generate the table), will try out tomorrow. - I'm almost perplexed how fast and good in quality these tex.stackexchange answers are. Many thanks to all of you! – h2459jl Aug 15 '12 at 16:50
  • there were vertical lines (in table 2B), I used the following line to get rid of them:code\multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}\[-\normalbaselineskip]code – h2459jl Aug 16 '12 at 12:59
2

It's a feature of the underlying \halign primitive that any column boundary that is spanned in every row is removed from the column width calculation.

Perhaps the easiest thing is to change the first line to

  \hline \multicolumn{2}{|l}{1}&\multicolumn{1}{l}{} &\

enter image description here

1

It seems that you want to have a 4 column table, but only have 3 columns of data. If you know the width of the additional column you want, you can add that as \hphantom{14} (assuming that the width of the other column is equivalent to the width of the digits 14:

enter image description here

Note:

  • The additional \hspace*{\tabcolsep} is required to take into account the separation that is added in between column tables.

Code:

\documentclass[10pt]{article}
\usepackage{multicol}
\begin{document}

\section{A}
{\begin{tabular}{|l|l|l|l|}
  \hline \multicolumn{4}{|l|}{1} \\
  \hline \multicolumn{3}{|l|}{2} & 14 \\
  \hline 3 & \multicolumn{2}{|l|}{7} & 15 \\
  \hline 4 & 8 & \multicolumn{2}{|l|}{12} \\
  \hline
\end{tabular}}

\section{B}
{\begin{tabular}{|l|l|l|l|}
  \hline \multicolumn{4}{|l|}{1} \\
  \hline \multicolumn{3}{|l|}{2} & 14 \\
  \hline &   &   &   \\
  \hline 3 & \multicolumn{2}{|l|}{7} & 15 \\
  \hline 4 & 8 & \multicolumn{2}{|l|}{12} \\
  \hline
\end{tabular}}

\section{Goal}

I would like to have a table as in section B but without the third (empty) row.

\section{C}
{\begin{tabular}{|l|l|l|l|}
  \hline \multicolumn{4}{|l|}{1} \\
%  \hline 1 & & & \\
  \hline \multicolumn{3}{|l|}{2} & 14 \\
  \hline 3 & \multicolumn{2}{|l|}{7\hspace*{\tabcolsep}\hphantom{14}} & 15 \\
  \hline 4 & 8 & \multicolumn{2}{|l|}{12}  \\
  \hline
\end{tabular}}





\end{document}

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.