2

I need to have two boxes with the size of the bigger one, including the "text" inside each of them a number of times (different for each of them).

When I write the following code it gives me the result shown below (the text appears outside the box and not in the desired number of repetitions).

Can anyone help?

Thanks

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

Here is a link to the figure since uploading it is not working with me: https://drive.google.com/file/d/1e_bcgi96F45zsaeAd0kYY96fpe53mAR4/view?usp=sharing

3
  • 1
    You can upload images directly to your post, please do that, the method you used doesn't work. Feb 21, 2018 at 13:49
  • Note that nesting tikzpictures is in general a bad idea, and can cause problems, so it might be better to find a different approach. Feb 21, 2018 at 13:55
  • add the picture and remove the initial ! we will add it for you but google drive linking won't work. Right click the picture select copy image and then in the question click the image add button and directly paste.
    – percusse
    Feb 21, 2018 at 14:01

2 Answers 2

1

The issue is that one should not nest TikZ pictures, and here is also no need for this.

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

enter image description here

1

It's not clear if you need to use Tikz. If not, you could use tcolorbox and build the result with an 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}

enter image description here

You must log in to answer this question.

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