1

Given the code below, the missing piece of the puzzle is to determine the width of the bounding box from within (which should be less cryptic than from outside).

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{backgrounds, calc}

\begin{document}

\newlength{\myl}

\begin{tikzpicture} \draw (-2,-1) rectangle (2,1); \begin{pgfonlayer}{background} \path[fill=red](current bounding box.south west)rectangle(current bounding box.north east); \end{pgfonlayer}

%\settowidth{\myl}{current bounbind box.width} % HOW TO? \coordinate (sw) at (current bounding box.south west); %\node[black,above right] at (sw) {\the\myl}; % (0pt)

\node[above right, text width=5em % text width=\myl ] (bottom) at (current bounding box.south west) {\color{black} %\makebox[\myl]{% A\hspace*{\fill}Z %} };

% UPDATE:

\path (bottom.north west) coordinate (SW); \node[black] (SW) {SW};

\end{tikzpicture}

\end{document}

enter image description here

UPDATE # 1:

Subsidiary question, I would have expected SW north-west of the bounding box of (A)--(Z): why is it so far off?

enter image description here

UPDATE # 2: Here's a clarification

  \node[above right,
  text width=5em
%  text width=\myl
  ] (bottom) at (current bounding box.south west) {\color{black}
    \frame{\makebox[5em]{A\hspace*{\fill}Z}}
    };

enter image description here

Erwann
  • 2,100

2 Answers2

0

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{backgrounds, calc, shapes, positioning}

\begin{document}

\newlength{\myl}

\begin{tikzpicture}
    \node[rectangle, draw, fill=red, inner sep=2.5cm,]at(0,0)(a){};
    \node at( a.south west) {\color{black}A};
    \node at($( a.south west)+(5cm,0cm)$) {\color{black}Z};

    % UPDATE:

    \path (a.north west) coordinate (SW);
    \node[black] at(SW.east) {SW};

\end{tikzpicture}

\end{document}

js bibra
  • 21,280
0

You can probably achieve such automatical streching by using anchors. There is no need to calculate the width of the rectangle then.

\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{backgrounds}

\begin{document}

\begin{tikzpicture} \draw (-2,-1) rectangle (2,1); \begin{pgfonlayer}{background} \path[fill=red] (current bounding box.south west) rectangle (current bounding box.north east); \end{pgfonlayer}

\node[text=black, anchor=south west] (bottoml) at (current bounding box.south west) {A}; \node[text=black, anchor=south east] (bottomr) at (current bounding box.south east) {Z};

\node[text=black, anchor=south west] at (bottoml.north west) {SW};

\end{tikzpicture}

\end{document}

enter image description here

By the way, I would probably first define the coordinates for the corners of the rectangle and then use them to draw the rectangle and place the nodes. You should consider whether you really need the backgrounds library here. The red rectangle with black border can easily be drawn with \draw[fill=red] (-2,-1) rectangle (2,1);.


Update:

If you really want to calculate the width of the rectangle, you can indeed use the cited approach:

\begin{tikzpicture}
  \draw (-2,-1) rectangle (2,1);
  \begin{pgfonlayer}{background}
    \path[fill=red] (current bounding box.south west) rectangle (current bounding box.north east);
  \end{pgfonlayer}

\newdimen\myl \pgfextractx{\myl}{\pgfpointdiff{\pgfpointanchor{current bounding box}{west}} {\pgfpointanchor{current bounding box}{east}}}

\node at (0,0) {\the\myl};

\node[text=black, draw, anchor=south west, text width={\myl-2\pgfkeysvalueof{/pgf/inner xsep}-2\pgflinewidth}] (bottoml) at (current bounding box.south west) {A \hfill Z};

\node[text=black, anchor=south west] at (bottoml.north west) {SW};

\end{tikzpicture}

enter image description here

The code stores the difference of the x values of the two coordinates current bounding box.west and current bounding box.east in a newly defined dimension macro (\myl). This is almost what you already had. However, you cannot directly use this, because you still need to subtract from this width the inner sep (padding) and the border width of the node to make it fit. The value of the (horizontal) inner sep, which can be assessed with \pgfkeysvalueof{/pgf/inner xsep}, as well as the value of the border, to be assessed with \pgflinewidth, need both to be multiplied by two, because the padding and the border are on either side of the node.

  • And finally, I somehow feel, that you could also achieve this using the fit library. – Jasper Habicht Mar 26 '21 at 10:04
  • I used rectangle for MWE's sake in defining the dimension of the bounding box but in fact I wanted something like this: \pgfdeclareimage[width=\textwidth]{img}{example-image-a} and in place of \draw (-2,-1)..., \node[ anchor=south west, inner sep=0, opacity=0.1] (img) at (0,0) {\pgfuseimage{img}};. Then imgbounding box, but not equal. In the end I want to save the pdf as an image, there should be no gap at the edges. Could you please make a suggestion? – Erwann Mar 26 '21 at 20:38
  • 1
    @Erwann: Try \node[inner sep=0, opacity=0.1] (img) at (0,0) {\pgfuseimage{img}}; without the anchor option. – Jasper Habicht Mar 26 '21 at 20:42
  • It worked, thanks. – Erwann Mar 26 '21 at 20:43