3

For the following code,

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\usetikzlibrary{decorations.pathmorphing,arrows.meta,backgrounds,hobby}
\tikzset{>={Latex[width=2mm,length=2mm]},
Nozzle/.pic={
    \draw[fill=yellow]
    (0,0)
    -- ++(0.4,0) coordinate(-a)
    -- ++(1,-2) coordinate(-b)
    -- ++(0,-0.2) coordinate(-c)
    -- ++(-0.2,0) coordinate(-d)
    -- ++(0,-0.2) coordinate(-e)
    -- ++(-0.2,0)
    -- ++(0,0.4)
    -- cycle ;

    \draw[fill=brown] (#1-c)
    -- (#1-d)
    -- (#1-e)
    -- ++(0.4,-0.1)
    -- ++(0,0.1)
    -- cycle;

}}

\begin{document}
    \begin{tikzpicture}

    \pic (left) at (0,0) {Nozzle};
    \pic[xscale=-1] (right) at (4,0) {Nozzle};

    \end{tikzpicture}
\end{document}

the output is

enter image description here

while the desired output should be

enter image description here

Diaa
  • 9,599
  • First, you should remove #1 in the second draw statement. After that, the code still does not work which is due, I think, to a bug in the implementation of \pic. I remember to have read this argument (\pic being buggy) also as an answer to other complaints about \pic behaving strangely. I recommend to use a macro definition as I have proposed in my answer to your previous question. – gernot Apr 05 '17 at 17:49
  • @gernot I put #1 since I thought it should be after checking this answer. I tried to follow Ignasi's answer, but probably, I will stick to the macro approach. – Diaa Apr 05 '17 at 17:58

2 Answers2

2

Correct coordinate names inside pic are -c, -d, ... not #1-c, ... If you use them and don't let cycle to close the path, your code works for me.

Note: I've deleted hobby package due to compiling errors in my system. In any case, it's not necessary for this example.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\usetikzlibrary{decorations.pathmorphing,arrows.meta,backgrounds}
\tikzset{
    >={Latex[width=2mm,length=2mm]},
    Nozzle/.pic={
        \draw[fill=yellow]
        (0,0)
    -- ++(0.4,0) coordinate(-a)
    -- ++(1,-2) coordinate(-b)
    -- ++(0,-0.2) coordinate(-c)
    -- ++(-0.2,0) coordinate(-d)
    -- ++(0,-0.2) coordinate(-e)
    -- ++(-0.2,0) coordinate(-f)
    -- ++(0,0.4)
    -- cycle;
    \draw[fill=brown] (-c)
        -| (-e)-- ++(0.4,-0.1) -- ++(0,0.1) --(-c);
}
}

\begin{document}
    \begin{tikzpicture}
    \pic (left) at (0,0) {Nozzle};
    \pic[xscale=-1] (right) at (4,0) {Nozzle};
    \end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
  • Many thanks, for me, it works even with loading hobby. However, it is really strange that cycle doesn't work in the second draw. Is it mentioned as a bug in the manual to be fixed later or should I just ignore it and move on? – Diaa Apr 05 '17 at 18:24
  • Well, you are cheating a bit ... Replace (-c) -| (-e) by (-c) -- (-d) -- (-e), which should work as well, and you will note that the area is not completely filled. This pic thing is full of surprises. – gernot Apr 05 '17 at 18:38
2

Here is a solution along the lines of this answer. You have to change the following things in your code:

  • Add the option name prefix .. to the second draw command.

  • You have to provide a value for the parameter #1 by adding it after the name of the pic: Nozzle=left and Nozzle=right.

.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\tikzset{
Nozzle/.pic={
    \draw[fill=yellow]
    (0,0)
    -- ++(0.4,0) coordinate(-a)
    -- ++(1,-2) coordinate(-b)
    -- ++(0,-0.2) coordinate(-c)
    -- ++(-0.2,0) coordinate(-d)
    -- ++(0,-0.2) coordinate(-e)
    -- ++(-0.2,0)
    -- ++(0,0.4)
    -- cycle ;

    \draw[fill=brown,name prefix ..] (#1-c) % <<<<<<<<<<<<<<<<<<<<
    -- (#1-d)
    -- (#1-e)
    -- ++(0.4,-0.1)
    -- ++(0,0.1)
    -- cycle;

}}

\begin{document}
    \begin{tikzpicture}

    \pic (left) at (0,0) {Nozzle=left}; % <<<<<<<<<<<<<<<<
    \pic[xscale=-1] (right) at (4,0) {Nozzle=right}; % <<<<<<<<<<<<<<<<

    \end{tikzpicture}
\end{document}
gernot
  • 49,614
  • Many thanks for solving it. Could you please tell me where I can find more info on name prefix .. and {Nozzle=left/right} in the manual? – Diaa Apr 05 '17 at 18:50
  • 1
    Regarding name prefix .. see the Tikz manual page 255. Read the whole section 18 starting on page 251. – gernot Apr 05 '17 at 21:29