How to format an Association in Latex?

I use Courier New/Consolas/Source Code Pro as well as Mathematica Mono (can be found in the directory of your local installation) to format it:

enter image description here

\documentclass{article}
\usepackage{fontspec,xcolor}

% Default colors of Mathematica
\definecolor{mma@string}{RGB}{102,102,102}
\definecolor{mma@symbol}{RGB}{0,44,195}
\definecolor{mma@comment}{RGB}{69,148,174}

\newfontfamily\mma{MathematicaMono}[%
  Path=C:/Program Files/Wolfram Research/Mathematica/11.3/SystemFiles/Fonts/TrueType/,
  Extension=.ttf,
  UprightFont=*, BoldFont=*-Bold]
\newcommand\Association[1]{{\mma\symbol{"F113}}#1{\mma\symbol{"F114}}}
\newcommand\RightArrow{{\mma\symbol{"F522}}}
\newcommand\String[1]{"\textcolor{mma@string}{#1}"}
\newcommand\Comment[1]{\textcolor{mma@comment}{{\mma(*}\,#1\,{\mma*)}}}
\newcommand\Greek[1]{{\mma\textcolor{mma@symbol}{#1}}}

\def\TEST{%
  \Association{%
    1\,\,\RightArrow{}\,\,\String{a},
    2\,\,\RightArrow{}\,\,\String{b},
    3\,\,\RightArrow{}\,\,\Greek{αβ}}}

\begin{document}
\fontspec{Courier New}[BoldFont=* Bold]\TEST\
\Comment{Courier New}

\bfseries\TEST\
\Comment{Courier New Bold}

\fontspec{Consolas}[BoldFont=*]\TEST\
\Comment{Consolas}

\fontspec{Source Code Pro}[BoldFont=*]\TEST\
\Comment{Source Code Pro}
\end{document}

Note that the spaces are added by hand.


You can get automatic spacing, but it requires a bit of work. This needs XeLaTeX or LuaLaTeX.

A proof of concept:

\documentclass{article}
\usepackage{amsmath}
\usepackage{fontspec}

\newfontface{\assocfont}{Consolas}[NFSSFamily=assoc]
\newfontface{\wmmono}{MathematicaMono}[
  Path=./,
  Extension=.ttf,
  NFSSFamily=wmmono,
  UprightFont=*,
  BoldFont=*-Bold
]

\DeclareMathVersion{mono}

\DeclareSymbolFont{assoc}{TU}{assoc}{m}{n}
\SetSymbolFont{assoc}{mono}{TU}{assoc}{m}{n}
\DeclareSymbolFontAlphabet{\mathassoc}{assoc}
\DeclareSymbolFont{wmmono}{TU}{wmmono}{bx}{n}
\SetSymbolFont{wmmono}{mono}{TU}{wmmono}{bx}{n}

\newcommand{\association}[1]{{%
  \mathversion{mono}%
  % alphabetical symbols from Consolas
  \Umathcode`"= 7 \symassoc `"
  \Umathcode`α= 7 \symassoc `α
  \Umathcode`β= 7 \symassoc `β
  \Umathcode`,= 6 \symassoc `,
  % other symbols from MathematicaMono
  \Umathcode`→= 3 \symwmmono "F522
  \Umathchardef\openassociation= 4 \symwmmono "F113
  \Umathchardef\closeassociation= 5 \symwmmono "F114
  $\openassociation\mathassoc{#1}\closeassociation$%
}}

\begin{document}

\association{1 → "a",2 → "b"}

\association{1 → "α",2 → "β"}


\end{document}

enter image description here