6

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.

9

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}
  • Thanks for this great answer! I already started reworking the example of your initial post to get the required style, but I guess you beat me to it :) I'll post what I got anyway, in case somone finds that approach useful. but just to be clear: your edited post is exactly what I was looking for! I'll accept it after I post my solution to avoid closing the topic or something. – user3499027 Apr 28 '15 at 11:58
2

This answer is based on Ignasi's initial post. I started refining it only to see that he had already edited his answer to be exactly what I need. Still, for what it's worth, and hoping that someone will benefit from it, here is my approach so far. It requires a newer version of tcolorbox than the one that shipped with texlive in the repos, though.

This is what I came up with after working on his first example:

\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][]{minted language=bash,
    enhanced, listing engine=minted,
    listing only, #1, title=#2, 
    colback=mintedbackground, colbacktitle=mintedbackground, coltitle=darkgray, 
    arc=0pt,outer arc=0pt, 
    boxrule=0mm, 
    toptitle=1mm, bottomtitle=1mm,
    titlerule=1pt, colframe=gray, titlerule style={mintedbackground, dashed}    
}

\begin{document}

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

\end{document}

Which gives:

enter image description here

0

If you allow an answer using a different package, personally I am using listings to good effect.

\documentclass{report}

\usepackage{listings}

\begin{document}

\begin{lstlisting}[title=/files/location/on/system.sh,language=bash,numbers=left]
#!/bin/bash
echo "I am a bash file"
\end{listing}

\end{document}

I haven't tried minted yet; I liked listings for how easy it was to create a) syntax highlighting for a domain-specific language I was using, b) using custom highlighting to render a specific part of code differently. Lots of rendering / style options as well.

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.