# What wrapping should I use to create a new symbol?

It is a truth universally acknowledged, that a mathematician in possession of a good theorem must be in want of notation.1

It is as true, but perhaps less universal, that unicode doesn't contain every symbol under the sun.

So every now and then some brave soul sets forth and defines a new symbol, possibly using TikZ, possibly by drawing it in an external graphics program, or some other arcane ritual like the picture environment.

This question is not about creating the symbol, but about what to do with it then - with particular focus on its use in math mode. For the purposes of this question, suppose that I have a construction of a symbol which contains within it some way of resizing it (for example, in How to turn a figure into a symbol? then it is an external picture included via \includegraphics and the height option can be used, in a TikZ picture then a scale option can be given).

To use that symbol in mathematics, there are two things to sort out: size and spacing. Spacing is handled via \mathop, \mathbin, or \mathrel, size presumably needs \mathchoice to get it right (or can we use ex dimensions to avoid this - that is, in a subscript is 1ex set to the subscript font or to the normal font?). (I'm going to assume that the symbol definition sets the baseline correctly.)

So how should I define the wrapping of the symbol?

Here's some code to play with. The goal is for the new symbol (in red) to behave like the \neg symbol in both sizing and spacing (I'm presuming that code to make it behave like \neg will be easily adapted to, say, = or + by replacing the \mathwhatever by \mathrel or \mathbin). Let's assume that the size (and baseline) are correct for the \textstyle version, but as is clear from the image then the spacing is not correct.

