4

I am aiming to have an arrow vertically centered between two listings -- this works fine given that there is no [t]-alignment of the minipages, but with them the arrow is located at the top as well, while I want it to be located in the central vertically. Note that the [t] is wanted such that the unaligned code becomes aligned. Below is a MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\begin{document}


\begin{minipage}[t]{.4\textwidth}
\begin{lstlisting}[numbers=left]
for (i in 1..n):
  unaligned code
\end{lstlisting}
\end{minipage}
$\rightarrow$\hfill
\begin{minipage}[t]{.4\textwidth}
\begin{lstlisting}[numbers=left]
for (i in 1..n):
  unaligned code
  ...
  ...
\end{lstlisting}
\end{minipage}

\end{document}
1
  • 1
    How the arrow can be vertically centered if the two minipages do not have the same height ? – Jérôme Dequeker Jan 23 '17 at 17:52
3

Use a \raisebox, but have it calculate the shift based on 1/2 the height of the tallest box:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\begin{document}


\setbox0=\hbox{\begin{minipage}[t]{.4\textwidth}
\begin{lstlisting}[numbers=left]
for (i in 1..n):
  unaligned code
\end{lstlisting}
\end{minipage}}%
%
\setbox2=\hbox{\begin{minipage}[t]{.4\textwidth}
\begin{lstlisting}[numbers=left]
for (i in 1..n):
  unaligned code
  ...
  ...
\end{lstlisting}
\end{minipage}}%
\noindent\copy0%
\ifdim\dp0>\dp2\relax%
\raisebox{-.5\dp0}{$\rightarrow$}\else%
\raisebox{-.5\dp2}{$\rightarrow$}\fi%
\hfill
\copy2

\setbox0=\hbox{\begin{minipage}[t]{.4\textwidth}
\begin{lstlisting}[numbers=left]
for (i in 1..n):
  unaligned code
for (i in 1..n):
  unaligned code
for (i in 1..n):
  unaligned code
for (i in 1..n):
  unaligned code
for (i in 1..n):
  unaligned code
\end{lstlisting}
\end{minipage}}%
%
\setbox2=\hbox{\begin{minipage}[t]{.4\textwidth}
\begin{lstlisting}[numbers=left]
for (i in 1..n):
  unaligned code
  ...
  ...
\end{lstlisting}
\end{minipage}}%
\noindent\copy0%
\ifdim\dp0>\dp2\relax%
\raisebox{-.5\dp0}{$\rightarrow$}\else%
\raisebox{-.5\dp2}{$\rightarrow$}\fi%
\hfill
\copy2

\end{document}

enter image description here

If one wants the arrow to be centered on the shorter listing, rather than the longer one, change \dp0>\dp2 to \dp0<\dp2 in the \ifdim test.

0
4

You may avoid minipage and raisebox at all.

If your code is not longer than a page, put everything in a tabular and let LaTeX do the alignment for you.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}

\usepackage{listings}
\lstset{
numbers=left
}

\begin{document}
Example with the code on the right longer than the one on the left: 
\begin{center}
\begin{tabular}{m{.4\textwidth}m{.07\textwidth}m{.4\textwidth}}
\begin{lstlisting}
for (i in 1..n):
  unaligned code
\end{lstlisting}
& $\rightarrow$ &
\begin{lstlisting}
for (i in 1..n):
  unaligned code
  ...
  ...
\end{lstlisting}\\
\end{tabular}
\end{center}

Example with the code on the left longer than the one on the right:
\begin{center}
\begin{tabular}{m{.4\textwidth}m{.07\textwidth}m{.4\textwidth}}
\begin{lstlisting}
for (i in 1..n):
  long code on the left 
  long code on the left
  long code on the left
  long code on the left
  long code on the left
  long code on the left
  long code on the left
  long code on the left
  long code on the left
\end{lstlisting}
& $\rightarrow$ &
\begin{lstlisting}
for (i in 1..n):
unaligned code
...
...
\end{lstlisting}
\end{tabular}
\end{center}
\end{document}

enter image description here

3

You can move arrow down with \raisebox, and use tikz to customize arrow head

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{tikz}

\begin{document}

\noindent%
\begin{minipage}[t]{.28\textwidth}
\begin{lstlisting}[numbers=left]
for (i in 1..n):
  unaligned code
\end{lstlisting}
\end{minipage}
\hfill\raisebox{-2\baselineskip}{\tikz[>=stealth]\draw[thick,red,->](0,0)--(.2\textwidth,0);}\hfill
\begin{minipage}[t]{.28\textwidth}
\begin{lstlisting}[numbers=left]
for (i in 1..n):
  unaligned code
  ...
  ...
\end{lstlisting}
\end{minipage}
\par

\end{document}     

enter image description here

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.