12

I want to replace \vspace with \addvspace in a command. When so doing, I get obtuse "something's wrong--perhaps a missing \item." errors. I'd appreciate an explanation of why this happens and how to fix my \fieldinfo command (shown below) to use \addvspace.

I tried using \protect\addvspace, but that didn't work either.

Thanks for any help!

\documentclass{book}

\usepackage{tabularx}

% addvspace instead of vspace fails!
\newcommand{\fieldinfo}[2]{\addvspace{\baselineskip}\noindent\begin{tabularx}{\linewidth}{p{0.25in}X}%
        #1  &  #2 \\%
\end{tabularx} \\%
\addvspace{\baselineskip} \\ }%

\begin{document}
A, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi
tristique senectus et netus et malesuada fames ac turpis egestas. Mauris.

\fieldinfo{A}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
tristique senectus et netus et malesuada fames ac turpis egestas. Mauris.}

\fieldinfo{B}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
tristique senectus et netus et malesuada fames ac turpis egestas. Mauris.}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum.
\end{document}

2 Answers 2

11

Yor use of \addvspace fails because you are using it without being in vertical mode. Here's the definition of \addvspace:

\def\addvspace#1{%
  \ifvmode
    \if@minipage\else
      \ifdim \lastskip =\z@
        \vskip #1\relax
      \else
      \@tempskipb#1\relax
        \@xaddvskip
      \fi
    \fi
  \else
    \@noitemerr
  \fi}

as you can see, when not in vertical mode, \@noitemrr is called, which produces the error message "Something’s wrong--perhaps a missing \item".

To be able to use \addvspace you need to enter vmode, and this can be done, for example. by using \par:

\documentclass{book}
\usepackage{tabularx}

\newcommand{\fieldinfo}[2]{
  \addvspace{\baselineskip}
  \noindent\begin{tabularx}{\linewidth}{p{0.25in}X}%
        #1  &  #2 \\%
\end{tabularx}%
\par\addvspace{\baselineskip}}%

\begin{document}
A, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi
tristique senectus et netus et malesuada fames ac turpis egestas. Mauris.

\fieldinfo{A}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
tristique senectus et netus et malesuada fames ac turpis egestas. Mauris.}

\fieldinfo{B}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
tristique senectus et netus et malesuada fames ac turpis egestas. Mauris.}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum.
\end{document}

Of course, you cannot use \\ just after \addvpace{\baselineskip}

2
  • Thanks Gonzalo. BTW, do you know why \addvspace throws the @noitemerr? It's so confusing. Isn't there a way to offer some useful message to users when writing macros like this?
    – srking
    Apr 1, 2011 at 15:59
  • @srking: that's how \addvspace is defined in the LaTeX kernel; you can see the original definition (which I included in my answer) in the Section 16.5 Vertical spacing of the source2e document: tug.org/texlive/Contents/live/texmf-dist/doc/latex/base/… And yes, the error message could have been different and less confusing. Apr 1, 2011 at 16:12
4

The confusing error comes because you didn't use \addvspace in vertical mode. Try

\newcommand{\fieldinfo}[2]{\addvspace{\baselineskip}\noindent\begin{tabularx}{\linewidth}{p{0.25in}X}%
        #1  &  #2 \\%
\end{tabularx}\par
\addvspace{\baselineskip}}

That ends the paragraph and then adds the vspace afterward.

You must log in to answer this question.

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