将代码和公式组合在一起并对齐

将代码和公式组合在一起并对齐

这是我正在准备的演示文稿中的一张幻灯片:

\documentclass{beamer}
\usepackage{listings}
\begin{document}
\begin{frame}[fragile]{Example}
\begin{tabular}{ l r }
\begin{lstlisting}[mathescape=true]
harness void main(int x){
int y = x * ??$_0$;
assert y == x + x;
}
\end{lstlisting} & 
\begin{minipage}{.8\textwidth}
    $\phi:\{(??_0,\tau_{\emptyset}) \} \to \mathbb{Z}$. \newline
    $y \equiv \lambda \phi. x * \phi(??_0,\tau_{\emptyset})$ 
\end{minipage}%
 \\ \hline
\begin{lstlisting}[mathescape=true]
generator int linexp(int t){
 return t * ??$_0$ + ??$_1$ ;
}

generator int 
 linexp2(int t1, int t2){
 return linexp$_{g1}$(t1) + 
        linexp$_{g2}$(t2);
}

void main(int x, int z){
 int y = linexp2$_{g0}$(x, z);
 assert y == x + x + z;
}   
\end{lstlisting} &  
\begin{minipage}{.9\textwidth}
    $\tau_1 = g_0 \cdot g_1, \tau_2 = g_0 \cdot g_2$ \\
    $\phi:\{(??_0,\tau_1),(??_0,\tau_2), 
            (??_1,\tau_1),(??_1,\tau_2) \} \to \mathbb{Z}$ \\
    $y \equiv \lambda \phi. x * \phi(??_0,\tau_1) + \phi(??_1,\tau_1) 
       + z * \phi(??_0,\tau_2) + \phi(??_1,\tau_2)$ 
\end{minipage}%
\end{tabular}
\end{frame}
\end{document}

在此处输入图片描述

我怎样才能让文本向右对齐?例如:

在此处输入图片描述

答案1

我会使用aligned环境来处理数学材料,并且我会在第二个数学组中使用更多换行符。我还会使用\colon而不是:,因为后者默认被视为关系运算符,而这里似乎并非如此。

在此处输入图片描述

\documentclass{beamer}
\usepackage{listings}
\begin{document}
\begin{frame}[fragile]{Example}
\begin{tabular}{@{} ll @{}}
\begin{lstlisting}[mathescape=true]
harness void main(int x){
int y = x * ??$_0$;
assert y == x + x;
}
\end{lstlisting} & 
$\begin{aligned}
&\phi\colon\{(??_0,\tau_{\emptyset}) \} \to \mathbb{Z} \\
&y \equiv \lambda \phi\cdot x * \phi(??_0,\tau_{\emptyset})
\end{aligned}$ \\ 
\hline
\begin{lstlisting}[mathescape=true]
generator int linexp(int t){
 return t * ??$_0$ + ??$_1$ ;
}

generator int 
 linexp2(int t1, int t2){
 return linexp$_{g1}$(t1) + 
        linexp$_{g2}$(t2);
}

void main(int x, int z){
 int y = linexp2$_{g0}$(x, z);
 assert y == x + x + z;
}   
\end{lstlisting} &  
$\begin{aligned}
&\tau_1 = g_0 \cdot g_1,\\
&\tau_2 = g_0 \cdot g_2, \\
&\phi\colon\{(??_0,\tau_1),(??_0,\tau_2), \\
&\qquad(??_1,\tau_1),(??_1,\tau_2) \} \to \mathbb{Z} \\
&y \equiv \lambda \phi\cdot x * \phi(??_0,\tau_1) \\
&\qquad+ \phi(??_1,\tau_1) \\
&\qquad+ z * \phi(??_0,\tau_2) \\
&\qquad+ \phi(??_1,\tau_2)
\end{aligned}$
\end{tabular}
\end{frame}

\end{document}

相关内容