9

I noticed that LaTeX places the slash created by \not weirdly, e.g. in \not\ni.

Using \centernot from package centernot looks nice, but for reasons outside of this scope I have no control over the source, but still want to change the output.

Usually, in this situation, I'd do \renewcommand*{\not}{\centernot}, but this results in a lot of errors, maybe because \centernot uses \not internally.

Is there a way to achieve this?

projetmbc
  • 13,315
peter
  • 2,895

2 Answers2

7

You get a weird positioning also with \not\in (at least with the Computer Modern fonts) and from the dawn of time TeX provides the command \notin. In LaTeX it's the same as in plain TeX, only with the command made robust. You find it in fontmath.ltx:

\DeclareRobustCommand\notin{\mathrel{\m@th\mathpalette\c@ncel\in}}
\def\c@ncel#1#2{\m@th\ooalign{$\hfil#1\mkern1mu/\hfil$\crcr$#1#2$}}

However, \centernot\in is not optimal anyway. Indeed, the slash used for \notin isn't \not, but a simple /.

It's easy to supplement a command \notni:

\documentclass{article}
\usepackage{centernot} % for the comparison

\makeatletter \DeclareRobustCommand\notni{\mathrel{\m@th\mathpalette\c@ncel\ni}} \makeatother

\begin{document}

\begin{tabular}{ll} Output with \verb|\notni| & $x\notni X$ \ Output with \verb|\not\ni| & $x\not\ni X$ \ Output with \verb|\centernot\ni| & $x\centernot\ni X$ \end{tabular}

\end{document}

enter image description here

You see that even replacing \not with \centernot the result would not be optimal. Just for comparison, here's the same table with in instead of ni:

enter image description here

You can get the best out of both worlds by using an old trick (which is also used by unicode-math.

\documentclass{article}
\usepackage{centernot} % for the comparison

\NewCommandCopy{\standardnot}{\not}

\ExplSyntaxOn

\NewDocumentCommand{\makenot}{mm} { % #1 = symbol to negate, #2 = replacement \exp_args:Nc \NewDocumentCommand{not\cs_to_str:N #1 } {} { #2 } } \RenewDocumentCommand{\not}{m} { \cs_if_exist:cTF { not\cs_to_str:N #1 } { \group_begin: \cs_set_eq:NN \not \standardnot % to keep centernot happy \use:c { not\cs_to_str:N #1 } \group_end: } { \standardnot#1 } }

\ExplSyntaxOff

\makeatletter \makenot{\ni}{\mathrel{\m@th\mathpalette\c@ncel\ni}} \makeatother

\makenot{\to}{\centernot\to}

\begin{document}

\begin{tabular}{ll} Output with \verb|\notni| & $x\notni X$ \ Output with \verb|\not\ni| & $x\not\ni X$ \ Output with \verb|\not\to| & $A\not\to B$ \end{tabular}

\end{document}

With \makenot{<symbol>}{<replacement>} you decide what \not<symbol> should stand for. With this code, \not\ni and \notni are equivalent.

enter image description here

What's the idea? First I alias \not in order to still have the original available. Then I redefine \not with an argument, which should be a control sequence.

Assuming the call is \not\foo, I check whether the control sequence \notfoo is defined and, in this case, use it. Otherwise the standard \not is used.

For instance \not\in would be transformed into \notin out of the box. We can also use \makenot to define new \not<symbol> commands. If we do

\makenot\foo{whatever}

the command \notfoo would be defined to mean whatever. In the example code, \notni is defined as suggested in the first part of the answer. Instead \notto is defined via \makenot\to{\centernot\to}.

There is a safety measure in that the usage of \notfoo is enclosed between \group_begin: and \group_end:, so \not can be redefined to the original meaning inside this group so to make \centernot happy (it uses \not with its original meaning).

Another possible usage is

\makenot{\mid}{\nmid}

so \not\mid would produce the better symbol \nmid; note that neither \not\mid (with the standard \not) nor \centernot\mid produce a nice result (big understatement).

The key is \cs_to_str:N that strips off the backslash leaving just the control sequence name.

egreg
  • 1,121,712
  • interesting, but doesnt meet the premise of my question. i need to be able to make code that already contains \not create different output. i am "happy enough" with the way \centernot looks, i dont want to optimize every occurrence individually. – peter Jul 10 '21 at 08:27
  • @peter I disagree: \centernot\ni is really awful. Anyway, you can do as you like using the code I added. – egreg Jul 10 '21 at 19:55
  • thanks, i like that code. i just hope i'll be able to understand it eventually. – peter Jul 12 '21 at 11:58
  • @peter I added a description – egreg Jul 12 '21 at 13:15
  • wow, that is way smarter than i expected. i'll accept this answer as superior since it doesnt require a package. – peter Jul 13 '21 at 22:38
2

The negated "contains" character is defined in Unicode as character number hex 220c "DOES NOT CONTAIN AS MEMBER" U+220C (∌) and is available in the unicode-math package as \nni and also in some fonts for pdflatex, notably stix2. Using this there is no need to position a slash over the base symbol at all.

enter image description here

\documentclass{article}

\usepackage{stix2}

\begin{document}

$ x \in Y \quad Y \ni x$

$ x \notin Y \quad Y \nni x$

\end{document}

David Carlisle
  • 757,742
  • What do you mean by "Negated contains is Unicode U+220C" (seems incomprehensible)? Do you mean "Negated is contained in Unicode U+220C"? Or something else? – Peter Mortensen Jul 10 '21 at 08:31
  • that would only help in my situation if i were able to make \not\ni produce that character. also wont that stix2 package change the look of every single character in my document? – peter Jul 10 '21 at 08:32
  • @PeterMortensen I mean the Unicode standard includes this negated character in position hex 220C as a single character not as a combination of two symbols. – David Carlisle Jul 10 '21 at 08:34
  • 1
    @peter using stix2 might change every character or might not, Since you gave no indication at all in your question about the fonts you are using, I can't say. stix2 was just an example here, you can read the answer as "some font sets contain this character as a ready made glyph" whether that applies to the fonts you are using, only you can know. – David Carlisle Jul 10 '21 at 08:37
  • @PeterMortensen “negated contains” is the name of the character U+220C. – L. F. Jul 10 '21 at 09:03
  • @L.F. yes I'll re-arrage the sentnce to use a less compressed form of the English – David Carlisle Jul 10 '21 at 09:07
  • @PeterMortensen I edited it a bit I hope it's clearer now. – David Carlisle Jul 10 '21 at 09:10