2

I am trying to get my table working for last couple of days. But I couldnt fix the problem. The way I want my table to look like is below (figure 1).

enter image description here

With my following code

\begin{table}[p]
\begin{tabular}{ccccccccc}
    \hline
    Compound               & \multicolumn{4}{c}{A}                                                                          & \multicolumn{4}{c}{B}                                                                         \\
    AT                     & \#1                     & \#2                    & \#1                    & \#2                    & \#1                    & \#2                    & \#1                    & \#2                    \\
    feedstock              & \multicolumn{2}{c}{C}                          & \multicolumn{2}{c}{B}                         & \multicolumn{2}{c}{C}                         & \multicolumn{2}{c}{B}                         \\ \hline
    Par 1                  & 1                      & 1                     & 2                     & 2                     & 3                     & 3                     & 4                     & 4                     \\
    Par 2                  & 1                      & 1                     & 2                     & 2                     & 3                     & 3                     & 4                     & 4                     \\
    Par 3                  & 1                      & 1                     & 2                     & 2                     & 3                     & 3                     & 4                     & 4                     \\
    Par 4                  & \multicolumn{2}{c}{1}                          & \multicolumn{2}{c}{2}                         & \multicolumn{2}{c}{3}                         & \multicolumn{2}{c}{4}                         \\ \hline
    \multicolumn{9}{l}{Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah} \\ \hline
\end{tabular} \end{table}

I am getting (figure 2): enter image description here

I tried using tabularx and defined the size of each column. It worked until I added last long row (which is a merged form of all 9 columns). Also the the header "B" and last coulmn (figure 1) is going out of the page in figure 2.

It would be nice if someone can help me. Also, detailed explanation of the solution would be appreciated.

Thanks in advance.

4
  • 2
    By using the l column type for the super-long word, you’ve disabled automatic line-breaking. Even if you chose a different column type, i.e., a column type that allows line breaking, how is LaTeX even supposed to decide how and where to insert line breaks in the super-long word?
    – Mico
    Commented Jan 5, 2019 at 18:51
  • Thanks, @Mico for your replay. I changed now a long word with a bunch of "Blah Blah"s (so latex should know where to break the line) and also changed column type from l to c. However, the problem isn't solved. Definitely, I am doing something wrong. Commented Jan 5, 2019 at 19:21
  • 2
    The c, l, and r column types do no allow line breaking.
    – Mico
    Commented Jan 5, 2019 at 19:26
  • 2
    we need to know your page layout and desired width of table. ...
    – Zarko
    Commented Jan 5, 2019 at 19:44

3 Answers 3

5

As far as I can tell, the "Blah Blah ..." material is only indirectly related to the preceding, table-like material. Is the "Blah Blah" material maybe some kind of legend? If this guess is correct, you'd be better off not placing it inside the tabular environment. Instead, I suggest you load the threeparttable package and place the legend in a tablenotes environment, which (thanks to the behind-the-scenes work of the threeparttable environment) will automatically be as wide as the preceding tabular environment.

In case you're curious about the name threeparttable of the package: It can handle three types of elements: The table caption (which is absent in the case of your code), the tabular-like material, and the tablenotes material. The package works, though, even if there is no caption.

enter image description here

\documentclass{article}
\usepackage[flushleft]{threeparttable}
\begin{document}
\begin{table}
\centering
\begin{threeparttable}
\begin{tabular}{@{} l *{8}{c} @{}}
\hline
Compound  & \multicolumn{4}{c}{A} & \multicolumn{4}{c}{B} \\
AT  & \#1 & \#2 & \#1 & \#2 & \#1 & \#2 & \#1 & \#2 \\
feedstock & \multicolumn{2}{c}{C} & \multicolumn{2}{c}{B} 
          & \multicolumn{2}{c}{C} & \multicolumn{2}{c}{B} \\ 
\hline
 Par 1 & 1 & 1 & 2 & 2 & 3 & 3 & 4 & 4 \\
 Par 2 & 1 & 1 & 2 & 2 & 3 & 3 & 4 & 4 \\
 Par 3 & 1 & 1 & 2 & 2 & 3 & 3 & 4 & 4 \\
 Par 4 & \multicolumn{2}{c}{1} & \multicolumn{2}{c}{2} 
       & \multicolumn{2}{c}{3} & \multicolumn{2}{c}{4} \\ 
\hline
\end{tabular}

\smallskip
\begin{tablenotes}
\item[]Notes: Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}
1
  • Thanks a lot @Mico ! I really liked this arrangement of having tablenotes. It precisely solved my problem. :) Commented Jan 5, 2019 at 21:01
4

since you tag your question with tabularx i suggest to use this package. beside it more nice result you will obtain by use of the booktabs package for horizontal rules:

\documentclass{article}
\usepackage{booktabs, tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mcc[1]{\multicolumn{2}{c}{#1}}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[p]
\begin{tabularx}{\linewidth}{@{} c *{8}{C} @{}}
    \toprule
Compound    & \multicolumn{4}{c}{A}         & \multicolumn{4}{c}{B}         \\
    \cmidrule(lr){2-5}\cmidrule(l){6-9}
    AT      & \#1   & \#2   & \#1   & \#2   & \#1   & \#2   & \#1   & \#2   \\
    \cmidrule(lr){2-3}\cmidrule(lr){4-5}
    \cmidrule(lr){6-7}\cmidrule(lr){8-9}
feedstock   & \mcc{C}       & \mcc{B}       & \mcc{C}       & \mcc{B}       \\
    \midrule
Par 1       & 1     & 1     & 2     & 2     & 3     & 3     & 4     & 4     \\
Par 2       & 1     & 1     & 2     & 2     & 3     & 3     & 4     & 4     \\
Par 3       & 1     & 1     & 2     & 2     & 3     & 3     & 4     & 4     \\
Par 4       & \mcc{1}       & \mcc{2}       & \mcc{3}       & \mcc{4}       \\
    \midrule
\multicolumn{9}{@{} p{\linewidth} @{}}{Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah} \\
    \bottomrule
\end{tabularx}
    \end{table}
\end{document}

enter image description here

(red lines indicate text border)

1

You can add width argument to your columns, like \multicolumn{9}{p{\linewidth}}. Perhaps \multicolumn{9}{p{.7\linewidth}} will do even better. Addding this to your example produces the following output https://i.sstatic.net/Axj6X.jpg

Here is the complete code of mwe

\documentclass{article}
\begin{document}
\begin{table}[p]
\begin{tabular}{ccccccccc}
\hline
Compound               & \multicolumn{4}{c}{A}                                                                          & \multicolumn{4}{c}{B}                                                                         \\
AT                     & \#1                     & \#2                    & \#1                    & \#2                    & \#1                    & \#2                    & \#1                    & \#2                    \\
feedstock              & \multicolumn{2}{c}{C}                          & \multicolumn{2}{c}{B}                         & \multicolumn{2}{c}{C}                         & \multicolumn{2}{c}{B}                         \\ \hline
Par 1                  & 1                      & 1                     & 2                     & 2                     & 3                     & 3                     & 4                     & 4                     \\
Par 2                  & 1                      & 1                     & 2                     & 2                     & 3                     & 3                     & 4                     & 4                     \\
Par 3                  & 1                      & 1                     & 2                     & 2                     & 3                     & 3                     & 4                     & 4                     \\
Par 4                  & \multicolumn{2}{c}{1}                          & \multicolumn{2}{c}{2}                         & \multicolumn{2}{c}{3}                         & \multicolumn{2}{c}{4}                         \\ \hline
\multicolumn{9}{p{\linewidth}}{Blah Blah Blah Blah Blah Blah Blah Blah Blah 
Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah} \\ \hline
\end{tabular} \end{table}
\end{document}
1
  • 1
    Your solution has the (presumably unintended) side-effect of making the final column very wide indeed in case the combined width of the 9 columns is less than \linewidth. Can you think of a way of avoiding this issue?
    – Mico
    Commented Jan 5, 2019 at 20:15

You must log in to answer this question.

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