5

Background:

I would like to improve my matlab function mat2lat(A):

https://github.com/KasparJohannesSchneider/mat2lat/issues/1

Problem:

For complex matrices, the alignment mode {c} doesn't look very good because the + and - signs in between the real and the imaginary part are not aligned, see screenshot below. Because the LaTex code is created programmatically, it wouldn't be a problem to just create a separate column for the sign, however, this results in a weird-looking space to the left and right of this column, see "Ugly workaround" below.

Output of the MWE down below

Question:

First of all, I would strongly prefer not to include any packages other than \usepackage{amsmath}. What is the best solution for displaying complex matrices in LaTex?

Minimal Working Example:

\documentclass[11pt, titlepage, oneside, a4paper]{article}
\usepackage{amsmath}

\begin{document}

    \textbf{Works for real matrices:}
    $$\left[\begin{array}{ccc}
        {64+2.828i} & {1+1i}      & {36+2.449i} \\
        {9+1.732i}  & {25+2.236i} & {49+2.646i} \\
        {16+2i}     & {81+3i}     & {4+1.414i} 
    \end{array}\right]$$

    \textbf{Ugly workaround:}
    $$\left[\begin{array}{rclcrclcrcl}
        64&+&2.828i && 1&+&1i      && 36&+&2.449i \\
        9&+&1.732i  && 25&+&2.236i && 49&+&2.646i \\
        16&+&2i     && 81&+&3i     && 4&+&1.414i 
    \end{array}\right]$$
\end{document}

3 Answers3

5

If no further packages can be loaded you have to add the correct \medmuskip around the binary operators by hand

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\[ % never use $$...$$ in LaTeX: https://tex.stackexchange.com/q/503/82917
\left[
\begin{array}{*{3}{r@{\mskip\medmuskip}c@{\mskip\medmuskip}l}}
64&+&2.828i & 1&+&1i      & 36&+&2.449i \\
9&+&1.732i  & 25&+&2.236i & 49&+&2.646i \\
16&+&2i     & 81&+&3i     & 4&+&1.414i 
\end{array}
\right]
\]

\end{document}

enter image description here

campa
  • 31,130
  • I only used $$...$$ for the Minimal Working Example, usually, I use \begin{equation}...\end{equation}.
  • Is there an elegant solution using other packages?
  • Sorry, I can't yet upvote in this StackExchange and I usually wait a while before I accept an answer.
  • – KasparJohannes Oct 21 '19 at 15:00
  • 1
    Well, loading the array package you can attach an empty group before and after the c column in order to get the correct spacing. It's not per se more elegant but it wouldn't require knowledge of which \XXXmuskip you need. And you are absolutely right with your third comment: accepting the answer right away discourages others from posting alternatives, I believe. – campa Oct 21 '19 at 15:06