10

So I am adding some C++ code snippets in my document using

\lstset {language=C++}
\begin{lstlisting}
for (int i=0; i<iterations;i++)
{
do something
}
\end{lstlisting}

My issue is that the font in the code appears different and apparently bigger than the other normal text of the document.Is it supposed to be like this or if not how can I specify a specific size for the font.Also is there a way were I can have a light background for the code something like what I have for the code above?

lockstep
  • 250,273
rty
  • 447

2 Answers2

20

This is what you could have found out with the manual too and maybe faster than posting your question here ;-)

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

\begin{document}
Text before \dots
\begin{lstlisting}
for (int i=0; i<iterations;i++)
{
do something
}
\end{lstlisting}
Text after it \dots
\end{document}

If this doesn't answer your question(s) please provide a full working minimal example which illustrates your problem and maybe you can describe the problem more precisely.

Alan Munn
  • 218,180
Tobi
  • 56,353
  • 1
    @AnnaVopureta: You can edit the settings for listings individually. You may also take a log at the minted package. – Tobi Feb 07 '20 at 14:27
0

As suggested in this Quora post, I'm using the minted package. That looks way better.

\documentclass{article}

\usepackage{minted}

\begin{document} \begin{minted}{c} #include <stdio.h> int main() { print("Hello, world!"); return 0; } \end{minted} \end{document}

Below is the output from the same answer.

enter image description here