I use the listings latex package to write/read the environment body to a file. But if in the environment are two spaces between some words, there is an additional linebreak added at the beginning (I made it red to see it better in this example). How can I prevent this?
\documentclass{article}
\usepackage{listings}
\usepackage{color}
\makeatletter
\lst@RequireAspects{writefile}
\lstnewenvironment{testenv}{%
\lstset{aboveskip=0pt,belowskip=0pt,backgroundcolor=\color{red}}%
\lst@BeginWriteFile{\jobname.tst}%
}{%
\lst@EndWriteFile% closes output file
% do something
\input{\jobname.tst}
% do something
}%
\makeatother
\begin{document}
Something wrong begins.
\begin{testenv}
This is a text with two spaces between some words.
\end{testenv}
And it ends.
\begin{testenv}
This is a text with only one space between all words.
\end{testenv}
\end{document}
The output:
\jobname.tst
file does not contain any additional lines in both cases. The extra line seems to be caused by the\lst@BeginWriteFile
..\lst@EndWriteFile
directly, which can be seen if you add something before the\input
line.