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}
