Como definir a largura do tikzpicture

Como definir a largura do tikzpicture

Quero criar a seguinte imagem tikz:

insira a descrição da imagem aqui

Quero que o retângulo que se ajusta à imagem de exemplo e aos nós 2 e 3 seja, digamos, 0.6\textwidthlargo.

Além disso, quero que os nós 1 e 2 sejam adjacentes um ao outro, mascentradodentro deste retângulo.

eu me depareiesta respostade 2012 onde Peter Grill diz que na sua opinião não faz sentido especificar a altura ou largura de uma imagem tikz.

Concordo com isso até certo ponto, mas não quero agrupar meu tikzpicture dentro de um arquivo resizebox.

O que tenho agora é isto:

% !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
\node[draw, fit=(n1) (n3),inner sep=0pt](rect1) {};
\end{tikzpicture}
\end{document}

insira a descrição da imagem aqui

Qualquer sugestão seria muito bem-vinda.

Responder1

Você pode definir a caixa delimitadora \pathe colocar os itens em relação às suas âncoras. Neste caso, apenas a largura importa até o final.

\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
\node[draw, fit=(n1) (n4),inner sep=0pt](rect1) {};
\end{tikzpicture}
\end{document}

Ligeira simplificação:

\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}

Responder2

Eu apenas adicionaria

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

para o ajuste (e possivelmente

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

estar do lado seguro se n1não estiver .3\textwidth(ou mais) longe do centro).

Eu adicionei algumas regras para mostrar que o meio n2e n3está .3\textwidthlonge do lado direito.

Se nenhum elemento estiver mais à esquerda ou à direita do seu rect1nó, você também pode configurá outer sep=0pt-lo e usá-lo trim left=(rect1.west), trim right=(rect1.east)como opções para a imagem TikZ, o que faz com que a caixa delimitadora não inclua metade da largura da linha em ambos os lados.

Código

% !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
\node[draw,
      fit={(n1)(n3)([xshift=.3\textwidth-.5\pgflinewidth]$(n2)!.5!(n3)$)},
      inner sep=0pt](rect1) {};
\end{tikzpicture}%
\llap{\rule{.6\textwidth}{1pt}}%
\llap{\clap{\rule[2pt]{.1pt}{2em}}\rule[2pt]{.3\textwidth}{.5pt}}
\end{document}

Saída

insira a descrição da imagem aqui

informação relacionada