# Exchange meaning of : and \colon

I usually use the colon symbol : to denote maps. For set syntax there is the braket package. So as the spacing of : is not appropriate, I want it to behave like \colon. I tried

\documentclass{article}

\usepackage[fleqn]{amsmath}
\usepackage{braket}
\usepackage{dsfont}
\usepackage{mathtools}
\mathtoolsset{centercolon}

\def\textcolon{:} \catcode\:=\active \def:{\relax\ifmmode\colon\else\textcolon\fi}

\begin{document}

Here is an example:
\begin{gather*}
f: x \mapsto x^2\\
g\colon \mathds R_+ \ni x \mapsto x^x \in \mathds R\\
A := \Set{ x \in \mathds R_+ | g(x) < x }
\end{gather*}

\end{document}


But that throws the error

! TeX capacity exceeded, sorry [grouping levels=255].

Without the active definition, it looks like

Notice that I use := for definitions. That should still be possible. The problem there is that \colon= looks terrible:

As I agree with egreg, that using all the active math chars is not a good idea. I'm going with David's approach and use:

\AtBeginDocument{\DeclareMathSymbol{:}{\mathpunct}{operators}{"3A}}
\def\colon{\mathrel{:}}

\usepackage{mathtools}
\usepackage{newunicodechar}

\def\textcoloneq{≔} \newunicodechar{≔}{\ifmmode \mathrel{{\vcentcolon}{=}} \else \textcoloneq \fi}
\def\texteqcolon{≕} \newunicodechar{≕}{\ifmmode \mathrel{{=}{\vcentcolon}} \else \texteqcolon \fi}


I use \AtBeginDocument so that I can use it together with fontspec.

-
Don't use := for definitions as it does not vertically center the colon (notice that the dots of colon and the bars of the equal are not symmetric) Use \coloneqq from mathtools package or colonequals from the colonequals package. –  Aditya Jul 23 '12 at 19:47
@Aditya: That's what \mathtoolsset{centercolon} is for! –  canaaerus Jul 23 '12 at 19:52
Oops, you are right. I did not notice that. –  Aditya Jul 23 '12 at 19:53
–  percusse Jul 23 '12 at 20:05

You should just need

\DeclareMathSymbol{:}{\mathpunct}{operators}{"3A}


to define : to be like \colon. the original definitions are:

\DeclareMathSymbol{:}{\mathrel}{operators}{"3A}
\DeclareMathSymbol{\colon}{\mathpunct}{operators}{"3A}

-
You are right, for the normal case this is a good solution. But then := has some space in between. (like \colon=, see my updated question). If there is no better solution I could probably use the unicode char for := instead... –  canaaerus Jul 23 '12 at 20:02
That's really a separate operator, if building it out of : and = best to do \mathrel{{:}{=}} or rather define a command that does that –  David Carlisle Jul 23 '12 at 20:08

I guess it's easier to type \colon :)

\documentclass{article}

\usepackage[fleqn]{amsmath}
\usepackage{braket}
\usepackage{dsfont}
\usepackage{etoolbox}

\edef\latexordinarycolon{\mathchar\the\mathcode: } % keep the ordinary :
\usepackage{mathtools}
\let\amsmathcolon\colon                             % keep amsmath's \colon
\patchcmd\amsmathcolon{:}{\latexordinarycolon}{}{}  % avoid the mention of : in it
\mathtoolsset{centercolon}
\makeatletter
\begingroup
\catcode:=\active \global\let\mathtools@colon=:    % : is math active with mathtools "centercolon"
\gdef:{\@ifnextchar=\mathtools@colon\amsmathcolon}  % if : is followed by = ...
\endgroup
\let\colon\latexordinarycolon                       % Now \colon is a relation symbol

%%% Now we redefine = so that =: works
\edef\latexequal{\mathchar\the\mathcode= }
\begingroup
\catcode==\active
\gdef={\latexequal\@ifnextchar:{\afterassignment\mathtools@colon\let\next}{}}
\endgroup
\AtBeginDocument{\mathcode=="8000 }

\begin{document}

Here is an example:
\begin{gather*}
f: x \mapsto x^2\\
g\colon \mathds R_+ \ni x \mapsto x^x \in \mathds R\\
A := \Set{ x \in \mathds R_+ | g(x) < x }\\
a:=b=:c=d
\end{gather*}

\end{document}

-
Nice. Unfortunately I just noticed that then there are issues with the 'is defined as', =:, operator. To make that work = would have to be active as well, right? –  canaaerus Jul 24 '12 at 4:41
@canaaerus Math active. Don't do this: I don't think that using : for mappings instead of \colon is a big saving. And don't use := or (worse) =: (but this is another question: mathematicians have done without them for some centuries and those symbol don't convey any meaning that can't be derived from the context). –  egreg Jul 24 '12 at 6:33
It's not about saving in typing, but making the code somewhat readable and editable. Also I must say that there where instances where I did wonder whether an = was the initial definition or just a statement of equality; and I was really confused. Especially for local variables that are reused I find it a good practise to hint at definition by =. Of course in many cases definition is done not by formula but with some prose, which is still better. –  canaaerus Jul 24 '12 at 6:39