사용자 정의 명령에 기본 인수 추가

사용자 정의 명령에 기본 인수 추가

일부 Python 코드를 표시하기 위해 목록 패키지를 사용하고 있지만 알고리즘에서 사용하는 글꼴 크기인 선택적 인수를 제공할 수 없는 것 같습니다. Y는 알고리즘이 일반 크기로 인쇄되기를 원하지만 환경에 매개변수를 전달하면 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}

관련 정보