1

Consider the following example:

\documentclass[
  a4paper,
  12pt
]{article}

\usepackage[
  hmargin = 2.4cm,
  vmargin = 3cm
]{geometry}
\usepackage[
  locale = DE,
  detect-weight = true,
  detect-inline-weight = math,
  round-mode = places,
  round-precision = 1
]{siunitx}

\def\mlrA{1ex}
\def\mlrB{\hspace{0.5em}}
\newcommand*\saenk[1]{\raisebox{-0.5ex}{#1}}

\ExplSyntaxOn
  \cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

\begin{document}

% constants
\def\masse{80}
\def\Jorden{9.8}
\def\Maanen{1.6}
\def\Juliper{26.5}
\def\Pluto{0.3}
\def\Mars{3.8}
\def\Venus{8.8}
\def\Merkur{5.4}
\def\Neptun{11.0}
\def\Uranus{10.4}
\def\Saturn{11.3}

\begin{center}
\large
 \begin{tabular}{
        |>{\raggedright\arraybackslash}m{2.4cm}
   *{3}{|>{\centering\arraybackslash}m{4cm}}|
 }
  \hline
    \multicolumn{1}{|c|}{\textbf{Planet}}
  &                      \textbf{Tyngdekraften p{\aa} \SI{1}{\kg}}
  &                      \textbf{Tyngdekraften p{\aa} personen}
  &        \vspace{0.5ex}\textbf{Svarer til Jordens tr{\ae}k i}                                                              \\[\mlrA]
  \hline Jorden  & \mlrB\SI{\Jorden}{\N}  &                                &                                                 \\[\mlrA]
  \hline Månen   & \mlrB\SI{\Maanen}{\N}  &                                &                                                 \\[\mlrA]
  \hline Jupiter &      \SI{\Juliper}{\N} &                                &                                                 \\[\mlrA]
  \hline Pluto   & \mlrB\SI{\Pluto}{\N}   &                                &                                                 \\[\mlrA]
  \hline Mars    & \mlrB\SI{\Mars}{\N}    &                                &                                                 \\[\mlrA]
  \hline Venus   & \mlrB\SI{\Venus}{\N}   &                                &                                                 \\[\mlrA]
  \hline Merkur  & \mlrB\SI{\Merkur}{\N}  & \SI{\calc{\masse*\Merkur}}{\N} & \saenk{\SI{\calc{\masse*\Merkur/\Jorden}}{\kg}} \\[\mlrA]
  \hline Neptun  &      \SI{\Neptun}{\N}  &                                &                                                 \\[\mlrA]
  \hline Uranus  &      \SI{\Uranus}{\N}  &                                &                                                 \\[\mlrA]
  \hline Saturn  &      \SI{\Saturn}{\N}  &                                &                                                 \\[\mlrA]
  \hline
 \end{tabular}
\end{center}

\end{document}

output

How do I automatically align the units (N) and the corresponding numbers in the second column of the table?

I've had a look at the code for table 49 in the siunitx manual but couldn't make it work. Therefore I've removed the S-column again in the table code, in order to make it look correct.

  • I am little bit lost: What do you mean with automatically? Like the S column specification allows you to do? But with units? Why the vertical-alignment tag? Also: Why not put the unit in the table header? (One might argument that this is a better way …) Why not even N/kg in the second column? – Qrrbrbirlbel Jun 19 '16 at 21:50
  • @Qrrbrbirlbel Automatically: yes, like the S-column specification allows for. Unit in table header: I'm teaching people how are not used to this, but normally I would do it. – Svend Tveskæg Jun 19 '16 at 21:57
3

You could use table-space-text-pos and the < column specification.

For some reason, the last column acts up with S and < which seems to be related with the problems in ctable and siunitx: Last column does not center correctly (possible incompatibility?). I have found that adding another column at the end helps, see also Peter Grill's comment.

If you change \mlrA to .5ex you can replace the first column with

>{\raggedright\arraybackslash\rule{0pt}{\dimexpr1em+.5ex}}b{2.4cm}

you don't need macros like \saenk.

You can even get rid of \mlrA itself if you use something like

>{\raggedright\arraybackslash\rule[-1.5ex]{0pt}{\dimexpr1em+2ex}}b{2.4cm}

(These measurements aren't the same that produces your vertical spacing but you get the idea.)

However, this does not help in the case of actual multi-line cells as those in the header because they are treated by TeX as one character on a single line and they add more height than their single-line companions of the same row. You will need to include another special strut at least for the tallest header.

The result of that is in Code B below. I also changed all multi-line column specifications to b as they bottom-align vertically.

Code

\documentclass[
  a4paper,
  12pt
]{article}

\usepackage[
  hmargin = 2.4cm,
  vmargin = 3cm
]{geometry}
\usepackage[
  locale = DE,
  detect-weight = true,
  detect-inline-weight = math,
  round-mode = places,
  round-precision = 1
]{siunitx}
\usepackage{xfp}

\def\mlrA{1ex}

\ExplSyntaxOn
  \newcommand*\siunitxNumberUnitProduct{\l__siunitx_number_unit_product_tl}
\ExplSyntaxOff

\begin{document}

\def\masse{80}
\def\Jorden{9.8}
\def\Maanen{1.6}
\def\Juliper{26.5}
\def\Pluto{0.3}
\def\Mars{3.8}
\def\Venus{8.8}
\def\Merkur{5.4}
\def\Neptun{11.0}
\def\Uranus{10.4}
\def\Saturn{11.3}

\begin{center}
\large
\begin{tabular}{
 |>{\raggedright\arraybackslash}m{2.4cm}
 |S[table-format = 2.1,
    table-space-text-post = \siunitxNumberUnitProduct\si{\N}]<{\siunitxNumberUnitProduct\si{\N}}
 |S[table-format = 3.0,
    table-space-text-post = \siunitxNumberUnitProduct\si{\N}]<{\siunitxNumberUnitProduct\si{\N}}
 |S[table-format = 3.1,
    table-space-text-post = \siunitxNumberUnitProduct\si{\kg}]<{\siunitxNumberUnitProduct\si{\kg}}
 |@{}c@{} % something's fishy with S in the last column
}
\hline\multicolumn{1}{|c|}{\textbf{Planet}}
    & \multicolumn{1}{>{\centering}m{4cm}|}{\textbf{Tyngdekraften p{\aa} \SI{1}{\kg}}}
    & \multicolumn{1}{>{\centering}m{4cm}|}{\textbf{Tyngdekraften p{\aa} personen}}
    & \multicolumn{1}{>{\centering}m{4cm}|}{\textbf{Svarer til Jordens tr{\ae}ki}}  \\[\mlrA]
  \hline Jorden  & \Jorden  & \fpeval{\masse*\Jorden} & \fpeval{\masse*\Jorden/\Jorden} & \\[\mlrA]
  \hline Månen   & \Maanen  & \fpeval{\masse*\Maanen} & \fpeval{\masse*\Maanen/\Jorden} & \\[\mlrA]
  \hline Jupiter & \Juliper & \fpeval{\masse*\Juliper}& \fpeval{\masse*\Juliper/\Jorden}& \\[\mlrA]
  \hline Pluto   & \Pluto   & \fpeval{\masse*\Pluto}  & \fpeval{\masse*\Pluto/\Jorden}  & \\[\mlrA]
  \hline Mars    & \Mars    & \fpeval{\masse*\Mars}   & \fpeval{\masse*\Mars/\Jorden}   & \\[\mlrA]
  \hline Venus   & \Venus   & \fpeval{\masse*\Venus}  & \fpeval{\masse*\Venus/\Jorden}  & \\[\mlrA]
  \hline Merkur  & \Merkur  & \fpeval{\masse*\Merkur} & \fpeval{\masse*\Merkur/\Jorden} & \\[\mlrA]
  \hline Neptun  & \Neptun  & \fpeval{\masse*\Neptun} & \fpeval{\masse*\Neptun/\Jorden} & \\[\mlrA]
  \hline Uranus  & \Uranus  & \fpeval{\masse*\Uranus} & \fpeval{\masse*\Uranus/\Jorden} & \\[\mlrA]
  \hline Saturn  & \Saturn  & \fpeval{\masse*\Saturn} & \fpeval{\masse*\Saturn/\Jorden} & \\[\mlrA]
  \hline
\end{tabular}
\end{center}
\end{document}

Code B

\documentclass[
  a4paper,
  12pt
]{article}

\usepackage[
  hmargin = 2.4cm,
  vmargin = 3cm
]{geometry}
\usepackage[
  locale = DE,
  detect-weight = true,
  detect-inline-weight = math,
  round-mode = places,
  round-precision = 1
]{siunitx}

\ExplSyntaxOn
  \newcommand*\siunitxNumberUnitProduct{\l__siunitx_number_unit_product_tl}
\ExplSyntaxOff

\begin{document}

\def\masse{80}
\def\Jorden{9.8}
\def\Maanen{1.6}
\def\Juliper{26.5}
\def\Pluto{0.3}
\def\Mars{3.8}
\def\Venus{8.8}
\def\Merkur{5.4}
\def\Neptun{11.0}
\def\Uranus{10.4}
\def\Saturn{11.3}

\newcommand*\myStrut{\rule[-1.5ex]{0pt}{\dimexpr1em+2ex}}
\newcommand*\myStrutH{\rule{0pt}{\dimexpr1em+.5ex}}

\begin{center}
\large
\begin{tabular}{
 |>{\raggedright\arraybackslash\myStrut}b{2.4cm}
 |S[table-format = 2.1,
    table-space-text-post = \siunitxNumberUnitProduct\si{\N}]<{\siunitxNumberUnitProduct\si{\N}}
 |S[table-format = 3.0,
    table-space-text-post=\siunitxNumberUnitProduct\si{\N}]<{\siunitxNumberUnitProduct\si{\N}}
 |S[table-format = 3.1,
    table-space-text-post=\siunitxNumberUnitProduct\si{\kg}]<{\siunitxNumberUnitProduct\si{\kg}}
 |@{}c@{} % something's fishy with S in the last column
}
  \hline\multicolumn{1}{|>\myStrut c|}{\textbf{Planet}}
      & \multicolumn{1}{>{\centering\myStrutH}b{4cm}|}{\textbf{Tyngdekraften p{\aa} \SI{1}{\kg}}}
      & \multicolumn{1}{>{\centering}b{4cm}|}{\textbf{Tyngdekraften p{\aa} personen}}
      & \multicolumn{1}{>{\centering}b{4cm}|}{\textbf{Svarer til Jordens tr{\ae}ki}}  \\
  \hline Jorden  & \Jorden  & \calc{\masse*\Jorden} & \calc{\masse*\Jorden/\Jorden} & \\
  \hline Månen   & \Maanen  & \calc{\masse*\Maanen} & \calc{\masse*\Maanen/\Jorden} & \\
  \hline Jupiter & \Juliper & \calc{\masse*\Juliper}& \calc{\masse*\Juliper/\Jorden}& \\
  \hline Pluto   & \Pluto   & \calc{\masse*\Pluto}  & \calc{\masse*\Pluto/\Jorden}  & \\
  \hline Mars    & \Mars    & \calc{\masse*\Mars}   & \calc{\masse*\Mars/\Jorden}   & \\
  \hline Venus   & \Venus   & \calc{\masse*\Venus}  & \calc{\masse*\Venus/\Jorden}  & \\
  \hline Merkur  & \Merkur  & \calc{\masse*\Merkur} & \calc{\masse*\Merkur/\Jorden} & \\
  \hline Neptun  & \Neptun  & \calc{\masse*\Neptun} & \calc{\masse*\Neptun/\Jorden} & \\
  \hline Uranus  & \Uranus  & \calc{\masse*\Uranus} & \calc{\masse*\Uranus/\Jorden} & \\
  \hline Saturn  & \Saturn  & \calc{\masse*\Saturn} & \calc{\masse*\Saturn/\Jorden} & \\
  \hline
\end{tabular}
\end{center}

\end{document}
  • Really great. Now, the only problem is that the physical quantities are at the top of each cell and not vertically centered inside them. – Svend Tveskæg Jun 19 '16 at 22:41
  • Ah, yeah, ... I deleted the use of \saenk and didn't understand why you used it. Why the \mlrA anyway? Easiest would be to half the \mlrA and add a strut with proper dimensions with > to the first column. – Qrrbrbirlbel Jun 19 '16 at 23:01
  • Can I make you add the things you are talking about in the comment? I've tried myself and falied. :-( – Svend Tveskæg Jun 22 '16 at 21:11
  • 1
    @SvendTveskæg I edited my answer. I wouldn't use that many horizontal and no vertical lines anyway (Hint: booktabs), but you talked about teaching people what not to do, so … – Qrrbrbirlbel Jun 22 '16 at 21:48

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.