表格和列表 - 在第 2 列导入代码时,第 1 列的段落会向下移动

表格和列表 - 在第 2 列导入代码时,第 1 列的段落会向下移动

在格式化我的十页长的文档时出现了一个问题,我将尝试发布与该问题相关的所有代码,但要知道我对 LaTeX 还很陌生,所以我可能没有意识到某些部分可能会影响其他事情等等。

\lstdefinestyle{customc}{
  language=C,
  showstringspaces=false,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\bfseries\color{green!40!black},
  commentstyle=\itshape\color{purple!40!black},
  identifierstyle=\color{blue},
  stringstyle=\color{orange},
}

\lstset{escapechar=@, style=customc}

\begin{tabular}{p{7cm} l}
\multicolumn{2}{c}{}\\
\hline
This is the TestAndSet atomic operation example implemented in C code. 
It takes a lock and sets it to true. 
The test is the return value of the function. This is usually used
in a while loop. The lock is initialised to false. If it is true, 
it will return true and the loop will not break. &
\begin{lstlisting}[aboveskip=-10pt]
   bool TestAndSet(bool *lock) {
     bool local = *lock;
     *lock = true;
     return local;
   }
\end{lstlisting} \\
\hline
This is the Swap atomic operation example implemented in C code. 
It takes a lock and a key
of type bool. The lock will be initialised to false.
It will then swap the values of these variables, and if the value of
lock was false, the key will now become false, and you can exit the loop 
where the key variable is the loop condition, which was initialised to 
true. &
\begin{lstlisting}
   void Swap(bool *lock, bool *key) {
     bool local = *lock;
     *lock = *key;
     *key = local;
   }
\end{lstlisting} \\
\multicolumn{2}{c}{}
\end{tabular}

我认为这就是与我遇到的问题相关的所有代码,这也是我现在要描述的内容。

每当我在那里有这些表格时,第一列中的段落就会多出一行或类似的东西,看起来它们是底部对齐而不是顶部对齐。如果我在第二列中有常规文本,则不会发生这种情况。我尝试将第二列的类型更改为具有设置宽度的其他类型,但它仍然这样做。如果你想让我发布更多代码,请告诉我,但整个文档有点大,输出大约 10 页。

你知道这可能出了什么问题吗?

答案1

我已经完善了您的代码,您将两个列(p-type 和l-type)并排放置。结果是处理文本时左列比您预期的要低。我将它们都设置为p-type,这样就很容易操作它们了。我附上了一个示例,供您进一步实验。

%! latex mal-listings.tex
\documentclass{article}
\pagestyle{empty}
\usepackage{listings}
\usepackage{xcolor}

\begin{document}
\lstdefinestyle{customc}{
  language=C,
  showstringspaces=false,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\bfseries\color{green!40!black},
  commentstyle=\itshape\color{purple!40!black},
  identifierstyle=\color{blue},
  stringstyle=\color{orange},
  aboveskip=-0.5\baselineskip,
  }
\lstset{escapechar=@, style=customc}

\begin{tabular}{p{6cm} p{6.5cm}}
%\multicolumn{2}{c}{}\\
\hline
This is the TestAndSet atomic operation example implemented in C code. 
It takes a lock and sets it to true. 
The test is the return value of the function. This is usually used
in a while loop. The lock is initialised to false. If it is true, 
it will return true and the loop will not break. &
%
\begin{lstlisting}
   bool TestAndSet(bool *lock) {
     bool local = *lock;
     *lock = true;
     return local;
   }
\end{lstlisting}
\\ \hline
This is the Swap atomic operation example implemented in C code. 
It takes a lock and a key
of type bool. The lock will be initialised to false.
It will then swap the values of these variables, and if the value of
lock was false, the key will now become false, and you can exit the loop 
where the key variable is the loop condition, which was initialised to 
true. &
\begin{lstlisting}
  void Swap(bool *lock, bool *key) {
     bool local = *lock;
     *lock = *key;
     *key = local;
   }
\end{lstlisting} 
\\ \hline
%\multicolumn{2}{c}{}
\end{tabular}
\end{document}

姆韦

相关内容