节点文本定位不正常

节点文本定位不正常

我需要两个与较大盒子大小相同的盒子,每个盒子里面都包含多次“文本”(每个盒子都不同)。

当我编写以下代码时,它给出了如下所示的结果(文本出现在框外面,并且没有达到所需的重复次数)。

有人可以帮忙吗?

谢谢

\documentclass[border=10pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}

\newbox\mybox
\def\mysaver{%
\pgfmathparse{\pgfpositionnodelatermaxx-\pgfpositionnodelaterminx}%
\xdef\nodeW{\pgfmathresult pt}%
\pgfmathparse{\pgfpositionnodelatermaxy-\pgfpositionnodelaterminy}%
\xdef\nodeH{\pgfmathresult pt}%
}

\begin{document}

\begin{tikzpicture}
\begin{scope}

{\pgfpositionnodelater{\mysaver}
% This node is clearly outside the grid
\node [inner sep=0] {Text};}

\node (a)
[draw,rounded corners=6pt,rectangle,inner sep=0.4cm]
{
    \tikz{
    \foreach \x in {0,\nodeW+0.2cm,\nodeW*2+0.2cm*2,\nodeW*3+0.2cm*3,\nodeW*4+0.2cm*4}
    \foreach \y in {0,\nodeH+0.2cm,\nodeH*2+0.2cm*2,\nodeH*3+0.2cm*3}
        \node [inner sep=0] at (\x,\y) {Text};
    \node [inner sep=0] at (\nodeW*2+0.2cm*2,\nodeH*4+0.2cm*4) {Text};
    }
};

\node (b) [right=0.4cm of a,inner sep=0] {Text Text};

{\pgfpositionnodelater{\mysaver}
% This node is clearly outside the grid
\node [inner sep=0] {Text};}

\node (c) [fit=(a),right=0.4cm of b,draw,rounded corners=6pt,rectangle,inner sep=0]
{
    \tikz{
        \foreach \x in {0,\nodeW+0.2cm}
        \foreach \y in {0,\nodeH+0.2cm,\nodeH*2+0.2cm*2}
            \node [inner sep=0] at (\x,\y) {Text};
    }
};

\end{scope}
\end{tikzpicture}
\end{document}

由于我无法上传该图,因此这里是该图的链接: https://drive.google.com/file/d/1e_bcgi96F45zsaeAd0kYY96fpe53mAR4/view?usp=sharing

答案1

问题是不应该嵌套 TiZ 图片,这里也没有必要。

\documentclass[border=10pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{fit,calc}

\newbox\mybox
\def\mysaver{%
\pgfmathparse{\pgfpositionnodelatermaxx-\pgfpositionnodelaterminx}%
\xdef\nodeW{\pgfmathresult pt}%
\pgfmathparse{\pgfpositionnodelatermaxy-\pgfpositionnodelaterminy}%
\xdef\nodeH{\pgfmathresult pt}%
}

\begin{document}

\begin{tikzpicture}
\begin{scope}

{\pgfpositionnodelater{\mysaver}
% This node is clearly outside the grid
\node [inner sep=0] {Text};}

\node (a)
[draw,rounded corners=6pt,rectangle,inner sep=0.4cm]
{
    \tikz{
    \foreach \x in {0,\nodeW+0.2cm,\nodeW*2+0.2cm*2,\nodeW*3+0.2cm*3,\nodeW*4+0.2cm*4}
    \foreach \y in {0,\nodeH+0.2cm,\nodeH*2+0.2cm*2,\nodeH*3+0.2cm*3}
        \node [inner sep=0] at (\x,\y) {Text};
    \node [inner sep=0] at (\nodeW*2+0.2cm*2,\nodeH*4+0.2cm*4) {Text};
    }
};

\node (b) [right=0.4cm of a,inner sep=0] {Text Text};

{\pgfpositionnodelater{\mysaver}
% This node is clearly outside the grid
\node [inner sep=0] {Text};}

\node (c) [fit=(a),right=0.4cm of b,draw,rounded corners=6pt,rectangle,inner sep=0]
{
};
        \foreach \x in {0,\nodeW+0.2cm}
        \foreach \y in {0,\nodeH+0.2cm,\nodeH*2+0.2cm*2}
            \node [inner sep=0] at ($(c.center)+(\x,\y)$) {Text};

\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

目前还不清楚是否需要使用Tikz。如果不需要,您可以使用tcolorbox并使用 构建结果equal height group

\documentclass{article}
\usepackage[most]{tcolorbox}

\newtcolorbox{mybox}[1][]{%
    notitle,
    equal height group=MyGroup,
    before=,
    after=\hfill,
    valign=center,
    #1}

\begin{document}
\begin{mybox}[width=.4\linewidth]
Text\\ Text Text Text Text\\
Text Text Text Text\\
Text Text Text Text\\
\end{mybox}
%
\begin{mybox}[blanker, width=15mm]
Text Text
\end{mybox}
%
\begin{mybox}[width=.3\linewidth]
Text Text\\
Text Text
\end{mybox}

\end{document}

在此处输入图片描述

相关内容