3

When I tried to import main.cpp inside the latex file, I always get an extra block which is below the Listing. Does anyone know how to solve this problem ?

\documentclass[12pt]{article}
\usepackage{listings}
\usepackage{color}
%Defint the color
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{1,1,1}

\begin{document}

\lstdefinestyle{cppStyle}{ %
    language=C++,
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    morekeywords={string,Node, Node*},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\tiny,                
    basicstyle=\ttfamily ,       
    numbers=left,                   
    numberstyle=\ttfamily ,    
    stepnumber=5,                   
    numbersep=5pt,                  
    showspaces=false,               
    showstringspaces=false,        
    showtabs=false,               
    frame=single,         
    tabsize=2,     
    captionpos=b,  
    breaklines=false, 
    breakatwhitespace=false,
    escapeinside={\%*}{*)}
}

\lstset{style=cppStyle}
\begin{minipage}{\linewidth}
    \lstinputlisting[nolol=true]{main.cpp}
    \begin{lstlisting}[caption={C++ Hello World}, label={lst:cpp_hello_world}]
    \end{lstlisting}
\end{minipage}
\end{document}

Result for the latex file

3
  • 3
    Welcome to TeX.SX! You have two listings, one of which is empty. The caption and label should be passed as options to \lstinputlisting.
    – egreg
    Aug 17, 2017 at 11:08
  • @egreg OMG! Thank you for your help ! It did solve my problem, and I think I need a cup of coffee.
    – thya421
    Aug 17, 2017 at 11:22
  • Well, it's lunch time for me. ;-)
    – egreg
    Aug 17, 2017 at 11:32

1 Answer 1

4

You have two listings, one of which is empty. The caption and label should be passed to the \lstinputlisting command.

I take the occasion to suggest some improvements.

  1. the minipage is useless (and makes harm, because it adds the paragraph indentation)
  2. the style can be passed as option
  3. the style should be defined in the preamble

Here's a full example, the filecontents* environment is just to make it self-contained (and not clobber my files).

\begin{filecontents*}{\jobname.cpp}
#include <iostream>
using namespace std;

int main()
{
  cout<<"Hello World in C++"<<endl;

  return 0;

}
\end{filecontents*}

\documentclass[12pt]{article}
\usepackage{listings}
\usepackage{color}
%Defint the color
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{1,1,1}

\lstdefinestyle{cppStyle}{
    language=C++,
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    morekeywords={string,Node, Node*},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\tiny,
    basicstyle=\ttfamily,
    numbers=left,
    numberstyle=\ttfamily,
    stepnumber=5,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    frame=single,
    tabsize=2,
    captionpos=b,
    breaklines=false,
    breakatwhitespace=false,
    escapeinside={\%*}{*)},
}

\begin{document}

\lstinputlisting[
  style=cppStyle,
  nolol=true,
  caption={C++ Hello World},
  label={lst:cpp_hello_world}
]{\jobname.cpp}

\end{document}

enter image description here

You must log in to answer this question.

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