
我正在嘗試使用以下命令在文件中輸入我的 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}