# Natural width when centering a minted environment

I'm writing a report in which I have some code snippets. Using the following code, I'm able to create, and centre, a good-looking minted environment:

\documentclass{article}

% Required for minted
\usepackage{keyval}
\usepackage{kvoptions}
\usepackage{fancyvrb}
\usepackage{float}
\usepackage{ifthen}
\usepackage{calc}
\usepackage{ifplatform}
\usepackage{pdftexcmds}
\usepackage{etoolbox}
\usepackage{xstring}
\usepackage{xcolor}
\usepackage{lineno}

\usepackage{minted}

\setminted[c++]{ %
linenos=true,             % Line numbers
autogobble=true,          % Automatically remove common whitespace
%bgcolor=dark-bg,
frame=lines,
framesep=2mm,
fontsize=\footnotesize
}

\newenvironment{code}{%
\VerbatimEnvironment
\begin{figure}[thp]%
\centering
\begin{minipage}{0.8\textwidth}%
\begin{minted}{c++}}
{%
\end{minted}
\end{minipage}
\end{figure}}

\begin{document}

\begin{code}
struct config {
/* Default values. */
std::string
port    = "6667",
nick    = "Temeraire",
user    = nick,

/* Optional */

/* NickServ identification */
nick_pw   = "",
server_pw = "";

bool ssl  = true;
};
\end{code}

\end{document}


But when the same environment is used with longer code, this is the result:

As seen here, minted's frame lines are far from long enough, and the whole of the code is no longer centred.

While 0.4\textwidth can be altered to fit the specific code used, it would require me to create a new environment for every code snippet I wish to include. How can the width of the environment be the same as the longest line of code?

• Fixed the example. Jan 22, 2016 at 21:09
• It's quite easy to get minted into using BVerbatim instead of Verbatim, but, unfortunately, it seems that BVerbatim can't show line numbers. Jan 22, 2016 at 21:14
• doesn't quite do the right thing with varwidth, shame.. Jan 22, 2016 at 21:18

You can use BVerbatim instead of Verbatim, but the line numbers and the frame must be added manually.

\documentclass{article}
\usepackage{minted}

\usepackage{lipsum}

\setminted[c++]{ %
linenos=true,             % Line numbers
autogobble=true,          % Automatically remove common white space
frame=lines,
framesep=2mm,
fontsize=\footnotesize
}

\makeatletter
\newenvironment{code}
{\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
\def\FV@BProcessLine##1{%
\hbox{%
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\FancyVerbFormatLine{##1}%
}%
}%
\VerbatimEnvironment
\setbox\z@=\hbox\bgroup
\begin{minted}{c++}}
{\end{minted}\egroup
\leavevmode\vbox{\hrule\kern2mm\box\z@\kern2mm\hrule}}
\makeatother

\begin{document}

\begin{minted}{c++}
struct config {
/* Default values. */
std::string
port    = "6667",
nick    = "Temeraire",
user    = nick,

/* Optional */

/* NickServ identification */
nick_pw   = "",
server_pw = "";

bool ssl  = true;
};
\end{minted}

\lipsum[2]

\begin{figure}[htp]
\centering
\begin{code}
struct config {
/* Default values. */
std::string
port    = "6667",
nick    = "Temeraire",
user    = nick,

/* Optional */

/* NickServ identification */
nick_pw   = "",
server_pw = "";

bool ssl  = true;
};
\end{code}

\caption{Some centered code}
\end{figure}

\end{document}