[Tex/LaTex] Place text on lower right corner of picture

graphicsoverpic

I am trying to place some text (a credit-line) on the lower right corner of (many) pictures. The text should be right-aligned with the right edge of the picture. I tried things like

\documentclass[a3paper,english]{article}
\usepackage[percent]{overpic}

\begin{document}

    \begin{overpic}[width=0.5\textwidth]{#1}%
        \put (98,2) {\makebox(0,0)[r]{\footnotesize some text}}%
    \end{overpic}%

\end{document}

where #1 is the filename of the figure.
This seems to work if the the picture has landscape format. If it is in portrait the text appears outside on the right. It seems that the coordinates used by put are the same in x- and y-direction and determined by the larger of the dimensions of the images.
Is there a way to correctly right justify the text with the right edge of the pictures?

Best Answer

I see no need for extra packages in this case (if I understand the question). You can use just

\includegraphics[width=5cm]{example-image-a}% <--important!
\makebox[0pt][r]{\footnotesize some text}

The comment sign is important in order to avoid spurious space. A little \raisebox may be helpful. So e.g. the following code

\documentclass[convert={size=640}]{standalone}

\usepackage{graphicx}

\begin{document}

\includegraphics[width=5cm]{example-image-a}%
\makebox[0pt][r]{\footnotesize some text}

\includegraphics[angle=90,width=5cm]{example-image-a}%
\raisebox{1pt}{\makebox[0pt][r]{\footnotesize some other text}}

\end{document}

generates enter image description here