在 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

將節點文字作為第三個參數傳遞給\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}

相關內容