[Tex/LaTex] Formatting a large matrix across multiple lines

formattingline-breakingmatrices

I have the following latex to create a matrix:

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{graphicx,ctable,booktabs}

\begin{document}
$\left(\begin{array}{rrr}
-\frac{200 \, x^{2} {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \frac{200 \, x^{2}}{x^{2} + y^{2}} + \frac{200 \, {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{\sqrt{x^{2} + y^{2}}} + \frac{2000 \, \pi {\left(5 \, \pi \arctan\left(\frac{y}{x}\right) - z\right)} y}{x^{3} {\left(\frac{y^{2}}{x^{2}} + 1\right)}} + \frac{5000 \, \pi^{2} y^{2}}{x^{4} {\left(\frac{y^{2}}{x^{2}} + 1\right)}^{2}} - \frac{2000 \, \pi {\left(5 \, \pi \arctan\left(\frac{y}{x}\right) - z\right)} y^{3}}{x^{5} {\left(\frac{y^{2}}{x^{2}} + 1\right)}^{2}} & -\frac{200 \, x y {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \frac{200 \, x y}{x^{2} + y^{2}} - \frac{1000 \, \pi {\left(5 \, \pi \arctan\left(\frac{y}{x}\right) - z\right)}}{x^{2} {\left(\frac{y^{2}}{x^{2}} + 1\right)}} - \frac{5000 \, \pi^{2} y}{x^{3} {\left(\frac{y^{2}}{x^{2}} + 1\right)}^{2}} + \frac{2000 \, \pi {\left(5 \, \pi \arctan\left(\frac{y}{x}\right) - z\right)} y^{2}}{x^{4} {\left(\frac{y^{2}}{x^{2}} + 1\right)}^{2}} & \frac{1000 \, \pi y}{x^{2} {\left(\frac{y^{2}}{x^{2}} + 1\right)}} \\
-\frac{200 \, x y {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \frac{200 \, x y}{x^{2} + y^{2}} - \frac{1000 \, \pi {\left(5 \, \pi \arctan\left(\frac{y}{x}\right) - z\right)}}{x^{2} {\left(\frac{y^{2}}{x^{2}} + 1\right)}} - \frac{5000 \, \pi^{2} y}{x^{3} {\left(\frac{y^{2}}{x^{2}} + 1\right)}^{2}} + \frac{2000 \, \pi {\left(5 \, \pi \arctan\left(\frac{y}{x}\right) - z\right)} y^{2}}{x^{4} {\left(\frac{y^{2}}{x^{2}} + 1\right)}^{2}} & -\frac{200 \, y^{2} {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \frac{200 \, y^{2}}{x^{2} + y^{2}} + \frac{200 \, {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{\sqrt{x^{2} + y^{2}}} + \frac{5000 \, \pi^{2}}{x^{2} {\left(\frac{y^{2}}{x^{2}} + 1\right)}^{2}} - \frac{2000 \, \pi {\left(5 \, \pi \arctan\left(\frac{y}{x}\right) - z\right)} y}{x^{3} {\left(\frac{y^{2}}{x^{2}} + 1\right)}^{2}} & -\frac{1000 \, \pi}{x {\left(\frac{y^{2}}{x^{2}} + 1\right)}} \\
\frac{1000 \, \pi y}{x^{2} {\left(\frac{y^{2}}{x^{2}} + 1\right)}} & -\frac{1000 \, \pi}{x {\left(\frac{y^{2}}{x^{2}} + 1\right)}} & 202
\end{array}\right)$
\end{document}

Here is the result:
Large Matrix

How can I properly format the above matrix so that it does not get cut off? How can I make span multiple lines instead?

Best Answer

I am not sure that is good enough for you, but one way to display this matris is to:

  1. Define symbols for expressions that occur often.
  2. Even with #1, this still required scaling the matrix to fit in the line width

enter image description here


Another solution is to define a symbol for the four complicated entries:

enter image description here

Notes:

  • The [showframe] option was applied to the geometry package just to show the page margins.
  • Numerous \, were eliminated -- They are not necessary as the spacing is correct without them.

Code:

\documentclass[12pt]{article}
\usepackage[margin=1in,showframe]{geometry}
\usepackage{mathtools}
\usepackage{graphicx}

\newcommand{\SQ}{\phi}
\newcommand{\SQExpanded}{x^{2} + y^{2}}
\newcommand{\FivePi}{\omega}
\newcommand{\FivePiExpanded}{5 \pi \arctan\left(\frac{y}{x}\right) - z}
\newcommand{\Denominator}{\left(\frac{\phi}{x^2}\right)}
% ----
\newcommand{\A}{%
-\frac{200 x^{2} {\left(\sqrt{\SQ} - 1\right)}}{{\left(\SQ\right)}^{\frac{3}{2}}} + \frac{200  x^{2}}{\SQ} + \frac{200  {\left(\sqrt{\SQ} - 1\right)}}{\sqrt{\SQ}} + \frac{2000  \pi {\FivePi} y}{x^{3} {\Denominator}} + \frac{5000  \pi^{2} y^{2}}{x^{4} {\Denominator}^{2}} - \frac{2000  \pi {\FivePi} y^{3}}{x^{5} {\Denominator}^{2}}%
}%
\newcommand{\B}{%
-\frac{200  x y {\left(\sqrt{\SQ} - 1\right)}}{{\left(\SQ\right)}^{\frac{3}{2}}} + \frac{200  x y}{\SQ} - \frac{1000  \pi {\FivePi}}{x^{2} {\Denominator}} - \frac{5000  \pi^{2} y}{x^{3} {\Denominator}^{2}} + \frac{2000  \pi {\FivePi} y^{2}}{x^{4} {\Denominator}^{2}}%
}
\newcommand{\C}{%
-\frac{200  x y {\left(\sqrt{\SQ} - 1\right)}}{{\left(\SQ\right)}^{\frac{3}{2}}} + \frac{200  x y}{\SQ} - \frac{1000  \pi {\FivePi}}{x^{2} {\Denominator}} - \frac{5000  \pi^{2} y}{x^{3} {\Denominator}^{2}} + \frac{2000  \pi {\FivePi} y^{2}}{x^{4} {\Denominator}^{2}}%
}
\newcommand{\D}{%
-\frac{200  y^{2} {\left(\sqrt{\SQ} - 1\right)}}{{\left(\SQ\right)}^{\frac{3}{2}}} + \frac{200  y^{2}}{\SQ} + \frac{200  {\left(\sqrt{\SQ} - 1\right)}}{\sqrt{\SQ}} + \frac{5000  \pi^{2}}{x^{2} {\Denominator}^{2}} - \frac{2000  \pi {\FivePi} y}{x^{3} {\Denominator}^{2}}%
}

\begin{document}
The matrix is as follows
\par\noindent
\scalebox{0.70}{\renewcommand{\arraystretch}{2.5}%%
$\left(\begin{array}{rrr}
                                      \A & \B      &  \frac{1000  \pi y}{x^{2} {\Denominator}} \\
                                      \C & \D      & -\frac{1000  \pi}{x {\Denominator}} \\
\frac{1000  \pi y}{x^{2} {\Denominator}} & -\frac{1000  \pi}{x {\Denominator}} & 202
\end{array}\right)$}

where
\begin{align*}
        \SQ &= \SQExpanded  \\
    \FivePi &= \FivePiExpanded
\end{align*}
An alternate solution is:
\[ \renewcommand{\arraystretch}{2.0}
\left(\begin{array}{ccc}
                                       A &  B      &  \frac{1000  \pi y}{x^{2} {\Denominator}} \\
                                       C &  D      & -\frac{1000  \pi}{x {\Denominator}} \\
\frac{1000  \pi y}{x^{2} {\Denominator}} & -\frac{1000  \pi}{x {\Denominator}} & 202
\end{array}\right)
\]
where
\begin{align*}
    A &= \A \\
    B &= \B \\
    C &= \C \\
    D &= \D \\
    \shortintertext{and}
    \SQ &= \SQExpanded  \\
\FivePi &= \FivePiExpanded
\end{align*}
\end{document}
Related Question