2

In math, I exclusively use the colon : for punctuation, i.e. in the sense of \colon (compare this question). Hence I would like to change the way : is being typeset, so that it resembles \colon.

Is there a way to achieve this in XeTeX / unicode-math?

balu
  • 239
  • 3
  • 10

2 Answers2

3
\documentclass{article} 
\usepackage{amsmath}
\begin{document} 
$\forall x: P (x)$  using \verb|:|

$\forall x \colon P(x)$ using \verb|\colon|

\def\newcolon{%
  \nobreak\mskip2mu\mathpunct{}\nonscript\mkern-\thinmuskip{\text{:}}%
  \mskip 6mu plus 1 mu \relax}

\mathcode`\:="8000 %
{\catcode`:=\active \global\let:\newcolon}

$\forall x: P (x)$ using revised \verb|:|

$\scriptstyle \forall x: P (x)$

$\scriptscriptstyle \forall x: P (x)$

\end{document}

enter image description here

  • This sends TeX into infinite loop if used along with \usepackage[french]{babel}. Anyway, \number is not needed after \mathcode. – egreg Feb 14 '20 at 20:31
  • @egreg Thanks for noting both points. Removed \number...don't know how to resolve babel issue. – Steven B. Segletes Feb 14 '20 at 20:33
  • Thanks so much for your answer! I honestly don't see a difference between \colon and your \newcolon, though? (: – balu Feb 14 '20 at 21:51
  • Also, it seems the colon now sometimes gets typeset italic (which looks weird), as far as I can tell, this happens in theorem environments where the surrounding text is italic, too. – balu Feb 14 '20 at 21:58
  • On a different note, I just discovered \DeclareMathSymbol (see e.g. this question). Unfortunately, it seems the command requires specifying the font. Does anyone happen to know whether it's possible to use that command in a font-agnostic way to redefine the colon? (Maybe that'd be a solution for French speakers.) – balu Feb 14 '20 at 22:03
  • @balu The difference is only in the default spacing. What other difference should there be? As to italic, let me have a look – Steven B. Segletes Feb 14 '20 at 23:02
  • 1
    @balu As to the italic, changing the {\text{:}} in my definition to {\text{\textup{:}}} will do it. However, the 1st version approach of egreg does not suffer this issue, in which you define \mathchardef\normalcolon=\mathcode\:and then replace in my definition{\text{:}}with{\normalcolon}`. – Steven B. Segletes Feb 14 '20 at 23:07
  • @StevenB.Segletes I don't actually notice a difference when it comes to the spacing in your examples, that's why I was asking. (Am I blind? :)) As for making the colon non-italic, egreg's solution doesn't seem to work with XeTeX (as he mentioned). The error I'm getting is Extended mathchar used as mathchar (6300214). [\mathchardef\normalcolon=\mathcode:]`. In any case, your first solution works! Thanks so much! – balu Feb 15 '20 at 13:52
  • @balu The location of the : in my output is different on the first line (using :) than it is on the 2nd and 3rd lines (using \colon and revised definition of :. – Steven B. Segletes Feb 15 '20 at 19:28
  • @StevenB.Segletes Yes, but I what I meant was that I don't see a difference between the second line (\colon) and the third line (revised colon / \newcolon)? Basically, what I'm asking is why you defined a new command \newcolon when \colon is already available? – balu Feb 15 '20 at 19:48
  • @balu My understanding was that you were tired of typing of \-c-o-l-o-n and wanted to get the same thing by simply typing : in math mode. – Steven B. Segletes Feb 15 '20 at 20:57
  • @StevenB.Segletes No, absolutely. :) I was merely asking why you didn't define \global\let:\colon instead of \global\let:\newcolon but now I realize that the former causes an infinite loop, so I guess that must have been it. – balu Feb 15 '20 at 21:26
  • 1
    @balu That is correct...\colon is defined in terms of math :, so defining : in terms of \colon creates an infinite loop. Here, \newcolon is defined in terms of a non-math colon. – Steven B. Segletes Feb 15 '20 at 21:49
  • Ohh, that makes sense! Thanks! – balu Feb 15 '20 at 21:55
1

If you're not French and plan to never write in French, you can just do

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\mathchardef\normalcolon=\mathcode`:
\def\colon{%
  \noexpand\nobreak
  \mskip 2mu
  \mathpunct{}
  \nonscript\mkern -\thinmuskip
  {\normalcolon}%
  \mskip 6muplus1mu
  \relax
}
\begingroup\lccode`\~=`\:\lowercase{\endgroup\let~}\colon
\mathcode`\:=\string"8000
\makeatother

\begin{document}

$f: A \to B$

$a\normalcolon b$

\end{document}

This would send TeX into infinite loop if babel-french is used. A safer patch:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{amsmath}

\makeatletter
\mathchardef\normalcolon=\mathcode`:
\def\colon{%
  \noexpand\nobreak
  \mskip 2mu
  \mathpunct{}
  \nonscript\mkern -\thinmuskip
  {\normalcolon}%
  \mskip 6muplus1mu
  \relax
}
\@ifpackagewith{babel}{french}{% french babel makes : active
  \declare@shorthand{french}{:}{%
    \ifmmode % <--- added
      \colon % <--- added
    \else % <--- added
      \ifFB@spacing
        \ifhmode
          \ifdim\lastskip>1sp
            \unskip\penalty\@M\FBcolonspace
          \else
            \FDP@colonspace
          \fi
        \fi
      \fi
      \string:%
    \fi % <--- added
  }%
}{%
  \begingroup\lccode`\~=`\:\lowercase{\endgroup\let~}\colon
  \mathcode`\:=\string"8000
}
\makeatother

\begin{document}

$f: A \to B$

$a\normalcolon b$

\end{document}

enter image description here

You can still use \colon with the standard meaning. For the colon as relation symbol (what you get normally in TeX with :) I provided \normalcolon.

Disclaimer. This doesn't work with XeLaTeX or LuaLaTeX.

egreg
  • 1,121,712