# Ensure minimum distance between two elements

Context: I'm typesetting a songbook in which there are occasionally "chords" typeset above one word, i.e. if I type someting like

This \chord{A}is my \chord{B}beautiful song


I'll get something that visually looks like that:

     A     B
This is my beautiful song


I have no difficulty doing that with essentially a combination of \raise and \hss. However, sometimes, when the \chord commands are too close together, the chords are typeset above one another (normal, they occupy no space):

This \chord{Am}i\chord{B7m}s \chord{C7}a mess


When I typeset this, the Am, B7m and C7 overlap to some extent.

My question is then: how can I make sure they don't overlap ? In other words, I'd like to insert a space before the chord is typeset long enough that it is positioned after the previous space has been flushed.

For the record, the definition of \chord is

\leavevmode\raise2.3ex\hbox to 0pt{\rule{0pt}{2.3ex}#1\hss


MWE:

\documentclass{article}

\newcommand{\chord}[1]{\leavevmode\raise2.3ex\hbox to 0pt{\rule{0pt}{2.3ex}#1\hss}}

\begin{document}
This \chord{A}is my \chord{B}beautiful song

This \chord{Am}i\chord{B7m}s \chord{C7}a mess

% I'd like something that looks about like this, but automatically
This \chord{Am}i\hspace{5mm}\chord{B7m}s \hspace{5mm}\chord{C7}a mess

\end{document}

• Do you know about packages like leadsheets? – cgnieder Sep 25 '15 at 11:31
• @clemens leadsheets does not seem to solve directly my problem, but it looks like something I should have had a look at before anyway. Thanks ! – Vincent Fourmond Sep 28 '15 at 19:54

My suggestion would be to change the input slightly. That is, instead of just supplying the chord to the macro \chord, supply the chord and the letter coverage:

\documentclass{article}

\newcommand{\chord}[1]{\leavevmode\raise2.3ex\hbox to 0pt{\rule{0pt}{2.3ex}#1\hss}}
\newcommand{\newchord}[2]{\begin{tabular}[b]{@{}l@{}}
\scriptsize #1 \\ \strut#2
\end{tabular}}

\begin{document}
This \chord{A}is my \chord{B}beautiful song

This \chord{Am}{i}\chord{B7m}s \chord{C7}a mess

% I'd like something that looks about like this, but automatically
This \chord{Am}i\hspace{5mm}\chord{B7m}s \hspace{5mm}\chord{C7}a mess

\hrulefill

This \newchord{A}{is} my \newchord{B}{beautiful} song

This \newchord{Am}{i}\newchord{B7m}{s} \newchord{C7}{a} mess

\end{document}


The tabular structure will expand to fit the widest length (chord or letter).

• This is of course a possibility, but, unfortunately, that would mean going again through a rather large number of songs that were already typeset... So upvoting, but I can't accept as it does not solve my problem ! – Vincent Fourmond Sep 28 '15 at 19:28