3

The following code produces a table. On the right half (frogs) the columns are not correctly distributed as on the left half (birds). How to make it work correctly? enter image description here

\documentclass{article}
\usepackage{multirow}   
\begin{document}
\begin{table}
\centering
\begin{tabular}{c|c| c|c| c|c| c|c |c}
\multirow{2}{*}{AAA} & \multicolumn{4}{c|}{birds} & \multicolumn{4}{c}{frogs} \\ \cline{2-9}
& \multicolumn{2}{c|}{w/o huge hat} & \multicolumn{2}{c|}{w/ huge hat} & \multicolumn{2}{c|}{w/o huge hat} & \multicolumn{2}{c}{w/ huge hat} \\
\hline
$\frac{1}{3}$ & 2.75 & 3.6 & 2.75 & 3.68 & 5 & 5 & 5 & 5 \\ \hline
1 & 8.3 & 10 & 8.2 & 10 & 15 & 15 & 15 & 15 \\ \hline
3 & 24.25 & 28 & 24.25 & 28.5 & 44 & 48 & 45 & 45 \\ \hline\hline
$\frac{p}{q}$ & $\frac{31}{97}$ & $\frac{53}{157}$ & $\frac{31}{97}$ & $\frac{53}{157}$ & $\frac{31}{97}$ & $\frac{53}{157}$ & $\frac{31}{97}$ & $\frac{53}{157}$ \\ 
\end{tabular}
\end{table}
\end{document}
3

Similar to @Zarko's answer, but omitting all vertical lines and using fewer but well-spaced horizontal lines in order to create a more open "look".

enter image description here

\documentclass{article}
\usepackage{tabularx,booktabs,ragged2e}  
\newcolumntype{C}{>{\Centering}X} 

\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{@{} c *{8}{C} @{}}
AAA & \multicolumn{4}{c}{birds} & \multicolumn{4}{c@{}}{frogs} \\ 
\cmidrule(lr){2-5} \cmidrule(l){6-9}
& \multicolumn{2}{c}{w/o huge hat} & \multicolumn{2}{c}{w/ huge hat} 
& \multicolumn{2}{c}{w/o huge hat} & \multicolumn{2}{c@{}}{w/ huge hat} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(l){8-9} 
$1/3$ & 2.75 & 3.6 & 2.75 & 3.68 & 5 & 5 & 5 & 5 \\
1 & 8.3 & 10 & 8.2 & 10 & 15 & 15 & 15 & 15 \\ 
3 & 24.25 & 28 & 24.25 & 28.5 & 44 & 48 & 45 & 45 \\ 
\midrule
$p/q$ & $\frac{31}{97}$ & $\frac{53}{157}$ & $\frac{31}{97}$ & $\frac{53}{157}$ 
   & $\frac{31}{97}$ & $\frac{53}{157}$ & $\frac{31}{97}$ & $\frac{53}{157}$ \\ 
\end{tabularx}
\end{table}
\end{document}
3
  • Thank you! Is it possible to add a vertical line after the 'AAA' column while not extending to the 'p/q' row?
    – xiaohuamao
    May 3 '20 at 18:13
  • @xiaohuamao - Short answer: No. Longer answer: The whole point of not using any vertical lines and using fewer, but well spaced horizontal lines is to give the table an open and inviting look. If, however, you prefer a closed "look", then this solution is definitely not the right one for you.
    – Mico
    May 3 '20 at 18:26
  • @xiaohuamao - Even longer answer: If the purpose of the vertical bar between the first and second column is not to reintroduce a closed look but just to provide a bit more visual separation between the header column and the 8 data columns, then no vertical bar is needed. Instead, just replace \begin{tabularx}{\textwidth}{@{} c *{8}{C} @{}} in the answer shown above with, say, \begin{tabularx}{\textwidth}{@{} c @{\qquad} *{8}{C} @{}}. Give it a try.
    – Mico
    May 3 '20 at 18:36
3

The width of the multi column cell withw/o huge hat is bigger than sum of widths of the spanned columns. To have equal width that columns you need to define their widths (for example as p{<width>}) or simpler use tabularx with all columns with equal widths:

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

\begin{document}
\begin{table}
\centering
\begin{tabularx}{\linewidth}{C|C| C|C| C|C| C|C |C}
\multirow{2}{*}{AAA} & \multicolumn{4}{c|}{birds} & \multicolumn{4}{c}{frogs} \\ \cline{2-9}
& \multicolumn{2}{c|}{w/o huge hat} & \multicolumn{2}{c|}{w/ huge hat} & \multicolumn{2}{c|}{w/o huge hat} & \multicolumn{2}{c}{w/ huge hat} \\
\hline
$\frac{1}{3}$ & 2.75 & 3.6 & 2.75 & 3.68 & 5 & 5 & 5 & 5 \\ \hline
1 & 8.3 & 10 & 8.2 & 10 & 15 & 15 & 15 & 15 \\ \hline
3 & 24.25 & 28 & 24.25 & 28.5 & 44 & 48 & 45 & 45 \\ \hline\hline
$\frac{p}{q}$ & $\frac{31}{97}$ & $\frac{53}{157}$ & $\frac{31}{97}$ & $\frac{53}{157}$ & $\frac{31}{97}$ & $\frac{53}{157}$ & $\frac{31}{97}$ & $\frac{53}{157}$ \\
\end{tabularx}
\end{table}
\end{document}

enter image description here

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.