[Tex/LaTex] Expanding (edef) a lipsum command

expansion

Just trying to debug some stuff, and I thought about 'expanding' a \lipsum[1-2] statement using \edef into a new command (that would contain the "raw" text); strangely I cannot get it to work; this example:

\documentclass{article}

\usepackage{lipsum}

\begin{document}

% \lipsum[1-2] % works fine

\edef\mycommand{\lipsum[1-2]}
\mycommand

\end{document}

… crashes with:

! Undefined control sequence.
\GenericError  ...                                
                                                    #4  \errhelp \@err@     ...
l.9 \edef\mycommand{\lipsum
                           [1-2]}

How would I go about "unpacking" the 'contents' of the \lipsum command into a new command?

Thanks in advance for any answers,
Cheers!

Best Answer

You can't use \edef for commands accepting optional arguments; or, at least, not in this way. The paragraphs are stored in commands called

\lipsum@i
\lipsum@ii
\lipsum@iii

and so on. You can emulate your \edef by

\makeatletter
\def\unpacklipsum#1#2#3{%
  \count@=#1\relax
  \advance\count@\m@ne
  \def#3{}%
  \loop\ifnum\count@<#2\relax
    \advance\count@\@ne
    \edef#3{#3\csname lipsum@\romannumeral\count@\endcsname}%
  \repeat}
\makeatother

Then \unpacklipsum{1}{2}{\mycommand} will do what you need: this will store in \mycommand the paragraphs from 1 to 2. Paragraph number 42 can be stored in \mycommand with

\unpacklipsum{42}{42}{\mycommand}