# How to align the quotient when using polynom

I am using the polynom package to illustrate long division with polynomials. Typically, when I do those problems by hand, I have the first term in the quotient aligned with the first term in the dividend. However, as my code below will show, the alignment is essentially what I need.

\documentclass{article}
\usepackage{polynom}

\begin{document}
\polylongdiv{2x^4+7x^3+8x^2+5x+4}{x^3+x^2+x+1}
\end{document}


Is there a way to align the 2 of the quotient with the 2 of the dividend?

• Well as you could see the package sorts the polynomials belong to the degree or exponent in decreasing order. In this case the alignment is right. Perhaps the package documentation gives you an option for sort the polynomials in the way you are asking for. – Aradnix Aug 20 '14 at 5:01
• I checked the manual and couldn't find any such option, unfortunately. – DJJerome Aug 20 '14 at 19:26
• but... Why you want to align the polynomials in that way? – Aradnix Aug 20 '14 at 19:34
• That is my personal preference and I wanted to know if it is possible. I am not unique in having that as my preference, searching polynomial division on the web yields many examples of the kind of alignment I am after. – DJJerome Aug 20 '14 at 21:04
• Let's see if I understand, do you want that the 2 (of 2x+5) is exactly above of 2 of 2x^4+7x^3+...? – skpblack Aug 20 '14 at 23:47

\documentclass{article}
\usepackage{polynom}
\begin{document}
\makeatletter
\let\oldpld@SplitQuotient\pld@SplitQuotient
\def\pld@SplitQuotient{\oldpld@SplitQuotient\def\pld@pattern{}}
\polylongdiv{X^9-X^8-X^5+X^4+X+1}{X-1}
\end{document}


After some experiments, one may notice that polynom leaves some holes while printing non-consecutive monomials.

The mechanism is that polynom uses \pld@pattern as a placeholder. Since the dividend is of degree 9, \pld@pattern is X^9+X^8+...+1 (sort of).

It then starts to match \pld@quotient and \pld@pattern. In this case, X^8 goes to X^8 and -X^4 goes to X^4. Same thing happens when polynom tries to print the dividend. That is why the X^8 in quotient is aligned with -X^8 in dividend.

To get over this, we need to replace the placeholder by a shorter one.

But then it would be not so smart if we have to assign the placeholder by hand. The good news is, replacing the placeholder by nothing forces polynom to (re)generate a shorter one. Now it should be what you want.

The remaining problem is where to replace it? Here I append a \def\pld@pattern{} after \pld@SplitQuotient just because polynom is about to print the quotient.

\documentclass{article}
\usepackage{polynom}
\usepackage[active,floats,tightpage]{preview}
\begin{document}
\makeatletter
\let\oldpld@SplitQuotient\pld@SplitQuotient
\begin{figure}
\polylongdiv{X^9-X^8-X^5+X^4+X+1}{X-1}
\end{figure}
\begin{figure}
\def\pld@SplitQuotient{\oldpld@SplitQuotient\def\pld@pattern{}}
\polylongdiv{X^9-X^8-X^5+X^4+X+1}{X-1}
\end{figure}
\begin{figure}
\def\pld@SplitQuotient{\oldpld@SplitQuotient
\def\pld@quotient{\pld@V{X}{9}+\pld@V{X}{8}+\pld@V{X}{7}+\pld@V{X}{6}+\pld@V{X}{5}+\pld@V{X}{4}+\pld@V{X}{3}+\pld@V{X}{2}+\pld@V{X}{1}+\pld@R 11}}
\polylongdiv{X^9-X^8-X^5+X^4+X+1}{X-1}
\end{figure}
\begin{figure}
\def\pld@SplitQuotient{\oldpld@SplitQuotient\def\pld@pattern{}
\def\pld@quotient{\pld@V{X}{8}+\pld@V{X}{7}+\pld@V{X}{6}+\pld@V{X}{5}+\pld@V{X}{4}+\pld@V{X}{3}+\pld@V{X}{2}+\pld@V{X}{1}+\pld@R 11}}
\polylongdiv{X^9-X^8-X^5+X^4+X+1}{X-1}
\end{figure}
\end{document}