1

I want to draw the following multiple column table in latex. The headings are centrally aligned and the other text is left aligned. How to do this? Here all fields such as One, Two etc may contain arbitrary amount of text.

enter image description here

My current attempt is as below. I further want to have:

(a) Two and Three split in the middle with respect to the text in column one.

(b) All text except headings to be left aligned

(c) A straight line between Two and Three

\documentclass{article}
\usepackage{blindtext}
\begin{document}
\begin{tabular}{|p{5cm}|c|c|c|}
\hline
\textbf{A} & \textbf{B} & \textbf{C} & \textbf{D}\\
\hline
\blindtext & \begin{tabular}{@{}c@{}}Two \\ Three\end{tabular} & Four & Six
\end{tabular}
\end{document}
6
  • How far did you get? Is field One having a predefined height, will it stay empty or be filled with text?
    – Johannes_B
    Feb 15, 2016 at 9:21
  • @Johannes_B: It will be filled with text. My attempt till now is that I have tried nesting a table within a table. The problem is that the table starts going out of the margin if too much text is filled in.
    – user8735
    Feb 15, 2016 at 9:23
  • Is the material in cells "One" and "Six" supposed to be able to span (vertically) the material in cells "Two"/"Three" and "Four"/"Five"?
    – Mico
    Feb 15, 2016 at 15:11
  • @Mico: All cells are supposed to span the material in cell One.
    – user8735
    Feb 16, 2016 at 4:05
  • @Shahab - I'm afraid I don't understand the "all cells" part of your reply. Why all cells? Why not just the ones in cells "Two" and "Three"? What do cells "Eight", "Nine", and "Ten" have to do with the height of cell "One"? Please advise.
    – Mico
    Feb 16, 2016 at 5:02

4 Answers 4

1
\documentclass{article}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{blindtext}
\begin{document}
\begin{tabular}{|p{5cm}|l|l|l|}
\hline
\multicolumn{1}{|c}{\textbf{A} }& \multicolumn{1}{|c|}{\textbf{B} }& \multicolumn{1}{c}{\textbf{C} }& \multicolumn{1}{|c|}{\textbf{D} }\\
\hline
\multirow{2}{*}{One} & Two  & Four & \multirow{2}{*}{Six}  \\ \cline{2-3}
 &  Three & Five & \\ \hline
Seven& Eight & Nine & Ten \\ \hline
\end{tabular}
\end{document}

enter image description here

5
  • 1
    Is the multicol package needed for this table?
    – Mico
    Feb 15, 2016 at 10:24
  • Yes. Because I have the alignment of the whole table (all columns left) and the first row I am using the multicol command to align it to the center. Maybe other solutions are possible. Feb 15, 2016 at 10:27
  • 4
    Peter, not needed. multicol is for multicolumn text. Nothing to do with tables.
    – Johannes_B
    Feb 15, 2016 at 10:37
  • 1
    Note that you're not using \multicol, but \multicolumn. A conclusive way of verifying that the multicols package is not needed is simply not to load it and recompile your code.
    – Mico
    Feb 15, 2016 at 14:55
  • \multicolumn doesn't require any package, only \multirow does.
    – Alenanno
    Feb 16, 2016 at 16:34
1

I'm interpreting your requirements as follows: (a) Column "A" should be half as wide as column "B", and together they should span the first half the available width; (b) columns "C" and "D" should be equally wide and span the second half; (c) the contents of all cells should be top-algned; and (d) the height of the middle row of cells is determined by whichever of cells "One" and "Six" is taller.

If this interpretation is correct, the following setup may be what you're looking for. The only shortcoming, in my view, is that the horizontal line that separates cells Two/Four from cells Three/Five isn't balanced automatically; depending on the contents of cells Two and Four, a \phantom instruction may be needed, as is demonstrated in the code below.

enter image description here

\documentclass{article}
\usepackage{array,ragged2e}
\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}

\newlength\tablengtho
\newlength\tablengtha
\newlength\tablengthb
\newlength\tablengthcd

% Calculare widths of columns A, B, and C/D
\setlength\tablengtho{\dimexpr\textwidth-8\tabcolsep-5\arrayrulewidth\relax}
\setlength\tablengtha{\dimexpr0.6667\tablengtho/4\relax}
\setlength\tablengthb{\dimexpr1.3333\tablengtho/4\relax}
\setlength\tablengthcd{\dimexpr\tablengtho/4\relax}

\begin{document}

\noindent
\begin{tabular}{| P{\tablengtha} | P{\tablengthb} |
                  P{\tablengthcd} | P{\tablengthcd} |}
\hline
\multicolumn{1}{|c|}{\textbf{A}} & \multicolumn{1}{c|}{\textbf{B}} &
\multicolumn{1}{c|}{\textbf{C}} & \multicolumn{1}{c|}{\textbf{D}} \\
\hline
One One One One One One One One One  & 
\multicolumn{1}{@{}c@{}|}{%
   \begin{tabular}[t]{P{\tablengthb}}
         Two Two Two Two Two Two Two   \\ 
         \hline
         Three Three Three Three Three \\
   \end{tabular}} &
\multicolumn{1}{@{}c@{}|}{%
   \begin{tabular}[t]{P{\tablengthcd}}
         Four Four Four \phantom{Four} \\ % \phantom{Four} needed for balance
         \hline
         Five Five Five \\
   \end{tabular}} &
Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six \\
\hline
Seven & Eight & Nine & Ten \\
\hline
\end{tabular}

\end{document}
1

I tried to do this with stables – Simplified Plain TEX tables which usually places text centered in each cell. I used 4 macros one for each row to control the horizontal size of the rows and to provide a way to control placing of text in each cell both horizontally and vertically.

\documentclass{article}

\input stables.tex

%the width of each column is controlled by the "\hsize=" in each of the four macros
%for each macro the first variable controls horizontal placement of the text, the second 
%takes the text itself and the third controls vertical placement of the text.
\def\AA#1#2#3{\hphantom{\hspace{#1}}% 
   \vtop{\hsize=1.2in\vskip#3 #2}\hphantom{\hspace{1pt}}}

\def\BB#1#2#3{\hphantom{\hspace{#1}}%
   \vtop{\hsize=2.2in\vskip#3 #2}\hphantom{\hspace{1pt}}}

\def\CC#1#2#3{\hphantom{\hspace{#1}}%
   \vtop{\hsize=1in\vskip#3 #2}\hphantom{\hspace{1pt}}}

\def\DD#1#2#3{\hphantom{\hspace{#1}}%
   \vtop{\hsize=1.4in\vskip#3 #2}\hphantom{\hspace{1pt}}}

\begin{document}

\begintable

A|B|C|D\elt
\multirow4{\AA{-.1cm}{One}{-.9cm}}|\BB{-.6cm}{Two}{-.2cm} |\CC{-.8cm}%
{Four}{-.2cm}|\multirow4{\DD{-.1cm}{Six}{-.9cm}}\elspec
|||\el
|||\el
|\trule|\trule| \el
|\BB{-.6cm}{Three}{-.4cm} |\CC{-.8cm}{Five}{-.4cm}|\el
|||\elt
\AA{-.9cm}{Seven}{-.2cm}|\BB{-.9cm}{Eight}{-.2cm}|\CC{-.8cm}{Nine}%  
{-.2cm}|\DD{-1cm}{Ten}{-.2cm}

\endtable

\end{document}

This was my result:

enter image description here

0

It is better to use package booktabs. Rules provide some space between rows.

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}
\begin{tabular}{p{5cm}|l|l|l}
\toprule
\multicolumn{1}{c}{\textbf{A} }& \multicolumn{1}{|c}{\textbf{B} }& \multicolumn{1}{|c}{\textbf{C} }& \multicolumn{1}{|c}{\textbf{D} }\\
\midrule
\multirow{2}{*}{One} & Two  & Four & \multirow{2}{*}{Six}  \\ \cmidrule{2-3}
&  Three & Five & \\ \midrule
Seven& Eight & Nine & Ten \\ \bottomrule
\end{tabular}
\end{document}

tblwithtabs

1
  • If one uses the line-drawing macros of the booktabs package, one should not use vertical lines. (One may verify this claim by reading the user guide of the booktabs package.)
    – Mico
    Feb 15, 2016 at 14:56

You must log in to answer this question.