我正在使用 listings 包来显示一些 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}