# Render trivial nested boxes

I just need nested boxes in latex. Like this:

with the following constraints:

• the outermost box (black) will never be larger than \textwidth or pagewidth
• there is never more than one box in a row

I've already tried all kinds of fboxes, makeboxes, colorboxes, stacks etc. This comes close, but it still has the problem with several boxes in a row.

Here is a MWE:

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\begin{document}
{\fboxrule=.75pt%
\fbox{\stackunder{Box 1 }
{
\fbox{\stackunder{ Box 2 }{
\fbox{\stackunder{ Box 4 }{}}
}}
\fbox{\stackunder{ Box 3 }{}}
}
}
}
\end{document}


How can I get box 3 under box 2 and limit the entire width to the pagesize?

• I've updated the question. – user1511417 Feb 15 '18 at 15:41

I just added another \stackunder with the box2/4 as the first argument, and the box3 as the 2nd.

I also left-aligned the stacks (by default).

However, note that an \fbox approach will not limit the eventual overall width, unless you make the content of the \fbox to be, for example, a \parbox of specified width.

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\def\stackalignment{l}
\begin{document}
{\fboxrule=.75pt%
\fbox{\stackunder{Box 1 }
{
\stackunder{%
\fbox{\stackunder{ Box 2 }{
\fbox{\stackunder{ Box 4 }{}}
}}
}{%
\fbox{\stackunder{ Box 3 }{}}%
}
}
}
}
\end{document}