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.

I want to create two tables that have the same width of columns. Both tables consist of 6 rows and 3 columns. In both tables, row 1 and row 4 are explanatory, and should expand in all columns. In the first table, rows 2, 3, 5, and 6 should have three columns of the same width. In the second table, rows 2 and 3 should have 2 columns of the same width, and rows 5 and 6 should have three columns of the same width.

This is how I've made the first table:

    \begin{table}[h]
        \caption{Distribution of mappings for the SLA parameter $\pi$.}
        \label{tab:distPi} \centering
        \begin{tabular*}{0.45\textwidth}{@{\extracolsep{\fill}}|c|c|c|}
            \hline
                \multicolumn{3}{|c|}{\textbf{Name of the SLA parameter $\pi$}} \\
            \hline
                \emph{Cost} & \emph{Charge} & \emph{Rate} \\
            \hline
                15\% & 15\% & 40\% \\
            \hline
            \hline
                \multicolumn{3}{|c|}{\textbf{Unit of the SLA parameter $\pi$}} \\
            \hline
                \emph{USD} & \emph{GBP} & \emph{YPI} \\
            \hline
                38\% & 2\% & 40\% \\
            \hline
        \end{tabular*}
        \vspace{-0.3cm}
    \end{table}

and this is how I've made the second table:

    \begin{table}[h]
        \caption{Distribution of mappings for the SLA parameter $\mu$.}
        \label{tab:distMu} \centering
        \begin{tabular*}{0.45\textwidth}{@{\extracolsep{\fill}}|c|c|c|c|c|c|}
            \hline
                \multicolumn{6}{|c|}{\textbf{Name of the SLA parameter $\mu$}} \\
            \hline
                \multicolumn{3}{|c|}{\emph{MemoryConsumption}} & \multicolumn{3}{|c|}{\emph{Consumption}} \\
            \hline
                \multicolumn{3}{|c|}{70\%} & \multicolumn{3}{|c|}{30\%} \\
            \hline
            \hline
                \multicolumn{6}{|c|}{\textbf{Unit of the SLA parameter $\mu$}} \\
            \hline
                \multicolumn{2}{|c|}{\emph{Mbit}} & \multicolumn{2}{|c|}{\emph{Gbit}} & \multicolumn{2}{|c|}{\emph{Tbit}} \\
            \hline
                \multicolumn{2}{|c|}{5\%} & \multicolumn{2}{|c|}{55\%} & \multicolumn{2}{|c|}{40\%} \\
            \hline
        \end{tabular*}
        \vspace{-0.3cm}
    \end{table}

However, the formatting is not good, as you can see in the given screenshot.

alt text

Does anyone have an idea how to do it better? The page is two-columned and the tables will be in one of the two columns. Their width should be ca. 90% of the width of the column.

share|improve this question
2  
Ta-da! You now have enough rep to post the screenshot. –  Andrew Stacey Dec 9 '10 at 11:23
    
Thanks, my life finally makes sense. :) –  Ivan Dec 9 '10 at 11:35
add comment

2 Answers

I'm sure there are many ways to skin this particular cat. The following short introduction to the awesomeness that is \halign might hopefully be helpful if it works in LaTeX.

I don't personally fancy "boxing" up the contents in a table.

tl;dr:

