Here's a function that automatically calculates the right key signature spacing according to the staff size.