[Tex/LaTex] angle brackets in math and text mode

math-modetext-mode

I use \textlangle and \textrangle in a command

\newcommand{\qdist}[1]{\textlangle#1\textrangle}

which is used in math and text mode. In math mode, I get the error message

LaTeX Warning: Command \textrangle invalid in math mode on input line 952.

what similar symbol could be used in both modes? I use Lualatex.

the MWE

\documentclass[msmallroyalvopaper
    ]{memoir}

\newcommand{\qdist}[1]{\textlangle#1\textrangle}

\begin{document}

text with \qdist{word} 

and some math 
\[   a = \qdist{symbol} \]
The multiple applications of a GIS can be summarized in three prototypical situations:

more text 

\end{document}

Best Answer

You could incorporate an \ifmmode conditional, to use \langle and \rangle in math mode and \textlangle and \textrangle in text mode.

enter image description here

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.