Tx · LaTeX & TikZ

How to Replace Repeated Text with a LaTeX Macro

Published By the 9to5Science editorial team

At a glance

  1. 1. Identify repeated text

    Choose the exact phrase or name you want to keep consistent throughout the document.

  2. 2. Define a command

    Create a macro with newcommand and put the replacement text in its definition.

  3. 3. Use the command

    Replace each occurrence in your source with the command, then compile the document.

  4. 4. Update the definition

    Change the macro definition once to update every place where you used that command.

If you want to change a repeated phrase everywhere in a LaTeX document, the right approach depends on whether you are planning ahead or editing text that is already there. For reusable text, define a macro and use it in the document. For a one-time change to existing wording, use your text editor’s find-and-replace tool.

A LaTeX macro does not search through your source and replace ordinary words automatically. It expands only where you have written the macro’s command.

Define a command for repeated text

Use \newcommand to give a command a definition. In this example, \projectname produces the project name wherever it is used:

\documentclass{article}

\newcommand{\projectname}{Aster Survey}

\begin{document}
The \projectname{} begins in June.
Later, the results of the \projectname{} will be published.
\end{document}

The command name begins with a backslash. Its definition goes in braces after the command name. Each \projectname in the document produces the text assigned to that command when LaTeX processes it. To change all uses consistently, edit the definition (normally in the preamble) and compile again. A \renewcommand partway through the document affects only later uses in its scope.

The empty braces after \projectname in the sentences preserve the following space, because TeX ignores spaces immediately after a control-word command. They also terminate the command before any following letters; punctuation already ends the command name.

Replace phrases already in the source

Suppose your file already contains the phrase “Aster Survey” in many places, and you want to change them all to “Aster Sky Survey.” Defining a macro will not change those existing phrases: they are literal text, not uses of \projectname.

For a one-time edit, use your editor’s Find and Replace command. Search for the old phrase, enter the replacement, and review the matches before applying the change. If the phrase occurs inside other words, use whole-word matching if your editor provides it. For a long document or an important file, save a copy first or inspect the version-control diff afterward.

If you expect the wording to change again, replace the literal occurrences with a macro instead. A practical workflow is:

  1. Define a descriptive command such as \projectname.
  2. Use find-and-replace to change each exact occurrence of the phrase to \projectname{}.
  3. Compile the document and check the output, especially around punctuation and line breaks.

This gives you a single place to update the wording in the future. Use a command name that explains what the text means, rather than a generic name such as \x.

Use commands for variants

If several phrases should all produce the same wording, you can define a command for each meaning and give them the same replacement text. For example:

\newcommand{\shortname}{Aster}
\newcommand{\fullname}{Aster Sky Survey}

Use \shortname{} where the short name is appropriate and \fullname{} where the full name is needed. Keeping distinct commands is clearer than making one command stand for multiple ideas, even if their wording sometimes overlaps.

If the replacement text includes formatting, put that formatting in the definition. For instance, \newcommand{\emphterm}{\textit{important term}} consistently prints the phrase in italics. Avoid building a macro that takes over a standard LaTeX command unless you have a specific reason: changing standard commands can affect unrelated parts of the document.

Common mistakes

  • Expecting a macro to find plain text: A command only expands where you wrote the command. It cannot retroactively replace matching words in the source.
  • Using \newcommand for an existing command: LaTeX reports an error if the command is already defined. Choose a less likely name, or use \renewcommand only when you intentionally want to redefine a command you know exists.
  • Leaving old wording in place: After defining a macro, every occurrence you want controlled by it must actually use the macro.
  • Replacing partial matches by accident: Bulk replacement can change words that merely contain the searched phrase. Check the matches and use whole-word or exact-phrase options when appropriate.
  • Using a cryptic command name: Descriptive names make the source easier to maintain and reduce the risk of confusing one replacement with another.

For repeated text that may change, a macro provides consistent output and a single point of maintenance. For an isolated, immediate edit, a careful find-and-replace is usually faster.

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.