[Tex/LaTex] Aligning complex numbers in centre of table with siunitx

horizontal alignmentsiunitxtables

I'm trying to align complex numbers in the middle of a table column using the siunitx package for nicer alignment, but the numbers are centring based on where the decimal point would be on the real parts.

Here's my MWE:

\documentclass[a4paper]{article}

\usepackage{booktabs}
\usepackage{array}
\usepackage{siunitx}
\sisetup{
    output-complex-root = \ensuremath{\mathrm{j}},
    complex-root-position = before-number
}
\begin{document}
\begin{table}[h!]
    \caption{Bus Loads}
    \label{fig:figurename}
    \centering
        \begin{tabular}{l|S[]}
        \toprule
        \textbf{Bus} & \multicolumn{1}{c}{\textbf{Bus Load (MVA)}} \\
        \midrule
            b1 & 50 + j30.99 \\
            b2 & 170 + j105.35\\
            b3 & 200 + j123.94 \\
            b4 & 150 + j49.58 \\
        \bottomrule
        \end{tabular}
\end{table}
\end{document}

And here's the output:

MWE output

I'd like the complex numbers to be more aligned in the centre of the column. Any idea how to do this?

I've tried passing the option table-number-alignment=left to the column, but then the imaginary part disappears:

MWE output with table-number-alignment=left

Best Answer

Now that we know that complex numbers are not fully supported by the siunitx package, here is a workaround that adapts DrJay's solution and uses the collcell package to automate the formatting of the number in that the integer real part is right aligned so that the + sign is aligned:

enter image description here

Notes:

  • The real part is currently right aligned -- could be enhanced to align on the decimal point. Let me know if this is required.
  • The imaginary part is left aligned. This could also be adjusted as needed.

Code:

\documentclass[a4paper]{article}

\usepackage{booktabs}
\usepackage{array}
\usepackage{siunitx}
\usepackage{xstring}
\usepackage{collcell}

\sisetup{
    output-complex-root = \ensuremath{\mathrm{j}},
    complex-root-position = before-number
}

\newlength{\WidestRealNum}
\settowidth{\WidestRealNum}{$99999$}
\newcommand*{\ApplyNumFormatting}[1]{%
    \StrBefore{#1}{+}[\RealPart]%
    \StrBehind{#1}{j}[\ImagPart]%
    $\makebox[\WidestRealNum][r]{$\RealPart$} + j\,\ImagPart$%
}%
\newcolumntype{N}{>{\collectcell\ApplyNumFormatting}l<{\endcollectcell}}

\begin{document}
\begin{table}[h!]
    \caption{Bus Loads}
    \label{fig:figurename}
    \centering
        \begin{tabular}{l|N}
        \toprule
        \textbf{Bus} & \multicolumn{1}{c}{\textbf{Bus Load (MVA)}} \\
        \midrule
            b1 &  50 + j 30.99 \\
            b2 & 170 + j105.35\\
            b3 & 200 + j123.94 \\
            b4 & 150 + j49.58 \\
        \bottomrule
        \end{tabular}
\end{table}
\end{document}
Related Question