6

MWE

\documentclass[a4paper,11pt]{article}
\usepackage{multirow}
\usepackage{array}
 \begin{document}

\begin{tabular}{|>{\centering\arraybackslash}m{2cm}
                |>{\centering\arraybackslash}m{2.4cm}|}\hline
X & Y \\ \hline
\multirow{2}{*}{No problem} & One Line \\ \cline{2-1} 
                            & Two Line \\ \hline
\multirow{5}{*}{Problem} & One Line \\ \cline{2-1} 
                         & 1 \newline 2 \newline 3 \newline 4\\ \hline
\end{tabular}

\end{document}

and displaying

enter image description here.

Only last row (4) is centered. 1, 2, and 3 not center. Does anyone know how to fix it?

6

Here you are:

\documentclass[a4paper,11pt]{article}
\usepackage{multirow}
\usepackage{array}
 \begin{document}


\begin{tabular}{|>{\centering\arraybackslash}m{2cm}
                |>{\centering\arraybackslash}m{2.4cm}|}\hline
X & Y \\ \hline
\multirow{2}{*}{No problem} & One Line \\ \cline{2-1} 
                            & Two Line \\ \hline
\multirow{5}{*}{Problem} & One Line \\ \cline{2-1} 
                         & \hfil1 \newline \null\hfil 2 \newline\null\hfil 3 \newline 4\\ \hline
\end{tabular}

\end{document}

\hfil corrects additional horizontal space added by \newline, \null is a horizontal box with zero width.

enter image description here

  • Perfect working, @Przemysław Scherwent – Özgür Apr 21 '15 at 12:56
4

This is the problem with m type column when it appears at the end (i.e., as the last column). You can add an empty column at the end and things are on track again.

\documentclass[a4paper,11pt]{article}
\usepackage{multirow}
\usepackage{array}
 \begin{document}

\begin{tabular}{|>{\centering\arraybackslash}m{2cm}
                |>{\centering\arraybackslash}m{2.4cm}|@{}c@{}}\hline
X & Y \\ \hline
\multirow{2}{*}{No problem} & One Line &\\ \cline{2-1}
                            & Two Line &\\ \hline
\multirow{5}{*}{Problem} & One Line &\\ \cline{2-1}
                         & 1 & \\
                         & 2 & \\
                         & 3 & \\
                         & 4 &\\ \hline
\end{tabular}

\end{document}

enter image description here

  • Perfect working, too. @Harish Kumar – Özgür Apr 21 '15 at 13:17
2

I slightly simplify the code using the makecell package, which allows for line breaks in cells and a common formatting. There is also a Xhline command for \hlines of variable thickness:

\documentclass[a4paper,11pt]{article}
\usepackage{multirow, array, makecell}

 \begin{document}

\begin{tabular}{!{\vrule width1pt}% >{\centering\arraybackslash}m{2cm}|>{\centering\arraybackslash}m{2.4cm}!{\vrule width1pt}}
  \Xhline{1pt}
  X & Y \\
  \hline
  \multirow{2}{*}{No problem} & One Line \\
  \cline{2-1}
                              & Two Line \\ \hline
  \multirow{5}{*}{Problem} & One Line \\
  \cline{2-1}
                              & \makecell{1 \\2 \\ 3 \\ 4}\\
  \Xhline{1pt}
\end{tabular}

\end{document} 

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.