Estoy intentando obtener algunos listados y texto dentro de un entorno tabularx, pero no se ve muy bien:
Usé la sugerencia en¿Listado dentro de tabularx?para agregar los literales ^^J para que la cosa realmente se compile, pero el resultado se ve bastante horrible y el código no es mejor.
\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}
¿Cuál es la mejor manera de hacer esto para que no haya saltos de línea extraños en la tercera columna? (¡Y la mejor manera de hacer esto de manera más general!)
Respuesta1
Propongo dos soluciones (ciertamente no son las mejores, pero podría ser una forma de llamar la atención sobre la cuestión).
Primero: si puede prescindir del listings
paquete, puede definir uno \newcolumntype
que utilice \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}
Segundo: si no puedes prescindir del listings
paquete, aquí tienes una solución (muy) sucia con \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}