[Tex/LaTex] Typesetting guitar chord diagrams in a songbook

musicpositioning

I am writing a songbook and I would like to insert guitar chord diagrams in the appropriate positions above the lyrics of each verse. I have found the musixguit package capable of drawing nice diagrams which I would like to include in my songbook (showing fret numbering for barre chords and possibly tabular notation of the chord) but I am not able to position them in accordance with the lyrics.

\documentclass{article}
\usepackage{musixguit}
\begin{document}
\raiseguitar {14}
\NOtes\guitar {Cm $^7$}{2}x-----\gbarre1\gdot33\gdot52
\guitar G{}o-----\gbarre3\gdot25\gdot35\gdot44\en
*Lyrics of the first *verse...
\end{document}

How to place the diagrams above their intended positions (denoted by asterisks)? Would it be possible to define a new command for in-line insertion of predefined chords into the appropriate positions in lyrics like in the following example?

\chord{Cm7}Lyrics of the first \chord{G}verse...

Thank you.

Best Answer

The chords only have to be defined via \guitarchord once, and can be reutilized as needed. Rather that using \raiseguitar to set the vertical position, I use \setstackgap{L}{<baselinekip of stack>} to do so.

EDITED to address deficiencies noted by OP. Macros to use include

\guitarchord\<chordname>{<chord-definition>}

and

\showchord [<short entry text>]{\<chordname>}

where the optional argument is used if the chord text is shorter than the chord width. Note two lengths specified in the MWE that allow adjustments in alignment and minimum chord spacing.

RE-EDITED to provide optional syntax that some may find preferable:

\chordline{\<chordname>}{<text>}

Here is the MWE:

\documentclass{article}
\usepackage{musixguit,stackengine}
\def\chordalign{\dimexpr2.2ex}% 2.2ex sets alignment of chord
\def\chordminwidth{\dimexpr6.5ex}% 6.5ex provides min. hskip for optional argument
\newcommand\guitarchord[2]{%
  \savestack#1{\kern\chordalign\NOtes\guitar #2\en}
}
\newcommand\showchord[2][\relax]{%
  \ifx\relax#1\relax\def\tmpuaw{T}\else\def\tmpuaw{F}\fi%
  \stackengine{\Lstackgap}{#1}{%
    \makebox[0ex][l]{#2}\kern\chordminwidth}{O}{l}{F}{\tmpuaw}{L}%
}
\newcommand\chordline[2]{\setbox0=\hbox{#2}%
  \ifdim\wd0>\chordminwidth\showchord{#1}#2\else\showchord[#2]{#1}\fi%
}
\raiseguitar {0}
\setstackgap{L}{2.7\baselineskip}
\begin{document}
\guitarchord\Cm{{Cm $^7$}{2}x-----\gbarre1\gdot33\gdot52}
\guitarchord\GM{G{}o-----\gbarre3\gdot25\gdot35\gdot44}
\showchord[Ly-]{\Cm} \showchord{\GM}rics of the first 
  \showchord[of 2]{\Cm} \showchord{\GM}verses ...

Alternative syntax:

\chordline{\Cm}{Ly-}
\chordline{\GM}{rics of the first}
\chordline{\Cm}{of 2}
\chordline{\GM}{verses ...}
\end{document}

enter image description here

While I don't recommend it because of potential danger, the OP followed up asking if the # character (and things like parentheses) could be incorporated into chord names. To do so, I make # catcode 11, and employ a lot of \csname nomenclature.

Here is such a version:

\documentclass{article}
\usepackage{musixguit,stackengine}
\def\chordalign{\dimexpr2.2ex}% 2.2ex sets alignment of chord
\def\chordminwidth{\dimexpr9ex}% 6.5ex provides min. hskip for optional argument
\newcommand\guitarchord[2]{%
  \expandafter\savestack\expandafter{\csname #1\endcsname}{%
    \kern\chordalign\NOtes\guitar #2\en}
}
\newcommand\showchord[2][\relax]{%
  \ifx\relax#1\relax\def\tmpuaw{T}\else\def\tmpuaw{F}\fi%
  \stackengine{\Lstackgap}{#1}{%
    \makebox[0ex][l]{\csname #2\endcsname}\kern\chordminwidth}%
    {O}{l}{F}{\tmpuaw}{L}%
}
\newcommand\chordline[2]{\setbox0=\hbox{#2}%
  \ifdim\wd0>\chordminwidth\relax\showchord{#1}#2\else
    \showchord[#2]{#1}\fi%
}
\raiseguitar {0}
\setstackgap{L}{2.7\baselineskip}
\begin{document}
\catcode`#=11
\guitarchord{F#m7(add4)}{{F$\sharp$m7(add4)$^7$}{2}x-----\gbarre1\gdot33\gdot52}
\guitarchord{GM}{G{}o-----\gbarre3\gdot25\gdot35\gdot44}
\showchord[Ly-]{F#m7(add4)} \showchord{GM}rics of the first 
  \showchord[of 2]{F#m7(add4)} \showchord{GM}verses ...

Alternative syntax:

\chordline{F#m7(add4)}{Ly-}
\chordline{GM}{rics of the first}
\chordline{F#m7(add4)}{of 2}
\chordline{GM}{verses ...}
\end{document}

enter image description here

Related Question