1

Hi I have a gray box that contains my python code, I want to reduce the size of the code, but keep the grey box the same size. This is my latex code:

\lstset
{ %Formatting for code in appendix
    language={Python},
    basicstyle=\fontsize{9}{9}\ttfamily,
    numbers=left,
    stepnumber=1,
    showstringspaces=false,
    keywordstyle=\color{blue},
    commentstyle=\color{magenta},
    stringstyle=\color{ForestGreen},
    tabsize=4,
    breaklines=true,
    breakatwhitespace=false,
}


\fcolorbox{gray}{gray!15}{
\lstinputlisting[language=Python]{./Code/get_zip_files.py}

Without specifying the font size, the output looks like this:

enter image description here

With the code I mentioned at the top (including the font size) it looks like this:

enter image description here

As you can see the box became smaller. Is there a way to keep the same size for the box but lower the font size at the same time?

1

1 Answer 1

3

It is probably best to set the frame up with the mechanisms that listings provides, e.g.

backgroundcolor=\color{gray!15},
rulecolor=\color{gray},
frame=tlrb

Sample output

\documentclass{article}

\usepackage{listings,xcolor}

\lstset
{ %Formatting for code in appendix
    language={Python},
    basicstyle=\fontsize{9}{9}\ttfamily,
    numbers=left,
    stepnumber=1,
    showstringspaces=false,
    keywordstyle=\color{blue},
    commentstyle=\color{magenta},
    stringstyle=\color{green},
    tabsize=4,
    breaklines=true,
    breakatwhitespace=false,
    backgroundcolor=\color{gray!15},
    rulecolor=\color{gray},
    frame=tlrb
}

\begin{document}

\noindent
A full line of text before the sample code.
A full line of text before the sample code.
A full line of text before the sample code.
\begin{lstlisting}[language=Python]
import requests

path = 'A:/Thesis_project/SBP/'
\end{lstlisting}

\end{document}
1
  • Thank you for the solution!
    – Adrian
    Commented May 2, 2019 at 17:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .