How to show the full remainder in polynomial long division?
The following code should work; I've included a few test cases. To keep the code shorter, I didn't include your changes to the style of the output (red color, and bar between dividend and divisor instead of parenthesis). My changes and additions to polynom
are marked with !!!
.
\documentclass{article}
\usepackage{polynom,array}
\makeatletter
\def\pld@DivPoly@l{%
\ifx\pld@remainder\@empty\else
\pld@IfNeedsDivision\pld@remainder\pld@divisor
{\pld@ExtendPoly\pld@quotient\pld@factor
\pld@NMultiplyPoly\pld@sub\pld@divisor\pld@factor
\pld@SubtractPoly\pld@remainder\pld@sub
\expandafter\pld@DivPoly@l}%
{\expandafter\pld@insert@remainder % !!!
\pld@last@remainder+\relax\relax} % !!!
\fi}
\def\pld@insert@remainder#1+#2\relax{% % !!!
\ifx\relax#1\relax\else\pld@InsertItems\@empty\@empty{#1}\fi % !!!
\ifx\relax#2\relax\else\pld@insert@remainder#2\relax\fi} % !!!
\def\pld@SubtractPoly@l#1+#2\@empty#3+#4\@empty{%
\ifx\relax#1\relax
\let\pld@last@remainder\@empty % !!!
\ifx\relax#3\relax \let\pld@next\@empty \else
\pld@AddToPoly\pld@tempoly{#3}%
\pld@if \pld@InsertItems{#3}{#3}{}\fi
\def\pld@next{\pld@SubtractPoly@l\relax+\@empty#4\@empty}%
\fi
\else
\ifx\relax#3\relax
\pld@SubtractPoly@r#1+#2\@empty
\let\pld@next\@empty
\else
\pld@IfMonomE{#1}{#3}%
{\def\pld@temp{#1+#3}%
\pld@CondenseMonomials\pld@true\pld@temp
\ifx\pld@temp\@empty\else
\pld@ExtendPoly\pld@tempoly\pld@temp
\fi
\pld@if \expandafter\pld@InsertItems\expandafter
{\pld@temp}{#3}{#1}\fi
\def\pld@next{\pld@SubtractPoly@l#2\@empty#4\@empty}}%
{\pld@IfMonomL{#1}{#3}%
{\pld@AddToPoly\pld@tempoly{#3}%
\pld@if \pld@InsertItems{#3}{#3}{}\fi
\def\pld@next{\pld@SubtractPoly@l#1+#2\@empty#4\@empty}}%
{\pld@AddToPoly\pld@tempoly{#1}%
\pld@if \pld@InsertItems{#1}{}{#1}\fi
\def\pld@next{\pld@SubtractPoly@l#2\@empty#3+#4\@empty}}%
}%
\fi \fi
\pld@next}
\def\pld@SubtractPoly@r#1+\relax+\@empty{%
\pld@AddToPoly\pld@tempoly{#1}%
\def\pld@last@remainder{#1}} % !!!
\makeatother
\def\strut{\rule[-6pt]{0pt}{12pt}}
\begin{document}
\polylongdiv{x^5-1}{x-1}
\polylongdiv{x^5-x^2}{x^2-1}
\polylongdiv{\frac{3}{7}x^9+x^2-\frac{1}{4}}{\frac{9}{5}x^4-1}
\polylongdiv{x^9+x^2-1}{x^4-x}
\polylongdiv{x^{15}+1}{x^5+x^3+x+1}
\polylongdiv{x^4-1}{x^2-1}
\end{document}
Well the 1/4 is not the remainder, it is the last part of the first argument. The line you are trying to change is the line which shows both polynomials. You can see this if you use a bit simpler arguments:
\documentclass{article}
\usepackage{polynom,array}
\usepackage[table]{xcolor}
\arrayrulecolor{red}
\arrayrulewidth=0.8pt
\def\strut{\rule[-6pt]{0pt}{12pt}}
\begin{document}
\polylongdiv[style=A]{x^3-1}{x^2+1}
\bigskip
\polylongdiv[style=B]{x^3-1}{x^2+1}
\bigskip
\polylongdiv[style=C]{x^3-1}{x^2+1}
\end{document}
Edit: The good news are that the remainder is accessible:
\documentclass{article}
\usepackage{polynom}
\usepackage[table]{xcolor}
\arrayrulecolor{red}
\arrayrulewidth=0.8pt
\def\strut{\rule[-6pt]{0pt}{12pt}}
\makeatletter
\renewcommand*\polylongdiv[1][]{%
\begingroup
\let\pld@stage\maxdimen \polyset{#1}%
\pld@GetPoly{\pld@polya\pld@polyb}%
{\pld@LongDividePoly\pld@polya\pld@polyb
\pld@PrintLongDiv\\
$\expandafter\pld@PrintRemain\expandafter{\pld@remainder}$%new
\endgroup \ignorespaces}}
\begin{document}
\polylongdiv[style=A,stage=5]{x^3-1}{x^2+1}
\bigskip
\polylongdiv[style=A]{\frac{3}{7}x^9+x^2-\frac{1}{4}}{\frac{9}{5}x^4-1}
\end{document}
But I don't have the time to find out how to insert it correctly in the array.