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)
(::)