TransWikia.com

Change the layer of `show background rectangle`

TeX - LaTeX Asked on August 30, 2021

The backgrounds tikz library defines framed (a.k.a. show background rectangle) which draws a background rectangle.

Now, when we have a picture that involves different layers (say, a background and a main layer), we wouldd like the frame of the picture to be drawn on the deeper layer (background in our case), or even deeper. However, it seems that the frame is shown on the main layer.

This MWE contains a single node on the background layer in a framed picture. The background rectangle fills the main layer, and therefore the node is not visible anymore.

documentclass{article}

usepackage{tikz}
usetikzlibrary{backgrounds}

tikzset{background rectangle/.style={fill=blue!5}}
pgfdeclarelayer{background}
pgfsetlayers{background, main}

begin{document}

begin{tikzpicture}[framed]
    begin{pgfonlayer}{background}
    node {Hello world};
    end{pgfonlayer}
end{tikzpicture}
end{document}

The tikz picture appears as a single filled rectangle, since the node is behind the background rectangle.


Note: I know that I could reorder the layers to let the main at the bottom (like main, background, mymain), but if I can avoid to surround the important parts of the picture with a mymain layer, that would be better.

One Answer

(I'd kind of taken it for granted that the backgrounds library defines a layer called background ...) You are entirely correct that the library defines a background layer, the first thing it does is

pgfdeclarelayer{background}%
pgfsetlayers{background,main}%

So for your case it might make more sense to declare a middle layer, and do pgfsetlayers{background,middle, main}.

enter image description here

documentclass[border=5mm]{standalone}

usepackage{tikz}
usetikzlibrary{backgrounds}

tikzset{background rectangle/.style={fill=blue!25}}
pgfdeclarelayer{middle}
pgfsetlayers{background,middle, main}

begin{document}

begin{tikzpicture}[framed]
    node [fill=blue] {hello};
    begin{pgfonlayer}{middle}
    node {Hello world};
    end{pgfonlayer}
end{tikzpicture}
end{document}

Correct answer by Torbjørn T. on August 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP