5

I need to align numbers in a table. The numbers are generated from the \Convert command from the lengthconvert package. I want to use an S column from the siunitx package to align the numbers, but this doesn't work. Following is a MWE.

The first error reported is Argument of \tl_clear:N has an extra }. I suspect that \Convert is not fully expanded when read by the S macro. How do I fix this?


\documentclass{minimal}
\usepackage{siunitx}
\usepackage{lengthconvert}
\begin{document} \begin{tabular}{S} \Convert[number-only, unit=cm]{100pt} \\ 3.14 \\ 2.71828 \end{tabular}
\end{document}

  • No; the \Convert command, despite being defined with \DeclareExpandableDocumentCommand is not fully expandable. – egreg Nov 19 '14 at 23:39
3

The \Convert command is not fully expandable, despite being defined with \DeclareExpandableDocumentCommand (which is an error in the package).

There's a simpler way using a built-in function in expl3:

\documentclass{article}
\usepackage{siunitx}
% \usepackage{xparse} % already loaded by siunitx

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\convertdim}{mm}
 {
  \dim_to_decimal_in_unit:nn { #2 } { 1#1 }
 }
\ExplSyntaxOff

\begin{document}
\begin{tabular}{S[table-format=1.5,group-four-digits]}
    \convertdim{cm}{100pt} \\
    3.14 \\
    2.71828
\end{tabular}

\end{document}

enter image description here

A fancier version where you can state the number of decimal digits (with rounding):

\documentclass{article}
\usepackage{siunitx}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\convertdim}{O{2}mm}
 {
  \fp_eval:n { round ( \dim_to_decimal_in_unit:nn { #3 } { 1#2 } , #1 ) }
 }
\ExplSyntaxOff

\begin{document}
\begin{tabular}{S[table-format=1.5,group-four-digits]}
    \convertdim{cm}{100pt} \\
    \convertdim[0]{cm}{100pt} \\
    \convertdim[1]{cm}{100pt} \\
    \convertdim[2]{cm}{100pt} \\
    \convertdim[3]{cm}{100pt} \\
    \convertdim[4]{cm}{100pt} \\
    \convertdim[5]{cm}{100pt} \\
    3.14 \\
    2.71828
\end{tabular}

\end{document}

enter image description here

Note that \dim_to_decimal_in_unit:nn returns at most five decimal digits (but the last digit is not necessarily accurate).

  • Doesn't seem to work for me. I get an error: ./Untitled.tex:14: Undefined control sequence. \convertdim #1#2->\dim_to_decimal_in_unit:nn {#2}{1#1} l.14 \convertdim{cm}{100pt} \\ – mrclary Nov 20 '14 at 1:02
  • Okay, looks like \dim_to_decimal_in_unit:nn was not added to expl3 until July 15 2014. I'm using TeXLive 2014 release June 14 2014. :-( Any suggestions that will work for TeXLive 2014 distribution? – mrclary Nov 20 '14 at 1:24
  • @mrclary Why don't you update? Is it a TeX Live provided by some package manager (Debian, Fedora or similar) or the TUG one? – egreg Nov 20 '14 at 9:28
  • I may just have to update. However, I'd like to keep a standard TeXLive 2014 distribution as well, so I'll have to install a new distribution that is reserved for these types of minor updates. I've always used MacTex for my distribution installations, so I'll have to figure out the best way to go about this. – mrclary Nov 20 '14 at 16:37
  • @mrclary My concept of “standard” TeX Live is an updated one. – egreg Nov 20 '14 at 16:47

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.