1

This is potentially a very noob question, but how could one use multiple conditions when parsing with xstring?

The accepted answer that I'm linking below, shows how one can align complex numbers in a table. https://tex.stackexchange.com/a/83639/118726 I want the complex numbers to be allowed to be of the form a+ib but also a-ib. I need to modify the line

\StrBefore{#1}{+}[\RealPart]%

to look for the string before + OR -.

Any ideas?

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}

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

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

\newlength{\WidestRealNum}
\settowidth{\WidestRealNum}{$99999$}
\newcommand*{\ApplyNumFormatting}[1]{%
    \StrBefore{#1}{+}[\RealPart]%
    \StrBehind{#1}{i}[\ImagPart]%
    $\makebox[\WidestRealNum][r]{$\RealPart$} + i\,\ImagPart$%
}%

\newcolumntype{N}{>{\collectcell\ApplyNumFormatting}l<{\endcollectcell}}

\begin{document}
\begin{table}
\caption{Some complex numbers }
\label{table:Fhkl}
\centering
\begin{tabular}{ N S[]}
    \toprule
      \multicolumn{1}{c}{N column  } &  \multicolumn{1}{c}{S[] column}\\
        \midrule
     -774.66 - i 325.61    & -774.66 - i 325.61   \\
     774.66 - i 325.61     & 774.66 - i 325.61   \\
     -151.85 + i 325.61    & -151.85 + i 325.61    \\
     151.85 + i 325.61     & 151.85 + i 325.61    \\
        \bottomrule
    \end{tabular}
\end{table}
%


\end{document}

The S[] type is indeed a solution but I do prefer the center alignment of the sign. You can see below the N format as it is now fails for anything other than a+ib or -a+ib.

mwe table of complex numbers

2
  • 1
    Welcome to TeX.SX! Why don't you use the S column added by siunitx? It is good that you linked the answer, but it would've been better if you posted a minimal working example (MWE).
    – Skillmon
    Sep 1 '18 at 13:13
  • 1
    The listofitems package can parse based on multiple separators (e.g., \setsepchar{+||-}). Not sure if it can be leveraged for your app. Sep 1 '18 at 15:42
3

Not the most elegant solution but here is what I have so far, based on the solution linked above. I actually tweaked this solution starting from the idea suggested by @koleygr:

If these is a '+' then Real part is before the '+'. If there is no '+' check if there are two '-'s. If there are two '-'s the Real part is before the second '-', else it is before the first '-'.

\documentclass[a4paper]{article}

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

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


\newlength{\WidestRealNum}
\settowidth{\WidestRealNum}{$99999$}
\newcommand*{\ApplyNumFormatting}[1]{%
\IfSubStr{#1}{+}{%
    \StrBefore{#1}{+}[\RealPart]}{%
    \IfSubStr[2]{#1}{-}{%
        \StrBefore[2]{#1}{-}[\RealPart]}{%
        \StrBefore{#1}{-}[\RealPart]}%
   }
 \StrBehind{#1}{i}[\ImagPart]%

\IfSubStr{#1}{+}{%
    $\makebox[\WidestRealNum][r]{$\RealPart$} + i\,\ImagPart$}{%
    $\makebox[\WidestRealNum][r]{$\RealPart$} - i\,\ImagPart$}
}%


\newcolumntype{N}{>{\collectcell\ApplyNumFormatting}l<{\endcollectcell}}

And the table is:

table of complex numbers

1
  • Welcome Elena, nice solution.... You may add \IfStreq conditions as second solution (xstring's other command) using your own checks and I am going to delete my solution ... (just added for pluralism for now) (+1)
    – koleygr
    Sep 1 '18 at 13:30
1

Another solution with \IfStrEq command. The idea is that if "+" is not found by \StrBefore command, then the optional argument \RealPart will remain empty... I use this case in an \IfStrEq command.

\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]{%
\gdef\msign{+}%
    \StrBefore{#1}{\msign}[\RealPart]\IfStrEq{\RealPart}{}{\gdef\msign{-}\StrBefore{#1}{\msign}[\RealPart]}{}%
    \StrBehind{#1}{j}[\ImagPart]%
    $\makebox[\WidestRealNum][r]{$\RealPart$} \msign 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}

Just as another fast fix and idea just for your problem (But @ElenasPascal answer is more complete and she has +1 from me)

1
  • 1
    I like it as an alternative for the above issue; the problem is now that both of these fail for -a-ib... Back to the drawing board Sep 1 '18 at 15:55

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.