0

I want to create the following tikzpicture:

enter image description here

I want the rectangle that fits around the example-image and nodes 2 and 3 to be, say, 0.6\textwidth wide.

Moreover, I want nodes 1 and 2 to be one adjacent to the other but centered inside this rectangle.

I came across this answer from 2012 where Peter Grill says that in his opinion it doesn't make sense to specify the height or width of a tikzpicture.

I agree with that to some extent but I don't want to wrap my tikzpicture inside a resizebox.

What I have now is this:

% !TEX TS-program = pdflatex
\documentclass{article}
\usepackage{tikz,lipsum}
\usetikzlibrary{positioning,fit}

\begin{document} \lipsum[1] \vskip 10pt

\noindent \begin{tikzpicture} \node(n0) [draw=black,outer sep=0pt] {node 1}; \node (n1) [below right = 1pt and 0pt of n0.south west,outer sep=0pt] {\includegraphics[width=0.3\textwidth]{example-image}}; \node (n2) [below right = 10pt and 20pt of n1,draw=black] {node 2}; \node (n3) [right = 1pt of n2,draw=black] {node 3}; \node (n4) [below left = 0pt and 130pt of n3.south west,outer sep=0pt,draw=black] {node 4};

%draw a rectangle as the border \nodedraw, fit=(n1) (n3),inner sep=0pt {}; \end{tikzpicture} \end{document}

enter image description here

Any suggestion would be very welcome.

tush
  • 1,115
  • Do you actually need the drawn rectangle with width .6\textwidth? Or do you just want the picture to have a specific width centered around a point? What about the drawn line? It will add half the line width on both sides to the final width of the TikZ picture. – Qrrbrbirlbel Mar 14 '23 at 09:32
  • @Qrrbrbirlbel Thanks. I want the drawn rectangle to be .6\textwidth; Well yes, I forgot this small detail about the addition of half the line width on both sides. Then I guess I can make the drawn rectangle to be 0.6textwidth minus edge width or something similar. – tush Mar 14 '23 at 10:02
  • I'm still a bit confused. What should happen when the left side of the n1 node is further away from the center of n2 and n3 than .3\textwidth? – Qrrbrbirlbel Mar 14 '23 at 13:59
  • Then the tikzpicture would occupy on the page a width of this "further away distance" +.6\textwidth. – tush Mar 14 '23 at 14:04

2 Answers2

5

You can set the bounding box using \path then place items relative to its anchors. In this case, only the width matters until the end.

\documentclass{article}
\usepackage{tikz,lipsum}
\usetikzlibrary{positioning,fit}

\begin{document} \lipsum[1] \vskip 10pt

\noindent \begin{tikzpicture} \path (0,0) (0.6\textwidth,0);% set bounding box \node(n0) [draw=black,outer sep=0pt, below right] at (current bounding box.west) {node 1}; \node (n1) [below right = 1pt and 0pt of n0.south west,outer sep=0pt] {\includegraphics[width=0.3\textwidth]{example-image}}; \node (n2) [below left=1pt, draw=black] at (current bounding box.center |- n1.south) {node 2}; \node (n3) [right = 1pt of n2,draw=black] {node 3}; \coordinate (n4) at (current bounding box.south east);% set other corner

%draw a rectangle as the border \nodedraw, fit=(n1) (n4),inner sep=0pt {}; \end{tikzpicture} \end{document}


Slight simplification:

\documentclass{article}
\usepackage{tikz,lipsum}
\usetikzlibrary{positioning}

\begin{document} \lipsum[1] \vskip 10pt

\noindent \begin{tikzpicture} \path (0,0) (0.6\textwidth,0);% set bounding box \node(n0) [draw=black,outer sep=0pt, below right] at (current bounding box.west) {node 1}; \node (n1) [below right = 1pt and 0pt of n0.south west,outer sep=0pt] {\includegraphics[width=0.3\textwidth]{example-image}}; \node (n2) [below left=1pt, draw=black] at (current bounding box.center |- n1.south) {node 2}; \node (n3) [right = 1pt of n2,draw=black] {node 3};

%draw a rectangle as the border \draw (n1.north west) rectangle (current bounding box.south east); \end{tikzpicture} \end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • to have the same vertical space between node1 and the rectangle, and node3 and the rectangle, the path must be lowered by one point. \path ($(n3.south)+(0,-1pt)$); – pascal974 Mar 14 '23 at 05:36
  • Very nice. I don't understand what is the meaning of \path (coord1) (coord2);. There is no -- between the two coordinates so it is not a line yet tikz sees it as a bounding box. Can you please briefly explain it verbally? Moreover, what are the conditions for which a path becomes a current bounding box? How do I know if I can use something as a current bounding box? – tush Mar 16 '23 at 13:36
  • The (current bounding box) is a rectangular node fitting everything in the tikzpicture up to now, even invisible points from \path. See also \useasboundingbox (page 184) and local bounding box=name (page 1143). – John Kormylo Mar 16 '23 at 16:02
  • \path[use as bounding box] (0,0) (.6\textwidth,0); is equivalent. – tush Mar 16 '23 at 19:10
1

I would just add

([xshift=.3\textwidth-.5\pgflinewidth]$(n2)!.5!(n3)$)

to the fit (and possibly

([xshift=-.3\textwidth+.5\pgflinewidth]$(n2)!.5!(n3)$)

to be on the safe side if n1 is not .3\textwidth (or further) away from the center).

I've added a few rules to show that the middle of n2 and n3 is .3\textwidth away from the right side.

If no element is further to the left or right of your rect1 node, you can also set outer sep=0pt for it and use trim left=(rect1.west), trim right=(rect1.east) as options for the TikZ picture which makes the bounding box not include half the line width on both sides.

Code

% !TEX TS-program = pdflatex
\documentclass{article}
\usepackage{tikz, lipsum}
\usetikzlibrary{calc, fit, positioning}
\begin{document}
\lipsum[1]
\vskip 10pt

\noindent \begin{tikzpicture} \node (n0) [draw=black, outer sep=0pt] {node 1}; \node (n1) [below right = 1pt and 0pt of n0.south west, outer sep=0pt] {\includegraphics[width=0.3\textwidth]{example-image}}; \node (n2) [below right = 10pt and 20pt of n1, draw=black] {node 2}; \node (n3) [right = 1pt of n2, draw=black] {node 3}; \node (n4) [below left = 0pt and 130pt of n3.south west, outer sep=0pt, draw=black] {node 4};

%draw a rectangle as the border \nodedraw, fit={(n1)(n3)([xshift=.3\textwidth-.5\pgflinewidth]$(n2)!.5!(n3)$)}, inner sep=0pt {}; \end{tikzpicture}% \llap{\rule{.6\textwidth}{1pt}}% \llap{\clap{\rule[2pt]{.1pt}{2em}}\rule[2pt]{.3\textwidth}{.5pt}} \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821