5

I have some code in which I write this: $\sim_r$. I want to use this command as a binary operator (in the same way as $\sim$ is normally used, but with a subscript).

I would like to create a custom command so that the subscript r appears immediately under the tilde, rather than to the right. I have tried this:

\newcommand{\stb}[1]{{\genfrac{}{}{-2pt}{}{\sim}{#1}}}

but it looks bad because there is too large a gap between the tilde and the r. Does anyone have an easy fix?

Sorry if this is too easy a question!

10
  • Like this: $\underset{r}{\sim}$?
    – Sigur
    Jan 20, 2020 at 14:36
  • Would your problem be solved by just placing a tilde above r? For example with $\widetilde{r}$
    – TivV
    Jan 20, 2020 at 14:39
  • tex.stackexchange.com/a/281887/197451
    – js bibra
    Jan 20, 2020 at 14:40
  • @Sigur: Thanks for your comment. I tried this, but the r floats down below the line -- it's ends up worse than $\genfrac$ unfortunately.
    – Nick Gill
    Jan 20, 2020 at 15:32
  • 1
    Are you going to use that as binary operator? So the \sim should be aligned as a\sim b, right?
    – Sigur
    Jan 20, 2020 at 15:33

2 Answers 2

6

The following uses \mathpalette to negotiate the different math font styles, together with \ooalign to overlay \sim and r into \simr.

enter image description here

\documentclass{article}

\usepackage{amsmath,graphicx}

% \reducesize{<math style>}}{<object>}
\newcommand{\reducesize}[2]{%
  \mathbin{% This will be a binary math symbol (in terms of spacing around it)
    \ooalign{% Overlay a number of symbols
      $#1\sim$% First symbol (\sim in correct math style)
      \cr % Move to next symbol
      \hidewidth% Move symbol to right (~\hfill)
      \raisebox% Adjust vertical positioning of <object>
        {-.25ex}% Move it down relative to current font
        {\scalebox% Change the "font size"
          {.5}% to 50% of current font size
          {$#1#2$}% <object> in current math style
        }% \raisebox
      \hidewidth% Move symbol to left (~\hfill)
    }% \ooalign
  }% \mathbin
}% \reducesize
\newcommand{\simr}{\mathpalette\reducesize{r}}

\begin{document}

$a \simr b_{a \simr b_{a \simr b}}$

\end{document}
1
  • Thanks heaps! I'll look into this option which seems like it will work for me.
    – Nick Gill
    Jan 21, 2020 at 13:08
3

Welcome to TeX.SX! Here is somewhere to get you started, and a comparison of the answers in the comments already given. I'm borrowing from another answer on how to show baselines. Let's compare a few approaches:

enter image description here

This is the code:

\documentclass[margin=5mm]{standalone}

\usepackage{amsmath,stackengine,calc}

\newsavebox\textbox
\newcommand\showbaseline[1]{%
  \leavevmode
  \sbox\textbox{#1}%
  \rlap{\rule{\wd\textbox}{.1pt}}%
  \usebox\textbox
}

\def\stackalignment{c}
\stackMath

\begin{document}

\showbaseline{
$a \sim b$
\hspace{5pt} $a \sim_r b$
\hspace{5pt} $a \genfrac{}{}{-2pt}{}{\sim}{r} b$
\hspace{5pt} $a \underset{r}{\sim} b$
\hspace{5pt} $a \sim_{\hspace{-7pt}r\hspace{3pt}} b$
\hspace{5pt} $a \stackengine{0pt}{\sim}{\scriptstyle{r}}{U}{\stackalignment}{\quietstack}{\useanchorwidth}{\stacktype} b$
\hspace{5pt} $a\ \stackengine{0pt}{\sim}{\scriptstyle{r}}{U}{\stackalignment}{\quietstack}{\useanchorwidth}{\stacktype}\ b$}

\end{document}

From your question it sounds like you want the 5th approach - simply moving the subscript in \sim_r backwards - but this is hacky, because you need to know precise symbol width to get this right (and so does not generalize). I think you actually want the 7th approach - using the stackengine package and adjusting spacing back to what it should be. There is probably a more elegant way than just putting an empty space \ in.

1
  • Thanks. This gives all the options very clearly, I appreciate it. I think @werner's solution and your 7th approach both give good methods for what I have in mind. Thanks again.
    – Nick Gill
    Jan 21, 2020 at 13:10

You must log in to answer this question.

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