# Expand LaTeX macros to modify

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.

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

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

Not extensively tested but...

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

• It will not work, if the result is 1+d, then the 1 will also vanish. – Heiko Oberdiek Sep 18 '13 at 20:14
• @HeikoOberdiek ah, it worked on the two I tested it with:-), if you know this code is it likely to be easy to fix? I'll have a quick look but delete the answer if I don't spot something obvious. – David Carlisle Sep 18 '13 at 20:21
• @FrankEpps see Heiko's comment above – David Carlisle Sep 18 '13 at 20:22
• @HeikoOberdiek better? – David Carlisle Sep 18 '13 at 20:29
• Thanks, but already red-capped. BTW, it is not "stealing". "Well deserved" is a better phrase, if a tick moves to me. :-)) – Heiko Oberdiek Sep 18 '13 at 21:13

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