1

There is a few questions regarding inserting and appropriately formatting code in Google Docs (here and here for example). Indeed there is a very useful Google-Docs add-on called Code Blocks which works very well.

However, I would like the option to number the lines of code.

I got the idea from the output seen on this page (scroll down a bit and you 'll see what I mean). I am writing student notes for a beginners class I am teaching and it would be helpful to have similar numbers in the code formatted by Code Blocks within Google Docs.

I searched for a solution (this thread seemed promising, but no real solution).

Does anyone know of a way to have formatted code with numbered lines in Google Docs?

An (easily attainable) output like this would be ideal:

enter image description here

Thanks.

PS - I also searched for a 'reverse' solution, i.e. copy code WITH line numbers. My IDE is Visual Studio 2017 Community edition - and it doesn't seem to provide this option, I have only found extensions which work for Visual Studio Code (an altogether different application). However, it would be better to find a way to 'add' line numbers in google docs - this way one would not be restricted by the IDE/editor. Copying code and pasting in an editor and then re-copying with line numbers would be a choice, but way too cumbersome.

0

The workaround I use is codepad.org.
I paste the code, select the language and then the website outputs the code in a colour coded way along with the line numbers.

  1. I paste the code:

I pasted my code here.

  1. Then Copy the formatted code (If you want the line numbers, then include them in your selection too):

enter image description here

  1. Pasting it in Google Docs will add it as a table preserving all the formatting.

enter image description here

You can use other "syntax highlighters" too, but when pasting the code in Google Docs, you may have to adjust some line numbers manually.

  • Interesting idea, however it's a pity that codepad has support only for a few languages - I 'm quite unlucky, cause the two languages I was interested in, C# and Java, don't seem to be supported .. – thomas Jan 22 at 23:00
  • @thomas , then maybe you can try other "syntax highlighter". Another thing that I have noticed is, that I have my PyCharm's theme dark, and when I copy-pasted the code to Docs, it was copied along with the text colors and the dark background color (but not the line numbers though) – PlopMon Jan 24 at 16:00

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.