6

The following code generates a table:

enter image description here

\begin{table}[!t]
% increase table row spacing, adjust to taste
\renewcommand{\arraystretch}{1.3}
\caption{An Example of a Table}
\label{table_example}
\centering
\begin{tabular}{|c|c|c|c|c|}
%\toprule
\hline
a & b & c & \multicolumn{2}{c|}{abcdefg fewfewfe}\\
%\midrule
& & & st1 & st2\\

\hline
a         & 1  & 2 &  3 & 4  \\
b         & 1  & 2 &  3 & 4 \\
%\bottomrule
\hline
\end{tabular}
\end{table}

I don't know why columns of st1 and st2 are not properly aligned. When I replace 'abcdefg fewfewfe' with a single word say 'abcde', the alignment will be good.

lockstep
  • 250,273

3 Answers3

2

If you need the final two columns to have exactly equal widths and if they happen to be spanned by the string abcdefg fewfewfe, you could proceed as in the example below.

enter image description here

\documentclass{article}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\newlength{\mylen} \settowidth\mylen{abcdefg fewfewfe} \addtolength\mylen{-2\tabcolsep} \addtolength\mylen{-\arrayrulewidth} \setlength\mylen{\dimexpr\mylen/2\relax}

\begin{document} \begin{table}[!t] % increase table row spacing, adjust to taste \renewcommand{\arraystretch}{1.3} \caption{An Example of a Table} \label{table_example} \centering \begin{tabular}{|c|c|c|C{\mylen}|C{\mylen}|} %\toprule \hline a & b & c & \multicolumn{2}{c|}{abcdefg fewfewfe}\ %\midrule & & & st1 & st2\ \hline a & 1 & 2 & 3 & 4 \ b & 1 & 2 & 3 & 4 \ %\bottomrule \hline \end{tabular} \end{table} \end{document}


April 2023 Addendum to address the follow-up question by @MorganRogers: To achieve your objective, I suggest you replace the code block

\newlength\mylen
\settowidth\mylen{abcdefg fewfewfe}
\addtolength\mylen{-2\tabcolsep}
\addtolength\mylen{-\arrayrulewidth}
\setlength\mylen{\dimexpr\mylen/2\relax}

with

\newlength\mylen % or some other suitable name
\usepackage{calc}
\newcommand\CalcHalfWidth[2]{\setlength{#1}%
   {(\widthof{#2}-2\tabcolsep-\arrayrulewidth)/2}}

and then issue the directive

\CalcHalfWidth{\mylen}{abcdefg fewfewfe}

at the point where you wish to set the (usable) column width of the pair of columns in question. Of course, you're free to choose another length name than \mylen, and you'll have to replace abcdefg fewfewfe with the string that spans the pair columns in question.

Mico
  • 506,678
  • Suppose I need to perform this operation many times in one article, or several times in a single table. Is there a way to construct a macro from the length definition you give which would allow me to generate evenly spaced columns on the fly (without guessing how wide the columns should be as MS Taylor's answer)? – Morgan Rogers Apr 05 '23 at 13:32
  • @MorganRogers - Please see the addendum I posted. Does it address your query? (Of course, if the header string is supposed to span 3 equal-width columns instead of just 2, you'd have to change 2\tabcolsep to 4\tabcolsep, \arrayrulewidth to 2\arrayrulewidth, and /2 to /3.) – Mico Apr 05 '23 at 14:46
  • Thanks! I probably need several of these length variables because they need to be computed before defining a table with two instances of the annoying column spacing, but I can make that work. – Morgan Rogers Apr 06 '23 at 13:43
  • 1
    @MorganRogers - You're most welcome. The setup I suggested in the addendum is definitely compatible with needing two or more length variables for a given table, e.g., \mylenA and \mylenB. And if you need to obtain the width of a triple of columns, it would make sense to define a macro such as \newcommand\CalcThirdWidth[2]{\setlength{#1} {(\widthof{#2}-4\tabcolsep-2\arrayrulewidth)/2}. – Mico Apr 06 '23 at 14:30
  • It's worth me mentioning that @Michael_S_Taylor 's custom column definition \newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}} resolved an issue I had when implementing this solution, namely that it created extra space under an \hline compared with the ordinary c column of my tables. Otherwise, this solution has been very useful to me, thanks again! – Morgan Rogers Apr 13 '23 at 20:18
1

Tabular adjusts column widths automatically to cell contents. It doesn't have enough information to equally space the st1 and st2 columns relative to the multicolumn header. It fits st1, then adjusts st2 to fit the rest of the multicolumn header. Your example does not work for single word headings. Remove the space between abcdefg and fewfewfe to see the same behavior.

If you specify the width of the two columns to be equal, then you get the behavior you seek.

\documentclass{article}

% The four lines came from 
% http://tex.stackexchange.com/questions/12703/how-to-create-fixed-width-table-columns-with-text-raggedright-centered-raggedlef
% I changed the m{#1} to p{#1}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}

\begin{document}
\begin{table}
% increase table row spacing, adjust to taste
\renewcommand{\arraystretch}{1.3}
\caption{An Example of a Table}
\label{table_example}
\centering
\begin{tabular}{|c|c|c|C{1cm}|C{1cm}|}
%\toprule
\hline
a & b & c & \multicolumn{2}{c|}{abcdefg fewfewfe}\\
%\midrule
& & & st1 & st2\\

\hline
a         & 1  & 2 &  3 & 4  \\
b         & 1  & 2 &  3 & 4 \\
%\bottomrule
\hline
\end{tabular}
\end{table}\end{document}

enter image description here

0

Because the caption (abcdefg fewfewfe) is wider than the columns the last column is widened. Below is one solution, which you may or may not like.

\documentclass{article}

\usepackage{minibox}
\begin{document}
\begin{table}[!t]
% increase table row spacing, adjust to taste
\renewcommand{\arraystretch}{1.3}
\caption{An Example of a Table}
\label{table_example}
\centering
\begin{tabular}{|c|c|c|c|c|}
%\toprule
\hline
a & b & c & \multicolumn{2}{c|}{\minibox{abcdefg\\fewfewfe}}\\
%\midrule
& & & st1 & st2\\

\hline
a         & 1  & 2 &  3 & 4  \\
b         & 1  & 2 &  3 & 4 \\
%\bottomrule
\hline
\end{tabular}
\end{table}


\end{document}
JPi
  • 13,595