2

I am trying to write one line of code in a listing in scrartlc. The problem is that the space between the listing and the text below is too big.

How it looks when using scrartlc:

enter image description here

This is how it looks like when changing \documentclass[parskip=full]{scrartlc} to \documentclass{article}:

enter image description here

I want to get the same result as I got with article but with scrartlc. How do I get it like this?

Here is my code:

\documentclass[parskip=full]{scrartcl}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\lstdefinestyle{customjava}{
language=Java,
keywordstyle=\bfseries\color{green!40!black},
\lstset{escapechar=@, style=customjava}
}

\begin{document}
test
\begin{lstlisting}
public int a;
\end{lstlisting}
test
\end{document}
  • 2
    Do you want to remove this space for all lstlistigs or just for a single one? If it's just for a single one maybe \end{lstlisting} \vskip-\belowdisplayskip test could help. – user36296 Jun 23 '17 at 17:30
  • Thank you very much! It works. What could I do for removing the space for all listings? – sininen Jun 23 '17 at 18:06
3

The additional vertical space comes from the parskip option. You will get a similar issue if you use the standard class article with package \parskip.

If you do not want to remove option parskip=full, then you could use

\usepackage{etoolbox}
\newlength\savedparskip
\BeforeBeginEnvironment{lstlisting}{\setlength\savedparskip{\parskip}}
\lstset{belowskip=\dimexpr-\savedparskip+\medskipamount\relax}

enter image description here

Code:

\documentclass[
  parskip=full
]{scrartcl}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\lstdefinestyle{customjava}{
language=Java,
keywordstyle=\bfseries\color{green!40!black}
}
\lstset{escapechar=@, style=customjava}

\usepackage{etoolbox}
\newlength\savedparskip
\BeforeBeginEnvironment{lstlisting}{\setlength\savedparskip{\parskip}}
\lstset{belowskip=\dimexpr-\savedparskip+\medskipamount\relax}

\begin{document}
test
\begin{lstlisting}
public int a;
\end{lstlisting}
test
\end{document}
2

You could define one or more lstenvironments with custom settings.

\documentclass[parskip=full]{scrartcl}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}



\makeatletter
\lstnewenvironment{rmlisting}
    {\lstset{escapechar=@, language=Java,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{DarkBlue},
  keywordstyle=\color{Green},
  stringstyle=\color{Purpple}}
    }
    {
    \vspace*{-12pt}   
    }
\makeatother    

\begin{document}

test
\begin{lstlisting}
 public int a;
\end{lstlisting}
test


test
\begin{rmlisting}
 public int a; @%and here your comment etc@
\end{rmlisting}
test

\end{document}

See here how it works

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.