3

I have been trying to create a table detailing the changes in speed over time, and have noticed that the last column (22 seconds) is disproportionately wide. Is there any way I can reduce the size of it so that it is the same width as the others?

\begin{center}
\begin{tabularx}{\columnwidth}{|l|l|l|l|l|l|l|l|l|l|l|l|X|}
\hline
Time (sec)    & 0  & 2  & 4  & 6  & 8  & 10 & 12 & 14 & 16 & 18 & 20 & 22 \\
 \hline
Speed (m/sec) & 10 & 14 & 20 & 24 & 22 & 18 & 16 & 15 & 14 & 14 & 13 & 11 \\
 \hline
\end{tabularx}
\end{center}\\
2
  • you don't need tabularx try \begin{tabular}{|l|l|l|l|l|l|l|l|l|l|l|l|l|} \hline Time (sec) & 0 & 2 & 4 & 6 & 8 & 10 & 12 & 14 & 16 & 18 & 20 & 22 \\ \hline Speed (m/sec) & 10 & 14 & 20 & 24 & 22 & 18 & 16 & 15 & 14 & 14 & 13 & 11 \\ \hline \end{tabular}
    – touhami
    Mar 15, 2015 at 20:30
  • 1
    also don't use \\ after \end{center} Mar 15, 2015 at 21:10

2 Answers 2

5

If you want the table to span a certain width, e.g., the width of the text block, but don't need (or want) all columns to be equally wide, you could employ a tabular* environment instead of the tabularx environment you're using at the moment. (The directive @{\extracolsep{\fill}} serves to insert additional intercolumn whitespace.)

enter image description here

\documentclass{article}
\begin{document}
\noindent
\begin{tabular*}{\columnwidth}{ @{\extracolsep{\fill}} |l| *{12}{l|} }
\hline
Time (sec)    & 0  & 2  & 4  & 6  & 8  & 10 & 12 & 14 & 16 & 18 & 20 & 22 \\
 \hline
Speed (m/sec) & 10 & 14 & 20 & 24 & 22 & 18 & 16 & 15 & 14 & 14 & 13 & 11 \\
 \hline
\end{tabular*}
\end{document}

Echoing the comments made by @BenediktBauer in his answer, you may want to give some serious thought to omitting all vertical bars -- trust me, they add clutter rather than provide clarity -- and to replacing the \hline directives with the macros of the booktabs package, as they produce horizontal lines with much improved spacing properties.

enter image description here

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\setlength\tabcolsep{0.1pt} % let tabular* figure out the intercolumn separation
\noindent
\begin{tabular*}{\columnwidth}{ @{} l @{\extracolsep{\fill}} *{12}{r} @{}}
\toprule
Time (sec)    & 0  & 2  & 4  & 6  & 8  & 10 & 12 & 14 & 16 & 18 & 20 & 22 \\
\midrule
Speed (m/sec) & 10 & 14 & 20 & 24 & 22 & 18 & 16 & 15 & 14 & 14 & 13 & 11 \\
\bottomrule
\end{tabular*}
\end{document}
3
  • Just out of curiosity, why have you removed the side padding?
    – yo'
    Mar 15, 2015 at 21:23
  • 1
    I'm in reasonably good company in this regard: Simon Fear, the creator of the booktabs package, also inserts @{} to left of the first column and to the right of the final column. More generally, I don't see the need to provide padding in these two places unless the table features vertical bars in these two locations -- which, of course, shouldn't be necessary in a well-designed table.
    – Mico
    Mar 15, 2015 at 21:48
  • ah ok. Something I don't do (and probably won't start doing, but I might see). It as well explains why \cmidrule has so "bad" UI :)
    – yo'
    Mar 15, 2015 at 21:49
3

You are using the X column type of the tabularx environment. A tabularx basically stretches a table to the width it has been given as parameter (\columnwidth in your case). In this context the X column takes over all the space that is not occupied by the other columns to expand the table to the full width. If you have several X columns, the available space will be equally distributed among them.

To solve your problem, you can either set all but the first column to be an X column:

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\linewidth}{|l|X|X|X|X|X|X|X|X|X|X|X|X|}
\hline
Time (sec)    & 0  & 2  & 4  & 6  & 8  & 10 & 12 & 14 & 16 & 18 & 20 & 22 \\
\hline
Speed (m/sec) & 10 & 14 & 20 & 24 & 22 & 18 & 16 & 15 & 14 & 14 & 13 & 11 \\
\hline
\end{tabularx}
\end{document}

Or, if the table does not necessarily be full column width, just use the normal tabular environment with all l columns:

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|l|l|l|}
\hline
Time (sec)    & 0  & 2  & 4  & 6  & 8  & 10 & 12 & 14 & 16 & 18 & 20 & 22 \\
\hline
Speed (m/sec) & 10 & 14 & 20 & 24 & 22 & 18 & 16 & 15 & 14 & 14 & 13 & 11 \\
\hline
\end{tabular}
\end{document}

For tables containing numbers I would prefer the latter solution.

Also, just as a comment concerning style: from a typographic point of view vertical lines in tables are concerned bad as they disturb readability of the table and should only be used very sparsely to highlight/clarify certain features of the table. Try to not stick too much to this Excel-like style of table formatting!

You must log in to answer this question.

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