4

I have been following the tutorial to format a table in Latex but I have a consistent problem. What I am aiming for is this: enter image description here

But with the following code:

\begin{table}[h!]
  \begin{center}
    \caption{Multicolumn table.}
    \label{tab:table1}
     \begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|p(3cm)} 
      \hline
      |c| & \multicolumn{5}{|c|}{WordCNN} & \multicolumn{5}{|c|}{WordLSTM} & \multicolumn{5}{|c|}{BERT}
      \hline
      |c| & MR & IMDB & YELP & YG & Fake & MR & IMDB & YELP & YG & Fake & MR & IMDB & YELP & YG & Fake
      \hline
    \end{tabular}
  \end{center}
\end{table}

So far, I have achieved this: enter image description here

I see that the placement of the table is off and I have problems with blank spaces. I have tried to work with solutions with the following sites: https://latex-tutorial.com/tutorials/tables/#Csv Latex table goes out of page border But no luck. Any help would be greatly appreciated, many thanks!

4
  • 2
    Welcome to TeX.SX! Please make your code snippet compilable, i.e. wrap it into some code so that the whole thing starts with \documentclass and ends with \end{document}. Remember to include the packages needed. Now, to your example: What do you aim to achieve with putting an array parameter like |c| at the beginning of each row of your table? It does not work like that and only has a function as argument, for example, to the tabular environment or the \multicolumn macro. Nov 9 '21 at 16:30
  • Your code results in a bunch or error messages. Once you end up with errore, don't even look at the "output", as even if you get something that on first glance resembles a pdf file, there can still be issues with it. After an error, TeX only tries to recover enough to syntax check more of the file, it does not try to make sensible output after an error. Let's start getting rid of the error messages: The width argument of a p type column should be placed in a set of {}, not a set of ().
    – leandriis
    Nov 9 '21 at 19:12
  • 1
    You define a total of 15 columns, but the line & \multicolumn{5}{|c|}{WordCNN} & \multicolumn{5}{|c|}{WordLSTM} & \multicolumn{5}{|c|}{BERT} uses a total of 16 columns, hence the error message " & \multicolumn{5}{|c|}{WordCNN} & \multicolumn{5}{|c|}{WordLSTM} & \multicolumn{5}{|c|}{BERT}". Also, each table row must end with \\ . These are missing in your code, as well.
    – leandriis
    Nov 9 '21 at 19:14
  • What about using booktabs (and consequently reducing/removing the use of vertical rules)?
    – Werner
    Nov 10 '21 at 1:14
4

@leandriis has already mentioned some errors in your code. Also, if you have many identical type of columns, you may define all of them at once using the *{num}{str} syntax. This increases the readability of your code and decreases the probability of error.

Your table is too wide, even using the footnotesize font, you need to use landscape mode to show the full table. For landscape layout, the code and output are as follows:

\documentclass{article}

\usepackage[landscape]{geometry}
\usepackage{caption}
%------------------------------
% to show page layout, should be excluded in original document
\usepackage{showframe}
\renewcommand*\ShowFrameColor{\color{red}}
%-----------------------------
\begin{document}
\begin{table}[htbp]
    \centering
    \caption{Multicolumn table.}
    \label{tab:table1}
    \footnotesize
     \begin{tabular}{p{3cm}|*{3}{*{5}{c}|}} 
      \hline
       & \multicolumn{5}{c|}{WordCNN} & \multicolumn{5}{c|}{WordLSTM} & \multicolumn{5}{c|}{BERT}\\
      %\hline
       & MR & IMDB & YELP & YG & Fake & MR & IMDB & YELP & YG & Fake & MR & IMDB & YELP & YG & Fake\\
      \hline
      Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 & 12 & 15.0 & 12.0 & 12.0 & 12.0 & 12 & 15.0 & 12.0 & 12.0 & 12.0\\
      Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 & 12 & 15.0 & 12.0 & 12.0 & 12.0 & 12 & 15.0 & 12.0 & 12.0 & 12.0\\
      Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 & 12 & 15.0 & 12.0 & 12.0 & 12.0 & 12 & 15.0 & 12.0 & 12.0 & 12.0\\
      Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 & 12 & 15.0 & 12.0 & 12.0 & 12.0 & 12 & 15.0 & 12.0 & 12.0 & 12.0\\
      \hline
      Average Text Length & 12 & 15.0 & 12.0 & 12.0 & 12.0 & 12 & 15.0 & 12.0 & 12.0 & 12.0 & 12 & 15.0 & 12.0 & 12.0 & 12.0\\
      \hline
    \end{tabular}
