Alinhamento vertical da seta entre duas minipáginas alinhadas na parte superior

Alinhamento vertical da seta entre duas minipáginas alinhadas na parte superior

Meu objetivo é ter uma seta centralizada verticalmente entre duas listagens - isso funciona bem, visto que não há alinhamento [t] das minipáginas, mas com elas a seta também está localizada na parte superior, enquanto eu quero que seja localizado no centro verticalmente. Observe que o [t] é desejado de forma que o código desalinhado fique alinhado. Abaixo está um 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}

Responder1

Use a \raisebox, mas calcule o deslocamento com base na metade da altura da caixa mais alta:

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

insira a descrição da imagem aqui

Se alguém quiser que a seta fique centrada nomais curtalistagem, em vez da mais longa, muda \dp0>\dp2para \dp0<\dp2no \ifdimteste.

Responder2

Você pode evitar minipagee raiseboxde todo.

Se o seu código não for maior que uma página, coloque tudo em a tabulare deixe o LaTeX fazer o alinhamento para você.

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

insira a descrição da imagem aqui

Responder3

Você pode mover a seta para baixo com \raiseboxe usar tikzpara personalizar a ponta da seta

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

insira a descrição da imagem aqui

informação relacionada