Größe des Python-Codes im LaTeX-Dokument ändern

Größe des Python-Codes im LaTeX-Dokument ändern

Ich versuche, meinen Python-Code in ein Dokument einzugeben mitDasVorlage, die dieInseratePaket. Ich möchte jedoch die Größe der Codeschriftart ändern, also alle Erwähnungen \texttt{}und die lstlistingUmgebung.

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

Ich habe versucht, fontsize=\footnotesizeverschiedene Stellen in die Präambel einzufügen, aber das hat nicht funktioniert. Irgendwelche Vorschläge?

Antwort1

Hinzufügen \footnotesizezu 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}

Bildbeschreibung hier eingeben

Allerdings sollte man auch Bera Mono noch etwas weiter verkleinern, das \footnotesizescheint in diesem Fall aber nicht nötig.

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

Bildbeschreibung hier eingeben

verwandte Informationen