I am following Adding C++ code in Latex for adding C++ code to the document.
What I have is a table with the following format:
\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}
My problem is that when I use the lstlisting environment there I only want the "gray" background in the first column, not in the whole line. How can I do that?
Best Answer
One option would be to keep two separate environments with almost the same settings, except for the background color; inside the table you use the environment without the background color and let
\columncolor
(from thecolortbl
package, loaded through thetable
option forxcolor
) add the color:Another option is to locally choose an appropriate value for
linewidth
: