# Context-aware dots at the end of formulas

amsmath defines \dots which does a look-ahead and it uses different dots (vertically centred or not, basically) depending on the next token. See How do magic \dots work in amsmath?

However, because of this (at least, this is my understanding), it does not work when the dots are at the end of the formula:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

$x + \dots + x + \dots$

\end{document}


Now, it is not a big deal when using + because I can directly use \dotsb. However, custom commands can also be made "dots-aware":

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\newcommand*\myop{\DOTSB\rightarrow}

$x \myop \dots \myop x \myop \dots$

\end{document}


In this case I would like the last \dots to automatically adjust itself depending on the definition of \myop. How can this be done?

I do not necessarily expect \dots to "remember" what happened before (though it would be nice), it would be ok to give it some kind of hint by repeating the operator after the dots:

$x \myop \dots \myop x \myop \dots \hint\myop$


EDIT: or even better:

$x \myop \dots \myop x \myop \dotsfor{\myop}$


Maybe a look-ahead macro looking at the first token of its argument could be used? Though it would not be just look-ahead, the rest of the argument should be discarded.

• Why don’t you just use \dotsb for this one occurrence? Any automated solution will have corner cases and frankly \dots\hint\myop is much longer to type than \dotsb. – Henri Menke Jan 31 '17 at 11:24
• @HenriMenke Because if I decide to change \myop then I have to find and replace all those occurrences of \dotsb. – effeffe Jan 31 '17 at 11:28
• Why not have \def\myopdots{\myop\dotsb} then? – Henri Menke Jan 31 '17 at 12:40
• @HenriMenke That is not too bad, though then two macros would need to be defined for each operator. I thought that a macro taking only the first token of its argument and discarding the rest could work... – effeffe Jan 31 '17 at 13:14

this doesn't seem to be explained in any readily available documentation, but there is an explanation in the joy of tex (the manual for ams-tex) which is where the dot variations were first defined. from p.156:

Thus, when you use \dots in math mode, the particular kind of dots to be typeset is determined both by the style of and by the next symbol in the formula. Unfortunately, there is one situation where this scheme fails, namely, when there isn't any next symbol. If you type [examples showing \dots at the end of a formula] then \dots can't be expected to know which sorts of dots you want -- it can only look ahead, and has no way of knowing what symbol was typed before it -- and \dots simply chooses \dotso in all those cases. So when a formula ends with dots, the proper thing to do is to tell AMS-TeX which sort of dots it should be using.

at the time ams-tex was created, this problem was thought about carefully, and the edge cases were too unpredictable to consider.

i think the two-macro approach is your best option: \myop and \myopdots (or other, possibly shorter, name for the second); easy to find in your file if you need to change.

edit: as pointed out by @Zarko in a comment, \dotsm (for "multiplication dots") would be appropriate, as would \dotsb ("dots with binary operators/relations"). both resolve to \cdots.

• Good reference! Since I am not very comfortable with the most technical issues of (La)TeX, may I ask you what is so hard/dangerous/unpredictable (or maybe just plain wrong) about implementing the idea I put in my edit? – effeffe Jan 31 '17 at 15:55
• @effeffe -- the code necessary to implement this could be rather complicated, and it would still not likely catch all the possible edge cases. thus it's not really "cost effective". the fact that tex is a "one-way" system -- it can't backtrack -- does make this inconvenient, but knuth did comment in the texbook that exceptions of this sort are not all that frequent, and best handled on a case-by-case basis. – barbara beeton Jan 31 '17 at 17:03
• @barbarabeeton, in OP case the \dotsm defined amsmath seems to be a solution: $x + \dots + x + \dotsm$ or even more cosistent $x + \dotsm + x + \dotsm$ . – Zarko Jan 31 '17 at 18:06
• @Zarko -- yes, it should be. information added. thanks. however, take a look at section 4.3, p.12, of the amsmath user's guide, which recommends \dotsb associated with plus signs. – barbara beeton Jan 31 '17 at 18:36
• @Zarko That is what I already wrote in my answer: it was just an example, the problem is about user-defined operators, which could be either "low" (like commas) or "vertically centred" (like plus). – effeffe Jan 31 '17 at 19:17