[Tex/LaTex] Unnecessary space after \end{spacing}

setspacespacing

This is my latex file:

\documentclass[11pt]{article}
\usepackage{setspace}
\begin{document}
   \section*{Section 1}
      \begin{spacing}{1.5}
         Unnecessary\\
         space\\
         after\\
         this1
      \end{spacing}
   \section*{Section 2}
      Perfect\\
      space\\
      after\\
      this2
   \section*{Section 3}
\end{document}

This is the output:

enter image description here

I want as much space after 'this1' as there is after 'this2'. Any help would be appreciated.

Best Answer

You can achieve your goal by modifying the spacing environment provided by the setspace environment. This may be done along the following lines, which uses the calc package and changes what's done at the end of a spacing environment.

enter image description here

\documentclass[11pt]{article}
\usepackage{setspace}
%% insert the following material in preamble
\usepackage{calc}
\newlength\modparskip
\newlength\modbaselineskip
  \def\baselinestretch{1} % this parameter will be redefined at start of 'spacing' environment
  \setlength\modparskip{\parskip/\real{\baselinestretch}}%
  \setlength\modbaselineskip{\baselineskip/\real{\baselinestretch}}%
\makeatletter
\renewenvironment{spacing}[1]{\par%
   \def\baselinestretch{#1}%
   \ifx\@currsize\normalsize\@normalsize\else\@currsize\fi%
}%
{\par%
   \vskip \modparskip%      % originally: \vskip \parskip
   \vskip \modbaselineskip% % originally: \vskip \baselineskip
}
\makeatother
%% end of material to be inserted in preamble

\begin{document}
   \section*{Section 1}
      \begin{spacing}{1.5}
         Now also ``correct'' space after this1
      \end{spacing}
   \section*{Section 2}
      Perfect space after this2
   \section*{Section 3}
\end{document}
Related Question