I would like to give me an example of how to use the `polynom`

package. More specifically, I want to write down on my latex editor the following polynomial division (source):

Thanks in advance.

Skip to content
# [Tex/LaTex] Polynomial division code

#### Related Solutions

## Basic Example:

## Notes:

## Extended Example

## Notes

## Code: Basic Example

## Code: Extended Example

polynom

I would like to give me an example of how to use the `polynom`

package. More specifically, I want to write down on my latex editor the following polynomial division (source):

Thanks in advance.

Here is an example of one way to reproduce the Division Algorithm from Page 4 of Grobner Bases and their Applications by Kaitlyn Moran using an `array`

.

`multirow`

was used to center the`\sqrt{xy^2 + 1}`

term in between the two rows.`\multicolumn{1}{c}{<text>}`

was used to center content within a cell

For the more complicated case (from Page 5 of the above referenced paper), where there are multiple align points, I would recommend using an `\hphantom{}`

to leave the appropriate amount of horizontal spacing. The simplest way to do that is to start at the bottom and type in the polynomial using the widest terms at each point. This is shown in red below:

From this widest polynomial, work upwards and place the terms you don't want printed in an `\hphantom{}`

to get the appropriate spacing. In the code below I carefully aligned the code so that the `\hpantom{}`

is easier to visualize.

The other complication with this is that the horizontal rules do not extend across the entire width of the column, so a simple `\cline{2-2}`

won't work in this case. Luckily, you can use `\cmidrule`

from the `booktabs`

package which accepts an optional parameter that allows for trimming on the left and right of the `\cmidrule`

. Since the syntax of this is not quite standard I wrapped that in the `\CMidRule{<left trim>}{<right trim>}{<columns>}`

to which you can specify how much trimming to do on each side. So `\CMidRule{0.0ex}{0.0ex}{<columns>}`

is equivalent to `cmidrule{<columns>}`

.

- The portion in red is only used to illustrate the alignment points and should be deleted.
- For more details about the
`\phantom`

macro, see Indent as much as the width of a word for a basic example, or How to align across ordinary text; as in breaking matrices, sets of equations, tables for a more elaborate usage. - One thing to be careful about is that if a trailing binary operator (
`+`

, or`-`

) is part of the`\hphantom{}`

, then an additional`{}`

needs to be inserted to ensure that it is treated as a binary operator. See Phantom width of binary operator. - The trim lengths specified in the
`\CMidRule`

can be computed based on the length of the various portions of the code using the`calc`

package. However, in this case, using actual numbers to tweak the trims was more flexible and resulted in code that was easier to read. - It turns out that wrapping
`cmidrule`

in a macro is not straightforward. The earlier solution presented here seemed to work only because there is a single use of this macro per line, in this specific case. When an attempt to use this macro more than once per line, problems arose and discussed at Wrapping \cmidrule in a macro.

```
\documentclass{article}
\usepackage{multirow}
\newcommand{\PhantC}{\phantom{\colon}}%
\newcommand{\CenterInCol}[1]{\multicolumn{1}{c}{#1}}%
\begin{document}
\[
\begin{array}{rr}
a_1\colon & \CenterInCol{y}\\
a_2\colon & \CenterInCol{-1}\\
xy + 1\PhantC & \multirow{2}*{$\sqrt{xy^2 + 1}$}\\
y + 1\PhantC &\\
& xy^2 + y\\\cline{2-2}
& -y + 1 \\
& -y - 1 \\\cline{2-2}
& 2
\end{array}
\]
\end{document}
```

```
\documentclass{article}
\usepackage{xcolor}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{xparse}
\usepackage{calc}
\newcommand{\PhantC}{\phantom{\colon}}%
\newcommand{\PhantSQ}{\phantom{\sqrt{\hspace{0.3ex}}}}%
% https://tex.stackexchange.com/questions/63355/wrapping-cmidrule-in-a-macro
\ExplSyntaxOn
\makeatletter
\newcommand{\CMidRule}{\noalign\bgroup\@CMidRule{}}
\NewDocumentCommand{\@CMidRule}{
m % Material to reinsert before cmidrule.
O{0.0ex} % #1 = left adjust
O{0.0ex} % #1 = right adjust
m % #3 = columns to span
}{
\peek_meaning_remove_ignore_spaces:NTF \CMidRule
{ \@CMidRule { #1 \cmidrule[\cmidrulewidth](l{#2}r{#3}){#4} } }
{ \egroup #1 \cmidrule[\cmidrulewidth](l{#2}r{#3}){#4} }
}
\makeatother
\ExplSyntaxOff
\begin{document}
\[
\begin{array}{rll}
a_1\colon & \multicolumn{1}{l}{x+y}\\
a_2\colon & \multicolumn{1}{l}{1} & r\\
xy + 1\PhantC & \multirow{2}*{$\sqrt{x^2y + xy^2+y^2}$} \\\cline{3-3}
y^2 + 1\PhantC &\\
%
&\PhantSQ x^2y - x \\\CMidRule[3.0ex][9.0ex]{2-2}
&\PhantSQ \hphantom{x^2y +{}} xy^2 + x + y^2 \\
&\PhantSQ \hphantom{x^2y +{}} xy^2 - y \\\CMidRule[9.0ex][5.0ex]{2-2}
&\PhantSQ \hphantom{x^2y + xy^2 +{}} x + y^2 + y \\\CMidRule[16.0ex][5.0ex]{2-2}
&\PhantSQ \hphantom{x^2y + xy^2 + x +{}} y^2 + y \\
&\PhantSQ \hphantom{x^2y + xy^2 + x +{}} y^2 - 1 \\\CMidRule[20.0ex][5.0ex]{2-2}
&\PhantSQ \hphantom{x^2y + xy^2 + x + y^2 +{}} y + 1 \\\CMidRule[25.0ex][1.0ex]{2-2}
&\PhantSQ \hphantom{x^2y + xy^2 + x + y^2 + y +{}} 1 &\to x+y \\\CMidRule[25.0ex][1.0ex]{2-2}
&\PhantSQ \hphantom{x^2y + xy^2 + x + y^2 + y +{}} 0 &\to x+y+1
\\&\PhantSQ\color{red}x^2y + xy^2 + x + y^2 + y + 1
% Last line above should be removed -- used for alignment purposes only.
\end{array}
\]
\end{document}
```

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}
```

## Best Answer

It looks like people did not sure whether this is a "do for me" question or you are asking anything else. Let me start to clarify something...

First you mentions

`polynom`

, which is good at creating long divisionwith relatively easy code

But its output is relatively difficult to modify since it is hard-coded using

`tabular`

internally. You would need to rewrite the output scheme. If it is not the desired, the remaining problem is whether you prefer`polynom`

's default output or thecrossone you provide?Second,

`Mathjax`

, though extremely convenient over HTML, uses its own engine. It supports small amount of packages by rewriting them case by case. (Which may save some resources since one does not need line-breaking/page-breaking/etc anymore.) Thus it is hard to create a consistent user experience between`MathJax`

and`polynom`

, a probably-will-never-support package. You might either`Mathjax`

-friendly syntax in your LaTeX file`MathJax`

and adopt that they are not not going to be the same.