
Suponha que eu tenha a seguinte tabela
\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}
e eu gostaria de arredondar os números de forma que os números acima de 1 sejam arredondados para números inteiros e os números abaixo de 1 sejam arredondados para a primeira casa decimal. O resultado aqui seria 11111, 11, 0.1
.
Eu tentei as seguintes combinações, que produziram resultados indesejáveis
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
mas não consigo descobrir as configurações corretas para obter o que preciso (e espero que seja comum o suficiente para que seja possível siunitx
).
Responder1
Se for apenas para uso em tabelas, você poderá usar pgfplotstable
e pré-processar os números. Abaixo defino uma nova chave round int
que fornece o pré-processamento.
\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}
Infelizmente, o padrão preproc/expr
incluído pgfplotstable
possui algumas configurações que não são apropriadas para seus dados. O código acima é baseado em como preproc/expr
é implementado nesse pacote.
Se você quiser que os números sejam alinhados à direita na coluna, basta substituir o table-format
argumento da S
coluna por 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}
Responder2
Se você precisar usar esse recurso tanto em uma tabela quanto fora do texto, poderá usar os recursos matemáticos pgfmath
para definir \MyRoundMacro
:
\newcommand*{\MyRound}[1]{%
\pgfmathtruncatemacro{\@IntegerComponent}{abs(#1)}%
\ifnum\@IntegerComponent=0
\num[round-mode=places, round-precision=1]{#1}%
\else
\num{\@IntegerComponent}%
\fi
}%
Você pode usar isso diretamente paracadaentrada na tabela ou use o collcell
pacote para definir um tipo de coluna personalizado R
e use isso:
\newcolumntype{R}{>{\collectcell\MyRound}r<{\endcollectcell}}
onde the r
é o alinhamento de coluna desejado.
As tabelas produzidas pelo MWE abaixo são as desejadas:
Notas:
- Usar o
R
tipo de coluna requer que você envolvaqualquernão conteúdo numérico (como as linhas de título) dentro de um arquivo\multicolumn{1}{c}{}
.
Código:
\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}