exsheet 中的 Pycode 代码问题

exsheet 中的 Pycode 代码问题

我想在 exsheets 问题(或解决方案)中创建一个表(使用在其外部的另一个 pycode 环境中定义的数据)。但这行不通。我做错了什么,我该如何让它工作?

这是一个简单的例子:

\documentclass{article}

\usepackage{exsheets}
\usepackage[gobble=auto]{pythontex}

\usepackage{siunitx}
\sisetup{
  round-mode=places,
  round-precision=1
}

\begin{document}

\begin{pycode}[mySession]
X = [1,2,3]
Y = [4,5,6]
k = len(X)
\end{pycode}

\begin{question}
A question with a table:

% The following code doesn't work

% \begin{pycode}[mySession]
% print(r'''\begin{tabular}{c*{%s}{|c}}''' %k)
% print(r'''$X$''')
% for x in X:
%     print(r'''& \num{%s}''' %x)
% print(r'''\\\hline''')
% print(r'''$Y$''')
% for y in Y:
%     print(r'''& \num{%s}''' %y)
% print(r'''\end{tabular}''')
% \end{pycode}

\end{question}

Table outside of the question environment:

\begin{pycode}[mySession]
print(r'''\begin{tabular}{c*{%s}{|c}}''' %k)
print(r'''$X$''')
for x in X:
    print(r'''& \num{%s}''' %x)
print(r'''\\\hline''')
print(r'''$Y$''')
for y in Y:
    print(r'''& \num{%s}''' %y)
print(r'''\end{tabular}''')
\end{pycode}
\end{document}

编辑

如果我用 xsim 替换 exsheets,它可以在练习中起作用,但如果我在练习和解决方案环境中都有它,它就不会起作用:

\documentclass{article}

\usepackage{xsim}
\usepackage[gobble=auto]{pythontex}

\usepackage{siunitx}
\sisetup{
  round-mode=places,
  round-precision=1
}

\begin{document}

\begin{pycode}[mySession]
X = [1,2,3]
Y = [4,5,6]
k = len(X)
\end{pycode}

\begin{exercise}
A exercise with a table:

\begin{pycode}[mySession]
print(r'''\begin{tabular}{c*{%s}{|c}}''' %k)
print(r'''$X$''')
for x in X:
    print(r'''& \num{%s}''' %x)
print(r'''\\\hline''')
print(r'''$Y$''')
for y in Y:
    print(r'''& \num{%s}''' %y)
print(r'''\end{tabular}''')
\end{pycode}

\end{exercise}

\begin{solution}
\begin{pycode}[mySession]
print(r'''\begin{tabular}{c*{%s}{|c}}''' %k)
print(r'''$X$''')
for x in X:
    print(r'''& \num{%s}''' %x)
print(r'''\\\hline''')
print(r'''$Y$''')
for y in Y:
    print(r'''& \num{%s}''' %y)
print(r'''\end{tabular}''')
\end{pycode}

\end{solution}

\printsolutions
\end{document}

答案1

exsheets软件包已过时,并且已被正式替换为该软件包xsim

您需要替换\begin{question}\begin{exercise}

相关内容