如何在表格中嵌入列表?

如何在表格中嵌入列表?

如何在表格中嵌入源代码列表而不弄乱间距?

以下是该问题的 MWE:

\documentclass{article}
\thispagestyle{empty}
\usepackage{listings}

\begin{document}

\begin{table*}[h]
  %\lstset{numbers=none, language={[x86masm]Assembler}, basicstyle=\linespread{0.8}\footnotesize}
  \begin{tabular}{l|p{6cm}|l}
    Fact name
    & Description
    & Assembly code example\\
    \hline
    hello & Instruction  in  allocates  bytes of memory for the object blah blah blah.
    &
\begin{lstlisting}
push 28h
call operator new
\end{lstlisting}%
  \end{tabular}
  \caption{Attempt one.}
\end{table*}
\begin{table*}[h]
  %\lstset{numbers=none, language={[x86masm]Assembler}, basicstyle=\linespread{0.8}\footnotesize}
  \begin{tabular}{l|p{6cm}|p{6cm}}
    Fact name
    & Description
    & Assembly code example\\
    \hline
    hello & Instruction  in  allocates  bytes of memory for the object blah blah blah.
    &
\begin{lstlisting}
push 28h
call operator new
\end{lstlisting}\\%
  \end{tabular}
  \caption{Attempt two.}
\end{table*}

\end{document}

渲染的 MWE

答案1

您可以定义\lstnewenvironment针对具体情况进行调整。

这里的选项\lstset也可以作为选项给出,tabularlstlisting或者使用

\begin{tabularlstlisting}[<options>]

完整示例:

\documentclass{article}
\thispagestyle{empty}
\usepackage{listings}

\lstnewenvironment{tabularlstlisting}[1][]
 {%
  \lstset{aboveskip=-1.3ex,belowskip=-3.5ex,#1}%
 }
 {}

\begin{document}

\lstset{numbers=none, language={[x86masm]Assembler}, basicstyle=\linespread{0.8}\footnotesize}

\begin{tabular}{l|p{6cm}|p{4cm}}
Fact name
 & Description
 & Assembly code example\\
\hline
hello & Instruction  in  allocates  bytes of memory for the object blah blah blah. &
\begin{tabularlstlisting}
push 28h
call operator new
\end{tabularlstlisting}
\\
\hline
\end{tabular}

\bigskip

\begin{tabular}{l|p{6cm}|p{4cm}}
Fact name
 & Description
 & Assembly code example\\
\hline
hello & Instruction  in  allocates  bytes of memory for the object blah blah blah. &
\begin{tabularlstlisting}
push 28h
call operator new
push 28h
call operator new
push 28h
call operator new
push 28h
call operator new
\end{tabularlstlisting}
\\
\hline
\end{tabular}

\end{document}

在此处输入图片描述

答案2

您可以使用\vskip来调整位置。我发现\baselineskip+\smallskipamount结果不错。如添加的规则所示,以使事物可视化。

\documentclass{article}
\thispagestyle{empty}
\usepackage{listings}

\begin{document}

\begin{table*}[h]
  \lstset{numbers=none, language={[x86masm]Assembler},
    basicstyle=%\linespread{0.8}
               %\footnotesize
  }
  \begin{tabular}{l|p{6cm}|p{6cm}}
    Fact name
    & Description
    & Assembly code example\\
    \hline
    hello & Instruction\rlap{\rule{10cm}{0.4pt}}  in  allocates  bytes of memory for the object blah blah blah.
    &
\vskip-\baselineskip
\vskip-\smallskipamount
\begin{lstlisting}
push 28h
call operator new
\end{lstlisting}
\kern-\baselineskip
  \end{tabular}
  \caption{Attempt two.}
\end{table*}

\end{document}

在此处输入图片描述

答案3

具有相关值的解决方案(该键\raisebox 的默认值为):aboveskip\medskipamount

\documentclass{article}
\thispagestyle{empty}
\usepackage{listings}
\usepackage{array}

\begin{document}

\begin{table*}[!h]\setlength{\extrarowheight}{3pt}
  %\lstset{numbers=none, language={[x86masm]Assembler}, basicstyle=\linespread{0.8}\footnotesize}
\setlength{\tabcolsep}{4pt}
\centering
  \begin{tabular}{l|p{6cm}|>{\vspace*{-\dimexpr \baselineskip+0.55ex}\arraybackslash}l}
    Fact name
    & Description
    & Assembly code example\\[1ex]
    \hline
    hello & Instruction in allocates bytes of memory for the object blah blah blah.
    &
\raisebox{-\medskipamount}{
\hspace{-1em}\begin{lstlisting}^^J
push 28h^^J
call operator new
\end{lstlisting}}\\%
  \end{tabular}
  \caption{Attempt two.}
\end{table*}

\end{document}

在此处输入图片描述

答案4

您还可以使用tikz matrix

\documentclass{article}
\thispagestyle{empty}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{table*}[h]
    \centering
    \begin{tikzpicture}
    \matrix[matrix of nodes,
        column sep=0pt, row sep=0pt,
        column 1/.style={nodes={text width=2cm}},
        column 2/.style={nodes={text width=4.5cm}},
        column 3/.style={nodes={text width=4.5cm}},
        row 1/.style={nodes={minimum height=1ex, 
            text height=1.5ex,text depth=.25ex}},
        row 2/.style={nodes={minimum height=11ex, anchor=center}},
        ] (m) {%
            Fact name
            & Description
            & Assembly code example\\
            hello & Instruction  in  allocates  bytes of memory for the object blah blah blah.
            &
            % leave the code without space before
            \begin{lstlisting}
push 28h
call operator new
            \end{lstlisting}
            \\
        };
        \draw[thick] (m-1-1.south west) -- (m-1-3.south east); 
        \draw[thick] (m-1-1.north east) -- (m-2-1.south east);
        \draw[thick] (m-1-2.north east) -- (m-2-2.south east);
    \end{tikzpicture}
    \caption{Attempt one.}
\end{table*}
\end{document}

在此处输入图片描述

相关内容