2

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?

knzhou
  • 175

3 Answers3

2

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:

\documentclass{article}
\begin{document}
\[
  a\colon b:c
\]
%
\mathchardef\relcolon=\mathcode`\:
\mathcode`\:=\colon
%
\[
  a:b\relcolon c
\]
\end{document}

Result

Heiko Oberdiek
  • 271,626
2

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:

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.

1

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.

\documentclass{article}
\usepackage{amsmath}

\usepackage{etoolbox}

% a colon as ordinary symbol
\DeclareMathSymbol{\mathordcolon}{\mathord}{operators}{"3A}

% a colon as relation symbol
\DeclareMathSymbol{\relcolon}{\mathrel}{operators}{"3A}

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

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

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

\begin{document}

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

\end{document}

enter image description here

egreg
  • 1,121,712