我正在关注这个答案中的解决方案为文件中的代码块添加标题和交叉引用.Rmd
。
我想使用cleveref
来引用这些代码块。但是,目前我的\cref
的前缀是??
。我该如何修复我的代码以防止这种情况发生?
平均能量损失
\documentclass{report}
\usepackage{color}
\usepackage{cleveref}
\usepackage{floatrow}
\usepackage{caption}
\usepackage{framed}
\usepackage{fancyvrb}
% Extra bits for context
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
\definecolor{shadecolor}{RGB}{248,248,248}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
% Make a new chunk float to attach label and caption to
\DeclareNewFloatType{chunk}{placement=H, fileext=chk, name=}
\captionsetup{options=chunk}
\renewcommand{\thechunk}{Chunk~\thesection.\arabic{chunk}}
\makeatletter
\@addtoreset{chunk}{chapter}
\makeatother
\begin{document}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{## Assign the value 5 to x}
\NormalTok{x =}\StringTok{ }\DecValTok{5}
\NormalTok{x}
\end{Highlighting}
\end{Shaded}
\captionof{chunk}{This is an example of a code caption.}\label{chk:example}
In \cref{chk:example} we do computering.
\end{document}
答案1
您必须声明\crefname
& \Crefname
。我借此机会简化了您的代码。
\documentclass{report}
\usepackage{color}
\usepackage{cleveref}
\usepackage{floatrow}
\usepackage{caption}
\usepackage{framed}
\usepackage{fancyvrb}
% Extra bits for context
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
\definecolor{shadecolor}{RGB}{248,248,248}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
% Make a new chunk float to attach label and caption to
\DeclareNewFloatType{chunk}{placement=H, fileext=chk, within=section, name=Chunk}
\crefname{chunk}{chunk}{chunks}
\Crefname{chunk}{Chunk}{Chunks}
\begin{document}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{## Assign the value 5 to x}
\NormalTok{x =}\StringTok{ }\DecValTok{5}
\NormalTok{x}
\end{Highlighting}
\end{Shaded}
\captionof{chunk}{This is an example of a code caption.}\label{chk:example}
In \cref{chk:example} we do computering.
\end{document}