
Estou usando o pacote de listagens para mostrar algum código Python, mas não consigo fornecer um argumento opcional que seja o tamanho da fonte usado pelo algoritmo. Y gostaria que o algoritmo fosse impresso em tamanho normal, mas se eu passar um parâmetro para o python
ambiente, usaria o novo tamanho de fonte. Já vi muitos posts sobre como \newcommand
funciona, mas não consigo fazer funcionar (viesseeessepor exemplo ). O código com o qual comecei é este:
\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}
Responder1
Eu não faria isso, porque a solução da questão já suporta o uso, por exemplo, \begin{python}[\basicstyle=\ttfamily\Huge]
, no entanto:
\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}