4

I want to change the usual role of : and \colon. Consider the following code

\documentclass{scrreprt}

\usepackage{amsmath}
\usepackage{unicode-math}

%\unimathsetup{colon=literal}
%\DeclareMathSymbol{:}{\mathpunct}{operators}{"3A}
%\let\colon\relax
%\DeclareMathSymbol{\colon}{\mathrel}{operators}{"3A}


\begin{document}
    \newcommand\mycolon{\mathpunct{\char"003A}}
    \newcommand\myratio{\mathrel{\char"003A}}

    \( \pi:X \to Y \) (1)

    \( \pi\myratio X \to Y \) (2)

    \( \pi\colon X \to Y \) (3)

    \( \pi\mycolon X \to Y \) (4)
\end{document}

The first two have the same gap between $\pi$ und $X$. (3) has less space and (4) the least amount. Remember the spacing of (3). Now uncomment the lines to interchange the role of ':' and \colon. Through now (1') is now correctly set as a punctation, the spacing is equal to (4') and NOT equal to the previous space of (3). Is there a way to adjust them?

EDIT: Solution due to egreg (with minimal changes)

\documentclass{scrreprt}

\usepackage{amsmath}
\usepackage{unicode-math}
\usepackage{etoolbox}


\patchcmd{\colon}{:}{\relcolon}{}{}         % Remove ':' from the definition of \colon 
\newcommand\myratio{\mathrel{\char"02236}}  % Define how the colon should look like as a ratio 
%\newcommand\myratio{\mathrel{\char"003A}}  % Use for wider space
\AtBeginDocument{
    \edef\relcolon                          % Define \recolon with current ':'
        {\mathrel{\Umathcharnum\the\Umathcodenum`:}}    
    \begingroup\lccode`~=`:                 % Make ':' math-active and redefine it to be \colon
    \lowercase{\endgroup\let~}\colon
    \mathcode`:="8000
    \let\colon\myratio                      % Redefine \colon with \myratio                      
}

\begin{document}
    \( \pi:X \to Y \) (1)

    \( \pi\colon X \to Y \) (2)
\end{document}
egreg
  • 1,121,712

2 Answers2

4

With amsmath, \colon is not simply a punctuation symbol, but has some spacing before it and more space after it than a punctuation symbol.

A (not very straightforward) way to (almost) interchange the meanings is

\documentclass{scrreprt}

\usepackage{amsmath}
\usepackage{unicode-math}
\usepackage{etoolbox}
\patchcmd{\colon}{:}{\relcolon}{}{}
\AtBeginDocument{
  \edef\relcolon{\mathrel{\Umathcharnum\the\Umathcodenum`:}}
  \begingroup\lccode`~=`:
  \lowercase{\endgroup\let~}\colon
  \mathcode`:="8000
}


\begin{document}

    \( \pi:X \to Y \) (1)

    \( \pi\mathpunct{\relcolon} X \to Y \) (2)

    \( \pi\relcolon X \to Y \) (3)

\end{document}

Case (1) is the same as \colon; case (2) is with \mathpunct to show the difference; case (3) is the colon as relation.

First of all I patch \colon so it has not an explicit : in its definition, or an infinite loop would happen. Then (at begin document), I define \relcolon with the current mathcode of : (\edef is necessary to get the explicit number). Finally I make : math active and give it the same meaning as amsmath's \colon. The usage of \relcolon in it has no consequence, because it appears braced, so considered an ordinary symbol.

However, I don't think it's a good idea to change the semantics, because it limits portability.

enter image description here

If you really want to use \colon for the relation, then this modification of the above code should work:

\documentclass{scrreprt}

\usepackage{amsmath}
\usepackage{unicode-math}
\usepackage{etoolbox}
\let\amsmathcolon\colon
\patchcmd{\amsmathcolon}{:}{\colon}{}{}
\AtBeginDocument{
  \edef\colon{\mathrel{\Umathcharnum\the\Umathcodenum`:}}
  \begingroup\lccode`~=`:
  \lowercase{\endgroup\let~}\amsmathcolon
  \mathcode`:="8000
}


\begin{document}

    \( \pi:X \to Y \) (1)

    \( \pi\mathpunct{\colon} X \to Y \) (2)

    \( \pi\colon X \to Y \) (3)

\end{document}

Note that the option colon=literal doesn't do what's requested; here's an example

\documentclass{scrreprt}

\usepackage{amsmath}
\usepackage[colon=literal]{unicode-math}


\begin{document}

    \( \pi:X \to Y \) (1)

    \( \pi\mathpunct{\colon} X \to Y \) (2)

    \( \pi\colon X \to Y \) (3)

\end{document}

enter image description here

So : uses a wrong spacing and \colon isn't redefined.

egreg
  • 1,121,712
  • It works perfectly, thanks! We just need to redefine \colon with a custom implementation (e.g \mathrel{\char"02236} ) to completely finish the interchange. (see my original post for the complete solution) Also, thanks for the warning about the change of semantics. I'm aware of the limits, but I think this is a better solution then to write every function definition with a \colon, because this clutters the code. – Tobias Diez Oct 29 '13 at 08:43
  • @altertoby I added a better way to interchange the meanings. – egreg Oct 29 '13 at 08:50
  • Yes, this works also but I think the \char"02236 is more appropriate, as it really stands for the relational colon. (Also there is a slight difference between your solution and mine, at least in Minion Pro) – Tobias Diez Oct 29 '13 at 09:01
  • @altertoby I don't agree about appropriateness. Minion Pro doesn't have an accompanying math font, so I don't think that a math intensive document should use it as main font. – egreg Oct 29 '13 at 09:03
  • If I use your solution in a tikz-environment then the compilation 'ends' in a loop. Have you a solution for this problem? – Tobias Diez Dec 08 '13 at 12:43
  • @altertoby That's a know problem, because TikZ uses the colon in its syntax. I remain with my opinion that such a change in semantics is not a good idea. – egreg Dec 08 '13 at 13:34
  • If this problem is well-known, exists there also a well-known solution? ;-) And since this failure is just a result of the active colon, it would be nice if you can put your concerns regarding the semantics aside (Moreover, I'm the only one who ever sees the code) – Tobias Diez Dec 08 '13 at 14:53
  • @altertoby See http://tex.stackexchange.com/questions/89467/why-does-pdftex-hang-on-this-file If you're able to use the answers for your problem, good. I'll waste my time with funner activities. ;-) – egreg Dec 08 '13 at 15:05
0

See section §5.5.6 of unicode-math documentation:

\documentclass{scrreprt}

\usepackage{amsmath}
\usepackage[colon=literal]{unicode-math}
\begin{document}

    \( \pi:X \to Y \) (1)

\end{document}