0

Consider the following MWE:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{latexcolors}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
    \begin{scope}[local bounding box = bbox]
        \draw[fill=blizzardblue] (-2,0) rectangle (2,0.3);
        \draw[fill=dimgray] (-1.5,0.3) rectangle (1.5,0.7);
        \draw[fill=blizzardblue] (-2,0.7) rectangle (2,1);
        \draw[pattern=north east lines, pattern color=coral, draw=coral] (-2.2,1) rectangle (2.2,1.5);
        \draw[fill=peach] (-2.3,0) -- (-2.3,2) -- (2.3,2) -- (2.3,0) -- (2.5,0) -- (2.5,2.2) -- (-2.5,2.2) -- (-2.5,0) -- cycle;
    \end{scope}

    \begin{scope}[x={(a.south east)},y={(a.north west)}]
    \draw (0,0) -- (1,1);
    \end{scope}
\end{tikzpicture}
\end{document}

First, a scope is created and some drawing is made in that scope. Also, I assign the scope a bounding box name (bbox).

Later, I create a second scope, and I want that scope's coordinate (0,0) to correspond with (bbox.south west), while (1,1) shall map to (bbox.north east).

The result which I expect is the following:

expected result

However, the result I actually get looks like this:

actual result

a) why is this so? for tikz-imagelabels I did it exactly like this, and there it works, why doesn't it work with a scope? see here, line 543. b) how can I fix that?

  • The points (0,0) and (1,1) use the same coordinate as before. This isn't like (rel axis cs: x,y) from pgfplots. OTOH, see page 140 of the PGF manual. – John Kormylo Sep 9 at 15:01
2

In your code, you define the x and y directions/lengths in the second scope using whole coordinates, not scalar lengths. (0,0) stays at the origin as you don't shift the scope, (1,1) is some weird combination of the coordinates of the two corners of the first bounding box. What you need to do is shifting your scope to bbox.south west and adjusting the x and y lengths using scalar values, i.e., the width and height of bbox.

You can use \pgfgetlastxy to extract the coordinates of a node. Using these, the following code should do the trick:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \begin{scope}[local bounding box=bbox]
        \draw[fill=cyan!30!white] (-2,0) rectangle (2,0.3);
        \draw[fill=red!30!white] (0,-1) rectangle (0.3,0.7);
    \end{scope}

    \path (bbox.south west);
    \pgfgetlastxy{\XA}{\YA};

    \path (bbox.north east);
    \pgfgetlastxy{\XB}{\YB};

    \begin{scope}[x=\XB-\XA, y=\YB-\YA, xshift=\XA, yshift=\YA]
    \draw[red] (0,0) -- (1,1);
    \end{scope}
\end{tikzpicture}
\end{document}
  • 1
    +1. If you load the calc library, you could also do \begin{scope}[shift={(bbox.south west)},x={($(bbox.south east)-(bbox.south west)$)},y={($(bbox.north west)-(bbox.south west)$)}] – Schrödinger's cat Sep 9 at 17:12
1

Is there special reason, that you looking for so complicated way to draw your image? If not, you can simplify your document example to:

\documentclass[tikz]{standalone}
\usepackage{latexcolors}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
    \begin{scope}[local bounding box=f]
\draw[fill=blizzardblue]    (-2,0)      rectangle (2,0.3);
\draw[fill=dimgray]         (-1.5,0.3)  rectangle (1.5,0.7);
\draw[fill=blizzardblue]    (-2,0.7)    rectangle (2,1);
\draw[pattern=north east lines,
      pattern color=coral, 
      draw=coral]           (-2.2,1)    rectangle (2.2,1.5);
\draw[fill=peach] (-2.3,0) |- (2.3,2) |- (2.5,0) |- (-2.5,2.2) |- cycle;
    \end{scope}
\coordinate (a) at (f.south west);
\coordinate (b) at (f.north east);
\draw[red] (a) -- (b);
% or
%\draw[red] (f.south west) -- (f.north east);
\end{tikzpicture}
\end{document}

enter image description here

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.