Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to present code listings with a "file icon box". Something like this: source code with "file icon box"

I'm aware of listings package allowing rectangle frames. My interest is in the specific shape of the frame. I think it is not specific to source code. If it does not exist off-the-shelf, I'm not afraid of some coding to achieve this.

share|improve this question
4  
I would define my own listings environment (\lstnewenvironment) which draws the box using TikZ around the listing. –  Martin Scharrer Aug 5 '11 at 10:33
add comment

1 Answer

up vote 2 down vote accepted

I would define my own listings environment (\lstnewenvironment) which draws the box using TikZ around the listing:

\documentclass{article}

\usepackage{listings}
\usepackage{tikz}

\newlength{\cornerlength}
\setlength{\cornerlength}{1cm}

\tikzset{listingborder/.style={thick}}

\makeatletter
\lstnewenvironment{mylisting}[1][]{%
    \lstset{#1}%
    \setbox0\hbox\bgroup\color@setgroup
}{%
    \color@endgroup\egroup
    \begin{tikzpicture}
    \node (L) [text width=\linewidth] {%
        \usebox{0}%
    };
    \draw [listingborder]
           (L.north west)
        -- ([xshift=-\cornerlength]L.north east)
        -- ([yshift=-\cornerlength]L.north east)
        -- (L.south east)
        -- (L.south west)
        -- cycle;
    \draw [listingborder]
           ([yshift=-\cornerlength]L.north east)
        -| ([xshift=-\cornerlength]L.north east);
    \end{tikzpicture}%
}
\makeatother

\begin{document}

\begin{mylisting}[language=tex,basicstyle=\ttfamily]
\lstnewenvironment{mylisting}[1][]{%
    \lstset{#1}%
    \setbox0\hbox\bgroup\color@setgroup
}{%
    \color@endgroup\egroup
    \begin{tikzpicture}
    \node (L) [text width=\linewidth] {%
        \usebox{0}%
    };
    \draw [listingborder]
           (L.north west)
        -- ([xshift=-\cornerlength]L.north east)
        -- ([yshift=-\cornerlength]L.north east)
        -- (L.south east)
        -- (L.south west)
        -- cycle;
    \draw [listingborder]
           ([yshift=-\cornerlength]L.north east)
        -| ([xshift=-\cornerlength]L.north east);
    \end{tikzpicture}%
}
\end{mylisting}

\end{document}

Result

You still need to add the waved line at the bottom. Also note that usually you can wrap the node around the environment content be replacing \node {..}; with \node \bgroup and \egroup;, however, this didn't worked for the listings environment. Most likely some of the verbatim trickery causes issues. (This is funny because verbatim should work inside a TikZ node)

share|improve this answer
    
Will this break across pages nicely? –  Seamus Aug 5 '11 at 10:59
    
@Seamus: No, that wasn't requested. If this is required I would use the code in my answer to your question Test if a paragraph has a page break in it?. The different layout can easily be added. –  Martin Scharrer Aug 5 '11 at 11:04
    
yes that was a somewhat cheecky question. But since the normal listings frame environment does break across pages, I wonder if you could "piggy back" on that by just, say, adding a triangle to the top right corner of that, rather than drawing the box yourself... –  Seamus Aug 5 '11 at 11:11
3  
thank you very much, it's my first time on tex.stackexchange and I'm delighted and impressed of the answer (as of response time!) –  FabienAndre Aug 5 '11 at 12:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.