![改变减法运算结果的颜色](https://linux22.com/image/454813/%E6%94%B9%E5%8F%98%E5%87%8F%E6%B3%95%E8%BF%90%E7%AE%97%E7%BB%93%E6%9E%9C%E7%9A%84%E9%A2%9C%E8%89%B2.png)
我正在尝试制作类似此图的东西,答案中划掉的部分可以涂成白色或不显示,以尝试显示减法所涉及的步骤。
\PassOptionsToPackage{dvipsnames}{xcolor}
\documentclass{book}
\usepackage{tikz}
\usepackage{xlop}
\usepackage{pstricks}
\newcommand{\myRed}[1]{\textcolor{red}{#1}}
%-------numberline code----------
% arrows.meta for the uptodate arrow tips
% shapes.misc for the cross out shape
\usetikzlibrary{arrows.meta, shapes.misc, bending}
\newcommand*\tikznumberlineset{\pgfqkeys{/tikz/number line}}
\tikznumberlineset{
arrow/.style={/tikz/>={#1}},
start/.initial=1, end/.initial=5,
tick length/.initial=2pt, overhang/.initial=.75,
%
% Nodes
start-end dot/.style={% those are the dots
shape=circle, fill=black, minimum size=+4pt,
inner sep=+0pt, outer sep=+0pt, node contents=},
%% defaults for "start dot" and "end dot"
start dot/.style=number line/start-end dot,
end dot/.style =number line/start-end dot,
%% number below the ticks
value node/.style={
below=2pt, font=\footnotesize, node contents=$\tikznumberlinevalue$},
%% text above diagram
instruction node/.style={
anchor=south, yshift=5pt, font=\small, node contents=%
Move \nlvo{units text} to the \emph{\nlvo{direction text}}.},
%
% 'Start' and 'End' node, "start-end label"
% can be used for additional labels
start-end label/.style={font=\small,yshift=-.75cm},
start label/.style={number line/start-end label,
text=Cerulean,node contents=Start},
end label/.style ={number line/start-end label,
text=OrangeRed,node contents=End},
%
% Lines, can be used to change color, thickness, etc.
line/.style ={draw,<->},
ticks/.style=draw,
connection/.style={draw=MidnightBlue,out=45,in=135},
%
% direction=left or direction=right
%% the direction changes
%% * the arrow tip
%% * the text left/right in the instruction and
%% * where "Start" and "End" will be set.
direction text/.initial=, units text/.initial=,
direction/.is choice,
direction/left/.style={
connection/.append style=<-, direction text=left,
label left/.style =number line/end label,
label right/.style=number line/start label},
direction/right/.style={
connection/.append style=->, direction text=right,
label left/.style =number line/start label,
label right/.style=number line/end label},
direction=right,% default
%
% to doesn't like foreach, going to use .list
% which uses foreach internally
@toloop/.style={
/utils/exec=\def\tikznumberlinevalue{#1},% make #1 available
insert path={to[number line/edge/.try,number line/edge #1/.try] (#1,0)}},
add edge node/.style args={#1[#2]#3}{edge #1/.append style={
edge node={node
[number line/edge node/.try,number line/edge node #1/.try,#2]{#3}}}},
add edge label/.style args={#1[#2]#3}{
add edge node={#1[number line/start-end label,at end,#2]#3}},
add edge dot/.style args={#1[#2]}{
add edge node={#1[number line/start-end dot,behind path,at end,#2]}},
all dots/.style={edge/.append style={
edge node={node[number line/start-end dot,at end,#1]{}}}},
all dots/.default=}
\newcommand*\tikznumberline[1][]{%
\begin{tikzpicture}
\tikznumberlineset{#1}
\newcommand*\nlvo[1]{\pgfkeysvalueof{/tikz/number line/##1}}
\pgfmathint{\nlvo{end}-(\nlvo{start})}
\pgfkeyssetevalue{/tikz/number line/units text}{\pgfmathresult}
%
% 1. The lines and the ticks:
\path[number line/ticks] (\nlvo{start}-1-\nlvo{overhang},\nlvo{tick length})
grid[ystep=0] (\nlvo{end}+1+\nlvo{overhang},-\nlvo{tick length});
\path[number line/line] (\nlvo{start}-1-\nlvo{overhang},0)
-- (\nlvo{end}+1+\nlvo{overhang},0);
%
% 2. the black circles (these are nodes now)
% and the labels "Start" and "End"
\path (\nlvo{start},0) node (@number line@start) [number line/start dot]
node [number line/label left]
(\nlvo{end},0) node (@number line@end) [number line/end dot]
node [number line/label right];
%
% 3. the numbers below the ticks
\foreach \tikznumberlinevalue[expand list]
in {\the\numexpr\nlvo{start}-1\relax,...,\the\numexpr\nlvo{end}+1\relax}
\node at (\tikznumberlinevalue,0) [number line/value node];
%
% 4. the hopping line which is fully connected, no movetos anymore
% the last edge is available via the last edge key if one needs to add
\path[number line/connection](@number line@start)
[number line/@toloop/.list/.expanded={%
\the\numexpr\nlvo{start}+1\relax,...,\the\numexpr\nlvo{end}-1\relax}]
to[number line/last edge/.try] (@number line@end);
% 5.
\node at ({(\nlvo{start}+\nlvo{end})/2},\nlvo{tick length})
[number line/instruction node];
\end{tikzpicture}}
\tikznumberlineset{arrow=Stealth}
\tikzset{small cross out/.style={
cross out,at end,minimum size=2mm,draw=gray,ultra thick,line cap=round}}
%-------End numberline code-----------
%-------Start Subtraction Code----------%
\usepackage{stringstrings,stackengine}
\usepackage{xcolor}%
\colorlet{digits}{black}
\colorlet{cross}{teal}
\colorlet{upper}{blue}
\colorlet{answer}{red}
\colorlet{borrow}{blue}
\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}%
%-------------End Subtraction Code------------------%
}
\begin{document}
\opadd[carryadd=false, resultstyle=\white]{135}{49}\\
\showdiff{135}{9}\bigskip
\tikznumberline[start=6, end= 15, direction=left,
add edge label={0[green]Middle},
add edge node={14[fill=white,font=\tiny\sffamily]-6},
add edge dot={0[green]}
]
\showdiff{135}{49}\bigskip
\tikznumberline[start=8, end= 12, direction=left,
add edge label={0[green]Middle},
add edge node={11[fill=white,font=\tiny\sffamily]-4},
add edge dot={0[green]}
]
\showdiff{135}{49}\bigskip
\end{document}