2

I am trying to make a macro in the form \eqrel{}{}{} such that it gives #1 \quad #2 \quad #3 if #2 is either an English or Greek letter, and #1 #2 #3 otherwise.

My code:

\documentclass{article}

\usepackage{amsmath}

\newcommand{\eqrel}[3]{
    \ifcat#2\relax
    {#1#2#3}
    \else
    {#1\quad#2\quad#3}
    \fi
}
\begin{document}
\noindent
$ \eqrel{x}{\sim}{y} $\\
$ \eqrel{x}{R}{y} $\\
$ \eqrel{x}{\Omega}{y} $

\end{document}

But this does not detect Greek letters:

enter image description here

Any comment would be appreciated.

6
  • Should also the slanted forms (like \varOmega) be considered? It would be of course possible but I believe it would be much easier to define e.g. a starred form of the macro to use the "spaced" version.
    – campa
    Nov 8, 2021 at 13:41
  • 1
    Isn't it simpler to define \newcommand{\eqrel}[3]{#1\mathrel{#2}#3}?
    – egreg
    Nov 8, 2021 at 13:45
  • @egreg You're right... That's the answer I've been looking for.
    – Hermis14
    Nov 8, 2021 at 13:47
  • @Hermis14 I added also a way to enlarge the space for symbols that are not relations.
    – egreg
    Nov 8, 2021 at 13:57
  • @egreg I used \quad to exaggerate the difference. \mathrel is good enough but thank you for the elaborated answer.
    – Hermis14
    Nov 8, 2021 at 14:04

1 Answer 1

1

This might be doable; in case #1 is a control sequence, you may assume it's a \mathchardef token; then you could check the relative mathcode to see the math group it's taken from.

However, there is a simpler way around:

\newcommand{\eqrel}[3]{#1\mathrel{#2}#3}

so the middle symbol is treated as a relation symbol, independent of its standard nature.

\documentclass{article}

\usepackage{amsmath}

\newcommand{\eqrel}[3]{#1\mathrel{#2}#3}

\begin{document}

$ \eqrel{x}{\sim}{y} $

$ \eqrel{x}{R}{y} $

$ \eqrel{x}{\Omega}{y} $

\end{document}

enter image description here

If you want to use more space around a nonrelation symbol, you can do as follows:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\check@rel}[1]{%
  \sbox\z@{\thickmuskip=0mu${}#1{}$}%
  \sbox\tw@{${}#1{}$}%
  \ifdim\wd\z@=\wd\tw@
    % #1 is not a relation symbol
    \mathrel{\;#1\;}%
  \else
    #1
  \fi
}
\newcommand{\eqrel}[3]{#1\check@rel{#2}#3}
\makeatother

\begin{document}

$ \eqrel{x}{\sim}{y} $

$ \eqrel{x}{R}{y} $

$ \eqrel{x}{\Omega}{y} $

\end{document}

enter image description here

Experiment with the additional space to suit your taste.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .