siunitx:使用 table-space-text-pre/post 防止小数点对齐并在表格列周围添加空格

siunitx:使用 table-space-text-pre/post 防止小数点对齐并在表格列周围添加空格

下面,是否可以使用在各自的列周围创建table-space-text-pre = \hspace{2ex}table-space-text-post = \hspace{2ex}添加额外的空间,同时防止小数点对齐table-parse-only

在此处输入图片描述

\documentclass{article}
\usepackage{siunitx,tabularx,ragged2e}
\usepackage[table]{xcolor}

\sisetup{
    load-configurations = abbreviations,
}

\begin{document}
    \setlength{\tabcolsep}{0pt}
    \rowcolors{1}{gray!30}{}
    \begin{tabularx}{0.5\linewidth}{
            >{\RaggedLeft}X
            S[table-format = 4.3,
            table-parse-only,
            table-number-alignment = right,
            round-mode = off,
            tight-spacing = true,
            table-space-text-pre = \hspace{2ex},
            table-space-text-post = \hspace{2ex},
            ]
            s[table-unit-alignment = left,
            table-space-text-pre = \hspace{2ex},
            ]
        }
    another text & 1400  & \kilo\gram \\
    another text & 0.015 & \m \\
    \end{tabularx}
\end{document}

答案1

我必须承认我不太理解你的问题。首先你将其设置\tabcolsep为零,但随后你又要求列之间留有空格……无论如何,我猜你正在寻找类似这样的东西:

\documentclass{article}
\usepackage{siunitx,tabularx,ragged2e}
\usepackage[table]{xcolor}

\begin{document}
        \setlength\tabcolsep{0pt}
        \rowcolors{1}{gray!30}{}
    \begin{tabularx}{0.5\linewidth}{>{\RaggedLeft}X 
                                    >{\hskip 1em}r<{\,}
                                    s}
    another text & 1400  & \kilo\gram \\
    another text & 0.015 & \m \\
    \end{tabularx}
\end{document}

生成下表:

在此处输入图片描述

当然,这需要你手动定义列周围的空间。如果你有许多类似的情况,那么定义新的列类型会很方便。例如:

\documentclass{article}
\usepackage{siunitx,tabularx,ragged2e}
\newcolumntype{R}{>{\hskip 1em}r<{\,}} % <---
\usepackage[table]{xcolor}

\begin{document}
        \setlength\tabcolsep{0pt}
        \rowcolors{1}{gray!30}{}
    \begin{tabularx}{0.5\linewidth}{>{\RaggedLeft}X
                                    R % <---
                                    s}
    another text & 1400  & \kilo\gram \\
    another text & 0.015 & \m \\
    \end{tabularx}
\end{document}

答案2

只需使用>{\hspace{2ex}}

在此处输入图片描述

\documentclass{article}
\usepackage{siunitx,tabularx,ragged2e}
\usepackage[table]{xcolor}

\sisetup{
    load-configurations = abbreviations,
}

\begin{document}
    \setlength{\tabcolsep}{0pt}
    \rowcolors{1}{gray!30}{}
    \begin{tabularx}{0.5\linewidth}{
            >{\RaggedLeft}X
            >{\hspace{1ex}}S[
            table-format = 4.3,
            table-parse-only,
            table-number-alignment = right,
            round-mode = off,
            tight-spacing = true,
            ]<{\hspace{2ex}}
            s[
            table-unit-alignment = left,
            ]
        }
        another text & 1400  & \kilo\gram \\
        another text & 0.015 & \m \\
    \end{tabularx}
\end{document}

相关内容