10

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!

4
  • I am not sure of your intent. Wouldn't simply \def\mycommand{\lipsum[1-2]} be enough? Commented Aug 28, 2011 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!
    – sdaau
    Commented Aug 28, 2011 at 21:42
  • If it crashes at the bracket try to put the argument in extra braces, e.g. {\lipsum[1-2]}.
    – user2574
    Commented Aug 29, 2011 at 6:10
  • Many years later: \unpacklipsum is now in the package lipsum by default -- just refer to the documentation. And the internal macros as mentioned below no longer exist.
    – user202729
    Commented Apr 2, 2023 at 0:33

2 Answers 2

14

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}
1
  • Many, many thanks @egreg - exactly the kind of explanation I needed :) Cheers!
    – sdaau
    Commented Aug 28, 2011 at 21:49
1

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
  \advance\count@\m@ne
  \def#3{}%
  \loop\ifnum\count@<#2\relax
    \advance\count@\@ne
%     \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}
1
  • 1
    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.
    – egreg
    Commented May 28, 2012 at 23:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .