Long story short, I have been using \patchcommand within a \whiledo loop to try to construct a string piece-meal from an old string, inserting certain characters along the way.
I cut off a bit of the original \myText, and define it as \fragment and try to add the fragment onto the end a string macro called \myNewText, for a certain number of iterations.
The problem is, every time \fragment is redefined, all the previous additions I made to \myNewText change with it. I want to find a way to append the current value of \fragment to \myNewText so that \myNewText will not change whenever \fragment does.
Here's a MWE:
\documentclass[]{article}
\usepackage{ifthen}
\usepackage{patchcmd}
\usepackage{xstring}
\newcommand\myText{Hac est sententia.}
\newcommand\myNewText{}
\newcommand\iterations{5}
\newcounter{Completions}
\begin{document}
\whiledo{\theCompletions < \iterations}
{
\stepcounter{Completions}
\StrLeft{\myText}{\theCompletions}[\fragment]
\patchcommand{\myNewText}{}{\fragment{'}}
\myNewText
}
\end{document}
The code above produces:
H'
Ha'Ha'
Hac'Hac'Hac'
Hac 'Hac 'Hac 'Hac '
Hac e'Hac e'Hac e'Hac e'Hac e'
What I want to produce is:
H'
H'Ha'
H'Ha'Hac'
H'Ha'Hac'Hac
H'Ha'Hac'Hac 'Hac e'
Actually, what the final product I'm working towards is even more complex, but for the purposes of the MWE to illustrate my current problem, that'd do nicely.



! Undefined control sequence. <argument> ... }{}{\fragment {'}} \par \mynewtext– David Carlisle Jul 09 '20 at 21:40