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 Answers 3

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

2
  • 1. I only used $$...$$ for the Minimal Working Example, usually, I use \begin{equation}...\end{equation}. 2. Is there an elegant solution using other packages? 3. Sorry, I can't yet upvote in this StackExchange and I usually wait a while before I accept an answer. Oct 21, 2019 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, 2019 at 15:06
4

Why not simply three aligned environments inside a \bmatrix?

Unrelated: for displayed equations, use the latex construct \[ ... \], note plain TeX $$ ... $$.

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

\begin{document}

   \[ \begin{bmatrix}
      \, \begin{aligned}
         64&+2.828i \\ 9&+1.732i \\ 16&+2i
       \end{aligned}
      & \begin{aligned}
       1&+1i \\ 25&+2.236i \\ 81&+3i
       \end{aligned}
       & \begin{aligned}
       36&+ 2.449i \\ 49&+ 2.646i \\ 4 & + 1.414i
       \end{aligned}\,
    \end{bmatrix} \]

\end{document} 

enter image description here

1
  • This is an option too and it's probably easier to understand how it works. However, in my opinion, @campa 's solution is more elegant and looks cleaner in the .tex file. And I already implemented it anyway (the LaTex is generated by a Matlab fct)... Oct 21, 2019 at 17:48
3

If you're willing to work with some other packages, you can capture the argument of every cell in a newly-defined column (say) I and then process (separating out the real/imaginary parts):

enter image description here

\documentclass{article}

\usepackage{amsmath,eqparbox,collcell,etoolbox}

\newcolumntype{I}{>{\collectcell\RpmI}c<{\endcollectcell}}
\makeatletter
\def\rplusi@#1+#2\relax{% R + Ii
  \eqmakebox[R][r]{$#1$} + \eqmakebox[I][l]{$#2$}%
}
\def\rminusi@#1-#2\relax{% R - Ii
  \eqmakebox[R][r]{$#1$} - \eqmakebox[I][l]{$#2$}%
}
\newcommand{\RpmI}[1]{%
  \def\abc{#1}% Store argument R +/- Ii
  \patchcmd{\abc}{+}{+}{%
    \expandafter\rplusi@#1\relax% R + Ii
  }{%
    \expandafter\rminusi@#1\relax% R - Ii
  }%
}
\makeatother

\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{Workaround:}
\[
  \left[\begin{array}{III}
    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}
1
  • This is a very interesting approach and very clean/elegant (could be interesting for others that visit the post), however, not ideal for my application because I am trying to create a Matlab function that spits out a LaTex string that can be copied into a math environment of any LaTex document and should be compilable without any additional \usepackage{} or definitions. Oct 21, 2019 at 23:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .