5

In the following MWE, I use the siunitx package to align numerical entries in a table. How can I place a \sim symbol before the "6" without upsetting the alignment? Table A will compile; Table B will not.

\documentclass[oneside,11pt]{book}

\usepackage[semibold,tt=false]{libertine} \usepackage{libertinust1math} \usepackage[ expansion = false , tracking = smallcaps , letterspace = 40 , final ]{microtype} \usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption} \usepackage{mathtools} \usepackage{booktabs} \usepackage{siunitx}

\begin{document}

% A \begin{table}[!h] \centering \begin{tabular}{ l S[table-format=1.1,table-number-alignment=left,table-align-text-post=false] } \toprule & {X}\ \midrule A & 6\ B & 0.1\ \bottomrule \end{tabular} \caption{A} \end{table}

% B \begin{table}[!h] \centering \begin{tabular}{ l S[table-format=1.1,table-number-alignment=left,table-align-text-post=false] } \toprule & {X}\ \midrule A & \sim 6\ B & 0.1\ \bottomrule \end{tabular} \caption{B} \end{table}

\end{document}

mwe

Andrew
  • 1,249

3 Answers3

9

To add symbols, you have the key input-symbols, but this one is a comparator, so you just have to set the table-comparator switch to true:

\documentclass[oneside,11pt]{book}

\usepackage[semibold,tt=false]{libertine} \usepackage{libertinust1math} \usepackage[ expansion = false , tracking = smallcaps , letterspace = 40 , final ]{microtype} \usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption} \usepackage{mathtools} \usepackage{booktabs} \usepackage{siunitx}

\begin{document}

\begin{table}[!h] \centering \begin{tabular}{ l S[table-format=1.1,table-number-alignment=left,table-align-text-post=false, table-comparator] } \toprule & {X}\ \midrule A & \sim 6\ B & 0.1\ \bottomrule \end{tabular} \caption{B} \end{table}

\end{document}

enter image description here

Bernard
  • 271,350
  • I've been reading your horoscope for the new year and it looks like it starts for you with a great event: LOTS of points and lots of happiness! AHAHAHAHAHH. – Sebastiano Jan 01 '21 at 12:29
  • 1
    @Sebastiano: How that? You do not draw horoscopes yourself? I'm terribly disappointed… ;o) – Bernard Jan 01 '21 at 12:47
5

Try the following:

\documentclass[12pt]{article}

\usepackage[semibold,tt=false]{libertine} \usepackage{libertinust1math} \usepackage[expansion = false, tracking = smallcaps, letterspace = 40, final] {microtype}

\usepackage{booktabs} \usepackage{siunitx}

\begin{document} \begin{center} \begin{tabular}{c S[input-comparators, % <---- table-space-text-pre=$\sim$, % <---- table-format=1.1, ]
} \toprule type & {X} \ \midrule A & $\sim$ 6 \ B & 0.1 \ \bottomrule \end{tabular} \caption{A} \end{table}

\medskip

\medskip however, in table is better to use

\verb+table-comparator+ instead of \verb+input-comparators+ as suggested in @Bernard's answer:

\medskip \begin{tabular}{c S[table-format=1.1, table-space-text-pre=$\sim$, table-comparator] % <---- } \toprule type & {X} \ \midrule A & \sim 6 \ % <---- B & 0.1 \ \bottomrule \end{tabular} \end{center} \end{document}

In both examples result is the same:

enter image description here

Zarko
  • 296,517
3

Put the \SI{\sim 6}{} instead of \sim and it is works in pdfLaTeX. Happy new year :-). See the explanation here: How to correctly use siunitx to align number in the center of a table for number with preamble

\documentclass[oneside,11pt]{book}
\usepackage[semibold,tt=false]{libertine}
\usepackage{libertinust1math}
\usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
  final
]{microtype}
\usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
\usepackage{mathtools}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}

% A \begin{table}[!h] \centering \begin{tabular}{ l S[table-format=1.1,table-number-alignment=left,table-align-text-post=false] } \toprule & {X}\ \midrule A & 6\ B & 0.1\ \bottomrule \end{tabular} \caption{A} \end{table}

% B \begin{table}[!h] \centering \begin{tabular}{ l S[table-format=1.1,table-number-alignment=left,table-align-text-post=false] } \toprule & {X}\ \midrule A & \SI{\sim 6}{} \ B & 0.1\ \bottomrule \end{tabular} \caption{B} \end{table}

\end{document}

enter image description here

Edit by comment:

\documentclass[oneside,11pt]{book}
\usepackage[semibold,tt=false]{libertine}
\usepackage{libertinust1math}
\usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
  final
]{microtype}
\usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
\usepackage{mathtools}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}

% A \begin{table}[!h] \centering \begin{tabular}{ l S[table-format=1.1,table-number-alignment=left,table-align-text-post=false] } \toprule & {X}\ \midrule A & 6\ B & 0.1\ \bottomrule \end{tabular} \caption{A} \end{table}

% B \begin{table}[!h] \centering \begin{tabular}{ l S[table-format=1.1,table-number-alignment=left,table-align-text-post=false] } \toprule & {X}\ \midrule A & !!!\SI{\sim 6}{} \ B & 0.1\ \bottomrule \end{tabular} \caption{B} \end{table}

\end{document}

enter image description here

Sebastiano
  • 54,118
  • 2
    Thanks. Unfortunately, the "6" no longer lines up with the "0". Is there a way to accomplish that? – Andrew Dec 31 '20 at 22:04
  • 1
    @Andrew I understand the English language very bit. Use a trick :-). See \!\!\!\SI{\sim 6}{} if it is good for you without to change anything. – Sebastiano Dec 31 '20 at 22:08