Tikz,定义个位数、十分位数、百分位数块

Tikz,定义个位数、十分位数、百分位数块

下面是一段用十进制块生成数学问题的代码:

\tikzset {
   base graph/.pic={
      \draw [] 
         (0,0)   rectangle (1,0.1) 
         (0,0.2) rectangle (1,1.2) 
         (0,1.3) rectangle (1,2.3) ;
      \draw (-0.5,1.3)  node {2.1} ;
      \foreach \x in {1.1,1.3,1.5,1.7} {
         \draw (\x,2.3) rectangle (\fpeval{\x+0.1},1.3) ;
      }
      \draw (1.1,2.6)  node {1.4} ;
   }
}

\begin{tikzpicture}
   \pic {base graph};
\end{tikzpicture} 

\begin{tikzpicture}
   \pic {base graph};
   \foreach \x in {1.1,1.3,1.5,1.7} {
      \draw [color=blue] 
         (\x,1.2)  rectangle (\fpeval{\x+0.1},0.2) 
         (\x,0)    rectangle (\fpeval{\x+0.1},0.1);
}
\end{tikzpicture} 

代码很好,但是有没有办法定义特定的形状,以便您可以执行类似的操作

\draw [one] (0,0)
\draw [tenths] (0,0)
\draw [hundredths] (0,0)

其可读性/可重用性更强。

在此处输入图片描述

答案1

另一种方法是使用不同的styles矩形节点。放置由库完成positioning

\documentclass[tikz, border=2mm]{standalone}

\usetikzlibrary{positioning}

\tikzset {
    node distance=.1cm,
    one/.style={
        draw,
        minimum size=1cm,
        inner sep=0pt
        },
    tenthv/.style={
        draw,
        minimum height=1cm,
        minimum width=0.1cm,
        inner sep=0pt,
    },
    tenthh/.style={
        draw,
        minimum height=0.1cm,
        minimum width=1cm,
        inner sep=0pt,
    },
    hundreth/.style={
        draw,
        minimum size=0.1cm,
        inner sep=0pt,
    },
   base graph/.pic={
        \node[one] (00) {};
        \node[one, below=of 00] (10) {};
        \node[tenthh, below=of 10] (20) {};
        \foreach \i [count=\xi, remember=\xi as \lasti (initially 0)] in {1,2,3,4} 
          \node[tenthv, right=of 0\lasti] (0\xi) {};         
        \path (00.north west) -- (20.south west) node[midway, left] {2.1} ;
        \path (00.north west) -- (04.north east) node[midway, above] {1.4} ;
        }
    }

\begin{document}

      \begin{tikzpicture}
        \pic {base graph};
      \end{tikzpicture} 

      \begin{tikzpicture}
        \pic {base graph};
        \foreach \i [count=\xi] in {1,2,3,4} {
          \node[tenthv, blue, below= of 0\xi] (1\xi) {}; 
          \node[hundreth, blue, below= of 1\xi] (2\xi) {}; 
          }
      \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容