# Tikz: How to shade a node

I have made a graph with TikZ and now I would like to apply a shading to some of the nodes but I can't seem to find out how to do that.

This doesn't work:

\documentclass{article}
\usepackage{tikz}

\tikzset{every node/.append style={minimum size=.7cm, draw,circle,font=\sffamily\Large\bfseries,inner sep=0.05cm}}

\begin{document}

\node                                 (x1) {$x_1$};
\node[node distance=3cm,right of=x1]  (x2) {$x_2$};
\node[node distance=3cm,right of=x2]  (x3) {$x_3$};

\node[node distance=1cm,below of=x1]  (1) [sh] {};
\node[node distance=1cm,below of=x2]  (2) [sh] {};
\node[node distance=1cm,below of=x3]  (3) [sh] {};
\node[node distance=1.5cm,below of=1] (4) {};
\node[node distance=1.5cm,below of=2] (5) {};
\node[node distance=1.5cm,below of=3] (6) {};
\foreach \from/\to in {x1/1,x2/2,x3/3,1/4,2/5,3/6,4/5,5/6} \draw (\from) -- (\to);
\end{tikzpicture}

\end{document}

-

## migrated from stackoverflow.comJun 17 '12 at 15:55

This question came from our site for professional and enthusiast programmers.

I assume by "this doesn't work", you mean that your nodes get shaded, but the parameter shading angle=45 is ignored? To get a rotated linear shading, you'll have to use top color and bottom color instead of left color and right color, and the shading angle parameter needs to be provided last. –  Jake Jun 17 '12 at 15:58
Welcome to TeX.sx! Your question was migrated here from another stackexchange site. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Marco Daniel Jun 17 '12 at 15:59
@Jake: I assume this is an answer ;-) –  Marco Daniel Jun 17 '12 at 16:00
@Jake why change to top and bottom? left and right will also work, provided shading angle is used last. –  Gonzalo Medina Jun 17 '12 at 16:21
@GonzaloMedina: You're right! (Makes sense, too) –  Jake Jun 17 '12 at 16:24

As noted in the comments, the shading angle needs to be the last definition.

In the example I've used a matrix of math nodes just to show a different way to achieve the same goal:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{
mymx/.style={ matrix of math nodes, nodes in empty cells,
row sep=3mm, column sep=2cm,
nodes={ minimum size=.7cm, draw, circle,
font=\sffamily\Large\bfseries, inner sep=0.05cm } },
sh/.style={ shade, shading=axis, left color=red, right color=green,
shading angle=45 } }

\begin{document}

\begin{tikzpicture}
\matrix[mymx,row 2/.style={every node/.append style=sh}] (mx) {
x_1 & x_2 & x_3 \\
&&\\[5mm]
&&\\
};
\draw (mx-1-1) -- (mx-2-1) -- (mx-3-1) -- (mx-3-2) -- (mx-3-3) -- (mx-2-3) -- (mx-1-3)
(mx-3-2) -- (mx-2-2) -- (mx-1-2) ;
\end{tikzpicture}

\end{document}


-