7

I was wondering if there's a way to do the following:

  • to wrap an excerpt of text in an environment and to label it;
  • to quote it (entirely) later in the same document, via a \ref command or similar.

By doing so, one could modify the wrapped text, without having to update all of the later references to it in the document.

I'm aware that a simple workaround would be to use \input{} both in the first and in the later occurrences of the wrapped sentence; I only wanted to know if there's something more specific.

More in detail, my purpose is to wrap some text that I have to cite in an answer letter to the reviewer of a submitted paper.

Suppose I wrote in a first version of the paper that:

it is clear that 2+2=5

Now the reviewer makes me pay attention to the fact that there's an error. I correct it and:

1 - in the revised version of the paper I write:

\wraptext{twoplustwo}{it is clear that 2+2=4}

provided that \wraptext{}{} is the command I was looking for;

2 - in the answer to the reviewer, I specify:

the error has been corrected, as you can see in the following reported excerpt:

\citewrappedtext{twoplustwo}

again provided that \citewrappedtext{} is the command to recall the excerpt.

This way, one can later modify again the sentence:

\wraptext{twoplustwo}{it is obvious that 2+2=4}

without having to correct the quotes, too.

  • 2
    Welcome to TeX.SX! You don't have to sign with your name since it automatically appears in the lower right corner of your post. Could you give an example of how you'd like to use it? (I would check out \savebox.) – Sean Allred Oct 17 '13 at 16:17
  • 1
    A simple way is to use \newcommand; can you show some short examples of what you want? – egreg Oct 17 '13 at 16:52
  • I think OP asked for an environment, but a \newcommand would definitely get the job done. Couldn't manage to express \newcommand as an environment though, even with packages like environ. – Sean Allred Oct 17 '13 at 17:07
  • 1
    @SeanAllred You probably missed to put \expandafter here and there at random. ;-) – egreg Oct 17 '13 at 20:07
  • @egreg Ha! I've more-or-less given up on learning the effective control of expansion. For this, I was trying to use expl3 for practice. – Sean Allred Oct 17 '13 at 20:20
7

While the answers "use a command" or "use an environment" seem to be obvious, here's a solution "with batteries included" (@GitHub, ZIP archive):

\documentclass{scrartcl}
\pagestyle{empty}

\newcommand\declquotedtext[2]{\expandafter\def\csname quotedtext@#1 \endcsname{#2}}
\newcommand\defquotedtext[2]{\declquotedtext{#1}{#2}#2}
\newcommand\usequotedtext[1]{\csname quotedtext@#1 \endcsname}

\begin{document}
  \defquotedtext{test}{This is a test text.}

  \declquotedtext{later}{And this is a text declared for later use.}

  This is something completely unrelated.

  \usequotedtext{test}

  \usequotedtext{later}
\end{document}

The command \defquotedtext will accept a label and contents, define the label (for use in \usequotedtext) and print the contents (to avoid repetition). As suggested by Ruben, \declquotedtext will do what \defquotedtext does, without printing the contents:

Result of compilation

As usual with commands, be careful with spaces after \defquotedtext and \usequotedtext.

Actually, I assumed that this has been packaged somewhere...

With credits to: What exactly do \csname and \endcsname do?

  • An option that makes it possible to only store a citation and make it reusable later would be nice too, i guess. (Or better: I can imagine some sitautions in which it could be helpful.) – Ruben Oct 17 '13 at 20:27
  • @Ruben: See my edit. – krlmlr Oct 17 '13 at 20:42
6

Doing this with an environment is quite easy, by using environ.

The actual implementation depends on what you really have in mind. What I propose can even go in a secondary file that's \input at \begin{document}. The order of the text snippets is unimportant.

\documentclass{article}

\usepackage{environ}

\makeatletter
\NewEnviron{storequote}[1]{%
  \@ifundefined{storequote@\detokenize{#1}}
    {\global\expandafter
     \def\csname storequote@\detokenize{#1}\expandafter\endcsname
     \expandafter{\BODY}}
    {\@latex@error{storequote error: key `\detokenize{#1}' already used}{\@ehd}}%
}
\newcommand{\getstoredquote}[1]{%
  \@nameuse{storequote@\detokenize{#1}}%
}
\makeatother

\begin{document}

\begin{storequote}{funny}
This is a funny quotation.
\end{storequote}

\begin{storequote}{bad}
This is a bad quotation.
\end{storequote}

\begin{storequote}{funny}
This will raise an error.
\end{storequote}

Some text just to call ``\getstoredquote{bad}'' and
some other text for calling ``\getstoredquote{funny}''.
Enough.

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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