# align long equation

I know this type of questions have been asked previously but I haven't found something to solve this problem. I have the following long code :

\documentclass[12pt,a4paper,twoside]{report}
\usepackage{amsmath,amsthm,mathtools}
\begin{document}
\begin{align*}
f_{32}=&x^{32} +12198x^{31} +2314465x^{30} -12105775188x^{29} +5363823023298x^{28} +112255815323746\\
&8x^{27} -1190957568896565522x^{26} +209082600175304589984x^{25} +22989403151925296937\\
&003x^{24} -4914900851027091491992374x^{23} +187314971305181007593402091x^{22} +185143\\
&5573081259757991725916x^{21} -177112654221172660874138268072x^{20} +43926953348772\\
&854958076288336008x^{19} -8069925182155695353376402378426972x^{18} +68958542881736\\
&5840411341429649636128x^{17} -23740614034450231197314286171021009177x^{16} -155324\\
&9919261394918300632995703368436966x^{15} +25870940606770260632602122011252618896\\
&6887x^{14} -14511399786110161424357109505278330297341724x^{13} +1463537885226408987\\
&41319428949250465719024858x^{12} +32750278659384832647975670671061051222933180188\\
&x^{11} -2286974864798158343157856729712480557839267556386x^{10} +5932631975811948614\\
&5828221766889960355126201846912x^9 +6575146802218754701624346687966744930450753\\
&36579853x^8 -105521419234945522100651241454885243895292286754573898x^7 +40112381\\
&65200337244745440754326620606602486694989793197x^6 -886067081083651605769689130\\
&22740501751611649852083140204x^5 +127506445946767356976971370547537041919975453\\
&7492781650348x^4 -12161035278122430054818848726808537446643676687311798879424x^3\\
&+74518442436178190616718411536731834197503633925706662198320x^2 -2668848018838\\
&36189911937468492568061745225307284731520442304x +4266570219847726085064384156\\
&02232517825896105800369395838016
\end{align*}
\end{document}


The problem here is that I try to adjust the end of the equation by inserting \\ and & wherever it is appropriate to split the equations into multiple lines, unfortunately it looks messy and not aligned at all. I have insert an image with red circle identifying my problem.

• If you leave only one expression of the type big number x^n in a line, ale will be much more readable. The ragged expression is natural in he suggested case. – Przemysław Scherwentke Sep 16 '16 at 6:13
• @PrzemysławScherwentke. Good idea. Thank you for the suggestion. – shahrina ismail Sep 16 '16 at 7:40

In my opinion, in order to preserve some semblance of readability of this polynomial expression, you should have only one term per line for virtually all terms. For sure, don't introduce line breaks in the long integer terms.

\documentclass[12pt,a4paper,twoside]{report}
\usepackage{amsmath}
\allowdisplaybreaks % allow page breaks
\begin{document}
\begin{align*}
f_{32} &=x^{32} +12198x^{31} \\
\end{align*}
\end{document}


Addendum to address a comment left by the OP: If it's known that the equation has to (and will) fit on a single page, you may want to use an array environment instead of an align* environment. The former has an advantage in terms of more tighter line spacing, making it more likely that the full polynomial expression will indeed fit on a single page.

\documentclass[12pt,a4paper,twoside]{report}
\begin{document}
\appendix
\section*{Appendix: More information on \boldmath $f_{32}$}

