5

I have the following document:

\documentclass{article}
\usepackage{enumerate}
\usepackage{xparse}

\newcounter{probctr}

\NewDocumentEnvironment{prob}{o}
 {%
  \par
  \addvspace{.15in}%
  \addtocounter{probctr}{1}%
  \noindent\textbf{Problem \theprobctr}%
  \IfNoValueF{#1}{ (#1 points)}%
  \par\nopagebreak\smallskip\noindent\ignorespaces%
 }
 {\addvspace{.15in}}

 \begin{document}
     \begin{prob}[15]
       \begin{enumerate}[(a)]
       \item Item 1
       \item Item 2
     \end{enumerate}

      Consider this ...
   \end{prob}

 \end{document}

If I comment out the line "Consider this ...", the document compiles fine. But with that line I get LaTeX Error: Something's wrong--perhaps a missing \item. Can anyone tell me what is wrong here?

passerby51
  • 2,395
  • 1
    You need \par\addvspace{.15in} in the “end” part. – egreg Jun 05 '15 at 20:05
  • 1
    passerby51: I suggest to use \refstepcounter{prbcounter} instead of \addtocounter. You can use labels then –  Jun 05 '15 at 20:08
  • @egreg, I see. So the there reason this has never happened before is probably the blank line I always had before \end{prob}. The error is very misleading though. – passerby51 Jun 05 '15 at 20:30
  • @passerby51: Yes, it's an implicit \par –  Jun 05 '15 at 20:32
  • @ChristianHupfer, thanks. What is the advantage though besides saving an extra {1}? – passerby51 Jun 05 '15 at 20:32
  • @passerby51: Extra {1}? Sorry, I am confused! –  Jun 05 '15 at 20:37
  • @passerby51 Yes, it is a bit confusing; but since \addvspace constantly appears at the end of lists, the LaTeX team has decided to issue \@noitemerr when \addvspace is in unproper place. – egreg Jun 05 '15 at 20:47
  • @ChristianHupfer, I meant the advantage seems to be that you don't need to specify by how much to increment the counter; refstepcounter increments by 1 by default (it seems). Otherwise, what is the advantage of using it over \addtocounter? – passerby51 Jun 05 '15 at 21:46
  • @egreg, thanks... I guess something like "Error: \addvspace used in horizontal mode" would have been much more suitable. I have no idea how feasible that would have been. – passerby51 Jun 05 '15 at 21:49
  • @passerby51 Possibly so; alas, the team decided otherwise. – egreg Jun 05 '15 at 21:53
  • 1
    @passerby51: Using \refstepcounter allows to use a label for your problem environment, say \begin{prob} \label{foo} .... and later on, you could say In Problem \ref{foo} ... for example –  Jun 06 '15 at 06:39
  • @ChristianHupfer, Oh... I see, that seals the deal then! – passerby51 Jun 06 '15 at 07:11

1 Answers1

5

As egreg noted in the comments:

The \addvspace macro must be issued in vertical mode!

There is no conflict between \NewDocumentCommand and enumerate etc or enumitem. The solution to the error is that \addvspace must follow an explicit \par. The error would occur for \newenvironment too (see the probtrad environment).

I suggest to use \refstepcounter{probctr} instead of \addtocounter. The former allows to use \label and refer to it.

Please note, that my initial version worked (using \vskip 0.15in instead of \par\addvspace{0.15in}) just because \vskip implies \par). It was not really correct then. Joe Cocker sang: With a little help from my friends I took the comment (by permission from egreg) to apply it here.

\documentclass{article}
\usepackage{enumerate}
\usepackage{etoolbox}
%\usepackage[shortlabels]{enumitem} % as an alternative to enumerate
\usepackage{xparse}

\newcounter{probctr}

\NewDocumentEnvironment{prob}{o}
{%
  \par
  \addvspace{.15in}%
  \refstepcounter{probctr}%
  \noindent\textbf{Problem \theprobctr}%
  \IfNoValueF{#1}{ (#1 points)}%
  \par\nopagebreak\smallskip\noindent\ignorespaces%
}{%
  \par\addvspace{.15in}% egreg's suggestion
}



\newenvironment{probtrad}[1][]{%
 \par
 \addvspace{.15in}%
  \refstepcounter{probctr}%
  \noindent\textbf{Problem \theprobctr}%
  \ifblank{#1}{}{ (#1 points)}%
  \par\nopagebreak\smallskip\noindent\ignorespaces%
}{%
  \par\addvspace{.15in}% egreg's suggestion
}

\begin{document}
 \begin{prob}[15]
   \begin{enumerate}[(a)]
   \item Item 1
   \item Item 2
   \end{enumerate}
   Consider this ...
 \end{prob}

 \begin{probtrad}[20]
   \begin{enumerate}[(a)]
   \item Item 3
   \item Item 4
   \end{enumerate}
   Now consider this ...
 \end{probtrad}


\end{document}

enter image description here