4

I'm having some problems when try align numbers, results from calculations, using siunitx package. That is my code.

\documentclass{article}
\usepackage{siunitx}
\usepackage{fp}
\usepackage{booktabs}

% #1: decimal places (default=2), #2: value
\newcommand*{\KelvinToCelsius}[2][2]{%
    \FPeval\tempKtoC{round((#2-273.15):#1)}%
    \num{\tempKtoC}%
}

\begin{document}
    \begin{tabular}{S S}
        \toprule
        \si{\kelvin} & \si{\celsius} \\
        \midrule
           10.3  & {\KelvinToCelsius{10.3}}     \\
          200.34 & {\KelvinToCelsius{200.34}}   \\
          500.26 & {\KelvinToCelsius{500.26}}   \\
         1200.5  & {\KelvinToCelsius[1]{1200.5}}\\
        12345    & {\KelvinToCelsius[0]{12345}} \\
        \bottomrule
    \end{tabular}
\end{document}

Which produces:

output image

Note that the numbers in second column don't be align.

How to solve this?

2
  • 1
    Maybe section 7.2 "Expanding content in tables" of the siunitx manual can help you.
    – leandriis
    Apr 29, 2019 at 17:32
  • You need something expandable, use the xfp package instead of fp and avoid optional arguments. Apr 29, 2019 at 17:35

2 Answers 2

5

Use the full power of expl3; the \KelvinToCelsius command must be expandable and you can have it even with an optional argument.

Using \fpeval frees you from fp.

\documentclass{article}
\usepackage{siunitx,xfp}
\usepackage{booktabs}

% #1: decimal places (default=2), #2: value
\NewExpandableDocumentCommand{\KelvinToCelsius}{O{2}m}{%
  \fpeval{round(#2-273.15,#1)}%
}

\begin{document}

\begin{tabular}{
  S[table-format=5.2]
  S[table-format=5.2]
}
\toprule
{\si{\kelvin}} & {\si{\celsius}} \\
\midrule
   10.3  & \KelvinToCelsius{10.3}     \\
  200.34 & \KelvinToCelsius{200.34}   \\
  500.26 & \KelvinToCelsius{500.26}   \\
 1200.5  & \KelvinToCelsius[1]{1200.5}\\
12345    & \KelvinToCelsius[0]{12345} \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

1
3

The commands must be expandable so that siunitx sees "pure numbers". Better use the xfp package for the calculations and avoid optional arguments.

\documentclass{article}
\usepackage{siunitx}
\usepackage{xfp}
\usepackage{booktabs}

% #1: decimal places (default=2), #2: value
\newcommand*{\KelvinToCelsius}[1]{% no optional argument
    \fpeval{round((#1-273.15)/2,2)}%    
}

\begin{document}
    \begin{tabular}{S S}
        \toprule
        \si{\kelvin} & \si{\celsius} \\
        \midrule
           10.3  & \KelvinToCelsius{10.3}     \\
          200.34 & \KelvinToCelsius{200.34}   \\
          500.26 & \KelvinToCelsius{500.26}   \\
        %1200.5  & {\KelvinToCelsius[1]{1200.5}}\\
        %12345    & {\KelvinToCelsius[0]{12345}} \\
        \bottomrule
    \end{tabular}
\end{document}

enter image description here

2
  • Why no optional argument? ;-)
    – egreg
    Apr 29, 2019 at 17:50
  • @egreg because I was too lazy to check if it would work with xparse. Apr 29, 2019 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.