1보다 작은 숫자에 대해서만 siunitx 십진수

1보다 작은 숫자에 대해서만 siunitx 십진수

다음 테이블이 있다고 가정합니다.

\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래핑해야 합니다.어느. 내의 숫자 내용(예: 제목 줄)이 아닙니다 \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}

관련 정보