
假設我有下表
\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 以下的數字四捨五入到第一個小數。這裡的結果將是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
的參數替換S
為table-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
列類型需要換行任何不是 .txt 中的數字內容(例如標題行)\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}