Tx · LaTeX & TikZ

How to Insert Red XXX Placeholders with a LaTeX Command

Published By the 9to5Science editorial team

At a glance

  1. 1. Load xcolor

    Add \usepackage{xcolor} in the preamble so LaTeX knows the color command.

  2. 2. Define a macro

    Use \newcommand to name the red XXX placeholder.

  3. 3. Insert it

    Call the macro wherever the placeholder should appear; call it twice or define a pair macro.

Use the xcolor package and define a command in the preamble. Then invoke that command wherever you need a red XXX. Define one macro for a single placeholder and, if you want one command to insert a pair, define a second macro that uses it twice.

\documentclass{article}
\usepackage{xcolor}

\newcommand{\redXXX}{\textcolor{red}{XXX}}
\newcommand{\redXXXpair}{\redXXX\ \redXXX}

\begin{document}
One placeholder: \redXXX.

A pair of placeholders: \redXXXpair.
\end{document}

How the command works

Put \usepackage{xcolor} before \begin{document}. The xcolor package provides \textcolor{color}{text}, which applies the chosen color to its second argument.

The definition

\newcommand{\redXXX}{\textcolor{red}{XXX}}

creates a command named \redXXX. Each time you type that command in the document, LaTeX prints XXX in red. The definition of \redXXXpair calls \redXXX twice, with \ between the two calls to ensure a visible space. Use \redXXX when you want one placeholder and \redXXXpair when you want two from a single command.

Change the text or color

Edit the macro definition to change what appears or which color is used. For example, \textcolor{blue}{TBD} would produce blue TBD text. You can also use a color defined by xcolor, or define your own color with \definecolor in the preamble.

Keeping the definition in the preamble makes the appearance easy to change throughout a document: update it once, and every use of the command follows the new definition.

Common mistakes

  • Forgetting the package: Without \usepackage{xcolor}, LaTeX may report that \textcolor is undefined.
  • Defining the macro in the wrong place: Put \newcommand in the preamble, before \begin{document}.
  • Using the command name without its backslash: Write \redXXX, not redXXX.
  • Expecting a single placeholder macro to print two copies: The definition of \redXXX prints one XXX. Use it twice or call the pair macro.
  • Using a command name that already exists: If LaTeX reports that the command is already defined, choose another name or use \renewcommand only when you intend to replace an existing definition.

The examples are intended for ordinary text. If you need a placeholder in a mathematical expression, check its spacing and font in that context; text and mathematical typesetting use different conventions.

References

  1. The xcolor package documentation

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