6

How do I embed a source code listing in a tabular without messing up the spacing?

Here's a MWE of the problem:

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

Rendered MWE

6

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}

enter image description here

|improve this answer|||||
4

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}

enter image description here

|improve this answer|||||
2

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}

enter image description here

|improve this answer|||||
2

If you do not need syntax highlighting then use package fancyvrb:

\documentclass{article}
\usepackage{caption}
\usepackage{fancyvrb}
\usepackage{tabularx}

\begin{document}

\begin{table}
\begin{SaveVerbatim}[gobble=0]{VerbEnv}
push 28h
call operator new
\end{SaveVerbatim}

\caption{Attempt two.}

\begin{tabularx}{\linewidth}{l|X|l}
Fact name & Description & Assembly code example\\[1ex]\hline
hello & Instruction in allocates bytes of memory for the object blah blah blah.
      & \BUseVerbatim[baseline=t]{VerbEnv}
\end{tabularx}
\end{table}

\end{document}

enter image description here

|improve this answer|||||
2

You could also use a 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}

enter image description here

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.