I'm using the listings package to show some Python code, but I can't seem to provide an optional argument that is the font size used by the algorithm. Y would like the algorithm to be printed in normal size, but if I pass a parameter to the python environment, to use the new font size instead.
I've seen many posts about how the \newcommand works, but I can't seem to make it work (I've seen this and this for example
). The code I started with is this one:
\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}
, so make one, say,pythonstyleand then use\lstset{style=phytonstyle,#1}`, – daleif Feb 02 '17 at 14:22\begin{python}[basicstyle=\ttfamily\Huge]? – Schweinebacke Feb 02 '17 at 14:23\begin{python}[basicstyle=\ttfamily\tiny]works fine. – Ulrike Fischer Feb 02 '17 at 14:23\normalsizeby a user parameter if it's given or else use the normal size. I tried what @daleif suggested with lo luck. – aaragon Feb 02 '17 at 14:35\normalsizeis not an option, and listings does not know what do do with\begin{python}[\normalsize]all options in listings are key=value bases, and thus you need to use that. Sadly you cannot spitbasicstyleinto separate font and size part – daleif Feb 02 '17 at 14:45