Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 envirometn there I only want the "gray" background in the first column, not in the whole line. How can I do that?

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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

share|improve this answer
    
Thank you four your effort! –  Ander Biguri Jun 17 '13 at 14:13
    
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? –  Ander Biguri Jun 18 '13 at 8:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.