嗨,我有一个灰色框,里面有我的 Python 代码,我想减小代码的大小,但保持灰色框的大小不变。这是我的 Latex 代码:
\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}
如果不指定字体大小,输出如下所示:
使用我在顶部提到的代码(包括字体大小),它看起来像这样:
如您所见,框变小了。有没有办法让框保持相同的大小,同时减小字体大小?
答案1
最好是建立框架并listings
提供以下机制:
backgroundcolor=\color{gray!15},
rulecolor=\color{gray},
frame=tlrb
\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}