列表列表中的代码周围的矩形(处理多个页面)

列表列表中的代码周围的矩形(处理多个页面)

我一直试图让它工作,但遇到了一个问题。

我使用 TikZ 在代码块周围绘制矩形 - 因此无需拆分,我lstlisting可以添加起始和结束,它会在该块周围绘制一个圆角矩形。我已附上我的简单代码。

最大的问题是,它无法跨多个页面工作 - 因此对于跨页面的较长代码列表,它会失败:(

我尝试过框架和不同版本,但总是失败。

\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

framed它可以与包一起完成tcolorboxtcolorbox据我所知,单独使用不会在分页符处关闭圆角矩形。

\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}

在此处输入图片描述

答案2

您应该使用 \lstset 和 lstlistings 来排版源代码。

像这样:

\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}

相关内容