I am using minted for the first time and after reading the docs, I could not find a way to put a label in the code listing. Just to be clear, I am not speaking of the \label of a listing environment. I am trying to reproduce a listing like

  label={This is the label},numbers=left]
  int main() { return 0; }

which creates a nice frame with a label "This is the label" in the middle.

Since minted uses fancyvrb, I though it would be easy to do it, but I did not succeed. The closer approach so far is

 int main() { return 0; }

Does anyone know a way or a hack to achieve this?

  • 1
    For the record, this wasn’t included “on purpose” – I didn’t think it was really all that useful, considering that you can provide float captions, and the name “label” clashes with a distinct concept in TeX (i.e. references) which I find awkward. Anyway, I’ve now added the option (but heaven only knows when I’ll find time to push the next release). – Konrad Rudolph Jan 4 '11 at 20:49
  • @Konrad: how should there be a confict? And it is useful when all those verbatim listings are not of a float type. And if you have a problem then call it title, which doesn't really make a difference. And why do you not pass all options simply to fancyvrb, then you do not have to define them in the package? – user2478 Jan 5 '11 at 13:17
  • @Herbert: there’s a conflict of meanings when talking about this (e.g. in the documentation: it’s not enough to say that “this option adds a label” since that would be confusing). – Konrad Rudolph Jan 5 '11 at 13:20
  • @Konrad: understood, then call it a title – user2478 Jan 5 '11 at 13:24
  • 1
    @Konrad: why not: "All available optional arguments of fancyvrb are possible". Then you do not have to say anything about a label or title or ... – user2478 Jan 5 '11 at 13:26

I suppose, it is only missing in minted.sty


int main() { return 0; }
int foo() {return 1; }

alt text

  • (+1) Huh, that's really more elegant! – chl Jan 4 '11 at 19:30

I didn't see anything about that in the reference manual. However, I do think you can get a pretty output with tikz. Here is an example,

% that should go into your preamble
% -%<------------------------------


                   text width=0.85\textwidth,rectangle,
                   rounded corners=0pt,inner sep=0pt,inner ysep=10pt]
% ------------------------------>%-

% and here is a framed snippet
      int main() { return 0; }
\node[title] at (box.north) {Useless function};

The output is shown below:

alt text

  • Thanks chl (+1), that's a nice solution. I only have one problem: sometimes the code that I want to show is longer than a page. Normally fancyvrb and minted handle this gracefully by cutting it (when not using a float). However, with tikzpicture I imagine that it becomes a float and hence, it is not breakable in two pages. This is a supposition, of course, since I have never used tikz before. – YuppieNetworking Jan 4 '11 at 15:56
  • I used such construction for very short snippet of code, or text advertising. Actually, @Herbert's solution looks better! – chl Jan 4 '11 at 19:31

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.