How to draw a side line in table (Using multirow will exceed the width) [duplicate]

\documentclass{article}
\usepackage{}
\usepackage{makecell, multirow, tabularx}
\begin{document}

\begin{table}[h!]
\setlength\extrarowheight{2pt} % for a ever so slightly more open "look"
\begin{tabularx}{\textwidth}{X |c |c |c |c |}
\cline{2-5}
& Mean & N & Std. Deviation & Std. Error Mean \\
\hline
Testing123 Testin123 Testing123 Testin123 & 23\% & 10\% & 279\% & 233\% \\
\hline
\end{tabularx}
\caption{Testing123 Testin123Testing123 Testin123}
\end{table}


Outcome:

    \begin{table}[h!]
\setlength\extrarowheight{2pt} % for a ever so slightly more open "look"
\begin{tabularx}{\textwidth}{X |c |c |c |c |}
\cline{2-5}
& Mean & N & Std. Deviation & Std. Error Mean \\
\hline
\multicolumn{1}{|c|}{Testing123 Testin123 Testing123 Testin123} & 23\% & 10\% & 279\% & 233\% \\
\hline
\end{tabularx}
\caption{Testing123 Testin123Testing123 Testin123}
\end{table}


Outcome:

and I wanted to make "Testing123 Testin123 Testing123 Testin123 " into 2 row.

• It is a different pattern. I had tried \multicolumn{1}{|c|}{Testing123 Testin123 Testing123 Testin123}. following the suggestion on this page, but it came out out of width. Maybe I should rephrase my question to using multicolumn will create the words out of my width.
– aan
Aug 5, 2019 at 11:03

If you look at @Jennifer's code, you will see that the left vertical line in the first header cell (Mean) does not line up with the vertical line in the next row. This is because the vertical bars (|) are not correctly placed. You need to set the | to the right of X column in in the first \multicolumn and remove the \multicolumn command from the second header cell. The first row shall read:

\multicolumn{1}{X|}{} & Mean & N & Std. Deviation & Std. Error Mean \\


Here is a complete MWE:

\documentclass{article}
\usepackage{tabularx, caption}        % Load package caption for better captions
\usepackage{booktabs, ragged2e}       % Use ragged2e to improve ragged text
\setlength\extrarowheight{1pt}        % Both line necessary for decent spacing
\renewcommand{\arraystretch}{1.2}

\begin{document}

\begin{table}[!ht]
\caption{Testing123 Testin123Testing123 Testin123%   % Caption above table
\label{tab:test123}%                             % Label inside caption
}
\begin{tabularx}{\textwidth}{|>{\RaggedRight\arraybackslash}X|c |c |c |c |}
\cline{2-5}
\multicolumn{1}{X|}{}   % Right vertical rules in first header cell
& Mean                  % Remove multicolumn from second header cell
& N & Std. Deviation
& Std. Error Mean \\
\hline
Testing123 Testin123 Testing123 Testin123 & 23\% & 10\% & 279\% & 233\% \\
\hline
\end{tabularx}
\end{table}


However, I suggest you typeset the tabular in a different way:

1. Use booktabs for better rules
2. Get rid of all vertical rules (see the booktabs manual)
3. Use @{} to get rid of left and right side bearings
4. Set the four columns with figures with equal width by defining a new column type
5. Set figure columns right aligned
6. Move the per cent sign to the header

The tabular preamble is:

\begin{tabularx}{\textwidth}{@{}M*{4}{N}@{}}


which means:

1. @{} = remove the side bearing to the left of column one, i.e the small space you see at the start of the first column
2. M = set one column of type M (which we have defined using the command \newcolumntype in the document preamble).
3. *{4}{N} = means create four columns of type N (which we have defined using the command \newcolumntype in the document preamble). It is the same as writing four NNNN in this MWE, but normally it is a short cut.
4. @{} = same as item 1 above for the last column: Remove the side bearing to the right of column five, i.e the small space you see at the end of the last column.

Hopefully, it is now more understandable.

