Text FormattingString Manipulations

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
hyc_n
Posts: 5
Joined: Thu Aug 30, 2012 9:45 am

String Manipulations

Post by hyc_n »

I don't know how to write three commands:
  1. to store append some string to some string S.
  2. to recall that S.
  3. to remove S (reset it to an empty string)
An example will be like this.

Code: Select all

I want to \store{this} and \store{that},
to \recall and \store{some more} too. \recall!
After \remove, nothing is \recall -ed.
the output will be like this
I want to and , to thisthatand too. thisthatsome more! After , nothing is -ed.
(The argument in \store may contain other commands such as \alph{enumii}. When recalled, the enumii counter should print its value at storing, not (its value at) recalling.)

Can anyone teach me how to do this? Thank you.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

String Manipulations

Post by cgnieder »

Here you go:

Code: Select all

\documentclass{article}

\makeatletter% make @ a letter
\def\@storagebin{} % this is our empty storage bin

% this commands adds tokenlists to the bin. This is done by first
% expanding its contents once before being redefined. In order to do that
% we need a chain of \expandafter s
\newcommand\store[1]{%
  \expandafter\def\expandafter\@storagebin\expandafter{\@storagebin#1}}

\newcommand*\recall{\@storagebin}% this simply calls the storage bin
\newcommand*\remove{\def\@storagebin{}}% this redefines the storage bin to be empty again
\makeatother% make @ other again

\begin{document}

I want to \store{this} and \store{that},
to \recall and \store{some more} too. \recall!
After \remove, nothing is \recall -ed.

\newcounter{test}
\setcounter{test}{12}
\store{\roman{test}}
\recall

\end{document}
Regards
site moderator & package author
hyc_n
Posts: 5
Joined: Thu Aug 30, 2012 9:45 am

String Manipulations

Post by hyc_n »

When recalled, the counter should print its value at storing, not its value at recalling.

Code: Select all

\remove
\newcounter{test}
\setcounter{test}{12}
\store{\roman{test}}
\recall
\setcounter{test}{16}
\recall
The result is "xii xvi", but I prefer "xii xii".

Code: Select all

\remove
\begin{enumerate}
\item Thank you.\store{\arabic{enumi}}
\end{enumerate}
\recall
Nothing recalled.

Can you help me a bit further? Thank you.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

String Manipulations

Post by cgnieder »

You need two changes for these tasks. For once the expansion at storage time must be exhaustive, i.e., with an \edef, and second storage and clearance must be \global:

Code: Select all

\documentclass{article}

\makeatletter
\def\@storagebin{}
\providecommand\expandonce[1]{%
  \unexpanded\expandafter{#1}}
\newcommand\store[1]{%
  \global\edef\@storagebin{\expandonce\@storagebin#1}}
\newcommand*\recall{\@storagebin}
\newcommand*\remove{\global\def\@storagebin{}}
\makeatother

\begin{document}

\remove
\newcounter{test}
\setcounter{test}{12}
\store{\roman{test}}
\recall
\setcounter{test}{16}
\recall

\remove
\begin{enumerate}
 \item Thank you.\store{\arabic{enumi}}
\end{enumerate}
\recall

\end{document}
Regards
site moderator & package author
Post Reply