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
3  
a2ps is your friend. Take your time to configure it. – Sato Katsura yesterday
5  
For requests like this, you might want to consider softwarerecs.stackexchange.com – Eric Renouf yesterday
21  
sounds like your teacher has invested in the logging industry. – Anthon yesterday
5  
Your teacher wants code ... on paper?! – Lightness Races in Orbit 23 hours ago
5  
@loi219 An "old-school" teacher or an old "school teacher", or both? ;-) – Christophe Strobbe 20 hours ago
up vote 53 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 create an indexed PDF file with each in a separate section, see my answer here.

share|improve this answer
10  
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 yesterday
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 yesterday
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 yesterday
2  
@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 20 hours ago
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 18 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 yesterday
    
I second emscript. It has loads of options and gives very good results. – rubik yesterday

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

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 yesterday
34  
Eww, manual syntax highlighting. – Useless yesterday
    
Do you have some idea? – loi219 yesterday
3  
Also, eww, code in variable-width font :( – cat 23 hours ago
1  
There is a LibreOffice extension for code colouring: Code Colorizer Formatter. (I haven't tested it yet.) – Christophe Strobbe 20 hours ago

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.

share|improve this answer

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

share|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.

share|improve this answer
2  
This does not really answer the question, does it? – countermode yesterday
    
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 23 hours ago

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.