\documentclass{article}
\usepackage{tabularx, caption}          % Load package caption for decent foramtting and spacing
\usepackage{booktabs, ragged2e}         % Use booktabs rules and get rid of vertical rules,
% ragged2e to improved ragged right and left text
\setlength\extrarowheight{1pt}          % Both line necessary for decent spacing, unless you are using booktabs rules.
\renewcommand{\arraystretch}{1.2}

\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hsize=2\hsize\linewidth=\hsize}X}
\newcolumntype{W}{>{\RaggedLeft\arraybackslash\hsize=0.75\hsize\linewidth=\hsize}X}

\begin{document}

\begin{table}[!ht]
\caption{Testing123 Testin123Testing123 Testin123%
\label{tab:test123}%
}
\begin{tabularx}{\textwidth}{@{}Y*{4}{W}@{}}
\toprule
& \small Mean (\%)
& \small N (\%)& \small Std. Deviation~(\%)
& \small Std. Error Mean (\%)\\
\midrule
Testing123 Testin123 Testing123 Testin123 & 23 & 10 & 279 & 233 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

• I cannot get the meaning of \begin{tabularx}{\textwidth}{@{}M*{4}{N}@{}}. It is \begin{tabular}[pos]{cols} and I still cannot figure out the meaning of {@{}M*{4}{N}@{}}.
– aan
Aug 5, 2019 at 21:41
• Actually, if i cancel out \setlength\extrarowheight{1pt} % Both line necessary for decent spacing \renewcommand{\arraystretch}{1.2} \newcolumntype{M}{>{\RaggedRight\arraybackslash\hsize=2\hsize\linewidth=\hsize}X} \newcolumntype{N}{>{\RaggedLeft\arraybackslash\hsize=0.75\hsize\linewidth=\hsize}X} . It still work.
– aan
Aug 5, 2019 at 22:16
• @user193541 Then you have another package defining column Mand N. If I comment out the newcolumntype lines, the MWE does not compile (for obvious reasons). The arraystretch and extrarowheight are in fact not necessary when using booktabs rules. Aug 5, 2019 at 22:39
• @user193541 You may change the M and N to Yand Z Aug 5, 2019 at 22:41
• i get your meaning now. \newcolumntype{Y}{>{\RaggedRight\arraybackslash\hsize=2\hsize\linewidth=\hsize}X} is used in {@{}Y*{4}{W}@{}}. Thanks, very well written!
– aan
Aug 6, 2019 at 0:36

I attached a multicolumn twice to the header row. I hope this is something that you are looking for

\documentclass{article}
\usepackage{}
\usepackage{makecell, multirow, tabularx}
\begin{document}

\begin{table}[h!]
\setlength\extrarowheight{2pt} % for a ever so slightly more open "look"
\begin{tabularx}{\textwidth}{|X|c |c |c |c |}
\cline{2-5}
\multicolumn{1}{l}{}
& \multicolumn{1}{|l|}{Mean} & N & Std. Deviation & Std. Error Mean \\
\hline
Testing123 Testin123 Testing123 Testin123 & 23\% & 10\% & 279\% & 233\% \\
\hline
\end{tabularx}
\caption{Testing123 Testin123Testing123 Testin123}
\end{table}

\end{document}


• Thanks. It works well.
– aan
Aug 5, 2019 at 7:27
• I have a question, why can't we use \multicolumn{1}{|c|}{Testing123 Testin123 Testing123 Testin123}?
– aan
Aug 5, 2019 at 8:23
• @user193541 Because the vertical line also start at the top of the table and that looks weird. Aug 5, 2019 at 9:10
• A suggestion how to improve your answer: \documentclass{article} \usepackage{makecell, tabularx} \begin{document} \begin{table}[ht] \setcellgapes{3pt} \makegapedcells \begin{tabularx}{\linewidth}{|>{\raggedright}X|c |c |c |c |} \cline{2-5} \multicolumn{1}{l|}{} & {Mean} & N & Std. Deviation & Std. Error Mean \\ \hline Testing123 Testin123 Testing123 Testin123 & 23\% & 10\% & 279\% & 233\% \\ \hline \end{tabularx} \caption{Testing123 Testin123 Testing123 Testin123} \end{table} \end{document} Aug 5, 2019 at 9:38