在表格中并排显示两个列表

在表格中并排显示两个列表

我想知道是否有可能在一个表中并排显示两个不同的列表?这是我的代码,只显示一个监听,但我如何将其嵌入到一个表和另一个表中?

编辑:即使我尝试了下面提到的链接,我在编译文件时也遇到了一些错误。我还发布了我编辑的 LaTeX 代码。

    \documentclass[english,a4paper,12pt]{report}
\usepackage{mathpazo}
\usepackage{graphicx}
\renewcommand{\sfdefault}{lmss}
\usepackage{courier}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}

\begin{document}
\begin{tabular}{|l|l|}

\begin{minipage}{.48\textwidth}
\begin{lstlisting}[
basicstyle={\scriptsize\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
        x.thread.start(0);
        x.thread.join(0);
    }
}
\end{lstlisting}
\end{minipage}

&

\begin{minipage}{.48\textwidth}
\begin{lstlisting}[
basicstyle={\scriptsize\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
        x.thread.start(0);
        x.thread.join(0);
    }
}
\end{lstlisting}
\end{minipage}
\\

\end{tabular}
\end{document}

我想展示的是: enter image description here

答案1

按照 David 所说的去做。在本例中,我将结果放在 中tabular。我还将列表放在 中\tiny,以便使两个并排的列表适合边距宽度。

就我而言,我还事先将它们放在盒子里,以便我的tabular代码看起来美观:

\begin{tabular}{|c|c|}
\hline
\stackinset{l}{-5pt}{t}{13\llength}{$\bullet$}{\listingA} &
\stackinset{l}{-5pt}{t}{ 7\llength}{$\bullet$}{\listingB} \\
\hline
\end{tabular}

编辑以在每个列表中添加小黑圈。在这种情况下,我将它们叠加在带有 的列表框上\stackinset,定义 的长度\llength以对应于小列表中的一行间距。因此,像这样的命令

\stackinset{l}{-5pt}{t}{13\llength}{$\bullet$}{\listingA}

指示将项目符号放在第一个列表的第 13 行以下。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{stackengine}
\newlength\llength
\llength=1.38ex\relax
\begin{document}

\clearpage

\setbox0=\hbox{%
\begin{minipage}{1.9in}
\begin{lstlisting}[
basicstyle={\tiny\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
    }
}
\end{lstlisting}
\end{minipage}
}
\savestack{\listingA}{\box0}

\setbox0=\hbox{%
\begin{minipage}{1.9in}
\begin{lstlisting}[
basicstyle={\tiny\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
    }
}
\end{lstlisting}
\end{minipage}
}
\savestack{\listingB}{\box0}

\begin{tabular}{|c|c|}
\hline
\stackinset{l}{-5pt}{t}{13\llength}{$\bullet$}{\listingA} &
\stackinset{l}{-5pt}{t}{ 7\llength}{$\bullet$}{\listingB} \\
\hline
\end{tabular}

\end{document}

enter image description here

答案2

您的示例由于color未加载而生成错误,但除此之外它基本上可以正常工作。您不需要tabular两个“minipages”就足够了。您的某些行太长,无法并排显示,但这是另一个问题。

enter image description here

\documentclass[english,a4paper,12pt]{report}
\usepackage{mathpazo}
\usepackage{graphicx}
\renewcommand{\sfdefault}{lmss}
\usepackage{courier}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{color}%<<<<<<<<<<<<<<<

\begin{document}
%\begin{tabular}{|l|l|}

\begin{minipage}{.48\textwidth}
\begin{lstlisting}[
basicstyle={\scriptsize\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
        x.thread.start(0);
        x.thread.join(0);
    }
}
\end{lstlisting}
\end{minipage}%%
\hfill
%&
%
\begin{minipage}{.48\textwidth}
\begin{lstlisting}[
basicstyle={\scriptsize\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
        x.thread.start(0);
        x.thread.join(0);
    }
}
\end{lstlisting}
\end{minipage}
%\\

%\end{tabular}
\end{document}

相关内容