Overfull hbox in matrix

I am typing up a technical report that contains many large matrices. I am looking for a way to print these matrices with an equation number next to them without getting an overfull \hbox error.

I was thinking it might be possible to temporarily alter the page margins for the matrix. I don't really want to use smaller text as the matrix then starts becoming a little hard to read. This is currently what my matrix looks like. Any suggestions would really be appreciated. Thanks.

My code in creating the above matrix is as follows:

\documentclass[12pt]{article}

\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\setcounter{MaxMatrixCols}{20}

\begin{document}

$$\textbf{k} = \begin{bmatrix} \frac{EA}{L} & 0 & 0 & 0 & 0 & 0 & \frac{-EA}{L} & 0 & 0 & 0 & 0 & 0 \\ & \frac{12EI_z}{L^3} & 0& 0& 0& \frac{6EI_z}{L^2} & 0& \frac{-12EI_z}{L^3} & 0 & 0& 0 &\frac{6EI_z}{L^2} \\ & & \frac{12EI_y}{L^3} & 0& \frac{-6EI_y}{L^2} & 0& 0& 0& \frac{-12EI_y}{L^3} & 0& \frac{-6EI_y}{L^2} & 0 \\ & & & \frac{GI_p}{L} & 0 & 0 & 0 & 0 & 0 & \frac{-GI_p}{L} & 0 & 0 \\ & & & & \frac{4EI_y}{L}& 0 & 0& 0 & \frac{6EI_y}{L^2}& 0 & \frac{2EI_y}{L} & 0 \\ & & & & & \frac{4EI_z}{L} & 0 & \frac{-6EI_z}{L^2}& 0 & 0 & 0 & \frac{2EI_z}{L} \\ & & & & & & \frac{EA}{L} & 0 & 0 & 0 & 0 & 0 \\ & & & & & & & \frac{12EI_z}{L^3} & 0 & 0 & 0 & \frac{-6EI_z}{L^2} \\ & & & & & & & & \frac{12EI_y}{L^3} & 0 & \frac{6EI_y}{L^2} & 0 \\ & & & & & & & & & \frac{GI_p}{L} & 0 & 0 \\ & & & & & & & & & & \frac{4EI_y}{L}& 0 \\ symm. & & & & & & & & & && \frac{4EI_z}{L} \end{bmatrix} \label{kmat}$$

\end{document}

• Please provide the community with some code to replicate your current problem. It'll make it easier to start getting solutions your way. That is, help us to help you. Your code should start with \documentclass and end with \end{document}. – Werner Mar 17 '15 at 14:24
• it looks like the columns could be a bit narrower, so you could decrease the inter-column space just a bit. and the "symm." at the bottom left could be inserted in the first column with a \mathclap (requires \mathtools) so that it doesn't affect the horizontal spacing. – barbara beeton Mar 17 '15 at 14:42
• Or reduce \arraycolsep. Or you could switch to landscape using pdflscape. – John Kormylo Mar 17 '15 at 14:45

