As discussed here, mathematical functions should be defined using \colon, while set definitions should be done using :. My problem is that I define functions about twenty times more often that I define sets, and typing out \colon every time is a serious overhead; it makes my code much less readable, and it incurs some mental overhead every time that slows down note-taking.

I already prefer the | notation for defining sets over :, so the default : has no use for me. Is it possible to redefine it so it behaves just like \colon does?

  • 1
    \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). Dec 4 '17 at 18:33
  • 1
    @StevenB.Segletes I'm sad hhline didn't come to mind:-) Dec 4 '17 at 19:27
  • @StevenB.Segletes This seems to break equation labels, which I write as “eq:blah”.
    – knzhou
    Dec 5 '17 at 16:31
  • Indeed. As I noted, "This will break other constructs, if they rely on : as part of the syntax" Dec 5 '17 at 16:37

With amsmath, that you probably are using, \colon is not simply a punctuation symbol, but uses a somewhat different spacing (line 3 is what you'd get without amsmath).

In line 4 I use \relcolon for the set denotation.

Caution This will break with babel-french.



% a colon as ordinary symbol

% a colon as relation symbol

% the definition of \colon in amsmath contains :
% which would start an infinite loop

% provide a definition of : as an active character
\begingroup\lccode`~=`: \lowercase{\endgroup\let~}\colon

% make : math active
\AtBeginDocument{\mathcode`:="8000 }


1. $f\colon A\to B$ with \verb|\colon|

2. $f:A\to B$ with \verb|:|

3. $f\mathpunct{\mathordcolon} A\to B$

4. $\{\,x\in X\relcolon x=x\}$


enter image description here

  • Thanks! Warning for others: this conflicts with mathtools, in which case the name ordinarycolon must be changed.
    – knzhou
    Dec 5 '17 at 16:47
  • @knzhou I forgot about that; better change the name, then.
    – egreg
    Dec 5 '17 at 17:41

The following example first shows the original behavior of : and \colon. Then the relational colon is saved as command \relcolon and the colon symbol is reassigned as punctuation colon:

  a\colon b:c
  a:b\relcolon c


  • Is it possible to run this code in a .sty file, or does it need to be inside the document itself?
    – knzhou
    Dec 4 '17 at 23:19
  • @knzhou It can be put in a package file. Dec 4 '17 at 23:24
  • When I place this right into my .sty file I get several errors, starting with Missing number, treated as zero. [\mathcode`\:=\colon], Missing $ inserted. [\mathcode`\:=\colon]. Is there something I need to adjust?
    – knzhou
    Dec 5 '17 at 16:32
  • Ah, I see, I think this solution is incompatible with amsmath.
    – knzhou
    Dec 5 '17 at 16:46
  • @knzhou This is already addressed by egreg's answer. Dec 5 '17 at 17:12

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:

\usepackage{amsmath, amssymb}

% http://tex.stackexchange.com/questions/209863
{\IfNoValueTF{#2}{#1} {#1\,\delimsize|\,\mathopen{}#2}}

\newcommand\map[1]{\colon #1\longrightarrow}



  \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$



The output:

enter image description here

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.