capnfabs

Method for auto-aligning parts of complex numbers in table.

Nov 21st, 2012
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. \documentclass[a4paper]{article}
  2.  
  3. \usepackage{booktabs}
  4. \usepackage{array}
  5. \usepackage{xstring}
  6. \usepackage{collcell}
  7.  
  8. % see http://tex.stackexchange.com/a/83639/14685
  9. \newlength{\WidestRealNum}
  10. \newcommand*{\ApplyNumFormatting}[1]{%
  11.     \StrBefore{#1}{+}[\RealPart]%
  12.     \StrBehind{#1}{j}[\ImagPart]%
  13.     $\makebox[\WidestRealNum][r]{$\RealPart$} + \text{j}\ImagPart$%
  14. }%
  15. \newcolumntype{N}[1]{>{\settowidth{\WidestRealNum}{$#1$}\collectcell\ApplyNumFormatting}l<{\endcollectcell}}
  16.  
  17. \begin{document}
  18. \begin{table}[h!]
  19.    \caption{Bus Loads}
  20.    \label{fig:figurename}
  21.    \centering
  22.        \begin{tabular}{l|N{999}}
  23.        \toprule
  24.        \textbf{Bus} & \multicolumn{1}{c}{\textbf{Bus Load (MVA)}} \\
  25.        \midrule
  26.            b1 &  50 + j 30.99 \\
  27.            b2 & 170 + j105.35\\
  28.            b3 & 200 + j123.94 \\
  29.            b4 & 150 + j49.58 \\
  30.        \bottomrule
  31.        \end{tabular}
  32. \end{table}
  33. \end{document}
RAW Paste Data