3

I need a very simple table with all the cells (except the first) of the same width. The first row has two characters in the first few cells and only one character in the last three.

I am not able to have uniform spacing even with a fixed \setlength: the columns with the single character in the first cell are always thinner.

\documentclass{scrbook}
\begin{document}


\chapter{votazioni quesiti e documento}

\setlength{\tabcolsep}{7pt}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}

        \hline
            &1A &1B  &1C  &1D  &1E  &1F  &2A  &2B  &2C  &2D  &2E  &2F  & 3   & 4   & D \\
    \hline
    white&  &  &  &  &  &  &  &  &  &  &  &  &  &  &   \\
    \hline
    red&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    blue&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    cyan&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    yellow&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    green&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    purple&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    black&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    silver&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    gold&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    pink&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    brown&  &  &  &  &  &  &  &  &  &  &  &  &  &  &   \\
    \hline
    orange&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    darkblue&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    lightblue&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    whatelse&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    VOTO \%&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline

\end{tabular}
\end{document}
1
  • 2
    \tabcolsep does not set the width of the columns but the spacing inbetween. You could use p{2em} instead of c as column specifier. Jan 24 at 21:11

3 Answers 3

4

Since automatic line breaking within cells doesn't seem to be a required feature for the table at hand, I suggest that you not employ the basic p column type. Instead, I would like to suggest that you load the array package and employ its w column type. This column type lets you specify a desired fixed width of, say, 5mm.

enter image description here

\documentclass{scrbook}
\usepackage{array} % for 'w' column type

\begin{document}
   
\chapter{votazioni quesiti e documento}

\begin{center}
\setlength{\tabcolsep}{4pt} % default: 6pt
\begin{tabular}{ |l| *{15}{wc{5mm}|} } %15 'w' cols, centered, width 5mm
    \hline
          &1A&1B&1C&1D&1E&1F&2A&2B&2C&2D&2E&2F& 3& 4& D  \\
    \hline
    white &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    red   &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    blue  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    cyan  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    yellow&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    green &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    purple&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    black &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    silver&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    gold  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    pink  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    brown &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    orange&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    darkblue &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    lightblue&  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    whatelse &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
    VOTO \%  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &    \\
    \hline
\end{tabular}
\end{center}
\end{document}
3
  • this works very well. Just out of curiosity: because I want to center the cell content, before trying your solution , that is certainly better, I tried to define a new column like this: \newcolumntype{C}[1]{>{\centering}m{#1}} \begin{tabular}{|c|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|C{2em}|} it works well except for the last column. When I put the C in the last column (instead of p) I get a misalignment. Jan 24 at 21:58
  • 1
    @FabioCampanile, correct definition for new column type is \newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
    – Zarko
    Jan 24 at 22:14
  • @FabioCampanile - There's an important aesthetic difference between mathematical and optical vertical centering. The m column type gives you the former, but the result won't really "feel" like it's vertically centered. Achieving optical centering is as much an art as a science, but if the main text font size is 10pt, a good starting point is to run \setlength\extrarowheight{2.5pt} before the start of the tabular material. It's true that the latter method increases the overall height of the tabular material; however, the aesthetic benefit may well be worth the extra (spatial) cost.
    – Mico
    Jan 24 at 22:39
3

With tabularray package and X[c] column types for all except the first columns:

\documentclass{scrbook}
%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\usepackage{tabularray}

\begin{document}
\chapter{votazioni quesiti e documento}

\centering
\begin{tblr}{hlines, vlines,
             colspec = {l *{15}{X[c]} },
              colsep = 3.4pt,
             }
            & 1A & 1B & 1C & 1D & 1E & 1F & 2A & 2B & 2C & 2D & 2E & 2F & 3 & 4 & D \\
white       &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
red         &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
blue        &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
cyan        &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
yellow      &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
green       &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
purple      &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
black       &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
silver      &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
gold        &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
pink        &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
brown       &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\ 
orange      &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
darkblue    &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
lightblue   &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
whatelse    &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
VOTO \%     &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
\end{tblr}

\end{document}

enter image description here

(red lines shows text area borders)

Addendum: The same result you get with use of the tabularx packages and columns >{\centering\arraybackslash}X. In this case the MWE is:

\documentclass{scrbook}
\usepackage{tabularx}

\begin{document}
\chapter{votazioni quesiti e documento}

\centering
\setlength\tabcolsep{3.4pt}
    \begin{tabularx}{\linewidth}{|l| *{15}{ >{\centering\arraybackslash}X| } }
    \hline
            & 1A & 1B & 1C & 1D & 1E & 1F & 2A & 2B & 2C & 2D & 2E & 2F & 3 & 4 & D \\
    \hline
white       &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
white       &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
red         &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
blue        &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
cyan        &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
yellow      &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
green       &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
purple      &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
black       &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
silver      &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
gold        &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
pink        &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
brown       &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
orange      &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
darkblue    &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
lightblue   &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
whatelse    &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
    \hline
VOTO \%     &    &    &    &    &    &    &    &    &    &    &    &    &   &   &   \\
\hline
    \end{tabularx}
\end{document}
0

Add \hspace{1mm} to the column with a single letter. That should make all the columns equal width.

You can always adjust the width by having different spacing \hspace{2mm}, \hspace{3mm}, ...

3
  • This does not make all columns qually wide, but just adds 1mm width to the columns with one letter ... Jan 24 at 21:40
  • 1
    sorry but this does not answer the question, it does not ensure equal width columns. Jan 24 at 21:40
  • In addition it does not allow to center the content Jan 24 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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