# Expanding (edef) a lipsum command?

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?

Cheers!

• I am not sure of your intent. Wouldn't simply \def\mycommand{\lipsum[1-2]} be enough? Aug 28 '11 at 21:38
• Well, I am trying to basically use \lipsum[1-2] as an argument to another command; and that one apparently crashes at the bracket [; so I though, well, I'll just "extract the contents" into a new command without arguments, and then I'd use that.. \def does indeed work, but seemingly it will not expand down to the actual words contained in the \lipsum - I'm suspecting that would be the way to go if I want to cheat that original problem. Thanks for the feedback @Gonzalo Medina - cheers! Aug 28 '11 at 21:42
• If it crashes at the bracket try to put the argument in extra braces, e.g. {\lipsum[1-2]}.
– user2574
Aug 29 '11 at 6:10

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
\def#3{}%
\loop\ifnum\count@<#2\relax
\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}

• Many, many thanks @egreg - exactly the kind of explanation I needed :) Cheers! Aug 28 '11 at 21:49

And here I just wanted to post a supplement to this question - so as to those wandering what the use of this is. Have you ever tried striking out a lipsum paragraph? Both ulem's \sout{\lipsum[1]} and soul's \so{\lipsum[1]} would fail (the first specially because of not being able to deal with embedded \pars).

So, thanks to the answer, here is a piece of code that will properly typeset a \lipsum paragraph with ulem's \sout (soul's \so also works, but the words seem stretched - it can be directly replaced in the below MWE if you want to see its effects, too):

\documentclass{article}

\usepackage{lipsum}
\usepackage[normalem]{ulem}
\usepackage{soul}
% \usepackage{trace}

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

\begin{document}

% unpack a lipsum paragraph
\unpacklipsum{2}{2}{\mycommand}
% remove the \par token at end (hopefully) using \rempar:
\edef\tmpx{\expandafter\rempar\mycommand}
% check if the actual lipsum text appears in terminal
\typeout{\meaning\tmpx}

% typeset a working "\sout{\lipsum[2]}":
\expandafter\sout\expandafter{\tmpx}

\end{document}

• If you want no \par at the end, just load lipsum with the nopar option. Then \unpacklipsum{2}{2}\tmp\expandafter\sout\expandafter{\tmp} will work. May 28 '12 at 23:35