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}
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}
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}




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 '23 at 02:03\unskipand final\ignorespacesafterend, these were addressed in cfr's answer. The question is whether or not the hooks around the theorem body should behave likePOSTHEAD body PREFOOT(no adjustment),POSTHEADbody PREFOOT(thmtools behavior), orPOSTHEADbodyPREFOOT(remove both spaces) – mbert Nov 25 '23 at 21:27