3

I am creating a beamer presentation with some code inside and I would like to add some vertical spacing inside a listings environment.

I tried using the phantom family commands, but as it is in a listings environment the phantom commands are not interpreted.

My Question is: how can I add some vertical spacing (eg. 3cm) inside my listings environment?

MWE

\documentclass[table,dvipsnames]{beamer}

%%% Basic packages
\usepackage{amsmath}
\usepackage{pgfpages}
\usepackage{pgfplots}
\usepackage{graphicx}
\usepackage{picture}
\usepackage{filecontents}
\usepackage{multicol}
\usepackage{ragged2e}

\usepackage{listingsutf8}
\lstset{
  language=python,
  inputencoding=utf8/latin1,
  extendedchars=true,
  keywordstyle=\bfseries\ttfamily\color{blue},
  identifierstyle=\ttfamily,
  commentstyle=\color{gray},
  stringstyle=\ttfamily\color{green!50!black},
  showstringspaces=false,
  basicstyle=\footnotesize\ttfamily,
  tabsize=2,
  breaklines=true,
  extendedchars=true,
  xleftmargin=1cm, 
  xrightmargin=1cm,
  backgroundcolor=\color{white!80!black},
  literate=%
    {é}{{\'{e}}}1
    {è}{{\`{e}}}1
    {ê}{{\^{e}}}1
    {ë}{{\¨{e}}}1
    {û}{{\^{u}}}1
    {ù}{{\`{u}}}1
    {â}{{\^{a}}}1
    {à}{{\`{a}}}1
    {î}{{\^{i}}}1
    {ô}{{\^{o}}}1
    {ç}{{\c{c}}}1
}

\title[Test listings]{Test listings}

\subtitle{ }
\author{ }
\institute{ } 
\date{ }

\begin{document}

\begin{frame}[fragile]
\frametitle{test}
  \begin{lstlisting}
  if (1<2):
    print('python')

  \vphantom{vertical spacing}

  if (2<3):
    print('yay')
  \end{lstlisting}
\end{frame}

\end{document}
belitd
  • 345
  • 2
    I you want to add a spacing between two blocks inside a listings environment, you can simply add lines in the listed code. If you want to add spacing between two listings environments, you can then use \vspace{<someheight>} – BambOo Aug 21 '18 at 09:25
  • ugh... forgot to try that. Btw the simplest solutions are the greatest - thank you! – belitd Aug 21 '18 at 09:31
  • however, is there a way to specify the desired vertical spacing (eg. 3cm) inside the listing environment? – belitd Aug 21 '18 at 09:33

1 Answers1

3

You could escape a latex sequence to add some space, unfortunately this will also interrupt the background colouring.

\documentclass[table,dvipsnames]{beamer}

%%% Basic packages
\usepackage{amsmath}
\usepackage{pgfpages}
\usepackage{pgfplots}
%\usepackage{graphicx}
\usepackage{picture}
\usepackage{filecontents}
%\usepackage{multicol}
\usepackage{ragged2e}
\usepackage{listings}

\usepackage{listingsutf8}
\lstset{
  language=python,
  inputencoding=utf8/latin1,
  extendedchars=true,
  keywordstyle=\bfseries\ttfamily\color{blue},
  identifierstyle=\ttfamily,
  commentstyle=\color{gray},
  stringstyle=\ttfamily\color{green!50!black},
  showstringspaces=false,
  basicstyle=\footnotesize\ttfamily,
  tabsize=2,
  breaklines=true,
  extendedchars=true,
  xleftmargin=1cm, 
  xrightmargin=1cm,
  escapeinside={(*@}{@*)},  
  backgroundcolor=\color{white!80!black},
  literate=%
    {é}{{\'{e}}}1
    {è}{{\`{e}}}1
    {ê}{{\^{e}}}1
    {ë}{{\¨{e}}}1
    {û}{{\^{u}}}1
    {ù}{{\`{u}}}1
    {â}{{\^{a}}}1
    {à}{{\`{a}}}1
    {î}{{\^{i}}}1
    {ô}{{\^{o}}}1
    {ç}{{\c{c}}}1
}

\title[Test listings]{Test listings}

\subtitle{ }
\author{ }
\institute{ } 
\date{ }

\begin{document}

\begin{frame}[fragile]
\frametitle{test}
  \begin{lstlisting}
  if (1<2):
    print('python')

  (*@\vspace*{3cm}@*)

  if (2<3):
    print('yay')
  \end{lstlisting}
\end{frame}

\end{document}