-1

How can I make long division of polynomials in LaTeX? My goal is having something like picture below: (actually, this is a Polish notation)

Long division

I know about polynom package, but the problem is, that this is a little bit different situation than mentioned there...

Many thanks in advance!

3 Answers3

3

Improving @Henri Menke answer, taking into account the main requirement about divisor placement, using polynom package with automated long division, you could do this like so:

enter image description here

\documentclass{article}
\usepackage{polynom}
\makeatletter
\def\pld@ArrangeResult#1{%
    \ifx\pld@remainder\@empty
        \@tempcnta\pld@maxcol\relax
        \pld@InsertItems@do\pld@lastline{\pld@firsttrue\pld@PLD{\pld@R{0}{1}}}%
    \fi
    \pld@Extend\pld@allines{\pld@lastline\cr}%
    \let\pld@lastline\@empty
    \expandafter\pld@AR@col\expandafter\pld@PLD
                           \expandafter\pld@lastline#1+\relax+%
    \pld@SplitQuotient
    \let\pld@currentline\@empty
    \expandafter\pld@AR@col\expandafter\pld@PLD
                           \expandafter\pld@currentline
                                       \pld@quotient+\relax+%
    \expandafter\pld@AR@col\expandafter\pld@XPLD
                           \expandafter\pld@currentline
                                       \pld@shadow+\relax+%
    \edef\pld@subline{%
        \noexpand\cline{\tw@-\pld@maxcol}%
        \noalign{\vskip\jot}}%
    \pld@Extend\pld@currentline{\expandafter\cr\pld@subline}%
    \pld@AddTo\pld@lastline{
         &&&&{}~:~\pld@PrintPoly\pld@divisor % <- crucial change
    }
    \expandafter\pld@AR@\pld@allines\relax
}
\makeatother

\begin{document} [ \polylongdiv{x^3 + x - 2}{x - 1} ] \end{document}


Edit:

After a couple of issues that have been found and pointed out to in the comments, I have to make some remarks.

First of all, the solution based on changing some parts of polynom's source code. Unfortunately, everything related to the arrangement happens inside \pld@ArrangeResult macro that make it impossible to make tiny changes without copy pasting the whole definition of \pld@ArrangeResult into your preamble.

Since the OP's request was aimed to achieve the exact placement of a delimiter, I thought that I will keep only the important bits of \pld@ArrangeResult definition to make it work. But, as has been mentionoed before, apperantly I erased too much (but the code above seems bugless yet the smalles). In sake of versatility, I provide the MWE with the complete \pld@ArrangeResult definition with the only change for delimiter placement so that changing styles via \polyset{style=C} will work now as well.

\documentclass{article}
\usepackage{polynom}
\makeatletter
\def\pld@ArrangeResult#1{%
    \ifx\pld@remainder\@empty
        \@tempcnta\pld@maxcol\relax
        \pld@InsertItems@do\pld@lastline
            {\pld@firsttrue\pld@PLD{\pld@R{0}{1}}}%
    \fi
    \ifnum\pld@currstage>\z@
        \pld@Extend\pld@allines{\pld@lastline\cr}%
    \else
        \pld@InsertFake\pld@lastline
    \fi
    \let\pld@lastline\@empty
    \ifx C\pld@style
      \def\pld@lastline{\pld@leftdelim\strut\pld@rightxdelim&}%
    \fi
    \expandafter\pld@AR@col\expandafter\pld@PLD
                           \expandafter\pld@lastline#1+\relax+%
    \pld@SplitQuotient
    \pld@iftopresult
        \let\pld@currentline\@empty
        \expandafter\pld@AR@col\expandafter\pld@PLD
                               \expandafter\pld@currentline
                                           \pld@quotient+\relax+%
        \expandafter\pld@AR@col\expandafter\pld@XPLD
                               \expandafter\pld@currentline
                                           \pld@shadow+\relax+%
        \edef\pld@subline{%
            \noexpand\cline{\tw@-\pld@maxcol}%
            \noalign{\vskip\jot}}%
        \pld@Extend\pld@currentline{\expandafter\cr\pld@subline}%
        \pld@AddTo\pld@lastline{
             &&&&{}~:~\pld@PrintPoly\pld@divisor % <- crucial change
        }
    \else
        \@tempcnta-\@tempcnta
        \advance\@tempcnta\pld@maxcol\relax \advance\@tempcnta\@ne
        \edef\pld@span{\the\@tempcnta}%
        \@tempcntb\pld@maxcol\relax\advance\@tempcntb\pld@span%
        \advance\@tempcntb\@ne%
        \edef\pld@linepos{\the\@tempcntb}%
        \ifx B\pld@style
          \pld@AddTo\pld@lastline{%
            &\multispan\pld@span${}=%
            \pld@PrintPolyWithDelims\pld@divisor
            \expandafter\pld@IfSum\expandafter{\pld@divisor}{}{\cdot}%
            \expandafter\pld@IfSum\expandafter{\pld@quotient}\pld@true
                                                             \pld@false
            \pld@if \pld@leftdelim
                    \pld@PrintPolyShadow
                    \pld@rightdelim
              \else \pld@PrintPolyShadow \fi
            \pld@firstfalse
            \expandafter\pld@PrintRemain\expandafter{\pld@remainder}$}%
        \else
          \if C\pld@style
            \pld@AddTo\pld@lastline{%
              &\multispan\pld@span$\pld@leftxdelim\strut\pld@rightdelim
              \pld@div
              \pld@PrintPolyWithDelims\pld@divisor=
              \pld@PrintPolyShadow
              \ifx\pld@remainder\@empty\else
                  +{}%
                  \setbox\z@=\hbox{$\displaystyle
                    \frac{\let\strut\@empty\pld@firsttrue \expandafter
                          \pld@PrintRemain\expandafter{\pld@remainder}}%
                         {\let\strut\@empty\pld@PrintPoly\pld@divisor}$}%
                  \dp\z@=\z@\box\z@
              \fi
              $}%
          \else
            \pld@AddTo\pld@lastline{%
              \cr%
              \noalign{\vskip-\normalbaselineskip}%
              \multispan\pld@maxcol~&~&\multispan\pld@span${}\vrule~%
              \pld@PrintPoly\pld@divisor\hfil\hfil$\cr%
              \cline{\pld@maxcolplustwo-\pld@linepos}%
              \multispan\pld@maxcol~&~&\multispan\pld@span${}\vrule height
              2.25ex~\pld@PrintPolyShadow$\hfil\cr\noalign{\vskip-2\normalbaselineskip}}%
          \fi
        \fi
    \fi
    \expandafter\pld@AR@\pld@allines\relax}
