I am not strictly answering the question but rather than playing with :
for sets and functions I, instead, have a \set{...}
macro (from http://tex.stackexchange.com/questions/209863), and a \map
macro to achieve similar outcomes:
\documentclass{article}
\usepackage{mathtools}
\usepackage{amsmath, amssymb}
\usepackage{xparse}
% http://tex.stackexchange.com/questions/209863
\DeclarePairedDelimiterX{\set}[1]{\{}{\}}{\setargs{#1}}
\NewDocumentCommand{\setargs}{>{\SplitArgument{1}{|}}m}{\setargsaux#1}
\NewDocumentCommand{\setargsaux}{mm}
{\IfNoValueTF{#2}{#1} {#1\,\delimsize|\,\mathopen{}#2}}
\newcommand\map[1]{\colon #1\longrightarrow}
\begin{document}
\verb+$\set{1,2,\dots,n}$+
$\set{1,2,\dots,n}$
\verb+$\set{1\le k\le n|k\in\mathbb{Z}}$+
$\set{1\le k\le n|k\in\mathbb{Z}}$
\verb+$\set[\Bigg]{\displaystyle\sum_{k=1}^n| 1\le n\le 100}$+
$\set[\Bigg]{\displaystyle\sum_{k=1}^n| 1\le n\le 100}$
\verb+$f\map AB$+
$f\map AB$
\verb+$f\map{\mathbb{R}}\mathbb{R}$+
$f\map{\mathbb{R}}\mathbb{R}$
\end{document}
The output:

As I prefer using \longrightarrow
to \to
, typing \map
is much better than typing \colon
and then \longrightarrow
. The other advantage is that both \set
and \map
look very close to the mathematical meaning.
\catcode`\:=\active \def:{\ifmmode\colon\else\char58\fi}
. This will break other constructs, if they rely on:
as part of the syntax (though none come to mind at the moment). – Steven B. Segletes Dec 4 '17 at 18:33:
as part of the syntax" – Steven B. Segletes Dec 5 '17 at 16:37