[Tex/LaTex] Horizontal alignment of text blocks

horizontal alignmentminipagespacing

I want two text blocks in a horizontal row. In the middle between them, there should be an arrow pointing from left to right.

This is what I currently have:

enter image description here

As you see, the arrow is not in the middle between the two text blocks. That's something I want to achieve.

Right now I'm using minipages with fixed width to achieve the result above.

\begin{minipage}{.2\textwidth}
\begin{verbatim}
X := 3
Y := Z * W
Q := X + Y
\end{verbatim}
\end{minipage}
%
\begin{minipage}{.2\textwidth}
  \centering $\Rightarrow$
\end{minipage}
%
\begin{minipage}{.2\textwidth}
\begin{verbatim}
X := 3
Y := Z * W
Q := 3 + Y
\end{verbatim}
\end{minipage}

But with that solution I would have to adjust the absolute width of the minipages until the text fits inside. That's not a clean solution though, because when I change the text size (or anything else related to the font) everything will break.

Is there a clean solution to horizontally align the two text blocks, with an arrow in the middle between them, and with adjustable whitespace between the blocks and the arrow?

Best Answer

Use \hfill:

\documentclass{article}
\begin{document}
\begin{minipage}{.2\textwidth}
\begin{verbatim}
X := 3
Y := Z * W
Q := X + Y
\end{verbatim}
\end{minipage}
\hfill $\Rightarrow$ \hfill
\begin{minipage}{.2\textwidth}
\begin{verbatim}
X := 3
Y := Z * W
Q := 3 + Y
\end{verbatim}
\end{minipage}
\end{document}

Note that the arrow seem not centered because the space not filled in minipages. For a better results you can use varwidth package instead of minipage

\documentclass{article}
\usepackage{varwidth}
\begin{document}
\hfill
\begin{varwidth}{.2\textwidth}
\begin{verbatim}
X := 3
Y := Z * W
Q := X + Y
\end{verbatim}
\end{varwidth}%
\hfill$\Rightarrow$\hfill%
\begin{varwidth}{.2\textwidth}
\begin{verbatim}
X := 3
Y := Z * W
Q := X + Y
\end{verbatim}
\end{varwidth}
\hfill
\end{document}

Using \dotfill instead of \hfill you can check visually that both block are reaally equally spaced.

MWE

Note: Be careful of remove spaces around \hfill (thanks to David to note the lack of ending %).