2

I'm trying to make a table in which the last column has mixed input of text and numbers in the same cell, and I want to align them at the decimal point, irrespective of the lenght of the preceeding text (though is usually a symbol like <, >, etc.). The problem is that the numbers overlap the text.

MWE:

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{table}
\begin{tabular}{@{}l S[table-format=1.4] @{}}
& {p} \\
\midrule
Model & {\textless{}} 0,0001 \\
\hspace{0.5cm}A & {\textless{}} 0,0001 \\
\hspace{0.5cm}B & 0,2980 \\
\hspace{0.5cm}C & 0,0007 \\
\end{tabular}
\end{table} 
\end{document}

results in:

If I use:

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{table}
\begin{tabular}{@{}l S[table-format=1.4] @{}}
& {p} \\
\midrule
Model & {text} {0.0001} \\
\hspace{0.5cm}A & {\textless{}} {0.0001} \\
\hspace{0.5cm}B & 0.2980 \\
\hspace{0.5cm}C & 0.0007 \\
\end{tabular}
\end{table} 
\end{document}

I get:

with no space between "text" and "0.0001", and the numbers not aligned at the decimal point.

If I try aligning the numbers to the right and centering the title:

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{table}
\begin{tabular}{@{}l r@{}}
& \multicolumn{1}{c}{p} \\
\midrule
Model & {\textless{}} {0.0001} \\
\hspace{0.5cm}A & {\textless{}} {0.0001} \\
\hspace{0.5cm}B & 0.2980 \\
\hspace{0.5cm}C & 0.0007 \\
\end{tabular}
\end{table} 
\end{document}

the result is:

which is still wrong, as the title is not centered in the column (it's more to the left side than it should be).

I also tried declaring mode=text in the preamble for siunitx, but still couldn't get the result I want.

Is there any way to get a single column with the header centered with the text below, and cells with some text followed by a space and then numbers aligned at the decimal point? Something closer to this mock-up:

I'm trying to avoid manual adjustments like \hspace{} or the like. Thanks.

EDIT OK, thanks to @Mico's code I got closer to what I want. I've added a few digits at both sides of the decimal marker to check the alignment of the text before and after the numerals. MWE:

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{
  output-decimal-marker = {,},
  table-number-alignment = center,
  table-align-comparator = false,
  table-align-text-pre = false,
  table-align-text-post = false,
  table-space-text-pre = \, ,
}
\begin{document}

\begin{tabular}{c S[table-format=2.8, table-comparator=true]}
  & {Value} \\
  \midrule
  Model & <9,0001 \\
  A     & >90,001 \\
  B     &  0,29801111 \\
  C     & \sim 0,0007 \\
  D     & \approx 1,030356{\textsuperscript{a}}
\end{tabular}

\end{document}

gives:

Now I only need to find a way to control the spacing between the pre- and post- text. I'd like a small space (like \,) between the comparator symbol and the numeral. The idea is to define this globally, to avoid having to define something like \newcommand{\ssim}{\sim\,} for every symbol (<, >, ~, ±, etc.). Thanks.

3 Answers 3

1

You could add the options table-align-text-pre = false and table-space-text-pre = < to the specification of the S column.

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage[output-decimal-marker={,}]{siunitx}
\begin{document}
\begin{tabular}{@{} c S[table-format=1.4,
                        table-align-text-pre = false,
                        table-space-text-pre = <] @{}}
  & $p$ \\
  \midrule
  Model & <0,0001 \\
  A     & <0,0001 \\
  B     &  0,2980 \\
  C     &  0,0007
\end{tabular}
\end{document}
3
  • Hello, thanks for your input. I've tried it, but I get: "Package siunitx Error: No space reserved for a comparator on line 10".
    – Pertrecho
    Sep 8, 2021 at 14:35
  • @Pertrecho - Sadly (but maybe not surprisingly), my mind-reading skills are absolutely and irredeemably worthless. If you'd like me to say something useful regarding the error (or warning?) message you've encountered, you need to tell me (much) more about what you're actually doing in your document. For sure, the little test document I posted does not generate the message you report.
    – Mico
    Sep 8, 2021 at 15:38
  • The error I mentioned occurred with TeXLive 2020. But I've now tried with 2021 and your MWE worked just fine. I'm still looking for a way to add a space between the "<" symbol and the number. Ideally, the column would be aligned at the decimal point, irrespective of any text before or after the number. Also I'd like to define all of this in the preamble, to avoid having to manually adjust every column each time I insert a table. Thank you.
    – Pertrecho
    Sep 8, 2021 at 21:45
0

Have you tried the option table-alignment-mode=marker.

Like so:

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
    \begin{tabular}{@{}c S[table-format=1.4,table-alignment-mode = marker] @{}}
        & {p} \\
        \midrule
        Model & {text} 0.0001 \\
        A & < 0.0001 \\
        B & 0.2980 \\
        C & 0.0007 \\
    \end{tabular}
\end{document}

Looks like:

enter image description here

Note that actual text requires {}, while math operators such as > or < do not, because siunitx knows how to interpret them.

EDIT: If you want the decimal point to be a comma like in your initial example feel free to add an additional output-decimal-marker={,}

5
  • Hi, thanks for your help. I don't know if there is a siunitx version issue, but I get: "Package siunitx Error: Unknown option 'table-alignment-mode'."
    – Pertrecho
    Sep 8, 2021 at 14:36
  • Is your system up to date? If you have issues with both solutions, it sounds like you should update the package at least.
    – Markus G.
    Sep 8, 2021 at 14:46
  • Hi, I'm currently using TeXLive 2020, but after trying version 2021 I had no errors with your MWE. As I mentioned to @Mico, my idea is to have the numbers aligned at the decimal point, no matter the text before of after them. In your example the word "text" is aligned to the left of the column, instead of being right next to the number, separated by a space, as I'd like. Also, there is no space between "<" and the number. Thanks.
    – Pertrecho
    Sep 8, 2021 at 21:51
  • As for the space between < and the number: There shouldn't be one normally, so siunitx doesn't put one there. Concerning the rest: If the text you need is longer, then I suggest simply using a normal column-type and placing the \SI{}{} manually. If you made your issue more specific there may be ways to help you, e.g. for placing footnotemarkers before or after the numbers, but other than that I can't help you.
    – Markus G.
    Sep 9, 2021 at 14:42
  • Thanks for your suggestion for longer text. Regarding the spacing, as I understand, there should be a space before and after each relational symbol, that's why I want to add the space there (apart from aesthetics).
    – Pertrecho
    Sep 9, 2021 at 15:04
0

If anyone is interested, the answer is to add the comparator to the preamble:

\usepackage{siunitx}
\sisetup{
  table-align-comparator=false,
  table-align-text-pre=false,
  input-comparators={\textless}
}

and then add a space after the comparator in the table:

\textless{ } 0,0001

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.