10

I want to use some background color on my node, but not for the whole node. Just for 75% of the node.

My code is this:

\documentclass[tikz,margin=5mm]{standalone}
\begin{document}
\tikz\node[draw, fill=gray!30]{Some Text};
\end{document}

Which produces this:

But I want more something like this:

Edit: The white and the grey part should be reversed in the second image.

21

You can use path picture and path picture bounding box:

\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\tikz
\node[draw]
  [path picture={
    \fill[gray!30](path picture bounding box.north west)rectangle
        ($(path picture bounding box.north east)!.75!(path picture bounding box.south east)$);
  }](n)
  {Some Text}
;
\end{document}

enter image description here

This works also if the node is a circle:

enter image description here

or a cloud (options: cloud,cloud ignores aspect, needs library shapes.symbols):

enter image description here

Only if the node is a rectangle, you can use the node name instead path picture bounding box:

\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\tikz
\node[draw]
  [path picture={
    \fill[gray!30](n.north west)rectangle($(n.north east)!.75!(n.south east)$);
  }](n)
  {Some Text}
;
\end{document}

Update (because of a comment)

If the background should be colored in an intervall, eg. from 0.3 to 0.8, use

($(<name>.north west)!.3!(<name>.south west)$)
rectangle
($(<name>.north east)!.8!(<name>.south east)$);

Replace <name> by the node name or path picture bounding box.

enter image description here

Code:

\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\tikz
\node[draw]
  [path picture={
    \fill[gray!30]
      ($(path picture bounding box.north west)!.3!(path picture bounding box.south west)$)
      rectangle
      ($(path picture bounding box.north east)!.8!(path picture bounding box.south east)$);
  }](n)
  {Some Text}
;
\end{document}
  • Thanks for the answer, thats it! How can i define an specific intervall? i.e. 'just color the background from 0.8 till 0.3 gray' – ipo May 10 '17 at 5:11
  • 1
    @ipo See the example at the end of my updated answer. – esdd May 10 '17 at 8:38
13

Doing the fill in a separate step might be an option.

enter image description here

\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{calc,backgrounds,scopes}
\begin{document}
\begin{tikzpicture}
\node[draw] (a) {Some Text};
{ [on background layer]
\fill[gray!30] (a.north west) rectangle ($(a.south east)!0.3!(a.north east)$);
}
\end{tikzpicture}
\end{document}
  • Oops, that 0.3 should of course be 0.25. – Torbjørn T. May 9 '17 at 16:29
8

I propose something similar: shading

\documentclass[tikz,margin=5mm]{standalone}
\begin{document}
\tikz\node[draw, top color=gray!30,bottom color=white]{Some Text};
\end{document}

enter image description here

EDIT

Since it was requested, here is a sample with a fading; I set the background as orange, in order to make the effect apparent

\documentclass{article}
\usepackage{pagecolor}
\usepackage{tikz}
\pagecolor{yellow!30!orange}
\usetikzlibrary{fadings} 
\begin{document}

\tikzfading[name=fade down,
top color=transparent!0,
bottom color=transparent!100]

\begin{tikzpicture}[path fading=fade down]
\node[draw, fill=gray!30,path fading]{Some Text};
\end{tikzpicture}
\end{document}

With result:

enter image description here

  • Is there also a way to make the color transparent? And i don't mean white. i.e. top color=white!30,bottom color=none – ipo May 9 '17 at 18:39
  • @ipo you should look into fadings, I give only an example – Moriambar May 9 '17 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.