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

2
  • 2
    Does this answer solve your question? (Of course, you'll need to replace bcbcbcbc with zzzzzzzzzzzzzzzzzzzzzzzz...)
    – Mico
    Dec 23, 2020 at 1:42
  • I'm not sure how I should change my code--I'm a beginner with tables and multicol/multirow
    – Ali
    Dec 23, 2020 at 1:53

3 Answers 3

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

6
  • Did the OP want the first three columns to become wider as well?
    – Mico
    Dec 23, 2020 at 2:00
  • I want them to be fit to the text I'll add in each cell.
    – Ali
    Dec 23, 2020 at 2:02
  • Don´t know. I just though it looks nicer.
    – Roland
    Dec 23, 2020 at 2:02
  • 2
    The column specs should be like this: \begin{tabularx}{\textwidth}{| c | c | c | A | A |} ..
    – AboAmmar
    Dec 23, 2020 at 2:04
  • @ Abo Ammar Yes
    – Roland
    Dec 23, 2020 at 2:07
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}
1
  • 1
    How professional your code is....Marvelous...
    – MadyYuvi
    Dec 23, 2020 at 12:38
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .