# How to display the same text in various parts of the document?

In a long, long, long writing. I have a piece of text that repeats in several places/sections/chapters. When I'm editing, I change 2 or 3 words in that piece of text. The changes have to be applied to all its repetitions in the document. The question is, how can I automate this?

The solution I'm looking for would be something like:

Lorem ipsum \textthatrepeatselsehwhere{This text will be shown in several places}\label{textrep1} lorem ipsum.

Lorem ipsum \displaytextoflabel{textrep1} lorem lorem.


The output should look like:

Lorem ipsum This text will be shown in several places lorem ipsum.

Lorem ipsum This text will be shown in several places lorem lorem.

The solution might be obvious but I don't know how to do it. Thanks, any help will be appreciated.

-
my temporary solution is to use \input{textrep1} where textrep1.tex is a file with the text I want to repeat, works nice but not ideal to have many files. –  Juan Leonardo Sep 22 '12 at 14:31
–  Peter Grill Sep 22 '12 at 17:55

## Direct \newcommand

The easiest answer is \newcommand:

\documentclass{article}

\newcommand*{\textrepi}{This text will be shown in several places}
\newcommand*{\Lorem}{Lorem ipsum}

\begin{document}

\Lorem \textrepi lorem lorem. (!)

\Lorem\ \textrepi\ lorem lorem.

\Lorem{} \textrepi{} lorem lorem.

\end{document}


As you can see in the first line the space control is error-prone, because TeX ignores spaces after command names. This can be fixed by package xspace that uses a heuristics to find out, if a space should be inserted or suppressed.

\documentclass{article}

\usepackage{xspace}

\newcommand*{\textrepi}{This text will be shown in several places\xspace}
\newcommand*{\Lorem}{Lorem ipsum\xspace}

\begin{document}

\Lorem. \textrepi lorem lorem.

\end{document}


• The macro names for \newcommand are not free. Other characters than letters cannot be used directly.

## Indirect \newcommand

This method uses strings instead of commands to access the texts. In the example \newreptext remembers a text using a name. \reptext prints the text that is stored under the given name:

\newreptext{lorem}{Lorem ipsum dolor sit amet, \dots}
Some text. \reptext{lorem} consectetuer ...


Implementation and example:

\documentclass{article}

\makeatletter
\newcommand*{\newreptext}[1]{%
\begingroup % babel shorthand support
\csname @safe@actives@true\endcsname
\expandafter\endgroup
\expandafter\newcommand\csname reptext@#1\endcsname
}

\newcommand*{\reptext}[1]{%
\begingroup
\csname @safe@actives@true\endcsname % babel shorthand support
\@ifundefined{reptext@#1}{%
\@latex@error{\string\reptext{#1} is undefined}\@ehc
\endgroup
\textbf{??}%
}{%
\endgroup
\@nameuse{reptext@#1}%
}%
}
\makeatother

\newreptext{textrep1}{This text will be shown in several places}
\newreptext{Lorem}{Lorem ipsum}

\begin{document}

\reptext{Lorem} \reptext{textrep1} lorem lorem.

\end{document}


• The essence of the macro definition for \newreptext is quite short:

% \newreptext{<name>}{<text>}
\newcommand*{\newreptext}[1]{%
\expandafter\newcommand\csname reptext@#1\endcsname
}


Internally (the second argument, not yet seen at this stage) ist stored in the macro \reptext@<name>. The TeX primitive \csname allows the construction of arbitrary macro names. The macro name prefix reptext@ creates a kind of poor man's name space to avoid macro name conflicts.

• The example also adds support for active shorthand characters of package babel inside the <name> argument.

• Because of the syntax of \reptext{<name>}, the curly brace at the end avoids the problems that spaces after command names are ignored.

## LaTeX references

Also LaTeX referencing system can be used for this purpose as started in the question. \displaytextoflabel becomes a simple \ref and \textthatrepeatselsehwhere can handle the \label.

\documentclass{article}

\makeatletter
\newcommand*{\labeltext}[2]{%
\begingroup
\def\@currentlabel{#2}%
\label{#1}%
\endgroup
#2%
}
\makeatother

\begin{document}

Lorem ipsum \ref{textrep1} lorem lorem.

Lorem ipsum \labeltext{textrep1}{This text will be shown in several
places} lorem ipsum.

Lorem ipsum \ref{textrep1} lorem lorem.

\end{document}

• Because the text goes into the .aux file, caution is needed for fragile macros that need \protect.
• Two LaTeX runs are necessary to resolve the references.

## Package acronym

A package for acronyms seems a little bit overkill, it is easy to use and provide additional features. Just \acl is needed for referencing, because we always want the long text form of the acronym.

\documentclass{article}

\usepackage{acronym}

\newacro{textrep1}{This text will be shown in several places}

\begin{document}

\acl{Lorem} ipsum \acl{textrep1} lorem lorem.

\acl{Lorem} \acl{textrep1} lorem lorem.

\acrodef{Lorem}{Lorem ipsum}

\end{document}

• \newacro is similar to the \newcommand method, the acronym is saved in memory and available afterwards. It is not stored and remembered in the .aux file.

• \defacro is similar to the \label/\ref method. The acronym is stored in the .aux file and therefore available at \begin{document}.

The package acronym could also be used to help in organize the abbreviated texts. Example scenario:

The texts are stored in environment acronym in a separate text file reptexts.tex:

\begin{acronym}
\acro{rep1}{My repetition text}
\acro{relax}{Relax and calm down}
\acro{Disclaimer}{My standard disclaimer}
\acro{textrep1}{This text will be shown in several places}
\acro{Lorem}{Lorem ipsum}
\end{acronym}


Now the example of the previous section simplifies to:

\documentclass{article}

\usepackage{acronym}
\renewenvironment{acronym}{\let\acro\newacro}{}
\input{reptexts}

\begin{document}

\acl{Lorem} \acl{textrep1} lorem lorem.

\acl{Lorem} \acl{textrep1} lorem lorem.

\end{document}


And you can generate another document that lists all the texts with its abbreviation labels:

\documentclass{article}
\usepackage{acronym}
\begin{document}
\input{reptexts}
\end{document}


-

I interpret your question as suggesting that you a have long piece of text that's repeated several times in the document, and that you need to change two or three words within this text from one instance of the text to the other. To meet this need, you could define a macro that takes as its arguments both the long piece of text and the words that can change from instance to instance within the long piece of text.

The following MWE takes some filler text as the basic element of the long text and provides the means for changing three words from one invocation to another.

\documentclass{article}
\newcommand{\longtext}[3]{Lorem ipsum dolor sit amet, consectetur #1 elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute #2 dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat #3 non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.}

\begin{document}
\end{document}

-
Actually, the OP stated "The changes have to be applied to all its repetitions in the document." Please consider to edit your answer to something like "In case you'd want to provide several slightly different variants of some long text ..." –  lockstep Sep 22 '12 at 15:21
I agree that your answer is about a slightly different problem; still I'd upvote it if you'd change the introduction. –  lockstep Sep 22 '12 at 15:29

You can define a macro for this job.

\newcommand\mytext{This text will be shown in several places\xspace}


A MWE:

\documentclass{article}
\usepackage{xspace}
\newcommand\mytext{This text will be shown in several places\xspace}
\begin{document}
Lorem ipsum \mytext lorem ipsum.

Lorem ipsum \mytext lorem lorem \mytext.

\end{document}


-