節點文字定位無法正常運作

節點文字定位無法正常運作

我需要有兩個盒子,其大小與較大的盒子相同,其中每個盒子內都包含多次“文本”(每個盒子都不同)。

當我編寫以下程式碼時,它會給出如下所示的結果(文字出現在框外,並且未達到所需的重複次數)。

有人可以幫忙嗎?

謝謝

\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

問題是不應該嵌套 TikZ圖片,這裡也不需要這個。

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

在此輸入影像描述

相關內容