# Nesting boxes from the outside in

Array programming languages have the concept of boxing. J is such a language. Boxes can contain anything, nothing included. Most of all, because of array languages' design, boxing happens from the outside inwards, and is also visually presented that way, as can be seen in the environment provided by fancyvbr, wherein lays the result from jconsole from an input of <(<1),<<2.

For non-nested examples, e.g. list of empty and 1, \fbox{} suffices.

However, for a box of (a box with 1) and (a box with (a box with 2)), the two boxes' frames at level 1 do not align because they are constructed from the inside out, that is, they grow instead of shrink.

I sought for similar quandries in the TeX SE, I sought boxing and nesting packages in CTAN, but found nothing relevant. Using \hlines and nested \tabular would have worked were it not for no explicit \vspace{...}. I may missing something very simple, but I expect a massive block of TiKZ code, nothing simple.

Here is my practice sheet, that is the MWE:

\documentclass{article}
\usepackage{microtype,calc}
\usepackage{fancyvrb}
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{0.20pt}
\setlength{\dashlength}{3pt}
\setlength{\hangindent}{0pt}
\setlength{\parindent}{0pt}
\AtBeginDocument{
\newlength{\myht}   \settoheight{\myht}{0}
\newlength{\mydp}   \settodepth{\mydp}{0}
\newlength{\mylen}  \setlength{\mylen}{\dimexpr\baselineskip-\myht-\mydp-1.1pt\relax}
\def\ebox{\fbox{\hbox to 0.25ex{\vbox to \mylen{}}}}
}
\usepackage[raster]{tcolorbox}
\begin{document}

\fbox{\ebox\fbox{\fbox{1}}}

\fbox{\fbox{1}\fbox{\fbox{2}}}

\fbox{ \begin{tabular}{|c|c|}\hline 1 & \fbox{ \hbox to 0.2ex {\vbox to 5ex{}}2 }\\ \hline\end{tabular} }
\begin{Verbatim}
┌───────┐
│┌─┬───┐│
││1│┌─┐││
││ ││2│││
││ │└─┘││
│└─┴───┘│
└───────┘
\end{Verbatim}

{
\setlength{\fboxsep}{1ex}
\begin{tabular}{|c|c|}\hline
1 & \fbox{2} \\
\hline\end{tabular}
}

\fbox{\fbox{\ebox}\fbox{1 2}}\\
\fbox{%
\begin{minipage}{2cm}\begin{tcbitemize}[size=fbox,sharp corners,colframe=black,colback=white,raster equal height,raster force size=false,raster equal skip=0pt,raster column skip=2mm]%
\tcbitem 1 2 3%
\tcbitem 1 2%
\end{tcbitemize}\end{minipage}%
}
\end{document}


Which compiles to

EDIT: I want to reproduce the Verbatim example. It doesn't matter whether boxes' contents are vertically centered, only that boxes at the same level have frames at the same baseline, that is, their depths and heights are the same.

• Your question is rather hard to follow. Perhaps it would be easier if we could see the MWE output but the supplied code gives ! Missing \endcsname inserted. <to be read again> \protect l.4 \DefineShortVerb{\Ь} Jul 13 at 18:21
• What exactly do you mean by "do not align?" All boxes, whether next to each other or nested, share the same baseline. Tabular and minipage have multiple baselines inside and have to choose one. See also \vbox, \vcenter and \vtop. Jul 13 at 18:23
• Can you clarify what you want that is not this output from \fbox{\fbox{ 1 \fbox{ 2 }}} Jul 13 at 18:33
• @DavidCarlisle Hey, Dave, long time no see, We talked on the chat long ago, I've lost account details. Anyway, the fboxed example is impossible in most-to-all array languages because at the same level you have both a number and a box (different data types)
– r440
Jul 13 at 18:46
• you may find that not providing untested broken examples and not being rude to people offering to help makes it more likely you get answers. Jul 13 at 20:35

Maybe this??

\documentclass{article}
\usepackage{listofitems}
\newlength\maxht
\newlength\maxdp
\newcommand\cbox[1]{%
\maxht=0pt
\maxdp=0pt
\foreachitem\z\in\mylist[]{%
\setbox0=\hbox{\z}%
\ifdim\ht0>\maxht\relax\maxht=\ht0\fi
\ifdim\dp0>\maxdp\relax\maxdp=\dp0\fi
}
\foreachitem\z\in\mylist[]{%
\fbox{\z\rule[-\maxdp]{0pt}{\dimexpr\maxdp+\maxht}}%
\kern-\fboxrule
}
}
\parskip1em
\begin{document}

\cbox{1,2}

\cbox{1,\cbox{2}}

\cbox{\cbox{1,\cbox{2}}}

\cbox{\cbox{\cbox{\cbox{1,2},3,4}, 5}}

\end{document}


This shows how to match the height, width or both of a savebox.

\documentclass{article}
\parskip=1ex

\newcommand{\matchbox}[2]% #1 = box register, #2 = contents
{\makebox[\wd#1]{\raisebox{0pt}[\ht#1][\dp#1]{#2}}}

\begin{document}
\sbox0{\fbox{A}}%
\fbox{\usebox0}\fbox{\raisebox{0pt}[\ht0][\dp0]{B}}

\fbox{\usebox0}\fbox{\makebox[\wd0]{B}}

\fbox{\usebox0}\fbox{\matchbox{0}{B}}

\sbox1{\fbox{\usebox0}\fbox{\matchbox{0}{B}}}%
\sbox2{\fbox{\usebox1}\fbox{\matchbox{1}{C}}}%
\fbox{\usebox2}\fbox{\matchbox{2}{D}}

\end{document}


Not a great answer, but fiddling with the frame width and the separation lengths an invisible box could be salable:

\documentclass{article}
\setlength{\fboxrule}{0.2pt}
\setlength{\fboxsep}{1.0pt}
\newcommand{\FBOX}[1]{{\fbox{\setlength{\fboxsep}{\dimexpr\fboxsep+\fboxrule\relax}\setlength{\fboxrule}{0pt}\fbox{#1}}}}
\newcommand{\nbox}[1]{{\fbox{\setlength{\fboxsep}{\dimexpr\fboxsep+\fboxrule\relax}\setlength{\fboxrule}{0pt}#1}}}
\begin{document}
\fbox{\fbox{1}\fbox{\fbox{2}}}

\nbox{\fbox{1}}\fbox{\fbox{2}}

\FBOX{1}\fbox{\fbox{2}}
\end{document}


EDIT: Increasing the fboxsep by the to-be-gone fboxrule helps alleviate the discrepancy, but there persists a visible distance between neighboring boxes, something miniscule, like 1 pixel.

• Please embed and highlight somehow your solution in the code, which is complete in the senses of: "can be copied and compiled". Wrt the question including a screenshot may be helpful, too. Jul 13 at 21:14