我想在 TikZ 中制作如下所示的符号。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[thick] (0,0) rectangle (3,1);
\draw[thick] (0.5,0) -- (0.5,1);
\draw[thick] (0.5,0.5) -- (3.5,0.5);
\draw[thick] (0.25,0) -- +(0,-0.3);
\draw[thick] (2.75,0) -- +(0,-0.3);
\end{tikzpicture}
\end{document}
我需要插入它(比如说圆柱) 在 TikZ 代码中,如下所示:
\draw[cylinder] (2,2); % this inserts the symbol at point (2,2)
答案1
正如 John Wickerson 所建议的,您可以编写\cylinder
宏:
\documentclass{standalone}
\usepackage{tikz}
\newcommand\cylinder[2][]{
\begin{scope}[shift={(#2)}]
\draw[#1] (0,0) rectangle (3,1);
\draw[#1] (0.5,0) -- (0.5,1);
\draw[#1] (0.5,0.5) -- (3.5,0.5);
\draw[#1] (0.25,0) -- +(0,-0.3);
\draw[#1] (2.75,0) -- +(0,-0.3);
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\cylinder{0,0}
\cylinder[draw=blue,very thick]{2,2}
\coordinate (A) at (4,4);
\cylinder[draw=red,line width=5pt]{A}
\end{tikzpicture}
\end{document}
结果: