[Tex/LaTex] File icon (folded corner rectangle) frame around text

framed

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.

Best Answer

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)

Related Question