4

the following code :

\documentclass{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage{tikz}
\usepackage[many]{tcolorbox}
\usepackage{graphicx}
\usepackage{float}
\usepackage{caption}
\usepackage{subcaption}


\usepackage{listings}

\lstset{ 
    backgroundcolor=\color{gray!10},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}; should come as last argument
    %basicstyle=\footnotesize,        % the size of the fonts that are used for the code
    breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
    breaklines=true,                 % sets automatic line breaking
    captionpos=b,                    % sets the caption-position to bottom
    commentstyle=\color{cyan},    % comment style
    escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
    extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
    keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
    keywordstyle=\color{darkblue},       % keyword style
    language=bash,       
    % numbers=left,             % the language of the code
    otherkeywords={xxx},
    numbersep=5pt,                   % how far the line-numbers are from the code
    showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
    showstringspaces=false,          % underline spaces within strings only
    showtabs=false,                  % show tabs within strings adding particular underscores
    stepnumber=1,                    % the step between two line-numbers. If it's 1, each line will be numbered
    stringstyle=\color{red},     % string literal style
    tabsize=2,                     % sets default tabsize to 2 spaces
    title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}
\begin{document}
\sffamily


  \begin{lstlisting}[language=bash,caption={xxx}]
    ubuntu@kubernetesmaster:~$ sudo apt get install
        \end{lstlisting}
\end{document}    

Gives me : Output

How can I remove the ugly gray under the command?

Thanks for the help

  • Adding belowskip=0pt to your \lstset removes all space after the listing. – Alain Merigot Jul 9 '19 at 9:35
  • The culprit are the spaces before \end{lstlisting} which add another line... – Rmano Jul 9 '19 at 9:39
8

Well, simply remove some blanks causing your second line in listing:

\begin{lstlisting}[language=bash,caption={xxx}]
  ubuntu@kubernetesmaster:~$ sudo apt get install
\end{lstlisting}

With the complete corrected mwe

\documentclass{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage{tikz}
\usepackage[many]{tcolorbox}
\usepackage{graphicx}
\usepackage{float}
\usepackage{caption}
\usepackage{subcaption}


\usepackage{listings}

\lstset{ 
    backgroundcolor=\color{gray!10},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}; should come as last argument
    %basicstyle=\footnotesize,        % the size of the fonts that are used for the code
    breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
    breaklines=true,                 % sets automatic line breaking
    captionpos=b,                    % sets the caption-position to bottom
    commentstyle=\color{cyan},    % comment style
    escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
    extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
    keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
    keywordstyle=\color{darkblue},       % keyword style
    language=bash,       
    % numbers=left,             % the language of the code
    otherkeywords={xxx},
    numbersep=5pt,                   % how far the line-numbers are from the code
    showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
    showstringspaces=false,          % underline spaces within strings only
    showtabs=false,                  % show tabs within strings adding particular underscores
    stepnumber=1,                    % the step between two line-numbers. If it's 1, each line will be numbered
    stringstyle=\color{red},     % string literal style
    tabsize=2,                     % sets default tabsize to 2 spaces
    title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}


\begin{document}
\sffamily

\begin{lstlisting}[language=bash,caption={xxx}]
  ubuntu@kubernetesmaster:~$ sudo apt get install
\end{lstlisting}
\end{document}

you get the result:

result

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.