Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)

share|improve this question
    
He wants the name + surname, the class number (on header), the number of page at the bottom and the reserved words are to be bold for every pages! – loi219 10 hours ago
    
a2ps is your friend. Take your time to configure it. – Sato Katsura 10 hours ago
    
I used to print my C/C++ code with colors via gVim. I think you just need LibreOffice and paste your c++ code. You can just highlight the words by "find" and select bold. – GeoMint 10 hours ago
    
For requests like this, you might want to consider softwarerecs.stackexchange.com – Eric Renouf 2 hours ago
up vote 25 down vote accepted

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 created an indexed PDF file with each in a separate section, see my answer here.

share|improve this answer
5  
As soon as you start with LaTeX you can not let go! I wish I knew TeX in highschool... (or they would teach it as an alternative to word). – Kyslik 8 hours ago

I'd usually use enscript: something like

$ enscript --highlight=cpp
           --header='|Real Name|Class 101'
           --footer='|Page $% of $=|'
           -poutput.ps *.cpp

will be a start - this writes postscript output to output.ps, so you can preview and overwrite that while you're tinkering with the config and then print it once you're happy. See the man page for more very extensive options.

EDIT getting the footer to work correctly is a bit of a pain with enscript - I'd never noticed because I've never required it. If you save this file to ~/.enscript/so.hdr (you probably need to create the directory), you'll actually get the required output with

$ enscript --highlight=cpp
           --header='|Real Name|Class 101'
           --footer='|Page $% of $=|'
           --fancy-header=so
           -poutput.ps *.cpp

giving

enter image description here


Roughly,

  • LaTeχ is the best quality and the most work to set up,
  • enscript or a2ps are intermediate in both quality and work,
  • vim's :hardcopy command is easy but not that flexible, and
  • doing syntax highlighting manually in a non-code-aware editor is a lot of effort for a poor return.
share|improve this answer
    
This is absolutely what I'm searching. Thank you very much! – loi219 8 hours ago
    
I second emscript. It has loads of options and gives very good results. – rubik 8 hours ago
    
That's one comprehensive edit, thanks! – terdon 7 hours ago

You just need LibreOffice Writer .

Paste your C/C++ code.

Find the words and make the Bold all the C++ keywords.

You can find them here: http://en.cppreference.com/w/cpp/keyword

To find a word Press Ctrl + F. Select "Match case" and click Find All (as shown in screenshot).

enter image description here

After that you need a Header and Footer with your name and page number.

share|improve this answer
    
Thanks, I'm going to test it! – loi219 9 hours ago
11  
Eww, manual syntax highlighting. – Useless 9 hours ago
    
Do you have some idea? – loi219 9 hours ago

gedit allows you to print out code with a header. It should be available on any gnome system.

share|improve this answer

You can use the :TOhtml command in vim. This renders what you see (i.e. syntax highlighting) as html. From there, a web browser that can print to pdf works, as you can usually customize the header/footer content.

This is probably similar to the :hardcopy command mentioned by Useless, but I can't verify on my system right now.

Another possibility is to print from QtCreator, however there doesn't appear to be a way to set the headers/footers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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