7

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?

0

1 Answer 1

6

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 the colortbl package, loaded through the table option for xcolor) add the color:

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

enter image description here

Another option is to locally choose an appropriate value for 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}

enter image description here

1
  • Hi. It seems that your solutions dont work very well in the longtable enviroment, as the gray bos ovrflows from the bottom. Do you ahve any solution for this? Jun 18, 2013 at 8:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .