4

Thanks to Thomas F. Sturm (https://tex.stackexchange.com/a/124688/15907) I have a little environment for creating nice looking source code in LaTeX.

I started my enviroment with this here:

\begin{listingsbox}[label={lst:agent:logstash:configuration}]{mycode}{Konfiguration von LogStash auf Agent}

(The part in the square brackets are the same commands as for tcolorbox)

And I have my own little ref command:

\newcommand{\myref}[1]{see \cref{#1} \nameref{#1} \vpageref{#1}}

So I get something like: "see figure 3.1: How to chuck wood on page 31" which is really nice.

If I do this with the label of the tcolorbox:

\myref{lst:agent:logstash:configuration}

I get: "see ?? 3.1: Konfiguration von LogStash on page 31".

As you can see, cleveref is not able to provide a correct name here. I know I can change this when defining it with \crefname{<type>}{<singular>}{<plural>} but I can not figure out which type I have to provide.

Does anyone have a clue?

5

The type you are looking for is tcb@cnt@mintedbox:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{minted}
\usepackage{cleveref}

\newminted[mycsharp]{csharp}{tabsize=2,fontsize=\footnotesize}
\newminted[myjson]{js}{tabsize=2,fontsize=\footnotesize}
\newminted[myxml]{xml}{tabsize=2,fontsize=\footnotesize}
\newminted[myshell]{shell-session}{tabsize=2,fontsize=\footnotesize}
\newminted[mycode]{text}{tabsize=2,fontsize=\footnotesize}

\newtcolorbox[auto counter,number within=section,
  list inside=mypyg]{mintedbox}[2][]{%
  title={Listing \thetcbcounter: #2},
  list entry={\numberline{\thetcbcounter}#2},
  enhanced,colframe=red!50!black,drop fuzzy shadow,#1}

\newenvironment{listingsbox}[3][]
 {%
   \def\listingsboxenvironment{#2}%save the environments
   \VerbatimEnvironment%
   \begin{mintedbox}[#1]{#3}%
     \begin{\listingsboxenvironment}}%
 {%
  \end{\listingsboxenvironment}%
  \end{mintedbox}%
}

\makeatletter
\crefname{tcb@cnt@mintedbox}{minted listing}{minted listings}
\Crefname{tcb@cnt@mintedbox}{Minted listing}{Minted listings}
\makeatother

\begin{document}

\section{Test}

\Cref{lst:agent:logstash:configuration} and \cref{lst:agent:logstash:configuration}.
\Cref{lst:agent:logstash:configuration,lst:test} and \cref{lst:agent:logstash:configuration,lst:test}.

\begin{listingsbox}[label={lst:agent:logstash:configuration}]{myshell}{A nice title}
[root@localhost ~]# vi /etc/sysconfig/network
\end{listingsbox}

\begin{listingsbox}[label={lst:test}]{myshell}{Something else}
[root@localhost ~]# vi /etc/sysconfig/something
\end{listingsbox}

\end{document}

enter image description here

The type can be easily obtained examining the output console; if you process my code without the \crefname, \Crefname lines, you will get several warnings like this one:

LaTeX Warning: cref  reference format for label type `tcb@cnt@mintedbox' undefi
ned on input line 33.

And the messages show the type that has to be used in \crefname.

  • Just a little question, maybe it's easy, so you can update your answer, otherwise I would open a new question: As you can see, I'm using \nameref in my \myref marco. But it outputs the section title instead the title of the listing. Do you have any idea here? – Manuel Rauber Jul 23 '13 at 12:16
  • @Raubi I think it's better to open a new question; please provide in that question a complete little example code (like the one I used in my answer here) showing your current settings. – Gonzalo Medina Jul 23 '13 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.