\end{table}
\end{document}

landscape output

However, you can modify your table to be fit in portrait layout. One approach may be as follows:

output protrait

\documentclass{article}

\usepackage{caption}
\usepackage{multirow}
%------------------------------
% to show page layout, should be excluded in original document
\usepackage{showframe}
\renewcommand*\ShowFrameColor{\color{red}}
%-----------------------------

\begin{document}
\begin{table}[htbp]
    \centering
    \caption{Multicolumn table.}
    \label{tab:table1}
     \begin{tabular}{ll|*{5}{c}|} 
      \cline{3-7}
      & & MR & IMDB & YELP & YG & Fake\\
      \hline%\cline{3-7}
      \multirow{5}{*}{WordCNN} & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      \hline
      \multirow{5}{*}{WordSTM} & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      \hline
      \multirow{5}{*}{BERT} & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      & Original Accuracy & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      \hline
      & Average Text Length & 12 & 15.0 & 12.0 & 12.0 & 12.0 \\
      \hline
    \end{tabular}
\end{table}
\end{document}

Also, for formal tables, you may consider using the booktabs package.

3

With tabularray and assumed, that page layout is determined by \usepackage[margin=25mm]{geometry}, than is possible to fir your table to test area in portrait page orientation:

enter image description here

\documentclass{article}

\usepackage[margin=25mm]{geometry}
\usepackage{caption}
\usepackage{tabularray}
%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[ht]
    \centering
    \caption{Multicolumn table.}
    \label{tab:table1}
    \footnotesize
\begin{tblr}{hline{1,Z} = 1pt, hline{3,Y} = 0.6pt,
             vline{2,7,12},        
              colsep = 2.1pt,
             colspec = {@{} l *{15}{c} @{}},
           column{1} = {font=\bfseries},
            row{1,2} = {font=\bfseries},
             }
\SetCell[r=2]{l}
    &   \SetCell[c=5]{c} WordCNN
        &   &   &   &   &   \SetCell[c=5]{c} WordLSTM
                            &   &   &   &   &   \SetCell[c=5]{c} BERT
                                                &   &   &   &   \\
    & MR    & IMDB  & YELP  & YG    & Fake 
            & MR    & IMDB  & YELP  & YG    & Fake  
                    & MR    & IMDB  & YELP  & YG    & Fake      \\
Original Accuracy 
    & 12    & 15.0  & 12.0  & 12.0  & 12.0 
            & 12    & 15.0  & 12.0  & 12.0  & 12.0 
                    & 12    & 15.0  & 12.0  & 12.0  & 12.0      \\
Original Accuracy 
    & 12    & 15.0  & 12.0  & 12.0  & 12.0  
            & 12    & 15.0  & 12.0  & 12.0  & 12.0 
                    & 12    & 15.0  & 12.0  & 12.0  & 12.0      \\
Original Accuracy 
    & 12    & 15.0  & 12.0  & 12.0  & 12.0 
            & 12    & 15.0  & 12.0  & 12.0  & 12.0 
                    & 12    & 15.0  & 12.0  & 12.0  & 12.0      \\
Original Accuracy 
    & 12    & 15.0  & 12.0  & 12.0  & 12.0
            & 12    & 15.0  & 12.0  & 12.0  & 12.0
                    & 12    & 15.0  & 12.0  & 12.0  & 12.0      \\
Average Text Length 
    & 12    & 15.0  & 12.0  & 12.0  & 12.0
            & 12    & 15.0  & 12.0  & 12.0  & 12.0
                    & 12    & 15.0  & 12.0  & 12.0  & 12.0      \\
\end{tblr}
    \end{table}
\end{document}

Edit: In cases, when the text width is smaller as is anticipated in above MWE, or you like to use bigger font size, for example \small, than you may allow that table can protrude to outside of text area. This can be simple done by use of the adjustwidth* environment defined in the changepage package. For example:

% existed preamble
\usepackage[strict]{changepage}

\begin{document}
    \begin{table}[ht]
\begin{adjustwidth*}{}{-11mm}
    \centering
    \caption{Multicolumn table.}
    \label{tab:table1}
    \small
% table
\end{adjustwidth*}
    \end{table}
\end{document}

In this case the table looks as follows:

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.