我有一个固定列列表,我已将其转换为 LaTeX 以编写一定比例的文本。下图说明了我所拥有的内容:
在绿色框上,比例文本与等宽文本完美对齐,这可能是偶然的,但这正是我想要的。
在红色框中,由于下标的存在,文本没有与固定列正确对齐。(在这种情况下,我认为应该增加间距,以便分号与其上方的空间对齐。)
我如何调整转义文本(可能使用数学模式方程式)以便它与列表内最接近的固定宽度列数对齐?
以下是重现上表的代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{color}
\usepackage{mathtools}
\usepackage{booktabs}
\begin{document}
\pagestyle{empty}
\lstnewenvironment{rgxbc}
{\lstset{
basicstyle=\ttfamily,
columns=fixed,
escapeinside={@}{@}
}
}
{}
\subsection{Relation to NFAs}
\begin{center}
\begin{tabular}{*3l}
\toprule
\multicolumn{1}{c}{\emph{Expression}} & \multicolumn{1}{c}{\emph{NFA}} & \multicolumn{1}{c}{\emph{Bytecode}} \\
\midrule
$\texttt{e}_{1}\texttt{e}_{2}$ &
...
&
\begin{rgxbc}
@\textnormal{\textit{code for }}$\texttt{e}_{1}$@;
@\textnormal{\textit{code for }}$\texttt{e}_{2}$@;
\end{rgxbc}
\\
\hline
$\texttt{e}_{1}| \texttt{e}_{2}$ &
...
&
\begin{rgxbc}
SPL .L1, .L2;
.L1: @\textnormal{\textit{code for }}$\texttt{e}_{1}$@;
JMP .L3;
.L2: @\textnormal{\textit{code for }}$\texttt{e}_{2}$@;
.L3:
\end{rgxbc}
\\
\hline
$\texttt{e?}$ &
...
&
\begin{rgxbc}
SPL .L1, .L2;
.L1: @\textnormal{\textit{code for }}$\texttt{e}$@;
.L2:
\end{rgxbc}
\\
\hline
$\texttt{e+}$ &
...
&
\begin{rgxbc}
.L1: @\textnormal{\textit{code for }}$\texttt{e}$@;
SPL .L1, .L2;
.L2:
\end{rgxbc}
\\
\hline
$\texttt{e*}$ &
...
&
\begin{rgxbc}
.L1: SPL .L2, .L3;
.L2: @\textnormal{\textit{code for }}$\texttt{e}$@;
JMP .L1;
.L3:
\end{rgxbc}
\\
\bottomrule
\end{tabular}
\end{center}
\end{document}
答案1
您可以强制将所有转义代码放入大小为列表固定列宽倍数的框中
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{color}
\usepackage{mathtools}
\usepackage{booktabs}
\makeatletter
\def\zz#1{{%
\setbox0\hbox{{#1}}%
\count0=\wd0
\divide\count0 by \dimexpr\lst@widthfixed\relax
\dimen0=\count0 \dimexpr\lst@widthfixed\relax
\ifdim\dimen0<\wd0 \advance\dimen0 \lst@widthfixed\fi
\makebox[\dimen0][l]{\usebox0}}}%
\makeatletter
\begin{document}
\pagestyle{empty}
\lstnewenvironment{rgxbc}
{\lstset{
basicstyle=\ttfamily,
columns=fixed,
escapeinside={@}{@}
}
}
{}
\subsection{Relation to NFAs}
\begin{center}
\begin{tabular}{*3l}
\toprule
\multicolumn{1}{c}{\emph{Expression}} & \multicolumn{1}{c}{\emph{NFA}} & \multicolumn{1}{c}{\emph{Bytecode}} \\
\midrule
$\texttt{e}_{1}\texttt{e}_{2}$ &
...
&
\begin{rgxbc}
@\zz{\textnormal{\textit{code for }}$\texttt{e}_{1}$}@;
@\zz{\textnormal{\textit{code for }}$\texttt{e}_{2}$}@;
\end{rgxbc}
\\
\hline
$\texttt{e}_{1}| \texttt{e}_{2}$ &
...
&
\begin{rgxbc}
SPL .L1, .L2;
.L1: @\zz{\textnormal{\textit{code for }}$\texttt{e}_{1}$}@;
JMP .L3;
.L2: @\zz{\textnormal{\textit{code for }}$\texttt{e}_{2}$}@;
.L3:
\end{rgxbc}
\\
\hline
$\texttt{e?}$ &
...
&
\begin{rgxbc}
SPL .L1, .L2;
.L1: @\zz{\textnormal{\textit{code for }}$\texttt{e}$}@;
.L2:
\end{rgxbc}
\\
\hline
$\texttt{e+}$ &
...
&
\begin{rgxbc}
.L1: @\zz{\textnormal{\textit{code for }}$\texttt{e}$}@;
SPL .L1, .L2;
.L2:
\end{rgxbc}
\\
\hline
$\texttt{e*}$ &
...
&
\begin{rgxbc}
.L1: SPL .L2, .L3;
.L2: @\zz{\textnormal{\textit{code for }}$\texttt{e}$}@;
JMP .L1;
.L3:
\end{rgxbc}
\\
\bottomrule
\end{tabular}
\end{center}
\end{document}