2

How do I make it so all cells in my table share a uniform length?

I tried the following LaTeX code:

\begin{tabular}{c|c||c|c}
    S & R & Q & Q' \\
    \hline
    0 & 0 & \multicolumn{2}{c}{als voordien} \\
    0 & 1 & 0 & 1 \\
    1 & 0 & 1 & 0 \\
\end{tabular}

It generates the following table however:

enter image description here

As you can see, the fourth column is wider than the others. Is there a way to force it to be uniformly distributed?

I'm really sorry if there is an obvious answer to this question, but I am very much new to Latex, and I wasn't able to find anything on Google.

This question isn't the same as Column with fixed width and centre alignment that was commented below. That question is about retaining cell uniformity in the scenario of sub-cells. This one is about how to get a table with a uniform cell size in the first place. I am not satisfied with simply setting the table width to an absolute, as this seems inelegant and hackish. I'd much rather have my table be the smallest size it can be while keeping cell uniformity.

4

Like this?

enter image description here

\documentclass{article}
\usepackage{array,calc}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newsavebox{\mcw}
\newlength{\mcwlength}

\begin{document}

\sbox\mcw{als voordien}
\settowidth{\mcwlength}{\usebox{\mcw}}

\setlength\tabcolsep{3pt}
    \begin{tabular}{C{\mcwlength/2}|C{\mcwlength/2}||C{\mcwlength/2}|C{\mcwlength/2}}
    S & R & Q & Q' \\
    \hline
    0 & 0 & \multicolumn{2}{c}{\usebox\mcw} \\
    0 & 1 & 0 & 1 \\
    1 & 0 & 1 & 0 \\
    \end{tabular}
\end{document}

Short explanation: The idea is to measure length of content in the multicolumn cell, result divided by 2 and result use in p{...} column type. For this I define:

  • new savebox named \mcw (as shotness for multi colum width}
  • new lenght \mcvlength, which contain measured length of text stored in \mcw

Procedure is the following:

  1. store the text of multicolumn cell in \mcw
  2. with \settowidth{\mcwlength}{\usebox{\mcw}} store width of \mcw with stored text
  3. use \mcwlength/2 as length of table columns.

That cells content would be centered, I define new column tyle C. And that is. However, the actual width of colummns is biger as it is calculated by described procedure for 2\tabcolsep. This can give visual impression, that columns are wider than necessary. Therefore I reduce tabcolsep from default value of 6pt to 3pt with \setlength\tabcolsep{3pt}.

Addendum: A variation of table looks out (as sugested Bernard in his comment}:

enter image description here

\documentclass{article}
\usepackage{array,calc}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newsavebox{\mcw}
\newlength{\mcwlength}

\begin{document}

\sbox\mcw{als voordien}
\settowidth{\mcwlength}{\usebox{\mcw}}

\setlength\tabcolsep{3pt}
    \begin{tabular}{C{\mcwlength/2}|C{\mcwlength/2}
                    !{\vline width 1pt}% <-- determine thick vertical line
                    C{\mcwlength/2}|C{\mcwlength/2}}
    S & R & Q & Q' \\
    \hline
    0 & 0 & \multicolumn{2}{c}{\usebox\mcw} \\
    0 & 1 & 0 & 1 \\
    1 & 0 & 1 & 0 \\
    \end{tabular}
\end{document}
  • Yes, could you elaborate on what mcwlength does? – Jeroen Bollen Oct 5 '16 at 21:20
  • yes, i already do this. meantime I made some more tests and refinement of result – Zarko Oct 5 '16 at 21:45
  • Maybe using \hline{-||---} instead of \hline would look nicer. Or a thick \vrule. – Bernard Oct 5 '16 at 23:39
  • @Bernard, probably you mean \hhline{--||--} and !{\vline width 1pt}. I prefer this late option. I will add it to my answer. – Zarko Oct 6 '16 at 0:01
  • Yes. For the \hhline, I forgot to check the placement of the double rule.. But I think it's \vrule, not \vline (anyway, you'll check! ;o)) – Bernard Oct 6 '16 at 0:19
2

Here's a solution that first calculates the required overall width of the table (called \mylength in the code below)and uses the tabularx package to set up four centered columns.

If this were my table, I would get rid of the vertical rules entirely; the result is shown in the second table below. Give it a try.

enter image description here

\documentclass{article}
\usepackage{tabularx,booktabs}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\newlength\mylength
\settowidth\mylength{als voordien}
\setlength\mylength{\dimexpr2\mylength+4\tabcolsep+6\arrayrulewidth\relax}

\begin{document}
\begin{tabularx}{\mylength}{C|C||C|C}
    S & R & Q & Q' \\
    \hline
    0 & 0 & \multicolumn{2}{c}{als voordien} \\
    0 & 1 & 0 & 1 \\
    1 & 0 & 1 & 0 \\
\end{tabularx}

\bigskip
% recalculate \mylength
\settowidth\mylength{als voordien}
\setlength\mylength{\dimexpr2\mylength+4\tabcolsep\relax}
\begin{tabularx}{\mylength}{CCCC}
    S & R & Q & Q' \\
    \midrule
    0 & 0 & \multicolumn{2}{c}{als voordien} \\
    0 & 1 & 0 & 1 \\
    1 & 0 & 1 & 0 \\
\end{tabularx}

\end{document}

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.