I would need \cref
to use the same counter for two different kind of code blocks in my document (one using listings
, another using minted
).
With listings
I've defined a new language and a new style, successfully assigning this name:
\crefname{lstlisting}{listing}{listings}
And all is fine, the reference is created with the correct name, e.g.
See \cref{example} as an example :
\begin{es6}[caption={Sample file}, label={example}]
const BlogTitle = ({ children }) => (
<h3>{children}</h3>
);
% etc...
With minted
I'm stuck instead. I'm using also tcolorbox
(because of this) and I've created a new environment but the labelling is problematic:
\newenvironment{jsx}[1]{\label[lstlisting]{#1}\tcblisting{lststyle}}{\endtcblisting}
See \cref{anotherexample} as an example :
\begin{jsx}{anotherexample}
const BlogTitle = ({ children }) => (
% etc.
My understanding is that forcing the \label
to be of type [lstlisting]
should result in the same counter being used (I'd expect "See listing 2 as another example").
Instead I get the following error: "Use of \cref@override@label@type doesn't match its definition. \label@optarg ...el@type \cref@currentlabel \@nil"
Is there a way to work around this and get the same counter+"listing" labelling? Thank you!
MWE
(requires --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}