# Issue with preceding whitespace in custom environment

I am having an issue with a custom environment where the first line of the environment gets a space preceding it. The example below illustrates the behevior. It seems to happen because of the newline character after \begin{oppg}. It also seems to have something to do with the \noindent in line 7, since removing it fixes the issue (but I don't want the line to be indented).

As you can see, the issue is easily fixed by adding a % at the end of the line when i begin the environment, but it seems to me that there should be a more robust way of doing it. There is probably an obvious explanation for this, but I can't see it.

\documentclass{article}

\newcounter{oppg}
\newenvironment{oppg}{%
\refstepcounter{oppg}%
\par\noindent\textbf{Oppgave \theoppg}%
\par\smallskip\noindent%
}%
{\bigbreak}%

\begin{document}

\begin{oppg}
Lorem ipsum, et cetera...
\end{oppg}

\begin{oppg}%
Lorem ipsum, et cetera...
\end{oppg}

\end{document}

• – Werner Sep 26 '16 at 18:03
• don't use \noindent in latex (more or less ever) no standard latex command uses it except one use in \@hangfrom – David Carlisle Sep 26 '16 at 18:11
• @DavidCarlisle Why is \noindent bad, and what is a good alternative? – lydhvin Sep 26 '16 at 20:46
• @lydhvin See my answer in the question that Werner references in the first comment. \noindent always produces the issue that you ask about here. If you use \ignorespaces as in the accepted answer here it masks some of the problems, but only some of them (for example you still can't leave a blank line) and it's better not to use it (and use a latex section or list item command) look at the definition of for example theorem environments which use exactly the kind of headed paragraph that you need here, they do not use \noindent – David Carlisle Sep 26 '16 at 20:52

Environments are essentially two macros, translating \begin{<env>} into \<env> and \end{<env>} into \end<env> together with the appropriate scoping (via a group). However, you're still calling a macro \begin with an argument, and these leave a space in the input stream if the definition of \<env> doesn't take care of it. For those instances, use \ignorespaces:

\documentclass{article}

\newcounter{oppg}
\newenvironment{oppg}
{\par\refstepcounter{oppg}%
\noindent\textbf{Oppgave \theoppg}%
\par\nobreak\smallskip\noindent\ignorespaces}

\begin{document}

\begin{oppg}
Lorem ipsum, et cetera...
\end{oppg}

\begin{oppg}%
Lorem ipsum, et cetera...
\end{oppg}

\end{document}


I've done the same for \end{<env>}.

• Excellent, thank you. Could you explain the reasoning for using \addvspace{\bigskipamount}instead of \bigskip? – lydhvin Sep 26 '16 at 20:47
• @lydhvin: \addvspace inserts a vertical space up to \bigskipamount, while \bigskip inserts a fixed vertical space of exactly \bigskipamount. If you have multiple oppg environments following one another and they add space before and after the environment, compounding these vertical spaces may make for a very stretched-out output. Using \addvspace will only add space if needed. – Werner Sep 26 '16 at 20:51