[Tex/LaTex] Multivariate polynomial long division using LaTeX


How do I typeset polynomial long division using multiple variables in LaTeX? I am hoping to be able to do polynomial long division using 2 or more divisors. Currently, the packages I am using are:

\usepackage{amsmath, amsthm, amssymb}

I know that there are a lot of packages that I am using which are unrelated to my question, but I wouldn't want to use conflicting packages.

I thought the the polynom package would be my solution, but one of the disclaimers in the package is that "Multivariate polynomials are currently not supported."

Any help would be greatly appreciated.

Best Answer

Basic Example:

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.

enter image description here


  • 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

Extended Example

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:

enter image description here

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.

Code: Basic Example



   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

Code: Extended Example



% https://tex.stackexchange.com/questions/63355/wrapping-cmidrule-in-a-macro
    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} }

     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.
Related Question