3

Node TXT1 is automatically sized as well as node TXT2 which is following TXT1. But how to get TXT1 width so that it can be used with text width of 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}

2
  • Do you mean the red rectangle around the first to be the same width like the second's at width? Or you want to change contents of lines too (break later)?
    – koleygr
    Commented Oct 21, 2017 at 0:09
  • Ok... So, is this ok to give an initial text and then to adjust every rectangle to this initial texts width?
    – koleygr
    Commented Oct 21, 2017 at 0:32

2 Answers 2

5

You can calculate the width of TXT1 using the let syntax. That is the total width though, so you need to subtract the inner sep and line width to get a value that can be used for text width.

Output of code

\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}
5
  • +1 for the idea... I also think that the custom "hardcoded calculations" would be simplified with inner sep or something simmilar [not tested]
    – koleygr
    Commented Oct 21, 2017 at 13:09
  • @koleygr What do you mean? Commented Oct 21, 2017 at 14:27
  • 1
    \x1-\x2-2*\pgfkeysvalueof{/pgf/inner xsep}-\pgflinewidth... you can change inner sep then too without worrying for the calculation
    – koleygr
    Commented Oct 21, 2017 at 14:58
  • @koleygr Ah, that's right, I forgot that it works with inner xsep. (It doesn't work with \pgfkeysvalueof{/pgf/inner sep}.) Thanks, I'll edit. Commented Oct 21, 2017 at 15:02
  • No problem... trying to find a way to accept inner BASICSTYLE and to work... I am close but it seems dificult
    – koleygr
    Commented Oct 21, 2017 at 15:08
3

Here is a way:

\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}

Output:

enter image description here

1st Edit: (before @DavidCarlisle will see it and get disapointed that I didn't learn my lesson from his comments here: 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}

Also fixed the spaces in start of text.

Second Edit (automating for user):

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.

\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}

Output:

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .