上揃えの 2 つのミニページ間の矢印の垂直配置

上揃えの 2 つのミニページ間の矢印の垂直配置

私は、2 つのリストの間に垂直に中央に矢印を配置することを目指しています。ミニページに [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

を使用します\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

コードが 1 ページより長くない場合は、すべてを に入れて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

矢印を下に移動するには を使用し\raiseboxtikz矢印の頭をカスタマイズするにはを使用します。

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

ここに画像の説明を入力してください

関連情報