2

I've been trying to get this working, but have hit an issue.

I was using TikZ to draw rectangles around blocks of code - so without splitting up my lstlisting I could add start-end and it would draw a rounded rectangle around that block. I've included my simple code.

BIG problem is, it won't work across multiple pages - so for longer code listings that go across pages, it fails :(

I've tried frames and different version, but always failing.

\documentclass[11pt]{article}
\usepackage{listingsutf8}
\usepackage{tikz}

\usetikzlibrary{decorations.text}

\newcommand\myboxs[1]%
{
    \tikz[remember picture,overlay]\node (mk1) {};
}

\newcommand\myboxe[1]%
{
    \tikz[remember picture,overlay]\node (mk2) {};

    \begin{tikzpicture}[remember picture,overlay]
    \draw[red,ultra thick,rounded corners] ([shift={(-10pt,2ex)}] mk1) rectangle ([shift={(\textwidth,-0.65ex)}] mk2);
    \end{tikzpicture}
}



\begin{document}


    \begin{lstlisting}[language=c,escapeinside={(*@}{@*)}]
    #include <stdio.h>

    #include <iostream>

    int test()
    {
    std::cout << "Hello World!";
    }

    (*@ \myboxs{}; @*)
    int main (void)
    {
        int value1, value2, sum;//declaring
        value1 = 50;//assign values
        value2 = 25;
        sum = value1 + value2;//sum them
        //output

        printf ("%d (*@  @*)+%d \n");


        while (true)
        {
            printf(..)
        }
    (*@ \myboxe{}; @*)


    int etc()
    {
    std::cout << "Hello World!";
    }

    int another()
    {
    std::cout << "Hello World!";
    }


    \end{lstlisting}

\end{document}
1

2 Answers 2

2

It can be done with the framed package together with tcolorbox. tcolorbox alone will not close the rounded rectangles at a page break, as far as I know.

\documentclass[a4paper, 12pt, twoside]{book}

\usepackage{sourcecodepro}
\usepackage{listingsutf8}

\usepackage{framed}
\usepackage{tcolorbox}
\newenvironment{roundedframe}{%
  \def\FrameCommand{\tcbox[arc=5mm,colframe=red]}%
  \MakeFramed {\advance\hsize-\width \FrameRestore}}%
 {\endMakeFramed}

\lstnewenvironment{clisting}
    {\lstset{language=c,escapeinside={(*@}{@*)}}}
    {}

\lstnewenvironment{boxedlisting}
    {\lstset{language=c,escapeinside={(*@}{@*)}}}
    {}

\usepackage{etoolbox}
\BeforeBeginEnvironment{boxedlisting}{\begin{roundedframe}}
\AfterEndEnvironment{boxedlisting}{\end{roundedframe}}

\usepackage{lipsum}
\begin{document}
\lipsum[1-2] % to force a page break

\begin{clisting}
 #include <stdio.h>

 #include <iostream>

 int test()
 {
 std::cout << "Hello World!";
 }
\end{clisting}
\begin{boxedlisting}
 int main (void)
 {
     int value1, value2, sum;//declaring
     value1 = 50;//assign values
     value2 = 25;
     sum = value1 + value2;//sum them
     //output

     printf ("%d (*@  @*)+%d \n");


     while (true)
     {
         printf(..)
     }
 }
\end{boxedlisting}
\begin{clisting}

 int etc()
 {
 std::cout << "Hello World!";
 }

 int another()
 {
 std::cout << "Hello World!";
 }

\end{clisting}
\end{document}

enter image description here

9
  • Thanks for your reply, I didn't want to `frame' the whole listing- just insert an overlay on regions (e.g., drawing rounded rectangle around the main function, and other functions).
    – Alan
    Commented Jan 2, 2017 at 0:28
  • \begin{lstlisting}[language=c,escapeinside={(@}{@)}] dummy verbatim text (@ \begin{framed} @) <-- overlapping rectangle starts here dummy verbatim text (@ \end{framed} @) <---ends here dummy verbatim text dummy verbatim text \end{lstlisting}
    – Alan
    Commented Jan 2, 2017 at 0:33
  • Oh, I see. Shall I remove my answer? Commented Jan 2, 2017 at 0:37
  • It's up to you Piet, I've been searching online and trying all different methods to get something working - starting to think it can't be done :(
    – Alan
    Commented Jan 2, 2017 at 0:42
  • So if there is a pagebreak in the boxed part do you want each part to be in a rounded rectangle? Commented Jan 2, 2017 at 0:58
0

You should use \lstset with lstlistings for typesetting source code.

Like this:

\documentclass[11pt]{article}
\usepackage{listingsutf8}
\usepackage{color}


\lstdefinestyle{Cstyle}{
    basicstyle=\ttfamily,
    keywordstyle=\color{blue}\ttfamily,
    stringstyle=\color{red}\ttfamily,
    commentstyle=\color{green}\ttfamily,
    morecomment=[l][\color{magenta}]{\#},
    rulecolor= \color{red}, %red frame
    frame=single, %single line frame
    framerule=3pt, %thick frame
    numbers=left
}

\begin{document}

    \lstset{style=Cstyle}
        \begin{lstlisting}[language=C]
        #include <stdio.h>

        #include <iostream>

    int test()
    {
    std::cout << "Hello World!";
    }

    (*@ \myboxs{}; @*)
    int main (void)
    {
    int value1, value2, sum;//declaring
    value1 = 50;//assign values
    value2 = 25;
    sum = value1 + value2;//sum them
    //output

    printf ("%d (*@  @*)+%d \n");       

    while (true)
    {
    printf(..)
    }
    (*@ \myboxe{}; @*)      

    int etc()
    {
    std::cout << "Hello World!";
    }

    int another()
    {
    std::cout << "Hello World!";
    }
    %to make it multipage
    a
    a
    a
    a

    a
    a
    a
    a
    a

    a
    a
    a
    a

    a
    a
    \end{lstlisting}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .