# Force inline-type superscript for display equations (but display-type subscript)

I am trying to create a macro \jjoin for a mathematical operator which roughly produces the following behaviour:

Inline: \bigvee^{(J)}, subscript (typically i \in I) goes in the usual position (as in rendering \bigvee^{(J)}_{i \in I});
Displaystyle: The (J) goes in the same spot as in inline equations, i.e. to the top-right, but the i \in I goes below the \bigvee as is usual in display style.

I have tried the following definitions for \jjoin, to no avail:

• \mathop{\bigvee{}^{(J)}} (the i \in I appears too far to the right, because the (J) part is taken into consideration for determining the middle of the expression);
• \makebox[0]{\ensuremath{\hphantom{\bigvee}^{(J)}}}\bigvee (I'm not sure why this fails, but the position of (J) is wildly varying; perhaps because \ensuremath does not remember whether or not \jjoin is invoked in display mode?);

but that's as far as my knowledge of (La)TeX goes.

I have seen #99538 but the result is the same as my first attempt above.

MWE:

\documentclass[a4paper]{article}
\usepackage{amssymb,amsmath}

\newcommand{\jjoin}{\mathop{\bigvee\nolimits^{(J)}}}

\begin{document}
Inline: $\jjoin \qquad \jjoin_{i\in I}$

Display: $\jjoin \qquad \jjoin_{i\in I}$
\end{document}


NB. This is my first question here on TeX.SE; I hope that I didn't make too many beginner's errors. Feel free to point them out.

• Hi! 1) Please provide a full Minimal Working Example (the only beginner's error I think ;) ). 2) Have you tried \mathop{\bigvee\nolimits^{(J)}}\limits_{i\in I\phantom{(J)}} ?
– yo'
Jun 5, 2013 at 21:56
• @tohecz Added MWE; the code you provide works for the specific case, but doesn't provide the required flexibility (I'd like it to work with any subscript). Jun 5, 2013 at 22:05
• That's four great answers -- within the hour! I thank you all for a very warm welcome to a mundane TeXnician. Jun 5, 2013 at 22:47

This should work exactly as desired:

\documentclass[a4paper]{article}
\usepackage{amssymb,amsmath}

\pagestyle{empty}