{
  \def\trule{\noalign{\medskip\hrule\medskip}}
  \def\header#1#2{\multispan#1\bf#2\hfill\cr\trule}
  $$
    \vbox{\hrule height1pt\medskip\halign to.4\hsize{
      #\hfil\tabskip.2\hsize plus.1\hsize minus.15\hsize&&#\hfil\cr
      % double ampersands begin the repeating part of the preamble.
      \header{3}{Name of the SLA parameter $\pi$}
      \it Cost&\it Charge&\it Rate\cr
      15\%&5\%&40\%\cr
      \noalign{\bigskip}
      \header{3}{Unit of the SLA parameter $\pi$}
      \it USD&\it GBP&\it YPI\cr
      38\%&2\%&40\%\cr}
    \medskip\hrule height1pt}
  $$
}
% end tl;dr
%
% General remarks about horizontal alignment ("halign" or "table" for short):
% "&" is used to separate columns.
% "\cr" is an abbreviation for "Carriage Return". It ends a row.
% "#" is used in the preamble to tell where the given content goes. Compare
% with positional parameters in, eg. `\def\foo#1{<...>#1<...>}`.
%
\vsize=.7\vsize
\input eplain
% eplain is input purely for the following command:
\doublecolumns
% Some text to show the default vertical spacing.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas pulvinar
tristique felis, a mollis ante tincidunt eu. Maecenas orci quam, auctor vitae
auctor quis, rhoncus quis nisi. Curabitur tincidunt consectetur turpis, vel
eleifend neque tristique faucibus. Praesent tristique vehicula nisl sed
pharetra.
\bigskip
{% Begin a grouping scope, so that all the settings we define here are confined.
\offinterlineskip% Omit whitespace between rows. Without this setting, there are
                 % gaps between vertical rules.
\tabskip0pt% Tabskip is a glue which will be put in every column.
           % For the beginning of the first column, we don't want any
           % horizontal space, so we set the tabskip to 0pt (it already is by
           % default, but lets be sure about it).
\everycr={\noalign{\hrule}}% For every "Carriage Return", insert a horizontal rule.
\halign to .9\hsize{% Begin the preamble of the table. A preamble defines how the
                    % columns of a table are formatted.
  \strut#&% A strut is a invisible "thing", whose height is the height of a
          % paren. We insert a strut as the first column because we have set
          % the whitespace between rows to be zero for the vertical rules, so
          % we need to make a little vertical room for the actual content.
  \vrule#\tabskip1em plus 2em minus .5em&% We set the glue between the columns so
    % that there is stretchability for the columns.
  \hfil#\hfil&% First (actual content) column is defined to be centered.
  \vrule#&% After the content column, there is a vertical rule.
  \hfil#\hfil&\vrule#&\hfil#\hfil&% Ditto for the other two content columns.
  \vrule#\tabskip0pt% For the last vertical rule -column, we set the
                    % stretchability back to 0pt, so that it doesn't "eat up"
                    % the stretchability of the content columns.
  \cr% End the preamble of the table with a "Carriage Return".
  % Now starts the actual contents of the table:
  &&% Skip the strut-column and the vrule-column.
  \multispan5% Let this column span five columns (content-columns and their
             % accompanied vrule-columns, excluding the last vrule-column)
  \hidewidth% Skip this columns width in the calculations of the column widths,
            % because this is a "header" -column/-row.
  \hfil\bf Name of the SLA parameter $\pi$\hfil% We again center the contents.
  \hidewidth% We need to hide the width of this column on its right-hand-side
            % as well.
  &\cr% Finally we are done with the first header-row. The seemingly surplus
      % column-separation ampersand is there to account for the last
      % vrule-column.
  &&\it Cost&&\it Charge&&\it Rate&\cr% The double ampersands account for the
                                      % vrule-columns.
  &&15\%&&5\%&&40\%&\cr
  \noalign{\smallskip\hrule}% Make a small gap before the next header-row.
  &&\multispan5\hidewidth\hfil\bf Unit of the SLA parameter $\pi$\hfil\hidewidth&\cr
  &&\it USD&&\it GBP&&\it YPI&\cr
  &&38\%&&2\%&&40\%&\cr
}% End of the halign.
}% End of the grouping scope. After the scope, TeX no longer knows what `\cc`
 % or `\header` means. Also the tabskip and interlinespacing are returned.
\bigskip
%
% Now that we're familiar with the basics (and some of the not-so-basics) of
% halign, we could further simplify the recurring commands, as well as separate
% the used dimens for re-usability.
%
\def\mytablehelper{
% First off, the dimens(ions):
\dimen0=.9\hsize% We'll use this dimen to set the width of the table.
\skip0=1em plus2em minus.5em% We'll use this glue to set the tabskip amount.
\offinterlineskip
\tabskip0pt
\everycr={\noalign{\hrule}}
\smallskipamount=2pt% remove stretchability
}
%
% For the recurring commands, there are at least the centered content-columns:
\def\cc#1{\hfil#1\hfil}
% and the header-rows:
\def\header#1#2{\multispan#1\hidewidth\cc{\bf#2}\hidewidth}
% and we might as well throw in the header separators in the mix:
\def\headsep{\noalign{\smallskip\hrule}}
%
% As we're getting greedier and greedier, let's wrap the whole thing in a `\def`:
\def\mytablethreecols#1{\centerline{\vbox{\mytablehelper
  \halign to\dimen0{\strut##&\vrule##\tabskip=\skip0&
  \cc{##}&\vrule##&\cc{##}&\vrule##&\cc{##}&\vrule##\tabskip=0pt\cr
  #1}}}}
% Looking ahead, we're going to need one with just two cols as well. I know. Ugly.
\def\mytabletwocols#1{\centerline{\vbox{\mytablehelper
  \halign to\dimen0{\strut##&\vrule##\tabskip=\skip0&
  \cc{##}&\vrule##&\cc{##}&\vrule##\tabskip=0pt\cr
  #1}}}}
%
% And here we go:
\mytablethreecols{
  &&\header{5}{Name of the SLA parameter $\pi$}&\cr
  &&\it Cost&&\it Charge&&\it Rate&\cr
  &&15\%&&5\%&&40\%&\cr
  \headsep
  &&\header{5}{Unit of the SLA parameter $\pi$}&\cr
  &&\it USD&&\it GBP&&\it YPI&\cr
  &&38\%&&2\%&&40\%&\cr
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas pulvinar
tristique felis, a mollis ante tincidunt eu. Maecenas orci quam, auctor vitae
auctor quis, rhoncus quis nisi. Curabitur tincidunt consectetur turpis, vel
eleifend neque tristique faucibus. Praesent tristique vehicula nisl sed
pharetra.
\smallskip
\mytabletwocols{
  &&\header{3}{Name of the SLA parameter $\mu$}&\cr
  &&\hidewidth\it MemoryConsumption\hidewidth
  &&\hidewidth\it Consumption\hidewidth&\cr
  &&70\%&&30\%&\cr
}% Ewww, two *different* tables!
\vskip1pt
\mytablethreecols{
  &&\header{5}{Unit of the SLA parameter $\mu$}&\cr
  &&\it Mbit&&\it Gbit&&\it Tbit&\cr
  &&5\%&&55\%&&40\%&\cr
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas pulvinar
tristique felis, a mollis ante tincidunt eu. Maecenas orci quam, auctor vitae
auctor quis, rhoncus quis nisi. Curabitur tincidunt consectetur turpis, vel
eleifend neque tristique faucibus.

\bye

share|improve this answer
    
Isn't there a package you could have used for the "lorum ipsum" bit? –  SamB Dec 17 '10 at 2:25
    
@SamB: plain doesn't have "packages" in the same sense that latex has. Loading (some) latex packages is possible with eplain, but I couldn't get it to load lipsum. I didn't feel that the text-parts take up unreasonable amount of space. –  morbusg Dec 17 '10 at 8:36
    
oh; I somehow managed to miss that this was in plain. –  SamB Dec 17 '10 at 17:31
add comment

Here's my ugly solution....I managed to make the formatting better...though I don't like the implementation details that much.

\documentclass[10pt,letterpaper]{article}
\usepackage{array}

\newlength{\onesixth}
\setlength{\onesixth}{.1667 \linewidth}
\setlength{\onesixth}{.5 \onesixth} 

\newcolumntype{C}{>{\begin{minipage}{2\onesixth}\begin{center}}{c}<{\end{center}\end{minipage}}}
\newcolumntype{D}{>{\begin{minipage}{3\onesixth}\begin{center}}{c}<{\end{center}\end{minipage}}}
\newcolumntype{E}{@{}l@{}} %I don't understand why this is needed...but without it the code doesn't compile

\begin{document}

 \begin{table}[h]
        \caption{Distribution of mappings for the SLA parameter $\pi$.}
        \label{tab:distPi}\centering
        \begin{tabular}{|C|C|C|E} 
            \hline
                \multicolumn{3}{|c|}{\textbf{Name of the SLA parameter $\pi$}} 
                \\\hline 
                  \emph{Cost} & \emph{Charge} &\emph{Rate}&
            \\\hline
                1 & 15 & 40 &\\
            \hline
            \hline
                \multicolumn{3}{|c|}{\textbf{Unit of the SLA parameter $\pi$}} \\
            \hline
                \emph{USD} & \emph{GBP} & \emph{YPI}& \\
            \hline
                38\% & 2\% & 40\% &\\
            \hline
        \end{tabular}
        \vspace{-0.3cm}
    \end{table}
    \begin{table}[h]
        \caption{Distribution of mappings for the SLA parameter $\mu$.}
        \label{tab:distMu} \centering
        \begin{tabular}{|c|c|c|c|c|c|}
            \hline
                \multicolumn{6}{|c|}{\textbf{Name of the SLA parameter $\mu$}} \\
            \hline
                \multicolumn{3}{|D|}{\emph{MemoryConsumption}} & \multicolumn{3}{D|}{\emph{Consumption}} \\
            \hline
                \multicolumn{3}{|D|}{70\%} & \multicolumn{3}{D|}{30\%} \\
            \hline
            \hline
                \multicolumn{6}{|c|}{\textbf{Unit of the SLA parameter $\mu$}} \\
            \hline
                \multicolumn{2}{|C|}{\emph{Mbit}} & \multicolumn{2}{C|}{\emph{Gbit}} & \multicolumn{2}{C|}{\emph{Tbit}} \\
            \hline
                \multicolumn{2}{|C|}{5\%} & \multicolumn{2}{C|}{55\%} & \multicolumn{2}{C|}{40\%} \\
            \hline
        \end{tabular}
        \vspace{-0.3cm}
    \end{table}

        \end{document}

which results in alt text

I am unhappy with: not being able to have a \multicolumn{6}{|C|C|C|}{..} and by that pesky E that seems to be needed so that the endline doesn't make its way into the \begin{}...\end{} block. If someone knows how to remove either of these two, please let me know, or post a better solution.

Oh, BTW, MemoryConsumption is too big for the box I defined...I'm not sure how it will fit for you, but you can fiddle with it yourself.

share|improve this answer
add comment

Your Answer

 
discard

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