最后呈现完整的 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}