
我正在使用列表包來顯示一些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}