如何設定tikz圖片的寬度

如何設定tikz圖片的寬度

我想創建以下 tikzpicture:

在此輸入影像描述

我希望適合範例圖像以及節點 2 和 3 的矩形是0.6\textwidth寬的。

此外,我希望節點 1 和 2 彼此相鄰,但是居中在這個矩形內。

我碰到這個答案從 2012 年開始,Peter Grill 表示,在他看來,指定 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}

輸出

在此輸入影像描述

相關內容