
Я хочу создать следующую tikzpicture:
Я хочу, чтобы прямоугольник, охватывающий изображение-пример и узлы 2 и 3, был, скажем, 0.6\textwidth
широким.
Более того, я хочу, чтобы узлы 1 и 2 были смежными друг с другом, ноцентрированныйвнутри этого прямоугольника.
Я наткнулся наэтот ответиз 2012 года, где Питер Грилл говорит, что, по его мнению, не имеет смысла указывать высоту или ширину tikzpicture.
Я с этим в какой-то степени согласен, но я не хочу упаковывать свою tikzpicture в resizebox
.
Вот что у меня сейчас есть:
% !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}
Любые предложения будут очень приветствоваться.
решение1
Вы можете задать ограничивающую рамку, используя \path
затем размещать элементы относительно ее якорей. В этом случае только ширина имеет значение до конца.
\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}
Небольшое упрощение:
\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}
решение2
Я бы просто добавил
([xshift=.3\textwidth-.5\pgflinewidth]$(n2)!.5!(n3)$)
в соответствии (и, возможно,
([xshift=-.3\textwidth+.5\pgflinewidth]$(n2)!.5!(n3)$)
(чтобы быть в безопасности, если n1
он не находится .3\textwidth
(или находится дальше) от центра).
Я добавил несколько правил, чтобы показать, что середина n2
и n3
находится .3\textwidth
далеко от правой стороны.
Если дальше слева или справа от вашего rect1
узла нет элементов, вы также можете установить outer sep=0pt
для него и использовать trim left=(rect1.west), trim right=(rect1.east)
в качестве параметров изображение TikZ, которое заставит ограничивающую рамку не включать половину ширины линии с обеих сторон.
Код
% !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}