auto label/index of scores

91 views
Skip to first unread message

Rob Leduc

unread,
Feb 15, 2021, 9:48:25 AM2/15/21
to Gregorio Users
I'd like to automatically label scores for page references and indexing by reading the gabc name field and creating a label/index entry with the "right" page number.  In this example, I just deal with labels for page references; a similar trick will work with \index eventually if I can get page references working.

The challenge in all these things is working with the LaTeX output routine to get the right page, as some commands are deferred until a page is shipped out and shipping out of a page may or may not be triggered by gregorioscore trying to write its first syllable.  So I may "think" the chant will start on one page, but in fact it starts on the next page.  Also, some scores will run over 2 pages or more. 

Is there a clever way to use \label (or modify its definition) to get the right page?

In the attached MWE, I try four ideas.  Using \grebeforeheader I save the value of \page in another counter called \chantpage.  Then I try to write labels at two different time points, using \greafterheaders and again using \afterscore, my own macro called after \gregorioscore.  At each time point, I write two labels, one based on the "saved" value \chantpage (using a modification of the \label command) and the other using the "\current" value of page at that "time".

So I have a timepoint of After Headers with labels based on "Saved" and "Current" page, and a timepoint of After Score with labels based on "Saved" and "Current"

The problem is with the second chant, Domine labia mea (in Festis) that appears to begin at the top of page 2 (identifiable by the above lines text Sive hoc modo in Festis solemnioribus) but really is probably called prior to page 1 being shipped out.  For this chant, of the four labels defined, only the last one - "Current page" "After Score" is 2; the others are all 1.  But in other cases, a chant that starts on one page and goes over a page break would also have a "Current page" "After Score" label as high or higher than the other labels, but not be the right label as the chant started on the prior page.

Or maybe this whole approach is doomed.  Most of the references I can find online deal with counters for sections or captions and the like, and not for trying to have a reference for terms in a paragraph of text. 

Any input, either on the following code or simply recommendations for a better angle of attack would be most appreciated! 

Rob

The MWE consists of a tex file, and two gabc files below.  Because I'm reading from gabc headers, I need separate files rather than using gabcsnippet.
--------------------------------------
pagemwe.tex
---------------------------------------
% !TEX TS-program = lualatex
% !TEX encoding = UTF-8

\documentclass[12pt]{book}
%\usepackage{fontspec}
\usepackage[oldstyle]{libertine}
\usepackage[verbose, a5paper,width=4.25in,outer=0.5in,top=0.75in,bottom=0.5in,includeheadfoot]{geometry}
\usepackage[autocompile]{gregoriotex}

%remove spaces macro
\makeatletter
\def\RemoveSpaces#1{\zap@space#1 \@empty}
\makeatother

%At GREBeforeHeaders, save the "current" value of the page number.
\newcounter{chantpage}
\grebeforeheaders{\setcounter{chantpage}{\value{page}}}

%Macro Chantlabel - modified label to use the stored \chantpage rather than "current" \page.
\makeatletter
\def\chantlabel#1{\@bsphack
  \protected@write\@auxout{}%
         {\string\newlabel{\expandafter\RemoveSpaces\expandafter{#1}}{{\@currentlabel}{\thechantpage}}}%
  \@esphack}
\makeatother

%Called by GREsetheadercapture{name} to create global label of the gabc name field without spaces.  
\usepackage{listofitems}
\newcommand{\mkchantlabel}[1]{%
 \setsepchar{!!!!}%
 \greadlist*\chantname{#1}%
  \itemtomacro\chantname[1]\thechantname%
}

%After header processing, write two labels - one with the "Saved" page number and one with the "current" page.
\greafterheaders{\chantlabel{\thechantname1}\label{\expandafter\RemoveSpaces{\thechantname}2}}

%Macro called after gregorioscore to create labels after a score, one using the "saved" page name and one using
% the "current" page (after gregorioscore macro, but when expanded/executed based on shipping pages?)
\newcommand{\afterscore}{
 \chantlabel{\expandafter\RemoveSpaces\expandafter{\thechantname}3}%
  \label{\expandafter\RemoveSpaces\expandafter{\thechantname}4}%
 \let\chantname= \relax%
 \let\chantpart=\relax%
}

%macros processed in order of gabc tag in gabc files.
\gresetheadercapture{name}{mkchantlabel}{string}
%\gresetheadercapture{office-part}{mkchantindex}{string}  


\usepackage{polyglossia}
\setdefaultlanguage[variant=ecclesiastic,hyphenation=liturgical]{latin}


\begin{document}
\grechangedim{afterinitialshift}{0.1cm}{scalable}
\grechangenextscorelinedim{7}{abovelinestextheight}{1cm}{scalable}
\gresetinitiallines{2}
\grechangedim{afterinitialshift}{0.1cm}{scalable}
\grechangenextscorelinedim{7}{abovelinestextheight}{1cm}{scalable}
\gregorioscore{Domine}\afterscore
\grechangedim{afterinitialshift}{0.2457cm}{scalable}

\grechangenextscorelinedim{1}{abovelinestextheight}{1cm}{scalable}
\grechangenextscorelinedim{5}{abovelinestextheight}{1cm}{scalable}
\gresetinitiallines{0}
\gregorioscore{Domine.Festis}\afterscore
\bigskip

\begin{tabular}{lrrrrr}
& & \multicolumn{4}{c}{Label Created By Time and Page Counter} \\
 & & \multicolumn{2}{c}{After Headers} & \multicolumn{2}{c}{After Score} \\
 Chant & Page & Saved & Current & Saved & Current \\
 \hrulefill
In Feriis & 1 & \pageref{Dominelabiamea(inFeriis)1}  & \pageref{Domine labia mea (in Feriis)2} & \pageref{Dominelabiamea(inFeriis)3} & \pageref{Dominelabiamea(inFeriis)4} \\
%
In Festis & 2 & \pageref{Dominelabiamea(inFestis)1}  & \pageref{Domine labia mea (in Festis)2}  & \pageref{Dominelabiamea(inFestis)3}  &  \pageref{Dominelabiamea(inFestis)4} \\
\end{tabular}
\end{document}
%%%%%%%%%%%%%%%%%%%%%
%  Domine.gabc 
%%%%%%%%%%%%%%%%%%%%%
name: Domine labia mea (in Feriis);
office-part:Antiphona;
commentary:AR3*;
%%
(c4) DO(j)MI(j)NE(j), lá(j)bi(j)a(j) me(j)a(j) a(j)pé(i)ri(j)es.(j)
<sp>R/</sp>(;)  Et(j)  (z)
os(j) me(j)um(j)  an(j)nun(j)ti(j)á(j)bit(j) lau(j)dem(j) tu(iz-)am.(j::)
%(::z)
%
<sp>V/</sp>( ) De(j)us(j) in(j) ad(j)ju(j)tó(jk)ri(j)um(j) me(j)um(j) in(j)tén(ij)de.(j)  
<sp>R/</sp>(;)
%
Dó(j)mi(j)ne(j) ad(j)ju(j)ván(j)dum(j) me(j) fe(j)stí(ij)na.(j) (:)
%
Gló(j)ri(j)a(j) Pa(j)tri,(j) et(j) Fí(j)li(j)o,(j) et(j) Spi(j)rí(j)tu(j)i(j) Sanc(ij)to.(j) (;)
%
Si(j)cut(j) e(j)rat(j) in(j) prin(j)cí(j)pi(j)o,(j) et(j) nunc(j), et(j) sem(j)per,(j) (,)
et(j) in(j) sǽ(j)cu(j)la(j) sæ(j)cu(j)ló(j)rum.(j) A(ij)men.(j) (;) Al(j)le(k)lú(ji~)ja.(i)
(::Z-)
%
<alt>A Septuagesima usque ad Pascha, loco {\rm Alleluia} dicitur:</alt>
Laus(j) ti(j)bi(j) Dó(j)mi(j)ne,(j) Rex(j) æ(j)tér(j)næ(k) gló(j)ri(j)æ.(i)
(::)
%%%%%%%%%%%%%%%%%%%%%%%%%
%Domine.Festis.gabc
%%%%%%%%%%%%%%%%%%%%%%%%%%
name: Domine labia mea (in Festis);
office-part:Antiphona;
commentary:AR2*;
%%
(c4) <alt> Sive hoc modo in Festis solemnioribus. </alt>  <sp>V/</sp>.
{De}(f)us(gh) in(h) ad(h)ju(h)tó(h)ri(h)um(h) me(ixghi)um(h)
in(h)tén(hg)de.(gh)
<sp>R/</sp>(;)  Dó(f)mi(gh)ne(h) ad(h)ju(h)ván(h)dum(h) me(ixghi) fe(h)stí(hg)na.(gh) (:)
Gló(f)ri(gh)a(h) Pa(h)tri,(h) et(h) Fí(h)li(h)o,(h) et(h) Spi(h)rí(ixghi)tu(h)i(h) Sanc(hg)to.() (;)
Si(f)cut(gh) e(h)rat(h) in(h) prin(h)cí(h)pi(h)o,(h) et(h) nunc,(ixghi) et(h) sem(hg)per,(gh) (,)
et(h) in(h) sǽ(h)cu(h)la(h) sæ(h)cu(h)ló(h)rum.(ixi) A(h)men.(g) (;) Al(h)le(ixi)lú(hg~)ja.(g) (::Z-)
<alt>A Septuagesima usque ad Pascha, loco {\upshape Alleluia} dicitur:</alt>
Laus(h) ti(h)bi(h) Dó(h)mi(h)ne,(h) Rex(h) æ(h)tér(h)næ(ixi) gló(h)ri(g)æ.(g)
(::)

Fr. Samuel Springuel

unread,
Feb 15, 2021, 2:21:30 PM2/15/21
to gregori...@googlegroups.com
> On 15 Feb, 2021, at 9:48 AM, Rob Leduc <rled...@gmail.com> wrote:
>
> I'd like to automatically label scores for page references and indexing by reading the gabc name field and creating a label/index entry with the "right" page number. In this example, I just deal with labels for page references; a similar trick will work with \index eventually if I can get page references working.
>
> The challenge in all these things is working with the LaTeX output routine to get the right page, as some commands are deferred until a page is shipped out and shipping out of a page may or may not be triggered by gregorioscore trying to write its first syllable. So I may "think" the chant will start on one page, but in fact it starts on the next page. Also, some scores will run over 2 pages or more.
>
> Is there a clever way to use \label (or modify its definition) to get the right page?

You might try one of our undocumented future features: `\optgabcAtScoreBeginning` (note name is unstable, as it doesn’t conform to our naming conventions). If this macro is defined, then it will be run at the beginning of a score after we’ve forced the new paragraph (all scores force themselves to be the start of a new paragraph, regardless of where `\gregorioscore` or `\gabcsnippet` appears in relation to the surrounding text). This is actually after the headers have been processed, and so may be closer to the right time than `\GreEndHeaders`. It’s still before we do any actual writing of the elements of the score, so it may still be too early, in which case we’d need to establish a new hook in the code.

This feature hasn’t been officially released since we haven’t figured out which hooks should be present or are possible (see https://github.com/gregorio-project/gregorio/issues/7 for when this idea was first floated and then mostly abandoned due to the logistical impossibilities of one of the proposed hooks).


✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝✝
Fr. Samuel, OSB
(R. Padraic Springuel)
St. Anselm’s Abbey
4501 South Dakota Ave, NE
Washington, DC, 20017
202-269-2300
(c) 202-853-7036

PAX ☧ ΧΡΙΣΤΟΣ

rled...@gmail.com

unread,
Feb 15, 2021, 4:44:05 PM2/15/21
to gregori...@googlegroups.com

Thanks - I’ll give that a try!

R
> --
> Gregorio homepage: http://gregorio-project.github.io
> Archives for the old mailing list: http://www.mail-archive.com/gregori...@gna.org/
> To report a bug, please post to: https://github.com/gregorio-project/gregorio/issues
> ---
> You received this message because you are subscribed to a topic in the Google Groups "Gregorio Users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/gregorio-users/WPbwsmIqHrc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to gregorio-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/gregorio-users/3695B1B5-3E8C-4F33-BF9E-BFE5C1CC752E%40gmail.com.

Rob Leduc

unread,
Feb 16, 2021, 8:32:21 AM2/16/21
to Gregorio Users
I am happy to report that this worked perfectly in this example, with the "current" value of \page (i.e. standard \label command), executed using \optgabcAtScoreBeginning. 

I'm going to implement this in my full document; I'll report back if I find any other cases where it doesn't work.  It's just one line of code in the header, so I won't have much to edit when the name eventually changes.

R

Rob Leduc

unread,
Feb 18, 2021, 4:53:40 PM2/18/21
to Gregorio Users
So far, this has worked in every case I have generated.  It's mostly antiphons, so one or two line chants and not likely to cause many problems.  But there are a number of longer Responsaries and Hymns as well, and they all work flawlessly.

So I think this hook \optgabcAtScoreBeginning is a winner that should be worthy of future development.  Sure helped me out.

R

[snip]

No dia 15 de fev de 2021, às 13:21, Fr. Samuel Springuel <rpspr...@gmail.com> escreveu:

>> On 15 Feb, 2021, at 9:48 AM, Rob Leduc <rled...@gmail.com> wrote:
>>
>> I'd like to automatically label scores for page references and indexing by reading the gabc name field and creating a label/index entry with the "right" page number. In this example, I just deal with labels for page references; a similar trick will work with \index eventually if I can get page references working.
>>
>> The challenge in all these things is working with the LaTeX output routine to get the right page, as some commands are deferred until a page is shipped out and shipping out of a page may or may not be triggered by gregorioscore trying to write its first syllable. So I may "think" the chant will start on one page, but in fact it starts on the next page. Also, some scores will run over 2 pages or more.
>>
>> Is there a clever way to use \label (or modify its definition) to get the right page?
>
> You might try one of our undocumented future features: `\optgabcAtScoreBeginning` (note name is unstable, as it doesn’t conform to our naming conventions).
[snip]

Matthew Roth

unread,
Mar 17, 2025, 2:48:52 PMMar 17
to Gregorio Users
Screenshot 2025-03-17 at 13.28.02.png

Screenshot 2025-03-17 at 13.28.18.png
Fr Samuel, I am starting to work with indexing (as a starting point, by working with what Matthias does for the Nocturnale Romanum, albeit with a less complex command). Anyhoo, some of the scores start before the page, i.e. I try to keep \label together with its \phantomsection using the needspace package (and also from Matthias, \protected @ edef\ @ currentlabelname{}; spaces are to get around this looking like an email address) on page 1, but the score runs to page 2, and the page reference is to page 2, when it should be to page 1 (see the Adore te devote example). Or it just gives me the wrong page (both a page forward and a page backwards). Something similar happens with \pageref. 

is \optgabcAtScoreBeginning still usable? and err, what do I need to put in my preamble if so? (Rob had posted initially before TL 2021, and that's what I have.) Putting \optgabcAtScoreBeginning alone gave me an undefined control sequence…

I don't think that it's a hyperref problem. And it's definitely not (just) a GregorioTeX problem (and it's not quite the same problem as in my examples, I don't think)

 I don't think that sticking the label in the first syllable of the score with a <v> tag is a good idea. 
Reply all
Reply to author
Forward
0 new messages