5

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 )

Marvin
  • 147
jpost
  • 51
  • 1
  • 3
  • Welcome to TeX SX! As to the wider box for your code, just insert in it in an adjustbox environment (from package chngpage). – Bernard Jan 10 '18 at 16:30
  • Thank you! In the meantime I also found this solution for the second part of the question: https://tex.stackexchange.com/questions/588/how-can-i-change-the-margins-for-only-part-of-the-text which also seems pretty much for what i was looking for. – jpost Jan 10 '18 at 16:38
  • 2
    Beauty is in the eye of the beholder. Without an example of what you mean, it's difficult to say anything about your problems. Please, add a minimal example. – egreg Jan 10 '18 at 17:15
  • 1
    thank you for the positive critics, i edited it to make my question more clear. – jpost Jan 10 '18 at 23:01

1 Answers1

5

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

egreg
  • 1,121,712
  • Thank you for the answer, the question i (still) have is: i dont like the font type thats written, and ttfamily does not look how i want it to look but i cant find a way to change it how i want it. For example if i want the text to be exacly like the text above, how and what would i change ? (to normal arial, to normal roman) (And no, i do not like the ttfamily look, but beauty not a uniform thing ;) – jpost Jan 11 '18 at 15:55
  • @jpost Use \rmfamily instead of \ttfamily, for instance. – egreg Jan 11 '18 at 15:59
  • and there is the problem i tried to tell: i does not work. I spent over 4 hours trying to do it: it simply does not work. The font of the source-code is changed and there seems to be no way to change it back. i am really frustrated – jpost Jan 11 '18 at 17:16
  • @jpost Do you mean that eith basicstyle=\rmfamily the outcome doesn’t change? – egreg Jan 11 '18 at 17:48
  • I think using Consolas font or something similar was what OP was asking – percusse Mar 28 '18 at 20:08
  • @percusse I added the image (and a hint for the code) with Source Code Pro. – egreg Mar 28 '18 at 21:10