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)

  • 4
    a2ps is your friend. Take your time to configure it. – Satō Katsura Oct 3 '16 at 15:55
  • 5
    For requests like this, you might want to consider softwarerecs.stackexchange.com – Eric Renouf Oct 3 '16 at 23:51
  • 30
    sounds like your teacher has invested in the logging industry. – Anthon Oct 4 '16 at 6:28
  • 10
    Your teacher wants code ... on paper?! – Lightness Races in Orbit Oct 4 '16 at 14:02
  • 8
    @loi219 An "old-school" teacher or an old "school teacher", or both? ;-) – Tsundoku Oct 4 '16 at 16:39

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:

    %% 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
      breaklines=true, %% Wrap long lines

    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.

| improve this answer | |
  • 17
    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 Oct 3 '16 at 18:08
  • 2
    I would think LaTex would be installed by default on most systems, especially in an engineering school. Type "which latex" at a command prompt: if it returns a path, typically /usr/bin/latex, you can skip step #1. Also, if your editor of choice has a decent macro language, you can run this script from within the editor, using e.g. "gv --watch" to display live output. – jamesqf Oct 4 '16 at 4:16
  • 4
    @jamesqf I've never seen a system with LaTeX installed by default. Yes, the IT people in some school might have installed it, and there may be specialized Linux distributions that have it by default, but the vast majority of systems won't. – terdon Oct 4 '16 at 13:12
  • 3
    @terdon - I've never seen an installation in an Engineering department without LaTeX. It's pretty essential for anyone who needs to produce printable output. – Toby Speight Oct 4 '16 at 16:39
  • 1
    @terdon: 'Default' or selected on install. I don't recall doing anything special to get LaTeX on my systems (OpenSuSE), but it's been a while since I did the last install. Either way, checking is simple. – jamesqf Oct 4 '16 at 18:35

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 $=|'
           -poutput.ps *.cpp


enter image description here


  • 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.
| improve this answer | |
  • This is absolutely what I'm searching. Thank you very much! – loi219 Oct 3 '16 at 17:21
  • I second emscript. It has loads of options and gives very good results. – rubik Oct 3 '16 at 17:36

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.

| improve this answer | |

Geany is a simple IDE that gives you many options for what goes in the header.

Reserved words can be manipulated in the options menu but the code highlighting does this for the notepad++ style. ( you may need to copy/download the color schemes to your "/home/$USER_NAME/.config/geany/colorschemes/" folder to get the one you want)

Very simple and personally I really like the line numbers and format it prints.

Also geany runs on both windows and linux.

Page 1

Page 2

Page 3

| improve this answer | |

Since you ask for an editor, you can print directly from Emacs, using ps-print-buffer.

The headers and footers are in the Customize group called ps-print-headers.

Assuming you use font-lock, you probably have the syntax highlighting that's required. It can be adjusted, if necessary, using the ps-extend-face function.

Having said that, I'd still recommend that you use a proper 'grind' tool such as a2ps, enscript, or LaTeX+listings.

| improve this answer | |

For completeness, and although I would not recommend that solution, the lp program has a -o prettyprint option which can do syntax highlighting.

You can use it like this for example:

lp -t "Name Surname | Class" -o document-format=application/x-csource -o prettyprint path/to/code.cpp

Here I used the -t (title) option to set the header. I cannot change the footer though (but the page number appears at the top). You can set the file type with the option -o document-format (the list of acceptable value should be in /usr/share/cups/mime/mime.types or maybe /etc/cups/mime/mime.types) but if your file has the correct extension, it should not be necessary. Also, the syntax highlighting is far from being perfect.

| improve this answer | |

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.

EDIT : ( Christophe Strobbe commented: )

There is a LibreOffice extension for code colouring: Code Colorizer Formatter. (I haven't tested it yet.)

| improve this answer | |
  • 41
    Eww, manual syntax highlighting. – Useless Oct 3 '16 at 16:34
  • Do you have some idea? – loi219 Oct 3 '16 at 16:37
  • 7
    Also, eww, code in variable-width font :( – cat Oct 4 '16 at 13:47
  • 2
    There is a LibreOffice extension for code colouring: Code Colorizer Formatter. (I haven't tested it yet.) – Tsundoku Oct 4 '16 at 16:42
  • 1
    This looks painful – Joe Oct 5 '16 at 21:25

I used to use Nedit when I was on a Solaris system many years ago as I liked a GUI interface. I see that it is still around. Check it out.

| improve this answer | |

In the past when KDE3 was still a big thing, I used Kate becaus for a text editor it really produces the best output. Today I would go with latex for a more embedable solution, with the package lstlisting. This package is not for color output, but on a laser printer colors are not an advantage.

| improve this answer | |
  • 2
    This does not really answer the question, does it? – countermode Oct 4 '16 at 13:20
  • Actually It does answer the question. The answer is: "Take a look at Kate, it proved to be good in the past for me" – Arne Oct 4 '16 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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