![定义一张图片并将其作为节点](https://linux22.com/image/453143/%E5%AE%9A%E4%B9%89%E4%B8%80%E5%BC%A0%E5%9B%BE%E7%89%87%E5%B9%B6%E5%B0%86%E5%85%B6%E4%BD%9C%E4%B8%BA%E8%8A%82%E7%82%B9.png)
我的目的是画一个水平的圆柱体。我用 定义了左底和右底/.pic
。现在我将用 链接这些底\draw
,使用底锚点.north
和.south
。出现错误:无输出
\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\tikzset{
leftbase/.pic={
code={
\def\xR{0.5cm}; %x radius of ellipse
\def\yR{1cm}; %y radius of ellipse
\draw (-\xR,0) arc(90:270:0.5cm and 1cm);
\draw (-\xR,0) arc(90:-90:0.5cm and 1cm);
}
},
rightbase/.pic={
code={
\def\xR{0.5cm}; %x radius of ellipse
\def\yR{1cm}; %y radius of ellipse
\draw[dotted] (-\xR+1.7in,0) arc(90:270:0.5cm and 1cm);
\draw (-\xR+1.7in,0) arc(90:-90:0.5cm and 1cm);
}
}
}
node[leftbase] (lb) {};
node[rightbase] (rb) {};
\end{tikzpicture}
\end{figure}
\end{document}
任何想法?
答案1
也许你没有pic
正确使用。这里有一个小小的改变,使你的代码工作:
\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\tikzset{
leftbase/.pic={
code={
\def\xR{0.5cm}; %x radius of ellipse
\def\yR{1cm}; %y radius of ellipse
\draw (-\xR,0) arc(90:270:0.5cm and 1cm);
\draw (-\xR,0) arc(90:-90:0.5cm and 1cm);
}
},
rightbase/.pic={
code={
\def\xR{0.5cm}; %x radius of ellipse
\def\yR{1cm}; %y radius of ellipse
\draw[dotted] (-\xR+1.7in,0) arc(90:270:0.5cm and 1cm);
\draw (-\xR+1.7in,0) arc(90:-90:0.5cm and 1cm);
}
}
}
\pic [local bounding box=lb] at (0,0) {leftbase};
\pic [local bounding box=rb] at (1,0) {rightbase};
\draw (lb.north) -- (rb.north);
\draw (lb.south) -- (rb.south);
\end{tikzpicture}
\end{figure}
\end{document}