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}
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
– Martin Oct 19 '17 at 15:22\xpretoit works only for arguments without macros within (see second example)