TikZ でユーザー定義の図形にラベルを付ける

TikZ でユーザー定義の図形にラベルを付ける

プリミティブな「マッチ」シェイプを回転させるためのコードを修正しました(ここにある回転カメラコードから適応しました(カスタムシェイプを「ビルディングブロック」として使用する)。

スコープ内のラベルパラメータを渡すと、各一致が異なる番号になります。現在、すべてのノードは「2」にハードコードされています。

私はTikZを初めて使うので、正確なやり方がわかりません。

\documentclass{book}    
\usepackage{tikz}

\begin{document}  

\def\match#1#2{    
\begin{scope}[shift={#1}, rotate=#2]    
\draw (0,0) rectangle (2.5,0.2);    
\draw [fill=black](2.3,0.1) ellipse (0.35 and 0.2) node at (1,0.1)
[fill=white,opacity=.2,text opacity=1,circle, inner sep=0pt,minimum size=1pt]{\textbf{2}};    
\end{scope}    
}

\begin{tikzpicture}    
\match{(0,0)}{45}    
\match{(-0.3,-0.18)}{315}    
\match{(1.85,2.06)}{315}    
\match{(1.89,-1.89)}{45}    
\end{tikzpicture}    
\end{document}

答え1

ノードテキストを3番目の引数として渡します\match:

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

\documentclass{book}
\usepackage{tikz}

\begin{document}

\newcommand\match[3]{%
    \begin{scope}[shift={#1}, rotate=#2]
    \draw (0,0) rectangle (2.5,0.2);
    \draw [fill=black](2.3,0.1) ellipse (0.35 and 0.2) node at (1,0.1)
        [fill=white,%
        opacity=.2,%
        text opacity=1,%
        circle,%
        inner sep=0pt,%
        minimum size=1pt]{\textbf{#3}};
    \end{scope}
}

\begin{tikzpicture}
    \match{(0,0)}{45}{4}
    \match{(-0.3,-0.18)}{315}{5}
    \match{(1.85,2.06)}{315}{3}
    \match{(1.89,-1.89)}{45}{1}
\end{tikzpicture}

\end{document}

関連情報