![即使文本大小发生变化,如何固定矩形大小?](https://linux22.com/image/469959/%E5%8D%B3%E4%BD%BF%E6%96%87%E6%9C%AC%E5%A4%A7%E5%B0%8F%E5%8F%91%E7%94%9F%E5%8F%98%E5%8C%96%EF%BC%8C%E5%A6%82%E4%BD%95%E5%9B%BA%E5%AE%9A%E7%9F%A9%E5%BD%A2%E5%A4%A7%E5%B0%8F%EF%BC%9F.png)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.geometric}
\tikzstyle{arrow} = [thick,->,>=stealth]
\begin{document}
\begin{tikzpicture}[node distance=10pt]
\node[draw, rounded corners, align=center, fill=blue!20, fill opacity=0.6, minimum width=2cm,minimum height=1cm] (start) {\textbf{unknown}};
\node[draw, below=of start, align=center, fill=blue!40, fill opacity=0.7, minimum width=3cm,minimum height=1cm] (step 1) {\textbf{My name in Alice}\\$A+B$};
\node[draw, below=of step 1, align=center, fill=blue!40, fill opacity=0.7, minimum width=3cm,minimum height=1cm] (step 2) {\textbf{What about you}};
\node[draw, below=of step 2, align=center, fill=blue!40, fill opacity=0.7, minimum width=3cm,minimum height=1cm] (step 3) {\textbf{Bob}\\ $A+B+C+D$};
\node[draw, rounded corners, fill=blue!40, fill opacity=0.7, below=20pt of step 3] (end) {End};
\draw[arrow] (start) -- (step 1);
\draw[arrow] (step 1) -- (step 2);
\draw[arrow] (step 2) -- (step 3);
\draw[arrow] (step 3) -- (end);
\end{tikzpicture}
\end{document}
您可以看到中间三个矩形的尺寸不同,尽管我已经设置了“最小宽度=2cm,最小高度=1cm”,但是这不起作用,我怎样才能将这些矩形的尺寸固定为相同大小?
答案1
- 在节点中,
ìnner sep
最初是.3333em
(文档pgfmanual
- 长度
\textbf{My name in Alice}
超过3厘米 - 最小尺寸为:长度
\textbf{My name in Alice}
+2x.3333em
评论:
\tikzstyle
已折旧- 如果不透明度没有用,我们可以
fill=blue!40,fill opacity=0.7
用(测试后) 代替fill=blue!29
代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
%\tikzstyle{arrow} = [thick,->,>=stealth]
\tikzset{
every node/.style={
draw,
align=center,
minimum height=1cm
},
start/.style={
rounded corners,
% fill=blue!30,
%fill opacity=0.6,
fill=blue!19,
minimum width=2cm,
},
rect/.style={
% fill=blue!40,
% fill opacity=0.7,
fill=blue!29,
minimum width=3.42cm,
},
arrow/.style={
thick,
-stealth,
}
}
\begin{document}
\ExplSyntaxOn
\NewDocumentCommand{\myconversion}{m}{%
\dim_to_decimal_in_unit:nn { #1 } { 1cm }
}
\ExplSyntaxOff
\newlength{\mylength}
\settowidth \mylength {\textbf{My name in Alice}}
\verb| length of \textbf{My name in Alice} | \myconversion{\the\mylength}
\addtolength \mylength {0.6666em}% the initially inner sep
\verb| length of minimum width |\myconversion{\the\mylength}
\begin{tikzpicture}[node distance=10pt]
\node[start] (start) {\textbf{unknown}};
\node[rect, below=of start] (step 1) {\textbf{My name in Alice}\\$A+B$};
\node[rect, below=of step 1,] (step 2) {\textbf{What about you}};
\node[rect, below=of step 2] (step 3) {\textbf{Bob}\\ $A+B+C+D$};
\node[rect,below=20pt of step 3] (end) {End};
\draw[arrow] (start) -- (step 1);
\draw[arrow] (step 1) -- (step 2);
\draw[arrow] (step 2) -- (step 3);
\draw[arrow] (step 3) -- (end);
\end{tikzpicture}
% test color
% \begin{tikzpicture}[thick]
% \filldraw[fill=blue!40,fill opacity=0.7] (0:1cm) circle (12mm);
% \filldraw[fill=blue!29] (0:2cm) circle (12mm);
% \end{tikzpicture}
\end{document}
答案2
代码(修复矩形相同的大小):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.geometric}
\tikzstyle{arrow} = [thick,->,>=stealth]
\begin{document}
\begin{tikzpicture}[node distance=10pt]
\node[draw, rounded corners, align=center, fill=blue!20, fill opacity=0.6, minimum width=2cm,minimum height=1cm] (start) {\textbf{unknown}};
\node[draw, below=of start, align=center, fill=blue!40, fill opacity=0.7, minimum width=4cm,minimum height=1.5cm] (step 1) {\textbf{My name in Alice}\\$A+B$};
\node[draw, below=of step 1, align=center, fill=blue!40, fill opacity=0.7, minimum width=4cm,minimum height=1.5cm] (step 2) {\textbf{What about you}};
\node[draw, below=of step 2, align=center, fill=blue!40, fill opacity=0.7, minimum width=4cm,minimum height=1.5cm] (step 3) {\textbf{Bob}\\ $A+B+C+D$};
\node[draw, rounded corners, fill=blue!40, fill opacity=0.7, below=20pt of step 3] (end) {End};
\draw[arrow] (start) -- (step 1);
\draw[arrow] (step 1) -- (step 2);
\draw[arrow] (step 2) -- (step 3);
\draw[arrow] (step 3) -- (end);
\end{tikzpicture}
\end{document}