How do I embed a listing in a tabular?
You can define a \lstnewenvironment
with the adjustments for the specific case.
The option here in \lstset
can also be given as options to tabularlstlisting
or changed locally with
\begin{tabularlstlisting}[<options>]
Full example:
\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}
You can use a \vskip
to adjust placement. I found that \baselineskip+\smallskipamount
gave ok result. As demonstrated by the added rule to visualize things.
\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}
A solution with a \raisebox
of the relevant value (the default value of the aboveskip
key is \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}