1

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 – Marco Daniel Jun 26 '13 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 '13 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 '13 at 10:42
  • Could you suggest any possible alternative? – Chatterjee Jun 26 '13 at 10:43
  • @egreg: That's the reason for BVerbatim ;-) – Marco Daniel Jun 26 '13 at 15:09
2

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

\documentclass{article}
\usepackage{minted,varwidth}
\makeatletter
\newenvironment{mintedshort}[2][]
 {\VerbatimEnvironment%
  \renewcommand{\minted@proglang}[1]{#2}%
  \minted@resetoptions%
  \setkeys{minted@opt}{#1}%
  \begin{VerbatimOut}[codes={\catcode`\^^I=12}]{\jobname.pyg}}%
 {\end{VerbatimOut}%
  \minted@pygmentize@short{\minted@proglang{}}%
  \DeleteFile{\jobname.pyg}}

\newcommand\minted@pygmentize@short[2][\jobname.pyg]{%
  \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}%
  \immediate\write18{\minted@cmd}%
  \begingroup%
    \setbox0=\hbox{%
     \let\Verbatim\BVerbatim%
     \let\endVerbatim\endBVerbatim%
      \input{\jobname.out.pyg}%
    }%
   \box0%
   \endgroup%
  \DeleteFile{\jobname.out.pyg}}
\makeatother

\begin{document}

\hrulefill
\begin{mintedshort}{latex}
\tableofcontents
\part{Part}
\chapter{Chapter}
\section{Section}
\end{mintedshort}
\hrulefill

\end{document}

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