我正在尝试使用以下方式在文档中输入我的 Python 代码这模板使用列表包。但是我想更改代码字体的大小,即任何提及\texttt{}
和lstlisting
环境的内容。
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage[scaled]{beramono}
\usepackage{listings}
\lstset{
language=Python, showstringspaces=false, formfeed=\newpage, tabsize=4,
commentstyle=\itshape, basicstyle=\ttfamily, morekeywords={models, lambda, forms}
fontsize=\footnotesize
}
\newcommand{\code}[2]{
\hrulefill
\subsection*{#1}
\lstinputlisting{#2}
\vspace{2em}
}
\begin{document}
This is the code from my file \texttt{mycode.py}:
\begin{lstlisting}
def factorial(n):
"""The factorial of a number the slow way"""
# comments are printed in italic
if n == 0:
return 1
else:
return n * factorial(n-1)
\end{lstlisting}
\end{document}
我尝试fontsize=\footnotesize
在序言中放置不同的位置,但没有效果。有什么建议吗?
答案1
添加:\footnotesize
basicstyle
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage[scaled]{beramono}
\usepackage{listings}
\lstset{
language=Python,
showstringspaces=false,
formfeed=\newpage,
tabsize=4,
commentstyle=\itshape,
basicstyle=\ttfamily\footnotesize,
morekeywords={models, lambda, forms},
}
\newcommand{\code}[2]{%
\hrulefill
\subsection*{#1}%
\lstinputlisting{#2}%
\vspace{2em}%
}
\begin{document}
This is the code from my file \texttt{mycode.py}:
\begin{lstlisting}
def factorial(n):
"""The factorial of a number the slow way"""
# comments are printed in italic
if n == 0:
return 1
else:
return n * factorial(n-1)
\end{lstlisting}
\end{document}
但是,您还应该将 Bera Mono 缩小一点;在这种情况下,\footnotesize
似乎没有必要。
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage[scaled=0.8]{beramono}
\usepackage{listings}
\lstset{
language=Python,
showstringspaces=false,
formfeed=\newpage,
tabsize=4,
commentstyle=\itshape,
basicstyle=\ttfamily\footnotesize,
morekeywords={models, lambda, forms},
}
\newcommand{\code}[2]{%
\hrulefill
\subsection*{#1}%
\lstinputlisting{#2}%
\vspace{2em}%
}
\begin{document}
This is the code from my file \texttt{mycode.py}:
\begin{lstlisting}
def factorial(n):
"""The factorial of a number the slow way"""
# comments are printed in italic
if n == 0:
return 1
else:
return n * factorial(n-1)
\end{lstlisting}
\end{document}