我需要\cref
对文档中的两种不同类型的代码块使用相同的计数器(一个使用listings
,另一个使用minted
)。
我listings
定义了一种新的语言和一种新的风格,并成功分配了该名称:
\crefname{lstlisting}{listing}{listings}
一切正常,使用正确的名称创建引用,例如
See \cref{example} as an example :
\begin{es6}[caption={Sample file}, label={example}]
const BlogTitle = ({ children }) => (
<h3>{children}</h3>
);
% etc...
而是用minted
我被困住了。我还用tcolorbox
(因为这),并且我创建了一个新的环境,但是标签有问题:
\newenvironment{jsx}[1]{\label[lstlisting]{#1}\tcblisting{lststyle}}{\endtcblisting}
See \cref{anotherexample} as an example :
\begin{jsx}{anotherexample}
const BlogTitle = ({ children }) => (
% etc.
我的理解是,强制\label
为类型[lstlisting]
应该导致使用相同的计数器(我希望“参见清单 2 作为另一个示例”)。
相反,我收到以下错误:"Use of \cref@override@label@type doesn't match its definition. \label@optarg ...el@type \cref@currentlabel \@nil"
有没有办法解决这个问题并获得相同的计数器+“列表”标签?谢谢!
平均能量损失
(需要--shell-escape)
\documentclass[]{scrbook}
\usepackage{xcolor}
\usepackage[]{cleveref}
\usepackage{listings}
\lstdefinelanguage{es6}{
morekeywords=[1]{break, continue, delete, else, for, function, if, in,
new, return, this, typeof, var, void, while, with, await, async, case, catch, class, const, default, do, enum, export, extends, finally, from, implements, import, instanceof, let, static, super, switch, throw, try },
morekeywords=[2]{false, null, true, boolean, number, undefined,
Array, Boolean, Date, Math, Number, String, Object },
morekeywords=[3]{eval, parseInt, parseFloat, escape, unescape },
otherkeywords = {+,-},
sensitive,
morecomment=[s]{/*}{*/},
morecomment=[l]//,
morecomment=[s]{/**}{*/},
morestring=[b]',
morestring=[b]"
}[keywords, comments, strings]
\lstdefinestyle{fancylisting}{
basicstyle=\ttfamily\linespread{1.0}\small,
}
\lstset{style=fancylisting}
\lstnewenvironment{es6}[1][]
{\lstset{
language=es6,
morekeywords=[4]{+,<,>,-,=},
#1
}}
{}
\crefname{lstlisting}{listing}{listings}
\Crefname{lstlisting}{Listing}{Listings}
\usepackage{fancyvrb}
\usepackage[cache=false,outputdir=.texpadtmp]{minted}
\usepackage{tcolorbox}
\usepackage{expl3}
\usepackage{tikz}
\tcbuselibrary{listings, minted, hooks, skins, breakable}
\makeatletter
\tcbset{
lststyle/.style={
enhanced,
left=10mm,
top=0mm,
bottom=0mm,
boxsep=0.5mm,
listing only,
listing engine=minted,
minted options={
obeytabs,
breaklines,
linenos,
autogobble,
fontsize=\footnotesize,
numbersep=5mm,
},
}
}
\crefname{lstlisting}{listing}{listings}
\newenvironment{jsx}[1]{\label[lstlisting]{#1}\tcblisting{lststyle}}{\endtcblisting}
\begin{document}
See \cref{example} as an example :
\begin{es6}[caption={Sample file}, label={example}]
const BlogTitle = ({ children }) => (
<h3>{children}</h3>
);
// class component
class BlogPost extends React.Component {
renderTitle(title) {
return <BlogTitle>{title}</BlogTitle>
};
render() {
return (
<div className="blog-body">
{this.renderTitle(this.props.title)}
<p>{this.props.body}</p>
</div>
);
}
}
\end{es6}
See \cref{anotherexample} as an example :
\begin{jsx}{anotherexample}
const BlogTitle = ({ children }) => (
<h3>{children}</h3>
);
// class component
class BlogPost extends React.Component {
renderTitle(title) {
return <BlogTitle>{title}</BlogTitle>
};
render() {
return (
<div className="blog-body">
{this.renderTitle(this.props.title)}
<p>{this.props.body}</p>
</div>
);
}
}
\end{jsx}
\end{document}
答案1
定义特定tcblisting
环境的规范方法是使用\newtcblisting
。此类环境的计数器可以在 的第一个参数中指定\newtcblisting
,标签可以在主参数中指定。您还可以指定用于环境的任何参数,例如标签的值。在本例中,主要基于结合 minted 和 tcolorbox 获取文件中的代码(inputminted?):
\newtcblisting[use counter=lstlisting]{jsx}[1]{
label=#1,
enhanced,
left=10mm,
top=0mm,
bottom=0mm,
boxsep=0.5mm,
listing only,
listing engine=minted,
minted options={
obeytabs,
breaklines,
linenos,
autogobble,
fontsize=\footnotesize,
numbersep=5mm,
},
}
完整 MWE:
\documentclass[]{scrbook}
\usepackage{xcolor}
\usepackage{cleveref}
\usepackage{listings}
\lstdefinelanguage{es6}{
morekeywords=[1]{break, continue, delete, else, for, function, if, in,
new, return, this, typeof, var, void, while, with, await, async, case, catch, class, const, default, do, enum, export, extends, finally, from, implements, import, instanceof, let, static, super, switch, throw, try },
morekeywords=[2]{false, null, true, boolean, number, undefined,
Array, Boolean, Date, Math, Number, String, Object },
morekeywords=[3]{eval, parseInt, parseFloat, escape, unescape },
otherkeywords = {+,-},
sensitive,
morecomment=[s]{/*}{*/},
morecomment=[l]//,
morecomment=[s]{/**}{*/},
morestring=[b]',
morestring=[b]"
}[keywords, comments, strings]
\lstdefinestyle{fancylisting}{
basicstyle=\ttfamily\linespread{1.0}\small,
}
\lstset{style=fancylisting}
\lstnewenvironment{es6}[1][]
{\lstset{
language=es6,
morekeywords=[4]{+,<,>,-,=},
#1
}}
{}
\crefname{lstlisting}{listing}{listings}
\Crefname{lstlisting}{Listing}{Listings}
\usepackage{fancyvrb}
\usepackage{minted}
\usepackage{tcolorbox}
\usepackage{expl3}
\usepackage{tikz}
\tcbuselibrary{listings, minted, hooks, skins, breakable}
\newtcblisting[use counter=lstlisting]{jsx}[1]{
label=#1,
enhanced,
left=10mm,
top=0mm,
bottom=0mm,
boxsep=0.5mm,
listing only,
listing engine=minted,
minted options={
obeytabs,
breaklines,
linenos,
autogobble,
fontsize=\footnotesize,
numbersep=5mm,
},
}
\begin{document}
See \cref{example} as an example :
\begin{es6}[caption={Sample file}, label={example}]
const BlogTitle = ({ children }) => (
<h3>{children}</h3>
);
// class component
class BlogPost extends React.Component {
renderTitle(title) {
return <BlogTitle>{title}</BlogTitle>
};
render() {
return (
<div className="blog-body">
{this.renderTitle(this.props.title)}
<p>{this.props.body}</p>
</div>
);
}
}
\end{es6}
See \cref{anotherexample} as another example :
\begin{jsx}{anotherexample}
const BlogTitle = ({ children }) => (
<h3>{children}</h3>
);
// class component
class BlogPost extends React.Component {
renderTitle(title) {
return <BlogTitle>{title}</BlogTitle>
};
render() {
return (
<div className="blog-body">
{this.renderTitle(this.props.title)}
<p>{this.props.body}</p>
</div>
);
}
}
\end{jsx}
\end{document}
结果: