1

I want to create this table in LaTeX

enter image description here

and I wrote the following code:

\centering
\begin{tabular}{|l|c|c|c|}
\hline
\multicolumn{2}{|c|}{ \multirow{2}{*}{x}} \\ y& z\tabularnewline
\hline
\multirow{2}{*}{alpha} \\ & T1  & 0.37\% &68\\
\cline{2-4}
& T2 & 0.87\% & 45\\
\cline{2-4}
& T3 & 1.38\% & 49\\
\cline{2-4}
& T4 & 1.38\% & 49\\
\hline
\multirow{2}{*} {BETA}\\ & V1 & 0.5\% & 58\\
\cline{2-3}
& V2  & 60\% & 1\\
\cline{2-3}
& V3 & 5\% & 12\\
\cline{2-3}
& V4 & 60\% & 1\\
\hline
{sigma} 30.12\% & 2\\
\hline
{eta} & 10\% & 6\\
\hline
{pi} 9\% & 8 \\
\hline
\end{tabular}

but it did not give me the purpose thanks a lot in advance for any help

  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – ebosi Nov 29 '16 at 15:30
1

Here is a solution, where I tried to make the structure really obvious. Basically, your code is correct, there are just some dummy mistakes (i.e. creating a new line too "early", forgotting some \multicolumn{2}{|c|}{<content>})

\documentclass{scrartcl}
    \usepackage{array}
    \usepackage{multicol}
    \usepackage{multirow}
\begin{document}
    \centering
    \begin{tabular}{|l|c|c|c|}
        \hline
                \multicolumn{2}{|c|}{x} 
            &
                y
            & 
                z
        \\
        \hline
                \multirow{4}{*}{alpha}  
            &   
                T1  
            & 
                0.37\% 
            &
                68
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T2 
            & 
                0.87\% 
            & 
                45
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T3 
            & 
                1.38\% 
            & 
                49
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T4 
            & 
                1.38\% 
            & 
                49
        \\
        \hline
                \multirow{4}{*} {BETA} 
            & 
                V1 
            & 
                0.5\% 
            & 
                58
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T2 
            & 
                0.87\% 
            & 
                45
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T3 
            & 
                1.38\% 
            & 
                49
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T4 
            & 
                1.38\% 
            & 
                49
            \\
        \hline
                \multicolumn{2}{|c|}{sigma} 
            &
                30.12\% 
            & 
                2
        \\
        \hline
                \multicolumn{2}{|c|}{eta} 
            & 
                10\% 
            & 
                6
        \\
        \hline
                \multicolumn{2}{|c|}{pi}
            &
                9\% 
            & 
                8 
        \\
        \hline
    \end{tabular}
\end{document}

enter image description here

You can improve your table using the booktabs package that defines following macros \toprule, \midrule , and \bottomrule :

\documentclass{scrartcl}
    \usepackage{array}
    \usepackage{multicol}
    \usepackage{multirow}
    \usepackage{booktabs}
\begin{document}
    \centering
    \begin{tabular}{lccc}
        \toprule
        \multicolumn{2}{c}{\textbf{x}} 
        &
        \textbf{y}
        & 
        \textbf{z}
        \\
        \midrule
        \multirow{4}{*}{$\alpha$}  
        &   
        T1  
        & 
        0.37\% 
        &
        68
        \\
        %note that there is an empty cell here
        & 
        T2 
        & 
        0.87\% 
        & 
        45
        \\
        %note that there is an empty cell here
        & 
        T3 
        & 
        1.38\% 
        & 
        49
        \\
        %note that there is an empty cell here
        & 
        T4 
        & 
        1.38\% 
        & 
        49
        \\
        \midrule
        \multirow{4}{*} {$\beta$} 
        & 
        V1 
        & 
        0.5\% 
        & 
        58
        \\
        %note that there is an empty cell here
        & 
        T2 
        & 
        0.87\% 
        & 
        45
        \\
        %note that there is an empty cell here
        & 
        T3 
        & 
        1.38\% 
        & 
        49
        \\
        %note that there is an empty cell here
        & 
        T4 
        & 
        1.38\% 
        & 
        49
        \\
        \midrule
        \multicolumn{2}{l}{$\sigma$} 
        &
        30.12\% 
        & 
        2
        \\\midrule
        \multicolumn{2}{l}{$\eta$} 
        & 
        10\% 
        & 
        6
        \\\midrule
        \multicolumn{2}{l}{$\pi$}
        &
        9\% 
        & 
        8 
        \\
        \bottomrule
    \end{tabular}
\end{document}

enter image description here

Then, you can still improve by aligning % characters, etc. You should have a look on A short introdution to LaTeX for more details...

0

Reproduction of table showed in question

enter image description here

is obtained with the following MWE:

\documentclass[twocolumn,10pt]{article}
\usepackage[margin=25mm]{geometry}
\usepackage{multirow,tabularx}
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}
    \newcolumntype{R}{>{\raggedleft \arraybackslash}X}
    \newcommand\mcl[1]{\multicolumn{2}{|l|}{#1}}
\usepackage[table]{xcolor}

\begin{document}
    \begin{table}[ht]
\centering
\renewcommand\arraystretch{1.2}
\setlength\tabcolsep{4pt}
\begin{tabularx}{\linewidth}{|>{\hsize=0.9\hsize\columncolor{gray!30}}L
                             |>{\hsize=0.9\hsize\columncolor{gray!30}}L   
                             |>{\hsize=0.6\hsize}L
                              >{\hsize=0.6\hsize}R
                             |R|}
\hline
\rowcolor{gray!30}
\mcl{x} 
    &   \multicolumn{2}{l|}{y}      & \multicolumn{1}{l|}{z}     \tabularnewline
\hline
\multirow[t]{4}{=}{alpha} 
    &   T1  &   0.37\%  &           &   68  \tabularnewline
\cline{2-5}
    &   T2  & \cellcolor{cyan!50} 0.87\%  
                        & \cellcolor{cyan!50}          
                                    & \cellcolor{cyan!50} 45  \tabularnewline
\cline{2-5}
    &   T3  &   1.38\%  &           &   49  \tabularnewline
\cline{2-5}
    & T4    &   1.38\%  &           &   49  \tabularnewline
\hline
\multirow[t]{4}{=}{BETA}
    &   V1  &   0.5\%   &           &   58  \tabularnewline
\cline{2-5}
    &   V2  &           &   60\%    &    1  \tabularnewline
\cline{2-5}
    &   V3  & \cellcolor{cyan!50} 
                        & \cellcolor{cyan!50} 5\%     
                                    & \cellcolor{cyan!50} 12        \tabularnewline
\cline{2-5}
    &   V4  & \cellcolor{cyan!60!gray!30} 
                        & \cellcolor{cyan!50!gray!30} 60\%    
                                    & \cellcolor{cyan!60!gray!30} 1 \tabularnewline
\hline
\mcl{\cellcolor{gray!30} sigma} 
            & \cellcolor{cyan!60!gray!30} 30.12\% 
                        & \cellcolor{cyan!60!gray!30} 
                                    & \cellcolor{cyan!60!gray!30} 2 \tabularnewline
\hline
\mcl{\cellcolor{gray!30} eta}   
            &           &   10\%    &    6  \tabularnewline
\hline
\mcl{\cellcolor{gray!30} pi}    
            & \cellcolor{cyan!70} 
                        & \cellcolor{cyan!70} 9\%     
                                    & \cellcolor{cyan!70} 8  \tabularnewline
\hline
\end{tabularx}
    \end{table}
\end{document}

For percentage values of y is used two columns, one for left aligned and another for right aligned numbers. Columns and cells are colored with package xcolor with option table.

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.