カスタムコマンドにデフォルト引数を追加する

カスタムコマンドにデフォルト引数を追加する

私はlistingsパッケージを使用してPythonコードを表示していますが、アルゴリズムで使用されるフォントサイズであるオプションの引数を提供できないようです。アルゴリズムを通常のサイズで印刷したいのですが、環境にパラメータを渡すと、python代わりに新しいフォントサイズが使用されます。動作方法について多くの投稿を見てきました\newcommandが、うまく動作しないようです(これそしてこれたとえば )。私が最初に作成したコードは次のとおりです。

\documentclass{article}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{listings}

\newcommand\pythonstyle{\lstset{language=Python,
  basicstyle=\ttfamily\normalsize,
  showstringspaces=false,
  emph={ClassName},          % Custom highlighting
  morekeywords={>, >=, <, <=, ==, !=, __init__, __new__, __lt__, __eq__, __ne__, __le__, __ge__, __gt__, __repr__, __str__, __del__, __format__, __hash__, __bool__}
}}
% Python environment
\lstnewenvironment{python}[1][mathescape]
{
\pythonstyle
\lstset{#1}
}
{}

\begin{document}

\begin{python}
def hi(s):
    print("how are you?")
\end{python}

\end{document}

答え1

ただし、質問の解決策ではすでに の使用がサポートされているため、これを実行することはありません\begin{python}[\basicstyle=\ttfamily\Huge]

\documentclass{article}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{listings}

\newcommand\pythonstyle[1][\normalsize]{\lstset{language=Python,
  basicstyle=\ttfamily#1,
  showstringspaces=false,
  emph={ClassName},          % Custom highlighting
  morekeywords={>, >=, <, <=, ==, !=, __init__, __new__, __lt__, __eq__, __ne__, __le__, __ge__, __gt__, __repr__, __str__, __del__, __format__, __hash__, __bool__}
}}
% Python environment
\lstnewenvironment{python}[1][\normalsize]
{
\pythonstyle[#1]
}
{}

\begin{document}

\begin{python}
def hi(s):
    print("how are you?")
\end{python}

\begin{python}[\Huge]
def hi(s):
    print("how are you?")
\end{python}


\end{document}

関連情報