5

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:

Code result

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}

?

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

2 Answers 2

6

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.

14
  • 1
    okay thanks I will try that. But is there also a way to ignore all normal space signs in the definition of an environment? Commented Sep 6, 2018 at 12:11
  • @NathanaelSkrepek not that I know of. Commented Sep 6, 2018 at 12:12
  • hmm anyway your command seems enough to make the environment do what it was supposed to do. Commented Sep 6, 2018 at 12:15
  • 2
    @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
    Commented Sep 6, 2018 at 12:15
  • 1
    @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
    Commented Sep 6, 2018 at 12:23
4

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}
 {
  \par\addvspace{\medskipamount}
  \noindent
  \normalfont
  \textbf{Theorem.~}
  \IfValueT{#1}{(#1)~}
  \itshape\ignorespaces
 }
 {
  \par\addvspace{\smallskipamount}
 }
\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.

enter image description here

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}

enter image description here

5
  • 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? Commented Sep 6, 2018 at 12:37
  • @NathanaelSkrepek With \addvspace only one, with \medskip two.
    – egreg
    Commented Sep 6, 2018 at 12:38
  • In normal TeX, ~ is a little different from a normal space, in that it prevents a line break (possibly among other effects—I can't remember). Is it so also in \ExplSyntaxOn?
    – LSpice
    Commented Jun 16, 2021 at 15:04
  • 2
    @LSpice In the scope of \ExplSyntaxOn the character ~ denotes a space. No strings attached. If, in that framework, you want a nonbreaking space, use \nobreakspace.
    – egreg
    Commented Jun 16, 2021 at 15:20
  • @egreg, thanks! "No strings attached" is a very apt way of saying that it's no longer a tie. 😄
    – LSpice
    Commented Jun 16, 2021 at 15:21

You must log in to answer this question.

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