Text editor for printing C++ code

ceditorside

I'm looking for an editor to print (on paper) C++ code. I'm currently in engineering school and the instructor has asked us to submit the code on paper.

He wants name + surname, the class number (on header), the number of page at the bottom, and the reserved words bolded for every page!

On Windows it can be done with notepadd++. But I'm on Linux and I haven't found an IDE or text editor that works. (I've already tried SCITE, gedit, and Syntaxic)

Best Answer

Well, if you want to go the extra mile, do it in LaTeX and provide a professional level PDF file. You haven't mentioned your distribution so I'll give instructions for Debian based systems. The same basic idea can be done on any Linux though.

  1. Install a LaTeX system and necessary packages

    sudo apt-get install texlive-latex-extra latex-xcolor texlive-latex-recommended
    
  2. Create a new file (call it report.tex) with the following contents:

    \documentclass{article}
    \usepackage{fancyhdr}
    \pagestyle{fancy}
    %% Define your header here. 
    %% See http://texblog.org/2007/11/07/headerfooter-in-latex-with-fancyhdr/
    \fancyhead[CO,CE]{John Doe, Class 123}
    
    \usepackage[usenames,dvipsnames]{color}  %% Allow color names
    
    %% The listings package will format your source code
    \usepackage{listings}
    \lstdefinestyle{customasm}{
      belowcaptionskip=1\baselineskip,
      xleftmargin=\parindent,
      language=C++,
      breaklines=true, %% Wrap long lines
      basicstyle=\footnotesize\ttfamily,
      commentstyle=\itshape\color{Gray},
      stringstyle=\color{Black},
      keywordstyle=\bfseries\color{OliveGreen},
      identifierstyle=\color{blue},
      xleftmargin=-8em,
      showstringspaces=false
    }        
    \begin{document}
    
    \lstinputlisting[style=customasm]{/path/to/your/code.c}
    
    \end{document}
    

    Just make sure to change /path/to/your/code.c in the penultimate line so that it point to the actual path of your C file. If you have more than one file to include, add a \newpage and then a new \lstinputlisting for the other file.

  3. Compile a PDF (this creates report.pdf)

    pdflatex report.tex    
    

I tested this on my system with an example file I found here and it creates a PDF that looks like this:

first page of the created pdf

For a more comprehensive example that will automatically find all .c files in the target folder and create an indexed PDF file with each in a separate section, see my answer here.

Related Question