\makeatletter
\newcommand*\jjoin{\@ifnextchar_\jj@in\jj@@n}
\newcommand*\jj@@n{\bigvee\nolimits^{(J)}}
\newcommand*\jj@in[2]{\mathchoice
{\mathop{\jj@@n}_{#2\phantom{(J)}}}%
{\jj@@n_{#2}}%
{\jj@@n_{#2}}%
{\jj@@n_{#2}}%
}
\makeatother

\begin{document}
Inline: $A=\jjoin B \qquad A=\jjoin_{i\in I} B_i$

Display: $A=\jjoin B \qquad A=\jjoin_{i\in I} B_i$
\end{document}

• Yep, that's exactly it. I'll have to remember \mathchoice, it's good to be aware of that one. Jun 5, 2013 at 22:30
• @Lord_Farin Just remember never to use multiple \mathchoice at once, and never to mix \mathchoice with \mathpalette. The complexity of the typesetting is exponential (4^n) in number of nested instances of these two commands ;)
– yo'
Jun 5, 2013 at 22:38
• If the subscript has a larger width, then the added space (\phantom) becomes too large. Jun 5, 2013 at 22:52
• @HeikoOberdiek If the subscript has a larger width, every good typesetter adjusts the overlaps manually anyways, and can therefore take case of this.
– yo'
Jun 6, 2013 at 6:01
• @tohecz: A good typesetter would not like unpredictable spacing. Jun 6, 2013 at 7:47

\documentclass{article}
\usepackage{mathtools}

\begin{document}

$\mathop{\bigvee{}^{\mathrlap{(J)}}}_{i\in I}$
\bigskip
$\mathop{\bigvee{}^{\mathrlap{(J)}}}_{i\in I}$
\end{document}


It's easier to control the space after if you change the markup a bit:

\documentclass{article}
\usepackage{mathtools}

\begin{document}

$\mathop{\bigvee{}^{\mathrlap{(J)}}}_{i\in I}X$
\bigskip
$\mathop{\bigvee{}^{\mathrlap{(J)}}}_{i\in I}X$

\newcommand\foo[1][]{%
\mathop{{}\bigvee{}^{\mathrlap{(J)}}}_{#1}\mathchoice
{\scriptstyle\phantom{(J)}}{}{}{}}

$\foo[i\in I] X$
\bigskip
$\foo[i\in I] X$
\end{document}

• A following letter clashes with the superscript in the display style case. Jun 5, 2013 at 22:07
• Seems I was overexcited. Upon placing A_i behind it, the (J) overlaps, as Heiko remarks :(. Jun 5, 2013 at 22:10
• @Lord_Farin although beware Heiko's comment: this hides the width of (J) completely so you may need to follow it by some space to compensate Jun 5, 2013 at 22:10
• @Lord_Farin updated oops not right in inline case: will fix.... Jun 5, 2013 at 22:18
• I've solved it by passing the subscript to the macro as a parameter, following it by \hphantom{{}^{(J)}}. (Hm, that's what you did in your edit too. I'll treat that as a compliment to myself. :) (But this doesn't work with inline case :(; thankfully, more answerers have arrived.)) Jun 5, 2013 at 22:19

This answer addresses the remaining problem of David Carlisle's solution that a following symbol clashes with the superscript in the display style version.

\documentclass[a4paper]{article}
\usepackage{amssymb,amsmath}
\usepackage{mathtools}

\makeatletter
\newcommand*{\jjoin}{%
\@ifnextchar_{\@jjoin}{%
\@ifnextchar\sub{\@jjoin@sub}{%
\bigvee\nolimits^{(J)}%
}%
}%
}
\def\@jjoin_#1{%
\mathpalette\@@jjoin{#1}%
}
\def\@jjoin@sub\sub#1{%
\mathpalette\@@jjoin{#1}%
}
\def\@@jjoin#1#2{%
\ifx#1\displaystyle
\sbox0{$\m@th\displaystyle\bigvee$}%
\sbox2{$\m@th\displaystyle\bigvee\nolimits^{(J)}\kern-\scriptspace$}%
\sbox4{$\m@th\displaystyle\bigvee_{#2}\kern-\scriptspace$}%
\mathop{\bigvee\nolimits^{\mathrlap{(J)}}\kern-\scriptspace}_{#2}%
\dimen@=\dimexpr
\wd2-\wd0 %
\ifdim\wd4>\wd0 %
-.5\dimexpr\wd4-\wd0\relax
\fi
\relax
\ifdim\dimen@>\z@
\kern\dimen@
\fi
\else
\bigvee\nolimits^{(J)}_{#2}%
\fi
}
\makeatother

\begin{document}
Inline: $A\jjoin X \qquad A\jjoin_{i\in I}X$

\bigskip

Display: $\displaystyle A\jjoin X\qquad A\jjoin_{i\in I}X$

\bigskip

Inline: $A\jjoin_{i\in I\cup K}X \qquad A\jjoin_{i\in I\cup K\cup L}X$

\bigskip

Display:
$\displaystyle A\jjoin_{i\in I\cup K}X \qquad A\jjoin_{i\in I\cup K\cup L}X$
\end{document}


Remarks:

• A following subscript is caught as macro argument if it is preceded by _ or \sub.
• \displaystyle is detected via using \mathpalette.
• Special treatment is only needed, if \displaystyle is active and a subscript is given. Then the amount of the overlapping superscript (J) is calculated and added as \kern after the expression.
• Even numbered box registers below ten can be used for local assignments.
• \m@th kill the effect of \mathsurround, because we are inside a formula.
• \mathrlap is not enough to get a superscript without horizontal spacing, because TeX adds \scriptspace after sub- or superscripts.
• Because of \mathpalette and the measurement of the subscript in display style, the subscript is called five times. Unless the subscript contains global assignments or commands with side effects (e.g. \write, \typeout), this is usually not a problem. However \mathpalette/\mathchoice is the only way to get the math style in a reliable way, because of TeX's \over and similar primitives.
• +1 :-) (so now I voted for everyone except me on this question:( Jun 5, 2013 at 22:48
• @DavidCarlisle: +1 for you, because I have based my solution on yours. Jun 5, 2013 at 22:55
• Now you expand #2 twice :-/ No solution is perfect.
– yo'
Jun 6, 2013 at 5:59
• @tohecz: You are forgetting \mathpalette that calls the TeX primitive \mathchoice that sets the formula in all four math styles first, before TeX selects the right math style later. Thus the subscript is executed five times. Jun 6, 2013 at 8:04
• @HeikoOberdiek \sbox4{$\m@th\displaystyle\bigvee_{#2}\kern-\scriptspace$}\mathop{\bigvee\nolimits^{\mathrlap{(J)}}\kern-\scriptspace}_{#2}%
– yo'
Jun 6, 2013 at 13:12