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}

相關內容