angle brackets in math and text mode
You could incorporate an \ifmmode
conditional, to use \langle
and \rangle
in math mode and \textlangle
and \textrangle
in text mode.
This approach works with pdfLaTeX, LuaLaTeX, and XeLaTeX.
\documentclass{memoir}
\usepackage{textcomp} % for \textlangle and \textrangle macros
\newcommand{\qdist}[1]{\ifmmode\langle#1\rangle\else\textlangle#1\textrangle\fi}
\begin{document}
\qdist{word}, $\qdist{symbol}$
\end{document}
Addendum -- As @egreg has pointed out in a comment and in his separate answer, using \newcommand
to create the \qdist
macro, as is done above, can run into trouble. It's necessary to use \DeclareRobustCommand
instead of \newcommand
.
I guess you don't want that the typesetting of the argument to \qdist
changes font when in math or in text, so a simpler
\newcommand{\qdist}[1]{\text{\textlangle#1\textrangle}}
might suffice. On the other hand, if this appears in an italic context such as a theorem statement, the effect would be unpleasant:
\newcommand{\qdist}[1]{\textup{\textlangle#1\textrangle}}
seems better.
If you intend that in math the argument to \qdist
should be some math symbol, then in my opinion you should use two distinct commands, because the situations are different and semantics of commands is important.
If you don't want to follow the advice, use
\DeclareRobustCommand{\qdist}[1]{%
\ifmmode
\langle#1\rangle
\else
\textup{\textlangle#1\textrangle}%
\fi
}
which differs from Mico's suggestion in two important details: it will work correctly in every situation (Mico's wouldn't) and keeps the output upright in any an italic context.