# Vertical spacing of text changes in a macro depending on input

I defined a keynote macro to insert key ideas in my document using varwidth. The document is in \doublespacing and I have defined \singlespacing inside my macro. However, the correct spacing is achieved only for long sentences and not for short text like here.

\documentclass{standalone}
\usepackage{setspace,varwidth,lipsum}

\newcommand{\keynote}[1]{%
\begin{varwidth}{4cm}%
{\singlespacing \narrowragged #1}%
\end{varwidth}%
}

\begin{document}
\doublespacing
\hspace{1cm} \keynote{\lipsum[2]}
\hspace{1cm} \keynote{This note explaining a key idea should go to at least two lines.} \hspace{1cm}
\end{document}

-

## 2 Answers

You have to terminate the paragraph, or it will be done when the change in line spacing has already been forgotten because of the }; but since \end{varwidth} ends the paragraph anyway and you're in an environment that forms a group, you can just say

\newcommand{\keynote}[1]{%
\begin{varwidth}{4cm}
\singlespacing \narrowragged #1
\end{varwidth}%
}


Why the example had that peculiarity? Because \lipsum[2] has a \par command inside it. If you used \lipsum*[2], that doesn't issue \par you'd have seen the same problem.

Now you know why: in

\begin{varwidth}{4cm}
{\singlespacing\narrowragged some text to be single spaced}
\end{varwidth}


the paragraph "some text to be single spaced" will be built when the } has already undone the \singlespacing\narrowragged setting. Without the inner braces it's OK.

-
wonderful. I played around with \par and different environments, and it is beautiful...just shows me how much more there is to understand. –  devendra Jan 9 '13 at 10:35

Use the spacing environment of the 'setspace' package.

\documentclass{standalone}
\usepackage{setspace,varwidth,lipsum}

\newcommand{\keynote}[1]{%
\begin{varwidth}{4cm}%
\begin{spacing}{1}
{\narrowragged #1}%
\end{spacing}
\end{varwidth}%
}

\begin{document}
\doublespacing
\hspace{1cm} \keynote{\lipsum[2]} \hspace{1cm} \keynote{This note
explaining a key idea should go to at least two lines.}
\hspace{1cm}
\end{document}

-
Thank you for the answer. Any idea why \singlespacing won't work? –  devendra Jan 9 '13 at 9:24
I don't know really. Please if you like the answer mark it as accepted by clicking the ok symbol. –  zunbeltz Jan 9 '13 at 9:50
thanks. I think its only fair to wait for any other answers. Otherwise, I will accept yours by tomorrow. –  devendra Jan 9 '13 at 9:54