2

It happens sometimes that because I deal with complicated formulas, I want to add extra space around certain symbols, e.g. \longrightarrow, \subset, \cong, etc.

Here is a MWE:

\documentclass{article}
\usepackage{ifthen}

\let\OLDsubset\subset
\renewcommand{\subset}[1][0]{
    \ifthenelse{\equal{#1}{0}}{\OLDsubset}{}
    \ifthenelse{\equal{#1}{1}}{\;\OLDsubset\;}{}
    \ifthenelse{\equal{#1}{2}}{\;\;\OLDsubset\;\;}{}
    \ifthenelse{\equal{#1}{3}}{\quad\OLDsubset\quad}{}
}

\begin{document}
$$A \subset B$$
$$A \subset[2] B$$
\end{document}

It works well, but my question is the following. Is there a way to create a single 'meta-command' (macro) e.g. \AddSpace that takes a command as argument, and then for instance \AddSpace{\subset} would produce the above code automatically.

The goal is to be able to use the same code as above for other commands as \subseteq, \cong (that do not initially take optional arguments, for safety), etc., without having to copy-paste the same piece of code for each of those commands.

I am aware of multidef package, but it doesn't seem to be the right tool here. I tried to play around the ideas from here, without much success. Maybe with xparse package, and \RenewDocumentCommand, etc.?


NB: I do not want to have something like this:

\newcommand{\AddMoreSpace}[2][1]{
    \ifthenelse{\equal{#1}{1}}{\;#2\;}{}
    \ifthenelse{\equal{#1}{2}}{\;\;#2\;\;}{}
    \ifthenelse{\equal{#1}{3}}{\quad #2 \quad}{}
}

because using e.g.

$$A \AddMoreSpace{\subset} B$$
$$A \AddMoreSpace[2]{\subset} B$$

is too "complicated" / too long to write...

3
  • (Other commands: \cong, \Equal (create new command for "="), \leq, \geq, \ll, \gg, \to, \mapsto, \into, \longmapsto, \longrightarrow, \implies, \cdot, \Plus (to be created), \oplus, \times, \otimes, ...)
    – Watson
    May 8 at 14:39
  • I don't see why this isn't achievable with a for loop and \csname and \endcsname to pass the command name as a "string". I would pay attention to global definitions here. You basically want to zip two lists together, one with a command sequence, and one with the spacing category, right? And then redefine accordingly? My knee jerk reaction would tell me to use LuaLaTeX because that's my comfort zone. May 8 at 14:56
  • I think I initially misunderstood the problem. Disregard all but the first two sentences. May 8 at 15:27
2

The command \; is shorthand for \mskip\thickmuskip, so you can multiply it, instead of adding.

The old command name is saved as \watson<oldcommand using a \csname trick.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\AddSpaceToCommand}[1]{%
  \expandafter\NewCommandCopy\csname watson\string#1\endcsname #1%
  \renewcommand#1[1][0]{%
    \mspace{\muexpr\thickmuskip*##1\relax}%
    \csname watson\string#1\endcsname
    \mspace{\muexpr\thickmuskip*##1\relax}%
  }%
}

\AddSpaceToCommand{\subset}
\AddSpaceToCommand{\mapsto}

\begin{document}

$a \subset b$

$a \subset[1] b$

$a \subset[2] b$

$a \subset[3] b$

$a \mapsto b$

$a \mapsto[1] b$

$a \mapsto[2] b$

$a \mapsto[3] b$

\end{document}

enter image description here

1

After searching quite a bit, I finally found a way to solve my problem. Any comment is welcome -- the following MWE might show some "bad practice" (but at least it works!)...

\documentclass{article}
\usepackage{ifthen}

\newcommand{\AddSpace}[1]{
    \expandafter\let\csname old#1\expandafter\endcsname\csname#1\endcsname
    \expandafter\renewcommand\csname #1\endcsname[1][0]{%
        \ifthenelse{\equal{##1}{0}}{\csname old#1\endcsname}{}%
        \ifthenelse{\equal{##1}{1}}{\;\csname old#1\endcsname\;}{}%
        \ifthenelse{\equal{##1}{2}}{\;\;\csname old#1\endcsname\;\;}{}%
        \ifthenelse{\equal{##1}{3}}{\quad\csname old#1\endcsname\quad}{}%       
    }%
}

\AddSpace{subset}
\AddSpace{mapsto}


\begin{document}

$a \subset[3] b$

$a \subset b$

$a \mapsto[2] b$

$a \mapsto b$

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.