# fit a very wide matrix

I'm tring to fit a 4x4 matrix that has long formulas as elements to A4 width.

\documentclass[]{scrbook}
\usepackage{amsmath}

\begin{document}
\begin{math}
\begin{bmatrix}
cos(a1)*cos(a4) - sin(a4)*(cos(a2)*cos(a3)*sin(a1) - sin(a1)*sin(a2)*sin(a3)) & cos(a4)*(cos(a2)*cos(a3)*sin(a1) - sin(a1)*sin(a2)*sin(a3)) - cos(a1)*sin(a4) & cos(a2)*sin(a1)*sin(a3) + cos(a3)*sin(a1)*sin(a2) & - b*sin(a1) - d*(cos(a2)*cos(a3)*sin(a1) - sin(a1)*sin(a2)*sin(a3)) - e*(cos(a2)*sin(a1)*sin(a3) + cos(a3)*sin(a1)*sin(a2)) - c*cos(a2)*sin(a1) \\
sin(a4)*(cos(a1)*cos(a2)*cos(a3) - cos(a1)*sin(a2)*sin(a3)) + cos(a4)*sin(a1) & cos(a4)*(cos(a1)*cos(a2)*cos(a3) - cos(a1)*sin(a2)*sin(a3)) - sin(a1)*sin(a4) & cos(a1)*cos(a2)*sin(a3) - cos(a1)*cos(a3)*sin(a2) & b*cos(a1) + d*(cos(a1)*cos(a2)*cos(a3) - cos(a1)*sin(a2)*sin(a3)) + e*(cos(a1)*cos(a2)*sin(a3) + cos(a1)*cos(a3)*sin(a2)) + c*cos(a1)*cos(a2) \\
sin(a4)*(cos(a2)*sin(a3) + cos(a3)*sin(a2)) & cos(a4)*(cos(a2)*sin(a3) + cos(a3)*sin(a2)) & cos(a2)*cos(a3) - sin(a2)*sin(a3) & a + d*(cos(a2)*sin(a3) + cos(a3)*sin(a2)) + e*(sin(a2)*sin(a3) - cos(a2)*cos(a3)) + c*sin(a2) \\
0 & 0 & 0 & 1
\end{bmatrix}
\end{math}
\end{document}


Is there a way to wrap the text? Please be patient, I'm new to this.

## 3 Answers

This is how I will do it.

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage[margin=1in]{geometry}

\begin{document}
$\begin{bmatrix} A_{11} & A_{12} & A_{13} & A_{14} \\ B_{11} & B_{12} & B_{13} & B_{14} \\ C_{11} & C_{12} & C_{13} & C_{14} \\ 0 & 0 & 0 & 1 \end{bmatrix}$
where
\begin{align*}
A_{11} &= \cos(a1)*\cos(a4) - \sin(a4)*(\cos(a2)*\cos(a3)*\sin(a1) - \sin(a1)*\sin(a2)*\sin(a3)) \\
A_{12} &= \cos(a4)*(\cos(a2)*\cos(a3)*\sin(a1) - \sin(a1)*\sin(a2)*\sin(a3)) - \cos(a1)*\sin(a4) \\
A_{13} &= \cos(a2)*\sin(a1)*\sin(a3) + \cos(a3)*\sin(a1)*\sin(a2) \\
A_{14} &= {}-{} b*\sin(a1) - d*(\cos(a2)*\cos(a3)*\sin(a1) - \sin(a1)*\sin(a2)*\sin(a3)) \\
&\phantom{{}={}} - e*(\cos(a2)*\sin(a1)*\sin(a3)+ \cos(a3)*\sin(a1)*\sin(a2)) - c*\cos(a2)*\sin(a1) \\
B_{11} &= \sin(a4)*(\cos(a1)*\cos(a2)*\cos(a3) - \cos(a1)*\sin(a2)*\sin(a3)) + \cos(a4)*\sin(a1) \\
B_{12} &= \cos(a4)*(\cos(a1)*\cos(a2)*\cos(a3) - \cos(a1)*\sin(a2)*\sin(a3)) - \sin(a1)*\sin(a4) \\
B_{13} &= \cos(a1)*\cos(a2)*\sin(a3) - \cos(a1)*\cos(a3)*\sin(a2) \\
B_{14} &= b*\cos(a1) + d*(\cos(a1)*\cos(a2)*\cos(a3) - \cos(a1)*\sin(a2)*\sin(a3)) \\
&\phantom{=} + e*(\cos(a1)*\cos(a2)*\sin(a3) +
\cos(a1)*\cos(a3)*\sin(a2)) + c*\cos(a1)*\cos(a2) \\
C_{11} &= \sin(a4)*(\cos(a2)*\sin(a3) + \cos(a3)*\sin(a2)) \\
C_{12} &= \cos(a4)*(\cos(a2)*\sin(a3) + \cos(a3)*\sin(a2)) \\
C_{13} &= \cos(a2)*\cos(a3) - \sin(a2)*\sin(a3) \\
C_{14} &= a + d*(\cos(a2)*\sin(a3) + \cos(a3)*\sin(a2)) + e*(\sin(a2)*\sin(a3) \\
&\phantom{=}- \cos(a2)*\cos(a3)) + c*\sin(a2)
\end{align*}
\end{document}


Also, please use \sin, \cos instead of sin and cos.

