我正在尝试将 Python 列表放入 a 中\fbox\parbox
,但总是出错。
这是我的代码:
\documentclass[12pt]{article}
% Packages
\usepackage[utf8]{inputenc} % support for accents
\usepackage[T1]{fontenc}
\usepackage[francais]{babel} % doc language
\usepackage{lmodern}
\usepackage[a4paper]{geometry} % marges
\usepackage{xcolor} % text color
\usepackage{sectsty} % colorize sections
\usepackage{changepage}
\usepackage{moreverb} % code with indent
\usepackage{listings} % display code with magnification
\usepackage{amssymb}
\usepackage{amsmath} % Text into equation
\usepackage{enumitem} % Continue enumerate numbers
\usepackage{fourier} % Double brackets
% Python listing
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\sffamily,
keywordstyle=\textbf,
commentstyle=\color{blue},
showstringspaces=false,
frame=tb, numbers=left }}
% Python environment
\lstnewenvironment{python}[1][]{
\pythonstyle \lstset{#1} }{}
\begin{document}
\begin{enumerate}
\item On suppose qu'on dispose d'une fonction \texttt{binom(n,k)}, prenant en entrée deux entiers, et qui calcule $\binom{n}{k}$. Ecrire en langage \textsc{Python} une fonction \texttt{bernoulli(n)}, qui prend en entrée un entier naturel, et qui renvoie la liste \texttt{[b\_0, b\_1, ..., b\_n]}. \par
\fbox{\parbox{\linewidth-2\fboxrule-2\fboxsep}{
La relation précédente donne, pour $n \ge 2$ :
\[ \binom{n}{n-1} b_{n-1} = - \sum_{j=0}^{n-2} \binom{n}{j} b_j \]
autrement dit on a la relation de récurrence, pour tout $m \ge 1$ :
\[ b_m = -\frac{1}{m+1} \sum_{j=0}^{m-1} \binom{n}{j} b_j \]
On en déduit le code demandé :
}}
\fbox{\parbox{\linewidth-2\fboxrule-2\fboxsep}{
\begin{python}
def bernoulli(n) :
liste_bj = [1]
for m in range(1,n+1):
b = 0
for j in range(m):
b -= binom(m+1,j) * liste_bj[j]
b /= m+1
liste_bj.append(b)
return liste_bj
\end{python}
}} %err1
\end{enumerate}
我收到以下错误:
- 在文档类上:紧急停止,发生致命错误,未生成输出 PDF!
- 在 err1 上:\lst@next 的参数有一个额外的 }。段落在 \lst@next 完成之前结束。额外的 },或者忘记了 \endgroup
- 在 \end{enumerate} 上: \begin{python} 由 \end{enumerate} 结束
- 在 \end{document} 上:\begin{enumerate} 以 \end{document} 结束。您不能在内部垂直模式下使用 '\end'。缺少 { 插入
我试图删除序言中的“python 列表”和“python 环境”并替换\begin{python}
为\begin{listings}
(与 相同\end
)以使用标准代码表示来重置我的 LaTeX 项目,但我仍然遇到相同的错误。因此,LaTeX 似乎很难阅读我的列表关闭命令是在关闭枚举命令之前放置的。也许是因为我将列表放入了\fbox\parbox
?
它\fbox\parbox
适用于其中的文本和方程式,因此问题不来自那里。