Spaces in newenvironment

I observed an unwanted behavior of my self-made environment. The following code sometimes creates spaces after the environment head and sometimes doesn't.

\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{ifthen}

\newenvironment{Theorem}[1][]{%
\medskip%
\par%
\noindent%
\textbf{Theorem.}%
\ifthenelse{ \equal{#1}{} }{}{ (#1)}%
\it%
}%
{%
\smallskip\par%
}

\newenvironment{Theorem2}{%
\medskip%
\par%
\noindent%
\textbf{Theorem.}%
\it%
}%
{%
\smallskip\par%
}

\begin{document}

\begin{Theorem}
That is a cool Theorem
\end{Theorem}

\begin{Theorem}[of some famous guy]
That is a cool Theorem
\end{Theorem}

\begin{Theorem2}
another cool theorem
\end{Theorem2}

\begin{Theorem2}%
another cool theorem
\end{Theorem2}

\end{document}


The result is:

Is there a way of forcing LaTeX to ignore all normal spaces in the definition of an environment such that I can only add spaces by \space?

And can I add something to the definition of the environment such that it ignores all spaces and pars that appear directly after the call of the environment such that

\begin{Theorem2}
some text
\end{Theorem2}


produces the same as

\begin{Theorem2}%
some text
\end{Theorem2}


?

• What's wrong with \usepackage{amsthm} and \newtheorem*{Theorem}{Theorem}? – egreg Sep 6 '18 at 12:16
• the space is not space in your definition it is space added (or not) in the document – David Carlisle Sep 6 '18 at 12:32

Yes! Use \ignorespaces at the end of the first argument to \newenvironment

\documentclass{article}
\newenvironment{Theorem2}{%
\medskip%
\par%
\noindent%
\textbf{Theorem.}%
\itshape\ignorespaces%
}%
{%
\smallskip\par%
}
\begin{document}
\begin{Theorem2}
some text
\end{Theorem2}
\end{document}


\ignorespaces is a primitive that ignores the next spaces until some text is to be output. When you do e.g. \newenvironment{thm}{}{} then a macro \thm is made, which in turn is the last macro executed by the \begin{thm} command.

• okay thanks I will try that. But is there also a way to ignore all normal space signs in the definition of an environment? – Nathanael Skrepek Sep 6 '18 at 12:11
• @NathanaelSkrepek not that I know of. – Andreas Storvik Strauman Sep 6 '18 at 12:12
• hmm anyway your command seems enough to make the environment do what it was supposed to do. – Nathanael Skrepek Sep 6 '18 at 12:15
• @NathanaelSkrepek No, Inside a definition, you just have to write a space where you want one, and not write a space where you don't want one! (That's why lines in definitions often end with %, of course) – alephzero Sep 6 '18 at 12:15
• @AndreasStorvikStrauman The OP says the unwanted behaviour is that it sometimes creates spaces and sometimes doesn't. I assumed that in normal English text, you always want a space in the output after 'Theorem.' – alephzero Sep 6 '18 at 12:23

Yes, there is: use xparse and exploit that in the scope of \ExplSyntaxOn spaces are ignored; one can use ~ to insert a real space.

\documentclass[a4paper]{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentEnvironment{Theorem}{o}
{
\noindent
\normalfont
\textbf{Theorem.~}
\IfValueT{#1}{(#1)~}
\itshape\ignorespaces
}
{
}
\ExplSyntaxOff

\begin{document}

Some text before the theorem to see the vertical spacing
in context.

\begin{Theorem}
That is a cool Theorem
\end{Theorem}

Some text after and before the theorem to see the vertical spacing
in context.

\begin{Theorem}[of some famous guy]
That is a cool Theorem
\end{Theorem}

Some text after the theorem to see the vertical spacing
in context.

\end{document}


I made some adjustments: \medskip and \smallskip are wrong in this context. However, \smallskipamount is definitely too small as the image shows. With \addvspace the vertical spaces don't accumulate; with your code two consecutive theorems would be separated by a small skip plus a medium skip.

On the other hand, \newtheorem* from amsthm is much simpler.

\documentclass[a4paper]{article}

\usepackage{amsthm}

\newtheorem*{Theorem}{Theorem}

\begin{document}

Some text before the theorem to see the vertical spacing
in context.

\begin{Theorem}
That is a cool Theorem
\end{Theorem}

Some text after and before the theorem to see the vertical spacing
in context.

\begin{Theorem}[of some famous guy]
That is a cool Theorem
\end{Theorem}

Some text after the theorem to see the vertical spacing
in context.

\end{document}


• So if you do at the beginning and at the end \medskip and one theorem follows an other then between them is still one \medskip instead of two? – Nathanael Skrepek Sep 6 '18 at 12:37
• @NathanaelSkrepek With \addvspace only one, with \medskip two. – egreg Sep 6 '18 at 12:38