Tikz:具有不同文本的相同大小的节点

Tikz:具有不同文本的相同大小的节点

我是新手Tikz,我需要绘制两行大小相同但包含不同长度文本的节点。确保上下节点有足够的空间容纳两个文本中最长的文本。上部节点应与下部节点对齐并具有相同的大小。

具体来说,我需要使用绘制下图Tikz

在此处输入图片描述

答案1

一种可能的 TikZ 免费解决方案,具有自动宽度计算并允许多行文本(下面是 TikZ 解决方案):

在此处输入图片描述

该命令\EightTexts有八个强制参数,LaTeX 使用简单条件测试对相应字符串的长度执行所有必要的计算。图片中的代码是使用

\EightTexts{Small}{Medium}{Large}{Larger}{AA}{Even larger}{B}{The largest}

\vskip1cm

\EightTexts{AAAA}{B}{CCCCC}{DD}{EEEEEEEEE}{FFFFF}{GG}{HHHHHHHH}

完整代码:

\documentclass{article}
\usepackage{varwidth}

\newlength\mylenai
\newlength\mylenbi
\newlength\mylenMaxi
\newlength\mylenaii
\newlength\mylenbii
\newlength\mylenMaxii
\newlength\mylenaiii
\newlength\mylenbiii
\newlength\mylenMaxiii
\newlength\mylenaiv
\newlength\mylenbiv
\newlength\mylenMaxiv

\newsavebox\myboxi
\newsavebox\myboxii
\newsavebox\myboxiii
\newsavebox\myboxiv
\newsavebox\myboxv
\newsavebox\myboxvi
\newsavebox\myboxvii
\newsavebox\myboxviii

