# Aligning complex numbers in centre of table with siunitx

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!]
\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:

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:

-
I tested the same formatting with the dcolumn package just to see if it is not limited to siunitx and I ended up with the same alignment. I don't know if it is a related problem though. – hpesoj626 Nov 21 '12 at 6:12

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:

## 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!]
\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}
-
Not sure what you are up to with \jmath: it's for adding an accent to! Here, a normal j would be correct. – Joseph Wright Nov 21 '12 at 8:50
@JosephWright: Ok, will correct -- I normally use the dotless variants for complex numbers. – Peter Grill Nov 21 '12 at 8:52
Thanks for that - great solution. I modified the code so I could specify the \WidestRealNum as a column argument. You can see my changes at pastebin.com/M1nEYuNZ . – Fabian Tamp Nov 21 '12 at 22:40

The siunitx package is primarily focussed on physical quantities (numbers with units). As such, the first version did not even cover complex numbers. For version 2 I was asked to add them as they crop up for example in some parts of electronics. However, as this is a relatively unusual situation, support for complex numbers is somewhat limited, and in particular the functionality for numbers with real plus complex parts is not complete. One reason for this, apart from code complexity, is maintaining reasonable performance for the most common cases: the more situations you cover, the more tests and so on are required.

In the case of numbers in tables, the real and complex parts would to me naturally form two separate columns

\documentclass[a4paper]{article}

\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{tabular}{lS[table-format = 3]S[table-format = 3.2]}
\toprule
\textbf{Bus} & \multicolumn{2}{c}{\textbf{Bus Load (MVA)}} \\
& {Real} & {Complex} \\
\midrule
b1 &  50 &  30.99 \\
b2 & 170 & 105.35 \\
b3 & 200 & 123.94 \\
b4 & 150 &  49.58 \\
\bottomrule
\end{tabular}
\end{document}

as this removes the redundant + and j in each value.

That said, I am always willing to consider extensions to siunitx, particularly if the requests come with a link demonstrating use of the requested format in the published literature. Please feel free to open an issue in the tracker.

-
Thanks for your contribution Joseph! I'd originally done something like this, but the Real + jImag notation seems to be relatively standard amongst power engineering papers. – Fabian Tamp Nov 21 '12 at 22:19

Well, it seems siunitsx has a problem with its tabular additions regarding complex numbers, For a real number your example aligns correctly. There are still a few things to do for the developers of that package regarding complex numbers. You could try:

\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!]
\label{fig:figurename}
\centering
\begin{tabular}{l|S[]}
\toprule
\textbf{Bus} & \multicolumn{1}{c}{\textbf{Bus Load (MVA)}} \\
\midrule
b1 & \num{ 50 + j 30.99} \\
b2 & \num{170 + j105.35}\\
b3 & \num{200 + j123.94} \\
b4 & \num{150 + j49.58} \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

This is a bit more cumbersome to write for long tables, but at least it aligns the complex numbers nicely:

-
Good idea. You could probably automate the application of \num with the collcell package. Also, please post complete compilable code starting from \documentclass{} thru \end{document}. – Peter Grill Nov 21 '12 at 8:16
Edit: added compilable code in the answer. – DrJay Nov 21 '12 at 8:23