Say I have two csv files representing the real and imaginary part of a complex matrix. I would like to nicely display this matrix in LaTeX. The difficulty is to correctly round numbers and not displaying them if they are rounded to 0.
For example, 1.0000000e+00 and 5.0000000e-01 should give 1+0.5i and 5.0000000e-01 and 0.0000000e+00 should not display the imaginary part (ie 0.5).
I started with these questions: Aligning complex numbers in centre of table with siunitx and Combine columns of a csv but couldn't come up with a satisfying solution.
Here is the start!
\documentclass{article}
\usepackage{pgfplotstable}
\begin{filecontents}{blah_real.csv}
re1 re2
1.0000000e+00 1.0000000e+00
5.0000000e-01 5.0000000e-01
0.0000000e+00 0.0000000e+00
5.0000000e-01 5.0000000e-01
\end{filecontents}
\begin{filecontents}{blah_im.csv}
0.0000000e+00 0.0000000e+00
5.0000000e-01 5.0000000e-01
1.0000000e+00 1.0000000e+00
5.0000000e-01 5.0000000e-01
\end{filecontents}
\begin{document}
\newcommand{\complex}[1]{%
\pgfplotstableread[col sep=space, trim cells=true]{#1_real.csv}\real
\pgfplotstableread[col sep=space, trim cells=true, header=false]{#1_im.csv}\imaginary
\pgfplotstablecreatecol[copy column from table={\imaginary}{[index] 0}]{im1}{\real}
\pgfplotstablecreatecol[copy column from table={\imaginary}{[index] 1}]{im2}{\real}
\pgfplotstabletypeset[
columns/complex1/.style={string type, column name={}},
create on use/complex1/.style={%
create col/assign/.code={%
\edef\value{%
\noexpand
\pgfmathprintcomplexnumber[fixed zerofill]{\thisrow{re1}}{\thisrow{im1}}}
\pgfkeyslet{/pgfplots/table/create col/next content}\value
}
},
columns/complex2/.style={string type, column name={}},
create on use/complex2/.style={%
create col/assign/.code={%
\edef\value{%
\noexpand
\pgfmathprintcomplexnumber[fixed zerofill]{\thisrow{re2}}{\thisrow{im2}}}
\pgfkeyslet{/pgfplots/table/create col/next content}\value
}
},
columns={complex1, complex2}
]{\real}
}
\makeatletter
\def\pgfmathprintcomplexnumber{%
% \protect allows to supply \pgfmathprintnumber inside of latex
% captions. The \csname yields \relax in case protect is undefined.
\pgf@texdist@protect\pgfmathprintcomplexnumber@protected
}%
\def\pgfmathprintcomplexnumber@protected{%
\pgfutil@ifnextchar[%
{\pgfmathprintcomplexnumber@OPT}%
{\pgfmathprintcomplexnumber@noopt}%
}
\def\pgfmathprintcomplexnumber@noopt#1#2{%
\begingroup
\pgfmathprintnumber@{#1}%
\let\pgfmathresultreal\pgfmathresult
\pgfmathfloatparsenumber{\pgfmathresult}
\pgfmathfloatifflags{\pgfmathresult}{0}{
\let\pgfmathresultreal\empty
\pgfqkeys{/pgf/number format}{showpos=false}%
}{
\pgfqkeys{/pgf/number format}{showpos=true}%
}%
\pgfmathprintnumber@{#2}%
\let\pgfmathresultim\pgfmathresult
\pgfmathfloatparsenumber{\pgfmathresult}
\pgfmathfloatifflags{\pgfmathresult}{0}{%
\let\pgfmathresultim\empty
}{
\edef\pgfmathresultim{\pgfmathresultim i}
}%
\ifpgfmathprintnumber@assumemathmode
\pgfmathresultreal\pgfmathresultim
\else
\pgfutilensuremath{\pgfmathresultreal\pgfmathresultim}%
\fi
\endgroup
}%
\def\pgfmathprintcomplexnumber@OPT[#1]#2#3{%
\begingroup
\pgfqkeys{/pgf/number format}{#1}%
\pgfmathprintnumber@{#2}%
\let\pgfmathresultreal\pgfmathresult
\pgfmathfloatparsenumber{\pgfmathresult}
\pgfmathfloatifflags{\pgfmathresult}{0}{
\let\pgfmathresultreal\empty
\pgfqkeys{/pgf/number format}{showpos=false}%
}{
\pgfqkeys{/pgf/number format}{showpos=true}%
}%
\pgfmathprintnumber@{#3}%
\let\pgfmathresultim\pgfmathresult
\pgfmathfloatparsenumber{\pgfmathresult}
\pgfmathfloatifflags{\pgfmathresult}{0}{%
\let\pgfmathresultim\empty
}{%
\edef\pgfmathresultim{\pgfmathresultim i}
}%
\ifpgfmathprintnumber@assumemathmode
\pgfmathresultreal\pgfmathresultim
\else
\pgfutilensuremath{\pgfmathresultreal\pgfmathresultim}%
\fi
\endgroup
}%
\makeatother
\complex{blah}
\end{document}
Any idea how to do this?
EDIT: Almost working example but I still have 0.00+1.00i and the plus sign is closer to the imaginary part...
EDIT2: \pgfmathprintcomplexnumber largely inspired by \pgfmathprintnumber. Remaining problem: 1i might be displayed

