# How can I solve the problem of a matrix overflowing into the margin?

When I write this matrix it goes beyond the page.

$$\begin{bmatrix} 1 & x_2x_6 + x_1x_3x_6 + x_1x_4x_5 + x_2x_3x_4x_5 +x_1x_3x_4x_5 + x_1x_5x_6 + x_2x_3x_5x_6 + x_1x_3x_5x_6 &x_1x_5x_7+x_2x_3x_5x_7+x_1x_3x_5x_7 \\ x_2x_6+ x_1x_3x_6 + x_4+x_4x_5x_6 + x_3x_4x_5x_6+ x_3x_5x_6 & 1 & x_8+x_4x_7+x_3x_5x_6x_7\\ x_1x_5x_7+x_3x_5x_7 & x_8+x_4x_7+x_5x_6x_7 & 1 \end{bmatrix}$$


How can I fix this?

• How do you expect to improve the formatting? Do you want to make the font smaller? What about not printing everything (and using alternative variables instead)? – Werner Dec 23 '16 at 6:06
• All depends on point size of document class (and class). Please provide a minimum working example. Besides that, if the presentation doesn't fit the margins, you may consider simplifying the presentation. – user10274 Dec 23 '16 at 6:26
• For the sake of the reader, a rewrite like the one Werner suggests is better it will also look cleaner. – daleif Dec 23 '16 at 7:58

Apart from fitting easily on a page, the following solution has the advantage of introducing names for the expressions that allow you to refer to the expressions later on.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$$\begin{bmatrix} 1 & a_{12} & a_{13} \\ a_{21}& 1 & a_{23} \\ a_{21}& a_{32} & 1 \end{bmatrix}$$
where
\begin{align*}
a_{12} &=x_2x_6 + x_1x_3x_6
+ x_1x_4x_5 + x_2x_3x_4x_5\\
+ x_2x_3x_5x_6
+ x_1x_3x_5x_6 \\
a_{13} &=x_1x_5x_7+x_2x_3x_5x_7
+ x_1x_3x_5x_7 \\
a_{21} &= x_2x_6+ x_1x_3x_6 + x_4
+ x_4x_5x_6 + x_3x_4x_5x_6
+ x_3x_5x_6 \\
a_{23} &= x_8+x_4x_7+x_3x_5x_6x_7\\
a_{31} &= x_1x_5x_7+x_3x_5x_7\\
a_{32} &= x_8+x_4x_7+x_5x_6x_7
\end{align*}
\end{document}

• If I may, I would rather denote the names with capital letters (A_{12}, &c.), as lowercase looks a little too much ‘generic element’ of a matrix. – Bernard Dec 23 '16 at 11:37

You can combine the aligned environment for long entry, \medmath from nccmath (about 80 % of \displaystyle) and playing with arraycolsep:

\documentclass{article}

\usepackage{mathtools, nccmath}
\usepackage[ showframe]{geometry}

\begin{document}

\setlength\arraycolsep{4pt} \medmath{\begin{bmatrix} 1 &\begin{aligned}x_2x_6 + x_1x_3x_6 + x_1x_4x_5 + x_2x_3x_4x_5 \\[-0.8ex]{} +x_1x_3x_4x_5+ x_1x_5x_6 + x_2x_3x_5x_6 + x_1x_3x_5x_6\end{aligned} &\begin{aligned}x_1x_5x_7+x_2x_3x_5x_7\\[-0.8ex]{}+x_1x_3x_5x_7 \end{aligned} \\[3ex] \begin{aligned}x_2x_6 + x_1x_3x_6 + x_4+x_4x_5x_6\\[-0.8ex] {} + x_3x_4x_5x_6+ x_3x_5x_6\end{aligned} & 1 & x_8+x_4x_7+x_3x_5x_6x_7\\[3ex] x_1x_5x_7+x_3x_5x_7 & x_8+x_4x_7+x_5x_6x_7 & 1 \end{bmatrix}}

\end{document}


• @gernot: Congratulation for detecting my freudian slip ;o) – Bernard Dec 23 '16 at 11:35

Not my favourite solution, but if you want to have the expressions verbatim in the matrix and don't want to reduce the font size within the matrix compared to the surrounding text, this is a way.

\documentclass{article}
\usepackage{amsmath}
\newcommand\wrapped[1]%
{\renewcommand\arraystretch{1}%
\begin{array}{@{}l@{}}#1\end{array}%
}
\begin{document}
$$\renewcommand\arraystretch{1.5} \begin{bmatrix} 1 & \wrapped {x_2x_6 + x_1x_3x_6\\ {} + x_1x_4x_5 + x_2x_3x_4x_5\\ {} +x_1x_3x_4x_5 + x_1x_5x_6\\ {} + x_2x_3x_5x_6\\ {} + x_1x_3x_5x_6 } & \wrapped {x_1x_5x_7+x_2x_3x_5x_7\\ {}+x_1x_3x_5x_7 } \\ \wrapped {x_2x_6+ x_1x_3x_6 + x_4\\ {}+x_4x_5x_6 + x_3x_4x_5x_6\\ {}+ x_3x_5x_6 } & 1 & x_8+x_4x_7+x_3x_5x_6x_7 \\ x_1x_5x_7+x_3x_5x_7 & x_8+x_4x_7+x_5x_6x_7 & 1 \end{bmatrix}$$
\end{document}