# Formatting a large matrix across multiple lines

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:

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

• While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Apr 25 '14 at 1:42
• @PeterGrill I modified it to a MWE. Any ideas how to get the appropriate format? – CodeKingPlusPlus Apr 25 '14 at 1:48

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

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

## 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}


I don't think much good can come from squeezing this very large 3x3 matrix into a single line (or three lines). By the way, because matrix is symmetric, I think it would be nice to mention this fact explicitly.

I suggest you do something like the following, which gives the readers a chance to inspect the contents of each element of the matrix at their leisure. (If you don't like the use of square brackets, you're of course free to revert to round parentheses.)

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{mathtools,amsthm,amssymb}
\begin{document}
Consider the symmetric matrix
\begin{align*}
A &=
\begin{pmatrix}
a_{11} & a_{12} & a_{13}\\
a_{21} & a_{22} & a_{23}\\
a_{31} & a_{32} & a_{33}\\
\end{pmatrix}\\
\intertext{where}
a_{11} &=
-\frac{200 \, x^2 [\sqrt{x^2 + y^2} - 1]}{\left(x^2 + y^2\right)^{3/2}} +
\frac{200 \, x^2}{x^2 + y^2} +
\frac{200 \, [\sqrt{x^2 + y^2} - 1]}{\sqrt{x^2 + y^2}} \\
&\qquad{}+\frac{2000 \, \pi [5 \, \pi \arctan(y/x) - z] y}{x^3 [(y^2/x^2) + 1]} +
\frac{5000 \, \pi^2 y^2}{x^4 {[(y^2/x^2) + 1]}^2} \\
&\qquad{}- \frac{2000 \, \pi [5 \, \pi \arctan(y/x) - z] y^3}{x^5 \left[(y^2/x^2) + 1\right]^2}\1ex] a_{12} &= -\frac{200 \, x y [\sqrt{x^2 + y^2} - 1]}{\left(x^2 + y^2\right)^{3/2}} + \frac{200 \, x y}{x^2 + y^2} - \frac{1000 \, \pi [5 \, \pi \arctan(y/x) - z]}{x^2 [(y^2/x^2) + 1]} \\ &\qquad{}- \frac{5000 \, \pi^2 y}{x^3 \left[(y^2/x^2) + 1\right]^2} + \frac{2000 \, \pi [5 \, \pi \arctan(y/x) - z] y^2}{x^4 \left[(y^2/x^2) + 1\right]^2}\\[1ex] a_{13} &= \frac{1000 \, \pi y}{x^2 [(y^2/x^2) + 1]}\\ a_{21} &= a_{21}\\ a_{22} &= -\frac{200 \, y^2 [\sqrt{x^2 + y^2} - 1]}{\left(x^2 + y^2\right)^{3/2}} + \frac{200 \, y^2}{x^2 + y^2} + \frac{200 \,[\sqrt{x^2 + y^2} - 1]}{\sqrt{x^2 + y^2}} \\ &\qquad{}+ \frac{5000 \, \pi^2}{x^2 \left[(y^2/x^2) + 1\right]^2} - \frac{2000 \, \pi [5 \, \pi \arctan(y/x) - z] y}{x^3 \left[(y^2/x^2) + 1\right]^2}\\[1ex] a_{23} &= -\frac{1000 \, \pi}{x [(y^2/x^2) + 1]}\\ a_{31} &= a_{13}\\ a_{32} &= a_{23}\\ \shortintertext{and} a_{33} &= 202. \end{align*} \end{document}  I would suggest using (at least locally) a smaller fontsize (11pt will do), writing only a significant beginning of the longest terms (with dots) and giving the full terms as explanations underneath. The nccmath package is required to write medium sized fractions thanks to its medsize environment that reduces the size of math formulae by about 80%, a size intermediate between displaystyle and textstyle. I also use the mathrlap and mathllap from the mathtools package, within the flalign*environment to have (hopefully) a clean layout of the "explanations": \documentclass[11pt]{article} \usepackage[utf8]{inputenc} \usepackage[margin=1in, showframe]{geometry} \usepackage{amsthm,amssymb} \usepackage{mathtools} \usepackage{nccmath} \begin{document} \[ \begin{pmatrix} -\mfrac{200 \, x^{2} {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \mfrac{200 \, x^{2}}{x^{2} + y^{2}} + \cdots & -\mfrac{200 \, x y {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \mfrac{200 \, x y}{x^{2} + y^{2}} - \cdots & \mfrac{1000 \, \pi y}{x^{2} {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}} \\% -\mfrac{200 \, x y {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \mfrac{200 \, x y}{x^{2} + y^{2}} - \cdots & -\mfrac{200 \, y^{2} {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \mfrac{200 \, y^{2}}{x^{2} + y^{2}} + \cdots & -\mfrac{1000 \, \pi}{x {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}} \\% \mfrac{1000 \, \pi y}{x^{2} {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}} & -\mfrac{1000 \, \pi}{x {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}} & 202 \end{pmatrix}
where:
\fontsize{10}{10}\selectfont
\begin{flalign*}%\MoveEqLeft[40]
& \mathrlap{\begin{medsize} -\dfrac{200 \, x^{2} {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \dfrac{200 \, x^{2}}{x^{2} + y^{2}} + \cdots =-\dfrac{200 \, x^{2} {\left(\sqrt{x^{2}   + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}}  + \dfrac{200 \, x^{2}}{x^{2} + y^{2}}+ \dfrac{200 \, {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{\sqrt{x^{2} + y^{2}}}\end{medsize}} &  &
\\
& &  & \mathllap{\begin{medsize} + \dfrac{2000 \, \pi {\left(5 \, \pi \arctan\left(\dfrac{y}{x}\right) - z\right)} y}{x^{3} {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}} + \dfrac{5000 \, \pi^{2} y^{2}}{x^{4} {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}^{2}} - \dfrac{2000 \, \pi {\left(5 \, \pi \arctan\left(\dfrac{y}{x}\right) - z\right)} y^{3}}{x^{5} {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}^{2}}\end{medsize}}
\\[6pt]
\mathrlap{\begin{medsize}-\dfrac{200 \, x y {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\dfrac{3}{2}}} + \dfrac{200 \, x y}{x^{2} + y^{2}} - \cdots = -\dfrac{200 \, x y {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}}  + \dfrac{200 \, x y}{x^{2} + y^{2}} - \dfrac{1000 \, \pi {\left(5 \, \pi \arctan\left(\dfrac{y}{x}\right) - z\right)}}{x^{2} {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}} \end{medsize}}
\\
&  &  &  \mathllap{\begin{medsize}
- \dfrac{5000 \, \pi^{2} y}{x^{3} {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}^{2}} + \dfrac{2000 \, \pi {\left(5 \, \pi \arctan\left(\dfrac{y}{x}\right) - z\right)} y^{2}}{x^{4} {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}^{2}}
\end{medsize}}
\\[6pt]
& \mathrlap{\begin{medsize}      -\dfrac{200 \, y^{2} {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \dfrac{200 \, y^{2}}{x^{2} + y^{2}} + \cdots  =  -\dfrac{200 \, y^{2} {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{{\left(x^{2} + y^{2}\right)}^{\frac{3}{2}}} + \dfrac{200 \, y^{2}}{x^{2} + y^{2}} + \dfrac{200 \, {\left(\sqrt{x^{2} + y^{2}} - 1\right)}}{\sqrt{x^{2} + y^{2}}}
\end{medsize}}
\\
&  &  &  \mathllap{\begin{medsize}+ \dfrac{5000 \, \pi^{2}}{x^{2} {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}^{2}} - \dfrac{2000 \, \pi {\left(5 \, \pi \arctan\left(\dfrac{y}{x}\right) - z\right)} y}{x^{3} {\left(\dfrac{y^{2}}{x^{2}} + 1\right)}^{2}}
\end{medsize}}
\end{flalign*}
\normalsize

\end{document}


Result (the frame is here to show everything is within the margins):

• @CodeKingPLusPlus: I added a solution, with the beginning of long terlms in the matris, and details underneath. – Bernard Apr 25 '14 at 14:15