# Musixtex strange bar positioning

I'm trying to construct scales using a For Each loop. The trouble is, that when I do so, the bar positioning seems to be shifted up - i can't tell why. The top picture shows what the extract should look like, and the one below shows what i actually get. Is it not possible to produce MusixTex programatically like this?

Thanks for any assistance.

    \begin{music}
\Largemusicsize
\instrumentnumber{1}        % a single instrument
\setname1{}
\setstaffs1{1}              % with one staff
\generalmeter{\meterfrac44} % 4/4 meter chosen
\nobarnumbers
\generalsignature{2}            % Two sharps
\startpiece
\notes
\foreach \scalenote [
evaluate={
\noteletter={"N","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v"}[\scalenote+2];
\isendbar=int(mod(\scalenote,4));
}] in{1,...,7}{
\qu{\noteletter}
\ifnum\isendbar=0 {
\en\bar
\notes
}
\fi
}
\en
\endpiece
\end{music}


I believe it's a problem of space in the code, but I couldn't pinpoint it.

This seems to work as you'd like to:

\documentclass{article}
\usepackage{musixtex}

\ExplSyntaxOn

\tl_const:Nn \c_stephen_notelist_tl {abcdefghijklmnopqrstuv}
\int_new:N \g__stephen_scale_start_int

\NewDocumentCommand{\makescale}{m}
{
% #1 is the starting point in the list of notes (1=c)
\int_gset:Nn \g__stephen_scale_start_int { #1 }
\int_step_function:nnN { 1 } { 7 } \stephen_scale:n
}

\cs_new_protected:Nn \stephen_scale:n
{
\exp_args:Ne \qu { \tl_item:Nn \c_stephen_notelist_tl { \g__stephen_scale_start_int+#1+1 } }
\int_compare:nT { \int_mod:nn { #1 } { 4 } == 0 } { \en\bar\notes }
}

\ExplSyntaxOff

\begin{document}

\begin{music}
\Largemusicsize
\instrumentnumber{1}        % a single instrument
\setname1{}
\setstaffs1{1}              % with one staff
\generalmeter{\meterfrac44} % 4/4 meter chosen
\nobarnumbers
\generalsignature{2}            % Two sharps
\startpiece
\notes
\makescale{1}
\en
\endpiece
\end{music}

\begin{music}
\Largemusicsize
\instrumentnumber{1}        % a single instrument
\setname1{}
\setstaffs1{1}              % with one staff
\generalmeter{\meterfrac44} % 4/4 meter chosen
\nobarnumbers
\generalsignature{-2}            % Two sharps
\startpiece
\notes
\makescale{0}
\en
\endpiece
\end{music}

\end{document}


You may need to add \usepackage{xparse} if you're not with the latest LaTeX kernel.

• Oh, thank you very much. Yes, I thought it was something to do with spaces, but couldn't figure put how to solve. I will try your solution - thanks again. – Stephen Jan 26 at 14:30
• @Stephen I fixed the issue of the repeated note. – egreg Jan 27 at 10:38
• thank you very much...even after reading the expl3 reference doc, i would never have figured out the difference between \c and \g assignments by myself. Thank you. – Stephen Jan 27 at 11:21