siunitx 1 未満の数値のみ小数点付き

siunitx 1 未満の数値のみ小数点付き

次のような表があるとします

\documentclass{article}
\usepackage{siunitx}
\begin{document}

\begin{table}
\begin{tabular}{S[table-alignment=right, 
                  round-mode=places, 
                  round-precision=1, 
                  table-format=5.3,
                  zero-decimal-to-integer]}
{title}  \\
11111    \\
11.11    \\
0.11     \\
\end{tabular}
\end{table}

\end{document}

そして、1 より大きい数値は整数に丸められ、1 より小さい数値は小数点第 1 位に丸められるよう数値を丸めたいと思います。結果は次のようになります11111, 11, 0.1

私は以下の組み合わせを試しましたが、望ましくない結果になりました

round-mode=figures and round-precision=1: 10000, 10, 0.1
round-mode=figures and round-precision=5: 11111, 11.110, 0.11000
round-mode=places  and round-precision=1: 11111, 11.1, 0.1

しかし、必要なものを取得するための適切な設定がわかりません( で可能になるくらい一般的な設定になることを願っていますsiunitx)。

答え1

これをテーブル内でのみ使用する場合は、pgfplotstable数値を使用して前処理することができます。以下では、round int前処理を提供する新しいキーを定義します。

サンプル出力

\documentclass{article}

\usepackage{siunitx,pgfplotstable}
\pgfplotsset{compat=1.17}

\begin{document}

\makeatletter
\pgfplotsset{table/round int/.style={%
  /pgfplots/table/preproc cell content/.append code={%
    \pgfkeysgetvalue{/pgfplots/table/@cell content}\pgfmathresult
    \ifx\pgfmathresult\pgfutil@empty
    \else
    \pgfmathparse{abs(\pgfmathresult) > 1 ? round(\pgfmathresult) : \pgfmathresult}%
    \pgfkeyslet{/pgfplots/table/@cell content}\pgfmathresult
    \fi}}}
    \makeatother

\pgfplotstabletypeset[multicolumn names,
  columns/0/.style={column name={title},
    string type,
    column type={S[table-alignment=right,
                  round-mode=places,
                  round-precision=1,
                  table-format=-5.1,
                  zero-decimal-to-integer]},
    round int
}
  ]{
11111
11.11
11.85
0.11
0.19
-10.2
-10.8
}

\end{document}

preproc/expr残念ながら、に含まれている標準にはpgfplotstable、データに適さない設定がいくつかあります。 上記のコードは、preproc/exprそのパッケージで が実装されている方法に基づいています。

列内で数字を右揃えにしたい場合は、列table-formatの引数を次のように置き換えます。Stable-parse-only

右揃えのサンプル

\documentclass{article}

\usepackage{siunitx,pgfplotstable}
\pgfplotsset{compat=1.17}

\begin{document}

\makeatletter
\pgfplotsset{table/round int/.style={%
  /pgfplots/table/preproc cell content/.append code={%
    \pgfkeysgetvalue{/pgfplots/table/@cell content}\pgfmathresult
    \ifx\pgfmathresult\pgfutil@empty
    \else
    \pgfmathparse{abs(\pgfmathresult) > 1 ? round(\pgfmathresult) : \pgfmathresult}%
    \pgfkeyslet{/pgfplots/table/@cell content}\pgfmathresult
    \fi}}}
    \makeatother

\pgfplotstabletypeset[multicolumn names,
  columns/0/.style={column name={title},
    string type,
    column type={S[table-alignment=right,
                  round-mode=places,
                  round-precision=1,
                  zero-decimal-to-integer,
                  table-parse-only]},
    round int
}
  ]{
11111
11.11
11.85
0.11
0.19
-10.2
-10.8
}

\end{document}

答え2

このような機能を表内と表外のテキストの両方で使用できるようにする必要がある場合は、 の数式機能を使用してpgfmathを定義できます\MyRoundMacro

\newcommand*{\MyRound}[1]{%
    \pgfmathtruncatemacro{\@IntegerComponent}{abs(#1)}%
    \ifnum\@IntegerComponent=0
        \num[round-mode=places, round-precision=1]{#1}%
    \else
        \num{\@IntegerComponent}%
    \fi
}%

これを直接使用することもできますそれぞれテーブルにエントリを追加するか、collcellパッケージを使用してカスタム列タイプを定義しR、それを使用します。

\newcolumntype{R}{>{\collectcell\MyRound}r<{\endcollectcell}}

ここで、 はr希望する列の配置です。

MWE によって生成された以下の表は、希望どおりです。

ここに画像の説明を入力してください

ノート:

  • 列型を使用するR場合は、どれでも内の数値コンテンツ (タイトル行など) ではありません\multicolumn{1}{c}{}

コード:

\documentclass{article}
\usepackage{siunitx}
\usepackage{collcell}% Needed only if desire to use the `R` column type defined below
\usepackage{pgfmath}

\begin{document}

\makeatletter
\newcommand*{\MyRound}[1]{%
    \pgfmathtruncatemacro{\@IntegerComponent}{abs(#1)}%
    \ifnum\@IntegerComponent=0
        \num[round-mode=places, round-precision=1]{#1}%
    \else
        \num{\@IntegerComponent}%
    \fi
}%
\makeatother

\newcolumntype{R}{>{\collectcell\MyRound}r<{\endcollectcell}}

In a table the \verb|R| column type (requires non data entries to be wrapped in a \verb|\multicolumn|):

\begin{tabular}{R}
    \multicolumn{1}{c}{\bfseries title}  \\
    11111    \\
    11.11    \\
    0.11     \\
\end{tabular}

Can use the \verb|\MyRound| macro directly in a table and
outside of a table:

\begin{tabular}{r}
    {\bfseries title}  \\
    \MyRound{11111}    \\
    \MyRound{11.11}    \\
    \MyRound{0.11}     \\
\end{tabular}

Outside of a table:

\MyRound{11111}\par
\MyRound{11.11}\par
\MyRound{0.11}\par

\end{document}

関連情報