TikZ 使用绘制颜色填充

TikZ 使用绘制颜色填充

以下代码使用装饰器将箭头尖端添加到边缘。从逻辑上讲,尖端是边缘的一部分,因此我希望它用线条颜色着色。但是,由于尖端是使用渲染的\fill,因此它从fill参数中获取颜色。我怎样才能使颜色响应draw=red,同时又不响应fill=blue

在此处输入图片描述

\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}

答案1

更新 2

您可以将装饰的颜色作为风格的参数。

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

只需调用此样式pe=yellow,默认情况下这些样式是红色的。

\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}

默认

更新

如果我理解正确的话,您必须在装饰中指定颜色。

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

装饰

\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}

答案2

只需为箭头定义一种样式并进行相应的设置。

\documentclass[tikz, crop,border=1]{standalone}
\usetikzlibrary{decorations.markings, decorations.pathreplacing}
\tikzset{pearrow/.style={fill},
peset/.code={\tikzset{pearrow/.style={#1}}}}
\newcommand{\drawArrow}[2]{
    \draw[
        decoration={
            markings,
            mark=at position 0.8 with {%
                \path[pearrow] (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
    }
}


% with peset
\draw[pe, red, fill=blue,peset={fill=red}] (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}

在此处输入图片描述

答案3

以下findArrowColor风格基于这是 Georg Sievelson 的回答,如果可用则选择当前描边颜色,如果可用则选择当前文本颜色,最后选择黑色。

在此处输入图片描述

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

\usepackage{ifthen}

\makeatletter
\tikzset{
  /tikz/findArrowColor/.style={/utils/exec={%
    \ifthenelse{\equal{\tikz@strokecolor}{}}%
    {%
        \ifthenelse{\equal{\tikz@textcolor}{}}%
        {%
            \colorlet{#1}{black}%
        }%
        {\colorlet{#1}{\tikz@textcolor}}%
    }%
    {%
        \colorlet{#1}{\tikz@strokecolor}%
    }%
  }}
}
\makeatother

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

\begin{document}
\begin{tikzpicture}[scale=2]

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

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

\fill[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}

相关内容