tikzpictureの幅を設定する方法

tikzpictureの幅を設定する方法

次のような 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

ノードより左または右に要素がない場合は、その要素を設定して、境界ボックスに両側の線幅の半分が含まれないようにする TikZ 画像のオプションとして使用することrect1もできます。outer sep=0pttrim left=(rect1.west), trim right=(rect1.east)

コード

% !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}

出力

ここに画像の説明を入力してください

関連情報