# Symbol inside math operator

I would like to define a math operator that looks a bit like this, which I mocked up in an image editor from the amsmath \square symbol:

Sometimes there will be a different letter than lambda inside the box, or an expression like \lambda', but the box should always be the same size, and always be square - it's meant to be an inline operator with a symbol drawn on it, not a box around part of a formula.

I don't mind if I have to manually position the formula inside it, as there will probably only be a few different letters that I'll use.

It doesn't have to be based on \square. In fact I think it would look nicer if the baseline was a bit lower, to match + for example.

Is there a reasonable way to achieve this?

Here's a solution using a \framebox of fixed size. I thought it would look better if the symbol in the square was smaller, in \scriptstyle. I think it looks reasonably good with any lowercase latin or greek letter.

\documentclass{article}
\usepackage{amsmath}
\newcommand{\squareop}[1]{%
\setlength{\fboxsep}{0pt}%
\setlength{\unitlength}{.7em}%
\mathrel{%
\raisebox{-1pt}{\framebox(1,1){$$\scriptstyle #1$$}}%
}%
}
\begin{document}
\begin{tabular}{lll}
$$x \squareop{a} y$$  & $$x \squareop{b} y$$    & $$x \squareop{c} y$$    \\
$$x \squareop{d} y$$  & $$x \squareop{e} y$$    & $$x \squareop{f} y$$    \\
$$x \squareop{g} y$$  & $$x \squareop{h} y$$    & $$x \squareop{i} y$$    \\
$$x \squareop{j} y$$  & $$x \squareop{k} y$$    & $$x \squareop{l} y$$    \\
$$x \squareop{m} y$$  & $$x \squareop{n} y$$    & $$x \squareop{o} y$$    \\
$$x \squareop{p} y$$  & $$x \squareop{q} y$$    & $$x \squareop{r} y$$    \\
$$x \squareop{s} y$$  & $$x \squareop{t} y$$    & $$x \squareop{u} y$$    \\
$$x \squareop{v} y$$  & $$x \squareop{w} y$$    & $$x \squareop{z} y$$    \\
\end{tabular}
\begin{tabular}{lll}
$$x \squareop{\alpha} y$$     & $$x \squareop{\beta} y$$    & $$x \squareop{\gamma} y$$  \\
$$x \squareop{\delta} y$$     & $$x \squareop{\epsilon} y$$ & $$x \squareop{\zeta} y$$   \\
$$x \squareop{\eta} y$$       & $$x \squareop{\theta} y$$   & $$x \squareop{\iota} y$$   \\
$$x \squareop{\kappa} y$$     & $$x \squareop{\lambda} y$$  & $$x \squareop{\mu} y$$     \\
$$x \squareop{\nu} y$$        & $$x \squareop{\xi} y$$      & $$x \squareop{\pi} y$$     \\
$$x \squareop{\rho} y$$       & $$x \squareop{\sigma} y$$   & $$x \squareop{\tau} y$$    \\
$$x \squareop{\upsilon} y$$   & $$x \squareop{\phi} y$$     & $$x \squareop{\varphi} y$$ \\
$$x \squareop{\chi} y$$       & $$x \squareop{\psi} y$$     & $$x \squareop{\omega} y$$  \\
\end{tabular}
\end{document}

• Wow, that looks amazing! It's slightly awkward to fit lambda prime in the box, but \lambda\!' looks reasonable. Apr 13, 2020 at 15:08
• @Nathaniel Thanks! Yes, I also thought that with the negative space, the λ' would look ok. I didn't test it so much for other symbols, but I figured there was enough space left with most letters to fit a prime in there. Were you looking for something with capital letters also? Apr 13, 2020 at 15:17
• No, this is great, it's perfect! Apr 13, 2020 at 15:31
• Letters with descenders, inside the box, are raised above the main baseline by the depth of the descender. Is this really what's wanted? Apr 13, 2020 at 20:49
• @barbarabeeton personally I like it, because otherwise the descender would protrude out of the box and it wouldn't read as a box with the letter drawn on it. (Then again I haven't seen what a baseline aligned version would look like.) Apr 13, 2020 at 21:15

You may also want the big version.

\documentclass{article}
\usepackage{amsmath,array,relsize}

\makeatletter
\DeclareRobustCommand{\boxop}[1]{\mathbin{\mathpalette\box@op{#1}}}
\DeclareRobustCommand{\bigboxop}[1]{\mathop{\mathpalette\bigbox@op{#1}}\slimits@}

\newcommand{\box@op}[2]{%
\begingroup
\sbox\z@{$\m@th#1\mkern15mu$}%
\dimen@=\wd\z@
\setlength{\fboxsep}{0pt}%
\makebox[\dimen@]{%
\framebox[0.9\dimen@]{%
\vbox to 0.9\dimen@{%
\vss
\hbox{\raisebox{\depth}{$\box@op@style{#1}#2$}}%
\vss
}%
}%
}%
\endgroup
}
\newcommand{\box@op@style}[1]{%
\ifx#1\displaystyle\scriptstyle\else
\ifx#1\textstyle\scriptstyle\else
\scriptscriptstyle\fi\fi\m@th
}
\newcommand{\bigbox@op}[2]{%
\begingroup
\sbox\z@{$\m@th#1\sum$}%
\dimen@=\wd\z@
\vphantom{\sum}%
\vcenter{%
\setlength{\fboxsep}{0pt}%
\hbox to \dimen@{%
\hss
\framebox[0.9\dimen@]{%
\vbox to 0.9\dimen@{%
\vss
\hbox{\raisebox{\depth}{$\m@th#1\box@op@larger{#1}{#2}$}}%
\vss
}%
}%
\hss
}%
}%
\endgroup
}
\newcommand{\box@op@larger}[2]{%
\ifx#1\displaystyle
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\mathlarger{#2}}{#2}%
}
\makeatother

\begin{document}

$x\boxop{\lambda}y\boxop{\lambda\!'}z\boxop{\varphi}w\boxop{\beta}u$

$\scriptstyle x\boxop{\lambda}y\boxop{\lambda\!'}z$

$\displaystyle\sum_{k=1}^n\bigboxop{\lambda}_{k=1}^n x_k$
$\bigboxop{\lambda}_{k=1}^n x_k$
$\scriptstyle\bigboxop{\lambda}_{k=1}^n x_k$

$\displaystyle\sum_{k=1}^n\sum_{k=1}^n x_k$

\end{document}


Using youngtab that I not knew with the symbol inside math operator. It is right to write that the square has the same dimension.

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath,amssymb}

\usepackage{youngtab}
\Ylinethick{0.5pt}
\begin{document}

$a \mathrel{\young(\lambda)} b$, $A_{\mathrel{\young(\mu)_c^d}}$, $X\sim\sum_{i=1}^nx_i\mathrel{\young(\diamond)}y_i$.

\end{document}