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}

산출

여기에 이미지 설명을 입력하세요

관련 정보