1

I'm trying to get some listings and text inside a tabularx environment, but it's not looking very good:

I used the suggestion at Listing inside tabularx? to add the ^^J literals to get the thing to actually compile, but the result looks rather awful and the code no better.

\begin{tabularx}{\linewidth}{l l X}\label{tab:peepholeex}
Original stack code & Optimised stack code & Notes \\ \toprule
\begin{lstlisting}^^J
SET 1^^J
ADD
\end{lstlisting} &
\begin{lstlisting}^^J
INC
\end{lstlisting} &
Take advantage of the J5's {\lstinline!INC!} instruction \\ \midrule
\begin{lstlisting}^^J
SET 1^^J
SUB
\end{lstlisting} &
\begin{lstlisting}^^J
DEC
\end{lstlisting} &
Same as above, but for subtraction \\ \midrule
\begin{lstlisting}^^J
SET 0^^J
TEQ^^J
DROP
\end{lstlisting} &
\begin{lstlisting}^^J
TSZ
\end{lstlisting} &
Generated with an {\lstinline!IFN, x, 0!} statement. Instead take advantage of
the {\lstinline!TSZ!} instruction of the J5 \\ \midrule
\begin{lstlisting}^^J
SET x^^J
STORE^^J
SET x^^J
LOAD
\end{lstlisting} &
\begin{lstlisting}^^J
DUP^^J
SET x^^J
STORE
\end{lstlisting} &
A store followed by an immediate load of the same value. Instead, duplicate the
stored value and just store (since it might be used elsewhere) \\
\end{tabularx}

What's the better way to do this, so that there aren't weird line breaks in the 3rd column? (And the better way to do this more generally!)

2

I propose two solutions (certainly they are not the best, but it could be a way to bring attention to the question).

First: if you can do without listings package, you can define a \newcolumntype which uses \ttfamily.

\documentclass{book}
\usepackage{capt-of}
\usepackage{booktabs}
\usepackage{tabularx}
\newcolumntype{L}{>{\ttfamily\arraybackslash}p{6em}}
\begin{document}
\captionof{table}{Some caption}\label{tab:peepholeex}\vspace{1ex}
\noindent
\begin{tabularx}{\linewidth}{LLX}
    \normalfont\raggedright Original stack code & \normalfont\raggedright Optimised stack code & Notes \\ 
    \toprule
    SET 1\newline
    ADD
    &
    INC
    &
    Take advantage of the J5's \texttt{INC} instruction \\ \midrule
    SET 1\newline
    SUB
    &
    DEC
    &
    Same as above, but for subtraction \\ \midrule
    SET 0\newline
    TEQ\newline
    DROP
    &
    TSZ
    &
    Generated with an \texttt{IFN, x, 0} statement. Instead take advantage of
    the \texttt{TSZ} instruction of the J5 \\ \midrule
    SET x\newline
    STORE\newline
    SET x\newline
    LOAD
    &
    DUP\newline
    SET\newline
    STORE
    &
    A store followed by an immediate load of the same value. Instead, duplicate the
    stored value and just store (since it might be used elsewhere) \\
\end{tabularx}
\end{document} 

enter image description here

Second: if you can't do without listings package, here is a (very) dirty fix with \raisebox.

\documentclass{book}
\usepackage{ragged2e}
\usepackage{capt-of}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{listings}
\newlength{\myup}

\begin{document}
\settoheight{\myup}{\vbox{\texttt{X}}}
\addtolength{\myup}{1.5pt}
\captionof{table}{Some caption}\label{tab:peepholeex}\vspace{1ex}
\noindent
\begin{tabularx}{\linewidth}{llX}
    Original stack code & Optimised stack code & Notes \\[.5ex] \toprule
    \begin{lstlisting}^^J
    SET 1^^J
    ADD
    \end{lstlisting} & 
    \raisebox{\myup}{%  
    \begin{lstlisting}^^J
    INC
    \end{lstlisting}
    }\rule[0pt]{0pt}{2.5\myup} & 
    \raisebox{\myup}{%
    \parbox[t][2\myup][t]{13em}{\justifying\noindent
    Take advantage of the J5's {\lstinline!INC!} instruction
    }} \\[2ex] 
    \midrule
    \begin{lstlisting}^^J
    SET 1^^J
    SUB
    \end{lstlisting} &
    \raisebox{\myup}{%  
    \begin{lstlisting}^^J
    DEC
    \end{lstlisting}
    }\rule[0pt]{0pt}{2.5\myup} &
    \raisebox{\myup}{%
    \parbox[t][2\myup][t]{13em}{\justifying\noindent
    Same as above, but for subtraction
    }} \\[2ex] \midrule
    \begin{lstlisting}^^J
    SET 0^^J
    TEQ^^J
    DROP
    \end{lstlisting} &
    \raisebox{2\myup}{% 
    \begin{lstlisting}^^J
    TSZ
    \end{lstlisting}
    } &
    \raisebox{1.8\myup}{%
    \parbox[t][4\myup][t]{13em}{\justifying\noindent
    Generated with an {\lstinline!IFN, x, 0!} statement. Instead take advantage of
    the {\lstinline!TSZ!} instruction of the J5 
    }}\\[5.5ex] 
    \midrule
    \begin{lstlisting}^^J
    SET x^^J
    STORE^^J
    SET x^^J
    LOAD
    \end{lstlisting} &
    \raisebox{.8\myup}{%    
    \begin{lstlisting}^^J
    DUP^^J
    SET x^^J
    STORE
    \end{lstlisting}
    }\rule[0pt]{0pt}{4.2\myup} &
    \raisebox{2.5\myup}{%
    \parbox[t][4\myup][t]{13em}{\justifying\noindent
    A store followed by an immediate load of the same value. Instead, duplicate the
    stored value and just store (since it might be used elsewhere)
    }} \\
\end{tabularx}
\end{document} 

enter image description here

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.