如何在我的列表代码下添加图片?

如何在我的列表代码下添加图片?

我想把列表代码放在一个灰色容器里面,类似下图:

在此处输入图片描述

为此,我使用了以下方法

 \documentclass[svgnames]{report}
 \usepackage{tikz}
 \usepackage{verbatim}
 \usepackage{kpfonts}
 \usepackage[explicit]{titlesec}
 \usetikzlibrary{calc}
 \usetikzlibrary{shadows}
 \usetikzlibrary{shadows.blur}
 \usetikzlibrary{matrix}
 \usetikzlibrary{shapes,snakes}
 \usetikzlibrary{shapes.geometric, arrows}
 \usepackage{listings}

 \definecolor{codegreen}{rgb}{0,0,0}
 \definecolor{codegray}{rgb}{0,0,0}
 \definecolor{codepurple}{rgb}{0,0,0}
 %\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
 \definecolor{backcolour}{rgb}{1,1,1}
 \lstdefinestyle{mystyle}{
 backgroundcolor=\color{backcolour},   
 commentstyle=\color{codegreen},
 numberstyle=\tiny\color{codegray},
 stringstyle=\color{codepurple},
 basicstyle=\normalsize\ttfamily,
 breakatwhitespace=false,         
 breaklines=true,                 
 captionpos=b,                    
 keepspaces=true,                 
 numbers=none,                    
 numbersep=5pt,                  
 showspaces=false,                
 showstringspaces=false,
 showtabs=false,                  
 tabsize=4, 
 lineskip=.1cm
}

 \lstset{style=mystyle}

 \begin{document}
      \tikzstyle{Container} = [draw=none, fill=gray, thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
      \tikzstyle{ProgramBox} = [draw=black, fill=white,thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt,drop    shadow={color=black}
      ]

     \tikzstyle{ExampleBox} = [draw=black, fill=white, thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt,drop     shadow={color=black}]

\begin{center}  
        \begin{tikzpicture}
        \label{num}
        \node [Container](container){   
                    \begin{tikzpicture}
                    \node [ProgramBox] (pbox){%
                        \begin{minipage}{1\textwidth}
                        \hrule width \hsize height 1pt


                        \lstinputlisting[language=Java]{FristProgram.java}
                        \hrule width \hsize \kern 1mm \hrule width \hsize height 1pt

                        \end{minipage}      
                    };      
                    \end{tikzpicture}
        };
        \node [ExampleBox, left = -150pt] at (container.south west) {
                \begin{minipage}{0.3\textwidth}
                    this\\
                    is \\
                    the \\
                    first \\
                    example                     
                \end{minipage}
        };  

        \node [ExampleBox,right = -150pt] at (container.south east) {
                \begin{minipage}{0.3\textwidth}
                    this\\
                    is \\
                    the \\
                    second \\
                    example \\
                    which \\
                    is \\
                    larger\\
                    than \\
                    first\\
                    example                 
                \end{minipage}
            };
        \end{tikzpicture}%

        \end{center}

   \end{document}

此代码生成以下输出: 在此处输入图片描述

从图中可以看出,示例的位置不正确。我该如何解决这个问题呢?

答案1

只是一个概念(因为你没有提供文件FristProgram.java):

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{arrows,
                calc,
                positioning,
                shadows}
\usepackage{listings}
\usepackage{graphicx}

\begin{document}
\begin{center}
    \begin{tikzpicture}[
   node distance = 8pt and 24pt,
      box/.style = {rectangle, rounded corners, draw, thick, fill=white,
                    text width=#1, inner xsep=10pt, inner ysep=20pt,
                    drop shadow={color=black}
container/.style = {rectangle, rounded corners, fill=gray, 
                    text width=\linewidth, inner xsep=10pt, inner ysep=20pt}
                        ]
%\label{num}
\node [box=0.95\linewidth,align=center] (list)
    {
    \includegraphics[width=0.9\linewidth]{example-image-duck}%\lstinputlisting[language=Java]{FristProgram.java}
    };
\scoped[on background layer]
    \node [container, fit=(list)] {};
%\label{num}
\node [box=\linewidth,align=center] (container)
    {
    \includegraphics[width=0.9\linewidth]{example-image-duck}%\lstinputlisting[language=Java]{FristProgram.java}
    };
\node [box=0.3\linewidth, below right=of container.south west]
    {this\\
     is \\
     the \\
     first \\
     example};
\node [box=0.3\linewidth,below left=of container.south east]
    {this\\
     is \\
     the \\
     second \\
     example \\
     which \\
     is \\
     larger\\
     than \\
     first\\
     example};
     \end{tikzpicture}%
\end{center}
\end{document}

在此处输入图片描述

笔记:

  • tikzpicture不建议嵌套节点
  • 对于灰色背景,我建议使用库fit并将其绘制为背景中的节点
  • 如果你在节点中定义文本宽度,则不需要使用minipage
  • 用于节点定位的库positioning
  • 节点样式被定义为tikzpicture使用过时的\tikzstyle

编辑:哎呀,我忘记了灰色背景......现在已经添加了。

答案2

您应该使用anchor=来指定放置 的点,\nodes而不是left = -150pt。如果您希望与边框重叠,可以通过添加 来调整y shift=

在此处输入图片描述

笔记:

  • showframe包用于显示页面布局。

  • 需要调整的宽度minipage以适应阴影inner sep

      \textwidth-40pt
    

    似乎有效。

代码:

\begin{filecontents*}{FristProgram.java}
    import java.io.*;
    class ShowFile
    {
    }
\end{filecontents*}

\documentclass[svgnames]{report}
 \usepackage{tikz}
 \usepackage{verbatim}
 \usepackage{kpfonts}
 \usepackage[explicit]{titlesec}
 \usetikzlibrary{calc}
 \usetikzlibrary{shadows}
 \usetikzlibrary{shadows.blur}
 \usetikzlibrary{matrix}
 \usetikzlibrary{shapes,snakes}
 \usetikzlibrary{shapes.geometric, arrows}
 \usepackage{listings}
 \usepackage{showframe}

 \definecolor{codegreen}{rgb}{0,0,0}
 \definecolor{codegray}{rgb}{0,0,0}
 \definecolor{codepurple}{rgb}{0,0,0}
 %\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
 \definecolor{backcolour}{rgb}{1,1,1}
 \lstdefinestyle{mystyle}{
 backgroundcolor=\color{backcolour},   
 commentstyle=\color{codegreen},
 numberstyle=\tiny\color{codegray},
 stringstyle=\color{codepurple},
 basicstyle=\normalsize\ttfamily,
 breakatwhitespace=false,         
 breaklines=true,                 
 captionpos=b,                    
 keepspaces=true,                 
 numbers=none,                    
 numbersep=5pt,                  
 showspaces=false,                
 showstringspaces=false,
 showtabs=false,                  
 tabsize=4, 
 lineskip=.1cm
}

 \lstset{style=mystyle}

 \begin{document}
      \tikzstyle{Container} = [draw=none, fill=gray, thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
      \tikzstyle{ProgramBox} = [draw=black, fill=white,thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt,drop    shadow={color=black}
      ]

     \tikzstyle{ExampleBox} = [draw=black, fill=white, thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt,drop     shadow={color=black}]

{\centering  
        \begin{tikzpicture}
        \label{num}
        \node [Container](container){   
                    \begin{tikzpicture}
                    \node [ProgramBox] (pbox){%
                        \begin{minipage}{\dimexpr\textwidth-40pt\relax}
                        \hrule width \hsize height 1pt


                        \lstinputlisting[language=Java]{FristProgram.java}
                        \hrule width \hsize \kern 1mm \hrule width \hsize height 1pt

                        \end{minipage}      
                    };      
                    \end{tikzpicture}
        };
        \node [ExampleBox, anchor=north west] at (container.south west) {
                \begin{minipage}{0.3\textwidth}
                    this\\
                    is \\
                    the \\
                    first \\
                    example                     
                \end{minipage}
        };  

        \node [ExampleBox, anchor=north east] at (container.south east) {
                \begin{minipage}{0.3\textwidth}
                    this\\
                    is \\
                    the \\
                    second \\
                    example \\
                    which \\
                    is \\
                    larger\\
                    than \\
                    first\\
                    example                 
                \end{minipage}
            };
        \end{tikzpicture}%
\par}

   \end{document}

答案3

我已将您的代码改为使用tikzsettikzstyle请参阅应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?) 和层fit上的节点background以避免使用嵌套tikzpictures。使用positioning库可以轻松控制相对于其他节点放置节点的位置。

\documentclass[svgnames]{report}
 \usepackage{tikz}
 \usepackage{verbatim}
 \usepackage{kpfonts}
 \usepackage[explicit]{titlesec}
 \usetikzlibrary{calc}
 \usetikzlibrary{shadows}
 \usetikzlibrary{shadows.blur}
 \usetikzlibrary{matrix}
 \usetikzlibrary{shapes,snakes}
 \usetikzlibrary{shapes.geometric, arrows}
 \usetikzlibrary{backgrounds, fit, positioning}
 \usepackage{listings}

 \definecolor{codegreen}{rgb}{0,0,0}
 \definecolor{codegray}{rgb}{0,0,0}
 \definecolor{codepurple}{rgb}{0,0,0}
 %\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
 \definecolor{backcolour}{rgb}{1,1,1}
 \lstdefinestyle{mystyle}{
 backgroundcolor=\color{backcolour},   
 commentstyle=\color{codegreen},
 numberstyle=\tiny\color{codegray},
 stringstyle=\color{codepurple},
 basicstyle=\normalsize\ttfamily,
 breakatwhitespace=false,         
 breaklines=true,                 
 captionpos=b,                    
 keepspaces=true,                 
 numbers=none,                    
 numbersep=5pt,                  
 showspaces=false,                
 showstringspaces=false,
 showtabs=false,                  
 tabsize=4, 
 lineskip=.1cm
}

 \lstset{style=mystyle}

\tikzset{
    Container/.style = {draw=none, fill=gray, thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt},
   ProgramBox/.style = {draw=black, fill=white,thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt,drop    shadow={color=black}},
    ExampleBox/.style = {draw=black, fill=white, thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt,drop     shadow={color=black}},
      }


 \begin{document}

\begin{center}  
        \begin{tikzpicture}
                    \node [ProgramBox] (pbox){%
                        \begin{minipage}{\textwidth}
                        \hrule width \hsize height 1pt
                        \lstinputlisting[language=Java]{FirstProgram.java}
                        \hrule width \hsize \kern 1mm \hrule width \hsize height 1pt
                        \end{minipage}      
                    };      
        \begin{scope}[on background layer]
        \node[Container, fit=(pbox)] (container) {};
        \end{scope}

        \node [ExampleBox, below right=3mm and 1cm of pbox.south west] {
                \begin{minipage}{0.3\textwidth}
                    this\\
                    is \\
                    the \\
                    first \\
                    example                     
                \end{minipage}
        };  

        \node [ExampleBox, below left = 3mm and 1cm of pbox.south east] {
                \begin{minipage}{0.3\textwidth}
                    this\\
                    is \\
                    the \\
                    second \\
                    example \\
                    which \\
                    is \\
                    larger\\
                    than \\
                    first\\
                    example                 
                \end{minipage}
            };
        \end{tikzpicture}%

        \end{center}

   \end{document}

在此处输入图片描述

相关内容