muzimuzhi / thmtools

New home for LaTeX package bundle thmtools
LaTeX Project Public License v1.3c
16 stars 3 forks source link

wrong spacing above theorems #29

Open blumens opened 2 years ago

blumens commented 2 years ago

I noticed that sometimes there is too much spacing above a theorem. I found two cases where this occurs:

If the preceding paragraph ends in a line that is exactly as long as the \textwidth. (In this case, adding a % at the end of the paragraph solves the issue.)

If the theorem is preceded by an align* environment.

muzimuzhi commented 2 years ago

Does the following example reproduces the problem you reported? If so I'm afraid that's the default behavior inherited from LaTeX2e format.

\documentclass{article}
\usepackage{lipsum}
\usepackage{amsmath}

% case 1: LaTeX2e's \newtheorem
\newtheorem{theorem}{Theorem}

% case 2: amsthm's \newtheorem
% \usepackage{amsthm}
% \newtheorem{theorem}{Theorem}

% case 3: thmtools' \declaretheorem, with amsthm loaded
% \usepackage{amsthm, thmtools}
% \declaretheorem{theorem}

\parindent=0pt
\def\dummy{%
  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
  vestibulumut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
  \hfill mauris.}

\begin{document}
\dummy
\begin{theorem}
  preceded by a paragraph ends in a full-line
\end{theorem}
\dummy
\begin{align*}
  a + b = c
\end{align*}
\begin{theorem}
  preceded by an \verb|align*|
\end{theorem}
\dummy
\begin{align*}
  a + b = c
\end{align*}
\dummy

\bigskip
Accumulated natural height of the current page: \the\pagetotal
\end{document}

image

Accumulated natural height of the current page:

blumens commented 2 years ago

No, everything looks fine. Unfortunately, the only examples I do have use commercial fonts. Here are two pictures of how it looks like. The difference is just a % at the end of the line.

bad

good

muzimuzhi commented 2 years ago

I suspect the text before lemma wraps to the second line and leaves the second line empty. Can you check if the problem persists when the lemma env is replaced with other trivlist wrappers, for example center env?

Please share a short but complete latex example. If free fonts can't reproduce problem, commercial fonts are ok.

blumens commented 2 years ago

A center environment works fine. No additional space if I remove the %. I'll try to find a small example to reproduce, but it might take some time.

blumens commented 2 years ago

Okay, I found a small example using Computer Modern. It seems to be an interaction between thmtools and hyperref.

\PassOptionsToPackage{a5paper,pagesize}{typearea}                                                
\documentclass[a5paper,10pt,fleqn,DIV9]{scrbook}                                                 
\usepackage[fleqn]{amsmath}                                                                      
\usepackage[hidelinks]{hyperref}                                                                 
\usepackage{microtype}                                                                           
\usepackage{amsthm,thmtools}                                                                     

\declaretheorem[name=Lemma]{Lem}                                                    

\typearea{9}                                                                                     

\begin{document}                                                                                 
Finally, let us consider morphisms between polynomial functors.                                  
\begin{Lem}                                                                                      
foo                                                                                              
\end{Lem}                                                                                        
\end{document}                                                                                   
blumens commented 2 years ago

PS: If I run my example with amsthm only, it works as expected.

muzimuzhi commented 2 years ago

Thank you for the example. Starting from it I got a further simplified one:

\documentclass{article}
% \usepackage{showframe}
\usepackage{thmtools}
\usepackage{hyperref}

\newtheorem{Lem}{Lemma}

% \AddToHook{env/Lem/begin}{\unskip} % workaround

\begin{document}
Finally, let us consider morphisms between polynomial functors functors ab.
\begin{Lem}
foo
\end{Lem}
Finally, let us consider morphisms between polynomial functors functors ab.
\begin{center}
foo
\end{center}
\end{document}

A thorough analysis and fix would need more time. Before that, can you test if the workaround \AddToHook{env/Lem/begin}{\unskip} works for you?

blumens commented 2 years ago

I tried it with the large document I'm working on, and it seems to fix the spacing issue. But now I get a lot of errors:

! You can't use `\unskip' in vertical mode.

muzimuzhi commented 2 years ago

Then \AddToHook{env/Lem/begin}{\ifvmode\else\unskip\fi}.

blumens commented 2 years ago

That seems to fix the issue.

blumens commented 2 years ago

I spoke too soon: The issue is fixed after paragraphs with full lines, but it persists after align* environments.

blumens commented 2 years ago

Here is a test case:

\documentclass{article}
% \usepackage{showframe}
\usepackage{amsmath} 
\usepackage{thmtools}
\usepackage{hyperref}

\newtheorem{Lem}{Lemma}

\AddToHook{env/Lem/begin}{\unskip} % workaround                

\begin{document}
Finally, let us consider morphisms between polynomial functors functors ab.
\begin{Lem}
foo
\end{Lem}
Finally, let us consider morphisms between polynomial functors functors ab.
\begin{center}
foo
\end{center}
Finally, let us consider morphisms between polynomial functors functors ab.
\begin{align*}
  foo
\end{align*}
\begin{Lem}
foo
\end{Lem}

\end{document}
muzimuzhi commented 2 years ago

Hmm the align* case is reproducible with LaTeX2e's vanilla \newtheorem, but not amsthm.

blumens commented 1 year ago

Do you need more input from me?

It is clearly a bug, not only in thmtools but also for the vanilla version, that is fixed in amsthm.

muzimuzhi commented 1 year ago

Hope it will be resolved by https://github.com/latex3/latex2e/pull/990.

mbertucci47 commented 10 months ago

Hmm the align* case is reproducible with LaTeX2e's vanilla \newtheorem, but not amsthm.

I am able to reproduce the extra space with both the vanilla \newtheorem and amsthm. It goes away if you put an empty line (i.e. a \par) before the theorem, but elsewhere it is bad advice to put an empty line after align*.