[Tex/LaTex] QED sign does not always occur with package ntheorem

ntheoremtheorems

I want to construct some theorem-type of environment by using package ntheorem. I have edited the following source codes:

\documentclass[A4paper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[amsmath,amsthm,thmmarks]{ntheorem} 
\theoremstyle{plain}
\theoremheaderfont{\normalfont}
\theoremseparator{.}
\theorembodyfont{\normalfont }
\theoremsymbol{\ensuremath{\Box}}
\newtheorem{Def}{\bf Definition}

\begin{document}
\begin{Def}
    Let $n$ be a positive integer and $\mathbb{K}$ a field. Let $SL_n(\mathbb{K})$ denote the set of special linear group, that is
    $$SL_n(\mathbb{K}):=\{M\in\mathbb{K}^{n\times n}\mid |M|=1\}.$$
\end{Def}

 \begin{Def}
        Let $n$ be a positive integer and $\mathbb{K}$ a field. Let $SL_n(\mathbb{K})$ denote the set of special linear group, that is
        $SL_n(\mathbb{K}):=\{M\in\mathbb{K}^{n\times n}\mid |M|=1\}.$
\end{Def}

\begin{Def}
            Let $n$ be a positive integer and $\mathbb{K}$ a field. Let $SL_n(\mathbb{K})$ denote the set of special linear group, that is
            $$SL_n(\mathbb{K}):=\{M\in\mathbb{K}^{n\times n}\mid |M|=1\}.$$
        \vspace{-10ex}
        \begin{gather*}
        \end{gather*}
        \end{Def}
\end{document}

The result is as follows:
enter image description here

As you can see in Definition 1, the QED box dose not occur when the Def environment ends with the $$ … $$. But when I ends the whole Def environment with just $…$, as in Definition 2, the QED box does occur.

And I tried many ways. Finally, I have found some method to remedy this. That is to modify the vertical space of QED box by the code:

\vspace{-10ex}
\begin{gather*}
\end{gather*}

added before the line of "end\Def". As in Definition 3, this method works. But it is very complex.

Is there any easy way to solve this problem?

Best Answer

You need to use \[...\], or some other LaTeX math display environment, instead of the plain TeX $$...$$. Many LaTeX packages rely on this, see also Why is \[ ... \] preferable to $$ ... $$?. In the case of ntheorem the marker will not get placed on displays with $$...$$. Consider the following document, close to yours:

\documentclass[a4paper]{article}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[amsmath,amsthm,thmmarks]{ntheorem} 

\theoremstyle{definition}
\theoremseparator{.}
\theoremsymbol{\ensuremath{\Box}}
\newtheorem{Def}{Definition}

\begin{document}

\begin{Def}
  Let $n$ be a positive integer and $\mathbb{K}$ a field. Let
  $\mathit{SL}_n(\mathbb{K})$ denote the set of special linear group, that is
  \[
  \text{displaymath}\ \mathit{SL}_n(\mathbb{K}):=\{M\in\mathbb{K}^{n\times
  n}\mid |M|=1\}
  \]
  or
  \[
  \text{displaymath}\ \mathit{SL}_n(\mathbb{K}):=\{M\in\mathbb{K}^{n\times
  n}\mid |M|=1\}
  \]
  or
  $$ \text{dollars}\ \mathit{SL}_n(\mathbb{K}):=\{M\in\mathbb{K}^{n\times
  n}\mid |M|=1\}$$
  or
  $$ \text{dollars}\ \mathit{SL}_n(\mathbb{K}):=\{M\in\mathbb{K}^{n\times
  n}\mid |M|=1\}$$
  or
  \[
  \text{displaymath}\ \mathit{SL}_n(\mathbb{K}):=\{M\in\mathbb{K}^{n\times
  n}\mid |M|=1\}
  \]
  or
  $$ \text{dollars}\ \mathit{SL}_n(\mathbb{K}):=\{M\in\mathbb{K}^{n\times
  n}\mid |M|=1\}$$
  or
  $$ \text{dollars}\ \mathit{SL}_n(\mathbb{K}):=\{M\in\mathbb{K}^{n\times n}\mid |M|=1\}.$$
\end{Def}


\end{document}

Examining the aux file we find that ntheorem outputs the line

\global\def\markiDefiii{\ensuremath {\Box }}

which comes from the third \[...\] pair, corresponding the roman numeral iii. The $$...$$ do not get counted and on the next run ntheorem places the qed mark at the end of the third \[...\] display:

Sample output

Note also you definition of the Def environment is bad. Font formatting should not be put in the label, instead you should use

\theoremheadfont{\normalfont\bfseries}

Perhaps the amsthm option was causing problems with setting these fonts, it is deprecated in the ntheorem documentation, however if you do use then there is a built in definition theoremstyle which I have used above.

Related Question