LTHooks – Recommended Ways to Use \everypar with LaTeX Hooks

everyparlthooks

Now that LaTeX has hooks, there is a recommendation to use the hooks interface for everypar? How should this be done in l3 programs?

\documentclass{article}
\ExplSyntaxOn
\newcounter{eplinenumber}
\setcounter{eplinenumber}{0}
\def\epindent{3cm}
\def\inscriptionprefix{Line:\hskip3pt}
\cs_set:Npn \mymod #1 #2 {\int_mod:nn{#1}{#2}}
\def\step_mod{5}

\NewDocumentEnvironment{inscription}{}
{
\let\annumber\empty
\expandafter\parindent\epindent
\bigskip
\obeylines
\everypar{
   { \stepcounter{eplinenumber}
    \ifnum\mymod{\theeplinenumber}{\step_mod}=0           \edef\annumber{\llap{\inscriptionprefix\theeplinenumber\hskip2pt}}\fi
    \ifnum\theeplinenumber=0 \edef\annumber{}\fi
    \ifnum\theeplinenumber=1 
      \edef\annumber{\llap{\inscriptionprefix\theeplinenumber\hskip2pt}}
    \fi
    \annumber
   }
  %}
 }
}
{\bigskip\setcounter{eplinenumber}{0}}
\ExplSyntaxOff

\begin{document}
\begin{inscription}
L(ucius) Albinius f(ilius)] 
[cum Galli ob]siderent Capitolium 
[virgines Ve]stales Caere deduxit 
[ibi sacra at]que ritus sollemnes ne  
[intermitte]rentur curai sibi habuit 
[urbe recup]erata sacra et virgines 
[Romam rev]
\end{inscription}
\end{document}

Best Answer

Please respect expl3 naming conventions and avoid \cs_set for functions not previously declared.

I don't know that this is 'the recommended way', but it uses the hooks documented in ltpara and is informed to some extent by the examples in section 2.3. But note that the section warns the examples are insufficiently careful for production use and that the code below is less careful than the examples.

Kant provides a minimal check that I've successfully removed the code from the hook.

'l3' inscription

Note that I couldn't find a way to insert code at precisely the same point, so I altered the paragraph indent box, which is documented as available for modification in paragraph hooks.

\documentclass{article}
% ateb: https://tex.stackexchange.com/a/702496/
% i gwestiwn Yiannis Lazarides: https://tex.stackexchange.com/q/702429/
\usepackage{kantlipsum}
\ExplSyntaxOn

\dim_new:N \l_lazarides_epindent_dim
\dim_set:Nn \l_lazarides_epindent_dim { 3cm }

\int_new:N \g_lazarides_eplinenumber_int
\int_gzero:N \g_lazarides_eplinenumber_int
\int_new:N \l_lazarides_modstep_int
\int_set:Nn \l_lazarides_modstep_int { 5 }


\cs_new_protected:Nn \lazarides_inscriptionprefix:n {
  \hbox_gset:Nn \g_para_indent_box 
  {
    \skip_horizontal:n { \l_lazarides_epindent_dim }  
    \hbox_overlap_left:n   
    {
      Line : \skip_horizontal:n { 3pt}
      \int_to_arabic:n { #1 }
      \skip_horizontal:n { 2pt }
    }
  }
}

\NewDocumentEnvironment{inscription}{}
{
  \dim_set_eq:NN \parindent \l_lazarides_epindent_dim
  \bigskip
  \obeylines
  \AddToHook { para / begin } [ lazarides ]
  {
    \int_gincr:N \g_lazarides_eplinenumber_int
    \int_compare:nNnTF
    {
      \int_mod:nn { \g_lazarides_eplinenumber_int } { \l_lazarides_modstep_int }
    } = { 0 }
    {
      \lazarides_inscriptionprefix:n { \g_lazarides_eplinenumber_int } 
    }{
      \int_compare:nNnT { \g_lazarides_eplinenumber_int } = { 1 }
      {
        \lazarides_inscriptionprefix:n  { 1 } 
      }
    }
  }
}
{
  \bigskip
  \int_gzero:N \g_lazarides_eplinenumber_int
  \RemoveFromHook { para / begin }[ lazarides ]
}
\ExplSyntaxOff

\begin{document}
\begin{inscription}
  L(ucius) Albinius f(ilius)] 
  [cum Galli ob]siderent Capitolium 
  [virgines Ve]stales Caere deduxit 
  [ibi sacra at]que ritus sollemnes ne  
  [intermitte]rentur curai sibi habuit 
  [urbe recup]erata sacra et virgines 
  [Romam rev]
\end{inscription}

\kant[2]
\end{document}