2

I wrote the following code for a table:

\documentclass{article}
\begin{document}
\begin{table}
\centering
\begin{tabular}{|c|c|c|c|c|} 
\hline
x & \multicolumn{2}{c|}{y} & \multicolumn{2}{c|}{zzzzzzzzzzzzzzzzzzzzzzzzz}  \\ 
\hline
  & t & w                  & h & f                                           \\ 
\hline
  &   &                    &   &                                             \\ 
\hline
  &   &                    &   &                                             \\
\hline
\end{tabular}
\end{table}
\end{document}

The result is :

enter image description here

How can I center the columns including "h" and "f" under the multi-column including "zzzzzzzzzzzzzzzzzzzzzzzz"?

Actually, this is how I want it to be:

enter image description here

Thanks

Ali
  • 77

3 Answers3

5

In order to adapt the code in this answer to your document, all you need to do is take the following three steps.

  • First, copy the following preamble code into your document:

    \usepackage{calc}  % to ease performing of some calculations
    \usepackage{array} % for 'w' column type
    \newlength\lenA \newlength\lenB
    % Retrieve the usable width of the combined header cell:
    \settowidth\lenA{bcbcbcbc} 
    % Compute the usable width of the underlying columns:
    \setlength\lenB{(\lenA-2\tabcolsep-\arrayrulewidth)/2} 
    
  • Next, change bcbcbcbc to zzzzzzzzzzzzzzzzzzzzzzzzz (or whatever the combined header of colums 4 and 5 may contain).

  • Finally, change

    \begin{tabular}{|c|c|c|c|c|}
    

    to

    \begin{tabular}{|c|c|c|w{c}{\lenB}|w{c}{\lenB}|}
    

The full MWE (minimum working example) and its associated output:

enter image description here

\documentclass{article}
   \usepackage{calc}  % to ease performing of some calculations
   \usepackage{array} % for 'w' column type
   \newlength\lenA \newlength\lenB
   % Retrieve the usable width of the combined header cell:
   \settowidth\lenA{zzzzzzzzzzzzzzzzzzzzzzzzz} % rather than "bcbcbcbc"
   % Compute the usable width of the underlying columns:
   \setlength\lenB{(\lenA-2\tabcolsep-\arrayrulewidth)/2} 

\begin{document} \begin{table} \centering \begin{tabular}{|c|c|c|w{c}{\lenB}|w{c}{\lenB}|} \hline x & \multicolumn{2}{c|}{y} & \multicolumn{2}{c|}{zzzzzzzzzzzzzzzzzzzzzzzzz} \ \hline & t & w & h & f \ \hline & & & & \ \hline & & & & \ \hline \end{tabular} \end{table} \end{document}

Mico
  • 506,678
4

The desired feature seems to be very natural, but unfortunately, it is not implemented in internal algorithm of TeX (i.e. in \halign primitive). So, we must to do more complicated macros with compromises if we want such feature. The accepted answer here (by Roland) gives bigger but fixed width of the h and f column. The Mico's answer does a measurement of big multispan, but it must be given outside the table. My solution does measurement inside the table. But we must know what columns will be affected by this problem and do some special settings in the table declaration.

Because I don't know LaTeX, I show the implementation using OpTeX, but the principles are similar. You can run this example by optex document. You can try to change the number of zzz or hhh and you can see, what happens.

\newdimen\bigpartw
\def\bigpart#1{\setbox0=\hbox{#1}\global\bigpartw=\wd0 \box0 }
\def\multipart#1#2;;{%
   \setbox0=\hbox{\ignorespaces #2\unskip}%
   \ifdim\wd0<#1\bigpartw \setbox0=\hbox to#1\bigpartw{\hss\unhbox0\hss}\fi
   \hss\box0\hss}

\table{|c|c|c|:(\multipart{0.5})c(;;)|:(\multipart{0.5})c(;;)|}{ \crl x & \mspan2[c|]{y} & \mspan2[c|]{\bigpart{zzzzzzzzzzzzz} } \crl & t & w & hh & f \crl & a & b & c & d \crl }

\end

wipet
  • 74,238
3

You may use tabularx:

\documentclass{article}

\usepackage{tabularx} \begin{document} \newcolumntype{A}{>{\centering\arraybackslash}X}

\begin{table}[htbp] \centering \begin{tabularx}{\textwidth}{| c | c | c | A | A |} \hline x & \multicolumn{2}{c|}{y} & \multicolumn{2}{c|}{zzzzzzzzzzzzzzzzzzzzzzzzzzzz} \ \hline & t & w & h & f \ \hline & & & & \ \hline \end{tabularx} \end{table}

\end{document}

enter image description here

Roland
  • 6,655