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.

table before

I'm in the middle of a LaTeX battle with table attributes and so far I got to what you see above and the code I post below:

\begin{center}
\begin{table}
\begin{tabular}  {p{1.2cm}p{0.5cm}p{0.9cm}p{1.2cm}p{0.5cm}p{0.9cm}p{0.5cm}p{1.1cm}p{0.5cm}}
\rowcolor[rgb]{0.34,0.58,0.52} categoria & Durata & 2008 & 2009 & 2010 & 2011 & 2012 & Aggregato & Altro \\ 
infocommercio\cite{citaz121} & 3Q & 2.9\% & 21.0\% & 15.0\% & 52.5\% & 2.2\% & 1.5\% & 0.9\% \\
camera commercio\cite{citaz2} & 2Q & 33.1\% & 18.1\% & 28.5\% & 36.9\% & 12.7\% & 24.3\% & 18.4\% \\
Istat\cite{citazistat} & 12Q & 15.19\% & 89.76\% & 77.49\% & 65.76\% & 70.4\% & 84.5\% & .30\% \\
\end{tabular}
\caption{Percentuali PET dei GDO lombardi}
\label{tabella_gdo_2}
\end{table}
\end{center}

The problem is that I can specify the column width but not the padding, so as you can see there's a lot of wasted space, below I post how I would like the table to look more or less.

table after

so far I have followed advice from http://en.wikibooks.org/wiki/LaTeX/Tables by removing spacing between columns with {@{}c@{}c@{}} however the table content squeezes leaving a big space between columns unaltered. Can anyone help please?

share|improve this question
add comment

migrated from stackoverflow.com May 5 '12 at 12:58

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 3 down vote accepted

My suggestion would be to use tabularx:

enter image description here

\documentclass{article}
\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\begin{document}
\begin{table}
  \begin{tabularx}{\linewidth}{X*{8}{@{\hspace{2mm}}p{13mm}}}
    \rowcolor[rgb]{0.34,0.58,0.52} categoria & Durata & 2008 & 2009 & 2010 & 2011 & 2012 & Aggregato & Altro \\ 
    infocommercio[5] & 3Q & 2.9\% & 21.0\% & 15.0\% & 52.5\% & 2.2\% & 1.5\% & 0.9\% \\
    camera commercio[6] & 2Q & 33.1\% & 18.1\% & 28.5\% & 36.9\% & 12.7\% & 24.3\% & 18.4\% \\
    Istat[7] & 12Q & 15.19\% & 89.76\% & 77.49\% & 65.76\% & 70.4\% & 84.5\% & .30\% \\
  \end{tabularx}
  \caption{Percentuali PET dei GDO lombardi}
  \label{tabella_gdo_2}
\end{table}
\end{document}

In the above example, tabularx takes a width argument, specified as \linewidth. That implies that table will span the width of the text block within the table environment. Additionally, for the sake of consistency, each of the 8 columns (2-9) are given a fixed width of 13mm (using p{13mm} and a fixed column gap of 2mm (using @{\hspace{2mm}}). It would be possible to use other alignments as well by including the array package. However, the current MWE seems to provide what you're after.

The table still has some padding (a \tabcolsep) on either side. This can also be modified, if needed.

geometry was used to modify the page margins, since it seems necessary to fit that amount of information horizontally within the text block.

share|improve this answer
    
Thank you Werner, I spent hours and I wouldn't have figured it out.. this tabularx package wasn't mentioned in the latex websites I had searched for.. –  dendini May 8 '12 at 12:24
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.