![解决创建新代码环境时出现的 FancyVerb 错误](https://linux22.com/image/407213/%E8%A7%A3%E5%86%B3%E5%88%9B%E5%BB%BA%E6%96%B0%E4%BB%A3%E7%A0%81%E7%8E%AF%E5%A2%83%E6%97%B6%E5%87%BA%E7%8E%B0%E7%9A%84%20FancyVerb%20%E9%94%99%E8%AF%AF.png)
最后呈现完整的 MWE
我正在用 minted 创建我自己的代码环境,用于编写文本。它是可枚举的,也是可引用的。一切正常,文档按“预期”进行编译。不幸的是,即使文档编译成功,也会不断弹出错误。
为了创建我的代码环境,我使用:
\newcounter{code}[chapter]
\renewcommand\thecode{\thechapter.\arabic{code}}
\definecolor{bg}{rgb}{0.975,0.95,1.0}
\newenvironment{code}[3][h!bp]
{\VerbatimEnvironment \refstepcounter{code}
\label{#3}
\begin{listing}[#1]
\textbf{Code~\thecode} #2
\centering
\begin{minted}[fontsize=\small, bgcolor=bg, frame=lines, framesep=2mm]{julia}
}
{\end{minted}\end{listing}}
我称之为
\begin{code}{Example code defining the Lorenz-63 system, eq.5 in Julia, and obtaining a trajectory for it using DynamicalSystems.jl.}{DSY_cod_example}
using DynamicalSystems # load the library
# define dynamical rule for Lorenz-63
function lorenz_f(u, p, t)
σ = p[1]; ρ = p[2]; β = p[3]
du1 = σ*(u[2]-u[1])
du2 = u[1]*(ρ-u[3]) - u[2]
du3 = u[1]*u[2] - β*u[3]
return SVector{3}(du1, du2, du3)
end
tr = trajectory(lorenz, T; dt = dt)
\end{code}
它指向我代码中启动块的位置\begin{code}
。不幸的是,我不明白这个问题...
我该如何阻止此错误?在我的实际文档中,我有数十个这样的代码块,错误不断累积……
全功率
\documentclass{book}
\usepackage[utf8]{inputenc}
\title{codetest}
\author{test}
\date{January 2020}
\usepackage{natbib}
\usepackage{graphicx}
%% The following are for using code snippets
\usepackage{minted}
\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{fancyvrb} % for inline code
\usepackage{listings}
\begin{document}
\maketitle
\newcounter{code}[chapter]
\renewcommand\thecode{\thechapter.\arabic{code}}
\definecolor{bg}{rgb}{0.975,0.95,1.0}
\newcommand{\incode}[1]{\colorbox{bg}{\texttt{#1}}}
\newenvironment{code}[3][h!bp]
{\VerbatimEnvironment \refstepcounter{code}
\label{#3}
\begin{listing}[#1]
\textbf{Code~\thecode} #2
\centering
\begin{minted}[fontsize=\small, bgcolor=bg, frame=lines, framesep=2mm]{julia}
}
{\end{minted}\end{listing}}
\chapter{Introduction}
Look at Code.~\ref{DSY_cod_example}, similar to the code that we will be showing in the rest of the book.
\begin{code}{Example code defining the Lorenz-63 system, eq.5 in Julia, and obtaining a trajectory for it using DynamicalSystems.jl.}{DSY_cod_example}
using DynamicalSystems # load the library
# define dynamical rule for Lorenz-63
function lorenz_f(u, p, t)
σ = p[1]; ρ = p[2]; β = p[3]
du1 = σ*(u[2]-u[1])
du2 = u[1]*(ρ-u[3]) - u[2]
du3 = u[1]*u[2] - β*u[3]
return SVector{3}(du1, du2, du3)
end
tr = trajectory(lorenz, T; dt = dt)
\end{code}
\bibliographystyle{plain}
\bibliography{references}
\end{document}
答案1
未受保护的行尾。
\documentclass{book}
\usepackage[utf8]{inputenc}
\title{codetest}
\author{test}
\date{January 2020}
\usepackage{natbib}
\usepackage{graphicx}
%% The following are for using code snippets
\usepackage{minted}
\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{fancyvrb} % for inline code
\usepackage{listings}
\begin{document}
\maketitle
\newcounter{code}[chapter]
\renewcommand\thecode{\thechapter.\arabic{code}}
\definecolor{bg}{rgb}{0.975,0.95,1.0}
\newcommand{\incode}[1]{\colorbox{bg}{\texttt{#1}}}
\newenvironment{code}[3][h!bp]
{\VerbatimEnvironment \refstepcounter{code}%
\label{#3}%
\begin{listing}[#1]%
\textbf{Code~\thecode} #2%
\centering
\begin{minted}[fontsize=\small, bgcolor=bg, frame=lines, framesep=2mm]{julia}%
}
{\end{minted}\end{listing}}
\chapter{Introduction}
Look at Code.~\ref{DSY_cod_example}, similar to the code that we will be showing in the rest of the book.
\begin{code}{Example code defining the Lorenz-63 system, eq.5 in Julia, and obtaining a trajectory for it using DynamicalSystems.jl.}{DSY_cod_example}
using DynamicalSystems # load the library
# define dynamical rule for Lorenz-63
function lorenz_f(u, p, t)
σ = p[1]; ρ = p[2]; β = p[3]
du1 = σ*(u[2]-u[1])
du2 = u[1]*(ρ-u[3]) - u[2]
du3 = u[1]*u[2] - β*u[3]
return SVector{3}(du1, du2, du3)
end
tr = trajectory(lorenz, T; dt = dt)
\end{code}
\bibliographystyle{plain}
\bibliography{references}
\end{document}