4

When I use \shortstack to create newline in a table cell, the row separation is not the same as defined by the value of \arraystretch. The following MWE shows the problem:

\documentclass[a4paper]{article}
\usepackage{siunitx}
\usepackage{booktabs}

\begin{document}

\renewcommand{\arraystretch}{3}
\begin{tabular}{cc} \toprule
Metal & \shortstack{Temperature \\ m.p.(\si{\celsius}) \\ b.p(\si{\celsius}) } \\ \midrule
Mg & \shortstack[c]{650 \\ 1090} \\
Al & \shortstack[c]{660 \\ 2467} \\
\bottomrule
\end{tabular}

\par\bigskip

\renewcommand{\arraystretch}{1}
\begin{tabular}{cc} \toprule
Metal & \shortstack{Temperature \\ m.p.(\si{\celsius}) \\ b.p(\si{\celsius}) } \\ \midrule
Mg & \shortstack[c]{650 \\ 1090} \\
Al & \shortstack[c]{660 \\ 2467} \\
\bottomrule
\end{tabular}

\end{document}

How I can make the row separation of \shortstack the same as the table defined by \arraystretch? Or how can I control the row separation of \shortstack?

3
  • Wouldn't it just be easier to use a nested tabular? Or, redefine \shortstack to suit your needs?
    – Werner
    Jan 19, 2014 at 18:12
  • \shortstack is very primitive and doesn't take into account proper line spacing. I wish it had been defined very differently. Better not using it.
    – egreg
    Jan 19, 2014 at 21:06
  • Yes, nested tabular environment is definitely worked, but it seems the code is a little bit unclear, so wondering if it is ok to use \shortstack instead.
    – bingung
    Jan 20, 2014 at 9:55

3 Answers 3

1

The stackengine package has a \Shortstack macro which has a settable gap setting. Since the \shortstack gap is 3pt, I here set it to 9pt for \Shortstack.

