定义一张图片并将其作为节点

定义一张图片并将其作为节点

我的目的是画一个水平的圆柱体。我用 定义了左底和右底/.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}

相关内容