
Quiero crear la siguiente imagen tikz:
Quiero que el rectángulo que se ajusta alrededor de la imagen de ejemplo y los nodos 2 y 3 sea, digamos, 0.6\textwidth
ancho.
Además, quiero que los nodos 1 y 2 sean uno adyacente al otro, perocentradodentro de este rectángulo.
Me encontré conesta respuestade 2012 donde Peter Grill dice que, en su opinión, no tiene sentido especificar la altura o el ancho de una imagen tikz.
Estoy de acuerdo con eso hasta cierto punto, pero no quiero envolver mi tikzpicture dentro de un archivo resizebox
.
Lo que tengo ahora es esto:
% !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}
Cualquier sugerencia será muy bienvenida.
Respuesta1
Puede configurar el cuadro delimitador y \path
luego colocar elementos en relación con sus anclajes. En este caso, hasta el final solo importa el ancho.
\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}
Ligera simplificación:
\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}
Respuesta2
yo solo agregaría
([xshift=.3\textwidth-.5\pgflinewidth]$(n2)!.5!(n3)$)
al ajuste (y posiblemente
([xshift=-.3\textwidth+.5\pgflinewidth]$(n2)!.5!(n3)$)
para estar seguro si n1
no está .3\textwidth
(o más) lejos del centro).
Agregué algunas reglas para mostrar que el medio de n2
y n3
está .3\textwidth
alejado del lado derecho.
Si ningún elemento está más a la izquierda o derecha de su rect1
nodo, también puede configurarlo outer sep=0pt
y usarlo trim left=(rect1.west), trim right=(rect1.east)
como opciones para la imagen de TikZ, lo que hace que el cuadro delimitador no incluya la mitad del ancho de línea en ambos 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}