17

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[psamsfonts]{amsfonts}
\usepackage{sudoku}
\usepackage{natbib}
\usepackage{amsmath, amsthm, amssymb}
\usepackage{epsfig}
\usepackage{makeidx}
\usepackage{array}
\usepackage{enumerate}
\usepackage[usenames]{color}
\usepackage{setspace}
\usepackage{latexsym}
\usepackage[dvips]{graphicx}
\usepackage{eucal}
\usepackage{polynom}

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.

4
  • 1
    I am not understanding this. Can you show an image of what the desired output is preferably with a MWE that shows what you have attempted and the specific problem you have encountered. Apr 2, 2012 at 18:45
  • I have no idea how to even come close to writing the code that I need for this. The best that I can do is reference a paper that uses what I'm looking for: math.berkeley.edu/~aboocher/kaitlyn.pdf The fourth page has the type of division that I'm trying to do. Apr 7, 2012 at 1:20
  • 1
    Can you be a bit more specific as to exactly where on the paper. I appreciate that you can not produce this, but it is usually a good idea to show what you tried. Apr 7, 2012 at 1:22
  • In the middle of the fourth page and on the top of the fifth page, she does polynomial long division with multi-variables. I have tried using the polynom package but I am not at all familiar with code and it doesn't allow what I need. What I am trying now is using excel to write the formulas, saving it as a pdf, and inserting that into LaTeX. However, that looks extremely messy and unprofessional. Apr 7, 2012 at 2:13

1 Answer 1

14

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

Notes:

  • 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>}.

Notes

  • 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

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

Code: Extended Example

\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}
5
  • Thank you very much for your help! Is there a way to change the alignment of the terms being subtracted? If you look at the top of pg 5 of Kaitlyn Moran's document, math.berkeley.edu/~aboocher/kaitlyn.pdf she has multiple terms and I was hoping to have a similar effect. Apr 7, 2012 at 2:33
  • 1
    @KatherineM.: If there are not a large number of these I would resort to using \hphantom{} to leave the exact amount of space. Otherwise you could use multiple align points within the array. Apr 7, 2012 at 2:58
  • Do you have any suggestions for helpful websites to help me better understand how arrays work? Apr 7, 2012 at 3:00
  • 1
    There should be numerous examples on this site, very similar to tabular except in math mode. I will try to do a solution that uses \phantom{} to do the spacing. Better still is if you attempt to produce your version, and then you could post a question here as to how to do better alignment. Apr 7, 2012 at 3:08
  • @KatherineM.: Updated solution. Let me know if something is not clear. Apr 8, 2012 at 3:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .