siunitx v3:如何创建一个像版本 2 一样可定制的表单元列?

siunitx v3:如何创建一个像版本 2 一样可定制的表单元列?

后续行动这个答案,我需要表格单位列s,如下所示,

[per-mode = symbol, bracket-unit-denominator, sticky-per, inter-unit-product = \ensuremath{{\cdot}}]1- 尊重我传递给它的选项,并且

2- 还有两个选项可用于调整其宽度和单位的左中右对齐。

在此处输入图片描述

\documentclass[12pt]{article}

\usepackage{siunitx,xltabular,multicol,booktabs,ragged2e}
\usepackage[table]{xcolor}

\usepackage{collcell}
\newcolumntype{s}{>{\collectcell\si}l<{\endcollectcell}}
\makeatletter
\expandafter\renewcommand\expandafter*\expandafter
{\csname NC@rewrite@s\endcsname}[1][]%
{%
    \@temptokena\expandafter
    {%
        \the\@temptokena
        >{\collectcell\si}l<{\endcollectcell}%
    }%
    \NC@find
}
\makeatother

\parindent=0pt

\begin{document}
    \rowcolors{2}{gray!15}{}
    \setlength{\tabcolsep}{0mm}
    \begin{xltabular}{0.5\linewidth}{%
            X X
            S[
            table-format = 4.3e-1,
            parse-numbers = true,
            table-alignment-mode = none,
            table-number-alignment = right,
            exponent-mode = input,
            exponent-product = \times,
            round-mode = none,
            tight-spacing = true,
            ]<{\hspace{2ex}}
            s[
            per-mode = symbol, 
            bracket-unit-denominator, 
            sticky-per,
            inter-unit-product = \ensuremath{{\cdot}},
            ]
        }
        Area & A & 14.643  & \m\squared \\ % 
        Volume & V & 5.7e-04  & \m\cubed \\ % 
        Area & A & 1212.6 & \m\squared \\ % 
        Specific Heat  & c & 5.7e-04 &  \J\per\kg\K \\ % 
    \end{xltabular}
\end{document}

答案1

下面定义了一个列s,它采用一个可选参数(即siunitx应该在该列内应用的选项),后跟一个强制参数(即应该施加该参数的列类型,任何标准列都应该在这里工作,所以,,,,等等l)。crp

\documentclass[]{article}

\usepackage{siunitx}
\usepackage{array}
\usepackage{collcell}

\makeatletter
\@ifpackageloaded{mdwtab}
  {\newcolumntype{s}[2][]{>{\sisetup{#1}\collectcell\unit}#2<{\endcollectcell}}}
  {%
    \newcolumntype{s}{}%
    \renewcommand*\NC@rewrite@s[2][]
      {%
        \@temptokena\expandafter
          {%
            \the\@temptokena
            >{\sisetup{#1}\collectcell\unit}#2<{\endcollectcell}%
          }%
        \NC@find
      }%
  }
\makeatother

\begin{document}
% centred
\begin{tabular}
  {
    |
    S[table-format = 4.3e-1,
      parse-numbers = true,
      table-alignment-mode = none,
      table-number-alignment = right,
      exponent-mode = input,
      exponent-product = \times,
      round-mode = none,
      tight-spacing = true,
      ]<{\hspace{2ex}}
    |
    s[per-mode = symbol, 
      bracket-unit-denominator, 
      sticky-per,
      inter-unit-product = \ensuremath{{\cdot}}]{c}
    |
  }
  1.5 & \ohm \\
  2.5 & \ohm\second \\
  3.5 & \second\per\ohm \\
\end{tabular}

% specifying width
\begin{tabular}
  {
    |
    S[table-format = 4.3e-1,
      parse-numbers = true,
      table-alignment-mode = none,
      table-number-alignment = right,
      exponent-mode = input,
      exponent-product = \times,
      round-mode = none,
      tight-spacing = true,
      ]<{\hspace{2ex}}
    |
    s[per-mode = symbol, 
      bracket-unit-denominator, 
      sticky-per,
      inter-unit-product = \ensuremath{{\cdot}}]{p{5cm}}
    |
  }
  1.5 & \ohm \\
  2.5 & \ohm\second \\
  3.5 & \second\per\ohm \\
\end{tabular}
\end{document}

在此处输入图片描述

内部使用xltabular(具有通用性,mdwtab这对于 MWE 来说不是必需的)

\documentclass[12pt]{article}

\usepackage{siunitx}
\usepackage{array}
\usepackage{collcell}
\usepackage{xltabular}
\usepackage[table]{xcolor}

\usepackage{collcell}

\makeatletter
\@ifpackageloaded{mdwtab}
  {\newcolumntype{s}[2][]{>{\sisetup{#1}\collectcell\unit}#2<{\endcollectcell}}}
  {%
    \newcolumntype{s}{}%
    \renewcommand*\NC@rewrite@s[2][]
      {%
        \@temptokena\expandafter
          {%
            \the\@temptokena
            >{\sisetup{#1}\collectcell\unit}#2<{\endcollectcell}%
          }%
        \NC@find
      }%
  }
\makeatother

\begin{document}
\rowcolors{2}{gray!15}{}
\setlength{\tabcolsep}{0mm}
\begin{xltabular}{0.5\linewidth}
  {%
    X
    X
    S[table-format = 4.3e-1,
      parse-numbers = true,
      table-alignment-mode = none,
      table-number-alignment = right,
      exponent-mode = input,
      exponent-product = \times,
      round-mode = none,
      tight-spacing = true]
      <{\hspace{2ex}}
    s[per-mode = symbol, 
      bracket-unit-denominator, 
      sticky-per,
      inter-unit-product = \ensuremath{{\cdot}}]
      {c}
  }
    Area & A & 14.643  & \m\squared \\ % 
    Volume & V & 5.7e-04  & \m\cubed \\ % 
    Area & A & 1212.6 & \m\squared \\ % 
    Specific Heat  & c & 5.7e-04 &  \J\per\kg\K \\ % 
\end{xltabular}
\end{document}

相关内容