7

I would like to do something like the boxes in the equation below figure

But I have no idea how to implement this in Tex. Thanks in advance!

5

Another option (the example illustrates the size change according to the math style):

\documentclass{article}
\usepackage{amsmath}

\newlength\boxln

\newcommand\MyBox{{%
  \setlength\fboxsep{0pt}%
  \fbox{\phantom{\text{\raisebox{-0.25\boxln}{\rule{\boxln}{\boxln}}}}}}%
}
\newcommand\TwoBox[2]{%
\mathchoice
  {\setlength\boxln{1.5em}{\text{\footnotesize#1}}\,\,\underset{\textstyle\text{\raisebox{-0.3em}{\footnotesize#2}}}{\MyBox}}
  {\setlength\boxln{1em}{\scriptscriptstyle\text{#1}}\,\underset{\scriptscriptstyle\text{\raisebox{-0em}{#2}}}{\MyBox}}
  {\setlength\boxln{0.5em}{\scriptscriptstyle\text{#1}}\,\underset{\text{\raisebox{-0.1em}{#2}}}{\MyBox}}
  {\setlength\boxln{0.5em}{\scriptscriptstyle\text{#1}}\,\underset{\text{\raisebox{-0.25em}{#2}}}{\MyBox}}
}

\begin{document}

\[
\TwoBox{P}{A}\longleftrightarrow\TwoBox{A}{P}
\]

Some text $
\TwoBox{P}{A}\longleftrightarrow\TwoBox{A}{P}\quad
M_{\TwoBox{P}{A}\longleftrightarrow\TwoBox{A}{P}}\quad
A_{M_{\TwoBox{P}{A}\longleftrightarrow\TwoBox{A}{P}}}
$ and some more

\end{document}

enter image description here

  • Great! How can I change the size of the letters P and A? I would like them to be somewhat smaller. – Anne O'Nyme Jan 15 '15 at 17:12
  • @AnneO'Nyme Do you want to reduce the size for all math styles or just for displaystyle (the first one in my example code)? – Gonzalo Medina Jan 15 '15 at 17:24
  • For both I just want the relative size of the letters P and A to be smaller compared to the box. – Anne O'Nyme Jan 15 '15 at 18:55
  • 1
    @AnneO'Nyme I've updated my answer reducing the size for the letters. – Gonzalo Medina Jan 15 '15 at 23:03
6

Here's my solution; the box has the same height as \sum in text style.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\boxop}{\mathop{\mathpalette\doboxop\relax}}
\newcommand{\doboxop}[2]{%
  \sbox0{$\ifx#1\displaystyle\textstyle\else#1\fi\sum$}%
  \dimen0=\ht0 \advance\dimen0 \dp0
  \vcenter{\fboxsep=-\fboxrule
    \hbox{\fbox{\rule{0pt}{\dimen0}\rule{\dimen0}{0pt}}}
  }%
}
\newcommand{\nymefirst}[1]{\mathpalette\donymefirst{#1}}
\newcommand{\donymefirst}[2]{%
  \vcenter{\hbox{$
    \ifx#1\displaystyle\scriptstyle\else
      \ifx#1\textstyle\scriptstyle\else
    \scriptscriptstyle\fi\fi #2
  $}}%
}
\newcommand{\nymebox}[2]{%
  \nymefirst{#1}\mspace{2mu}{\boxop\limits_{#2}}%
}

\begin{document}

\begin{alignat*}{2}
&\text{Display}\qquad && \nymebox{P}{A}\leftrightarrow\nymebox{A}{P} \\
&\text{Text}          && \textstyle \nymebox{P}{A}\leftrightarrow\nymebox{A}{P} \\
&\text{Script}        && \scriptstyle \nymebox{P}{A}\leftrightarrow\nymebox{A}{P}
\end{alignat*}

\end{document}

enter image description here

If we change the definition of \boxop to

\newcommand{\doboxop}[2]{%
  \sbox0{$\ifx#1\displaystyle\textstyle\else#1\fi\sum$}%
  \dimen0=\ht0 \advance\dimen0 \dp0
  \vcenter{\fboxsep=-\fboxrule
    \hbox{\fbox{\rule{0pt}{\dimen0}\rule{\dimen0}{0pt}}}
  }%
}

the box will grow in display style, giving

enter image description here

3

EDITED to preserve math style:

\documentclass{article}
\usepackage{amssymb,stackengine,graphicx,scalerel}
\stackMath
\def\mybox{\scalebox{2.3}{\raisebox{\dimexpr-1.5\LMpt-0.5pt}{$\SavedStyle\square$}}}
\newcommand\labelbox[2]{\ThisStyle{\,\,\raisebox{\dimexpr1.5\LMpt+1pt}{$\SavedStyle_#1$}%
  \stackunder[2pt]{\mybox}{\SavedStyle_#2}\,\,}}
\begin{document}
\[X\quad\labelbox{P}{A} \longleftrightarrow \labelbox{A}{P}\]
\[\scriptstyle X\quad\labelbox{P}{A} \longleftrightarrow \labelbox{A}{P}\]
\[\scriptscriptstyle X\quad\labelbox{P}{A} \longleftrightarrow \labelbox{A}{P}\]
\end{document} 

enter image description here

ORIGINAL SOLUTION:

\documentclass{article}
\usepackage{amssymb}
\usepackage{stackengine,graphicx}
\def\mybox{\scalebox{2.3}{\raisebox{-2pt}{$\square$}}}
\newcommand\labelbox[2]{%
  \,\,\scriptstyle\mathrm{#1}\stackunder[2pt]{\mybox}{\scriptsize#2}\,\,}
\begin{document}
$\labelbox{P}{A} \longleftrightarrow \labelbox{A}{P}$
\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.