# newenvironment, label, indentation

I have a custom environment that I reference. It seems that the indentation changes if I do not include a space before \label (see MWE below). So, I have a few questions:

1. Should it matter where \label is placed on a custom environment; could the environment be changed so that I don't have to put a space before \label?
2. Is there a convention on where \label should be placed (on non-floating environments), or is this a matter of style?
3. Is there a preferred way of getting a line break rather than using \newline?

Please note that I do not want to use ntheorem as the actual version I use takes 2 optional arguments (using newenvironmentx from xargs).

EDIT: I am particularly interested in trying to answer my first question- could the definition of the environment be changed so that I don't have to put a space before \label, or a % after it? The ntheorem package seems to accomplish it using trivlist.

\documentclass{article}
\usepackage{lipsum}

\setlength{\parindent}{0mm}

\newcounter{problem}
\newenvironment{problem}%
{%
\refstepcounter{problem}%
\textbf{Problem \theproblem }\newline%
}{}

\begin{document}

\begin{problem}\label{firstlabel}
\lipsum[1]
\end{problem}

\begin{problem} \label{secondlabel}
\lipsum[1]
\end{problem}
\end{document}

-

1. Yes. See 3.
2. The convention is to put it after a \caption[..]{...}, or more accurately, after \refstepcounter{...} for correct labelling. Otherwise the label would refer to a previous instance of the most recent \refstepcounter{...} that was executed.
3. Use \par or \endgraf instead of \newline.

-
Thanks; if I use \par, I get the same skip as \parskip, which I don't want, unless I've misunderstood. –  cmhughes Aug 6 '11 at 22:47
@smr01cmh: You're right. Using \par or \endgraf inserts a \parskip. To avoid this is not so much a problem in the macro but in your problem environment. You need a % right after the \label{...}, otherwise LaTeX uses the line break in your code as an extra space. –  Werner Aug 6 '11 at 23:02
Thanks again; so it seems that I can either put a space before the \label, or % after it. I was hoping for a solution within the definition of the environment. When using a custom environment from ntheorem it doesn't have to be as delicate- i.e, no need for space before or % after –  cmhughes Aug 6 '11 at 23:06

You need to add comment characters % immediatly after the closing braces of the \label{...} arguments. This will prevent the spaces you saw.

That's where I put my labels in an environment, theorem, etc. But like Werner said you wont get the right cross references.

Have you looked at the amsthm package for making theorem like environments.

Try

\documentclass{article}
\usepackage{lipsum}
\usepackage{amsthm}

\newtheoremstyle{mystyle}                % Name
{}                                     % Space above
{}                                     % Space below
{}                                     % Body font
{}                                     % Indent amount
{.}                                    % Punctuation after theorem head
{\newline}                             % Space after theorem head, ' ', or \newline
{}                                     % Theorem head spec (can be left empty, meaning normal')

\theoremstyle{mystyle}
\newtheorem{problem}{Problem}

\begin{document}

\begin{problem}\label{firstlabel}%
\lipsum[1]
\end{problem}

\begin{problem} \label{secondlabel}%
\lipsum[1]
\end{problem}
\end{document}


Oh and that will give you the right cross referencing.

-
Thanks; I don't want to use a theorem environment for this, as the actual version uses newenvironmentx and has 2 optional arguments. –  cmhughes Aug 6 '11 at 22:35

I just saw this post because I have the same question when I am referencing a custom environment. Based on all the above information, I found a solution to solve the problem. You only need to slightly modify the definition of the environment as follows:

\newcounter{problem}
\newenvironment{problem}%
{%
\refstepcounter{problem}%
\textbf{Problem \theproblem } \par\vspace{-\parskip}%
}{}


My explanation: when using \par instead of \newline or \\, you don't have to put a space before \label, or a % after it. But in the meantime, \par leads to \parskip. Thus, you have to put \vspace{-\parskip} right after \par`.

I also expect any other solutions to this problem.

-