Square with a diamond in it

It is quit difficult to get the two symbols scaled and align correctly in all four math modes. Better draw it using TikZ. One difficulty is to properly scale the size and line width. This can be best done using amsmath \text macro. I made two versions which are slightly different.

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}

\newcommand{\sqdiamond}{\mathbin{\text{\tikz [x=1ex,y=1ex,line width=.1ex,line join=round] \draw (0,0) rectangle (1,1) (0,.5) -- (.5,1) -- (1,.5) -- (.5,0) -- (0,.5) -- cycle;}}}

\newcommand{\varsqdiamond}{\mathbin{\text{\tikz [x=1ex,y=1ex,line width=.1ex,line join=round] \draw (0,0) rectangle (1,1) (.5\pgflinewidth,.5) -- (.5,1ex-.5\pgflinewidth) -- (1ex-.5\pgflinewidth,.5) -- (.5,.5\pgflinewidth) -- (.5\pgflinewidth,.5) -- cycle;}}}

\begin{document}
% Test code:

.. $a \sqdiamond b$ ..

\[a \sqdiamond b \]

.. $X_{a \sqdiamond b}$ ..

.. $X_{X_{a \sqdiamond b}}$ ..


.. $a \varsqdiamond b$ ..

\[a \varsqdiamond b \]

.. $X_{a \varsqdiamond b}$ ..

.. $X_{X_{a \varsqdiamond b}}$ ..


\end{document}

Result 1

Zoomed:

Variant 1: sqdiamond

Variant 2: varsqdiamond


This seems to work with the Computer Modern fonts, but it's font dependent:

\newcommand{\sqdiamond}{\mathbin{\ooalign{%
  $\scriptstyle\square$\cr\hfil\raisebox{.15pt}{$\diamond$}\hfil\cr}}}