Create an invisible character or text in LaTeX

In this tutorial, I will show you how to create an invisible character or text in LaTeX. You can do this in three different ways.

  • Horizontal phantom (\hphantom)
  • Vertical phantom (\vphantom)
  • Horizontal and vertical space (\phantom)

Horizontal phantom in LaTeX

If you want to produce an invisible character or text with a width matching a specific character, use the \hphantom{<text>} command. This command generates horizontal space equivalent to the specified argument (<text>).

In the example below I’ve used \fbox so you can see how much space the invisible character is taking up.

\documentclass{article}
\begin{document}
  \noindent This text will be visible.\\
  \hphantom{This text will be invisible}\\
  This text will be visible.\\
  \fbox{\hphantom{Only horizontal space will matter}}\\
  This text will be visible too.
\end{document}

Output:

invisible text horizontally.

Vertical phantom in LaTeX

To generate an invisible character with the same height as a specific character or occupying identical vertical space, use the \vphantom{<text>} command. This command creates vertical space equivalent to the provided argument (<text>). Take a look.

\documentclass{article}
\begin{document}
  \noindent This text will be visible.\\
  \vphantom{This text will be invisible}\\
  This text will be visible.\\
  \fbox{\vphantom{Only vertical space will matter}}\\
  This text will be visible too.
\end{document}

Output:

invisible text vertically.

Horizontal and vertical space of an invisible text

If you want to get equivalent horizontal and vertical space based on the invisible text then you have to use the \phantom{<text>} command. This command generates equivalent vertical and horizontal space based on the provided argument (<text>).

\documentclass{article}
\begin{document}
  \noindent This text will be visible.\\
  \phantom{This text will be invisible}\\
  This text will be visible.\\
  \fbox{\phantom{Both vertical and horizontal space will matter}}\\
  This text will be visible too.\\[6pt]
  
  \underline{Here is the comparison:}\\[4pt]
  With \verb|\hphantom|:\\
  \fbox{\hphantom{Only horizontal space will matter}}\\

  With \verb|\vphantom|:\\
  \fbox{\vphantom{Only vertical space will matter}}\\
  
  With \verb|\phantom|:\\
  \fbox{\phantom{Both vertical and horizontal space will matter}}\\
\end{document}

Output:

 

invisible text both horizontally and vertically.

Leave a Reply

Your email address will not be published. Required fields are marked *