# Adding vertical space inside lstlisting

I am trying the add empty spaces between two lines in an lstlisting environment.

\begin{lstlisting}[language=Python, frame=single]
def foo():
# I need extra vertical space here
return x
\end{lstlisting}


belowskip adds space after the listing, vskip or vspace adds space but it breakes the frame as shown below.

and lineskip gives an undesired margin just above the first line, (i.e. def foo() ), shown below.

\begin{lstlisting}[language=Python, frame=single, lineskip = 3em]
def foo():

return x
\end{lstlisting}


You can use tcolorbox and escapechar:

\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{xparse,listings}

\NewTCBListing{framedlisting}{O{}}
{%
boxrule=0.4pt,
colback=white,
colframe=black,
sharp corners,
listing only,
listing options={#1},
}

\begin{document}

\begin{framedlisting}[language=Python,escapechar=$] def foo():$\vspace{3\baselineskip}\$
return x
\end{framedlisting}

\end{document}


• I edited... But still prefer your simples solution... (+1) – koleygr Dec 24 '17 at 11:14

Just adding empty lines between the lines you want the extra space will solve your problem. (In verbatim-litsing environments empty lines DOESN'T MEAN PARAGRAPH START BUT-> EMPTY LINE):

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
%opening
\title{}
\author{}

\begin{document}

\begin{lstlisting}[language=Python, frame=single]
def foo():
# I need extra vertical space here

return x
\end{lstlisting}

\end{document}


Output:

Edit (A solution with tikz):

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{calc}
%opening
\title{}

\author{}
\def\tikzmark#1{\tikz[overlay, remember picture]\coordinate(#1);}

\lstnewenvironment{frlstlisting}[1][]{\noindent\tikzmark{A}\lstset{#1}}
{\hspace{\fill}\tikzmark{B}%
\tikz[overlay,remember picture] \draw[very thick] ([xshift=-3.7pt,yshift=-7.6pt]A)rectangle([xshift=4.2pt, yshift=14.pt]B);}

\begin{document}

\begin{frlstlisting}[language={Python},escapeinside={/@}{@/}]
def foo():
/@\vspace{20pt}@/
return x
\end{frlstlisting}

\begin{frlstlisting}[language={Python},escapeinside={/@}{@/}]
def foo():
/@\vspace{70pt}@/
return x
\end{frlstlisting}

\end{document}


Output:

The frame is there that would be with framed option (after manually setting the spaces)

• that was my current solution, upped your answer but expecting a programmatic answer. – Emmet B Dec 24 '17 at 10:10
• Do you want a vertical space after any line? or just manually added (by using \vspace* for exmaple)? – koleygr Dec 24 '17 at 10:14
• manual is better – Emmet B Dec 24 '17 at 10:16