兩個頂部對齊的小頁面之間的箭頭垂直對齊

兩個頂部對齊的小頁面之間的箭頭垂直對齊

我的目標是在兩個列表之間有一個垂直居中的箭頭——考慮到小型頁面沒有 [t] 對齊,這很好用,但對於它們來說,箭頭也位於頂部,而我希望它位於垂直中央。請注意,需要 [t] 以便未對齊的程式碼變得對齊。下面是一個 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

使用 a \raisebox,但讓它根據最高框的高度的 1/2 計算偏移:

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

在此輸入影像描述

如果希望箭頭居中更短在測試中將列表更改\dp0>\dp2為,而不是較長的列表。\dp0<\dp2\ifdim

答案2

你可以minipage完全避免raisebox

如果您的程式碼不長於一頁,請將所有內容放入 a 中tabular,然後讓 LaTeX 為您進行對齊。

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

在此輸入影像描述

答案3

您可以使用 , 向下移動箭頭\raisebox,並使用tikz自訂箭頭

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

在此輸入影像描述

相關內容