33

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

4
  • 1
    Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. May 5, 2013 at 11:05
  • How do you want to handle captions? May 5, 2013 at 11:05
  • Thanks for embedding the image. And I don't really want caption, just a box around the code.
    – Argo
    May 5, 2013 at 11:31
  • Please see my edit. May 6, 2013 at 17:20

3 Answers 3

33

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

3
  • 4
    This solution is nice but it has a bug: github.com/ManuelRauber/PublicImages/blob/master/… When the code line is intended and gets breaked the "first" line will get an arrow, too. Just look at the picture with the ´CSV.foreach`-line. Is there any chance that you can fix this? Aug 12, 2013 at 9:11
  • How do you also add a title just on top of the frame, as usual in tcolorbox?
    – skan
    Apr 30, 2019 at 15:42
  • How would I need to adjust this for inputminted? Feb 25, 2022 at 5:54
22

Referring to the manual, page 9, you can add frame=single to the options for the minted environment. There are different types of frames, for example lines, which only places lines above and below the code. The thickness can be specified by framerule=<length>, and the distance between frame and content can be set with framesep=<length>.

You can also define a custom environment using \newminted{<language>}{<options>}.

\documentclass{article}
\usepackage{minted}
\newminted{python}{frame=lines,framerule=2pt}

\begin{document}
Adding a frame for a single code listing:
\begin{minted}[frame=single,framesep=10pt]{python}
import antigravity
print 'Hello world'
\end{minted}

Using the custom listing:
\begin{pythoncode*}{}
import antigravity
print 'Hello world'
\end{pythoncode*}

\end{document}

enter image description here

5
  • This is misleading: I think this picture corresponds to \newminted{python}{frame=lines,framerule=2pt} When I run your MWE the custom listing has a single frame (i.e. left and right bars). Could you please correct the code? Aug 31, 2013 at 9:04
  • 1
    @JodiSchneider Looks like you're right, sorry about that. Fixed. Aug 31, 2013 at 11:15
  • I get an error for your second example \begin{pythoncode} import antigravity print 'Hello world' \end{pythoncode} works instead
    – orestisf
    Apr 16, 2017 at 10:41
  • @orestisf Which error? I do not get an error with the code in my answer. Note that the starred version of custom minted environments have a mandatory argument, whereas the unstarred version (that you used) does not have a mandatory argument. Apr 16, 2017 at 10:46
  • @TorbjørnT this is weird, I swear I used your example in my code and there was an error but I can't reproduce it right now. Probably made a mistake somewhere before. Sorry for bothering you.
    – orestisf
    Apr 16, 2017 at 11:44
1

If you want to use \inputminted you can put the following in your preamble to get the output of Marco Daniels answer:

\usepackage[]{minted}
\usepackage{xpatch}
\xpretocmd{\inputminted}{\begin{tcolorbox}}{}{}%
\xapptocmd{\inputminted}{\end{tcolorbox}}{}{}%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .