# Formatting table

I have a table that is created with the following code:

\begin{table}[htdp]
\caption{Comparison of Elements in Air on the Space Station and sea level on Earth}
\centering
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
Chemical Component & Percentage in Earth's Atomsphere & Ideal Values for the Space Station &         Astronaut Exhalation\\ \hline
Nitrogen      & 78.084\% & 78.000\% & 74.200\% \\ \hline
Oxygen        & 20.946\% & 21.000\% & 15.300\% \\ \hline
Argon         & 0.934\%  &  0.000\% & 0.000\% \\ \hline
Carbon Dioxide& 0.033\%  &  0.000\% & 3.600\% \\ \hline
Water Vapour  & 0.030\%  &  1.000\% & 0.800\% \\ \hline
Trace Elements& 0.003\%  &  0.000\% & 0.800\% \\ \hline
\end{tabular}
\end{center}
\label{default}
\end{table}


But it sticks over the page so how could i format it so that it fits on a page in portrait mode not landscape.

-
FYI, you don't need both the center environment and the \centering command. The latter is the recommended way to center the content of your float. – Martin Tapankov Feb 27 '11 at 15:07
Ok ive used \centering. – Dean Feb 27 '11 at 15:09

\documentclass{article}
\usepackage{tabularx,ragged2e}
\newcolumntype{x}{>{\Centering}X}

\begin{document}
\begin{table}[htdp]
\caption{Comparison of Elements in Air on the Space Station and sea level on Earth}\label{default}
\begin{tabularx}{\linewidth}{|>{\RaggedRight}p{2.5cm}|x|x|x|}\hline
Chemical Component & Percentage in Earth's Atomsphere & Ideal Values for the Space Station & Astronaut Exhalation\\ \hline
Nitrogen      & 78.084\% & 78.000\% & 74.200\% \\ \hline
Oxygen        & 20.946\% & 21.000\% & 15.300\% \\ \hline
Argon         & 0.934\%  &  0.000\% &  0.000\% \\ \hline
Carbon Dioxide& 0.033\%  &  0.000\% &  3.600\% \\ \hline
Water Vapour  & 0.030\%  &  1.000\% &  0.800\% \\ \hline
Trace Elements& 0.003\%  &  0.000\% &  0.800\% \\ \hline
\end{tabularx}
\end{table}

\end{document}


btw: the vertical lines do not make a table more readable ...

-
Same for the horizontal. Stick to 1 or 2 maximum. Check tug.ctan.org/macros/latex/contrib/booktabs/booktabs.pdf and en.wikibooks.org/wiki/LaTeX/Tables for more info on this. – Elmer Feb 27 '11 at 15:05

You should stack the long cell titles e.g. by manually placing them over several rows or using \shortstack:

\shortstack{Ideal Values\\for the\\Space Station}


You might want to add a \strut to the \\ to get a constant distance between the lines.

-

To learn about table design in general, I cannot praise the documentation of the booktabs package enough (see a before-and-after example from the documentation below). For your problem at hand, I would suggest to either come up with shorter column titles or to break them into several lines. This problem has been discussed as How to break a line in a table.

In another answer Alan Munn has formatted the table from the question using the booktabs package and solved the problem how to break the column titles into two lines. I find it close to perfection.

-

Use the array package to have better control over table column specifications. Your problem is caused by the length of your column headings; you need to have specified widths but still centred titles. It's generally not advisable to have vertical lines in tables, nor to put the units (here '%') in each cell (they should go in the column heading). Given that all your numeric values are decimals, you also want to line up the decimal points. A right justified column can do this, but the dcolumn package provides more flexibility. As Christian mentions, the booktabs package is the gold standard here.

Here's a version of your table using these principles.

\documentclass{article}
\usepackage{booktabs,array,dcolumn}

\newcolumntype{d}{D{.}{.}{2.3}}
\newcolumntype{C}{>{\centering}p}
\begin{document}
\begin{table}[htdp]
\caption{Comparison of Elements in Air on the Space Station and sea level on Earth}
\centering
\begin{center}
\begin{tabular}{p{1.25in}ddd}
\toprule
\multicolumn{1}{C{1.25in}}{Chemical Component} & \multicolumn{1}{C{1in}}{Earth's Atmosphere (\%)} & \multicolumn{1}{C{1.25in}}{Ideal Values for the Space Station (\%)} &         \multicolumn{1}{C{1in}}{Astronaut Exhalation (\%)}\\
\midrule
Nitrogen & 78.084 & 78.000 & 74.200 \\
Oxygen & 20.946 & 21.000 & 15.300 \\
Argon & 0.934 & 0.000 & 0.000 \\
Carbon Dioxide & 0.033 & 0.000 & 3.600 \\
Water Vapour & 0.030 & 1.000 & 0.800 \\
Trace Elements & 0.003 & 0.000 & 0.800 \\
\bottomrule
\end{tabular}
\end{center}
\label{default}
\end{table}
\end{document}


-
Nice! A minor suggestion: The booktabs package recommends putting the units into the column titles (rather than into the rows). I get the feeling that the values for the space station and the astronauts suggest a precision that isn't there and would recommend (to the author) using fewer digits. – Christian Lindig Feb 27 '11 at 15:41
@Christian I agree with booktabs on this, but in this particular case I couldn't find any nice way to put the '%' in the heading without making the table look really ugly, so this was the best compromise. I might be inclined to have simply put a comment in the caption "All values are percentages" intstead. – Alan Munn Feb 27 '11 at 16:16
@Christian If I make the middle column a little wider it's not so ugly. I've updated my answer. (P.S. I agree with the precision comment as well.) – Alan Munn Feb 27 '11 at 16:22

I could feel my \halign-sense tingling. Mainly to highlight one other recommendation; to always left-align everything (or at least give you an idea how'd that look). But also the other option of splitting the column-heading row to two rows. It might not feel such a good idea, but the one thing going for it, is the lack of manually declared column widths. I should probably point out that some of the following is borderline heresy.

\vbox{\openup.4em% make the row separating space a bit bigger
\def\title#1&{\omit#1\hfil&}% omit the preamble formatting + left-align
% only thing "seen" is the decimal point itself
\hrule height 1pt \medskip
\halign{#\hfil\tabskip1em&&\decsplit#\hfil\cr
\title Chemical&\title Earth's&\title Ideal Values for the&\title Astronaut\cr
\noalign{\vskip-.4em}% make the gap between header rows smaller
\title Component&\title Athmosphere (\%)&\title Space Station (\%)&\title Exhalation (\%)\cr
\noalign{\smallskip\hrule\medskip}
Nitrogen      &78.084 &78.000 &74.200 \cr
Oxygen        &20.946 &21.000 &15.300 \cr
Argon         & 0.934 & 0.000 & 0.000 \cr
Carbon Dioxide& 0.033 & 0.000 & 3.600 \cr
Water Vapour  & 0.030 & 1.000 & 0.800 \cr
Trace Elements& 0.003 & 0.000 & 0.800 \cr
}
\medskip
\hrule height 1pt
}


-
It's probably worth going with the defaults for rule width and spacing given in booktabs. I.e., something like \kern\abovetopsep \hrule height\heavyrulewidth \kern\belowrulesep for the top rule, \kern\aboverulesep \hrule height\lightrulewidth \kern\belowrulesep for the middle rule, and \kern\aboverulesep \hrule height\heavyrulewidth \kern\belowbottomsep` for the bottom rule. Replacing those dimensions as appropriate. – TH. Feb 27 '11 at 23:56