Text editor for printing C++ code
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.
-
Install a LaTeX system and necessary packages
sudo apt-get install texlive-latex-extra latex-xcolor texlive-latex-recommended
-
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. -
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:
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
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.
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.
Related videos on Youtube
Comments
-
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 triedSCITE
,gedit
, andSyntaxic
)-
loi219 over 7 yearsHe 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 over 7 yearsa2ps is your friend. Take your time to configure it.
-
GeoMint over 7 yearsI 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 over 7 yearsFor requests like this, you might want to consider softwarerecs.stackexchange.com
-
Anthon over 7 yearssounds like your teacher has invested in the logging industry.
-
Lightness Races in Orbit over 7 yearsYour teacher wants code ... on paper?!
-
loi219 over 7 yearsYes, we don't really why he wants that. He's an old school teacher
-
Tsundoku over 7 years@loi219 An "old-school" teacher or an old "school teacher", or both? ;-)
-
Peaceful over 7 yearsI 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 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 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 over 7 yearsEww, manual syntax highlighting.
-
loi219 over 7 yearsDo you have some idea?
-
loi219 over 7 yearsThis is absolutely what I'm searching. Thank you very much!
-
rubik over 7 yearsI second
emscript
. It has loads of options and gives very good results. -
Kyslik over 7 yearsAs 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 over 7 yearsI 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 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 over 7 yearsThis does not really answer the question, does it?
-
Arne over 7 yearsActually It does answer the question. The answer is: "Take a look at Kate, it proved to be good in the past for me"
-
cat over 7 yearsAlso, eww, code in variable-width font :(
-
Lightness Races in Orbit over 7 yearsAt least the C++ code is pretty good.
-
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 over 7 yearsThere is a LibreOffice extension for code colouring: Code Colorizer Formatter. (I haven't tested it yet.)
-
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 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 over 7 yearsThis looks painful
-
Ciprian Tomoiagă over 7 yearsthe extension Christophe mentioned is the only decent way to do it in LibreOffice
-
GeoMint over 7 yearsBefore 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)