# How to format an Association in Latex?

I'm trying to format an Association from Mathematica which looks like this:

This is what I have so far, but it doesn't look the same:

\documentclass{article}
\usepackage{graphicx} % for \scalebox macro
\newcommand\rightassn{
\mathrel{\ooalign{\hss$|$\hss\cr%
\kern0.9ex\raise0.2ex\hbox{\scalebox{0.7}{$\rangle$}}}}}

\newcommand\leftassn{\mathrel{\ooalign{\hss\raise0.2ex\hbox{\scalebox{0.7}{$\langle$}}\hss\cr%
\kern0.9ex\hbox{$|$}}}}

\begin{document}
$\leftassn 1 \rightarrow a, 2 \rightarrow b \rightassn$
\end{document}


• “More exactly” means using a monospaced font? – egreg Dec 19 '18 at 10:36
• @egreg that's a start, I tried that and they still don't look nearly the same. Maybe changing the symbols I chose to kludge together will help... – M.R. Dec 19 '18 at 10:45
• @M.R. My point is that when people ask for clarifications in comments, as happened here, you should update your question, not provide more details in comments, as you did above. – Sverre Dec 19 '18 at 20:04
• Barring some font choices, how does this look? – Werner Dec 28 '18 at 6:57
• On Windows, Mathematica uses consolas; while on Linux/macOS, it uses Source Code Pro. The special symbols, are from Mathematica Mono. – stone-zeng Dec 28 '18 at 7:25

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:

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

• Is there some way to add the spaces automatically? – M.R. Dec 28 '18 at 7:52
• I guess no. To get the correct space, you need use the math mode; the correct font, however, cannot be used (at least easily) in the math mode. I think we need more setup to format the code in LaTeX as in Mathematica. – stone-zeng Dec 28 '18 at 8:09

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}
`