表格环境中的列表

表格环境中的列表

我正在关注在 Latex 中添加 C++ 代码用于将 C++ 代码添加到文档中。

我有一张具有以下格式的表格:

\documentclass[12pt,a4paper,final]{report}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{listings}
\lstset { %
    language=C++,
    backgroundcolor=\color{black!5}, % set backgroundcolor
    basicstyle=\footnotesize,% basic font setting
}

\begin{document}
  \begin{table}[H]
    \caption{C++ DIC class functions.}
    \centering
    \begin{tabular}{l c c c}
    \toprule[0.2em]

    \textbf{Function in C++} & \textbf{Input} & \textbf{Output} & \textbf{Document corresponding chapter}\\

    \midrule
    \multicolumn{4}{c}{Main setup and API functions. Public functions.}\\
    \begin{lstlisting}
     initialize() 
    \end{lstlisting}
     &sdf & df&f \\
    \bottomrule

    \end{tabular}
    \end{table}
\end{document}

我的问题是,当我使用 lstlisting 环境时,我只希望第一列有“灰色”背景,而不是整行都有。我该怎么做?

答案1

一种选择是保留两个独立的环境,这些环境具有几乎相同的设置,背景颜色除外;在表格内部,使用没有背景颜色的环境,并让\columncolor(从包中,通过选项colortbl加载)添加颜色:tablexcolor

\documentclass{article}
\usepackage{booktabs}
\usepackage{listings}
\usepackage[table]{xcolor}

\colorlet{listingscolor}{black!15}

\lstnewenvironment{mylistings}
  {\lstset{language=C++,
    backgroundcolor=\color{listingscolor}, % set backgroundcolor
    basicstyle=\footnotesize,% basic font setting
    }%
  }
  {}

\lstnewenvironment{cpptable}
  {\lstset{language=C++,
    basicstyle=\footnotesize,% basic font setting
    }%
  }
  {}

\begin{document}

\begin{table}
\caption{C++ DIC class functions.}
\centering
\begin{tabular}{>{\columncolor{listingscolor}}l c c c}
\toprule[0.2em]
\multicolumn{1}{c}{\textbf{Function in C++}} & \textbf{Input} & \textbf{Output} & \textbf{Document corresponding chapter}\\
\midrule
\multicolumn{4}{c}{Main setup and API functions. Public functions.}\\
\begin{cpptable}
 initialize() 
\end{cpptable}
 &sdf & df&f \\
\begin{cpptable}
 another_function() 
\end{cpptable}
 &sdf & df&f \\
\begin{cpptable}
 yet_another_function() 
\end{cpptable}
 &sdf & df&f \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

另一个选择是在本地选择一个合适的值linewidth

\documentclass{article}
\usepackage{booktabs}
\usepackage{listings}
\usepackage[table]{xcolor}
\lstset{ %
    language=C++,
    backgroundcolor=\color{black!15}, % set backgroundcolor
    basicstyle=\footnotesize,% basic font setting
}

\begin{document}

\begin{table}
\lstset{linewidth=4.5cm}
\caption{C++ DIC class functions.}
\centering
\begin{tabular}{l c c c}
\toprule[0.2em]
\multicolumn{1}{c}{\textbf{Function in C++}} & \textbf{Input} & \textbf{Output} & \textbf{Document corresponding chapter}\\
\midrule
\multicolumn{4}{c}{Main setup and API functions. Public functions.}\\
\begin{lstlisting}
 initialize() 
\end{lstlisting}
 &sdf & df&f \\
\begin{lstlisting}
 another_function() 
\end{lstlisting}
 &sdf & df&f \\
\begin{lstlisting}
 yet_another_function() 
\end{lstlisting}
 &sdf & df&f \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

相关内容