1

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...

enter image description here

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.

enter image description here

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}

1 Answer 1

1

The canonical way of defining a specific tcblisting environment is using \newtcblisting. The counter for such an environment can be specified in the first argument of \newtcblisting and the label in the main argument. You can also specify any arguments to be used for the environment, for example the value of the label. In this case, based mostly on Combine minted and tcolorbox for code from file (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,
    },
}

Full 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}

Result:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .