299

Google docs has a nice set of styles such as "Normal text" and "Header 1".

How do I add my own? I want to add a style called "code" for text that is programming code.

  • 42
    Exactly why I came to this question. :) I just wanted to make a style called Code and Inline code that wouldn't be paragraph style. – Robert Koritnik Oct 9 '13 at 13:58
  • 18
    I know the answer but I cannot add it because I don't have enough reputation. Basically you can utilize the Google Apps Scripts, here is what I did: pastebin.com/4pNBJ9pC. In order to enable it you need to create a new script: Tools >> Script Manager >> New and paste the code there. Then you will have to authorize the script and reload the document so that the menu appears on in the menu bar. Then select a patch of text and pick Extras >> Apply code style – Trident D'Gao Nov 5 '13 at 5:46
  • 3
    I know my answer but my 101 reputation is, apparently, not enough. As of 3/14', once you update the existing headers via "Update Heading X to match", GDocs will add an additional Heading Y. So, it starts with just Heading 1,2,3, but once you update Heading 3, GDocs will introduce Heading 4. Once you update Heading 4, GDocs will introduce Heading 5, etc. – JJ Rohrer Mar 3 '14 at 15:29
  • 7
    There's an Extension that does it: chrome.google.com/webstore/detail/paragraph-styles%20/… (I can't post an answer because of a bug in stackexchange - it says I need 10 rep to answer, I've got 103 rep, no Answer box avail.) – Chris Moschini Jul 21 '15 at 14:52
  • 1
    @AlekseyBykov answer helped me with this one. I've updated and fixed some issues with it here github.com/cr8ivecodesmith/google_docs_scripts/blob/master/… – cr8ivecodesmith Jul 6 '16 at 9:31
136

Right now it's not possible to add more styles or rename the existed ones, but you can modify them to match your needs.

For example you could style your code as you wish and then by selecting one of the headers and choosing the Update Heading # to match selection, that particular style could become your new "Code" style.

Then select something else and apply that heading and you're done.

Styles in google docs

  • 104
    downside is this will include "code" in my TOC. :/ I found that using Subtitle style for code prevents it from showing in TOC. – Mark Nadig Jul 30 '12 at 23:40
  • 9
    +1 @digger69 for that tip. Using “Subtitle” for code is the single best tip I've ever gotten from this StackExchange. Great for having line-height 1.15 throughout the document, but 1.0 for code! :D – ELLIOTTCABLE Jun 18 '13 at 15:35
  • 9
    As I also mentioned in digger69's answer, another downside is that this does not allow for inline styles; it has to be on its own line. Of course, short code, or more likely, code-related terms (such as tag, function or variable names) often want to live inline within a paragraph of text. – Drewdavid Nov 7 '14 at 17:56
  • 2
    is it true that it has to be "block" meaning the whole line or paragraph must be that same style -- it cannot be a line with code mixed with normal text like this: the window object – nopole Jan 24 '16 at 6:00
  • 12
    I just want to point out that it makes no sense that the "software experts" at Google have never added a feature to add a new style to Docs. That should have been one of their initial priorities in the decade or so this web app has been available. – Spencer Williams Oct 13 '16 at 18:18
56

As Lipis poins out, you still cannot create your own styles. So, you are left to repurpose an existing one. The downside to using Heading is this will result in "code" in my Table of Contents (TOC). :/ I found that using Title and Subtitle styles for code prevents those styles from showing in TOC.

  • 3
    The main downside of this approach is that while you can use ⌘+Alt+6 to use this layout if you override 6'th heading, there is no matching keyboard shortcut for the Subtitle style. – Amit Kotlovski Jun 14 '14 at 10:08
  • 5
    The other note is that this doesn't seem to work inline, has to be on its own line (true of all styles I imagine) – Drewdavid Nov 7 '14 at 17:54
27

A lot of the solutions don't work inline. This is a cleaned-up solution offered by @AlekseyBykov using Google App Scripts to add a custom menu action:

  1. Create a new script (Tools > Script Editor)
  2. Copy the following code into the editor:
// Add new menu item
function onOpen() {
  DocumentApp.getUi()
  .createMenu('Styles')
  .addItem('Format Code', 'formatCode')
  .addToUi();
}

