我的目标是在两个列表之间垂直居中放置一个箭头——考虑到小页面没有 [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
。
如果您的代码不超过一页,请将所有内容放在其中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}