Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
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! –  Hans-Peter 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 –  Hans-Peter Aug 16 '12 at 12:59
add comment

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

share|improve this answer
add comment

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.