\mathpalette
: See The mysteries of \mathpalette
.
\rlap
: This is referred to as a r
ight overlap
. That is, it makes a box of width 0pt
, and aligns the contents within to the left, similar to \makebox[0pt][l]{<stuff>}
, causing the contents to overlap to the right.
\mkern
: Kerning (or spacing) in math mode. The unit of measure is mu
(or m
ath u
nits). So, \mkern2mu
inserts a kern or space of 2mu
horizontally. For a take on these lengths (and others), see What are the possible dimensions / sizes / units LaTeX understands?
Specific to your question about how this works:

\documentclass{article}
\newcommand\independent{\protect\mathpalette{\protect\independenT}{\perp}}
\def\independenT#1#2{\mathrel{\rlap{$#1#2$}\mkern2mu{#1#2}}}
\begin{document}
$X \independent Y$ \par
$\scriptstyle X \independent Y$ \par
$\scriptscriptstyle X \independent Y$ \par
\bigskip
$X \independent Y_{X \independent Y_{X \independent Y}}$
\end{document}
The use of \mathpalette
- defined internally as a specialized \mathchoice
- allows the input to do defined in a way that captures the current math style. Note that there are four different math styles: \displaystyle
, \textstyle
, \scriptstyle
and \scriptscriptstyle
.
The above MWE shows how the single command \independent
scales to regardless of where it's used, all because of a nifty use of \mathpalette
and a helper macro \independenT
. The helper macro captures the current math style and re-uses it inside an \rlap
. The double use of the symbol is just for spacing purposes, adding to the \mkern
insertion. That is, it sets the symbol \perp
(of width 0pt
and r
ight overlap
), inserts of 2mu
kern (pushing the "cursor" to the right 2mu
s) and then inserts the symbol again. Since the symbol is set in the same style, the horizontal bar overlaps, but the vertical bar is spaced 2mu
apart. As a final take on mathematical spacing, the entire symbol is set as a relation using \mathrel
.