# How to tell alltt not to skip lines at beginning/end?

It seems like there is no option for specifying that, like a hack for verbatim I found on tex.stackexchange:

\usepackage{etoolbox}%
\makeatletter
\preto{\@verbatim}{\topsep=0pt \partopsep=0pt}
\makeatother


\begin{alltt} skips a line before en after its content, how can I remove this ? I tried to use the code below with @alltt instead of @verbatim but it doesn't work.

Indeed I am using knitr which places code input in alltt environment, there may be some other knitr specific solutions too I might be interested in.

MWE:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{alltt}
\usepackage{etoolbox}%

\begin{document}

Few words
\begin{alltt}
alltt words
\end{alltt}
something else

\begin{verbatim}
verbatim words
\end{verbatim}
and with the hack:

\makeatletter
\preto{\@verbatim}{\topsep=0pt \partopsep=0pt}
\makeatother

\begin{verbatim}
verbatim words
\end{verbatim}
the end

\end{document}


• Does knitr not use \begin{document} \end{document}? If it does, please provide the rest of your MWE Perhaps upload an image of output? – A Feldman May 23 '16 at 12:59
• knitr automatically produces a .tex file form the .Rnw; it's not directly related to my question but I thought there might be knitr options to change that. – ClementWalter May 23 '16 at 13:30

There is no \@alltt but like verbatim (and most latex display constructs) alltt is a trivlist so the same settings apply, you just need
\preto{\alltt}{\topsep=0pt \partopsep=0pt}