• +1 I would use \cdot instead of * and would also recommend that the OP uses subscripts such as a_1, a_2, ... instead of a1, a2, .... Oct 22 '13 at 18:07
• I would use nothing instead of *. One can also factor out common factors. For example a \sin a_1 can be factored out of the first parenthesis pair. And what remains can be reduced to \cos(a_2 + a_3).
– Dan
Oct 22 '13 at 22:35
• @cmhughes That is a nice suggestion too. Thanks :-)
– user11232
Oct 22 '13 at 22:43
• @Dan: That would simplify things further. Thanks for the suggestion. :-)
– user11232
Oct 22 '13 at 22:43

Never use letters such as cos for multi-letter identifiers, the math italic font is designed for single letter variable names. Use \cos in this case,

I made some other adjustments but this is going to be unreadable whatever you do, consider defining some variables for the subterms so the main matrix display is more reasonable.

\documentclass[]{scrbook}
\usepackage{amsmath,array}

\begin{document}
\begin{math}
\left[\begin{array}{*4{>{\centering\arraybackslash$}p{3.5cm}<{$}}}
\cos a_1\cos a_4 - \sin a_4(\cos a_2\cos a_3\sin a_1 - \sin a_1\sin a_2\sin a_3) \\
\cos a_4(\cos a_2\cos a_3\sin a_1 - \sin a_1\sin a_2\sin a_3) - \cos a_1\sin a_4 & \cos a_2\sin a_1\sin a_3 + \cos a_3\sin a_1\sin a_2 & - b\sin a_1 - d(\cos a_2\cos a_3\sin a_1 - \sin a_1\sin a_2\sin a_3) - e(\cos a_2\sin a_1\sin a_3 + \cos a_3\sin a_1\sin a_2) - c\cos a_2\sin a_1 \\
\sin a_4(\cos a_1\cos a_2\cos a_3 - \cos a_1\sin a_2\sin a_3) + \cos a_4\sin a_1 & \cos a_4(\cos a_1\cos a_2\cos a_3 - \cos a_1\sin a_2\sin a_3) - \sin a_1\sin a_4 & \cos a_1\cos a_2\sin a_3 - \cos a_1\cos a_3\sin a_2 & b\cos a_1 + d(\cos a_1\cos a_2\cos a_3 - \cos a_1\sin a_2\sin a_3) + e(\cos a_1\cos a_2\sin a_3 + \cos a_1\cos a_3\sin a_2) + c\cos a_1\cos a_2 \\
\sin a_4(\cos a_2\sin a_3 + \cos a_3\sin a_2) & \cos a_4(\cos a_2\sin a_3 + \cos a_3\sin a_2) & \cos a_2\cos a_3 - \sin a_2\sin a_3 & a + d(\cos a_2\sin a_3 + \cos a_3\sin a_2) + e(\sin a_2\sin a_3 - \cos a_2\cos a_3) + c\sin a_2 \\
0 & 0 & 0 & 1
\end{array}\right]
\end{math}
\end{document}


use abbreviations and write it in matrix notation:

\documentclass{scrbook}
\usepackage{geometry}
\usepackage{mathtools}

\begin{document}
$\begin{array}{@{}l} c1=\cos(a1)\\ c2=\cos(a2)\\ c3=\cos(a3)\\ c4=\cos(a4)\\ s1=\sin(a1)\\ s2=\sin(a2)\\ s3=\sin(a3)\\ s4=\sin(a4)\\ c12=c1*c2\\ c13=c1*c3\\ c14=c1*c4\\ c23=c2*c3\\ c123=c1*c2*c3\\ s12=s1*s2\\ s13=s1*s3\\ s14=s1*s4\\ s23=s2*s3\\ s123=s1*s2*s3 \end{array}$

$\left[\begin{array}{@{}llll@{}} c14 - s4*(c23*s1 - s123) & c4*(c23*s1 - s123) - c1*s4 & c2*s13 + c3*s12 \\ \multicolumn{4}{r@{}}{ - b*s1 - d*(c23*s1 - s123) - e*(c2*s13 + c3*s12) - c*c2*s1} \\ s4*(c123 - c1*s23) + c4*s1 & c4*(c123 - c1*s23) - s14 & c12*s3 - c13*s2 \\ \multicolumn{4}{r@{}}{ b*c1 + d*(c123 - c1*s23) + e*(c12*s3 + c13*s2) + c*c12 }\\ s4*(c2*s3 + c3*s2) & c4*(c2*s3 + c3*s2) & c23 - s23 \\ \multicolumn{4}{r@{}}{ a + d*(c2*s3 + c3*s2) + e*(s23 - c23) + c*s2} \\ 0 & 0 & 0 & 1 \end{array}\right]$

\bigskip
or

\bigskip
$\left[\begin{array}{@{}lll@{}} c14 - s4*(c23*s1 - s123) & c4*(c23*s1 - s123) - c1*s4 & c2*s13 + c3*s12 \\ s4*(c123 - c1*s23) + c4*s1 & c4*(c123 - c1*s23) - s14 & c12*s3 - c13*s2 \\ s4*(c2*s3 + c3*s2) & c4*(c2*s3 + c3*s2) & c23 - s23 \\ 0 & 0 & 0 \end{array}\right] +$

$+ \left[\begin{array}{@{}l@{}} - b*s1 - d*(c23*s1 - s123) - e*(c2*s13 + c3*s12) - c*c2*s1 \\ b*c1 + d*(c123 - c1*s23) + e*(c12*s3 + c13*s2) + c*c12 \\ a + d*(c2*s3 + c3*s2) + e*(s23 - c23) + c*s2 \\ 1 \end{array}\right]$

\end{document}