[Tex/LaTex] How to set letter above or below the symbol

stacking-symbols

I have known \overset{K}{\|} to set above. However how to do like this:

enter image description here

Best Answer

The same vertical size as \sum:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\nnn}{\DOTSB\nnn@\slimits@}
\newcommand{\nnn@}{\mathop{\mathpalette\nnn@@\|}}
\newcommand{\nnn@@}[2]{%
  \vphantom{\sum}%
  \ifx#1\displaystyle\big#2\else#2\fi
}
\makeatother

\begin{document}

\[
\nnn_{k=1}^K\quad \sum_{k=1}^K
\]
\begin{center}% just to show inline math below displaystyle
$\nnn_{k=1}^K$\quad$\sum_{k=1}^K$
\end{center}

\end{document}

enter image description here