3

I am trying to include a C++ code in my latex document. However, what I have tried is not working. Everything else works fine, but I am not seeing the C++ showing up as a C++ code. It is just written in the same format as the "Alpha particles .." text

Here is the MWE: The tex is:

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

\begin{document}  
Alpha particles \cite{wikip} (named \cite{Comp} after and denoted by the first letter     in the
Greek alphabet,\[\alpha\]) consist of two protons and two neutrons bound
together.
This means that an particle is a helium nucleus. 

\begin{lstlisting}
int size =1;
*ptr = Mole:getMole(size + 1);
\end{lstlisting}

\bibliographystyle{plain}
\bibliography{BibName}

\end{document}

Expected result:

enter image description here

  • 1
    basicstyle=\footnotesize\ttfamily – Steven B. Segletes Dec 17 '14 at 0:44
  • I added an image to the question (as it's easier than adding it to a comment) basically your file works without error and produces the result shown. Do you get an error (eg missing package??) – David Carlisle Dec 17 '14 at 0:44
  • Yes, I get the error ! LaTeX Error: File listings.sty' not found. Type X to quit or <RETURN> to proceed, or enter new name. (Default extension: sty)` Other than using listings, is there another way I can add C++ code to the latex file? – user4352158 Dec 17 '14 at 0:46
  • You could have mentioned that in the question:-) listings is part of any tex distribution so if you have texlive or miktex the package manager should be able to install it for you – David Carlisle Dec 17 '14 at 0:53
  • If you don't need syntax highlight you could just use \begin{verbatim} ...\end[verbatim} which needs no package, but better to update your installation to include listings – David Carlisle Dec 17 '14 at 0:54
2

You want to use a different font family for the listing. Since keywords are boldface in the style, the standard Computer Modern Typewriter fonts aren't well suited, because they don't have bold face.

A good monospaced font family that distinguishes well between medium series and boldface is BeraMono.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{xcolor}
\usepackage[scaled=.85]{beramono}

\lstset{
  language=C++,
  backgroundcolor=\color{black!5}, % set backgroundcolor
  basicstyle=\footnotesize\ttfamily,% basic font setting
  columns=fullflexible,
}

\begin{document}

Alpha particles (named after and denoted by the first letter in the Greek alphabet,
$\alpha$) consist of two protons and two neutrons bound together. This means that an
particle is a helium nucleus.

\begin{lstlisting}
int size =1;
*ptr = Mole:getMole(size + 1);
\end{lstlisting}

\end{document}

Note that for an inline formula you use $\alpha$ or \(\alpha\), rather than \[\alpha\] which would center the formula.

I removed the citations that are irrelevant for this topic.

enter image description here

0

If you are unable to get listings working, there is the verbatimbox package if you don't need keyword highlighting.

Here I show listings and verbatimbox for comparison. By using the verbnobox environment, one can obtain page-breaks in the middle of the listing.

\documentclass{article}
\usepackage{xcolor}
% LISTINGS PREP
\usepackage{listings}
\lstset { 
language=C++,
backgroundcolor=\color{black!5}, % set backgroundcolor
basicstyle=\footnotesize,% basic font setting
}
% VERBATIMBOX PREP
\usepackage{verbatimbox}
\def\codesize{\footnotesize}
\newsavebox\thecolorfield
\newcommand\setcolorfield[1][blue!9!gray!8]{%
  \savebox{\thecolorfield}{\codesize%
    \makebox[0pt][l]{\textcolor{#1}{%
    \rule[-\dp\strutbox]{\textwidth}{\dimexpr\ht\strutbox+\dp\strutbox}}}}%
}
\def\colorfield{\usebox{\thecolorfield}}
\setcolorfield
%
\begin{document}  
WITH LISTINGS:
\begin{lstlisting}
int size =1;
*ptr = Mole:getMole(size + 1);
\end{lstlisting}

WITH VERBATIMBOX (ttfamily):
\begin{verbnobox}[\colorfield\ttfamily\codesize]
int size =1;
*ptr = Mole:getMole(size + 1);
\end{verbnobox}

\setcolorfield[green!8]
WITH VERBATIMBOX (bold-rmfamily):
\begin{verbnobox}[\colorfield\rmfamily\bfseries\codesize]
int size =1;
*ptr = Mole:getMole(size + 1);
\end{verbnobox}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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