![如何设置 tikzpicture 的宽度](https://linux22.com/image/462317/%E5%A6%82%E4%BD%95%E8%AE%BE%E7%BD%AE%20tikzpicture%20%E7%9A%84%E5%AE%BD%E5%BA%A6.png)
我想要创建以下 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}