topnush
I would like to typeset some Python code in a question using exsheets. However this gives a fatal error:

> ! Argument of \lst@next has an extra }.
> <inserted text>
>                 \par
> l.56 \end{question}

If you remove the \begin{question} and \end{question} the document renders.

I am not particularly attached to this way of typesetting python and would be happy with an alternative that looked better (the commented lines aren't typeset well) and worked with exsheets.

\documentclass[12pt,a4paper]{article}
\usepackage{exsheets}
\SetupExSheets{
subtitle-format = \bfseries % default is \itshape
}
\usepackage{paralist}
\usepackage{amsfonts}
\usepackage{amsmath}
\DeclareMathOperator{\var}{var}
\DeclareMathOperator{\E}{\mathbb{E}}
\SetupExSheets{solution/print=true}
\SetupExSheets{question/type=exam}
\SetupExSheets[points]{name=point,name-plural=points}

% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tb,                         % Any extra options here
showstringspaces=false            %
}}

% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}

\begin{document}
\begin{question}
\begin{python}
# Test
\end{python}
\end{question}
\end{document}


samcarter
To quote from the exsheets documentation:

> I knew the day would come when people would ask how to include verbatim material in the question and solution environments. Since they’re defined with the environ package they’re reading their environment bodies like macros read their arguments. This makes it impossible to use verbatim material inside them. Now the day has come [Bra13]. Soon after the first question appeared I wrote the first draft for ExSheets-listings for a question on TEX.sx [Ass13].

Here an example how you can use the exsheets-listings package with your python setup:


\documentclass[12pt,a4paper]{article}
\usepackage{exsheets}
\usepackage{exsheets-listings}
\SetupExSheets{
subtitle-format = \bfseries % default is \itshape
}
\usepackage{paralist}
\usepackage{amsfonts}
\usepackage{amsmath}
\DeclareMathOperator{\var}{var}
\DeclareMathOperator{\E}{\mathbb{E}}
\SetupExSheets{solution/print=true}
\SetupExSheets{question/type=exam}
\SetupExSheets[points]{name=point,name-plural=points}

% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

% Python style for highlighting
\lstdefinestyle{mypython}{%
language=Python,
basicstyle=\ttm,
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tb,                         % Any extra options here
showstringspaces=false            %
}

\begin{document}
\begin{lstquestion}[%
pre=Explain something,
listings={style={mypython}}
]
# Test
\end{lstquestion}
\end{document}

![Screenshot 2021-09-09 at 13.48.40.png](/image?hash=62ddb5db8e6173ae30edc69fce54e34e560afd20ea518896c1662a68e90afd7b)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.