0

I looked at some posts about putting text on images but could not find an answer that helps me. I would be happy if someone could suggest a new way or simply a way to implement tikz or overpic better, even though \begin{tikzpicutre} or \begin{overpic} hasn't worked for me. Also when I try \put it places the text behind the images.

I am writing a scientific report and I need to number my images as A, B, C, D which are enclosed inside the same \begin{figure} \end{figure}. I assumed because I am using \subfloat tikz or overpic doesn't really work.

Is there a way for me to put text on the top left corner of each subfloat image and possibly choose the color, font and the background of the text?

\documentclass[12pt,a4paper]{report}
\usepackage[pdftex]{graphicx}
\usepackage{url} 
\usepackage[bookmarks, colorlinks=false, pdfborder={0 0 0}, pdftitle={<pdf title here>}, pdfauthor={<author's name here>}, pdfsubject={<subject here>}, pdfkeywords={<keywords here>}]{hyperref} 
\usepackage{mathptmx}
\usepackage{subfig} %for sub figures
\usepackage{float}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath} %use with \mathrm for non italic 
\usepackage{upgreek} % for non-italic greek letters
\usepackage{amssymb}
\usepackage{setspace}
\usepackage{indentfirst}
\usepackage[percentpdf]{overpic}
\usepackage[style=nature]{biblatex}
\addbibresource{main.bib}
\usepackage{hyperref}




\begin{document}

\begin{figure}[H]
    \centering
     \hfill
   \subfloat{\includegraphics[width=0.45\textwidth]{Images/img1.jpg}}
 \hfill
    \subfloat{\includegraphics[width=0.45\textwidth]{Images/img2.jpg}}
 
         \subfloat{\hbox{\hspace{0em}\includegraphics[width=0.49\textwidth]{Images/img3.png}}}
        \hfill
        \subfloat{\hbox{\hspace{0.5em}\includegraphics[width=0.49\textwidth]{Images/img4.png}}}
    \caption{ a) img1 b) img2 c) img3 d) img4}
    
\label{fig:imgs}
\end{figure}

\end{document}

4
  • Welcome. // Please complete your code (preamble etc.) so we can copy&run it. Answers may depend on what our preamble contains or misses.
    – MS-SPO
    Jun 9, 2022 at 12:26
  • 1
    @MS-SPO hello! thank you for the reply. i think, added the Preamble. Please let me know if I am still missing something.
    – zoeyay
    Jun 9, 2022 at 14:10
  • Perhaps package subfloat is a bit closer to what you want? ctan.org/pkg/subfloat
    – MS-SPO
    Jun 9, 2022 at 15:38
  • There are several ways to overlap text and images (see tex.stackexchange.com/questions/282619/… for example). Jun 9, 2022 at 22:14

1 Answer 1

1

This uses \llap, \makebox and \raisebox to overlap the text. The first step is to measure the width and height of the image using a savebox.

I used \subfloat for familiarity, but since you know the width, you could just as easily used a subfigure. The second image was overlaid with a complete subfigure caption.

\documentclass[12pt,a4paper]{report}
\usepackage{xcolor}
\usepackage[pdftex]{graphicx}
\usepackage{url} 
\usepackage[bookmarks, colorlinks=false, pdfborder={0 0 0}, pdftitle={<pdf title here>}, pdfauthor={<author's name here>}, pdfsubject={<subject here>}, pdfkeywords={<keywords here>}]{hyperref} 
\usepackage{mathptmx}
%\usepackage{subfig}% obsolete
\usepackage{subcaption}
\usepackage{float}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath} %use with \mathrm for non italic 
\usepackage{upgreek} % for non-italic greek letters
\usepackage{amssymb}
\usepackage{setspace}
\usepackage{indentfirst}
\usepackage[percentpdf]{overpic}
\usepackage[style=nature]{biblatex}
\addbibresource{main.bib}
\usepackage{hyperref}

\begin{document}

\begin{figure}[ht]% friends don't let friends use [H]
  \centering
  \sbox0{\includegraphics[width=0.45\textwidth]{example-image-a}}%{Images/img1.jpg}}
  \subfloat[]{\usebox0%
    \llap{\makebox[\dimexpr \wd0-2pt][l]{% left side
    \raisebox{\dimexpr \ht0-\ht\strutbox}[0pt][0pt]% top
      {\textcolor{red}{Text goes here}}}}}
  \hfil
  \sbox1{\includegraphics[width=0.45\textwidth]{example-image-b}}%{Images/img2.jpg}}
  \begin{subfigure}[b]{\wd1}
    \usebox1%
    \llap{\raisebox{\ht1}[0pt][0pt]{\begin{minipage}[t]{\dimexpr \wd1-4pt}
      \captionsetup{font={color=red}, skip=0pt, singlelinecheck=off}%
      \caption{A subfigure caption}
    \end{minipage}\hspace{2pt}}}
  \end{subfigure}
  
  \vskip\floatsep
  \subfloat[]{\includegraphics[width=0.49\textwidth]{example-image-c}}%{Images/img3.png}}}
  \hfil
  \subfloat[]{\includegraphics[width=0.49\textwidth]{example-image}}%{Images/img4.png}}}
  \caption{ a) img1 b) img2 c) img3 d) img4}% begins and ends with \par  
\label{fig:imgs}
\end{figure}

\end{document}

demo

6
  • this did not work for me so I decided to switch to \minipage. I did something like '''\begin{minipage}{0.45\textwidth} \llap{\makebox[0pt][l]{\textcolor{red}{A}}} \centering \includegraphics[width=\linewidth]{Images/usaf_1.26_2.jpg} \end{minipage}''' but now the text just appears on the top left corner of the minipage and not on the picture.
    – zoeyay
    Jun 11, 2022 at 15:32
  • The \llap has to go after the image. It overlaps to the left, so it has to be placed to the right of the image. Also, whatever goes first gets written over, so \rlap won't do. Jun 11, 2022 at 18:50
  • Still did not overlap the image, just appeared above it instead
    – zoeyay
    Jun 13, 2022 at 12:14
  • I don't know why you are trying so hard not to use a savebox. You need to know how wide and tall the image is in order to place the text from the lower right corner. Jun 13, 2022 at 13:39
  • I am not sure how to use it
    – zoeyay
    Jun 20, 2022 at 13:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .