
Mi objetivo es tener una flecha centrada verticalmente entre dos listados; esto funciona bien dado que no hay alineación [t] de las minipáginas, pero con ellas la flecha también está ubicada en la parte superior, mientras que yo quiero que esté Ubicado en el centro verticalmente. Tenga en cuenta que se desea [t] de manera que el código no alineado quede alineado. A continuación se muestra un 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}
Respuesta1
Utilice a \raisebox
, pero haga que calcule el desplazamiento basándose en la mitad de la altura de la caja más 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}
Si uno quiere que la flecha esté centrada en elcortalistado, en lugar del más largo, cambie \dp0>\dp2
a \dp0<\dp2
en la \ifdim
prueba.
Respuesta2
Puedes evitarlo minipage
y raisebox
en absoluto.
Si su código no tiene más de una página, coloque todo en un tabular
y deje que LaTeX haga la alineación por usted.
\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}
Respuesta3
Puede mover la flecha hacia abajo con \raisebox
y usarla tikz
para personalizar la punta de la flecha.
\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}