Is there a way to incorporate a minted environment inside a varwidth environment? I have colored backgrounds for code snippets, and I did manage to put a minted environment inside a minipage, based on the answer to my previous question:

minted Environment Inside minipage Environment

However, it is really frustrating to manually adjust the width of the minipage for each code snippet. I tried wrapping the minted environment with the varwidth environment in minted.sty, but am getting some weird errors.

  • minted uses fancyvr to print the material. Therefor the environment Verbatim is in used. Instead of this environment you can redefine some internals and use the environment BVerbatim which is also provided by fancyvrb Jun 26, 2013 at 5:10
  • I'm kind of struggling a bit to edit the minted.sty file, since I'm still quite new to this. Could you perhaps help out a bit? Or maybe suggest an alternative, if jumping into the minted internals is just too cumbersome?
    – Chatterjee
    Jun 26, 2013 at 10:08
  • Getting errors with varwidth and a nested minted is not unexpected; but even if it worked, you'd not get anything more than with minipage, since Verbatim always uses the full text width available (as set by the document or by the minipage you're in).
    – egreg
    Jun 26, 2013 at 10:42
  • Could you suggest any possible alternative?
    – Chatterjee
    Jun 26, 2013 at 10:43
  • @egreg: That's the reason for BVerbatim ;-) Jun 26, 2013 at 15:09

1 Answer 1


As a starting point you can use the following redefinition. Maybe someone can improve my code:


  \def\minted@cmd{pygmentize -l #2 -f latex -F tokenmerge
    \minted@opt{gobble} \minted@opt{texcl} \minted@opt{mathescape}
    \minted@opt{startinline} \minted@opt{funcnamehighlighting}
    \minted@opt{linenos} -P "verboptions=\minted@opt{extra}"
    -o \jobname.out.pyg #1}%




enter image description here

  • Apparently, the options aren't working. I tried changing the background color, but it's not working. Moreover, using this environment inside a varwidth environment in my TeX file is still producing errors. I will try to embed the varwidth environment in minted.sty, based on your definition. This might take a while
    – Chatterjee
    Jun 26, 2013 at 23:26

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.