tikz ノードのカスタム シェーディング パターン

tikz ノードのカスタム シェーディング パターン

私は「通常の」方法とは異なるノードシェーディングを実現する方法を探しています

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[top color=red, bottom color=green] {};
\end{tikzpicture}
\end{document}

この例では、グラデーションがあります。つまり、中央の色が実際の上部と下部の色と異なります。これを変更して、中央の色を変えずに色を厳密に分離する (上部 = 赤、下部 = 緑) ようにしたり、グラデーションのプロパティを変更したりすることは可能ですか?

答え1

このような?

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[draw,
    append after command={
        \pgfextra
            \fill[green] (\tikzlastnode.south west) rectangle (\tikzlastnode.east);
            \fill[red] (\tikzlastnode.north west) rectangle (\tikzlastnode.east);
    \endpgfextra
    }] {A};
\end{tikzpicture}
\end{document}

ここに画像の説明を入力してください

アップデート

または、オプションを使用するとさらに簡単になりますpath picture:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{tikzpicture}
\node[draw, fill=red,
        path picture={\fill[green] (path picture bounding box.west) rectangle (path picture bounding box.south east);}] {A};
\end{tikzpicture}

\end{document}

関連情報