TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to design a tikz shape that uses different background paths for different purposes.

Consider the following minimal example where a new shape named simple rectangle is declared. The background path used for filling the shape is different from the background path for other purposes, like drawing or clipping: filling does not include the region of the text below the base line.



\pgfdeclareshape{simple rectangle}{
    \pgf@x=.5\wd\pgfnodeparttextbox % widht of the box
    \pgf@y=.5\ht\pgfnodeparttextbox % height of the box, ignoring the depth
  \anchor{north east}{
      \advance \pgf@ya by -\depth



  \node at (0,3) [simple rectangle]{This is my test.};
  \node at (0,2) [simple rectangle,fill=yellow!40]{This is my test.};
  \node at (0,1) [simple rectangle,draw]{This is my test.};
  \node at (0,0) [simple rectangle,fill=yellow!40,draw]{This is my test.};


different paths for background

This example works only if filling is not combined with other actions, like drawing, as can be seen.

How to fix this?

share|improve this question
To clarify: in the last node, if you put your draw option into a postaction={draw} the draw will follow the simple rectangle border. Is this what you want to achieve? – domenico Nov 3 '13 at 17:08
Well, no surprise, you subtract \depth from \pgf@ya when you draw the path (\iftikz@mode@draw) and in both last two examples you draw the path. If you want to use different paths for drawing and filling you can use \behindbackgroundpath for the filling. (I also wouldn’t include TikZ stuff in the background path.) – Qrrbrbirlbel Nov 3 '13 at 17:09
@Qrrbrbirlbel, I would like to see a not too complex example of a shape that uses \behindbackgroundpath, but I am not finding one. It seems to be rarely used. I want to use different paths for drawing and filling, but without the need of using different commands or options when using the shape in a tikzpictue. That is, the document body should remain the same. For instance, the filling colour should be one specified by the fill option. How the shape should be fixed then? I am hoping someone could show the fix, or how to fix in more details. – Romildo Nov 3 '13 at 17:50

This is a simplification, but once you get to filling and drawing a path, the path is essentially fixed: the "shape" of the path is independent of the parameters (e.g., line width, dash pattern) or the actions applied (e.g., filling or drawing, and so on).

So creating different paths depending on whether the action is a fill or draw, isn't supported, would (I think) be a nightmare to implement in the general case, and I am not sure it would be logical to do so.

But the behind background path is pretty easy to illustrate:


  simple rectangle fill/.store in=\pgf@lib@sh@simplerectangle@fill,
  simple rectangle fill=}

\pgfdeclareshape{simple rectangle}{
  \saveddimen{\innerxsep}{\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/inner xsep}}}
  \saveddimen{\innerysep}{\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/inner ysep}}}
    % Only set the behind background path if
    % \pgf@lib@sh@simplerectangle@fill is not empty 


\node [simple rectangle, draw] at (0,0) {Test 1};

\node [simple rectangle, draw, simple rectangle fill=red!20] at (0,-1) {Test 2};


enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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