# How to create a box with a superscript?

I want to create something the image below on Latex. So essentially it is a math function which is surrounded by a box with a superscript. And boxes can be within other boxes.

Any thoughts?

Thanks you very much!

The use of scalerel's \ThisStyle{...\SavedStyle...} feature allows \mathbox to be applied to super/subscripts as well (e.g., ...^{\mathbox{F}}) and retain its proper scriptsize nature.

\documentclass{article}
\usepackage{scalerel}
\newcommand\mathbox[1]{\mathord{\ThisStyle{%
\fboxsep3\LMpt\relax\kern1\LMpt\fbox{$\SavedStyle#1$}\kern1\LMpt}}}
\begin{document}
$\mathbox{\neg s}^T \rightarrow \mathbox{ \Bigl(\mathbox{(\mathbox{r}^T\rightarrow \mathbox{\neg p}^F) }^F \vee \mathbox{\neg t}^F\Bigr)}^F$

$\mathbox{a}^{\mathbox{b}^{\mathbox{c}}}$
\end{document}


You can use \boxed of amsmath with some help:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\boxsym}[1]{%
\mathord{\mspace{2mu}\boxed{\vphantom{by}#1}\mspace{1mu}}%
}

\begin{document}
$\boxsym{\lnot s}^T \rightarrow \boxsym{ \biggl(\boxsym{ \bigl(\boxsym{r}^T\rightarrow \boxsym{\lnot p}^F\bigr) }^F \vee \boxsym{\lnot t}^F\biggr) }^F$
\end{document}