tabularx 中的列表和文本

tabularx 中的列表和文本

我正在尝试在 tabularx 环境中获取一些列表和文本,但看起来不太好:

我使用了建议在 tabularx 里面列出吗?添加 ^^J 文字以使内容真正编译,但结果看起来很糟糕并且代码也没有改善。

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

有什么更好的方法可以做到这一点,以便第三列中不会出现奇怪的换行符?(以及更普遍的更好方法!)

答案1

我提出了两种解决方案(当然它们不是最好的,但它可以成为引起人们对这个问题关注的一种方式)。

第一的:如果不用listings包的话,您可以定义一个\newcolumntype使用 的包\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} 

在此处输入图片描述

第二:如果没有包你就无法完成listings任务,这里有一个(非常)肮脏的修复方法\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} 

在此处输入图片描述

相关内容