How to put text on top of an image in LaTeX?

Text over images is used to give some small information about the image in the document, which is also nice to look at. In this tutorial, I will show you how you can do this in LaTeX.

Usually, we useĀ \includegraphics command to put images in LaTeX. But in this case, you need to use another environment which is tikzpicture and for that, you need to load the tikz package.

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\begin{document}

\begin{figure}[h]
    \centering
    \begin{tikzpicture}
        \node[anchor=south west, inner sep=0] (image) at (0,0) {\includegraphics[width=0.8\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[white, font=\bfseries] at (0.5,0.5) {Text on top of image}; % Adjust position and style of the text here
        \end{scope}
    \end{tikzpicture}
    \caption{Image with text overlay}
\end{figure}

\end{document}

Output:

text on top of an image.
You can also change the font style, color, or position of the image by changing this,

\node[<color-name>, font=<font-style>] at (<x>,<y>) {<text>}

Take a look.

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\begin{document}

\begin{figure}[h]
    \centering
    \begin{tikzpicture}
        \node[anchor=south west, inner sep=0] (image) at (0,0) {\includegraphics[width=0.8\textwidth]{example-image-a}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            \node[red, font=\itshape] at (0.5,0.9) {Hey! this is just a text}; % Adjust position and style of the text here
        \end{scope}
    \end{tikzpicture}
    \caption{Image with text overlay}
\end{figure}

\end{document}

Output:

text on top of an image.

Put text on top of an image with overpic

You can also use the overpic package to put text on top of an image in LaTeX. This package provides overpic environment, and with this environment, you can do this.

\documentclass{article}
\usepackage{xcolor}
\usepackage{overpic}

\begin{document}
\begin{figure}[h]
    \centering
    \begin{overpic}[width=.8\linewidth]{example-image-a}
       \put(35,35){\textcolor{red}{This is some text}}
    \end{overpic}
    \caption{Text on top of the image}
\end{figure}

\end{document}

Output:

text on top of an image.
You can also change the position of the text by changing the \put(<x>,<y>) command’s <x> and <y> values.

Leave a Reply

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