4

I am trying to put C code inside tcolorbox. See below the code snippet-

\documentclass{standalone}

% for code formatting
\usepackage[formats]{listings}
\usepackage{filecontents}

\usepackage{tcolorbox}

\begin{filecontents*}{code.c}
#define DATA_TYPE_BYTES 4

// receive data
unsigned char* byte_ptr = 
    static_cast<unsigned char*>(msg.data());

// deserialize 
int width;
std::memcpy(&width, byte_ptr, DATA_TYPE_BYTES);
byte_ptr += DATA_TYPE_BYTES;
\end{filecontents*}

\begin{document}
\begin{tcolorbox}[title=Deserialization, hbox]
    %\lstinputlisting[format=C]{code.c} %isn't working
    \lstinputlisting{code.c}
\end{tcolorbox}
\end{document}

The above code produces following output-

screenshot of the generated PDF

Notice that the formatting didn't work well since colors are unavailable in the generated PDF.

Next, I gave another attempt but failed miserably. See below the code snippet-

\documentclass{standalone}
\usepackage{tcolorbox}
\usepackage{listings}

\begin{document}
\begin{tcolorbox}[title=Deserialization, hbox]
    \lstset{language=C++,
        basicstyle=\ttfamily,
        keywordstyle=\color{blue}\ttfamily,
        stringstyle=\color{red}\ttfamily,
        commentstyle=\color{green}\ttfamily,
        morecomment=[l][\color{magenta}]{\#}
    }
\begin{lstlisting}
#define DATA_TYPE_BYTES 4

// receive data
unsigned char* byte_ptr = 
    static_cast<unsigned char*>(msg.data());

// deserialize 
int width;
std::memcpy(&width, byte_ptr, DATA_TYPE_BYTES);
byte_ptr += DATA_TYPE_BYTES;
\end{lstlisting}
\end{tcolorbox}
\end{document}

The above code doesn't compile and throws following error-

! Illegal parameter number in definition of \lst@insertargs.
<to be read again>
}
l.26 \end{tcolorbox}
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <7> on input line 26.

It seems that tcolorbox isn't compatible with listings. Well, I want to know how to format C code inside tcolorbox.

1 Answer 1

9

Look for tcblisting in the manual.

\documentclass{standalone}
\usepackage{tcolorbox}
\tcbuselibrary{listings}

\begin{document}

\begin{tcblisting}{
    title=Deserialization,
    hbox,
    listing only,
    listing options={
      language=C++,
      basicstyle=\ttfamily,
      keywordstyle=\color{blue}\ttfamily,
      stringstyle=\color{red}\ttfamily,
      commentstyle=\color{green}\ttfamily,
      morecomment={[l][\color{magenta}]{\#}},
    }
  }
#define DATA_TYPE_BYTES 4

// receive data
unsigned char* byte_ptr = 
    static_cast<unsigned char*>(msg.data());

// deserialize 
int width;
std::memcpy(&width, byte_ptr, DATA_TYPE_BYTES);
byte_ptr += DATA_TYPE_BYTES;
\end{tcblisting}

\end{document}

enter image description here

3
  • Thank you very much. +1 for providing the manual. Can you please tell me the difference between tcolorbox and tcblisting?
    – ravi
    Commented May 9, 2018 at 5:39
  • 3
    @RaviJoshi tcolorbox formats the input as regular text (and can only be used with regular text), tcblisting formats the input as a code listing. Commented May 9, 2018 at 5:40
  • How to define the style at the top where \usepackages are mentioned?
    – Robur_131
    Commented Aug 27, 2020 at 7:08

You must log in to answer this question.

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