Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
    
I am not sure of your intent. Wouldn't simply \def\mycommand{\lipsum[1-2]} be enough? –  Gonzalo Medina 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! –  sdaau 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]}. –  Axel Sommerfeldt Aug 29 '11 at 6:10
add comment

2 Answers

up vote 11 down vote accepted

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}
share|improve this answer
    
Many, many thanks @egreg - exactly the kind of explanation I needed :) Cheers! –  sdaau Aug 28 '11 at 21:49
add comment

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}
share|improve this answer
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 May 28 '12 at 23:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.