7

I am using the polynom package for polynomial long division. For example:

\documentclass{article}
\usepackage{polynom}

\begin{document}

\polylongdiv{x^3-x^2+d}{x-2}

\end{document}

Notice that I have another variable d there. polynom handles it well but calls it 1d.

screenshot

Is there any way to expand the code so I can edit out the 1?

2 Answers2

11

Like any latex package polynom is essentially a program whose source is available, so you can modify it:

Not extensively tested but...

enter image description here

\documentclass{article}
\usepackage{polynom}

\begin{document}

%\tracingall
\makeatletter
\def\pld@AccuPrint@#1#2{%
  \ifnum #2=\@ne \ifnum#1=\@ne\zz\else\number #1\fi\else \frac {\number #1}{\number #2}\fi }

\def\zz{\expandafter\zzz\romannumeral`\^^@}
\def\zzz{\futurelet\tmp\zzz@}
\def\zzz@{\ifx\tmp\let\else1\fi}

\polylongdiv{x^3-x^2+d}{x-2}

% check 1 is still printed if no following variable
\polylongdiv{x^3-x^2+1}{x-2}


%  Heiko
\polylongdiv{1+d}{1}

\end{document}
David Carlisle
  • 757,742
11

The final line is printed as

\pld@R {4}{1}+\pld@R {1}{1}\pld@S {d}{1}

\pld@R has the meaning of \pld@Rational. Thus the following example redefines \pld@Rational. If the numerator and denominator are equal and a symbol (\pld@S) follows, then \pld@R{1}{1} will omit the factor before the symbol.

\documentclass{article}
\usepackage{polynom}

\makeatletter
\let\pldx@saved@PrintRational\pld@PrintRational
\renewcommand*{\pld@PrintRational}[2]{%
  \def\pldx@do{\pldx@PrintRational{#1}{#2}}%
  \futurelet\pldx@token\pldx@do
}
\newcommand*{\pldx@PrintRational}[2]{%
  \let\pldx@do\@firstofone
  \ifx\pldx@token\pld@S
    \def\pldx@a{#1}%
    \def\pldx@b{#2}%
    \ifx\pldx@a\pldx@b
      \let\pldx@do\@gobble
    \fi
  \fi
  \pldx@do{%
    \pldx@saved@PrintRational{#1}{#2}%
  }%
}
\makeatother

\begin{document}

  \polylongdiv{x^3-x^2+d}{x-2}%

  \polylongdiv{x^3-x^2-3+d}{x-2}%

  % David Carlisle's test cases:

  \polylongdiv{x^3-x^2+1}{x-2}

  \polylongdiv{1+d}{1}

\end{document}

Result

Heiko Oberdiek
  • 271,626