I have two pgf fitting nodes:
- one below the other,
- left aligned,
- with different (unpredictable) widths,
and I'd like them be backgrounded, with backgrounds of the same width.
For this, I got help from Creating a node fitting the horizontal width of two other nodes but I encounter a trouble pointed out by the following MWE: the background of the second node with minimum width equal to the first one's is horizontally shifted.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,backgrounds,fit}
\begin{document}
\tikzstyle{every node}=[font=\ttfamily]
\begin{tikzpicture}
\node (n11) {x};
\node (n12) [right=of n11,xshift= 5mm] {x};
\node (n21) [below=of n11,yshift=10mm] {x};
\node (n22) [right=of n21] {x};
\begin{pgfonlayer}{background}
\node[
fill=red!20,
inner sep=0pt,
fit=(n11) (n12)
] (n1) {};
\path let
\p1=(n1.west),
\p2=(n1.east)
in node [
fill=blue!20,
inner sep=0pt,
fit=(n21) (n22),
minimum width=\x2-\x1-\pgflinewidth
] (n2) {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
Here is the output:

What's going on?

n2, you may usefit=(n21) (n22) (n1 |- n21) (n1 |- n21)withoutminimum width(and addline width=0ton1defintion). – Paul Gaborit Oct 17 '14 at 05:51