给减法代码添加颜色,同时借位、划掉并回答

给减法代码添加颜色,同时借位、划掉并回答

谁能帮我给这个代码添加一些颜色,例如数字、取消、借位和答案都是不同的颜色。

\documentclass{article}
\usepackage{stringstrings,stackengine}
\newcommand\showdiff[2]{%
  \let\savediff\relax%
  \def\borrow{}%
  \xdef\borrowed{0}%
  \ifnum#1>32\relax\showdiffengine{#1}{#2}\else
    {\ttfamily\Longstack[r]{#1\,{} \underline{-#2\,} {< 0\,}}}\fi
}
\newcommand\showdiffengine[2]{%
  \xdef\opA{#1}%
  \xdef\opB{#2}%
  \lastdigits{\opA}{\opB}%
  \ifnum\borrowed=1\relax\xdef\XdigA{\the\numexpr\digA- 1}\else\xdef\XdigA{\digA}\fi%
   \ifnum\XdigA<0\relax\xdef\XdigA{9}\xdef\futureborrowed{1}\else
    \xdef\futureborrowed{0}\fi
   \edef\coldiff{\the\numexpr\XdigA-\digB\relax}%
   \ifnum\coldiff<0\relax%
    \xdef\borrow{1}%
    \edef\coldiff{\the\numexpr\borrow\XdigA-\digB\relax}%
   \else
    \xdef\borrow{}%
  \fi
  \ifnum\borrowed=0\relax%
    \savestack{\thediff}{\ttfamily\Longstack[l]{%
      \tiny\bllap[1.5\baselineskip]{\borrow} \XdigA{} \underline{\VdigB\kern3pt} %
        \coldiff}}%
  \else
    \savestack{\thediff}{\ttfamily\Longstack[l]{%
     \tiny\brlap[.7\baselineskip]{\borrow\the\numexpr\XdigA} \crossout{\digA} %
        \underline{\VdigB\kern3pt} \coldiff}}%
   \fi
   \if\relax\borrow\relax\xdef\borrowed{0}\else\xdef\borrowed{1}\fi
   \savestack{\savediff}{\thediff\savediff}%
   \ifnum\futureborrowed>0\relax\xdef\borrowed{1}\xdef\fugureborrowed{0}\fi
   \nextcolumn{\opA}{\opB}%
   \ifnum\numexpr0\opA+0\opB>0\relax\showdiffengine{\opA}{\opB}%
   \else
    \Longstack{\underline{\ttfamily-\,} {}}\savediff\fi%
 }
 \newcommand\crossout[1]{\stackengine{0pt}{#1}{/}{O}{c}{F}{F}{L}}
 \newcommand\lastdigits[2]{%
   \substring[q]{#1}{$}{$}%
   \xdef\digA{\thestring}%
   \xdef\VdigA{\digA}\if0\digA\ifnum#1=0\relax\xdef\VdigA{~}\fi\fi%
   \substring[q]{#2}{$}{$}%
   \xdef\digB{\thestring}%
   \xdef\VdigB{\digB}\if0\digB\ifnum#2=0\relax\xdef\VdigB{~}\fi\fi%
 }
 \newcommand\nextcolumn[2]{%
   \substring[q]{#1}{1}{$-1}%
   \xdef\opA{0\thestring}%
   \substring[q]{#2}{1}{$-1}%
   \xdef\opB{0\thestring}%
 }
 \begin{document}

 \showdiff{135}{49}\qquad



 \end{document}

在此处输入图片描述

答案1

添加了一些颜色。

d

\documentclass{article}
\usepackage{stringstrings,stackengine}

\usepackage{xcolor}% added <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
\colorlet{digits}{green}
\colorlet{cross}{teal}
\colorlet{upper}{blue}
\colorlet{answer}{gray}
\colorlet{borrow}{red}  

\newcommand\showdiff[2]{%
    \let\savediff\relax%
    \def\borrow{}%
    \xdef\borrowed{0}%
    \ifnum#1>32\relax\showdiffengine{#1}{#2}\else
    {\ttfamily\Longstack[r]{#1\,{} \underline{-#2\,} {< 0\,}}}\fi
}
\newcommand\showdiffengine[2]{%
    \xdef\opA{#1}%
    \xdef\opB{#2}%
    \lastdigits{\opA}{\opB}%
    \ifnum\borrowed=1\relax\xdef\XdigA{\the\numexpr\digA- 1}\else\xdef\XdigA{\digA}\fi%
    \ifnum\XdigA<0\relax\xdef\XdigA{9}\xdef\futureborrowed{1}\else
    \xdef\futureborrowed{0}\fi
    \edef\coldiff{\the\numexpr\XdigA-\digB\relax}%
    \ifnum\coldiff<0\relax%
    \xdef\borrow{1}%
    \edef\coldiff{\the\numexpr\borrow\XdigA-\digB\relax}%
    \else
    \xdef\borrow{}%
    \fi
    \ifnum\borrowed=0\relax%
    \savestack{\thediff}{%
        \ttfamily\Longstack[l]{%
            \tiny\bllap[1.5\baselineskip]{\textcolor{borrow}{\borrow}} \textcolor{digits}{\XdigA{}} \underline{\textcolor{digits}{\VdigB}\kern3pt} % <<<<<<<<<<<
            \textcolor{answer}{\coldiff}}}% <<<<<<<<<<<
    \else
    \savestack{\thediff}{\ttfamily\Longstack[l]{%
            \tiny\brlap[.7\baselineskip]{\color{upper}\borrow\the\numexpr\XdigA} \crossout{\textcolor{digits}{\digA}} % % <<<<<<<<<<<
            \underline{\textcolor{digits}{\VdigB}\kern3pt}%
            \\ \textcolor{answer}{\coldiff}}}% <<<<<<<<<<<<<<<<<<
    \fi
    \if\relax\borrow\relax\xdef\borrowed{0}\else\xdef\borrowed{1}\fi
    \savestack{\savediff}{\thediff\savediff}%
    \ifnum\futureborrowed>0\relax\xdef\borrowed{1}\xdef\fugureborrowed{0}\fi
    \nextcolumn{\opA}{\opB}%
    \ifnum\numexpr0\opA+0\opB>0\relax\showdiffengine{\opA}{\opB}%
    \else
    \Longstack{\underline{\ttfamily-\,} {}}\savediff\fi%
}
\newcommand\crossout[1]{\stackengine{0pt}{#1}{\textcolor{cross}{/}}{O}{c}{F}{F}{L}}% <<<<<<<<<<<<<<<<<<,
\newcommand\lastdigits[2]{%
    \substring[q]{#1}{$}{$}%
    \xdef\digA{\thestring}%
    \xdef\VdigA{\digA}\if0\digA\ifnum#1=0\relax\xdef\VdigA{~}\fi\fi%
    \substring[q]{#2}{$}{$}%
    \xdef\digB{\thestring}%
    \xdef\VdigB{\digB}\if0\digB\ifnum#2=0\relax\xdef\VdigB{~}\fi\fi%
}
\newcommand\nextcolumn[2]{%
    \substring[q]{#1}{1}{$-1}%
    \xdef\opA{0\thestring}%
    \substring[q]{#2}{1}{$-1}%
    \xdef\opB{0\thestring}%
}
\begin{document}
    \Large
    
    \showdiff{135}{49}\bigskip          

    \showdiff{2022}{1776}\bigskip

\end{document}

相关内容