// Define code styling
var style = {};
style[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.CONSOLAS;
style[DocumentApp.Attribute.FONT_SIZE] = 10;
style[DocumentApp.Attribute.BACKGROUND_COLOR] = "#DDDDDD";
style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#333333";
style[DocumentApp.Attribute.BOLD] = false;

// Apply code formatting
function formatCode() {
  var selection = DocumentApp.getActiveDocument().getSelection();
  if (selection) {
    var elements = selection.getRangeElements();
    for (var i = 0; i < elements.length; i++) {
      var element = elements[i];

      // Only modify elements that can be edited as text; skip images and other non-text elements.
      if (element.getElement().editAsText) {
        var text = element.getElement().editAsText();

        // Style the selected part of the element, or the full element if it's completely selected.
        if (element.isPartial()) {
          text.setAttributes(element.getStartOffset(), element.getEndOffsetInclusive(), style);
        } else {
          text.setAttributes(style);
        }
      }
    }
  }
}
  1. Assign a trigger to run the onOpen function "On open" (Edit > Current Project's Triggers)
  2. After authorizing the script, reload the original document
  3. Use the new menu item to format selected text (Styles > Format Code)
  • 2
    Excellent and worked perfectly. For a more 'Slack' backtick style: style[DocumentApp.Attribute.BACKGROUND_COLOR] = "#FFEEEE"; style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#880000"; – Jim Morrison Jun 27 '18 at 21:00
  • 1
    excellent stuff. I didn't like that the background was missing between words, so I modified it to put the content in a table cell with a solid background: gist.github.com/fatso83/ffb7871c537e04d9ce95f66fccc30495 – oligofren Sep 19 '18 at 15:22
  • super nice script, thanks! I could now extend it to support more styles – Unreality Oct 22 '18 at 6:33
15

The gratis Google Docs extension Paragraph Styles+ allows to create custom paragraph styles (plus decimal system outline numbering for headings and a table of contents with page numbering).

I just tested it, and here's my first impression:

  • Good start. For small-ish documents, it should be good to use :-)
  • However it runs quite slow, even though recent versions improved on the performance already.
  • The UI does not integrate with the Google Docs style UI. Probably not possible otherwise in the Google Docs API.
  • The original Google Docs style feature does not know about custom styles, considering all text with custom styles as having the "Normal text" format. You can use both in parallel, but note that the "Clear formatting" button (looks like "Tx") will remove custom style formatting from selected text.
  • It seems to store your character styles on an external web service, means you have to entrust them access to your documents the way you entrust them to Google now …

Source: Acknowledging the comment by Chris Moschini above, who mentioned it first.

  • 6
    Are there any data security risks involved with installing this kind of docs extension, or has the code been verified by Google to be non-malicious? – Gruber Aug 19 '15 at 7:42
  • 7
    These permissions appear to enable, for example, your documents to be sent to or shared with others without further notice. That certainly scares me. – Reece Mar 21 '16 at 18:07
  • As it should, @Reece! Thanks for the pointer. – Cheeso Jan 31 '17 at 18:15
11

Until recently, it was possible to edit the CSS (stylesheet) and HTML source of a document. It took some work, but if you knew HTML and CSS you could add a CSS class to your doc, and then edit the HTML to use it, e.g. in the HTML

<span class='booktitle'>Infinite Jest</span>

and in the CSS

.booktitle { font-style:italic; }

Google has more or less phased out this capability with their new format, forcing old docs to the new format. But, I've heard there are ways to keep docs in the old format, so there may be some hope.

  • 1
    I would love more info about how to do this. – Stew Sep 21 '12 at 16:38
  • 2
    Doesnt work, google now strips all css except inlines – vs4vijay Jun 13 '13 at 7:16
  • 2
    The script posted above is almost the same as this feature. After installing, you can simply edit the style object as needed. It's javascript, so slightly different than CSS, but almost identical. Note: it does not make any external calls, send or load data from elsewhere. – Jonathan Cross Jun 8 '17 at 10:16

protected by Community Jul 16 '13 at 7:17

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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