Text editor for printing C++ code

7,958

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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:
7,958

Related videos on Youtube

loi219
Author by

loi219

Student

Updated on September 18, 2022

Comments

  • loi219
    loi219 over 1 year

    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)

    • loi219
      loi219 over 7 years
      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!
    • Satō Katsura
      Satō Katsura over 7 years
      a2ps is your friend. Take your time to configure it.
    • GeoMint
      GeoMint over 7 years
      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.
    • Eric Renouf
      Eric Renouf over 7 years
      For requests like this, you might want to consider softwarerecs.stackexchange.com
    • Anthon
      Anthon over 7 years
      sounds like your teacher has invested in the logging industry.
    • Lightness Races in Orbit
      Lightness Races in Orbit over 7 years
      Your teacher wants code ... on paper?!
    • loi219
      loi219 over 7 years
      Yes, we don't really why he wants that. He's an old school teacher
    • Tsundoku
      Tsundoku over 7 years
      @loi219 An "old-school" teacher or an old "school teacher", or both? ;-)
    • Peaceful
      Peaceful over 7 years
      I would simply add my name etc in the form of comment block and ask the teacher whether he knows any way to make it bold ;)
    • phuclv
      phuclv over 7 years
      @LightnessRacesinOrbit my professors said that a decade or 2 ago they have to write codes on paper and run them in their heads. Even nowadays some lecturers still require students to write paper code in tests to make them think very carefully and not relying on code autocomplete
    • tomsmeding
      tomsmeding over 7 years
      @Lightness In my university, we also have to submit report + code on paper for some subjects. Luckily, not all, and I suspect that's going to change now that printing isn't free for students anymore
  • Useless
    Useless over 7 years
    Eww, manual syntax highlighting.
  • loi219
    loi219 over 7 years
    Do you have some idea?
  • loi219
    loi219 over 7 years
    This is absolutely what I'm searching. Thank you very much!
  • rubik
    rubik over 7 years
    I second emscript. It has loads of options and gives very good results.
  • Kyslik
    Kyslik over 7 years
    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).
  • jamesqf
    jamesqf over 7 years
    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.
  • terdon
    terdon over 7 years
    @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.
  • countermode
    countermode over 7 years
    This does not really answer the question, does it?
  • Arne
    Arne over 7 years
    Actually It does answer the question. The answer is: "Take a look at Kate, it proved to be good in the past for me"
  • cat
    cat over 7 years
    Also, eww, code in variable-width font :(
  • Lightness Races in Orbit
    Lightness Races in Orbit over 7 years
    At least the C++ code is pretty good.
  • Toby Speight
    Toby Speight over 7 years
    @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.
  • Tsundoku
    Tsundoku over 7 years
    There is a LibreOffice extension for code colouring: Code Colorizer Formatter. (I haven't tested it yet.)
  • jamesqf
    jamesqf over 7 years
    @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.
  • terdon
    terdon over 7 years
    @jamesqf I'm pretty sure I had to install it manually last time I was using SuSe. That was back before Novell bought it though, so I have no idea what happens now. However, it's a pretty heavy package and used by a very specific subset of users, that's why I wouldn't expect it to be installed by default on any non-specialist distribution.
  • Joe
    Joe over 7 years
    This looks painful
  • Ciprian Tomoiagă
    Ciprian Tomoiagă over 7 years
    the extension Christophe mentioned is the only decent way to do it in LibreOffice
  • GeoMint
    GeoMint over 7 years
    Before downvote the answer take a minute and think that it really does the work. Its not the best, at least he can do it without using commands and installing latex packages. (latex is awesome)