使用 \usepackage{tikz,xparse,xstring,fp} 创建格子乘法

使用 \usepackage{tikz,xparse,xstring,fp} 创建格子乘法
   \documentclass[tikz,border95=3mm]{article}
   \usepackage{tikz}

 \begin{document}


   \begin{tikzpicture}[case/.style={minimum width=2cm,minimum height=2cm,draw},line/.style={blue!10!gray,line width=4,line cap=round,line join=round}]    

\node[case] (a) at (0,0){} ;
\node[case] (b) at (2,0){} ;
\node[case] (c) at (4,0){} ;

\draw[line] (a.south west) -- (a.north west) -- (a.north east) -- (a.south west)-- ++(-1,-1);
\draw[line] (b.south west) -- (b.north west) -- (b.north east) -- (b.south west)-- ++(-1,-1);
\draw[line] (c.south west) -- (c.north west) -- (c.north east) -- (c.south west)-- ++(-1,-1);
\draw[line] (a.south west) -- (c.south east) -- (c.north east);

\node[above=0.25 cm] at (a.north) {\huge 2};
\node[above=0.25 cm] at (b.north) {\huge 1};
\node[above=0.25 cm] at (c.north) {\huge 8};
\node[right=0.25 cm] at (c.east){\huge 3}; 
\node[text=red,shift={(0.5,0.5)}] at (c.north east) {\huge $\times$};
\node[below=0.25cm] at (c.north) {\hugh 2};
\node[above=0.25cm] at (c.south) {\hugh 4};
\node[below=0.25cm] at (b.north) {\hugh 0};
\node[above=0.25cm] at (b.south) {\hugh 3};
\node[below=0.25cm] at (a.north) {\hugh 0};
\node[above=0.25cm] at (a.south) {\hugh 6};
\node[below=0.25cm] at (c.south) {\hugh 4};
\node[below=0.25cm] at (b.south) {\hugh 5};
\node[below=0.25cm] at (a.south) {\hugh 6};
\node[left=0.25cm] at (a.west) {\hugh 0};
 \end{tikzpicture}\\

Displaying the totals from left to right we get:\\
      =$0654$\\
or if we forgget the leading zero we get\\
     Total=654

 \end{document}

我过去曾使用 \usepackage{tikz,xparse,xstring,fp} 创建一个长乘法表,我想知道我是否可以使用类似的系统自动从上述代码中得出结果。谢谢

答案1

另一种解决方案是节点内部和外部均带有标签

\documentclass[tikz,border95=3mm]{article}
\usepackage{tikz}

\begin{document}


\begin{tikzpicture}[
    case/.style={minimum width=2cm, minimum height=2cm, draw, line},
    line/.style={blue!10!gray, line width=4, line cap=round},
    every label/.style={font=\huge},
    inner label/.style={inner sep=2mm, outer sep=2mm, anchor=#1}]    

\node[case, label=2, label=left:0, label=below:6,
    label={[inner label=north west]north west:0},
    label={[inner label=south east]south east:6},
    ] (a) at (0,0){} ;

\node[case, label=1, label=below:5,
    label={[inner label=north west]north west:0},
    label={[inner label=south east]south east:3},
    ] (b) at (2,0){} ;

\node[case, label=8, label=east:3, label=below:4,
    label={[red]north east:$\times$},
    label={[inner label=north west]north west:2},
    label={[inner label=south east]south east:4},
    ] (c) at (4,0){} ;
    
\draw[line, shorten >=-5mm] ([shift={(-135:2mm)}]a.north east) -- (a.south west);
\draw[line, shorten >=-5mm] ([shift={(-135:2mm)}]b.north east) -- (b.south west);
\draw[line, shorten >=-5mm] ([shift={(-135:2mm)}]c.north east) -- (c.south west);
\end{tikzpicture}\\

Displaying the totals from left to right we get:\\
      =$0654$\\
or if we forgget the leading zero we get\\
     Total=654

 \end{document}

在此处输入图片描述

答案2

以下是基于以下可能性我的答案你的上一个问题基于此代码,还可以创建仅传递单个值的命令。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand\numberOfSquares{3}

\begin{document}
    \begin{tikzpicture}[
        every node/.style={
            font=\huge
        },
        line/.style={
            blue!10!gray,
            line width=3
        }
    ]

        % border
        \pgfmathtruncatemacro\totalWidth{2*\numberOfSquares}
        \draw[line] (0,2) rectangle (\totalWidth,0);

        % vertical lines
        \pgfmathtruncatemacro\lastX{\totalWidth-2}
        \foreach \x in {2,4,...,\lastX} {
            \draw[line] (\x,0) -- (\x,2);
        }

        % oblique lines
        \foreach \i in {2,4,...,\totalWidth} {
            \draw[line] (\i,2) -- (\i-3,-1);
        }

        % multiplication symbol
        \node[text=red] at (6.4,2.4) {$\times$};

        % values above and below
        \foreach \nodeTextAbove/\nodeTextBelow [count=\c] in {2/6,1/5,8/4} {
            \node at (\c*2-1,2.4) {\nodeTextAbove};
            \node at (\c*2-1,-.4) {\nodeTextBelow};
        }

        % values left and right
        \node at (-.4,1) {0};
        \node at (\totalWidth+.4,1) {3};
        % or for two values left and right
        %\node at (-.4,1.5) {0};
        %\node at (-.4,0.5) {X};
        %\node at (\totalWidth+.4,1.5) {3};
        %\node at (\totalWidth+.4,0.5) {Y};

        % values inside
        \foreach \nodeTextA/\nodeTextB [count=\c] in {0/6,0/3,2/4} {
            \node at (\c*2-1.4,1.4) {\nodeTextA};
            \node at (\c*2-0.6,0.6) {\nodeTextB};
        }
    \end{tikzpicture}\\

    \noindent
    Displaying the totals from left to right we get:\\
      = $0654$\\
    or if we forget the leading zero we get:\\
     Total = $654$
\end{document}

使用 TikZ 进行格子乘法。

相关内容