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 have a table of two rows, the second one consists of 32 columns, each containing its column number (from 0 to 31). I want each column to use as little space as possible (no inter column space), since there are quite a lot of columns. I have done this using @{}c@{}. However, I don't like the fact that the columns are not all of the same size (due to the fact that some numbers have 2 digits, and others have only one). Hence I have manually added @{\hspace{0.7 mm}}c@{\hspace{0.7 mm}} to each of the columns whose contents in the second row have only 1 digit (the first 10). I am quite pleased with the result.

My problem now is with the first row: the first row contains 4 columns, containing 0,1,2,3. Each column in the first row spans 8 columns of the second row. My problem is that the delimiters in the first row aren't aligned with the second row (in the sample below, notice that the delimiter between 7 and 8 is not aligned with the one above).

Is there an easier solution to my problem as a whole, or am I in the right direction and I just need to fix this alignment issue ?

Sample:

\begin{tabular}{|
% numbers with 1 digit
@{\hspace{0.7 mm}}c@{\hspace{0.7 mm}}|
@{\hspace{0.7 mm}}c@{\hspace{0.7 mm}}|
@{\hspace{0.7 mm}}c@{\hspace{0.7 mm}}|
@{\hspace{0.7 mm}}c@{\hspace{0.7 mm}}|
@{\hspace{0.7 mm}}c@{\hspace{0.7 mm}}|
@{\hspace{0.7 mm}}c@{\hspace{0.7 mm}}|
@{\hspace{0.7 mm}}c@{\hspace{0.7 mm}}|
@{\hspace{0.7 mm}}c@{\hspace{0.7 mm}}|
@{\hspace{0.7 mm}}c@{\hspace{0.7 mm}}|
@{\hspace{0.7 mm}}c@{\hspace{0.7 mm}}|
% numbers with 2 digits
@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|@{}c@{}|}
\hline
\multicolumn{8}{c|}{0} 
&\multicolumn{8}{c|}{1}
&\multicolumn{8}{c|}{2}
&\multicolumn{8}{c|}{3}\\
\hline
0&1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20&21&22&23&24&25&26&27&28&29&30&31
\end{tabular}
share|improve this question
3  
Is this a calendar? You might be better off with a more "dedicated" solution, such as a timeline, a gantt chart, or a "proper" calendar such as the ones possible with PGF/TikZ. –  Jake May 29 '12 at 15:48

2 Answers 2

up vote 7 down vote accepted

It's easier. :) Set a length to the width of two digits in the current type size (I've also added a small space just in order that the figures don't touch the vertical lines) and the parameter \tabcolsep to zero; then use p columns of the specified width and with \centering alignment:

\documentclass{article}

\usepackage{array}
\newlength{\twodigits}

\begin{document}

\begin{table}
\centering
\small
\settowidth{\twodigits}{00}
\addtolength{\twodigits}{.4pt}
\setlength{\tabcolsep}{0pt}
\begin{tabular}{|*{32}{>{\centering}p{\twodigits}|}}
\hline
\multicolumn{8}{|c|}{0}
&\multicolumn{8}{c|}{1}
&\multicolumn{8}{c|}{2}
&\multicolumn{8}{c|}{3}\\
\hline
0&1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20&21&22&23&24&25&26&27&28&29&30&31 \tabularnewline
\hline
\end{tabular}
\end{table}

\end{document}

If you don't want to use \tabularnewline, then add the magic \arraybackslash:

\begin{tabular}{|*{32}{>{\centering\arraybackslash}p{\twodigits}|}}

\hline
\multicolumn{8}{|c|}{0}
&\multicolumn{8}{c|}{1}
&\multicolumn{8}{c|}{2}
&\multicolumn{8}{c|}{3}\\
\hline
0&1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20&21&22&23&24&25&26&27&28&29&30&31 \\
\hline
\end{tabular}
share|improve this answer
    
I tried to adapt your answer to my code for the past hour, but I failed. There are two errors on a line that has \cline{2-34}. Those are "misplaced omit" and "extra alignment tab has been changed to cr". My actual tabular has two extra columns that are unrelated to this issue. I tried appending c|c| to |*{32}, but it returned these errors. –  bob May 29 '12 at 16:53
    
@bob It's impossible to say without seeing the code. Add it to your question. However, adding the two extra columns should be done as in \begin{tabular}{|*{32}{>{\centering}p{\twodigits}|}c|c|} –  egreg May 29 '12 at 17:08
    
Sorry, after extensive tests, it appears it's not related to me having two more columns. To make it simple, if I use the very code above, and just add \hline before \end{tabular}, I get 'misplaced noalign'. I tried adding \\ to the end of the previous line (so, the one with 0&1&...), but the error is still the same –  bob May 30 '12 at 9:19
    
@bob Now I understood; I've added two possible fixes. –  egreg May 30 '12 at 9:26
    
Thank you, it works wonders. Can I have a quick explanation of why \\ doesn't work without \arraybackslash, and an explanation of the syntax of |*{32}{>{\centering}p{\twodigits}|} ? –  bob May 30 '12 at 9:35

Another solution would be to rely on the tabularx package and its "X" column type. One of the neat things about this column type is that it does virtually all of the column width calculations for you. The following MWE implements this idea.

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{array,tabularx}
% define "Y" column type to be same as "X", but with contents centered
\newcolumntype{Y}{>{\centering\arraybackslash}X} 
\setlength{\tabcolsep}{0.1pt}
\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{|*{32}{Y|}}
\hline
\multicolumn{8}{|c|}{0}&
\multicolumn{8}{c|}{1} &
\multicolumn{8}{c|}{2} &
\multicolumn{8}{c|}{3} \\
\hline
0&1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20&21&22&23&24&25&26&27&28&29&30&31\\
\hline
\end{tabularx}
\end{table}
\end{document}

enter image description here

share|improve this answer

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.