4

As suggested by Barbara Beeton here, I am posting this as a new question.

When a proof ends in a displayed formula, to have the QED symbol in the correct place you have to use \qedhere inside the formula.

The problem is that if the formula is given by means of the alignat* environment, then LaTeX issues a (harmless) warning when compiling.

Here is a MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\begin{document}
\begin{proof}
 \begin{alignat*}{2}
  &x && =1 \\
  &y && =2 
  \qedhere
 \end{alignat*}
\end{proof}
\begin{proof}
 \begin{align*}
  x & =1 \\
  y & =2 
  \qedhere
 \end{align*}
\end{document}

Please note that amsmath is called before amsthm, as specified in amsthdoc

When compiling, I get the (double) warning

Package amsthm Warning: The \qedhere command may not work correctly here on input line 10.

Package amsthm Warning: The \qedhere command may not work correctly here on input line 10.

The QED symbol is still there (albeit not flushed to the right).

If you use the environment align* instead of alignat*, on the other hand, everything works flawlessly.

As Barbara said, they should work the same, but it looks like they don't.

P.S.: A way to avoid the warning is to use \tag*{\qedhere} instead of \qedhere, which has also the side effect to flush the QED symbol to the right, restoring it to its intended position, but this looks more like a sort of workaround than the intended use of \qedhere.

brad
  • 517
  • I am doubtful that they "should work the same". Firstly, amsthm.sty explicitly defines handlers for \qedhere for the environments equation, equation*, displaymath, split, align, align*, gather, and gather*. Interestingly none is defined for alignat or alignat*. Secondly, if one just copy over verbatim the definition of the handler for align* and try to use it for alignat*, one gets an incorrect result. So I am not sure whether the omission is an oversight or something deliberate. – Willie Wong Jul 20 '23 at 18:06
  • @WillieWong -- I don't remember any reason why alignat* would have been omitted from the applicable display structures. I've confirmed that this does indeed happen, and I'm quite puzzled. – barbara beeton Jul 20 '23 at 18:11
  • @barbarabeeton: I spent the last 1.5 hours looking at amsthm.sty and amsmath.sty and I think I have a fix, though I don't have a full explanation why the current code should work (it seems rather hacky) for align. (Basically it tries to place the tag using the same mechanism for both align and gather, which is strange as normally in amsmath.sty the align type environments and gather type environments have distinct methods for placing tags.) Anyway, I posted an answer below, feel free to edit / correct. – Willie Wong Jul 20 '23 at 20:05
  • @WillieWong -- I've called on David Carlisle for help, and turned the task for fixing this over to him and the AMS TeXnical support group. Let's hope a real fix becomes public soon. Thanks for your digging and analysis. – barbara beeton Jul 21 '23 at 01:18

1 Answers1

6

The following solution should work with both documents with or without leqno.

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}

\makeatletter \def\qed@tag@alignat{% \global\tag@true \nonumber &\omit\setboxz@h {\strut@ \qedsymbol}% \iftagsleft@% \global\advance\tagshift@-\displaywidth% \fi% \tagsleft@false \place@tag \kern-\tabskip \ifst@rred \else \global@eqnswtrue \fi \global\advance\row@@ne \cr }

\def\alignat@qed{% \ifmeasuring@ \tag{\qedsymbol}% \else \let\math@cr@@@\qed@tag@alignat \fi } @xp\let\csname alignat@qed\endcsname\alignat@qed \makeatother

\begin{document} \begin{proof} \begin{alignat}{2} &x && =1 \ &y && =2 \qedhere \end{alignat} \end{proof} \begin{proof} \begin{align} x & =1 \ y & =2 \qedhere \end{align} \end{proof} \end{document}

Explanation

Most of the code is copied directly from what is in use for the align environment in amsthm.sty, the key difference is that we define a new \qed@tag@alignat macro.

In amsthm.sty a \qed@tag macro is defined, which works well for both gather and align environments; but the fact that it works with align is mostly a hack. A short and slightly inaccurate description of why the hack is needed is:

Ideally we want something like \tag*{\qedsymbol} to work. If the world doesn't have leqno documents, then exactly the form above is okay. But if you have a left-numbered equation, then \tag*{\qedsymbol} would print it on the wrong side. So we have to do something more clever to shove the \qedsymbol all the way right even when leqno is involved.

Normally, the placement of the tag is computed differently between gather and align environments (they have their own \place@tag@gather and \place@tag macros). In \qed@tag a hack is (ab)used to allow \place@tag@gather to also work in the context of align environments; I haven't worked out fully the mechanism yet, but it has something to do with how \llap works in right-aligned columns and the spacing inserted between groups of columns in align. Whatever it is it doesn't work with alignat.

In the code above we try to solve the issue of leqno differently. Fundamentally the reason that leqno causes an issue for using \place@tag is because during typesetting, the position of the tag, determined by \tagshift@, is computed during the "measuring phase" of the typesetting of the equations (amsmath famously uses a two pass approach to set the equations). During this phase the code doesn't know that there will be a \qedsymbol that ought to be placed flush right. The difference (between whether leqno is in force or not) boils down to a length of \displaywidth. So in the code above, we call \place@tag (which is designed to work with alignat), but we correct for the missing \displaywidth if necessary. This allows for the correct placement both in the left- and right-numbered alignats.

(This should, in principle, also work with align; but this will not work for gather. So potentially one may envision patching amsthm.sty so that the \qedhere invocation works using the above code for both align and alignat, but leaving the original code for gather.

Willie Wong
  • 24,733
  • 8
  • 74
  • 106
  • looks good...... – David Carlisle Jul 20 '23 at 20:08
  • This looks promising! Your last remark makes sense to me: I can easily imagine that align and alignat share the same code as much as I can easily understand that gather needs different code. Finally, I must say that I do not agree with amsthdoc when it says that \qedhere is inappropriate for use with displays numbered on the right: IMO it is appropriate and should place the symbol just under the last tag. I obtain this simply adding a last empty line... ;) – brad Jul 20 '23 at 20:43