我们可以定义节点的最大宽度吗?

我们可以定义节点的最大宽度吗?

我的问题几乎与此相反:这个问题。

我已经定义了一个块:

\begin{tikzpicture}[node distance = 2cm, auto,->=stealth,point/.style= 
                   {circle,fill=red,minimum size=0pt,inner sep=0pt}]
\tikzstyle{block} = [rectangle, draw,thick,fill=blue!0,
    text centered, rounded corners, minimum height=1em]
\node [block] (start) {Start};
\node [block,below of=start] (start) {Start and blah blah blah blah blah blah
                       blah blah blah blah blah blah blah blah blah
                       blah blah blah blah blah blah blah blah blah
                       blah blah blah blah blah blah blah blah blah};
\end{tikzpicture}

该块会自行调整以适应文本。

在此处输入图片描述

但我想通过使用来控制块的宽度text width=15em

\tikzstyle{block} = [rectangle, draw,thick,fill=blue!0,text width=20em,<---------= 
             text centered, rounded corners, minimum height=1em]

我明白了:

在此处输入图片描述

虽然第二个块看起来不错,但第一个块(start)中有很多空白空间。我的问题是

如何根据文本调整块的宽度,同时将块的最大宽度限制为(比如说)15em?

换句话说,如何让第一个块适合start?或者一般来说,

如何自动调整块大小以适应文本,块的最大宽度为 15em?

请注意:我知道我可以定义block2一个合适的宽度并放入start其中。但我的限制是使用单一的 tikzstyle。

答案1

我猜没有 TikZ 方法,但你可以使用该varwidth包:

\documentclass{article}

\usepackage{tikz}
\usepackage{varwidth}

\begin{document}
\begin{tikzpicture}[node distance = 2cm, auto,->=stealth,point/.style= 
                   {circle,fill=red,minimum size=0pt,inner sep=0pt}]
\tikzstyle{block} = [rectangle, draw,thick,fill=blue!0,
    text centered, rounded corners, minimum height=1em]
\node [block] (start) {\begin{varwidth}{15em}Start\end{varwidth}};
\node [block,below of=start] (start) {%
   \begin{varwidth}{15em}
      Start and blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
    \end{varwidth}};
\end{tikzpicture}
\end{document}

结果

您可以将{varwidth}环境包装在一个更短的宏中……

更新

可以使用execute at begin/end node包含{varwidth}在样式定义中:

\begin{tikzpicture}[node distance = 2cm, auto,->=stealth]
\tikzstyle{block} = [%
   rectangle, draw,thick,fill=blue!0,
   text centered, rounded corners, minimum height=1em,
   execute at begin node={\begin{varwidth}{15em}},
   execute at end node={\end{varwidth}}]
\node [block] (start) {%
      Start
};
\node [block,below of=start] (start) {%
      Start and blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
};
\end{tikzpicture}

答案2

我只是想扩充 Tobi 的关于的回答execute at begin/end node,并将该部分放入自己的风格中,并对宽度进行论证。

\tikzset{
    max width/.style args={#1}{
        execute at begin node={\begin{varwidth}{#1}},
        execute at end node={\end{varwidth}}
    }
}
\begin{tikzpicture}
    \node[max width=3cm] {this node is maximum 3cm wide although its text is wider};
    \node[max width=10cm] {shrinks to textwidth};
\end{tikzpicture}

不要忘记事先加载 varwidth 包:\usepackage{varwidth}

答案3

这是我maximum text width基于以下实现的一个密钥daniatic 的回答下图和 MWE 展示了它如何与text width和交互minimum width,以及多个链式样式定义如何覆盖它。

欢迎提出改进建议。例如,目前多线节点的宽度是准随机的,因为它使用最长线的自然宽度而不拉伸。我想让所有长度超过一条线的节点都具有完全相同的宽度,例如text width

示例代码的输出

示例代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{varwidth}

\makeatletter
\newif\iftikz@maximum@text@width@hook@installed \tikz@maximum@text@width@hook@installedfalse%
\let\tikz@text@width@backup\pgfutil@empty%
\let\tikz@maximum@text@width@value\pgfutil@empty%
\pgfkeys{%
    /tikz/maximum text width/.code={%
        \def\tikz@maximum@text@width@value{#1}%
        \ifx\tikz@maximum@text@width@value\pgfutil@empty% 'maximum text width={}' should restore previous value for 'text width'
            \ifx\tikz@text@width\pgfutil@empty% but only of 'text width' has not been set in the meantime
                \let\tikz@text@width\tikz@text@width@backup% restore saved 'text width' value
            \fi
        \else% maximum text width=<dimension> wil install the varwidth environment code for all nodes
            \iftikz@maximum@text@width@hook@installed\else
                \ifx\tikz@text@width@backup\pgfutil@empty
                    \let\tikz@text@width@backup\tikz@text@width
                \fi
                \pgfkeysalso{
                    execute at begin node={%
                        \ifx\tikz@maximum@text@width@value\pgfutil@empty\else% Don't use varwidth if 'maximum text width' is empty
                            \ifx\tikz@text@width\pgfutil@empty% Don't use varwidth if 'text width' has a value
                                \begin{varwidth}{\tikz@maximum@text@width@value}%
                                \leavevmode\begingroup% Prevent Package varwidth Warning: Failed to reprocess entire contents on input line
                            \fi%
                        \fi%
                    },%
                    execute at end node={%
                        \ifx\tikz@maximum@text@width@value\pgfutil@empty\else% Don't use varwidth if 'maximum text width' is empty
                            \ifx\tikz@text@width\pgfutil@empty% Don't use varwidth if 'text width' has a value
                                \endgroup\end{varwidth}%
                            \fi%
                        \fi%
                    },%
                }%
                \tikz@maximum@text@width@hook@installedtrue%
            \fi%
            \pgfkeysalso{/tikz/text width={}}% 'text width' overrides 'maximum text width' if specified, so we unset it (the value was saved above)
        \fi
    },
}
\makeatother


\begin{document}
\begin{tikzpicture}[
        y=-1cm, font=\scriptsize,
        every node/.style={text width=6cm, draw=red, inner xsep=0},
        my style/.style={draw=blue, minimum width=2cm, maximum text width=4cm},
    ]
    \path[very thin, draw=black!10!white] (-5,-1) grid (5,7);
    \node at (0,0) {\verb|every node| has \verb|text width=5cm|};
    % 'maximum text width' overrides 'text width'
    \node[my style] at (0,1) {custom node style with \verb|maximum text width=4cm|. Note that it is narrower than 4 cm due to line splitting};
    \node[my style] at (0,2) {min width};
    \node[my style] at (0,3) {single line $>$ min width};
    \node[my style, maximum text width=4cm] at (0,4) {multiple \verb|maximum text width| assignments do not add nested \verb|varwidth| environments};
    % Setting maximum text width to an empty value restores 'text width'
    \node[my style, maximum text width={}] at (0,5) {same style, but setting \verb|maximum text width={}| falls back to previous \verb|text width| value};
    \node[my style, text width=8cm] at (0,6) {same style, but setting \verb|text width=8cm| overrides and disables \verb|maximum text width|};
\end{tikzpicture}
\end{document}

相关内容