# How to format a long polynomial

I have a long polynomial:

\documentclass{article}
\usepackage{mathtools}
\begin{document}
$f(z)=\frac{1}{382112640}(-306772802511648469920\eta^4z^4+762453974480763801600\eta^5z^4-1678626210368271790080\eta^5z^3-28510918043555533736160\eta^4z^3+11443138641451067779872\eta^3z^3-52164076923190540413504\eta^2z^2-78145258181161076156160\eta^5z^2-211306163712129371808450\eta^4z^2+228927087397104405937944\eta^3z^2+999881065017543109136462\eta^3z-317254092617698017425280\eta^5z-443761561344388063474665\eta^4z+82327155732241730770824\eta z-514623285385260545505123\eta^2z-1010535343560043404912120\eta^2-357788302700438191196160\eta^5-43808044579418934376632-214023244873618345872240\eta^4+11818373349781028\\ 079\eta^3+347370177721463765064153\eta)/((417420z^2-4169121z-15571312)(1546z+3537)(3092z+17001))$
\end{document}


How do I format such a long polynomial correctly?

• For anyone reaching this question in the future, I would strongly recommend writing a simple summation formula with coefficients $a_{i,j}$ and then adding a table to show the values. – Mefitico Apr 5 '19 at 11:59
• @Mefitico It is a nice option! Why don't you post an answer? – user156344 Apr 5 '19 at 12:12
• @JouleV: Because it wouldn't answer the question. Ever heard of the patient who went to the doctor and said: "It hurts when I do this", to which the doctor responded: "Then don't do this!" – Mefitico Apr 5 '19 at 12:19
• @Mefitico No, it is still an appropriate expression of the equation, in my opinion. You can see that my answer and egreg's answer use indirect expressions, and you are talking about an indirect expression. – user156344 Apr 5 '19 at 12:23

I would use something like this

\documentclass{article}
\usepackage{mathtools}
\begin{document}
Blah blah
$f(z)=\frac{1}{382112640}\cdot\frac{A}{B}$
where
\begin{align*}
A=&\,-306772802511648469920\eta^4z^4+762453974480763801600\eta^5z^4\\
&\,-1678626210368271790080\eta^5z^3-28510918043555533736160\eta^4z^3\\
&\,+11443138641451067779872\eta^3z^3-52164076923190540413504\eta^2z^2\\
&\,-78145258181161076156160\eta^5z^2-211306163712129371808450\eta^4z^2\\
&\,+228927087397104405937944\eta^3z^2+999881065017543109136462\eta^3z\\
&\,-317254092617698017425280\eta^5z-443761561344388063474665\eta^4z\\
&\,+82327155732241730770824\eta z-514623285385260545505123\eta^2z\\
&\,-1010535343560043404912120\eta^2-357788302700438191196160\eta^5\\
&\,-43808044579418934376632-214023244873618345872240\eta^4\\
&\,+11818373349781028079\eta^3+347370177721463765064153\eta
\end{align*}
and
$B=(417420z^2-4169121z-15571312)(1546z+3537)(3092z+17001)$
\end{document} • you could format this even more compactly by using matrix multiplication to express A – Tasos Papastylianou Apr 5 '19 at 15:44

I suggest something line the following, so the wide terms are reduced.

\documentclass{article}
\usepackage{mathtools}

\begin{document}

\begin{gather*}
\begin{align*}
g(\eta,z)&=
\parbox[t]{0.85\displaywidth}{\raggedright
$-306772802511648469920\eta^4z^4+ 762453974480763801600\eta^5z^4- 1678626210368271790080\eta^5z^3- 28510918043555533736160\eta^4z^3+ 11443138641451067779872\eta^3z^3- 52164076923190540413504\eta^2z^2- 78145258181161076156160\eta^5z^2- 211306163712129371808450\eta^4z^2+ 228927087397104405937944\eta^3z^2+ 999881065017543109136462\eta^3z- 317254092617698017425280\eta^5z- 443761561344388063474665\eta^4z+ 82327155732241730770824\eta z- 514623285385260545505123\eta^2z- 1010535343560043404912120\eta^2- 357788302700438191196160\eta^5- 43808044579418934376632- 214023244873618345872240\eta^4+ 11818373349781028079\eta^3+ 347370177721463765064153\eta$
}
\2ex] h(z)&=(417420z^2-4169121z-15571312)(1546z+3537)(3092z+17001) \end{align*} \\[2ex] f(z)=\frac{1}{382112640}\frac{g(\eta,z)}{h(z)} \end{gather*} \end{document} • your answer is OK, but some terms are out of pages margins. – Nick Apr 5 '19 at 8:01 • @Nick Without knowing the line width you're using it's difficult to say more. – egreg Apr 5 '19 at 8:19 • @Nick egreg's answer uses default margin of article, which is already really big. But it doesn't fit your margin? – user156344 Apr 5 '19 at 8:29 • I have moved the signs "-, +" from lines end and put them under sign "=". – Nick Apr 5 '19 at 8:36 Given the nature of the operations, you can probably express this in a tidy manner using matrix multiplication notation, eg: where: Code:  f(z)=\frac{1}{382,112,640} \; \frac{g(\eta, z)}{u(z) \, v(z) \, w(z) }  where  \begin{array}{ll} g(\eta, z) = \begin{bmatrix} \begin{array}{r @{\hspace{0em}} r} - & 306,772,802,511,648,469,920 \\ & 762,453,974,480,763,801,600 \\ - & 1,678,626,210,368,271,790,080 \\ - & 28,510,918,043,555,533,736,160 \\ & 11,443,138,641,451,067,779,872 \\ - & 52,164,076,923,190,540,413,504 \\ - & 78,145,258,181,161,076,156,160 \\ - & 211,306,163,712,129,371,808,450 \\ & 228,927,087,397,104,405,937,944 \\ & 999,881,065,017,543,109,136,462 \\ - & 317,254,092,617,698,017,425,280 \\ - & 443,761,561,344,388,063,474,665 \\ & 82,327,155,732,241,730,770,824 \\ - & 514,623,285,385,260,545,505,123 \\ - & 1,010,535,343,560,043,404,912,120 \\ - & 357,788,302,700,438,191,196,160 \\ - & 43,808,044,579,418,934,376,632 \\ - & 214,023,244,873,618,345,872,240 \\ & 11,818,373,349,781,028,079 \\ & 347,370,177,721,463,765,064,153 \end{array} \end{bmatrix}^T \begin{bmatrix} \begin{array}{l} \eta^4z^4 \\ \eta^5z^4 \\ \eta^5z^3 \\ \eta^4z^3 \\ \eta^3z^3 \\ \eta^2z^2 \\ \eta^5z^2 \\ \eta^4z^2 \\ \eta^3z^2 \\ \eta^3z \\ \eta^5z \\ \eta^4z \\ \eta z \\ \eta^2z \\ \eta^2 \\ \eta^5 \\ 1 \\ \eta^4 \\ \eta^3 \\ \eta \end{array} \end{bmatrix} & \begin{array}{l} u(z) = \begin{bmatrix} \begin{array}{r @{\hspace{0em}} r} & 417,420 \\ - & 4,169,121 \\ - & 15,571,312 \end{array}\end{bmatrix}^T \begin{bmatrix} \begin{array}{l} z^2 \\ z \\ 1 \end{array}\end{bmatrix}\\[3em] v(z) = \begin{bmatrix} \begin{array}{r @{\hspace{0em}} r} & 1,546 \\ & 3,537 \end{array}\end{bmatrix}^T \begin{bmatrix}\begin{array}{l} z \\ 1 \end{array}\end{bmatrix}\\[3em] w(z) = \begin{bmatrix}\begin{array}{r @{\hspace{0em}} r} & 3,092 \\ & 17,001 \end{array}\end{bmatrix}^T \begin{bmatrix}\begin{array}{l} z \\ 1 \end{array}\end{bmatrix} \\[3em] \end{array} \end{array}   PS. Having said that, given the nature of the numbers involved, I would also agree with Mefitico's point of view in the comments, i.e. it's best to create a variable with indices and express via a cleaner expression, and then refer to a table mapping those indices to the actual numbers involved. • The round brackets in the definitions of u, v and w seem superfluous. – jochen Apr 6 '19 at 11:00 • From the point of view of someone wishing or needing to use such a polynomial, it would be helpful to insert breaks in all the long numbers to ease readability. – JeremyC Apr 7 '19 at 3:49 • @jochen thanks, updated – Tasos Papastylianou Apr 7 '19 at 15:51 • @JeremyC thanks, updated. I went for comma delimiters rather than breaks, as that might have been misleading in the context of matrix notation. – Tasos Papastylianou Apr 7 '19 at 15:51 or \documentclass{article} %\usepackage{amsmath}% Loaded by mathtools \usepackage{mathtools, nccmath} \begin{document} \begin{multline*}\medmath f(z)=\frac{1}{382112640} \frac{\left[ \begin{multlined} -306772802511648469920\eta^4z^4+762453974480763801600\eta^5z^4-\\ 1678626210368271790080\eta^5z^3-28510918043555533736160\eta^4z^3+\\ 11443138641451067779872\eta^3z^3-52164076923190540413504\eta^2z^2-\\ 78145258181161076156160\eta^5z^2-211306163712129371808450\eta^4z^2+\\ 228927087397104405937944\eta^3z^2+999881065017543109136462\eta^3z-\\ 317254092617698017425280\eta^5z-443761561344388063474665\eta^4z+\\ 82327155732241730770824\eta z - 514623285385260545505123\eta^2z-\\ 1010535343560043404912120\eta^2-357788302700438191196160\eta^5-\\ 43808044579418934376632-214023244873618345872240\eta^4+\\ 11818373349781028079\eta^3+347370177721463765064153\eta \end{multlined}\right]} {(417420z^2-4169121z-15571312)(1546z+3537)(3092z+17001)} \end{multline*}  I recommend aligning the variables and adding some form of thousand-separators, both will enhance the readability. What I also recommend (but didn't do here) is sorting by the powers of the first and then the second variable. This is a modification of JuleV's answer. \documentclass{article} %\usepackage{amsmath}% Loaded by mathtools \usepackage{mathtools} \begin{document} Blah blah \[f(z)=\frac{1}{382112640}\cdot\frac{A}{B}
where
$\arraycolsep=0.5pt \begin{array}{rrllrll} A=&\, -306\,772\,802\,511\,648\,469\,920 &\eta^4 &z^4 & +762\,453\,974\,480\,763\,801\,600 &\eta^5 &z^4\\ &\, -1\,678\,626\,210\,368\,271\,790\,080 &\eta^5 &z^3 & -2\,8510\,918\,043\,555\,533\,736\,160 &\eta^4 &z^3\\ &\, +11\,443\,138\,641\,451\,067\,779\,872 &\eta^3 &z^3 & -5\,2164\,076\,923\,190\,540\,413\,504 &\eta^2 &z^2\\ &\, -78\,145\,258\,181\,161\,076\,156\,160 &\eta^5 &z^2 & -21\,1306\,163\,712\,129\,371\,808\,450 &\eta^4 &z^2\\ &\, +228\,927\,087\,397\,104\,405\,937\,944 &\eta^3 &z^2 & +99\,9881\,065\,017\,543\,109\,136\,462 &\eta^3 &z\\ &\, -317\,254\,092\,617\,698\,017\,425\,280 &\eta^5 &z & -44\,3761\,561\,344\,388\,063\,474\,665 &\eta^4 &z\\ &\, +82\,327\,155\,732\,241\,730\,770\,824 &\eta &z & -51\,4623\,285\,385\,260\,545\,505\,123 &\eta^2 &z\\ &\,-1\,010\,535\,343\,560\,043\,404\,912\,120 &\eta^2 & & -35\,7788\,302\,700\,438\,191\,196\,160 &\eta^5 &\\ &\, -43\,808\,044\,579\,418\,934\,376\,632 & & & -21\,4023\,244\,873\,618\,345\,872\,240 &\eta^4 &\\ &\, +11\,818\,373\,349\,781\,028\,079 &\eta^3 & & +34\,7370\,177\,721\,463\,765\,064\,153 &\eta & \end{array}$
and
$B=(417\,420z^2-4\,169\,121z-15\,571\,312)(1\,546z+3\,537)(3\,092z+17\,001)$
\end{document}


I'm sure there are also some custom packages that can do this for you but this is just using the packages you provided: I would usually use the package breqn. That automatically line-breaks equations, and has a lot of very nice features, but uses low-level having into the maths primitives, which means it tends to make a mess of other packages what do the same thing (for example, you can't use both breqn and sansmath in the same document)

\begin{dmath*}
f(z)=\frac{1}{382112640}\times-306772802511648469920\eta^4z^4+\left(762453974480763801600\eta^5z^4-1678626210368271790080\eta^5z^3-28510918043555533736160\eta^4z^3+11443138641451067779872\eta^3z^3-52164076923190540413504\eta^2z^2-78145258181161076156160\eta^5z^2-211306163712129371808450\eta^4z^2+228927087397104405937944\eta^3z^2+999881065017543109136462\eta^3z-317254092617698017425280\eta^5z-443761561344388063474665\eta^4z+82327155732241730770824\eta z-514623285385260545505123\eta^2z-1010535343560043404912120\eta^2-357788302700438191196160\eta^5-43808044579418934376632-214023244873618345872240\eta^4+11818373349781028079\eta^3+347370177721463765064153\eta\right)\times\left(\left(417420z^2-4169121z-15571312\right)\left(1546z+3537\right)\left(3092z+17001\right)\right)^{-1}
\end{dmath*}


IMO the right-alignment is ugly but apparently that's the AMS standard - without the brackets it left aligns all those lines like the alginat version.

Following the original disposition of the function, but using alignat, parenthesis, and fractions to emphasize its different terms.

\documentclass{article}

\usepackage{mathtools}

\begin{document}

\begin{alignat*}{2}
& f(z) && = \frac{1}{382112640} \times \left( \vphantom{\frac{1}{382112640}} -306772802511648469920 \eta^4 z^4 + 762453974480763801600 \eta^5 z^4 \right. \\[1.5ex]
& && -1678626210368271790080 \eta^5 z^3 -28510918043555533736160 \eta^4 z^3 \\[1.5ex]
& && +11443138641451067779872 \eta^3 z^3 -52164076923190540413504 \eta^2 z^2 \\[1.5ex]
& && -78145258181161076156160 \eta^5 z^2 -211306163712129371808450 \eta^4 z^2 \\[1.5ex]
& && +228927087397104405937944 \eta^3 z^2 +999881065017543109136462 \eta^3 z \\[1.5ex]
& && -317254092617698017425280 \eta^5 z -443761561344388063474665 \eta^4 z \\[1.5ex]
& && +82327155732241730770824 \eta z -514623285385260545505123 \eta^2 z \\[1.5ex]
& && -1010535343560043404912120 \eta^2 -357788302700438191196160 \eta^5 \\[1.5ex]
& && -43808044579418934376632 -214023244873618345872240 \eta^4 \\[1.5ex]
& && +11818373349781028079 \eta^3 +347370177721463765064153\eta \left. \vphantom{\frac{1}{382112640}} \right) \\[1.5ex]
& && \times \frac{1}{(417420z^2-4169121z-15571312)(1546z+3537)(3092z+17001)}
\end{alignat*}

\end{document} • Do you think this fits the page margin? – user156344 Apr 5 '19 at 9:14
• It fitted for me. An alternative is to add \\ to the last line to bring the last multiplication and fraction to an additional line. – Andre Apr 5 '19 at 9:22
• In the original question, the term 417420z^2-4169121z-15571312 is in denominator, not in the numerator as you place it. – quark67 Apr 7 '19 at 0:32
• OK. I will revise this. – Andre Apr 7 '19 at 8:37
• Revised the members of the last fraction. Also added a line for the last term (which also ensures that the display will fit the margins). – Andre Apr 7 '19 at 10:12