
Tengo un comando, alias de una expresión compleja, que uso con frecuencia y me gustaría negarlo sin introducir un nuevo alias.
Una solución simple introduce el tercer argumento con el valor predeterminado, pero es complicado escribirlo \myCommand[\neq]{arg1}{arg2}
, algo así \not\myCommand{arg1}{arg2}
es mucho mejor.
Ejemplo:
\newcommand{\divides}[2]{\ensuremath{#1\mid #2}}
Gracias por ambas respuestas, ahora veo dónde profundizar.
Respuesta1
Propongo una sintaxis con una variante * en lugar del \not
prefijo.
\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{xparse}
\NewDocumentCommand{\newrelation}{mmo}{%
% #1 is the command to define
% #2 is the relation to be used
% #3 (optional) is the alternative
\IfNoValueTF{#3}
{\NewDocumentCommand{#1}{smm}{%
\IfBooleanTF{##1}{##2\not#2##3}{##2#2##3}%
}%
}
{\NewDocumentCommand{#1}{smm}{%
\IfBooleanTF{##1}{##2#3##3}{##2#2##3}%
}%
}%
}
\newrelation{\EQ}{=}
\newrelation{\LESS}{<}
\newrelation{\divides}{\mid}[\nmid]
\begin{document}
$\EQ{a}{b}$ and $\EQ*{a}{b}$
$\LESS{a}{b}$ and $\LESS*{a}{b}$
$\divides{a}{b}$ and $\divides*{a}{b}$
\end{document}
Decir \newrelation{\EQ}{=}
es equivalente a escribir.
\NewDocumentCommand{\EQ}{smm}{%
\IfBooleanTF{#1}% true if * is present
{#2\not=#3}%
{#2=#3}%
}
mientras \newrelation{\divides}{\mid}[\nmid]
es como escribir
\NewDocumentCommand{\divides}{smm}{%
\IfBooleanTF{#1}% true if * is present
{#2\nmid#3}%
{#2\mid#3}%
}
Por ejemplo, como \not\in
está mal, podrías hacer
\newrelation{\IN}{\in}[\notin]
y los resultados estarán debidamente tipografiados. Del mismo modo, \not\mid
da un mal resultado y \nmid
debería preferirse.
Si prefiere usar un prefijo, aquí está (mismo resultado):
\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{xparse}
\NewDocumentCommand{\newrelation}{mmo}{%
% #1 is the command to define
% #2 is the relation to be used
% #3 (optional) is the alternative
\IfNoValueTF{#3}
{%
\NewDocumentCommand{#1}{mm}{##1#2##2}%
\expandafter\NewDocumentCommand\csname negate\string#1\endcsname{mm}{##1\not#2##2}%
}%
{%
\NewDocumentCommand{#1}{mm}{##1#2##2}%
\expandafter\NewDocumentCommand\csname negate\string#1\endcsname{mm}{##1#3##2}%
}%
}
\makeatletter
\NewDocumentCommand{\negate}{m}{%
\@ifundefined{negate\string#1}
{\@latex@error{Undefined relation}{}#1}{\@nameuse{negate\string#1}}%
}
\makeatother
\newrelation{\EQ}{=}
\newrelation{\LESS}{<}
\newrelation{\divides}{\mid}[\nmid]
\begin{document}
$\EQ{a}{b}$ and $\negate\EQ{a}{b}$
$\LESS{a}{b}$ and $\negate\LESS{a}{b}$
$\divides{a}{b}$ and $\negate\divides{a}{b}$
\end{document}
Respuesta2
Se requiere trabajo de configuración, pero se puede hacer. En esencia, ha definido varias macros que realizan el "símbolo A B" deseado para varios símbolos, y luego debe definir \negate
para saber cómo negar cada uno de estos símbolos, por turno.
\documentclass{article}
\usepackage{stackengine,xcolor,mathtools}
\def\eqsym{=}
\def\gtsymbol{>}
\let\svmid\mid
\let\svgtsymbol\gtsymbol
\newcommand{\divides}[2]{\ensuremath{#1\mid #2}}
\newcommand{\equals}[2]{\ensuremath{#1\eqsym #2}}
\newcommand\greaterthan[2]{\ensuremath{#1\gtsymbol #2}}
\newcommand\negate[1]{%
\ifx\divides#1\def\mid{\mathrlap{\,/}\svmid}\else%
\ifx\equals#1\let\eqsym\neq\else%
\ifx\greaterthan#1\def\gtsymbol{\mathrlap{\,\,/}\svgtsymbol}\else%
\fi\fi\fi%
#1%
}
\begin{document}
$\divides{1}{2} \quad \negate\divides{1}{2}$
$\equals{1}{2} \quad \negate\equals{1}{2}$
$\greaterthan{1}{2} \quad \negate\greaterthan{1}{2}$
$\divides{1}{2} \quad \equals{1}{2} \quad \greaterthan{1}{2}$
\end{document}
APÉNDICE:
Una forma de combinar la posible facilidad de las definiciones de egreg con la sintaxis de mi enfoque sería usar su MWE, con la definición adicional:
\def\negate#1{#1*}
De ese modo,
$\EQ{a}{b}$ and $\negate\EQ{a}{b}$
$\LESS{a}{b}$ and $\negate\LESS{a}{b}$
$\divides{a}{b}$ and $\negate\divides{a}{b}$
produciría el mismo resultado que su MWE.