2

The output of the following compiled code is not as expected. I want all text to be centered in the cell, so I need the text in the first and last columns to look like the other ones. How do I implement this?

\documentclass[a4paper,12pt]{article}
\usepackage{diagbox}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}


\begin{document} 

\begin{table}[ht]
\caption{Conditions for Neutral Stability} 
\centering
   \begin{tabular}{|l||*{3}{M{4cm}|}}\hline
\backslashbox{Density}{Velocity}
&\makebox[3em]{$U_{1} < U_{2}$}&\makebox[3em]{$U_{1} = U_{2}$}&\makebox[3em]{$U_{1} > U_{2}$}
\\ [.5cm]\hline\hline
$\rho_{1}<\rho_{2}$ & Neutrally stable if \newline $|U_{1}-U_{2}|<\sqrt{\frac{g(\rho_{1}^{2}-\rho_{2}^{2})}{k\rho_{1}\rho_{2}}}$ &Unstable& Neutrally stable if $|U_{1}-U_{2}|<\sqrt{\frac{g(\rho_{1}^{2}-\rho_{2}^{2})}{k\rho_{1}\rho_{2}}}$\\ [1cm]\hline
$\rho_{1}=\rho_{2}$ &Unstable&Neutrally Stable&Unstable\\ [1cm]\hline
$\rho_{1}>\rho_{2}$ &Unstable&Unstable&Unstable\\ [1cm]\hline
   \end{tabular}
\end{table}
 \end{document}

Output:

Compilation output

4
  • 1
    Welcome to TeX.sx! Please describe your question as precise as possible!
    – strpeter
    Dec 8, 2016 at 17:10
  • Please tell us how the M type is defined.
    – Mico
    Dec 8, 2016 at 17:20
  • Please rewrite your code into a minimal working example (MWE).
    – strpeter
    Dec 8, 2016 at 17:21
  • I edited the code to include the definition of M.
    – MOA
    Dec 8, 2016 at 17:37

1 Answer 1

2

Edit: Since use of diagbox is mandatory, I accordingly change MWE in first version of answer. Let me noted, that now the table is wider and probably will not fit in text width. Some space (8mm) can be earned write equation in text style (drop command \displaystyle in complete MWE nelow} and change column width to:

   \begin{tabular}{|l||>{\centering\arraybackslash}m{4cm}|
                        >{\centering\arraybackslash}m{3cm}|
                        >{\centering\arraybackslash}m{4cm}|}

enter image description here

I drop all options [3em] at \makebox and also replace \\[1cm] with \\. Instead lather I add \renewcommand\arraystretch{1.5}, equations in second and third row write in \displaystyle, where I also ad some vertical space above and below of equation.

enter image description here

\documentclass{article}
\usepackage{diagbox}
\usepackage{array}
\usepackage[font=small,labelfont=bf]{caption}

\begin{document}
    \begin{table}[ht]
\caption{Conditions for Neutral Stability}
\centering
\renewcommand\arraystretch{1.5}
\setlength\tabcolsep{3pt}
\begin{tabular}{|l||>{\centering\arraybackslash}m{4.4cm}|
                    >{\centering\arraybackslash}m{3.0cm}|
                    >{\centering\arraybackslash}m{4.4cm}|}
\hline
\backslashbox{Density}{Velocity}
    &   \makebox{$U_{1} < U_{2}$} 
        &   \makebox{$U_{1} = U_{2}$}
            &   \makebox{$U_{1} > U_{2}$}           \\
    \hline\hline

\rho_{1}<\rho_{2}   
    &   Neutrally stable if\smallskip\newline
        $\displaystyle
        |U_{1}-U_{2}|<
        \sqrt{\frac{g(\rho_{1}^{2}-\rho_{2}^{2})}{k\rho_{1}\rho_{2}}}
        $\smallskip
        &   Unstable    &   Neutrally stable if\smallskip\newline 
                            $\displaystyle
                            |U_{1}-U_{2}|<
                            \sqrt{\frac{g(\rho_{1}^{2}-
                                        \rho_{2}^{2})}{k\rho_{1}\rho_{2}}}
                            $\smallskip                              \\
    \hline
\rho_{1}=\rho_{2}
    &   Unstable        &   Neutrally Stable    &   Unstable                 \\
    \hline
\rho_{1}>\rho_{2}
    &   Unstable        &   Unstable            &   Unstable                 \\
    \hline
   \end{tabular}
    \end{table}
\end{document}

Addendum: Better look and small width of table can be achieved with use (i) hhline package, (ii) instead of \backslashbox{Density}{Velocity} use \diagbox[innerwidth=2.4cm]{Density}{Velocity}}, and shrinking third column to 1.8 cm. Also with insert \diagbox into curly braces all \makebox{...} become surplus:

\documentclass[a4paper,12pt]{article}
\usepackage{diagbox}
\usepackage{array}
\usepackage{hhline}
\usepackage[font=small,labelfont=bf]{caption}

\begin{document}
    \begin{table}[ht]
\caption{Conditions for Neutral Stability}
\centering
\renewcommand\arraystretch{1.5}
\setlength\tabcolsep{4pt}
    \begin{tabular}{|c||>{\centering\arraybackslash}m{4.4cm}|
                        >{\centering\arraybackslash}m{1.8cm}|
                        >{\centering\arraybackslash}m{4.4cm}|}
    \hhline{|-||-|-|-|}
{\diagbox[innerwidth=2.4cm]{Density}{Velocity}}
    &   $U_{1} < U_{2}$ 
        &   $U_{1} = U_{2}$
            &   $U_{1} > U_{2}$            \\
    \hhline{:=::=:=:=:}
$\rho_{1}<\rho_{2}$   
    &   Neutrally stable if\smallskip\newline
        $\displaystyle
        |U_{1}-U_{2}|<
        \sqrt{\frac{g(\rho_{1}^{2}-\rho_{2}^{2})}{k\rho_{1}\rho_{2}}}
        $\smallskip
        &   Unstable    &   Neutrally stable if\smallskip\newline 
                            $\displaystyle
                            |U_{1}-U_{2}|<
                            \sqrt{\frac{g(\rho_{1}^{2}-
                                        \rho_{2}^{2})}{k\rho_{1}\rho_{2}}}
                            $\smallskip                              \\
    \hhline{|-||-|-|-|}
$\rho_{1}=\rho_{2}$
    &   Unstable        &   Neutrally Stable    &   Unstable                 \\
    \hhline{|-||-|-|-|}
$\rho_{1}>\rho_{2}$
    &   Unstable        &   Unstable            &   Unstable                 \\
    \hhline{|-||-|-|-|}
   \end{tabular}
    \end{table}
\end{document}

enter image description here

4
  • Thank you Zarko! But I actually need it in the format above, with the split cell and the double horizontals and everything. The only thing I need changed is the centering of the text within the cells. If you have any help on that, that'd be great!
    – MOA
    Dec 8, 2016 at 18:30
  • meanwhile i resolve issue with diagbox and its macro \backslashbox, see if now the result is as you like to have.
    – Zarko
    Dec 8, 2016 at 19:02
  • @OAM, to my answer I add new possible solution.
    – Zarko
    Dec 8, 2016 at 21:12
  • if you liked solution and it help you, you can accept answer (click on check mark at top left side of answer :) )
    – Zarko
    Dec 12, 2016 at 13:59

You must log in to answer this question.

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