[Tex/LaTex] TikZ fill using draw color

tikz-decorationstikz-pgftikz-styles

The following code uses a decorator to add an arrow tip to an edge. Logically, the tip is part of the edge, and so I want it to be colored with the line-color. However, because the tip is rendered using \fill, it takes its color from the fill parameter. How can I make the color to respond to draw=red, and also not respond to fill=blue?

enter image description here

\documentclass[tikz, crop,border=1]{standalone}
\usetikzlibrary{decorations.markings, decorations.pathreplacing}

\newcommand{\drawArrow}[2]{
    \draw[
        decoration={
            markings,
            mark=at position 0.8 with {%
                \fill (0, 0.5pt) -- ++ (-0.25, 0.075) -- ++ (0, -0.075) -- cycle;
            }
        },
        postaction=decorate
    ] #1 -- #2;
}

\begin{document}
\begin{tikzpicture}[scale=2]
\tikzset{
    pe/.style={
        line width = 1pt,
        decoration={
            show path construction, 
            lineto code={%
                \drawArrow{(\tikzinputsegmentfirst)}{(\tikzinputsegmentlast)}
            },
            closepath code={%
                \drawArrow{(\tikzinputsegmentfirst)}{(\tikzinputsegmentlast)}
            }
        },
        postaction=decorate
    }
}

% Not what I want
\draw[pe, draw = red] (0, 0) -- (1, 0);

% Not what I want
\draw[pe, draw = red, fill=blue] (0, 0.25) -- (1, 0.25) -- (1, 0.5) -- (0, 0.5) -- cycle;

% What I want
\draw[pe, red] (0, 0.6) -- (1, 0.6);

% Not what I want
\draw[pe, red, fill=blue] (0, 0.75) -- (1, 0.75) -- (1, 1) -- (0, 1) -- cycle;

% What I want, simulated
\fill[pe, fill=blue] (0, 1.1) -- (1, 1.1) -- (1, 1.35) -- (0, 1.35) -- cycle;
\draw[pe, red] (0, 1.1) -- (1, 1.1) -- (1, 1.35) -- (0, 1.35) -- cycle;

\end{tikzpicture}
\end{document}

Best Answer

Update 2

You can make the color of the decoration a parameter of the style.

decoration={
    markings, mark=at position 0.8 with {%
        \fill[#1] (0, 0.5pt) -- ++ (-0.25, 0.075) -- ++ (0, -0.075) -- cycle;
    }

Just call this style pe=yellow, by default these style is red.

\documentclass[tikz, crop,border=1]{standalone}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}
\tikzset{
    pe/.style={
        %line width = 1pt,
        decoration={
            markings, mark=at position 0.8 with {%
                \fill[#1] (0, 0.5pt) -- ++ (-0.25, 0.075) -- ++ (0, -0.075) -- cycle;
            }
        },
        postaction=decorate
    },
    pe/.default=red
}
% Incorrect
\draw[pe=yellow, draw = red] (0, 0) -- (1, 0);

% Incorrect
\draw[pe=black, draw = red, fill=blue] (0, 0.25) -- (1, 0.25) -- (1, 0.5) -- (0, 0.5) -- cycle;

% Correct
\draw[pe, red] (0, 0.6) -- (1, 0.6);

% Incorrect
\draw[pe, red, fill=blue] (0, 0.75) -- (1, 0.75) -- (1, 1) -- (0, 1) -- cycle;

% Correct, simulated
\fill[pe, fill=blue] (0, 1.1) -- (1, 1.1) -- (1, 1.35) -- (0, 1.35) -- cycle;
\end{tikzpicture}
\end{document}

pe default

Update

If I understood correctly, you must specify the color in the decoration.

decoration={
            markings, mark=at position 0.8 with {%
                \fill[red] (0, 0.5pt) -- ++ (-0.25, 0.075) -- ++ (0, -0.075) -- cycle;
            }
        },

decoration

\documentclass[tikz, crop,border=1]{standalone}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}
\tikzset{
    pe/.style={
        %line width = 1pt,
        decoration={
            markings, mark=at position 0.8 with {%
                \fill[red] (0, 0.5pt) -- ++ (-0.25, 0.075) -- ++ (0, -0.075) -- cycle;
            }
        },
        postaction=decorate
    },
}

% Incorrect
\draw[pe, draw = red] (0, 0) -- (1, 0);

% Incorrect
\draw[pe, draw = red, fill=blue] (0, 0.25) -- (1, 0.25) -- (1, 0.5) -- (0, 0.5) -- cycle;

% Correct
\draw[pe, red] (0, 0.6) -- (1, 0.6);

% Incorrect
\draw[pe, red, fill=blue] (0, 0.75) -- (1, 0.75) -- (1, 1) -- (0, 1) -- cycle;

% Correct, simulated
\fill[pe, fill=blue] (0, 1.1) -- (1, 1.1) -- (1, 1.35) -- (0, 1.35) -- cycle;
\draw[pe, red] (0, 1.1) -- (1, 1.1) -- (1, 1.35) -- (0, 1.35) -- cycle;

\end{tikzpicture}
\end{document}

Simply by not specifying `draw=red`, but just `red`

    \draw[pe, red] (0, 0) -- (1, 0);

[![red][2]][2]

    \documentclass[tikz, crop,border=5mm]{standalone}
    \usetikzlibrary{decorations.markings}

    \begin{document}
    \begin{tikzpicture}
    \tikzset{
        pe/.style={
            decoration={
                markings, mark=at position 0.8 with {%
                    \fill (0, 0.5pt) -- ++ (-0.25, 0.075) -- ++ (0, -0.075) -- cycle;
                }
            },
            postaction=decorate
        }
    }
    \draw[pe, red] (0, 0) -- (1, 0);
    \end{tikzpicture}
    \end{document}
Related Question