[Tex/LaTex] C++ code, change the font

fontslistings

I am Google-ing now for a while but couldn't find the answer. So here are the questions again:

I am trying to include parts of my C++ source code into a LaTeX document, and right now I am using \lstinputlisting[language=C++, firstline=37, lastline=40] for this purpose, which seems pretty good for it. The only thing that annoys me is, that it seems impossible to
a) change the font type of the source code in the text. (It looks like 1997 not like 2018 :/ )
b) widen the space between the text and the frame (background-colour)
Right now my example is like this:

\usepackage{listings}

    \lstset{ %
  backgroundcolor=\color{gray},  
  basicstyle=\rmfamily,
  breakatwhitespace=false,      
  breaklines=true,                
  captionpos=b,                    
  commentstyle=\color{mygreen}, 
  extendedchars=true,              
  frame=single,                   
  keepspaces=true,             
  keywordstyle=\color{blue},      
  language=c++,                 
  numbers=none,                
  numbersep=5pt,                   
  numberstyle=\tiny\color{blue}, 
  rulecolor=\color{mygray},        
  showspaces=false,               
  showtabs=false,                 
  stepnumber=5,                  
  stringstyle=\color{mymauve},    
  tabsize=3,                      
  title=\lstname                
}

\begin{document}

\lstinputlisting[language=C++, firstline=37, lastline=300]{../source/document.cc}

/end{document}

And I want to make it look more like, for example, Github now (here is a random Github page, representing how I want the code to look like: https://github.com/tesseract-ocr/tesseract/wiki/APIExample )

Best Answer

I suggest basicstyle=\ttfamily (there are several monospaced fonts available) and also columns=fullflexible.

Here I guessed your colors and changed the background color to a much lighter gray.

Using lstlisting or \lstinput is equivalent as far as the output is concerned.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\colorlet{mygray}{black!30}
\colorlet{mygreen}{green!60!blue}
\colorlet{mymauve}{red!60!blue}

\lstset{
  backgroundcolor=\color{gray!10},  
  basicstyle=\ttfamily,
  columns=fullflexible,
  breakatwhitespace=false,      
  breaklines=true,                
  captionpos=b,                    
  commentstyle=\color{mygreen}, 
  extendedchars=true,              
  frame=single,                   
  keepspaces=true,             
  keywordstyle=\color{blue},      
  language=c++,                 
  numbers=none,                
  numbersep=5pt,                   
  numberstyle=\tiny\color{blue}, 
  rulecolor=\color{mygray},        
  showspaces=false,               
  showtabs=false,                 
  stepnumber=5,                  
  stringstyle=\color{mymauve},    
  tabsize=3,                      
  title=\lstname                
}

\begin{document}

\begin{lstlisting}
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>

int main()
{
    char *outText;

    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
    // Initialize tesseract-ocr with English, without specifying tessdata path
    if (api->Init(NULL, "eng")) {
        fprintf(stderr, "Could not initialize tesseract.\n");
        exit(1);
    }

    // Open input image with leptonica library
    Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
    api->SetImage(image);
    // Get OCR result
    outText = api->GetUTF8Text();
    printf("OCR output:\n%s", outText);

    // Destroy used object and release memory
    api->End();
    delete [] outText;
    pixDestroy(&image);

    return 0;
}
\end{lstlisting}

\end{document}

enter image description here

An alternative is minted.

\documentclass{article}
\usepackage{xcolor}
\usepackage{minted}

\begin{document}

\begin{minted}{c++}
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>

int main()
{
    char *outText;

    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
    // Initialize tesseract-ocr with English, without specifying tessdata path
    if (api->Init(NULL, "eng")) {
        fprintf(stderr, "Could not initialize tesseract.\n");
        exit(1);
    }

    // Open input image with leptonica library
    Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
    api->SetImage(image);
    // Get OCR result
    outText = api->GetUTF8Text();
    printf("OCR output:\n%s", outText);

    // Destroy used object and release memory
    api->End();
    delete [] outText;
    pixDestroy(&image);

    return 0;
}
\end{minted}

\end{document}

enter image description here

If you want a 21st century monospaced font, here's the result with minted after adding

\usepackage{sourcecodepro}

\setminted{fontsize=\footnotesize}

(or you could scale the font at the outset, with the specific option).

enter image description here