编辑:

编辑:

如何在 LaTeX 中进行多项式的长除法?我的目标是得到如下图所示的结果:(实际上,这是一种波兰表示法)

长除法

我知道polynom包裹,但问题是,这与那里提到的情况有点不同......

提前谢谢了!

答案1

改进@Henri Menke答案,考虑到关于除数放置的主要要求,使用polynom自动长除法包,您可以这样做:

在此处输入图片描述

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

编辑:

在评论中发现并指出了几个问题后,我必须发表一些评论。

首先,解决方案基于更改polynom源代码的某些部分。不幸的是,与安排相关的一切都发生在\pld@ArrangeResult宏内部,这使得如果不将整个定义复制粘贴\pld@ArrangeResult到序言中,就无法进行微小的更改。

由于 OP 的要求是实现分隔符的精确放置,因此我认为我将仅保留\pld@ArrangeResult定义中的重要部分以使其正常工作。但是,正如之前提到的,显然我删除了太多内容(但上面的代码似乎没有错误,而且是最小的错误)。为了通用性,我为 MWE 提供了完整的定义,\pld@ArrangeResult只对分隔符放置进行了更改,以便\polyset{style=C}现在也可以通过更改样式来工作。

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

答案2

两者之一tabular似乎array是最简单的方法;后者专门用于制表数学表达式,并且必须位于内部\begin{equation}...\end{equation}或任何其他数学模式相关的环境中。

解决方案基于array

在此处输入图片描述

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

答案3

为什么不直接使用polynom包裹?

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

在此处输入图片描述

相关内容