如何修复 TikZ 中的节点大小?

如何修复 TikZ 中的节点大小?

我只是在寻找一种方法来固定我的节点(块)的大小,而不管里面的文本是什么。我发现几篇帖子很相似,但并不相同。给出的所有解决方案都使用了minimum width=0.1cm行。但这不是我要找的解决方案。

我想要的是将一个块作为参考,并使所有其他块都与其大小相同。我尝试了以下方法,但失败了。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,positioning}
\begin{document}
\begin{tikzpicture}[block/.style={draw, fill=white, rectangle, minimum width=0.4*\columnwidth, anchor=south}, font=\small]
\node[block, minimum width=0.9*\columnwidth, minimum height = 5cm, fill=green, opacity=0.3, text opacity=1](sensors) at (0,0){};
\node[block, fill=white, minimum width=0.1cm, above right=0.1cm and 0.1cm of sensors.south west](acc){Accelerometer};
\node[block, fill=white, minimum width=\widthof{acc}, above right=0.1cm and 0.1cm of acc.north west](gyro){Gyroscope};
\node[block, fill=white, minimum width=\widthof{acc}, above right=0.1cm and 0.1cm of gyro.north west](mag){Magnetometer};
\end{tikzpicture}
\end{document}

在我的代码中,我希望该acc块成为参考块。

答案1

Acc 是一个节点名称,它不指代节点的内容,(Acc)而是\widthof{acc}测量包含文本的框acc。所以这行不通。

但是如果你记录了最长的文本,Magnetometer那么你就可以提供你想要的宽度(使用内部分隔符校正)并在你的风格中使用它

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,positioning}
\begin{document}
\begin{tikzpicture}[
block/.style={
draw,
fill=white,
rectangle, 
minimum width={width("Magnetometer")+2pt},
font=\small}]
\node[block](acc){Accelerometer};
\node[block,above=0.1cm of acc](gyro){Gyroscope};
\node[block,above=0.1cm of gyro](mag){Magnetometer};
\end{tikzpicture}
\end{document}

在此处输入图片描述

本网站的许多问题都证明了这一点,我不太明白为什么最小宽度不会让你满意,但正如上面所展示的那样,这是可能的。所以这两个是最基本的。你也可以更花哨一点,测量一些节点,并将其应用于它之后的其他节点,但我认为更改文本一次没有任何困难。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,positioning}
\begin{document}
\begin{tikzpicture}[
block/.style={
draw,
fill=white,
rectangle, 
text width={width("Magnetometer")},
align=center,
font=\small}]
\node[block](acc){Accelerometer};
\node[block,above=0.1cm of acc](gyro){Gyroscope};
\node[block,above=0.1cm of gyro](mag){Magnetometer};
\end{tikzpicture}
\end{document}

相关内容