How can I align two boxes to the top right and top left of a surrounding box?
(simplified the answer to show both candidate solution methods in one go)
Here are two candidate solutions. The first positions the sub- and superscript terms relative to the right-hand edge of the immediately preceding box, while the second ignores the height of the immediately preceding box.
The main changes, relative to your code, are (a) making \cwrapper
take three arguments (#1: the material to be placed in a box; #2 and #3: the superscript and subscript terms) and (b) using math mode to help position the super- and subscript terms relative to the preceding material.
\documentclass{article}
\newcommand*{\cscr}[1]{\textnormal{\scriptsize #1}}
\newcommand*{\cwrappera}[3]{%
$\left.{\textnormal{\fbox{#1}}}\ \right.^{\cscr{#2}}_{\cscr{#3}}$}
\newcommand*{\cwrapperb}[3]{%
\fbox{#1} ${\vphantom{\textnormal{#1}}}^{\cscr{#2}}_{\cscr{#3}}$}
\begin{document}
\cwrappera{AAA
\cwrappera{BBB
\cwrappera{CCC}{pC}{bC}}
{pB}{bB}}
{pA}{bA}
\medskip
\cwrapperb{AAA
\cwrapperb{BBB
\cwrapperb{CCC}{pC}{bC}}
{pB}{bB}}
{pA}{bA}
\end{document}
The original syntax of the OP can be preserved with this formulation:
\documentclass{article}
\usepackage{amsmath}
\newcommand*{\csub}[1]{_\textnormal{~#1}}
\newcommand*{\csup}[1]{^\textnormal{~#1}}
\newcommand*{\cnotation}[1]{#1}
\newcommand*{\crepeat}[1]{\fbox{#1}}
\newcommand*{\cwrapper}[1]{\ensuremath{#1}}
\begin{document}
\cwrapper{\crepeat{%
AAA
\cwrapper{\crepeat{%
BBB
\cwrapper{\crepeat{CCC}\csup{pC}\csub{bC}}}
\csup{pB}\csub{bB}}}
\csup{pA}\csub{bA}}
\end{document}
I use \cwrapper
to ensure math mode, and then use \csub
and \csup
to employ math sub and superscripts with \text
macros.
If one wants the sub- and superscripts tighter, then this would do, where I have moved the subscript up 1pt and the superscript down 2.5pt:
\documentclass{article}
\usepackage{amsmath}
\newcommand*{\csub}[1]{_\textnormal{~#1}}
\newcommand*{\csup}[1]{^\textnormal{~#1}}
\newcommand*{\cnotation}[1]{#1}
\newcommand*{\crepeat}[1]{\setbox0=\hbox{\fbox{#1}}\ht0=\dimexpr\ht0-2.5pt\relax%
\dp0=\dimexpr\dp0-1pt\relax\box0}
\newcommand*{\cwrapper}[1]{\ensuremath{#1}}
\begin{document}
\cwrapper{\crepeat{%
AAA
\cwrapper{\crepeat{%
BBB
\cwrapper{\crepeat{CCC}\csup{pC}\csub{bC}}}
\csup{pB}\csub{bB}}}
\csup{pA}\csub{bA}}
\end{document}
Here's a version respecting the clumsy syntax you want.
\documentclass{article}
\makeatletter
\newcommand{\cwrapper}[1]{%
\leavevmode\check@mathfonts
\begingroup\ignorespaces
#1%
\clement@wrap
\endgroup
}
\newcommand{\crepeat}[1]{%
\def\clement@repeat{#1}\ignorespaces
}
\newcommand{\csup}[1]{\def\clement@sup{#1}\ignorespaces}
\newcommand{\csub}[1]{\def\clement@sub{#1}\ignorespaces}
\newcommand{\clement@wrap}{%
\sbox\z@{\fbox{\clement@repeat}}%
\copy\z@
\raisebox{-\dp\z@}{%
\vbox to \dimexpr\ht\z@+\dp\z@{
\hrule height\z@
\hbox{\fontsize\sf@size\z@\selectfont\clement@sup}
\vss
\hbox{\fontsize\sf@size\z@\selectfont\clement@sub}
\hrule height\z@
}%
}%
}
\makeatother
\begin{document}
\cwrapper{\crepeat{AAA}\csup{a}\csub{b}}
\cwrapper{\crepeat{%
AAA
\cwrapper{\crepeat{%
BBB
\cwrapper{\crepeat{CCC}\csup{pC}\csub{bC}}}
\csup{pB}\csub{bB}}}
\csup{pA}\csub{bA}}
\Large
\cwrapper{\crepeat{AAA}\csup{a}\csub{b}}
\end{document}
Note that also font size changes are respected.