\documentclass[a4paper]{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[usestackEOL]{stackengine}
\begin{document}
\setstackgap{S}{9pt}
\renewcommand{\arraystretch}{3}
\begin{tabular}{cc} \toprule
Metal & \Shortstack{Temperature \\ m.p.(\si{\celsius}) \\ b.p(\si{\celsius}) } \\ \midrule
Mg & \Shortstack[c]{650 \\ 1090} \\
Al & \Shortstack[c]{660 \\ 2467} \\
\bottomrule
\end{tabular}

\par\bigskip

\renewcommand{\arraystretch}{1}
\begin{tabular}{cc} \toprule
Metal & \shortstack{Temperature \\ m.p.(\si{\celsius}) \\ b.p(\si{\celsius}) } \\ \midrule
Mg & \shortstack[c]{650 \\ 1090} \\
Al & \shortstack[c]{660 \\ 2467} \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

If that is not the spacing you want, there is also the \addstackgap macro that adds the stacking gap above and below the argument (in this case the \Shortstack).

\documentclass[a4paper]{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[usestackEOL]{stackengine}
\begin{document}
\setstackgap{S}{9pt}
\renewcommand{\arraystretch}{3}
\begin{tabular}{cc} \toprule
Metal & \addstackgap{\Shortstack{Temperature \\ m.p.(\si{\celsius}) \\ b.p(\si{\celsius}) }} \\ \midrule
Mg & \addstackgap{\Shortstack[c]{650 \\ 1090}} \\
Al & \Shortstack[c]{660 \\ 2467} \\
\bottomrule
\end{tabular}

\par\bigskip

\renewcommand{\arraystretch}{1}
\begin{tabular}{cc} \toprule
Metal & \shortstack{Temperature \\ m.p.(\si{\celsius}) \\ b.p(\si{\celsius}) } \\ \midrule
Mg & \shortstack[c]{650 \\ 1090} \\
Al & \shortstack[c]{660 \\ 2467} \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

1
  • This is what I want, to control the row spacing from \shortstack in a fair simple way. Thanks for bringing out the package stackengine which I do not know it before.
    – bingung
    Jan 21, 2014 at 16:14
1

Don't redefine kernel/core macros, since you don't know where they're used elsewhere (in the kernel, or other packages).

It seems fairly trivial to redefine \shortstack into something more relevant to suit your needs, and is not used in any of the base classes (article, book or report):

enter image description here

\documentclass{article}
\usepackage{siunitx,booktabs}% http://ctan.org/pkg/{siunitx,booktabs}

\renewcommand{\shortstack}[2][c]{%
  \begin{tabular}[b]{@{}#1@{}}% [c]enter; [t]op; [b]ottom
    #2
  \end{tabular}%
}
\begin{document}

\renewcommand{\arraystretch}{1.5}
\begin{tabular}{cc}
  \toprule
  Metal & \shortstack{Temperature \\ m.p.(\si{\celsius}) \\ b.p(\si{\celsius}) } \\
  \midrule
  Mg & \shortstack[c]{650 \\ 1090} \\
  Al & \shortstack[c]{660 \\ 2467} \\
  \bottomrule
\end{tabular}

\par\bigskip

\renewcommand{\arraystretch}{1}
\begin{tabular}{cc}
  \toprule
  Metal & \shortstack{Temperature \\ m.p.(\si{\celsius}) \\ b.p(\si{\celsius}) } \\
  \midrule
  Mg & \shortstack[c]{650 \\ 1090} \\
  Al & \shortstack[c]{660 \\ 2467} \\
  \bottomrule
\end{tabular}

\end{document}
1
  • Just wondering what the difference is by defining a new macro which contains the nested tabular environment?
    – bingung
    Jan 21, 2014 at 16:17
1

Here is a different approach from my other answer. If you want to change the actual definition of \shortstack, here is how it can be done. I have two new macros, \shortstackgap{} and \restoreshortstack. The first one takes a number argument, which is used (as a point value) to redefine the \shortstack gap. The second macro redefines \shortstack to its default definition, which has a 3pt stacking gap.

\documentclass[a4paper]{article}
\usepackage{siunitx}
\usepackage{booktabs}

\makeatletter
\newcommand\shortstackgap[1]{%
\gdef\@shortstack[##1]{%
  \leavevmode
  \vbox\bgroup
    \baselineskip-\p@\lineskip #1\p@
    \let\mb@l\hss\let\mb@r\hss
    \expandafter\let\csname mb@##1\endcsname\relax
    \let\\\@stackcr
    \@ishortstack}
\gdef\@ishortstack##1{\ialign{\mb@l {####}\unskip\mb@r\cr ##1\crcr}\egroup}
}
\newcommand\restoreshortstack{%
\gdef\@shortstack[##1]{%
  \leavevmode
  \vbox\bgroup
    \baselineskip-\p@\lineskip 3\p@
    \let\mb@l\hss\let\mb@r\hss
    \expandafter\let\csname mb@##1\endcsname\relax
    \let\\\@stackcr
    \@ishortstack}
\gdef\@ishortstack##1{\ialign{\mb@l {####}\unskip\mb@r\cr ##1\crcr}\egroup}
}
\makeatother

\begin{document}

\renewcommand{\arraystretch}{3}
\shortstackgap{9}
\begin{tabular}{cc} \toprule
Metal & \shortstack{Temperature \\ m.p.(\si{\celsius}) \\ b.p(\si{\celsius}) } \\ \midrule
Mg & \shortstack[c]{650 \\ 1090} \\
Al & \shortstack[c]{660 \\ 2467} \\
\bottomrule
\end{tabular}

\par\bigskip

\restoreshortstack
\renewcommand{\arraystretch}{1}
\begin{tabular}{cc} \toprule
Metal & \shortstack{Temperature \\ m.p.(\si{\celsius}) \\ b.p(\si{\celsius}) } \\ \midrule
Mg & \shortstack[c]{650 \\ 1090} \\
Al & \shortstack[c]{660 \\ 2467} \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

You must log in to answer this question.

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