0

Please see addendum before reading the question. I have not deleted or made major edits to it so as not to invalidate @cfr's answer.

I'm trying to add some hooks to a theorem environment, and I noticed they add unwanted spaces next to the surrounding and body text. Here's a simple first try that defines the environment between \ExplSyntaxOn and \ExplSyntaxOff to avoid any accidental spaces:

\documentclass{article}
\usepackage{amsthm}

\NewHook{mythm/prehead}
\NewHook{mythm/posthead}
\NewHook{mythm/prefoot}
\NewHook{mythm/postfoot}

\newtheorem{theorem}{Theorem}

\ExplSyntaxOn
\NewDocumentEnvironment{mytheorem}{ O{} }
  {
    \UseHook{mythm/prehead}
    \begin{theorem}[#1]
    \UseHook{mythm/posthead}
  }
  {
    \UseHook{mythm/prefoot}
    \end{theorem}
    \UseHook{mythm/postfoot}
  }
\ExplSyntaxOff

\AddToHook{mythm/prehead}{PREHEAD}
\AddToHook{mythm/posthead}{POSTHEAD}
\AddToHook{mythm/prefoot}{PREFOOT}
\AddToHook{mythm/postfoot}{POSTFOOT}

\begin{document}

before
\begin{mytheorem}
body
\end{mytheorem}
after

\end{document}

test1

Around the code added to each hook there are unwanted spaces. I'll note that this is the same output as if I'd defined

\NewDocumentEnvironment{mytheorem}{ O{} }
  {
    PREHEAD
    \begin{theorem}[#1]
    POSTHEAD
  }
  {
    PREFOOT
    \end{theorem}
    POSTFOOT
  }

so the \UseHooks aren't the real issue.

After playing around with \unskip, \ignorespaces, and \ignorespacesafterend, I found a combination that removes the spaces:

\documentclass{article}
\usepackage{amsthm}

\NewHook{mythm/prehead}
\NewHook{mythm/posthead}
\NewHook{mythm/prefoot}
\NewHook{mythm/postfoot}

\newtheorem{theorem}{Theorem}

\ExplSyntaxOn
\NewDocumentEnvironment{mytheorem}{ O{} }
  {
    \unskip % \ignorespaces does not work here
    \UseHook{mythm/prehead}
    \begin{theorem}[#1]
    \UseHook{mythm/posthead}
    \ignorespaces % \unskip does not work here
  }
  {
    \unskip % \ignorespaces does not work here
    \UseHook{mythm/prefoot}
    \end{theorem}
    \UseHook{mythm/postfoot}
    \ignorespacesafterend % neither \unskip nor \ignorespaces work here
  }
\ExplSyntaxOff

\AddToHook{mythm/prehead}{PREHEAD}
\AddToHook{mythm/posthead}{POSTHEAD}
\AddToHook{mythm/prefoot}{PREFOOT}
\AddToHook{mythm/postfoot}{POSTFOOT}

\begin{document}

before
\begin{mytheorem}
body
\end{mytheorem}
after

\end{document}

demo

Is this the correct way to go about removing spaces from around an environment? After reading this thread, I understand better what the commands do, but I'd like to know if there are any obvious downsides or incompatibilities (e.g. theorems won't ever be in vertical mode, right?).

Addendum

As @cfr points out in the answer below, there should be spaces before the prehead and postfoot hooks because there are explicit spaces (from the linebreaks) in the input. So, my question should only apply to the appropriateness of \ignorespaces to remove the space after the posthead hook and \unskip before the prefoot hook. Apologies for my silly mistake.

Comparison with thmtools

The idea for these hooks is coming from the thmtools package. It only removes space after the posthead hook, not before the prefoot. Here is an example.

\documentclass{article}
\usepackage{amsthm,thmtools}

\declaretheorem{theorem}

\addtotheorempreheadhook{PREHEAD}
\addtotheorempostheadhook{POSTHEAD}
\addtotheoremprefoothook{PREFOOT}
\addtotheorempostfoothook{POSTFOOT}

\begin{document}

before
\begin{theorem}
body
\end{theorem}
after

\end{document}

thmtools-comp  

6
  • For the prehead and postfoot cases, there are explicit spaces in the input. If you remove the explicit spaces from the input, the spaces in the output disappear. That is not something I'd expect an environment to do.
    – cfr
    Nov 25, 2023 at 1:50
  • Try ABC\UseHook{mythm/prehead}\UseHook{mythm/posthead}\UseHook{mythm/prefoot}\UseHook{mythm/postfoot}XYZ. I don't see what you think the space has to do with the hooks, given you say you realise they're not the issue?
    – cfr
    Nov 25, 2023 at 2:03
  • @DavidCarlisle Thanks. So, I guess the advice is don't try to remove any of the horizontal spaces around the body text
    – mbert
    Nov 25, 2023 at 18:47
  • @DavidCarlisle Sorry for the confusion. I thought the addendum clarified my intention but I guess not. Please ignore the first \unskip and final \ignorespacesafterend, these were addressed in cfr's answer. The question is whether or not the hooks around the theorem body should behave like POSTHEAD body PREFOOT (no adjustment), POSTHEADbody PREFOOT (thmtools behavior), or POSTHEADbodyPREFOOT (remove both spaces)
    – mbert
    Nov 25, 2023 at 21:27
  • @DavidCarlisle Yes at this point I'm aware \unskip is wrong. The standard environment hooks don't work here because I'm separating out before/after the theorem heading and before/after the theorem foot. This is exactly what thmtools does so I'm not reinventing the wheel. I could hook into the theorem code but the whole point of the posthead and prefoot hooks is they should be the very first thing and very last thing in the theorem body, so I don't see how I could get any more fine-grained placement than where they are in my example
    – mbert
    Nov 25, 2023 at 21:44

1 Answer 1

1

Note that this answer contains NO working code.

Caveat emptor ...


First, as you seem to realise, the spacing is not introduced by your use of hooks. To see this, we can use simply

ABC\UseHook{mythm/prehead}\UseHook{mythm/posthead}\UseHook{mythm/prefoot}\UseHook{mythm/postfoot}XYZ

hooks don't add space

It is evident from this that none of the hooks are adding any space.

Second, your proposed 'solution' will create many untoward effects. At best, you will get appalling typographical output. (Try \hrule before mytheorem or a sectioning command or a list or ...).

Third, All Kinds of Stuff will break if it stumbles into the vicinity of mytheorem. So while killing all vertical spacing above and below mytheorem will create very ugly documents, this is unlikely to be much of a problem because you're unlikely to get any output at all.

For instance,

\documentclass[a5paper]{article}% does NOT compile without error
\usepackage{amsthm}
\usepackage{kantlipsum}

\NewHook{mythm/prehead}
\NewHook{mythm/posthead}
\NewHook{mythm/prefoot}
\NewHook{mythm/postfoot}

\newtheorem{theorem}{Theorem}

\ExplSyntaxOn
\NewDocumentEnvironment{mytheorem}{ O{} }
  {
    \unskip % \ignorespaces does not work here
    \UseHook{mythm/prehead}
    \begin{theorem}[#1]
    \UseHook{mythm/posthead}
    \ignorespaces % \unskip does not work here
  }
  {
    \unskip % \ignorespaces does not work here
    \UseHook{mythm/prefoot}
    \end{theorem}
    \UseHook{mythm/postfoot}
    \ignorespacesafterend % neither \unskip nor \ignorespaces work here
  }
\ExplSyntaxOff

\AddToHook{mythm/prehead}{PREHEAD}
\AddToHook{mythm/posthead}{POSTHEAD}
\AddToHook{mythm/prefoot}{PREFOOT}
\AddToHook{mythm/postfoot}{POSTFOOT}

\begin{document}

\begin{mytheorem}

\begin{mytheorem}
\kant[1]
\end{mytheorem}

\begin{verbatim}
\macro 
\end{verbatim}

\begin{mytheorem}
\kant[2]
\end{mytheorem}

\end{document}

Perhaps you don't use verbatim?

\begin{mytheorem}
\kant[1]
\end{mytheorem}

\begin{center}
  I'm in the centre.
\end{center}

\begin{mytheorem}
\kant[2]
\end{mytheorem}

Or center? (Or flushleft or ...?)

\begin{mytheorem}
\kant[1]
\end{mytheorem}

\[
  x^2
\]

\begin{mytheorem}
\kant[2]
\end{mytheorem}

Or display-style maths?

Or ... ?

6
  • Okay I see I made a dumb mistake with the prehead and postfoot hooks. I will add an addendum to the question. But the question still applies for the posthead and prefoot hooks, which this does not answer
    – mbert
    Nov 25, 2023 at 4:48
  • @mbert The hooks are not adding the space.
    – cfr
    Nov 25, 2023 at 6:45
  • @mbert You asked if your solution to the problem of the hooks adding space had any obvious downsides. I answered (1) that none of the hooks are adding space (see edit for output of code I posted in a comment earlier demonstrating this) and (2) that eliminating space using the proposed code had several downsides. I don't know what more can be said about the posthead and prefoot hooks adding space. They clearly don't.
    – cfr
    Nov 25, 2023 at 6:54
  • Yes I do appreciate your answer regarding the downsides. In my previous comment I was not asserting (or at least did not mean to assert) that the posthead/prefoot hooks are causing the space. I merely meant to ask whether or not removing the resulting spaces around the body text is a good idea. For example, this is the default behavior of thmtools posthead but not prefoot. Please see the comparison I've added to the end of my question
    – mbert
    Nov 25, 2023 at 7:18
  • I think that's really dependant on how you want to use the hooks and the environment. Obviously, if you're going to add typeset output into those hooks, removing space before the body might be a bad idea because you don't typically want words run together, but I assume you have some other use in mind here which makes it appropriate to remove the spaces. You can remove the spaces manually when you use the environment by just commenting the line endings. Using \unskip before prefoot means the body mustn't end with something like a paragraph break.
    – cfr
    Nov 25, 2023 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .