[Tex/LaTex] Box around minted environment

framedminted

I'm using the minted package to produce nice looking java-code but I would like to make a box around the code so it is easier to separate the code from the rest of the document.

What is the easiest way to do this?

Here is an example of my document. I'd like the java-code to be inside a box.

: enter image description here


Thanks for the suggestions. Torbjørn T's solution seems to work perfect but some of my code is too wide for the normal width defined by the margins. The minted environment is ignoring the margins (I can't really decide wether or not this is a bad thing) but the frame produced by minted follows the margin-rule.

Would it be best to make the frame ignore the margins or make my code fit into the box (which probably will look pretty stupid with those lines consisting of a few words)?

enter image description here

Best Answer

As mentioned by Torbjørn T. minted has some options to add a frame. However minted doesn't do the frame. This is done by the package fancyvrb. The possibilities of frames with fancyvrb are limited. If you want e.g. round corners you can use another package like tcolorbox or mdframed.

The example below shows you the usage of tcolorbox. I am using the advantages of etoolbox to add a frame around every minted environment.

\documentclass[a5paper]{article}
\usepackage[]{minted}
\usepackage{tcolorbox}
\usepackage{etoolbox}
\BeforeBeginEnvironment{minted}{\begin{tcolorbox}}%
\AfterEndEnvironment{minted}{\end{tcolorbox}}%
\begin{document}
\begin{minted}{latex}
%Preamble
\usepackage[]{minted}
\usepackage{tcolorbox}
\usepackage{etoolbox}
\BeforeBeginEnvironment{minted}%
     {\begin{tcolorbox}}%
\AfterEndEnvironment{minted}
   {\end{tcolorbox}}%
\end{minted}
\end{document}

enter image description here


EDIT: If you want to have automatic line breaks with fancyvrb you can use the following. Please note line breaks only occur at spaces. A line break has pre hook in form of $\rightarrow$. You can change it, of course. To get the symbol in front of a line I used lineno.

\documentclass[a5paper]{article}
%\pagestyle{empty}
\usepackage[T1]{fontenc}
\usepackage[]{minted}
\usepackage{tcolorbox}
\usepackage{lineno}
\def\gobble#1{}
\renewcommand\DeleteFile[1]{}
\usepackage{xparse}
\ExplSyntaxOn
\box_new:N \l_fvrb_box
\tl_new:N \l_fvrb_tl

\RenewDocumentCommand \FancyVerbFormatLine { m }
 {
   \hbox_set:Nn \l_fvrb_box { #1 }
    \dim_compare:nNnTF { \box_wd:N \l_fvrb_box }>{ \linewidth }
      {%box to big 
       \tl_set:Nn \l_fvrb_tl { #1 }
       \fvrb_use_tl:N \l_fvrb_tl
      } 
      {%box fits
       \box_use:N \l_fvrb_box
      }
 }

\cs_new:Npn \fvrb_use_tl:N  #1
 {
  \group_begin:
   \null\hfill\vbox_set:Nn \l_fvrb_box
     {\hsize=\linewidth
      \renewcommand\thelinenumber
           {
             \ifnum\value{linenumber}=1\relax\else
                  $\rightarrow$
             \fi
           }
      \begin{internallinenumbers}
        \advance\hsize by -2em
        \hspace*{-2em}\tl_use:N #1
      \end{internallinenumbers}
     }
   \box_use:N \l_fvrb_box
  \group_end:
}

\ExplSyntaxOff


\usepackage{etoolbox}
\BeforeBeginEnvironment{minted}{\begin{tcolorbox}}%
\AfterEndEnvironment{minted}{\end{tcolorbox}}%
\begin{document}
\begin{minted}{latex}
%Preamble
\usepackage[]{minted}
\usepackage[fancyvrb=true]{listings}
\usepackage{tcolorbox}
\usepackage{etoolbox}
\BeforeBeginEnvironment {minted} { \begin{tcolorbox} } \AfterEndEnvironment {minted} { \end{tcolorbox} }%
\usepackage{lipsum}
\end{minted}
\end{document}

enter image description here