\newcommand\TikZEightTexts[8]{%
% Save the texts in boxes so we can measure their widths. 
\savebox\myboxi{\begin{varwidth}{\textwidth}#1\end{varwidth}}
\savebox\myboxii{\begin{varwidth}{\textwidth}#2\end{varwidth}}
\savebox\myboxiii{\begin{varwidth}{\textwidth}#3\end{varwidth}}
\savebox\myboxiv{\begin{varwidth}{\textwidth}#4\end{varwidth}}
\savebox\myboxv{\begin{varwidth}{\textwidth}#5\end{varwidth}}
\savebox\myboxvi{\begin{varwidth}{\textwidth}#6\end{varwidth}}
\savebox\myboxvii{\begin{varwidth}{\textwidth}#7\end{varwidth}}
\savebox\myboxviii{\begin{varwidth}{\textwidth}#8\end{varwidth}}
% Choose maximum width between width of first box and fifth box
% store the maximum in `\mylenMaxi`
\setlength\mylenMaxi{\the\wd\myboxi}%
\setlength\mylenai{\the\wd\myboxi}%
\setlength\mylenbi{\the\wd\myboxv}%
\ifdim\mylenbi>\mylenai\relax
  \setlength\mylenMaxi{\mylenbi}
\fi
% Choose maximum width between the width of second box and the 
% width of sixth box; store the maximum in `\mylenMaxii`
\setlength\mylenMaxii{\the\wd\myboxii}%
\setlength\mylenaii{\the\wd\myboxii}%
\setlength\mylenbii{\the\wd\myboxvi}%
\ifdim\mylenbii>\mylenaii\relax
  \setlength\mylenMaxii{\mylenbii}
\fi
% Choose maximum width between the width of third box and the 
% width of seventh box; store the maximum in `\mylenMaxiii`
\setlength\mylenMaxiii{\the\wd\myboxiii}%
\setlength\mylenaiii{\the\wd\myboxiii}%
\setlength\mylenbiii{\the\wd\myboxvii}%
\ifdim\mylenbiii>\mylenaiii\relax
  \setlength\mylenMaxiii{\mylenbiii}
\fi
% Choose maximum width between the width of fourth box and the 
% width of eighth box; store the maximum in `\mylenMaxiv`
\setlength\mylenMaxiv{\the\wd\myboxiv}%
\setlength\mylenaiv{\the\wd\myboxiv}%
\setlength\mylenbiv{\the\wd\myboxviii}%
\ifdim\mylenbiv>\mylenaiv\relax
  \setlength\mylenMaxiv{\mylenbiv}
\fi
\noindent
\fbox{\parbox[c][1.2cm][c]{\mylenMaxi}{\centering#1}}\hfill
\fbox{\parbox[c][1.2cm][c]{\mylenMaxii}{\centering#2}}\hfill
\fbox{\parbox[c][1.2cm][c]{\mylenMaxiii}{\centering#3}}\hfill
\fbox{\parbox[c][1.2cm][c]{\mylenMaxiv}{\centering#4}}%
\bigskip\par\noindent
\fbox{\parbox[c][1.2cm][c]{\mylenMaxi}{\centering#5}}\hfill
\fbox{\parbox[c][1.2cm][c]{\mylenMaxii}{\centering#6}}\hfill
\fbox{\parbox[c][1.2cm][c]{\mylenMaxiii}{\centering#7}}\hfill
\fbox{\parbox[c][1.2cm][c]{\mylenMaxiv}{\centering#8}}\par
}

\begin{document}

\TikZEightTexts{Small}{Medium}{Large}{Larger}{AA}{Even larger}{B}{The 3 lines\\largest solution\\ever}

\vskip1cm

\TikZEightTexts{AAAA \\ AAAAAAAA \\ AAA}{B}{CCCCC}{DD}{EEEEEEE}{FFFFF}{GG \\ GGG }{HH \\ HH}

\end{document}

评论中要求使用 TikZ 的解决方案;这里是:

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

\newlength\mylenai
\newlength\mylenbi
\newlength\mylenMaxi
\newlength\mylenaii
\newlength\mylenbii
\newlength\mylenMaxii
\newlength\mylenaiii
\newlength\mylenbiii
\newlength\mylenMaxiii
\newlength\mylenaiv
\newlength\mylenbiv
\newlength\mylenMaxiv

\newsavebox\myboxi
\newsavebox\myboxii
\newsavebox\myboxiii
\newsavebox\myboxiv
\newsavebox\myboxv
\newsavebox\myboxvi
\newsavebox\myboxvii
\newsavebox\myboxviii

\newcommand\TikZEightTexts[8]{%
% Save the texts in boxes so we can measure their widths. 
\savebox\myboxi{\begin{varwidth}{\textwidth}#1\end{varwidth}}
\savebox\myboxii{\begin{varwidth}{\textwidth}#2\end{varwidth}}
\savebox\myboxiii{\begin{varwidth}{\textwidth}#3\end{varwidth}}
\savebox\myboxiv{\begin{varwidth}{\textwidth}#4\end{varwidth}}
\savebox\myboxv{\begin{varwidth}{\textwidth}#5\end{varwidth}}
\savebox\myboxvi{\begin{varwidth}{\textwidth}#6\end{varwidth}}
\savebox\myboxvii{\begin{varwidth}{\textwidth}#7\end{varwidth}}
\savebox\myboxviii{\begin{varwidth}{\textwidth}#8\end{varwidth}}
% Choose maximum width between width of first box and fifth box
% store the maximum in `\mylenMaxi`
\setlength\mylenMaxi{\the\wd\myboxi}%
\setlength\mylenai{\the\wd\myboxi}%
\setlength\mylenbi{\the\wd\myboxv}%
\ifdim\mylenbi>\mylenai\relax
  \setlength\mylenMaxi{\mylenbi}
\fi
% Choose maximum width between the width of second box and the 
% width of sixth box; store the maximum in `\mylenMaxii`
\setlength\mylenMaxii{\the\wd\myboxii}%
\setlength\mylenaii{\the\wd\myboxii}%
\setlength\mylenbii{\the\wd\myboxvi}%
\ifdim\mylenbii>\mylenaii\relax
  \setlength\mylenMaxii{\mylenbii}
\fi
% Choose maximum width between the width of third box and the 
% width of seventh box; store the maximum in `\mylenMaxiii`
\setlength\mylenMaxiii{\the\wd\myboxiii}%
\setlength\mylenaiii{\the\wd\myboxiii}%
\setlength\mylenbiii{\the\wd\myboxvii}%
\ifdim\mylenbiii>\mylenaiii\relax
  \setlength\mylenMaxiii{\mylenbiii}
\fi
% Choose maximum width between the width of fourth box and the 
% width of eighth box; store the maximum in `\mylenMaxiv`
\setlength\mylenMaxiv{\the\wd\myboxiv}%
\setlength\mylenaiv{\the\wd\myboxiv}%
\setlength\mylenbiv{\the\wd\myboxviii}%
\ifdim\mylenbiv>\mylenaiv\relax
  \setlength\mylenMaxiv{\mylenbiv}
\fi
\noindent
\begin{tikzpicture}[every node/.append style={
draw,minimum height=1.3cm,align=center}
]
\node[text width=\mylenMaxi] (11) {#1};
\node[text width=\mylenMaxii,right= of 11] (12) {#2};
\node[text width=\mylenMaxiii,right= of 12] (13) {#3};
\node[text width=\mylenMaxiv,right= of 13] (14) {#4};
\node[text width=\mylenMaxi,below=of 11] (21) {#5};
\node[text width=\mylenMaxii,right= of 21] (22) {#6};
\node[text width=\mylenMaxiii,right= of 22] (23) {#7};
\node[text width=\mylenMaxiv,right= of 23] (24) {#8};
\end{tikzpicture}%
}

\begin{document}

\TikZEightTexts{Small}{Medium}{Large}{Larger}{AA}{Even larger}{B}{The 3 lines\\largest solution\\ever}

\vskip1cm

\TikZEightTexts{AAAA \\ AAAAAAAA \\ AAA}{B}{CCCCC}{DD}{EEEEEEE}{FFFFF}{GG \\ GGG }{HH \\ HH}

\end{document}

在此处输入图片描述

答案2

您可以使用tikz matrix简化放置和创建节点标记的,然后使用\draw以下框:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}

% http://tex.stackexchange.com/questions/33703/extract-x-y-coordinate-of-an-arbitrary-point-in-tikz
\newdimen\XCoordA
\newdimen\XCoordB
\newdimen\YCoordA
\newdimen\YCoordB
\newcommand{\ExtractMinX}[3]{%
    % #1 = csname to hold min x value
    % #2 = coordinate 1
    % #3 = coordinate 2
    \path (#2); \pgfgetlastxy{\XCoordA}{\YCoordA};%
    \path (#3); \pgfgetlastxy{\XCoordB}{\YCoordB};%
    \ifdim\XCoordA<\XCoordB
        \edef#1{\XCoordA}%
    \else
        \edef#1{\XCoordB}%
    \fi
}%
\newcommand{\ExtractMaxX}[3]{%
    % #1 = csname to hold max x value
    % #2 = coordinate 1
    % #3 = coordinate 2
    \path (#2); \pgfgetlastxy{\XCoordA}{\YCoordA};%
    \path (#3); \pgfgetlastxy{\XCoordB}{\YCoordB};%
    \ifdim\XCoordA>\XCoordB
        \edef#1{\XCoordA}%
    \else
        \edef#1{\XCoordB}%
    \fi
}%

\tikzset{My Matrix Node Style/.style={every node/.style={draw=none, minimum height=5ex, anchor=base, row sep=15.0ex, column sep=5.0em}}}


\begin{document}
\begin{tikzpicture}
\pgfsetmatrixcolumnsep{2.0em}
\matrix (M) [matrix of nodes, My Matrix Node Style] {
    Small & Medium & Larger Text & Even Larger Text \\[2.0ex]
    Even larger text & Larger Text & Medium & Small \\
};
% Now draw the boxes based on the min and max x value for each column
\foreach \x in {1,...,4} {% for each column
    \ExtractMinX{\MinX}{M-1-\x.south west}{M-2-\x.south west}%
    \ExtractMaxX{\MaxX}{M-1-\x.north east}{M-2-\x.north east}%
    \foreach \y in {1,...,2} {% for each row
        \coordinate (Left Bottom) at (\MinX,\YCoordA |- M-\y-\x.south west);
        \coordinate (Right Top) at   (\MaxX,\YCoordB |- M-\y-\x.north east);
        \draw [red, rounded corners=3pt, thick]
            (Left Bottom) rectangle (Right Top);
    }
}
\end{tikzpicture}
\end{document}

答案3

这个简单的事情不需要tikz

\documentclass{article}
\usepackage{calc}
\newlength\mylen

\settowidth\mylen{Even larger text}

\newcommand{\row}[4]{%
\fbox{\makebox[\mylen][c]{\rule[-0.55ex]{0ex}{1.2em}#1}}%
\hfill
\fbox{\makebox[\mylen][c]{\rule[-0.55ex]{0ex}{1.2em}#2}}%
\hfill
\fbox{\makebox[\mylen][c]{\rule[-0.55ex]{0ex}{1.2em}#3}}%
\hfill
\fbox{\makebox[\mylen][c]{\rule[-0.55ex]{0ex}{1.2em}#4}}%
}

\begin{document}

\noindent
\row{Small}{Medium}{Larger text}{Even larger text}

\vspace{1cm}
\noindent
\row{Even larger text}{Larger text}{Medium}{Small}

\end{document}

在此处输入图片描述

答案4

因此,您可能需要使用该\phantom{}命令。这是一个 MWE,但我相信您会明白的:

\documentclass[border=0.125cm,tikz]{standalone}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[blk/.style={rounded corners, minimum height=.7cm,draw,align=center},node distance=2cm,line width=1pt]

\node (a) [blk] at (0,0){\phantom{small no 2}\\Small\\ \phantom{small no 2}};
\node (b) [blk,below=of a] {\phantom{Small}\\small no 2\\ \phantom{Small;/}};
\node (c) [blk,right=of a] at (0,0){\phantom{short}\\very long text\\ \phantom{short}};
\node (d) [blk,below=of c] {\phantom{very long text}\\short\\ \phantom{very long text}};

\end{tikzpicture}
\end{document}

您需要的输出是:

在此处输入图片描述

相关内容