[Tex/LaTex] Polynomial Long Division Using Polynom

polynom

I have been toiling around polynom package to typeset polynomial long division. The polynom package produces wonderful long division with step by step process. But I just have two problems. Let me tell you what I need exactly

Here is the output that I produced with polynom for \polylongdiv{X^3+X^2-1}{X-1}

enter image description here

But instead of spaces for the missing x, I want to have 0x. Is it possible to do that? I checked the documentation and I couldn't find a way to do this.

You can read the documentation here: http://www.cs.brown.edu/system/software/latex/doc/polynom.pdf

I also realized that the polynomials aren't formatted inside the math environment. So is there anyway to format these equations inside math environment also? I tried putting $ signs around \polylongdiv{X^3+X^2-1}{X-1}, but that didn't change anything. Equations inside math environment produces something like this

enter image description here

I am very new to latex. So please help me.

Update: @polgab helped me solve the second question of producing math environment like output. I just need the solution for question 1 now which is I want to have 0x in the place of missing x instead of a space which is being produced by polynom.

Best Answer

Changing the internal macros that are responsible for omitting zero terms you get what you want (but you have to specify the zero term in the input):

\documentclass{article}
\usepackage{polynom}
\makeatletter
\def\pld@CF@loop#1+{%
    \ifx\relax#1\else
        \begingroup
          \pld@AccuSetX11%
          \def\pld@frac{{}{}}\let\pld@symbols\@empty\let\pld@vars\@empty
          \pld@false
          #1%
          \let\pld@temp\@empty
          \pld@AccuIfOne{}{\pld@AccuGet\pld@temp
                            \edef\pld@temp{\noexpand\pld@R\pld@temp}}%
           \pld@if \pld@Extend\pld@temp{\expandafter\pld@F\pld@frac}\fi
           \expandafter\pld@CF@loop@\pld@symbols\relax\@empty
           \expandafter\pld@CF@loop@\pld@vars\relax\@empty
           \ifx\@empty\pld@temp
               \def\pld@temp{\pld@R11}%
           \fi
          \global\let\@gtempa\pld@temp
        \endgroup
        \ifx\@empty\@gtempa\else
            \pld@ExtendPoly\pld@tempoly\@gtempa
        \fi
        \expandafter\pld@CF@loop
    \fi}
\def\pld@CMAddToTempoly{%
    \pld@AccuGet\pld@temp\edef\pld@temp{\noexpand\pld@R\pld@temp}%
    \pld@CondenseMonomials\pld@false\pld@symbols
    \ifx\pld@symbols\@empty \else
        \pld@ExtendPoly\pld@temp\pld@symbols
    \fi
    \ifx\pld@temp\@empty \else
        \pld@if
            \expandafter\pld@IfSum\expandafter{\pld@temp}%
                {\expandafter\def\expandafter\pld@temp\expandafter
                    {\expandafter\pld@F\expandafter{\pld@temp}{}}}%
                {}%
        \fi
        \pld@ExtendPoly\pld@tempoly\pld@temp
        \pld@Extend\pld@tempoly{\pld@monom}%
    \fi}
\makeatother
\begin{document}
\polylongdiv{X^3+X^2+0X-1}{X-1}
\end{document}

enter image description here

Related Question