\makeatother

\begin{document} [ \polylongdiv{x^3 + x - 2}{x - 1} ] \end{document}

antshar
  • 4,238
  • 9
  • 30
  • Very good! Thanks a lot! – constantin_d Jul 18 '22 at 09:29
  • @constantin_d apparently, the \polylongdiv breaks in math mode after my changes. Fixed it, see the edited post. – antshar Jul 18 '22 at 10:07
  • Many, many thanks! Beautiful solution! – constantin_d Jul 18 '22 at 10:11
  • Sorry for a trouble again... I tested Your code on different polynomials... Look, what happened: photo – constantin_d Jul 18 '22 at 10:31
  • @constantin_d it's me who should be excusing for such issues. Hope, now it's completely fixed, see the edits. Actually, the way how the solution works is a chunk of a source code from polynom package with some changes. Unfortunately, the author's style of coding isn't really flexible, so many things are hardcoded inside one macro. Basically, all if-else are stored there for each of the predefined styles, so I tried to remove everything extra that doesn't refer to our needs. – antshar Jul 18 '22 at 10:40
  • I appreciate Your work very much - it is very hardcoded, as You said... Are you able to open a chat with me? – constantin_d Jul 18 '22 at 10:42
  • 1
    @constantin_d, sure, you can find my email in the TeX.SE profile description. – antshar Jul 18 '22 at 10:43
  • I sent You an e-mail ;) – constantin_d Jul 18 '22 at 11:23
1

Either tabular or array seems the most trivial approach; the latter is specifically dedicated for tabulating math expressions and must be inside \begin{equation}...\end{equation} or any other math mode related environment.

The solution based on array

enter image description here

\documentclass{article}
\usepackage{array}
\usepackage{mathtools}

\renewcommand{\arraystretch}{1.25} \setlength\arraycolsep{3pt}

\begin{document}

[ \begin{array}{*7{c}} x^2 &+& x &+& 2 & & \ \cline{1-5} (x^3 &+& x &-& 2) &:& \mathrlap{(x-1)} \ -(x^3 &-& x^2) & & & & \ \cline{1-5} & & x^2 &+& x & & \ & & -(x^2 &-& x) & & \ \cline{3-7} & & & & 2x &-& 2 \ & & & & -(2x &-& 2) \ \cline{5-7} & & & & & & 0 \ \end{array} ]

\end{document}

Celdor
  • 9,058
0

Why not just use the polynom package?

\documentclass{article}
\usepackage{polynom}
\begin{document}
\[ \polylongdiv{x^3 + x - 2}{x - 1} \]
\end{document}

enter image description here

Henri Menke
  • 109,596
  • I tried this solution - is simply, but there is one problem with it - I need a division like in example... – constantin_d Jul 18 '22 at 08:37
  • @constantin_d This division is identical to the one in your example. If you don't like the style, you can try another one with \polylongdiv[style=B]. Available styles are A, B, C, and D. – Henri Menke Jul 18 '22 at 08:39
  • No, that is not actually as I really want - look about divisor... Here is on a left... In other version - is the quotient on the right... – constantin_d Jul 18 '22 at 08:42