# Continuos lemma: expansion problems using reledmac and xstring

Preface

The following problem is part of a rather complex class file. Therefore I tried to make it as simple as possible to show the actual problem (MWE). I did a lot of experiments with \expandafter, \noexpand, \unexpanded etc., but had to realize, that I can not solve the problem myself.

Motivation

Working with reledmac I defined two macros to be able to use continued lemmas. \footnoteAnmerkung* holds the beginning of the lemma and the note text while \footnoteAnmerkung holds the end of the lemma (and an empty note text). The connection is done via the optional argument of both macros. Without an optional argument the non-starred version is as well used for "normal" single footnotes.

In addition a macro \truncatelemma automatically truncates a lemma if it is longer than six words (using macros from xstring).

Problem

When using \gpreto to combine start and end of the lemma, the complete start part is counted as one word within \truncatelemma (see result of example as is).

When using \xpreto instead everything is fine as long as I do not use any macros within the lemma. As a result the second sentence gives a lot of errors.

So, what is the correct way to obtain a working and stable solution?

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[]{reledmac}
\usepackage{xstring}

%%% A dummy macro
\newcommand{\mymacroA}[1]{\textbf{#1}}

\newcommand{\morenoexpands}{%
\let\mymacroA\relax
}%

\makeatletter
\newcommand{\truncatelemma}[1]{%
\begingroup%
\morenoexpands%
\normalexpandarg%
\StrCount{#1}{ }[\@numspaces]%
\ifnum\@numspaces>5%
\noindent\ignorespaces%
\StrBefore[3]{#1}{ }%
\ \ldots{}\ %
\StrBehind[\numexpr\@numspaces-2\relax]{#1}{ }%
\else%
\noindent\ignorespaces%
#1%
\fi%
\endgroup%
}%

\Xwraplemma[A]{\truncatelemma}

\def\footnoteAnmerkung{\@ifstar\@footnoteAnmerkung\@@footnoteAnmerkung}
\newcommand{\@@footnoteAnmerkung}[3][]{%
\ifcsdef{lemmastart#1}%
{\xdef\@tempA{#2}%
%%% \xpreto vs. \gpreto
\gpreto{\@tempA}{\csuse{lemmastart#1} }%
#2%
\edtext{}{%
\expandafter\lemma\expandafter{\@tempA}%
\Afootnote{\noindent\ignorespaces\csuse{anmtext#1}}%
}%
}%
{\edtext{#2}{%
\Afootnote{\noindent\ignorespaces#3}%
}%
}%
}%
\newcommand{\@footnoteAnmerkung}[3][]{%
\ifstrempty{#1}%
{% ERROR
}%
{%
#2%
\csgdef{lemmastart#1}{#2}%
\csgdef{anmtext#1}{#3}%
}%
}%
\makeatother

\begin{document}
\beginnumbering
\autopar

This is \footnoteAnmerkung*[N1]{almost the same text}{now we have a
split note} with some text in between and \footnoteAnmerkung[N1]{to show   the
problem with expansion}{}. That's it.

%% Not working with \xpreto above
This is \footnoteAnmerkung*[N1]{almost \mymacroA{the same} text}{now we have a
split note} with some text in between and \footnoteAnmerkung[N1]{to show the
problem with expansion}{}. That's it.

\endnumbering

\end{document}

• what is exactly the need? what do you mean by continued lemma? – Maïeul Oct 18 '17 at 14:34
• Well, within a Virtual Research Environment researchers transcribe manuscripts page-by-page. Some critical notes, however, span over more than one page. Therefore researchers mark the first part and give the note text and then on the next page they mark the second part. – Martin Oct 19 '17 at 7:45
• you means that critical notes, in your case, are not notes made by scholar by notes present in the manuscripts that you want to reproduce on an other page? I don't still understand. Could you send a example of the expected result. – Maïeul Oct 19 '17 at 11:53
• Well, what I need is a way to combine two (critical) footnotes into one using the one note text. If you compile the example above you can see the desired result. This is already working fine. My problem is, that in addition I want to use another macro (in my case for truncation) to be applied on the combined lemma. This is not working at the moment as the first (stored) part of the lemma is considered as one word. In my opinion this is a expansion problem. However, when I tried to solve it with \xpreto it works only for arguments without macros within (see second example) – Martin Oct 19 '17 at 15:22
• I still does not understand to which need this feature is for (e.g what do you want do do scholarly), but I understand better what you want to do in computer problem. – Maïeul Oct 19 '17 at 15:33

Ok.

# Your problem (with the second example)

1. Your lemmastart#1 defined called on \@@footnoteAnmerkung is defined with \csgdef. So it is expanded as almost \mymacroA {the same} text. (as you can see using \csshow.
2. You call it on a \xpreto some want to fully expand it when defining the macro \@tempA (it is as you call it in a \xdef of \edef and not in a \gdef or a \def)
3. But ultimatly, that calls \bfseries (via mymacroA and \textbf). And so as \bfseries is not "safe in an expansion-only context", your \xpreto can't work in the second case (

\edef\toto#1{\bfseries#1} \toto{A}

does not work. But it's work with \def) 4. So your problem is to transform \textbf to something you can use safely before calling your \xpreto. 5. So you must add your \morenoexpands in the beginning of \@@footnoteAnmerkung. 6. I think your \morenoexpands should be defined as

\newcommand{\morenoexpands}{%
\let\textbf\@firstofone
}%

1. However, in this case, you lost strong (of course). But as you can test, \StrCount{\textbf{sncf toto}}{ } is not possible. And you can't preserve the bf and other thing using xstring splitting macros (or I don't see how to do it). Maybe it is possible using LuaTeXengines.

So the best I see you can do is

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[]{reledmac}
\usepackage{xstring}

%%% A dummy macro
\newcommand{\mymacroA}[1]{\textbf{#1}}

\makeatletter
\newcommand{\morenoexpands}{%
\let\textbf\@firstofone
}%
\newcommand{\truncatelemma}[1]{%
\begingroup%
\morenoexpands%
\normalexpandarg%
\StrCount{#1}{ }[\@numspaces]%
\ifnum\@numspaces>5%
\noindent\ignorespaces%
\StrBefore[3]{#1}{ }%
\ \ldots{}\ %
\StrBehind[\numexpr\@numspaces-2\relax]{#1}{ }%
\else%
\noindent\ignorespaces%
#1%
\fi%
\endgroup%
}%

\Xwraplemma[A]{\truncatelemma}

\def\footnoteAnmerkung{\@ifstar\@footnoteAnmerkung\@@footnoteAnmerkung}
\newcommand{\@@footnoteAnmerkung}[3][]{%
\ifcsdef{lemmastart#1}%
{\xdef\@tempA{#2}%
%%% \xpreto vs. \gpreto
\morenoexpands%
%\csshow{lemmastart#1}
\xpreto{\@tempA}{\csuse{lemmastart#1} }%
#2%
\edtext{}{%
\expandafter\lemma\expandafter{\@tempA}%
\Afootnote{\noindent\ignorespaces\csuse{anmtext#1}}%
}%
}%
{\edtext{#2}{%
\Afootnote{\noindent\ignorespaces#3}%
}%
}%
}%
\newcommand{\@footnoteAnmerkung}[3][]{%
\ifstrempty{#1}%
{% ERROR
}%
{%
#2%
\csgdef{lemmastart#1}{#2}%
\csgdef{anmtext#1}{#3}%
}%
}%
\makeatother

\begin{document}
\beginnumbering
\autopar

This is \footnoteAnmerkung*[N1]{almost the same text}{now we have a
split note} with some text in between and \footnoteAnmerkung[N1]{to show   the
problem with expansion}{}. That's it.

%% Not working with \xpreto above
This is \footnoteAnmerkung*[N1]{almost \mymacroA{the same} text}{now we have a
split note} with some text in between and \footnoteAnmerkung[N1]{to show the
problem with expansion}{}. That's it.

\endnumbering

\end{document}


# Some remarks on reledmac use

1. You should not use noindent in your Afootnote commands. First of all, as your lemma is printed after ligne number, that makes no sense. And also, reledmac does not add indent in critical footnotes (except if we ask explicitly vis Xparindent
2. I think it will be simpler for user to have a long critical footnote that to split it in two using label. And you would have a more light code.
3. In any case, if you want to have a start and a end critical footnote command, you should use \edlabel and \xxref command to get correct lines number.
• > In any case, if you want to have a start and a end critical footnote > command, you should use \edlabel and \xxref command to get > correct lines number. In the productive solution I do that. However, as this is not the problem here, I removed it from the example. – Martin Oct 20 '17 at 9:04
• Nice, that is ok. – Maïeul Oct 20 '17 at 12:30