
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}
Se alguém quiser que a seta fique centrada nomais curtalistagem, em vez da mais longa, muda \dp0>\dp2
para \dp0<\dp2
no \ifdim
teste.
Responder2
Você pode evitar minipage
e raisebox
de todo.
Se o seu código não for maior que uma página, coloque tudo em a tabular
e 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}
Responder3
Você pode mover a seta para baixo com \raisebox
e usar tikz
para 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}