0

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}
6
  • 1
    listings allows you to define your own style (\lstdefinestyle{name}{code}, so make one, say, pythonstyle` and then use \lstset{style=phytonstyle,#1},
    – daleif
    Feb 2, 2017 at 14:22
  • Didn't really understand your question. Are you searching for \begin{python}[basicstyle=\ttfamily\Huge]? Feb 2, 2017 at 14:23
  • You question is unclear. You are speaking of font sizes, but use mathescape as example. Beside this \begin{python}[basicstyle=\ttfamily\tiny] works fine. Feb 2, 2017 at 14:23
  • It's in the code, I would like to replace \normalsize by a user parameter if it's given or else use the normal size. I tried what @daleif suggested with lo luck.
    – aaragon
    Feb 2, 2017 at 14:35
  • \normalsize is 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 spit basicstyle into separate font and size part
    – daleif
    Feb 2, 2017 at 14:45

1 Answer 1

1

I wouldn't do this, because the solution of the question already supports using, e.g., \begin{python}[\basicstyle=\ttfamily\Huge], nevertheless:

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .