6

I asked a previous question about underlining data in a table when using the siunitx S column definition (see Use of underline and siunitx in a table). The advice given was perfect until I added \sisetup{table-format=2.1} to reduce the amount of space being used up the numbers. I have managed to get my table spaced out perfectly now, so I don't really want to remove the \sisetup, but this line means that any underlined values in the table are now not aligned by decimal point.

Is there any way to combine underlining with this \sisetup?

MWE:

\documentclass{article}
\usepackage{etoolbox,siunitx,booktabs,threeparttable,multirow,array,graphicx}
\usepackage[normalem]{ulem}
\robustify\bfseries
\robustify\uline
\sisetup{detect-weight        = true,
         detect-inline-weight = math,
         tight-spacing        = true,
         table-format         = 2.1,
         }

\begin{document}
    \begin{table}
    \caption{Insert caption here}
    \centering
    \begin{threeparttable}[b]
        \begin{tabular}{SS}
        \toprule
            {Column 1} & {Column 2}\\
        \midrule
            7.1 & 4.2\\
            \uline{8.7} & 6.5\\
            9.3 & \uline{1.0}\\
        \bottomrule
        \end{tabular}
    \end{threeparttable}
    \end{table}
\end{document} 
  • Seems to work with dcolumn if that helps: tex.stackexchange.com/a/258799 – LaRiFaRi Aug 5 '15 at 14:28
  • 1
    @LaRiFaRi I'm afraid this is really not going to work with siunitx: you can't apply underlining in a way that's compatible with boxing up material. – Joseph Wright Aug 5 '15 at 14:32
11
+200

Here, I just get clever with an \llap, which I incorporate into Uline. Not sure if it will handle all cases, but I am willing to adapt further as needed. This first version is simplest, but can get confused if the table data does not conform to the table-format:

\documentclass{article}
\usepackage{etoolbox,siunitx,booktabs,threeparttable,multirow,array,graphicx}
\usepackage[normalem]{ulem}
\robustify\bfseries
\robustify\uline
\sisetup{detect-weight        = true,
         detect-inline-weight = math,
         tight-spacing        = true,
         table-format         = 2.1,
         }
\def\Uline#1{#1\llap{\uline{\phantom{#1}}}}
\begin{document}
    \begin{table}
    \caption{Insert caption here}
    \centering
    \begin{threeparttable}[b]
        \begin{tabular}{SS}
        \toprule
            {Column 1} & {Column 2}\\
        \midrule
            7.1 & 4.2\\
            \Uline{8.7} & 6.5\\
            9.3 & \Uline{11.0}\\
        \bottomrule
        \end{tabular}
    \end{threeparttable}
    \end{table}
\end{document}

enter image description here


This alternate version is more robust, if the data entries do not conform to the table-format. However, the definition of \Decimal must be set in accord with table-format. Here are some corresponding values:

table-format = 2.0 ---> \def\Decimal{}

table-format = 2.1 ---> \def\Decimal{.0}

table-format = 2.2 ---> \def\Decimal{.00}

EDITED to handle case of \Uline{11.}, which auto-adds a zero following the decimal point. RE-EDITED to place cursor at end of data entry, so that, for example, \Uline{11.2}345 behaves as expected, with the 11.2 underlined and the 345 not so.

\documentclass{article}
\usepackage{etoolbox,siunitx,booktabs,threeparttable,multirow,array,graphicx}
\usepackage[normalem]{ulem}
\robustify\bfseries
\robustify\uline
\sisetup{detect-weight        = true,
         detect-inline-weight = math,
         tight-spacing        = true,
         table-format         = 2.2,
         }
\def\Decimal{.00}% This structure should corresponds to the ".2" of "table-format"
\def\Uline#1{\Ulinehelp#1 }
\def\Ulinehelp#1.#2 {%
  #1.#2\setbox0=\hbox{#1\Decimal}\hspace{-\wd0}{\if\relax#2\relax%
    \uline{\phantom{#1.0}}\else\uline{\phantom{#1.#2}}\fi}%
    }
\begin{document}
    \begin{table}
    \caption{Insert caption here}
    \centering
    \begin{threeparttable}[b]
        \begin{tabular}{SS}
        \toprule
            {Column 1} & {Column 2}\\
        \midrule
            \Uline{7.10} & \Uline{544.2123}\\
            \Uline{8.777} & 6.5\\
            9.3 & \Uline{11.}\\
        \bottomrule
        \end{tabular}
    \end{threeparttable}
    \end{table}
\end{document}

enter image description here

  • Looks great :) I haven't got time to see if it works in my table at the moment, but I will mark it as correct anyway! – emmalgale Aug 10 '15 at 9:34

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.