56
votes

Is there a easy way to insert source code with highlight in Google Docs?

2
  • Well, this question is not really a duplicate. The related question asks explicitly for Syntax Highlighting. This question is more generic. And the answer is: Currently this is not possible. You can use plugins which set colors for you, but inside the document this is just text with color, not "code" (pre). This becomes especially relevant, when handling with the document structure in the docs API.
    – Armin
    Commented Jul 27, 2021 at 15:48
  • This question doesn't meet the current quality standards. Nowadays this might be a trivial question. Commented Apr 7, 2023 at 19:00

2 Answers 2

23
votes

Not directly but there is an easy way to do it.

You can use the online Colorer-take5 HTML generator to produce the highlighted code. Afterwards just copy/paste it into your document, but don't forget to change the font to Courier New and probably reducing the size of it and you're done.

Here is an example for Java code and using the Eclipse style: alt text

2
  • 8
    Great idea, but I would suggest pygments.org to generate colored HTML (with multiple styles) and Consolas as font.
    – schlamar
    Commented Dec 12, 2012 at 21:33
  • I used Microsoft word for this. Pasted my code in Microsoft word and then copy/paste in google docs
    – gogagubi
    Commented Jun 15, 2019 at 12:19
59
votes

With the new Add-Ons for Google Drive, you can get code highlighting with the Code Pretty add-on.

After installing it, you can access the add-on by clicking Add-ons → Code Pretty, → Format Selection:
Screenshot of using the add-on

This results in:
Result of using the add-on

7
  • 4
    This is a much better answer!
    – jackocnr
    Commented Sep 18, 2014 at 22:58
  • 3
    It is but in defense of @Lipis he answered back in 2010.
    – Asken
    Commented Mar 5, 2015 at 6:29
  • Doesn't support code on dark background Commented Jun 6, 2016 at 20:42
  • 1
    @IgorVaschuk, for me it does (for instance, I use PyCharm with the Darkula theme). I just need to cancel the highlighting in the google doc. Commented Oct 17, 2016 at 21:21
  • 1
    'CodePretty' is no longer around, CodeBlocks is another option
    – xhocquet
    Commented Jan 10, 2022 at 19:11

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