topnush
https://topanswers.xyz/tex?q=1894 very nicely shows how to use listing in exsheets. But my document is a more complicated than the original example I gave. Here is a more realistic example, including lstquestion for the first question but then showing what I need for the second.
```
\documentclass[12pt,a4paper]{article}
\usepackage{exsheets}
\usepackage{exsheets-listings}
\SetupExSheets{
headings = block-subtitle ,
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,
otherkeywords={self}, % Add keywords here
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 1
\end{lstquestion}
\begin{question}
\begin{enumerate}
\item Implement:
\begin{itemize}
\item $A$
\item $B$
\end{itemize}
\begin{python}
# Test 2
print(function("A"))
\end{python}
\item Implement more
\begin{python}
# Test 3
\end{python}
\end{enumerate}
\end{question}
\end{document}
```
Top Answer
samcarter
Quick hack:
```
\documentclass[12pt,a4paper]{article}
\usepackage{exsheets}
\usepackage{exsheets-listings}
\SetupExSheets{
headings = block-subtitle ,
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,
otherkeywords={self}, % Add keywords here
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][]
{
\lstset{style=mypython,#1}
}
{}
\begin{document}
\begin{lstquestion}[%
pre=Explain something,
listings={style={mypython}}
]
# Test 1
\end{lstquestion}
\setbox0=\hbox{%
\begin{python}[linewidth=0.905\textwidth]
# Test 2
print(function("A"))
\end{python}%
}
\setbox2=\hbox{%
\begin{python}[linewidth=0.905\textwidth]
# Test 3
\end{python}%
}
\begin{question}
\begin{enumerate}
\item Implement:
\begin{itemize}
\item $A$
\item $B$
\end{itemize}
\usebox0
\item Implement more
\usebox2
\end{enumerate}
\end{question}
\end{document}
```
![Screenshot 2021-09-09 at 15.47.29.png](/image?hash=df919de1ca813d1a72de314f4f3ab1953202010cead776bb048efdb29f4d19f9)
Caveat: the width of the listings needs to be adjusted manually. Without the lined style, one would probably get away with using the default width in most cases, but for this style, one needs to handle every case.