The function $f_{32}$ is given by the following 32nd-degree polynomial in $x$:
$\begin{array}{@{}r@{}l@{}} f_{32} ={} &x^{32}\\ &{}+12198\,x^{31}\\ &{}+2314465\,x^{30}\\ &{}-12105775188\,x^{29}\\ &{}+5363823023298\,x^{28}\\ &{}+1122558153237468\,x^{27}\\ &{}-1190957568896565522\,x^{26}\\ &{}+209082600175304589984\,x^{25}\\ &{}+22989403151925296937003\,x^{24}\\ &{}-4914900851027091491992374\,x^{23}\\ &{}+187314971305181007593402091\,x^{22}\\ &{}+1851435573081259757991725916\,x^{21}\\ &{}-177112654221172660874138268072\,x^{20}\\ &{}+43926953348772854958076288336008\,x^{19}\\ &{}-8069925182155695353376402378426972\,x^{18}\\ &{}+689585428817365840411341429649636128\,x^{17}\\ &{}-23740614034450231197314286171021009177\,x^{16}\\ &{}-1553249919261394918300632995703368436966\,x^{15}\\ &{}+258709406067702606326021220112526188966887\,x^{14}\\ &{}-14511399786110161424357109505278330297341724\,x^{13}\\ &{}+146353788522640898741319428949250465719024858\,x^{12}\\ &{}+32750278659384832647975670671061051222933180188\,x^{11}\\ &{}-2286974864798158343157856729712480557839267556386\,x^{10}\\ &{}+59326319758119486145828221766889960355126201846912\,x^9\\ &{}+657514680221875470162434668796674493045075336579853\,x^8\\ &{}-105521419234945522100651241454885243895292286754573898\,x^7\\ &{}+4011238165200337244745440754326620606602486694989793197\,x^6\\ &{}-88606708108365160576968913022740501751611649852083140204\,x^5\\ &{}+1275064459467673569769713705475370419199754537492781650348\,x^4\\ &{}-12161035278122430054818848726808537446643676687311798879424\,x^3\\ &{}+74518442436178190616718411536731834197503633925706662198320\,x^2\\ &{}-266884801883836189911937468492568061745225307284731520442304\,x\\ &{}+426657021984772608506438415602232517825896105800369395838016 \end{array}$
\end{document}

• @shahrinaismail - Just out of idle curiosity: What purpose may be served by typesetting the 32 coefficients associated with x^{31}, x^{30}, ..., x, and x^0? Do you expect your readers to read these rather long numbers? Presumably, there's a formula that generates the coefficients; if so, might be readers be better served with being shown (just) the formula? – Mico Sep 16 '16 at 7:45
• Still, I'm curious about the misaligment. At first I thought it was beacause TeX wouldn't know where to break the number, though it does break them, but breaks in "different margins", what is the cause of this? – Guilherme Zanotelli Sep 16 '16 at 8:10
• @Mico. Yes there is a formula that generates this. In my thesis chapter i just wrote this expression as f_{32} and there's a footnote saying Refer Appendix. So this long equation goes to Appendix, not in the chapter itself. Another reason is that it is vital to observe which powers of x^n give odd or even coefficient which is why I have to include it i the Appendix. – shahrina ismail Sep 16 '16 at 8:29
• @GuilhermeZ.Santos I am not sure either. Would be nice if Tex knew where to break these long equations according to the page margin we set at the preamble. – shahrina ismail Sep 16 '16 at 8:33

@Mico's solution is more appropriate, but for the sake of founding the reason behind the misaligment I'm giving this answer. The problem was that your equation is completely off the margins, if you load \usepackage[showframe]{geometry} you will see this:

So, the real problem is the linebreaking. By using the align environment you must properly break lines. An alternative to this issue would be to use the breqn package which provides automatic linebreaking. See MWE below and compilation result.

\documentclass[12pt,a4paper,twoside]{report}
\usepackage{amsmath,amsthm,mathtools,breqn}
\usepackage[showframe]{geometry}
\begin{document}
\begin{dmath*}
f_{32} = x^{32} +12198x^{31} +2314465x^{30} -12105775188x^{29} +5363823023298x^    {28} +112255815323746
8x^{27} -1190957568896565522x^{26} +209082600175304589984x^{25}     +22989403151925296937
003x^{24} -4914900851027091491992374x^{23} +187314971305181007593402091x^{22}     +185143
5573081259757991725916x^{21} -177112654221172660874138268072x^{20}     +43926953348772
854958076288336008x^{19} -8069925182155695353376402378426972x^{18}     +68958542881736
5840411341429649636128x^{17} -23740614034450231197314286171021009177x^{16}     -155324
9919261394918300632995703368436966x^{15} +25870940606770260632602122011252618896
6887x^{14} -14511399786110161424357109505278330297341724x^{13}     +1463537885226408987
41319428949250465719024858x^{12}     +32750278659384832647975670671061051222933180188
x^{11} -2286974864798158343157856729712480557839267556386x^{10}     +5932631975811948614
5828221766889960355126201846912x^9 +6575146802218754701624346687966744930450753
36579853x^8 -105521419234945522100651241454885243895292286754573898x^7 +40112381
65200337244745440754326620606602486694989793197x^6 -886067081083651605769689130
22740501751611649852083140204x^5 +127506445946767356976971370547537041919975453
7492781650348x^4 -12161035278122430054818848726808537446643676687311798879424x^3
+74518442436178190616718411536731834197503633925706662198320x^2 -2668848018838
36189911937468492568061745225307284731520442304x +4266570219847726085064384156
02232517825896105800369395838016
\end{dmath*}
\end{document}


This result begin pretty similar to what Mico advised.

You can use a more natural input by defining a suitable environment; in addition this environment can take an optional argument for a font size command, in case at \normalsize the terms are too wide.

\documentclass[12pt,a4paper,twoside]{report}
\usepackage{amsmath,amsthm,mathtools}

\usepackage{lipsum} % just for the example

\makeatletter
\newenvironment{longpoly}[2][\normalsize]
{\par\nopagebreak\vspace{\abovedisplayskip}% end a par and emulate a display
\setlength{\parindent}{0pt}%
#1% a font size declaration
\raggedright
\sbox\z@{\quad$#2={}$}% measure the left-hand side
\setlength{\leftskip}{\wd\z@}%
\setplusandminus
$% start math mode \hspace*{-\wd\z@}% \quad#2=} {$\par\vspace{\belowdisplayskip}}
\mathchardef\std@plus=\mathcode+
\def\setplusandminus{%
\begingroup\lccode~=- \lowercase{\endgroup\def~}{\penalty-\@M\std@minus\nobreak}
\begingroup\lccode~=+ \lowercase{\endgroup\def~}{\penalty-\@M\std@plus\nobreak}
\mathcode+="8000
\mathcode-="8000
}
\makeatother

\begin{document}

\lipsum*[1-2]
\begin{longpoly}[\footnotesize]{f_{32}}
x^{32}
+12198x^{31}
+2314465x^{30}
-12105775188x^{29}
+5363823023298x^{28}
+1122558153237468x^{27}
-1190957568896565522x^{26}
+209082600175304589984x^{25}
+22989403151925296937003x^{24}
-4914900851027091491992374x^{23}
+187314971305181007593402091x^{22}
+1851435573081259757991725916x^{21}
-177112654221172660874138268072x^{20}
+43926953348772854958076288336008x^{19}
-8069925182155695353376402378426972x^{18}
+689585428817365840411341429649636128x^{17}
-23740614034450231197314286171021009177x^{16}
-1553249919261394918300632995703368436966x^{15}
+258709406067702606326021220112526188966887x^{14}
-14511399786110161424357109505278330297341724x^{13}
+146353788522640898741319428949250465719024858x^{12}
+32750278659384832647975670671061051222933180188x^{11}
-2286974864798158343157856729712480557839267556386x^{10}
+59326319758119486145828221766889960355126201846912x^9
+657514680221875470162434668796674493045075336579853x^8
-105521419234945522100651241454885243895292286754573898x^7
+4011238165200337244745440754326620606602486694989793197x^6
-88606708108365160576968913022740501751611649852083140204x^5
+1275064459467673569769713705475370419199754537492781650348x^4
-12161035278122430054818848726808537446643676687311798879424x^3
+74518442436178190616718411536731834197503633925706662198320x^2
-266884801883836189911937468492568061745225307284731520442304x
+426657021984772608506438415602232517825896105800369395838016
\end{longpoly}
\lipsum[4]

\end{document}
`