30

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

  • 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}. – Marco Daniel May 5 '13 at 11:05
  • How do you want to handle captions? – Marco Daniel May 5 '13 at 11:05
  • Thanks for embedding the image. And I don't really want caption, just a box around the code. – Argo May 5 '13 at 11:31
  • Please see my edit. – Marco Daniel May 6 '13 at 17:20
28

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

  • 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? – Manuel Rauber Aug 12 '13 at 9:11
  • How do you also add a title just on top of the frame, as usual in tcolorbox? – skan Apr 30 at 15:42
15

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

  • 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? – Jodi Schneider Aug 31 '13 at 9:04
  • 1
    @JodiSchneider Looks like you're right, sorry about that. Fixed. – Torbjørn T. Aug 31 '13 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 '17 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. – Torbjørn T. Apr 16 '17 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 '17 at 11:44

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.