如何使下一个节点与前一个节点的宽度相同?

如何使下一个节点与前一个节点的宽度相同?

text width节点 TXT1 的大小是自动调整的,TXT1 后面的节点 TXT2 也是如此。但是如何获取 TXT1 的宽度,以便与TXT2一起使用呢?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset
    {%
        BASICSTYLE/.style=
            {%
                align=left,
                draw=red
            }
    }
\begin{document}
    \begin{tikzpicture}
        \path node[BASICSTYLE](TXT1)
            {this is line 1\\this is line 2};
        \path node[BASICSTYLE,below=5mm of TXT1](TXT2)
            {this is ... long line one\\this is ... long line two};
    \end{tikzpicture}
\end{document}

答案1

TXT1您可以使用语法计算 的宽度let。但这是总宽度,因此您需要减去inner sep和 线宽才能得到可用于 的值text width

代码输出

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\tikzset
    {%
        BASICSTYLE/.style=
            {%
                align=left,
                draw=red
            }
    }
\begin{document}
    \begin{tikzpicture}
        \node[BASICSTYLE](TXT1)
            {this is line 1\\this is line 2};
        \path
            let
            \p1=(TXT1.east),\p2=(TXT1.west),
            \n1={\x1-\x2-2*\pgfkeysvalueof{/pgf/inner xsep}-\pgflinewidth}
            in
            node
            [BASICSTYLE,below=5mm of TXT1,text width=\n1]
            (TXT2)
            {this is ... long line one\\this is ... long line two};
    \end{tikzpicture}
\end{document}

答案2

这里有一种方法:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset
    {%
        myrect/.style=
            {%
                draw,red,text=black,thick,minimum width=\mymaximumwidth
            }
    }

\def\mytext#1{\noindent\begin{tabular}{l}#1\end{tabular}}
\def\mytextafter#1{\noindent\setlength\tabcolsep{0pt}\begin{tabular}{p{\mymaximumwidth}}#1\end{tabular}}
\newlength{\mymaximumwidth}
\newsavebox{\mybox}
\begin{document}

\def\mywidernode{\mytext{Here is the text\\ with width that\\will be used in nodes}}
\savebox\mybox{\mywidernode}
\setlength\mymaximumwidth{\wd\mybox}
    \begin{tikzpicture}
       \path node (rect1) [draw,red,thick,minimum height=2cm] {\usebox{\mybox}};
       \node[myrect,below=1cm of rect1] (rect2) {\mytextafter{Here is another\\ text\\ with same width}};
       \node[myrect,right=1cm of rect2] (rect3) {\mytextafter{Here is another with bigger text and automatic linebreaks}};
       \node[myrect,above=1cm of rect3]{\mytextafter{test\\text\\here\\with last row breaking manually}};
    \end{tikzpicture}
\end{document}
\section{}

\end{document}

输出:

在此处输入图片描述

第一次编辑:(在@DavidCarlisle 看到它并对我没能从他的评论中吸取教训而感到失望之前:https://tex.stackexchange.com/a/396972/120578):

\documentclass{standalone}
\usepackage{tikz}
\usepackage{array}
\usetikzlibrary{positioning}
\tikzset
    {%
        myrect/.style=
            {%
                draw,red,text=black,thick,minimum width=\mymaximumwidth,inner sep=5pt
            }
    }

\def\mytext#1{\begin{tabular}{@{\raggedleft}l}#1\end{tabular}}
\def\mytextafter#1{\setlength\tabcolsep{0pt}\begin{tabular}{p{\mymaximumwidth}}#1\end{tabular}}
\newlength{\mymaximumwidth}
\begin{document}

\def\mywidernode{\mytext{Here is the text\\ with width that\\will be used in nodes}}
\settowidth\mymaximumwidth{\mywidernode}
    \begin{tikzpicture}
       \path node (rect1) [draw,red,thick,minimum height=2cm,inner sep=5pt] {\mywidernode};
       \node[myrect,below=1cm of rect1] (rect2) {\mytextafter{Here is another\\ text\\ with same width}};
       \node[myrect,right=1cm of rect2] (rect3) {\mytextafter{Here is another with bigger text and automatic linebreaks}};
       \node[myrect,above=1cm of rect3]{\mytextafter{test\\text\\here\\with last row breaking manually}};
    \end{tikzpicture}
\end{document}
\section{}

\end{document}

还修复了文本开头的空格。

第二次编辑(为用户自动化):

用户只需给出\Startingtext命令内的文本并通过命令开始节点\StartNode\curstart是我们每个当前起始节点的名称。

\documentclass{standalone}
\usepackage{tikz,pgf}
\usepackage{array}
\usetikzlibrary{positioning}
\newlength{\mymaximumwidth}
\newsavebox{\mybox}
\newcounter{startnode}
\makeatletter
\def\mytext#1{\@ifundefined{pgf@sh@pi@\curstart}{\savebox\mybox{\begin{tabular}{@{\raggedleft}l}#1\end{tabular}}\usebox{\mybox}\setlength\mymaximumwidth{\wd\mybox}}
{\setlength\tabcolsep{0pt}\begin{tabular}{p{\mymaximumwidth}}#1\end{tabular}}%
}
\def\StartNode{\draw[myrect] node[draw] (\curstart)  {\usebox{\mybox}};}

\def\Startingtext#1{\stepcounter{startnode}\xdef\curstart{mystart\thestartnode}\savebox\mybox{\begin{tabular}{@{\raggedleft}l}\mytext{#1}\end{tabular}}
\setlength\mymaximumwidth{\wd\mybox}}
\makeatother

\tikzset
    {%
        myrect/.style=
            {%
                draw,red,text=black,thick,minimum width=\mymaximumwidth,inner sep=5pt
            }
    }

% The user just has to give the text inside `\Startingtext` command and
% begin the nodes by the command `\StartNode`. `\curstart` is the name of
% our every current starting node.
\begin{document}

\Startingtext{Here is the text\\ with width that\\will be used in nodes}


    \begin{tikzpicture}
       \StartNode
       \node[myrect,below=1cm of \curstart] (rect2) {\mytext{Here is another\\ text\\ with same width}};
       \node[myrect,right=1cm of rect2] (rect3) {\mytext{Here is another with bigger text and automatic linebreaks}};
       \node[myrect,above=1cm of rect3]{\mytext{test\\text\\here\\with last row breaking automatically}};
    \end{tikzpicture}\vspace*{40pt}


    \Startingtext{Small \\ width \\text}
       \begin{tikzpicture}
       \StartNode
       \node[myrect,below=1cm of \curstart] (rect2) {\mytext{Here is another\\ text\\ with same width}};
       \node[myrect,right=1cm of rect2] (rect3) {\mytext{Here is another with bigger text and automatic linebreaks}};
       \node[myrect,above=1cm of rect3]{\mytext{test\\text\\here\\with last row breaking manually}};
    \end{tikzpicture}\vspace*{40pt}


\end{document}

输出:

在此处输入图片描述

相关内容