I stopped symm extending the first column (don't use math italic for multi-letter words) and reduced the column padding a bit:

\documentclass[12pt]{article}

\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\setcounter{MaxMatrixCols}{20}

\begin{document}

$$\setlength\arraycolsep{4pt} \textbf{k} = \begin{bmatrix} \frac{EA}{L} & 0 & 0 & 0 & 0 & 0 & \frac{-EA}{L} & 0 & 0 & 0 & 0 & 0 \\ & \frac{12EI_z}{L^3} & 0& 0& 0& \frac{6EI_z}{L^2} & 0& \frac{-12EI_z}{L^3} & 0 & 0& 0 &\frac{6EI_z}{L^2} \\ & & \frac{12EI_y}{L^3} & 0& \frac{-6EI_y}{L^2} & 0& 0& 0& \frac{-12EI_y}{L^3} & 0& \frac{-6EI_y}{L^2} & 0 \\ & & & \frac{GI_p}{L} & 0 & 0 & 0 & 0 & 0 & \frac{-GI_p}{L} & 0 & 0 \\ & & & & \frac{4EI_y}{L}& 0 & 0& 0 & \frac{6EI_y}{L^2}& 0 & \frac{2EI_y}{L} & 0 \\ & & & & & \frac{4EI_z}{L} & 0 & \frac{-6EI_z}{L^2}& 0 & 0 & 0 & \frac{2EI_z}{L} \\ & & & & & & \frac{EA}{L} & 0 & 0 & 0 & 0 & 0 \\ & & & & & & & \frac{12EI_z}{L^3} & 0 & 0 & 0 & \frac{-6EI_z}{L^2} \\ & & & & & & & & \frac{12EI_y}{L^3} & 0 & \frac{6EI_y}{L^2} & 0 \\ & & & & & & & & & \frac{GI_p}{L} & 0 & 0 \\ & & & & & & & & & & \frac{4EI_y}{L}& 0 \\ \rlap{\textit{symm.}} & & & & & & & & & && \frac{4EI_z}{L} \end{bmatrix} \label{kmat}$$

\end{document}

• well, you were quicker than i was. the only differences in what i came up with were \setlength{\arraycolsep}{.5\arraycolsep} and using \tag(2.1) to show that it could accommodate a wider equation number. – barbara beeton Mar 17 '15 at 14:55

A TABstack can do it without overfull boxes. The intercolumn gap and interrow baseline skip are set, respectively, with

\setstacktabbedgap{1ex}
\setstackgap{L}{1.4\baselineskip}


EDITED to \rlap the "symm.", both to not have it affect the column size and to put it as text.

The MWE is thus:

\documentclass[12pt]{article}

\usepackage[margin=1in]{geometry}
\usepackage{amsmath,tabstackengine}
\setcounter{MaxMatrixCols}{20}

\begin{document}

$$\setstacktabbedgap{1ex} \setstackgap{L}{1.4\baselineskip} \textbf{k} = \bracketMatrixstack{ \frac{EA}{L} & 0 & 0 & 0 & 0 & 0 & \frac{-EA}{L} & 0 & 0 & 0 & 0 & 0 \\ & \frac{12EI_z}{L^3} & 0& 0& 0& \frac{6EI_z}{L^2} & 0& \frac{-12EI_z}{L^3} & 0 & 0& 0 &\frac{6EI_z}{L^2} \\ & & \frac{12EI_y}{L^3} & 0& \frac{-6EI_y}{L^2} & 0& 0& 0& \frac{-12EI_y}{L^3} & 0& \frac{-6EI_y}{L^2} & 0 \\ & & & \frac{GI_p}{L} & 0 & 0 & 0 & 0 & 0 & \frac{-GI_p}{L} & 0 & 0 \\ & & & & \frac{4EI_y}{L}& 0 & 0& 0 & \frac{6EI_y}{L^2}& 0 & \frac{2EI_y}{L} & 0 \\ & & & & & \frac{4EI_z}{L} & 0 & \frac{-6EI_z}{L^2}& 0 & 0 & 0 & \frac{2EI_z}{L} \\ & & & & & & \frac{EA}{L} & 0 & 0 & 0 & 0 & 0 \\ & & & & & & & \frac{12EI_z}{L^3} & 0 & 0 & 0 & \frac{-6EI_z}{L^2} \\ & & & & & & & & \frac{12EI_y}{L^3} & 0 & \frac{6EI_y}{L^2} & 0 \\ & & & & & & & & & \frac{GI_p}{L} & 0 & 0 \\ & & & & & & & & & & \frac{4EI_y}{L}& 0 \\ \rlap{symm.} & & & & & & & & & && \frac{4EI_z}{L} } \label{kmat}$$

\end{document}


• Thanks! I'll keep this method in mind for future reference! – Gareth Howard Mar 17 '15 at 14:56

And if you really need lots of space...

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{pdflscape}
\setcounter{MaxMatrixCols}{20}

\begin{document}
\begin{landscape}
\noindent\rule{\linewidth}{1pt}

$$\arraycolsep=1em \textbf{k} = \begin{bmatrix} \frac{EA}{L} & 0 & 0 & 0 & 0 & 0 & \frac{-EA}{L} & 0 & 0 & 0 & 0 & 0 \\ & \frac{12EI_z}{L^3} & 0& 0& 0& \frac{6EI_z}{L^2} & 0& \frac{-12EI_z}{L^3} & 0 & 0& 0 &\frac{6EI_z}{L^2} \\ & & \frac{12EI_y}{L^3} & 0& \frac{-6EI_y}{L^2} & 0& 0& 0& \frac{-12EI_y}{L^3} & 0& \frac{-6EI_y}{L^2} & 0 \\ & & & \frac{GI_p}{L} & 0 & 0 & 0 & 0 & 0 & \frac{-GI_p}{L} & 0 & 0 \\ & & & & \frac{4EI_y}{L}& 0 & 0& 0 & \frac{6EI_y}{L^2}& 0 & \frac{2EI_y}{L} & 0 \\ & & & & & \frac{4EI_z}{L} & 0 & \frac{-6EI_z}{L^2}& 0 & 0 & 0 & \frac{2EI_z}{L} \\ & & & & & & \frac{EA}{L} & 0 & 0 & 0 & 0 & 0 \\ & & & & & & & \frac{12EI_z}{L^3} & 0 & 0 & 0 & \frac{-6EI_z}{L^2} \\ & & & & & & & & \frac{12EI_y}{L^3} & 0 & \frac{6EI_y}{L^2} & 0 \\ & & & & & & & & & \frac{GI_p}{L} & 0 & 0 \\ & & & & & & & & & & \frac{4EI_y}{L}& 0 \\ symm. & & & & & & & & & && \frac{4EI_z}{L} \end{bmatrix} \label{kmat}$$

\end{landscape}
\end{document}


You CAN change the margin temporarily using a minipage and negative \hspace, but it would be wrong.

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\setcounter{MaxMatrixCols}{20}

\begin{document}
\noindent\rule{\textwidth}{1pt}
\medskip

\noindent\hspace{-.25in}\begin{minipage}{\dimexpr \textwidth+.5in}
\noindent\rule{\textwidth}{1pt}

$$\textbf{k} = \begin{bmatrix} \frac{EA}{L} & 0 & 0 & 0 & 0 & 0 & \frac{-EA}{L} & 0 & 0 & 0 & 0 & 0 \\ & \frac{12EI_z}{L^3} & 0& 0& 0& \frac{6EI_z}{L^2} & 0& \frac{-12EI_z}{L^3} & 0 & 0& 0 &\frac{6EI_z}{L^2} \\ & & \frac{12EI_y}{L^3} & 0& \frac{-6EI_y}{L^2} & 0& 0& 0& \frac{-12EI_y}{L^3} & 0& \frac{-6EI_y}{L^2} & 0 \\ & & & \frac{GI_p}{L} & 0 & 0 & 0 & 0 & 0 & \frac{-GI_p}{L} & 0 & 0 \\ & & & & \frac{4EI_y}{L}& 0 & 0& 0 & \frac{6EI_y}{L^2}& 0 & \frac{2EI_y}{L} & 0 \\ & & & & & \frac{4EI_z}{L} & 0 & \frac{-6EI_z}{L^2}& 0 & 0 & 0 & \frac{2EI_z}{L} \\ & & & & & & \frac{EA}{L} & 0 & 0 & 0 & 0 & 0 \\ & & & & & & & \frac{12EI_z}{L^3} & 0 & 0 & 0 & \frac{-6EI_z}{L^2} \\ & & & & & & & & \frac{12EI_y}{L^3} & 0 & \frac{6EI_y}{L^2} & 0 \\ & & & & & & & & & \frac{GI_p}{L} & 0 & 0 \\ & & & & & & & & & & \frac{4EI_y}{L}& 0 \\ symm. & & & & & & & & & && \frac{4EI_z}{L} \end{bmatrix} \label{kmat}$$
\end{minipage}\hspace{-.25in}

\end{document}