3

I have a fixed column listing on which I have escapped to LaTeX to write a proportional bit of text. The following pic illustrates what I have:

enter image description here

On the green box, the proportional text have perfectly aligned to the monospaced text, probably by accident, but that's exactly what I want.

On the red box, because of the subscript there, the text did not align properly to the fixed columns. (In this case, I believe the spacing should be increased so that the semicolon will align to the space above it.)

How could I justify escaped text (possibly with math mode equations) so that it would align to the closest number of fixed-width columns inside the listing?

Here's the code to reproduce the above table:

\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

You can force all the escaped code into a box a size a multiple of the listings fixed column width

enter image description here

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

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.