\documentclass{article}
%\url{https://tex.stackexchange.com/q/384784/86}
\usepackage{tikz}

\newcommand\sqsymbol[1]{
\tikz[x=#1,y=#1] {\draw[red,line cap=round,line join=round] (0,.83) -| ++(1.2,-.53) (0,0) coordinate (a);}
}
\newcommand\sq[0]{\sqsymbol{1ex}}

\begin{document}

$$\displaystyle A \sq B$$, $$A \sq B$$, $$A_{C \sq B}$$, $$A_{C_{D \sq B}}$$

$$\displaystyle A \neg B$$, $$A \neg B$$, $$A_{C \neg B}$$, $$A_{C_{D \neg B}}$$

\end{document}


Here's what that code produces:

1 Yeah, I know I've used that before, but I like the line.

• The width of \sq is different than \neg. That has nothing to do with the [empty] spacing, but do you wish the non-blank portion of \sq to take the same horizontal space as \neg? – Steven B. Segletes Aug 4 '17 at 11:54
• @StevenB.Segletes Hmm, good point. My intention is that that not be the focus of this question so I've replaced the square by a picture that (at 1ex) is almost an exact copy of the \neg symbol. Hopefully that ensures that the focus is on the spacing and relative size. – Andrew Stacey Aug 4 '17 at 12:10

The question is open-ended, but I will toss some things out there. It wasn't clear whether by saying "behave like", you wanted \sq to occupy the same horizontal space as \neg. If so, one could either increase the size of \sq or add extra space around it. But, as to handling the sizing of the new symbol, the scalerel package scales one symbol the vertical footprint of another, while preserving the current math style. Thus, if I tell \sq to take up the same vertical footprint as \neg, then since \neg changes size with the math style, so will \sq.

In addition, scalerel provides (in the context of scalerel arguments) the lengths \LMex and \LMpt (LM standing for "local mathstyle"), which provide the length of 1ex and 1pt, scaled to the current math style. This allows dimensions to be scaled to lower math styles.

But, as is often the case,some dimensions are not scaled to smaller math styles, for example glyph sidebearings(*) are often not (at the discretion of the font designer). To handle this non uniformity of scale, if I am trying to mimic the spacing of a pre-existing glyph (such as \neg), I can combine both dimensions (pts and \LMpts). So, for example, when I say \kern1.23pt\kern-.9\LMpt, this is .33pt in text and display style, but more than that in the smaller math styles because, in the smaller math styles, .9\LMpt is smaller than .9pt. Such techniques can be used to correct for the glyph sidebearing issue.

Fine tuning can be achieved by tweaking the scalerel-assumed scale factors associated with \scriptstyle and \scriptscriptstyle, which by default are

\def\scriptstyleScaleFactor{0.7}
\def\scriptscriptstyleScaleFactor{0.5}


As a point of interest, I invoke \sqsymbol with an argument of 1\LMex. Try replacing that with 1ex and see the difference at the smaller math styles.

The MWE:

\documentclass{article}

\usepackage{tikz,scalerel}

\newcommand\sqsymbol[1]{%
\tikz[x=#1,y=#1] {\draw[red,line cap=round,line join=round] (0,.83) -| ++(1.2,-.53) (0,0) coordinate (a);}%
}
\newcommand\sq[0]{\ThisStyle{\kern1.23pt\kern-.9\LMpt\scalerel*{\sqsymbol{1\LMex}}{\neg}%
\kern1.23pt\kern-.9\LMpt}}

\begin{document}
$$\displaystyle A \sq B$$, $$A \sq B$$, $$A_{C \sq B}$$, $$A_{C_{D \sq B}}$$

$$\displaystyle A \neg B$$, $$A \neg B$$, $$A_{C \neg B}$$, $$A_{C_{D \neg B}}$$

\end{document}


One might observe that, in \displaystyle, the \sq symbol is wider than \neg, while in \scriptscriptstyle, it is narrower. The reason for this can be explained. The glyph designer of \neg chose to change the aspect ratio of \neg in the smaller math styles (the vertical stub becomes relatively shorter). In contrast, the aspect ratio of \sq , defined generically, remains constant. Since \sq is scaled to the vertical footprint of \neg, the relative widths of \neg and \sq will change as the aspect ratio of \neg changes.

An alternative would be, not to scale \sq to the vertical footprint of \neg, but to the vertical footprint of something that changes both its absolute as well as relative height at smaller math styles, for example, \rule[-.12ex]{0pt}{\dimexpr.24ex+.7\LMex}}\kern.5pt}. Doing it this way allows the sidebearings to now be more simply expressed as a fixed .5pt, using

\renewcommand\sq[0]{\ThisStyle{\kern.5pt\scalerel{\sqsymbol{1ex}}{%
\rule[-.12ex]{0pt}{\dimexpr.24ex+.7\LMex}}\kern.5pt}}


Showing the original in line 1, this revised in in line 2, \neg in line 3, and the overlay of lines 2 and 3 in line 4:

\documentclass{article}
\usepackage{tikz,scalerel,stackengine}
\newcommand\sqsymbol[1]{%
\tikz[x=#1,y=#1] {\draw[red,line cap=round,line join=round] (0,.83) -| ++(1.2,-.53) (0,0) coordinate (a);}%
}

\newcommand\sq[0]{\ThisStyle{\kern1.23pt\kern-.9\LMpt\scalerel*{\sqsymbol{1\LMex}}{\neg}%
\kern1.23pt\kern-.9\LMpt}}

\begin{document}
$$\displaystyle A \sq B$$, $$A \sq B$$, $$A_{C \sq B}$$, $$A_{C_{D \sq B}}$$

\renewcommand\sq[0]{\ThisStyle{\kern.5pt\scalerel{\sqsymbol{1ex}}{%
\rule[-.12ex]{0pt}{\dimexpr.24ex+.7\LMex}}\kern.5pt}}

$$\displaystyle A \sq B$$, $$A \sq B$$, $$A_{C \sq B}$$, $$A_{C_{D \sq B}}$$

$$\displaystyle A \neg B$$, $$A \neg B$$, $$A_{C \neg B}$$, $$A_{C_{D \neg B}}$$

\stackengine{0pt%
}{$$\displaystyle A \neg B$$, $$A \neg B$$, $$A_{C \neg B}$$, $$A_{C_{D \neg B}}$$
}{$$\displaystyle A \sq B$$, $$A \sq B$$, $$A_{C \sq B}$$, $$A_{C_{D \sq B}}$$
}{O}{l}{F}{F}{L}
\end{document}


My answer here, How are big operators defined?, discusses the issue further, when differences between \displaystyle and \textstyle are relevant.

*Sidebearings are the amount of horizontal dead space that are intrinsically built into a glyph by the font designer. For instance, the code

\fboxsep=0pt
\fbox{$+$}
\fbox{$\neg$}
\fbox{$\scriptstyle\neg$}
\fbox{$\scriptscriptstyle\neg$}


yields

As one can see, the sidebearing space is non-trivial AND it does not necessarily scale with the smaller math styles. The OP, in creating \sqsymbol, created a symbol with no sidebearings. Thus, part of solving the spacing riddle is allotting some sidebearing and deciding whether it should scale at the smaller mathstyles or not. The choice is up to the font/symbol designer. However, it gets complicated if one wants their symbol to mimic the horizontal spacing of another pre-existing glyph. In that case, knowledge of the glyph's sidebearings at the different math sizes is essential to a satisfactory mimicry.

• scalerel is a new one on me, and looks to be very useful (so a +1 for that). I'm aiming for a more "low-level" answer to this particularly with regard to the horizontal spacing. The "magic numbers" in your answer (the \kern.5pt) should be auto-calculated according to TeX's rules for math atoms. – Andrew Stacey Aug 4 '17 at 12:15
• @LoopSpace I hear you, but would point out that a font symbol is provided in all the math styles, and the font designer chooses the sidebearings, etc. at the different sizes. None of that can be automated in TeX's rules for math atoms. It shouldn't really matter if you want your symbol to stand on its own; however, if you want it to mimic the behavior of an existing font glyph, I don't think there is an automatic solution. – Steven B. Segletes Aug 4 '17 at 12:37
• This is looking really good, packed with lots of information. I've never heard the word "sidebearing" before, could you add an explanation as to that? It sounds as though the symbol definition should contain a little more information in it than just the symbol itself. – Andrew Stacey Aug 4 '17 at 14:00
• Thanks. A key part (implied by what you'd written earlier, but nice to have it laid out) is that the sidebearing should be part of the symbol definition, but that to be done properly then that information might not scale linearly. Defining symbols ad-hoc like this would seem to be more complicated than I initially thought! Mind you, there's getting it right and then there's getting it right enough. – Andrew Stacey Aug 4 '17 at 14:24
• @LoopSpace: Allow me to corroborate Steven’s point about nonlinear scaling with some numerical data. Looking at the metrics of the cmsy family, where the glyph for\neg is drawn from (slot O 72), we see that it has CHARWD R 0.666669 and CHARHT R 0.430555 in cmsy10, CHARWD R 0.769845 and CHARHT R 0.430555 in cmsy7, CHARWD R 0.944458 and CHARHT R 0.430554 in cmsy5; recall that all these values are relative to the font design size. Thus we see that the relative height remains the same in all math styles, whereas the glyph gets relatively wider as the size gets smaller. – GuM Aug 5 '17 at 0:22

You definitely need \mathchoice and some time (while on holiday, you can find some).

\documentclass{article}
%\url{https://tex.stackexchange.com/q/384784/86}
\usepackage{amsmath}
\usepackage{tikz}

\newcommand\sqsymbol[1]{%
\tikz[x=#1,y=#1]{%
\draw[red,line cap=round,line join=round] (0,.83) -| ++(1.2,-.43) (0,0) coordinate (a);
}%
}

\newcommand\sq{%
\mathord{% or \mathrel or \mathbin
\mkern1mu
\mathchoice
{\expandafter\sqsymbol\expandafter{\the\dimexpr.42964\fontdimen6\textfont2}}
{\expandafter\sqsymbol\expandafter{\the\dimexpr.42964\fontdimen6\textfont2}}
{\expandafter\sqsymbol\expandafter{\the\dimexpr.41475\fontdimen6\scriptfont2}}
{\expandafter\sqsymbol\expandafter{\the\dimexpr.39672\fontdimen6\scriptscriptfont2}}
\mkern1mu
}%
}

\begin{document}

\sbox0{$A\neg B$}\the\wd0--\sbox0{$A\sq B$}\the\wd0

\sbox0{$\scriptstyle A\neg B$}\the\wd0--\sbox0{$\scriptstyle A\sq B$}\the\wd0

\sbox0{$\scriptscriptstyle A\neg B$}\the\wd0--\sbox0{$\scriptscriptstyle A\sq B$}\the\wd0

$$\displaystyle A \sq B$$, $$A \sq B$$, $$A_{C \sq B}$$, $$A_{C_{D \sq B}}$$

$$\displaystyle A \neg B$$, $$A \neg B$$, $$A_{C \neg B}$$, $$A_{C_{D \neg B}}$$

\leavevmode\rlap{%
$$\displaystyle A \sq B$$, $$A \sq B$$, $$A_{C \sq B}$$, $$A_{C_{D \sq B}}$$%
}%
$$\displaystyle A \neg B$$, $$A \neg B$$, $$A_{C \neg B}$$, $$A_{C_{D \neg B}}$$

\leavevmode\rlap{%
\color{red}%
$$\displaystyle A \sq B$$, $$A \sq B$$, $$A_{C \sq B}$$, $$A_{C_{D \sq B}}$$%
}%
$$\displaystyle A \neg B$$, $$A \neg B$$, $$A_{C \neg B}$$, $$A_{C_{D \neg B}}$$

\leavevmode\rlap{%
$$\displaystyle A \sq B$$, $$A \sq B$$, $$A_{C \sq B}$$, $$A_{C_{D \sq B}}$$%
}%
\color{red}$$\displaystyle A \neg B$$, $$A \neg B$$, $$A_{C \neg B}$$, $$A_{C_{D \neg B}}$$

\end{document}


The height is wrong in subscripts, probably two arguments are needed for getting a perfect result.

The difference in width is less than 0.0001pt (you can't get less, I believe, because of multiple roundings).

Instead of 1mu on either side, you could add something invisible to enlarge the symbol’s bounding box.