Tx · LaTeX & TikZ

How to Show or Hide Optional Text in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Create a switch

    Use LaTeX’s \newif command to define a Boolean switch for the optional text.

  2. 2. Set its default

    Turn the switch on or off before the text is processed.

  3. 3. Wrap the text

    Put optional material in a command that checks the switch.

  4. 4. Change it locally

    Use a group to apply a different setting temporarily.

To include material in a LaTeX source file but choose whether it appears in the document, define a Boolean switch and test it in a command. This is useful for optional explanations, instructor notes, draft text, or solutions that should be hidden in a student version.

The example below uses only built-in LaTeX commands. Set the switch to true or false before the optional text is processed.

Define a show-or-hide switch

\documentclass{article}

\newif\ifshowextra
\showextratrue % Change to \showextrafalse to hide the text

\newcommand{\optionaltext}[1]{%
  \ifshowextra
    #1%
  \fi
}

\begin{document}

This sentence always appears.

\optionaltext{This paragraph appears only when the switch is true.}

\end{document}

No additional package is required. The command \newif\ifshowextra creates a conditional named \ifshowextra and two commands for setting it: \showextratrue and \showextrafalse. The switch starts out false, so explicitly set its state in the preamble if you want a different default.

When LaTeX processes \optionaltext, it checks the switch. If the switch is true, the material in the command’s argument is typeset. If it is false, the material is skipped. Change \showextratrue to \showextrafalse to hide the paragraph without removing it from the source.

Use a different setting for one section

A switch can be changed locally inside a group. LaTeX restores its previous value at the end of the group:

\showextrafalse % Hide optional text in the main document

{%
  \showextratrue % Show it in this group only
  \optionaltext{This optional text appears here.}
}

\optionaltext{This text is hidden again after the group.}

Braces create the group in this example. You can use the same idea around a section or other block of content. The setting inside the group does not change the setting that was active outside it.

Show replacement text instead

Sometimes you want a different message when the optional material is disabled. Define a two-argument command with one branch for each state:

\newcommand{\optionaltextor}[2]{%
  \ifshowextra
    #1%
  \else
    #2%
  \fi
}

Use it like this:

\optionaltextor
  {The extended explanation is shown.}
  {See the full explanation in the instructor notes.}

The first argument is used when the switch is true; the second is used when it is false. Both arguments must be supplied, even if one is empty: write {} for a branch with no text.

Common mistakes

  • Using the wrong switch command: \showextratrue turns the switch on; \showextrafalse turns it off. The condition itself is \ifshowextra and belongs in the conditional test.
  • Expecting a hidden argument to be ignored by the parser: LaTeX must still read the command argument. It must have balanced braces and valid argument syntax even when the switch is false. Hiding text does not make malformed LaTeX safe.
  • Putting verbatim material inside the command: Commands such as \verb and the verbatim environment generally cannot be used inside a macro argument like \optionaltext{...}. Use a suitable conditional environment or a package designed for verbatim content if you need to hide code samples.
  • Changing the switch in the wrong place: Set it before the conditional text is processed. Changing it later will not retroactively alter material already typeset.

For ordinary paragraphs and short blocks, a \newif switch is a simple way to maintain one source file with selectable text. For larger sections or verbatim content, choose a conditional environment suited to that material instead.

References

  1. The LaTeX2e unofficial reference manual
  2. The TeXbook, Donald E. Knuth

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