[Tex/LaTex] How to show a filename above a code snippet just like on the arch wiki

code-snippetshighlighting

Context

I'm writing a tutorial-like document in LaTeX on how to create a certain Linux based device. Part of the effort of recreating the device is writing small pieces of code, in either Bash or Python, and editing config files.

All of these should be included in the document. To be able to recreate it, one should know the filename and the file contents, of course. I like the way the arch guys do it on their wiki, clean and simple:

Snippet from [arch wiki][1]

To exemplify, "/etc/systemd/system/unit.d/customexec.conf" is the filename, while the three lines below are the actual text in the file.

Finally, I want the code to be syntax-highlighted. I use minted for that. I also value having line numbers. It makes referencing them a lot easier.

What I have now

I've played around with several examples I found on the internet and the minted documentation. I still don't like what I have though. Here's a MWE:

\documentclass{report}

\usepackage[table,usenames,dvipsnames,hyperref]{xcolor}
\usepackage{listings}
\usepackage{caption}

\DeclareCaptionStyle{mystyle}%
[margin=2mm,justification=raggedright]%
{font=footnotesize,labelfont=sc,margin={100mm,10mm}}

\DeclareCaptionFormat{continued}{File#2#3}

\captionsetup{style=mystyle, indention=10cm, format=continued}

\usepackage[newfloat]{minted}
\SetupFloatingEnvironment{listing}{name=File}

\definecolor{mintedbackground}{rgb}{0.95,0.95,0.95}

% This is the bash profile used throughout the document.
% I've also got one for Python and console text (regular commands)
\newminted[bashcode]{bash}{
    bgcolor=mintedbackground,
    fontfamily=tt,
    linenos=true,
    numberblanklines=true,
    numbersep=12pt,
    numbersep=5pt,
    gobble=0,
    frame=leftline,
    framesep=2mm,
    funcnamehighlighting=true,
    tabsize=4,
    obeytabs=false,
    mathescape=false
    samepage=false,
    showspaces=false,
    showtabs =false,
    texcl=false,
    baselinestretch=1.2,
    fontsize=\footnotesize,
    breaklines=true,
}

\begin{document}

\begin{listing}[H]
\caption{/files/location/on/system.sh}
\begin{bashcode}
#!/bin/bash
echo "I am a bash file"
\end{bashcode}
\end{listing}

\end{document}

It outputs the following (cutout of the original output):

Result of the MWE above

As you can see in the MWE, I'm misusing the caption as a filename. I don't like this in general, but I guess that cleanly making the caption part of minted's background is also out of the question as the caption is outside the skope of the minted section.

Note: the MWE requires minted 2. My distro (Kubuntu 14.04) ships with an older version. I downloaded the .sty file here: https :// raw.githubusercontent.com/gpoore/minted/master/source/minted.sty (sorry, don't have enough points to include more than two links).

The question

Simply put, how do I get close to the arch wiki example?

I'm fine with a solution that doesn't make use of minted. The solution also doesn't need to be nice to the eye. I'm just now thinking of creating such snippets in an external HTML file and then including them. All fine with me as long as it remains workable.. The document is finished in a few weeks and I don't need to keep working on it, so having everything automated and integrated isn't very important.

Thanks in advance! Please let me know if I failed to include some info.

Best Answer

You can use tcolorbox package to configure your listings. It can work with minted. Next would be the more or less default output just customized to your minted style.

\documentclass{report}

\usepackage[skins,minted]{tcolorbox}

\definecolor{mintedbackground}{rgb}{0.95,0.95,0.95}

% This is the bash profile used throughout the document.
% I've also got one for Python and console text (regular commands)
\setminted[bash]{
    bgcolor=mintedbackground,
    fontfamily=tt,
    linenos=true,
    numberblanklines=true,
    numbersep=12pt,
    numbersep=5pt,
    gobble=0,
    frame=leftline,
    framesep=2mm,
    funcnamehighlighting=true,
    tabsize=4,
    obeytabs=false,
    mathescape=false
    samepage=false,
    showspaces=false,
    showtabs =false,
    texcl=false,
    baselinestretch=1.2,
    fontsize=\footnotesize,
    breaklines=true,
}

\newtcblisting{myminted}[2][]{listing engine=minted, listing only,#1, title=#2, minted language=bash, colback=mintedbackground}
\begin{document}

\begin{myminted}{/files/location/on/system.sh}
#!/bin/bash
echo "I am a bash file"
\end{myminted}

\end{document}

enter image description here

Some color and size adjustments are missing, but the result is more similar to OP's proposed box

enter image description here

\documentclass{report}

\usepackage[skins,minted]{tcolorbox}

\definecolor{mintedbackground}{rgb}{0.95,0.95,0.95}
\definecolor{mintedframe}{rgb}{0.70,0.85,0.95}

% This is the bash profile used throughout the document.
% I've also got one for Python and console text (regular commands)
\setminted[bash]{
    bgcolor=mintedbackground,
    fontfamily=tt,
    linenos=true,
    numberblanklines=true,
    numbersep=12pt,
    numbersep=5pt,
    gobble=0,
    frame=leftline,
    framesep=2mm,
    funcnamehighlighting=true,
    tabsize=4,
    obeytabs=false,
    mathescape=false
    samepage=false,
    showspaces=false,
    showtabs =false,
    texcl=false,
    baselinestretch=1.2,
    fontsize=\footnotesize,
    breaklines=true,
}

\newtcblisting{myminted}[2][]{enhanced, listing engine=minted, 
listing only,#1, title=#2, minted language=bash, 
coltitle=mintedbackground!30!black, 
fonttitle=\ttfamily\footnotesize,
sharp corners, top=0mm, bottom=0mm,
title code={\path[draw=mintedframe,dashed, fill=mintedbackground](title.south west)--(title.south east);},
frame code={\path[draw=mintedframe, fill=mintedbackground](frame.south west) rectangle (frame.north east);}
}
\begin{document}

\begin{myminted}{/files/location/on/system.sh}
#!/bin/bash
echo "I am a bash file"
\end{myminted}

\end{document}