Tikz、1/10、100分の1のブロックを定義する

Tikz、1/10、100分の1のブロックを定義する

以下は、10 進ブロックを使用して数学の問題を生成するコードのクリップです。

\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長方形ノードにdifferent を使用する代替手段。配置は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}

ここに画像の説明を入力してください

関連情報