3
\documentclass{article}
\usepackage{tabularx}
\usepackage{multirow}
\begin{document}

\begin{table}[h!]
\setlength\extrarowheight{2pt} % for a ever so slightly more open "look"
    \begin{tabularx}{\textwidth}{c X | c | c | c |}
    \cline{3-5}
    &  & \multicolumn{3}{c|}{Paired Differences} \\
    \cline{3-5}
     & & Mean &  Std. Deviation & Std. Error Mean \\
         \hline
    Pair 1 & Testing 1 and Testing 2 and Testing 3 & 4.20\% & 44.6\% & 4.54\%  \\
    \hline
      \end{tabularx}
\caption{Testing 123}
\end{table}

\end{document}

Outcome:

enter image description here

I cannot figure out. If I added \begin{tabularx}{\textwidth}{|c X | c | c | c |} It will not be working

moewe
  • 175,683
aan
  • 2,663
  • 2
    Try \multicolumn{1}{|c}{Pair 1} or just add it to the main column description and use \multicolumn to remove it where not wanted. – John Kormylo Aug 03 '19 at 14:41
  • Thanks, it work! – aan Aug 03 '19 at 14:43
  • On an completely unrelated note: with booktabs your table looks better: https://pastebin.com/r38axb1c. If you'd like to vertically center the cells, look into https://tex.stackexchange.com/questions/7208/how-to-vertically-center-the-text-of-the-cells. – Oleg Lobachev Aug 03 '19 at 15:27
  • Why do you ask the same question two times? – Sveinung Aug 05 '19 at 10:31
  • @Sveinung It is 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. – aan Aug 05 '19 at 11:01

1 Answers1

4

like this?

enter image description here

\documentclass{article}
\usepackage{multirow, tabularx}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c|}{#2}}
\usepackage{siunitx}     \begin{document}
    \begin{table}[ht]
\setlength\extrarowheight{2pt} % for a ever so slightly more open "look"
    \begin{tabularx}{\linewidth}{|c X |                % <---
                             *{3}{S[table-format=2.2,  % <---
                                    table-space-text-post=\,\%]<{\,\%}|}} 
    \cline{3-5}
\mcc[2]{} % <---
        & \mcc[3]{Paired Differences}       \cr
    \cline{3-5}
\mcc[2]{} % <---
        & \mcc{Mean} & \mcc{Std. Deviation} & \mcc{Std. Error Mean} \cr
         \hline
Pair 1 & Testing 1 and Testing 2 and Testing 3
        & 4.20  & 44.6  & 4.54                          \cr
    \hline
      \end{tabularx}
\caption{Testing 123}
    \end{table}
\end{document}

Addendum: Less sophisticated version of above answer is:

\documentclass{article}
\usepackage{multirow, tabularx}
\usepackage{siunitx}     


\begin{document}
    \begin{table}[ht]
\setlength\extrarowheight{2pt} % for a ever so slightly more open "look"
    \begin{tabularx}{\linewidth}{|c X |                % <---
                             *{3}{S[table-format=2.2,  % <---
                                    table-space-text-post=\,\%]<{\,\%}|}}
    \cline{3-5}
\multicolumn{2}{c|}{} 
        & \multicolumn{3}{c|}{Paired Differences}               \cr
    \cline{3-5}
\multicolumn{2}{c|}{} 
        & \multicolumn{1}{c|}{Mean} 
                & \multicolumn{1}{c|}{Std. Deviation} 
                        & \multicolumn{1}{c|}{Std. Error Mean}  \cr
         \hline
Pair 1 & Testing 1 and Testing 2 and Testing 3
        & 4.20  & 44.6  & 4.54                                  \cr
    \hline
      \end{tabularx}
\caption{Testing 123}
    \end{table}
\end{document}

where S column type is defined in siunitx package. Details of its options and use see the package documentation, (sub)section 5.14 Tabular material, pp 44. Short summary:

  • in tabular preamble are defined number of columns their types:

    • c column type center cells contents (and it is wide as text is long)
    • X column type exist in tabularx table environment. It width is calculate by package, contents in it is set as paragraph, meaning that automatically break lines
    • vertical bars | define vertical lines in table. It are present in all cells which are not overwrite with \multicolumn cells.
  • \multicolumn command merge adjacent cells into one cell (or define new column type for single cells)

  • table-format=2.2 means <integer part>.<decimal part> of number. Numbers in column are aligned at decimal point
  • table-space-text-post=\,\% determine additional to horizontal space after number (reserved for small space \, and symbol %

For more information I strongly encourage you to read some introductory text about table writing. For example Wiki book: Tables and of course in the packages documentations. They are part of your LaTeX installation (in latex/doc folder) or they are available on CTAN archive.

Concerning the original answer: - package xparse (stored in doc/generic folder, as stated in its document documentation is:

The xparse package provides a high-level interface for producing document-level commands. In that way, it is intended as a replacement for the LATEX 2ε \newcommand macro. However, xparse works so that the interface to a function (optional arguments, stars and mandatory arguments, for example) is separate from the internal implementation. xparse provides a normalised input for the internal form of a function, independent of the document-level argument arrangement.

so with \NewExpandableDocumentCommand\mcc{O{1}m}{...} is defined new command which enable witting shorter code. For example:

  • O{1} is option 1 (in our case number of columns. Is option is not used, number of columns is 1 . m is mandatory argument, which in our case is a text in the multi column cell.
  • example of its uses are:
    • \mcc{text} is defined as equivalent to code \multicolumn{1}{c|}{text} (see preamble in original answer)
    • \mcc[r]{text over three columns} is equivalent for \multicolumn{3}{c|}{text over three columns}`
Zarko
  • 296,517
  • Thanks! Worked well. One question, how to cancel off the verticle line between the Pair 1 and Testing 1 and testing 2 and testing 3? – aan Aug 04 '19 at 16:33
  • @user193541, just remove | between c and X column type. See corrected answer. – Zarko Aug 04 '19 at 16:42
  • Thanks. Hard to understand what \mcc doing. It is well very done. – aan Aug 04 '19 at 16:53
  • 1
    @user193541, mcc is new command with optional number of spanned columns (if it bigger than 1) for short writing \multicolumn{<number of spanned columns>}{c|}{< text in cell>}. Defined is by help of `xparse˙package. if answer solve your problem, you can accept it ;-). – Zarko Aug 04 '19 at 17:12
  • Thanks, what does *{3}{S[table-format=2.2, % <--- table-space-text-post=\,\%]<{\,\%}|}} means? – aan Aug 04 '19 at 17:22
  • 1
    @user193541, it defines three S columns' type, which enables align numbers in columns at their decimal points. For details see siunitx package documentation, subsection 5.14 Tabular material. – Zarko Aug 04 '19 at 17:39