Square Pegs now fit into Round Holes

One hurdle I’ve often encountered with Data Merge is when text fields which have to fit in a given space. Examples may be speech bubbles, headlines or nametags.

Normally, the way to proceed with this variety of project would be to determine the longest word in the database and set the size of the font to fit the longest word. This is fine for large words, but tends to make small words look out of place.

Ideally, it would be nice for the type to fit according to the size of the name, but that isn’t possible… is it? Well, with GREP styles, it is.

This example features a name-badge which contains first names which range between 3 to 11 letters long. The first thing to do is determine how many letters will fit within the given space. To do this, use the widest letter of the font being used (M in this example) and type the letter 3 times until the type fits the box as desired. Now create this as a paragraph style (e.g. Badgetype).

From here, type an additional letter. There will be an overflow warning – this is intentional. From here, use Ctrl-A to select the type and make a new character style called mynameis4. Make sure that the “Apply Style to Selection” checkbox is ticked; and that the “Preview” checkbox is ticked. Within the character style, only edit the Advanced Character Formats and change the items in this panel to distort the type accordingly until the type comes back into view. Once the type comes back into view and appears as desired, click OK.

Now, type another letter and once again an overflow warning will appear. Again, use Ctrl-A to select the type and duplicate the character style to create a new character style called mynameis5. Again, go to the Advanced Character Formats and again distort the type until it appears as desired. Click OK.

Repeat this process until all 11 letters have character styles assigned to them. Apart from [None], there should be 8 character styles from mynameis4 to mynameis11. From here, select all the type again and select the character style [None] to remove the style. The type will overflow once again, but this edit the Badgetype Paragraph style.

From this dialog box, go to the GREP styles options and make a new GREP style. From here, select the mynameis4 Character style; and in the GREP search below, type:


(four fullstops)

make another GREP style and select the mynameis5 Character style; and in the GREP search below, type:


(five fullstops)

Repeat this process until all remaining styles have GREP styles associated with them. That is, a seven letter style will have seven fullstops in the GREP search. The only exception is “mynameis11” which will have a “+” after the last fullstop. Click OK once this is done – the GREP styles dialog portion of the dialog box should look like the screengrab below.

If this is done correctly, the type should change size whenever a letter is added so that the type becomes incrementally smaller as type is entered.

To make this work with variable data, simply select the database and then insert the textfield and apply the paragraph style Badgetype to it.

Voila! I have included the PDF used for this tutorial which contains attachments of the InDesign and database files so that people can see exactly how this was constructed.


  1. Ho-Lee Balls… This took me a while to fine tune exactly the way I needed it,but WOW… this is EXACTLY what I have needed… To think about all of the countless hours I have wasted doing this manually… You sire, are a God amongst men.

  2. Interesting trick. I would make one change, to make it more approachable: instead of “….” (for periods, 5 etc) use proper grep repetitions: “.{4}”, “.{5}” etc.

  3. Hi,

    I’ve been trying to resize text to fit a maximum frame size but my longest degree name is 50 characters long. Do I have to create 50 character styles or is there a faster way?

    I’m doing a data merge from an Excel spreadsheet to produce degree certificates and most work fine except a few with very long degree names.



    • The initial article was an exaggerated example in a case where there is a small trim size but large letters, but to make headlines fit into a headline space, you can use Alecat’s trick above. That is, make two styles (one without being condensed, one condensed) and use the .{1,35} (this will leave anything under 35 characters long); and then use the .{36,} (this will condense everything over 36 characters).

  4. Hi,

    So I created two character styles for the degree title, one large and one small. Then I created two GREP styles associated with the character styles, one with .{1,50} and the other with .{51,}. Then when I ran the data merge I got the following message and the text hadn’t resized at all.

    1) Page 14, 13 chars/1 words f

    What am I doing wrong?



  5. Hi,

    I spoke too soon. The GREP styles worked perfectly and now I’m getting that error message again.

    1) Page 14, 13 chars/1 words f

    What am I doing wrong?


  6. Can you leave a mark in the data merge set, such as (just to use an example) an asterisk to denote “this text should be bold” (or perhaps “all the text between these asterisks should be bold”), and then have the style replace the text on merge?

    For example: *Steve* would then render as “Steve” but bold?

    (If so this would save many hours…)

    • If the only content to bold would be Steve then it would be easier to make a GREP to look for the word Steve and change it to bold. Perhaps if you elaborate on exactly what your project involves a solution can be provided.

      • I’m laying out text for a card game in indesign. I want to be able to make certain text (and it is unique text) bold, certain text italic, certain text semi-bold, etc. What I am doing right now is puttnig in a bunch of codes that I can search for and do a find/replace to change it. So, for example:

        *Ace of Spades*
        The Ace of Spades is a high card. /(It beats all other cards.)/

        (This is not the game I’m laying out :))

        But the idea would be that the text between the *’s would be bold, and the text around the /’s would be italicized. A similar (but not uniform) pattern would be used on all the other cards in the game. Is that clearer?

      • Your method will work, but would work better with XML where the words to be italicised, bolded etc are already tagged. XML into indesign is another issue altogether and is a very steep learning curve too.

        Nevertheless, what you would like to do can be done, it just involves using a “find-between” GREP. InDesignSecrets.com has a page for this: http://indesignsecrets.com/findbetween-a-useful-grep-string.php

        This would involve merging to a new indesign file and then running GREP find/changes on the file.

        As for merging to a PDF and using GREP styles to do the same task, that is more complicated. GREP styles doesn’t remove text, it only formats the patterns it sees. However, you can make a format that gives the text no fill or stroke and sets its width to zero so it looks like there is nothing there.

  7. Es un truco muy interesante. ¡Tengo una pregunta!
    Es posible por medio de un script o estilo GREP adaptar el interlineado de un texto en un marco de texto.

    • I just checked and the indesign file is definitely there. It may just be in a spot that wasn’t seen. It is an attachment within the PDF, so when viewing the PDF in Adobe Acrobat, look for a little paper clip icon, and there should be three attachments – the font, the database, and the InDesign file.

      I hope this helps.

This site uses Akismet to reduce spam. Learn how your comment data is processed.