1

I am using the minted package to insert code listings for different programming languages in my document. Here is a MWE:

\documentclass{article}

\usepackage{minted}
\usepackage{color}

\definecolor{bg}{rgb}{0.95,0.95,0.95}
\newminted{python}{autogobble, bgcolor=bg}
%... other minted environments for other languages

\begin{document}

\begin{listing}[htb]
  \begin{pythoncode}
    def myClass:
      pass
  \end{pythoncode}
  \caption{Some Python}
  \label{lst:python}
\end{listing}

\end{document}

Now, what I would like to do is specify for each listing what programming language is being used in in. I was thinking about putting a box around the listing, with a header in which I can specify the language. I would like this language label to be on the right end of the header.

Are there any packages that can help with this? How could I do this?

2

The documentation of minted says for bgcolor key:

Background color of the listing. Be aware that this option has several limitations (described below); see “Framing alternatives” below for more powerful alternatives.

In section "Framing alternatives", you have:

If you want more reliable and advanced options for background colors and framing, you should consider a more advanced framing package such as mdframed or tcolorbox.

Below you'll find two boxes with tcolorbox and standard minted (more accurate: fancyvrb) possibilities: You see the limitations of standard solution where background color box is larger than the frame. Code:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{xcolor}
\definecolor{bg}{rgb}{0.95,0.95,0.95}

\usepackage[newfloat]{minted}
\newminted{python}{%
  autogobble                               ,
  bgcolor       = bg                       ,
  frame         = single                   ,
  framerule     = 1mm                      ,
  framesep      = 3mm                      ,
  label         = \textrm{\textbf{python}} ,
  labelposition = topline
}

\usepackage{tcolorbox}
\tcbuselibrary{minted}

\newtcblisting{tcbpythoncode}[1][python]{%
  colback         = yellow!5        ,
  colframe        = yellow!50!black ,
  listing only                      ,
  title           = #1              ,
  halign title    = right           ,
  fonttitle       = \bfseries       ,
  listing engine  = minted          ,
  minted language = python
}

\begin{document}

\begin{tcbpythoncode}
def myClass:
  pass
\end{tcbpythoncode}

\begin{listing}
\begin{pythoncode}
def myClass:
  pass
\end{pythoncode}
\end{listing}

\end{document}

enter image description here

|improve this answer|||||

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.