# Displaying code blocks parallel with one another

I'm trying to copy an example from a paper which comprises two code snippets next to each other and separated by a symbol, as shown in the image. But I'm not sure how to do something like this. It looks like verbatim code but that stuff is fragile.

Bonus points if it works in Beamer, but any clues to solve this is appreciated.

## 3 Answers

Use fancyvrb:

\documentclass{article}
\usepackage{fancyvrb}

\begin{document}

\begin{BVerbatim}[baseline=c]
local r1;
x  := 1;
r1 := y;
if (r1 = 0) then
critical region
\end{BVerbatim}
\quad$\parallel$\quad
\begin{BVerbatim}[baseline=c]
local r2;
y  := 1;
r2 := x;
if (r2 = 0) then
critical region
\end{BVerbatim}

\end{document}

This works out of the box also with beamer, the only care is to use

\begin{frame}[fragile]

Beamer columns are your friend!

\documentclass{beamer}

\begin{document}

\begin{frame}[fragile]
\begin{columns}[onlytextwidth]
\begin{column}{.4\textwidth}
\begin{verbatim}
local r1
x := 1
\end{verbatim}
\end{column}
\begin{column}{.05\textwidth}
\rotatebox{90}{=}
\end{column}
\begin{column}{.4\textwidth}
\begin{verbatim}
local r2
y := 1
\end{verbatim}
\end{column}
\end{columns}
\begin{center}
Dekker's Mutal Exclusion Algorithm
\end{center}
\end{frame}

\end{document}

You could easily store the contents of the algorithms in boxes, and then use the boxes in whichever structure you're comfortable with (like tabular, or the like):

\documentclass{beamer}

\usepackage{varwidth}

\newsavebox{\leftCode}
\newsavebox{\rightCode}

\begin{document}

\begin{lrbox}{\leftCode}
\begin{varwidth}{\linewidth}
\obeylines\obeyspaces\ttfamily
local r1;
x  := 1;
r1 := y;
if (r1 = 0) then
~~critical region
\end{varwidth}
\end{lrbox}

\begin{lrbox}{\rightCode}
\begin{varwidth}{\linewidth}
\obeylines\obeyspaces\ttfamily
local r2;
y  := 1;
r2 := x;
if (r2 = 0) then
~~critical region
\end{varwidth}
\end{lrbox}

\begin{frame}
\centering
\usebox{\leftCode} \quad
$\parallel$ \quad
\usebox{\rightCode}

\bigskip

(a) Dekker's Mutual Exclusion Algorithm
\end{frame}

\end{document}