4

I am trying to create a table which uses both multicolumns and multirows, and I am getting some graphical problems.

As you can see on below image, the top right corner is missing a line and the second horizontal line cuts off in the middle of a cell)

Table Image

Find the code below.

\documentclass{article}

%Used to create Macro 1
\usepackage{array}

\usepackage{multirow}

%Used for \textsubscript
\usepackage{fixltx2e}

%Macro
\newcolumntype{"}{@{\hskip\tabcolsep\vrule width 1pt\hskip\tabcolsep}}
\makeatother

\begin{document}


\begin{table}
       \centering
       \begin{tabular}[ht]{|c"c|c|c|c|}

              \hline

              \multirow{2}{*}{\textbf{Change}}          & \multicolumn{2}{c}{\textbf{Main}}       & \multicolumn{2}{c}{\textbf{Sub}}        \\ \cline{2-5}

                     & \textbf{P}                & \textbf{A}                & \textbf{P}                & \textbf{A}         \\ \hline    
              1      & U                         & R\textsubscript{ef}       & R\textsubscript{ef}       & U                  \\ \hline
              2      & U                         & U                         & R\textsubscript{ef}       & R\textsubscript{ef} \\ \hline
              3      & G                         & P                         & P                         & G                  \\ \hline
              4      & P                         & P                         & G                         & G                  \\ \hline
              5      & A                         & A                         & N/A                       & N/A                \\ \hline
              6      & E                         & E                         & N/A                       & N/A                \\ \hline
              7      & E                         & E                         & N/A                       & N/A                \\ \hline
              8      & E                         & E                         & N/A                       & N/A                \\ \hline
              9      & E                         & E                         & N/A                       & N/A                \\ \hline
              10     & E                         & E                         & G                         & G                  \\ \hline
              11     & E                         & E                         & G                         & G                  \\ \hline
              12     & E                         & E                         & G                         & G                  \\ \hline
              13     & R\textsubscript{ed}       & R\textsubscript{ed} &     N/A                         & N/A                \\ \hline


       \end{tabular}
       \caption{X }
       \label{table:X}
\end{table}


\end{document}

If you need any additional information, please leave a comment and I'll add info.

3
  • Please mention the packages you are using Commented May 6, 2015 at 10:10
  • Please add the preamble, this is not a full MWE.
    – Alenanno
    Commented May 6, 2015 at 10:10
  • Sorry about that. I've added a new version which compiles on its own.
    – Willy G
    Commented May 6, 2015 at 10:16

2 Answers 2

8

Check this:

Added | in the tabular specification as

\begin{tabular}[ht]{|c|c|c|c|c|} %%instead of "

and

 \multicolumn{2}{c|}{\textbf{Sub}}  %%note the | after c

giving

\documentclass{article}

%Used to create Macro 1
\usepackage{array}

\usepackage{multirow}

 %Used for \textsubscript
 \usepackage{fixltx2e}

 %Macro

\newcolumntype{"}{@{\hskip\tabcolsep\vrule width 1pt\hskip\tabcolsep}}
\makeatother

\begin{document}


\begin{table}
   \centering
   \begin{tabular}[ht]{|c|c|c|c|c|} %%added | here instead of "

          \hline

          \multirow{2}{*}{\textbf{Change}}          & \multicolumn{2}{c}{\textbf{Main}}       & \multicolumn{2}{c|}{\textbf{Sub}}        \\ %added | after c       \cline{2-5}

                 & \textbf{P}                & \textbf{A}                & \textbf{P}                & \textbf{A}         \\ \hline    
          1      & U                         & R\textsubscript{ef}       & R\textsubscript{ef}       & U                  \\ \hline
          2      & U                         & U                         & R\textsubscript{ef}       & R\textsubscript{ef} \\ \hline
          3      & G                         & P                         & P                         & G                  \\ \hline
          4      & P                         & P                         & G                         & G                  \\ \hline
          5      & A                         & A                         & N/A                       & N/A                \\ \hline
          6      & E                         & E                         & N/A                       & N/A                \\ \hline
          7      & E                         & E                         & N/A                       & N/A                \\ \hline
          8      & E                         & E                         & N/A                       & N/A                \\ \hline
          9      & E                         & E                         & N/A                       & N/A                \\ \hline
          10     & E                         & E                         & G                         & G                  \\ \hline
          11     & E                         & E                         & G                         & G                  \\ \hline
          12     & E                         & E                         & G                         & G                  \\ \hline
          13     & R\textsubscript{ed}       & R\textsubscript{ed} &     N/A                         & N/A                \\ \hline


   \end{tabular}
   \caption{X }
   \label{table:X}
\end{table}


\end{document}

enter image description here

For a thicker vertical line use

\begin{tabular}[ht]{|c!{\vrule width 2pt}c|c|c|c|}

for an output like this:

enter image description here

2
  • Oh, so the macro was the problem. Thanks for this. Any idea how to make the vertical lines thicker?
    – Willy G
    Commented May 6, 2015 at 11:06
  • Use this: \begin{tabular}[ht]{|c!{\vrule width 2pt}c|c|c|c|} in the above table for a thicker vertical line for the first column Commented May 6, 2015 at 11:19
1

For information, here is a way to create that table with {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\newcolumntype{I}{|[tikz={line width=1pt}]}

\begin{table}
\centering
\begin{NiceTabular}{cIcccc}[hvlines]
\RowStyle[nb-rows=2]{\bfseries}
\Block{2-1}{Change}     & \Block{1-4}{} % to prevent a vertical rule between "Main" and "Sub"
                          \Block{1-2}{Main}    & & \Block{1-2}{Sub} \\ 
   & P                  & A                    & P                   & A          \\     
1  & U                  & R\textsubscript{ef}  & R\textsubscript{ef} & U                   \\ 
2  & U                  & U                    & R\textsubscript{ef} & R\textsubscript{ef} \\ 
3  & G                  & P                    & P                   & G                   \\ 
4  & P                  & P                    & G                   & G                   \\ 
5  & A                  & A                    & N/A                 & N/A                 \\ 
6  & E                  & E                    & N/A                 & N/A                 \\ 
7  & E                  & E                    & N/A                 & N/A                 \\ 
8  & E                  & E                    & N/A                 & N/A                 \\ 
9  & E                  & E                    & N/A                 & N/A                 \\ 
10 & E                  & E                    & G                   & G                   \\ 
11 & E                  & E                    & G                   & G                   \\ 
12 & E                  & E                    & G                   & G                   \\ 
13 & R\textsubscript{ed}& R\textsubscript{ed}  &     N/A             & N/A                 \\ 
\end{NiceTabular}
\caption{X}
\label{table:X}
\end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/TikZ nodes under the hood).

Output of the above code

You must log in to answer this question.

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