2

I have a simple macro that I often use in equations to write a comment

\newcommand{\com}[1]{,\text{ #1 }}

I also use the same macro to write formulas with multiple cases

\documentclass{article}
\usepackage{amsmath}
\newcommand{\com}[1]{,\text{ #1 }}
\begin{document}
\[
100\delta_n :=
\begin{cases}
100 &\com{if} n=0 \\
0 &\com{else}
\end{cases}
\]
\end{document}

The issue is that it is (without any surprise) rendered as this

enter image description here

I would like to modify the macro (preferably without adding optional arguments ) in way that when it follows an alignment character &, the comma is swapped with & to rather have this rendering

enter image description here

Is this even possible?


EDIT: A possible approach to avoid all the problem would make use of \mkern

100\delta_n :=
\begin{cases}
100 &\mkern-18mu,\mkern16mu\text{ if } n=0 \\
0 &\mkern-36mu,\mkern34mu\text{ else }
\end{cases}

which outputs the desired effect. Though to generalize, we would need to know the space created in cases environnement. Maybe we could redefine cases or align to make so that the space created is stored in an external variable each time it is computed, and that we could access in the com macro.

7
  • 1
    Not possible, unless you preprocess the environment's contents to do the swapping. But I don't think this is a good way to add comments, which generally should not be part of a formula. Say you have $x=y\com{if} n>0$: this should be instead $x=y$, if~$n>0$.
    – egreg
    Commented May 19 at 16:00
  • Wouldn't it be possible to detect the alignement, ignore it, set the comma then set another alignement character? And your example doesn't apply to displaystyle mathmode.
    – Nolord
    Commented May 19 at 16:32
  • 1
    you can't really swap, what you could do (perhaps) is detect if you were at the start of a math list and if so omit the comma, then in such an alignment you just need to add the ' in the previous cell Commented May 19 at 16:32
  • your suggested algorithm would not produce the layout requested (and would be technically difficult) it would produce a column just consisting of commas so they would all be in a vertical line not tight after the math expression as you show Commented May 19 at 16:35
  • Do you have an idea on how would you do what you wrote in your first comment? As long as it replicates the same result, that's good for me. And for your second comment, I think you misunderstood, I meant ignoring the alignement as in not taking it into account. Of course I don't want a column of commas.
    – Nolord
    Commented May 19 at 16:38

1 Answer 1

4

You can't really go back and add a , to the previous cell, but you can omit the comma if you are at the start of a math list, so can add the earlier commas explicitly:

enter image description here

\documentclass{article}
\usepackage{amsmath}
\newcommand{\com}[1]{\ifnum\lastnodetype>-1,\fi\text{ #1 }}
\begin{document}
some $x=y\com{test} ?$

\[
  a \com{hmm}
100\delta_n :=
\begin{cases}
100, &\com{if} n=0 \\
0, &\com{else}
\end{cases}
\]
\end{document}

You must log in to answer this question.

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