更改 LaTeX 文档中的 Python 代码的大小

更改 LaTeX 文档中的 Python 代码的大小

我正在尝试使用以下方式在文档中输入我的 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

添加:\footnotesizebasicstyle

\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}

在此处输入图片描述

相关内容