# LaTeX for music

I need help how to construct in Latex the following:

This is one of the standard chord explanations in the music theory or practice. It is used to explain the harmonic processes in a piece of music. There are several different ways to describe the harmonic progressions, this one is the German standard, having roots in the Baroque music and Generalbaß (Figured Bass). As a music teacher, I need to put these symbols in the score to explain the music. This one is a combination of the tonal system (T, D, S, s, P, F...) and the Figured Bass numbers.

• Welcome to TeX.SE! Could you share your efforts? – JJM Driessen Sep 15 '15 at 10:12
• Could you tell us a bit about what that is? What are the rules (algorithms), how do you call this technique, where do you want to include it? It is a lot of guessing for us, not showing any effort by you and will not help future users as they won't query this site for "LaTeX for music". If this is some major field in music, you could give some reference to what it is. – LaRiFaRi Sep 15 '15 at 12:51
• @LaRiFaRi I see. This is one of the standard chord explanations in the music theory or practice. It is used to explain the harmonic processes in a piece of music. There are several different ways to describe the harmonic progressions, this one is the German standard, having roots in the Baroque music and Generalbaß (figured bass). en.wikipedia.org/wiki/Figured_bass As a music teacher, I need to put these symbols in the score to explain the music. This one is a combination of the Tonal system (T, D, S, s, P, F...) and the Figured Bass numbers. – octo Sep 15 '15 at 13:14
• For those puzzled by the musical content: the numerals represent intervals or distances of notes above the lowest sounding pitch, the bass note, and the series of such "figures" represents the motion of different contrapuntal lines, also called "voice leading." If the bass note is C then a 6-5 voice leading means A moving to G. Here, instead of specifying the actual bass note note, the writer specifies the harmonic "function", which in this case is the "dominant" chord moving to the "tonic" chord. If this was in the key of C major, the dominant chord is G and the tonic is C. – musarithmia Sep 15 '15 at 21:40
• It's not LaTeX, but there is a philosophical cousin to LaTeX for typsetting music called LilyPond that is very much worth checking out. – Alexis Sep 15 '15 at 22:47

You could use a matrix for this. I put everything in custom commands. Like this, you can adjust kerning, line thickness, arraystretch, font, and so on later.

% arara: pdflatex

\documentclass{article}
\usepackage{mathtools}
\newcommand{\mystack}[1]{\mspace{1mu}{\scriptsize\begin{matrix*}[l]#1\end{matrix*}}\mspace{4mu}}
\newcommand*{\myrule}[1]{~\rule[.5ex]{#1}{0.3pt}~}

\begin{document}
$D\mystack{7 \cr 6\myrule{2em}5 \cr 4\myrule{1.5em}3} T\mystack{5\\3}$
\end{document}


• Thank you. I understand that you have manually aligned the numbers and lines, correct? – octo Sep 15 '15 at 12:16
• @St.Palamas Well, not completely manual. You can switch from two to three or four entries in your matrix and get them correctly aligned. The only thing which stays manual is the distance for the lines. If they are following some rule, you should tell us. A lot of guessing here as you have not provided a single line of code or explanation. If you want to have just a subscript with my syntax, just use a 2x1 matrix with \null in first place. E.g. \mystack{\null\\3\myrule{1em}2} – LaRiFaRi Sep 15 '15 at 12:29

I explained this implementation in more detail in my blog: http://tobiw.de/tbdm/funktionssymbole (in German only)

Usage:

\function{<Function>}[<Bass Notes>][<Length>]{<Notes>}

• <Funtion> = T, D, S, Sp, s, …
The command allows also some special function symbols:
• DD for double dominant (maybe the wrong english terms, sorry for that, I know only the german ones …)
• /D for dominant without root
• SS for double sub-dominant
• <Length> = Length for space between and
• <Notes> = comma separated list of changes in notes (from lower two higher voices). Note that the order is reversed compared to my fist implementation.
• <Bass Notes> = changes in bass/lowest voice
• In all note definitions:
• - gets replaced by a dash
• . gets replaced by a space
• Numbers greater than 9 must be set in braces, e.g. {11} instead of 11

All notes/spaces/lines get the same with to make them align vertically.

The bass voice is always align at the same vertical position (kind of subscript). The first change in the list for the higher voices has a fixed vertical position too (kind of superscript) an all following changes get stacked above.

If you use another font (size) you must adjust all the ex and em values.

Results:

more or less complex voicings:

\function{T}{..7-8,3---4} \enskip
\function{D}{4---3, 6-5, ..7-8} \enskip
\function{D}{4----3, 6-5---, ..7-8-,{11}-----} \enskip
\function{s}{5,6}


special functions:

\function{DD}{8-7} \enskip \function{/D}{8-7} \enskip \function{SS}{5,6}


manually adjusted spacing between function symbol and note changes:

\function{D}[]{8-7} \enskip \function{D}[4-3]{} \enskip \function{D}[4-3]{8-7}
\par vs. \par
\function{D}[][-1pt]{8-7} \enskip \function{D}[4-3][-2.3pt]{} \enskip \function{D}[4-3][-1pt]{8-7}


with guides to show the base line etc.:

\showAllLines\mbox{}\enskip
% single change in upper voice
\function{D}{8-7} \enskip
% single chane in bass voice
\function{D}[8-7]{} \enskip
% change in upper and bass voice
\function{D}[8-7]{4-3} \enskip
% multiple changes
\function{D}[8--7]{4-3,6-5}


Full Code:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

% width of the boxes containing the note numbers
\dim_new:N \l_palamas_boxwidth_dim
\dim_set:Nn \l_palamas_boxwidth_dim { 0.5em }

% token for the line betwwen two numbers
\tl_new:N \g_palamas_line_tl
\tl_set:Nn \g_palamas_line_tl { \rule [ 0.7ex ] { \l_palamas_boxwidth_dim } { 0.3pt } }

% token for a space between two numbers / befor a number
\tl_new:N \g_palamas_space_tl
\tl_set:Nn \g_palamas_space_tl { \hspace { \l_palamas_boxwidth_dim }  }

% function to place the numbers/lines/space in an equal width box
\cs_new_protected:Npn \palamas_makebox:n #1 {
\makebox [ \l_palamas_boxwidth_dim ] [ c ] { #1 }
}
% function to porcess a change in notes (replace dash and dot)
\cs_new_protected:Npn \palamas_process_list:n #1 {
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_replace_all:Nnn \l_tmpa_tl { - } { \g_palamas_line_tl }
\tl_replace_all:Nnn \l_tmpa_tl { . } { \g_palamas_space_tl }
\tl_map_function:NN \l_tmpa_tl \palamas_makebox:n
\\
}

% some tokes for special function symbols
%% double dominat
\tl_new:N \g_double_dominat_tl
\tl_set:Nn \g_double_dominat_tl { \makebox[0.2em][l]{\raisebox{0.2em}{D}}D }
%% double dominat
\tl_new:N \g_dominat_without_root_tl
\tl_set:Nn \g_dominat_without_root_tl { \makebox[0em][l]{\raisebox{0.25ex}{\small\kern0.2em/}}D }
%% double dominat
\tl_new:N \g_double_subdominat_tl
\tl_set:Nn \g_double_subdominat_tl { \makebox[0.1em][l]{\raisebox{0.2em}{S}}S }

% main function
\NewDocumentCommand { \function } { m O{} O{0pt} m } {
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_replace_all:Nnn \l_tmpa_tl { DD } { \g_double_dominat_tl }
\tl_replace_all:Nnn \l_tmpa_tl { /D } { \g_dominat_without_root_tl }
\tl_replace_all:Nnn \l_tmpa_tl { SS } { \g_double_subdominat_tl }
\tl_use:N \l_tmpa_tl
\clist_set:Nn \l_tmpa_clist { #4 }
\clist_reverse:N \l_tmpa_clist
\kern#3
{
\tiny
\renewcommand { \arraystretch } { 0.75 }
\raisebox { -0.68ex } {
\begin{tabular}[b]{@{}l@{}}
% upper voices
\clist_map_function:NN \l_tmpa_clist \palamas_process_list:n
% shift/gap
\\[-1.2ex]
% bass line
\palamas_process_list:n { #2 }
\end{tabular}
}
}
}

\ExplSyntaxOff

% following is only for the demo
\usepackage{parskip}
\usepackage{xcolor}
\newcommand{\showBaseLine}{\makebox[0pt][l]{\color{magenta}\rule{\textwidth}{0.1pt}}}
\newcommand{\showXHeightLine}{\makebox[0pt][l]{\color{yellow}\rule[1ex]{\textwidth}{0.1pt}}}
\newcommand{\showMidLine}{\makebox[0pt][l]{\color{cyan}\rule[0.785ex]{\textwidth}{0.1pt}}}
\newcommand{\showCapHeightLine}{\makebox[0pt][l]{\color{magenta}\rule[1.57ex]{\textwidth}{0.1pt}}}
\newcommand{\showAllLines}{\showBaseLine\showXHeightLine\showMidLine\showCapHeightLine}

\begin{document}
\showAllLines\mbox{}\enskip
% single change in upper voice
\function{D}{8-7} \enskip
% single chane in bass voice
\function{D}[8-7]{} \enskip
% change in upper and bass voice
\function{D}[8-7]{4-3} \enskip
% multiple changes
\function{D}[8--7]{4-3,6-5}

% more complex examples
\function{T}{..7-8,3---4} \enskip
\function{D}{4---3, 6-5, ..7-8} \enskip
\function{D}{4----3, 6-5---, ..7-8-,{11}-----} \enskip
\function{s}{5,6}

% special function symbols
\function{DD}{8-7} \enskip
\function{/D}{8-7} \enskip
\function{SS}{5,6}

% adjust the space between function letter and notes
\function{D}[]{8-7} \enskip
\function{D}[4-3]{} \enskip
\function{D}[4-3]{8-7}

vs.

\function{D}[][-1pt]{8-7} \enskip
\function{D}[4-3][-2.3pt]{} \enskip
\function{D}[4-3][-1pt]{8-7}
\end{document}

• Thank you. Is it music that you have in mind, this particular typesetting? Generally: - with one number it is superscript. - when two numbers it is super/subscript. The space in between is on the horisontal centre of the letter. - when three numbers, the middle number is horisontally centered with the letter. Is it possible to achieve this? – octo Sep 15 '15 at 12:15
• @St.Palamas: I had music in mind, but as I learned the subscript is only used when the notes are in the bass line, which is not necessarily the case. E.g. the (not written) root of the chord could be the bass. But I’ll take a look at it. – Tobi Sep 15 '15 at 13:18
• @St.Palamas: I extended my code – that should fit your needs … – Tobi Sep 15 '15 at 14:07
• That is absolutely amazing - with all this DD and SS. Thank you for your effort! – octo Sep 16 '15 at 4:52
• Your welcome! If you like it, you may click the accept button ;-) – Tobi Sep 16 '15 at 6:20

Here I use \muse{Note}{space-separated-stack} where the space-separated-stack uses a syntax of x\-[y]z for individual lines. x and z are the numbers that display at the beginning and end of the left-aligned row, and y is the optional length of the bar (in multiples of 4ex, default multiple=1).

EDITED so that \- has optional rather than mandatory argument.

\documentclass{article}
\usepackage{stackengine}
\newcommand\sline[1][1]{~\rule[2pt]{4\dimexpr#1ex}{.6pt}~}
\newcommand\muse[2]{\let\-\sline\setstackgap{L}{.9\baselineskip}{\itshape%
#1 \scriptsize\raisebox{1pt}{\Centerstack[l]{#2}}%
}}
\begin{document}
\end{document}


The \muse macro is fully adjustable. Here, for example, I have revised it to reflect \tiny numbers, thinner \rule, less pre/post line rule spacing, narrower stacking gap...

\documentclass{article}
\usepackage{stackengine}
\newcommand\sline[1][1]{\,\,\rule[1.3pt]{4\dimexpr#1ex}{.4pt}\,\,}
\newcommand\muse[2]{\let\-\sline\setstackgap{L}{.85\baselineskip}{\itshape%
#1 \tiny\raisebox{1.5pt}{\Centerstack[l]{#2}}%
}}
\begin{document}