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

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

ここに画像の説明を入力してください

関連情報