4

I am new to Latex and need help drawing tables. Here is what I have right now: enter image description here

and here is the code for it:

\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
Topology & Average Delay(s) & Average Bandwidth(kbps) & Hosts \\
\hline
Low & 0.0994 & 2441.85 & \multirow{2}{*}{64} \\
High & 0.1424 & 2515.77  \\ 
\hline
Low & 0.2522 & 1798.60 & \multirow{2}{*}{128} \\
High & 0.1113 & 2123.17  \\
\hline
\caption{This table shows}
\label{eval_table}
\end{tabular}
\end{center}

Here is is what I want to do:

1) Shrink the table width so that it matches the column width

2) Fix the last vertical line

3) Set up the caption properly

I have spent hours on this and I don't know how to fix it.

7
  • FWIW, don't use vertical rules in tables. See page 3 of the booktabs manual.
    – You
    Jul 18, 2012 at 9:10
  • Please provide information on the width of the textblock. I'm afraid it's not possible to tell this important piece of information from your code fragment.
    – Mico
    Jul 18, 2012 at 9:14
  • 1
    As you said you are new to LaTeX, maybe you do not yet know there exist tools like excel2latex that make producing tables a lot easier. Most of those tools are discussed in this question: tex.stackexchange.com/questions/49414/…
    – matth
    Jul 18, 2012 at 11:53
  • @Mico: The width of the text block is the standard two column width used by IEEE conference papers
    – Bruce
    Jul 18, 2012 at 16:13
  • Thanks for this follow-up. In this case, either the second or third method I show in my answer should do the job for you.
    – Mico
    Jul 18, 2012 at 19:46

3 Answers 3

5

I'm afraid that because you've provided only a code snippet rather than a full MWE (minimum working example), some vital information -- the width of the text block of your document -- is not available. I've thus had to make some (possibly incorrect) assumptions: A4 paper size, 1-inch margins, two-column mode.

Below is a succession of tables that address various issues you raise. I've deliberately not centered the tables to make it easier to compare the widths of the tables. (In a real document, you should of course encase each tabular environment inside \begin{table} \caption{...} \label{...} \centering [tabular stuff] \end{table} statements.) First, there's a horizontal rule indicating the width of the text block. In the first table, the missing vertical lines are restored by adding a couple of & symbols. Obviously, the table is (much) too wide and won't fit in the available space.

In the second table, the widths of columns 2 and 3 are reduced significantly by moving the unit designations to separate lines. (This should also help reduce any confusion over whether the (s) after Delay is a unit designation or the plural-s to Delay.) In the third table, the width is reduced further, succeeding in making the table fit in the text block. Importantly, all vertical lines are eliminated, allowing some more (horizontal) space saving; in addition, the look of the horizontal lines and the spacing above and below the horizontal lines is improved by replacing the basic LaTeX \hline macro with \toprule, \midrule, and \bottomrule macros.

enter image description here

\documentclass[a4paper,twocolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,booktabs}
\setlength\parindent{0pt}
\begin{document}
Width of text block, two-column mode:
\hrule
\bigskip
(a) Restore the missing vertical lines:

\smallskip
\begin{tabular}{|c|c|c|c|}
\hline
Topology & Average Delay(s) & Average Bandwidth(kbps) & Hosts \\
\hline
Low & 0.0994 & 2441.85 & \multirow{2}{*}{64} \\
High & 0.1424 & 2515.77  & \\ 
\hline
Low & 0.2522 & 1798.60 & \multirow{2}{*}{128} \\
High & 0.1113 & 2123.17  & \\
\hline
\end{tabular}

\bigskip
(b) Reduce table width by moving units designations 
to separate line:

\smallskip
\begin{tabular}{|c|c|c|c|}
\hline
Topology & Average Delay & Average Bandwidth & Hosts \\
& (s) & (kbps) & \\
\hline
Low & 0.0994 & 2441.85 & \multirow{2}{*}{64} \\
High & 0.1424 & 2515.77  & \\ 
\hline
Low & 0.2522 & 1798.60 & \multirow{2}{*}{128} \\
High & 0.1113 & 2123.17  & \\
\hline
\end{tabular}

\bigskip
(c) Better horizontal lines, no vertical lines, reduce table 
width some more:

\smallskip
\begin{tabular}{@{} cccc @{}}
\toprule
Topology & Avg.\ Delay & Avg.\ Bandwidth & Hosts \\
& (s) & (kbps) & \\
\midrule
Low & 0.0994 & 2441.85 & \multirow{2}{*}{64} \\
High & 0.1424 & 2515.77  & \\[2ex]
Low & 0.2522 & 1798.60 & \multirow{2}{*}{128} \\
High & 0.1113 & 2123.17  & \\
\bottomrule
\end{tabular}

\end{document}
5

Addressing some of your issues:

  1. Vertical line

Just add an extra ampersand after the column definition, that'll fix the vertical line. e.g.,

Low & 0.0994 & 2441.85 & \multirow{2}{*}{64} \\
High & 0.1424 & 2515.77 & \\

The extra vertical lines at the end are addressed by moving the \label block.

  1. Caption

The tabular environment, afaik, normally doesn't support captions. You may want to envelope all this with the \begin{table}...\end{table} and place the \caption{Table} after closing the tabular environment.

So, the complete code looks like:

\begin{table}
\centering
\begin{tabular}{|c|c|c|c|}
\hline
Topology & Average Delay(s) & Average Bandwidth(kbps) & Hosts \\
\hline
Low & 0.0994 & 2441.85 & \multirow{2}{*}{64} \\
High & 0.1424 & 2515.77 & \\
\hline 
Low & 0.2522 & 1798.60 & \multirow{2}{*}{128} \\
High & 0.1113 & 2123.17 & \\
\hline
\end{tabular}
\caption{This table shows}\label{eval_table}
\end{table}

producing the following output: example

UPD: made some corrections pointed by egreg

4
  • 2
    Never include table in a center environment; rather declare \centering after \begin{table}. The \label should go after \caption
    – egreg
    Jul 18, 2012 at 9:48
  • also see this question: center vs. centering
    – matth
    Jul 18, 2012 at 11:59
  • 1
    Also captions for tables are usually placed at top of the table, while captions for drawings are placed beneath. Jul 18, 2012 at 14:19
  • Here is a discussion of the caption placement
    – matth
    Jul 21, 2012 at 13:43
4

As stated in a comment, overuse of rules in a table is considered bad style. Perhaps you prefer this alternative design (which follows the rules and guidelines of the booktab manual):

\documentclass{article}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{table}\centering
\begin{tabular}{cccc}
\toprule
Topology & Average Delay(s) & Average Bandwidth(kbps) & Hosts \\
\midrule
Low & 0.0994 & 2441.85 & \multirow{2}{*}{64} \\
High & 0.1424 & 2515.77 & \\
\addlinespace 
Low & 0.2522 & 1798.60 & \multirow{2}{*}{128} \\
High & 0.1113 & 2123.17 & \\
\bottomrule
\end{tabular}
\caption{This table shows}\label{eval_table}
\end{table}
\end{document}

Which produces:

Output table

1
  • this is, like, walking along the street is considered "bad style" in LA. tables with only horizontal rules is one particular way of doing a table (that got popular in the 1960s—people were fine with vertical bars for ages before that). i might as well claim that your use of 'caps' numerals in your example must be considered bad style, as lowercase figures are much more readable. is that universally true? guess not.
    – flow
    Dec 16, 2013 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .